From 035625135e0294f6f0a2e941f784203f9293101e Mon Sep 17 00:00:00 2001 From: Andrew Hind Date: Wed, 12 Sep 2007 14:47:58 +0000 Subject: [PATCH] Better index clean up at start up git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@6766 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261 --- .../search/impl/lucene/index/IndexInfo.java | 65 ++++++++++++++++++- 1 file changed, 63 insertions(+), 2 deletions(-) diff --git a/source/java/org/alfresco/repo/search/impl/lucene/index/IndexInfo.java b/source/java/org/alfresco/repo/search/impl/lucene/index/IndexInfo.java index 7ac27f9987..0fb75564a6 100644 --- a/source/java/org/alfresco/repo/search/impl/lucene/index/IndexInfo.java +++ b/source/java/org/alfresco/repo/search/impl/lucene/index/IndexInfo.java @@ -84,6 +84,7 @@ import org.apache.lucene.store.FSDirectory; import org.apache.lucene.store.IndexInput; import org.apache.lucene.store.IndexOutput; import org.apache.lucene.store.RAMDirectory; +import org.doomdark.uuid.UUID; /** * The information that makes up an index. IndexInfoVersion Repeated information of the form @@ -584,6 +585,19 @@ public class IndexInfo releaseWriteLock(); } } + // Need to do with file lock - must share info about other readers to support this with shared indexer + // implementation + + getWriteLock(); + try + { + LockWork work = new DeleteUnknownGuidDirectories(); + doWithFileLock(work); + } + finally + { + releaseWriteLock(); + } // Run the cleaner around every 20 secods - this just makes the request to the thread pool timer.schedule(new TimerTask() @@ -594,7 +608,39 @@ public class IndexInfo cleaner.schedule(); } }, 0, 20000); + } + private class DeleteUnknownGuidDirectories implements LockWork + { + + public Object doWork() throws Exception + { + setStatusFromFile(); + + // If the index is not shared we can do some easy clean + // up + if (!indexIsShared) + { + // Safe to tidy up all files that look like guids that we do not know about + File[] files = indexDirectory.listFiles(); + if (files != null) + { + for (File file : files) + { + if (file.isDirectory()) + { + String id = file.getName(); + if (!indexEntries.containsKey(id) && isGUID(id)) + { + deleteQueue.add(id); + } + } + } + } + + } + return null; + } } /** @@ -1437,7 +1483,7 @@ public class IndexInfo private class RolledBackTransition implements Transition { ThreadLocal tl = new ThreadLocal(); - + public void beforeWithReadLock(String id, Set toDelete, Set read) throws IOException { closeDelta(id); @@ -1456,7 +1502,7 @@ public class IndexInfo { entry.setStatus(TransactionStatus.ROLLEDBACK); writeStatus(); - + registerReferenceCountingIndexReader(id, tl.get()); indexEntries.remove(id); if (s_logger.isDebugEnabled()) @@ -3287,6 +3333,21 @@ public class IndexInfo this.writerUseCompoundFile = writerUseCompoundFile; } + private boolean isGUID(String guid) + { + try + { + UUID id = new UUID(guid); + // We have a valid guid. + return true; + } + catch (NumberFormatException e) + { + // Not a valid GUID + } + return false; + } + interface Schedulable { void schedule();