From bcdf8558fa4d65ee2095b6d8bfae08628e3c9156 Mon Sep 17 00:00:00 2001 From: Dmitry Avdeev Date: Fri, 6 Apr 2012 14:37:16 +0400 Subject: [PATCH] issues cache survival --- .../intellij/tasks/impl/TaskManagerImpl.java | 19 +++++++---------- .../com/intellij/tasks/TaskManagerTest.java | 21 +++++++++++++++++++ 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/plugins/tasks/tasks-core/src/com/intellij/tasks/impl/TaskManagerImpl.java b/plugins/tasks/tasks-core/src/com/intellij/tasks/impl/TaskManagerImpl.java index 72eb5e6fe3c3..74fb0ee91648 100644 --- a/plugins/tasks/tasks-core/src/com/intellij/tasks/impl/TaskManagerImpl.java +++ b/plugins/tasks/tasks-core/src/com/intellij/tasks/impl/TaskManagerImpl.java @@ -83,7 +83,6 @@ public class TaskManagerImpl extends TaskManager implements ProjectComponent, Pe private final WorkingContextManager myContextManager; private final Map myIssueCache = Collections.synchronizedMap(new LinkedHashMap()); - private final Map myTemporaryCache = Collections.synchronizedMap(new LinkedHashMap()); private final Map myTasks = Collections.synchronizedMap(new LinkedHashMap() { @Override @@ -184,7 +183,6 @@ public class TaskManagerImpl extends TaskManager implements ProjectComponent, Pe set.removeAll(repositories); myBadRepositories.removeAll(set); // remove all changed reps myIssueCache.clear(); - myTemporaryCache.clear(); myRepositories.clear(); myRepositories.addAll(repositories); @@ -236,20 +234,14 @@ public class TaskManagerImpl extends TaskManager implements ProjectComponent, Pe @Override public List getIssues(String query, boolean forceRequest) { List tasks = getIssuesFromRepositories(query, 50, 0, forceRequest); - synchronized (myIssueCache) { - myTemporaryCache.clear(); - myTemporaryCache.putAll(ContainerUtil.assignKeys(tasks.iterator(), KEY_CONVERTOR)); - } + if (tasks == null) return getCachedIssues(); + myIssueCache.putAll(ContainerUtil.assignKeys(tasks.iterator(), KEY_CONVERTOR)); return tasks; } @Override public List getCachedIssues() { - synchronized (myIssueCache) { - ArrayList tasks = new ArrayList(myIssueCache.values()); - tasks.addAll(myTemporaryCache.values()); - return tasks; - } + return new ArrayList(myIssueCache.values()); } @Nullable @@ -694,6 +686,7 @@ public class TaskManagerImpl extends TaskManager implements ProjectComponent, Pe private void doUpdate(Runnable onComplete) { try { List issues = getIssuesFromRepositories(null, myConfig.updateIssuesCount, 0, false); + if (issues == null) return; synchronized (myIssueCache) { myIssueCache.clear(); @@ -724,8 +717,9 @@ public class TaskManagerImpl extends TaskManager implements ProjectComponent, Pe } } + @Nullable private List getIssuesFromRepositories(@Nullable String request, int max, long since, boolean forceRequest) { - List issues = new ArrayList(); + List issues = null; for (final TaskRepository repository : getAllRepositories()) { if (!repository.isConfigured() || (!forceRequest && myBadRepositories.contains(repository))) { continue; @@ -733,6 +727,7 @@ public class TaskManagerImpl extends TaskManager implements ProjectComponent, Pe try { Task[] tasks = repository.getIssues(request, max, since); myBadRepositories.remove(repository); + if (issues == null) issues = new ArrayList(tasks.length); ContainerUtil.addAll(issues, tasks); } catch (Exception e) { diff --git a/plugins/tasks/tasks-tests/test/com/intellij/tasks/TaskManagerTest.java b/plugins/tasks/tasks-tests/test/com/intellij/tasks/TaskManagerTest.java index 20c8caa86972..cf4d09679e21 100644 --- a/plugins/tasks/tasks-tests/test/com/intellij/tasks/TaskManagerTest.java +++ b/plugins/tasks/tasks-tests/test/com/intellij/tasks/TaskManagerTest.java @@ -5,6 +5,7 @@ import com.intellij.notification.NotificationDisplayType; import com.intellij.notification.Notifications; import com.intellij.openapi.components.ServiceManager; import com.intellij.openapi.util.Ref; +import com.intellij.tasks.impl.LocalTaskImpl; import com.intellij.tasks.impl.TaskProjectConfiguration; import com.intellij.tasks.youtrack.YouTrackRepository; import com.intellij.tasks.youtrack.YouTrackRepositoryType; @@ -14,6 +15,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.Collections; +import java.util.List; /** * @author Dmitry Avdeev @@ -114,4 +116,23 @@ public class TaskManagerTest extends TaskManagerTestCase { TaskRepository[] repositories = myManager.getAllRepositories(); assertEquals(1, repositories.length); } + + public void testIssuesCacheSurvival() throws Exception { + final Ref stopper = new Ref(Boolean.FALSE); + TestRepository repository = new TestRepository(new LocalTaskImpl("foo", "bar")) { + @Override + public Task[] getIssues(@Nullable String query, int max, long since) throws Exception { + if (stopper.get()) throw new Exception(); + return super.getIssues(query, max, since); + } + }; + myManager.setRepositories(Collections.singletonList(repository)); + + List issues = myManager.getIssues(""); + assertEquals(1, issues.size()); + + stopper.set(Boolean.TRUE); + issues = myManager.getIssues(""); + assertEquals(1, issues.size()); + } }