diff options
author | Steffen Pingel | 2012-03-02 19:35:11 +0000 |
---|---|---|
committer | David Green | 2012-03-02 19:35:11 +0000 |
commit | 2befecfd0c9ea65a84504ad94eea486e317407cf (patch) | |
tree | d8ae786d9caa90e9abcf7ea5a6978637e207d8ff | |
parent | 20cefdff005db68155f001f06733accae2382a1c (diff) | |
download | org.eclipse.mylyn.tasks-2befecfd0c9ea65a84504ad94eea486e317407cf.tar.gz org.eclipse.mylyn.tasks-2befecfd0c9ea65a84504ad94eea486e317407cf.tar.xz org.eclipse.mylyn.tasks-2befecfd0c9ea65a84504ad94eea486e317407cf.zip |
bug 372725: handle changes to task data directory
https://bugs.eclipse.org/bugs/show_bug.cgi?id=372725
Change-Id: I24cc1a042b41a0fc83c28638b78c25fb74a818f1
3 files changed, 131 insertions, 31 deletions
diff --git a/org.eclipse.mylyn.tasks.index.core/src/org/eclipse/mylyn/internal/tasks/index/core/TaskListIndex.java b/org.eclipse.mylyn.tasks.index.core/src/org/eclipse/mylyn/internal/tasks/index/core/TaskListIndex.java index 1d6b5af36..00ee23ea9 100644 --- a/org.eclipse.mylyn.tasks.index.core/src/org/eclipse/mylyn/internal/tasks/index/core/TaskListIndex.java +++ b/org.eclipse.mylyn.tasks.index.core/src/org/eclipse/mylyn/internal/tasks/index/core/TaskListIndex.java @@ -110,6 +110,7 @@ import org.eclipse.mylyn.tasks.core.data.TaskData; * </p> * * @author David Green + * @author Steffen Pingel */ public class TaskListIndex implements ITaskDataManagerListener, ITaskListChangeListener, IRepositoryListener { @@ -337,21 +338,7 @@ public class TaskListIndex implements ITaskDataManagerListener, ITaskListChangeL Assert.isNotNull(indexLocation); this.startupDelay = startupDelay; - if (!indexLocation.exists()) { - rebuildIndex = true; - if (!indexLocation.mkdirs()) { - StatusHandler.log(new Status(IStatus.ERROR, TasksIndexCore.ID_PLUGIN, - "Cannot create task list index folder: " + indexLocation)); //$NON-NLS-1$ - } - } - if (indexLocation.exists() && indexLocation.isDirectory()) { - try { - directory = new NIOFSDirectory(indexLocation); - } catch (IOException e) { - StatusHandler.log(new Status(IStatus.ERROR, TasksIndexCore.ID_PLUGIN, - "Cannot create task list index", e)); //$NON-NLS-1$ - } - } + setLocationInternal(indexLocation); initialize(); } @@ -393,6 +380,62 @@ public class TaskListIndex implements ITaskDataManagerListener, ITaskListChangeL this.reindexDelay = reindexDelay; } + public void setLocation(File indexLocation) { + try { + waitUntilIdle(); + } catch (InterruptedException e1) { + // ignore + } + setLocationInternal(indexLocation); + rebuildIndex = true; + scheduleIndexMaintenance(MaintainIndexType.STARTUP); + } + + private void setLocationInternal(File indexLocation) { + final boolean newLocationExists = indexLocation.exists(); + if (!newLocationExists) { + if (!indexLocation.mkdirs()) { + StatusHandler.log(new Status(IStatus.ERROR, TasksIndexCore.ID_PLUGIN, + "Cannot create task list index folder: " + indexLocation)); //$NON-NLS-1$ + } + } + + Lock writeLock = indexReaderLock.writeLock(); + writeLock.lock(); + try { + synchronized (this) { + if (indexReader != null) { + try { + indexReader.close(); + } catch (IOException e) { + // ignore + } + indexReader = null; + } + + if (indexLocation.exists() && indexLocation.isDirectory()) { + if (directory != null) { + try { + directory.close(); + } catch (IOException e) { + StatusHandler.log(new Status(IStatus.ERROR, TasksIndexCore.ID_PLUGIN, + "Cannot close index: " + e.getMessage(), e)); //$NON-NLS-1$ + } + } + try { + directory = new NIOFSDirectory(indexLocation); + } catch (IOException e) { + StatusHandler.log(new Status(IStatus.ERROR, TasksIndexCore.ID_PLUGIN, + "Cannot create task list index", e)); //$NON-NLS-1$ + } + } + + } + } finally { + writeLock.unlock(); + } + } + /** * the default field used to match tasks when unspecified in the query */ diff --git a/org.eclipse.mylyn.tasks.index.tests/src/org/eclipse/mylyn/internal/tasks/index/tests/TaskListIndexTest.java b/org.eclipse.mylyn.tasks.index.tests/src/org/eclipse/mylyn/internal/tasks/index/tests/TaskListIndexTest.java index 34a71db1c..c647c3d59 100644 --- a/org.eclipse.mylyn.tasks.index.tests/src/org/eclipse/mylyn/internal/tasks/index/tests/TaskListIndexTest.java +++ b/org.eclipse.mylyn.tasks.index.tests/src/org/eclipse/mylyn/internal/tasks/index/tests/TaskListIndexTest.java @@ -82,17 +82,30 @@ public class TaskListIndexTest { @Before public void setup() throws IOException { - tempDir = File.createTempFile(TaskListIndexTest.class.getSimpleName(), ".tmp"); + tempDir = createTmpDir(); + + context = new MockTestContext(); + } + + private File createTmpDir() throws IOException { + File tempDir = File.createTempFile(TaskListIndexTest.class.getSimpleName(), ".tmp"); tempDir.delete(); tempDir.mkdirs(); assertTrue(tempDir.exists() && tempDir.isDirectory()); - - context = new MockTestContext(); + return tempDir; } @After public void tearDown() { + disposeIndex(); + if (tempDir != null) { + delete(tempDir); + assertFalse(tempDir.exists()); + } + } + + private void disposeIndex() { if (index != null) { try { index.waitUntilIdle(); @@ -102,10 +115,6 @@ public class TaskListIndexTest { index.close(); index = null; } - if (tempDir != null) { - delete(tempDir); - assertFalse(tempDir.exists()); - } } private void delete(File file) { @@ -218,11 +227,7 @@ public class TaskListIndexTest { index.setDefaultField(FIELD_SUMMARY); - TestTaskCollector collector = new TestTaskCollector(); - index.find(task.getSummary(), collector, 1000); - - assertEquals(1, collector.getTasks().size()); - assertTrue(collector.getTasks().contains(task)); + assertCanFindTask(task); } @Test @@ -412,4 +417,40 @@ public class TaskListIndexTest { + index.escapeFieldValue(repositoryTask.getHandleIdentifier()))); } + @Test + public void testSetLocation() throws InterruptedException, IOException { + setupIndex(); + index.setDefaultField(FIELD_SUMMARY); + + ITask task = context.createLocalTask(); + + index.waitUntilIdle(); + + assertCanFindTask(task); + + File newLocation = createTmpDir(); + try { + assertEquals(0, newLocation.list().length); + + index.setLocation(newLocation); + + index.waitUntilIdle(); + assertCanFindTask(task); + + assertFalse(newLocation.list().length == 0); + } finally { + disposeIndex(); + delete(newLocation); + assertFalse(newLocation.exists()); + } + } + + private void assertCanFindTask(ITask task) { + TestTaskCollector collector = new TestTaskCollector(); + index.find(task.getSummary(), collector, 1000); + + assertEquals(1, collector.getTasks().size()); + assertTrue(collector.getTasks().contains(task)); + } + }
\ No newline at end of file diff --git a/org.eclipse.mylyn.tasks.index.ui/src/org/eclipse/mylyn/internal/tasks/index/ui/IndexReference.java b/org.eclipse.mylyn.tasks.index.ui/src/org/eclipse/mylyn/internal/tasks/index/ui/IndexReference.java index 742986d60..5181cbf43 100644 --- a/org.eclipse.mylyn.tasks.index.ui/src/org/eclipse/mylyn/internal/tasks/index/ui/IndexReference.java +++ b/org.eclipse.mylyn.tasks.index.ui/src/org/eclipse/mylyn/internal/tasks/index/ui/IndexReference.java @@ -14,12 +14,14 @@ package org.eclipse.mylyn.internal.tasks.index.ui; import java.io.File; import java.util.concurrent.atomic.AtomicInteger; +import org.eclipse.mylyn.internal.tasks.core.IRepositoryModelListener; import org.eclipse.mylyn.internal.tasks.index.core.TaskListIndex; import org.eclipse.mylyn.internal.tasks.ui.TasksUiPlugin; import org.eclipse.mylyn.tasks.core.IRepositoryManager; /** * @author David Green + * @author Steffen Pingel */ public class IndexReference { @@ -27,6 +29,20 @@ public class IndexReference { private static AtomicInteger referenceCount = new AtomicInteger(); + private static IRepositoryModelListener listener = new IRepositoryModelListener() { + public void loaded() { + synchronized (IndexReference.class) { + if (theIndex != null) { + theIndex.setLocation(getDefaultIndexLocation()); + } + } + } + }; + + static File getDefaultIndexLocation() { + return new File(TasksUiPlugin.getDefault().getDataDirectory(), ".taskListIndex"); //$NON-NLS-1$ + } + /** * When not null serves as flag indicating that theIndex is referenced, thus preventing bad behaviour if dispose is * called multiple times. @@ -38,11 +54,9 @@ public class IndexReference { if (index == null) { if (theIndex == null) { final IRepositoryManager repositoryManager = TasksUiPlugin.getRepositoryManager(); - final File indexLocation = new File(TasksUiPlugin.getDefault().getDataDirectory(), ".taskListIndex"); //$NON-NLS-1$ - theIndex = new TaskListIndex(TasksUiPlugin.getTaskList(), TasksUiPlugin.getTaskDataManager(), - repositoryManager, indexLocation); - + repositoryManager, getDefaultIndexLocation()); + TasksUiPlugin.getDefault().addModelListener(listener); } index = theIndex; referenceCount.incrementAndGet(); @@ -57,10 +71,12 @@ public class IndexReference { index = null; if (referenceCount.decrementAndGet() == 0) { + TasksUiPlugin.getDefault().removeModelListener(listener); theIndex.close(); theIndex = null; } } } } + } |