1 17 package org.alfresco.repo.search.impl.lucene.fts; 18 19 import java.util.HashSet ; 20 import java.util.LinkedHashSet ; 21 import java.util.Set ; 22 23 import org.alfresco.repo.search.impl.lucene.LuceneIndexer; 24 import org.alfresco.repo.search.impl.lucene.LuceneIndexerAndSearcherFactory; 25 import org.alfresco.service.cmr.repository.StoreRef; 26 import org.springframework.context.ApplicationContext; 27 import org.springframework.context.support.ClassPathXmlApplicationContext; 28 29 public class FullTextSearchIndexerImpl implements FTSIndexerAware, FullTextSearchIndexer 30 { 31 private enum State { 32 ACTIVE, PAUSING, PAUSED 33 }; 34 35 private static Set <StoreRef> requiresIndex = new LinkedHashSet <StoreRef>(); 36 37 private static Set <StoreRef> indexing = new HashSet <StoreRef>(); 38 39 LuceneIndexerAndSearcherFactory luceneIndexerAndSearcherFactory; 40 41 private int pauseCount = 0; 42 43 private boolean paused = false; 44 45 public FullTextSearchIndexerImpl() 46 { 47 super(); 48 } 50 51 56 public synchronized void requiresIndex(StoreRef storeRef) 57 { 58 requiresIndex.add(storeRef); 59 } 60 61 67 public synchronized void indexCompleted(StoreRef storeRef, int remaining, Exception e) 68 { 69 try 70 { 71 indexing.remove(storeRef); 72 if ((remaining > 0) || (e != null)) 73 { 74 requiresIndex(storeRef); 75 } 76 if (e != null) 77 { 78 throw new FTSIndexerException(e); 79 } 80 } 81 finally 82 { 83 this.notifyAll(); 85 } 86 } 87 88 93 public synchronized void pause() throws InterruptedException 94 { 95 pauseCount++; 96 while ((indexing.size() > 0)) 98 { 99 this.wait(); 101 } 102 pauseCount--; 103 if(pauseCount == 0) 104 { 105 paused = true; 106 this.notifyAll(); } 108 } 110 111 116 public synchronized void resume() throws InterruptedException 117 { 118 if(pauseCount == 0) 119 { 120 paused = false; 122 } 123 else 124 { 125 while(pauseCount > 0) 126 { 127 this.wait(); 129 } 130 paused = false; 131 } 132 } 133 134 private synchronized boolean isPaused() throws InterruptedException 135 { 136 if(pauseCount == 0) 137 { 138 return paused; 139 } 140 else 141 { 142 while(pauseCount > 0) 143 { 144 this.wait(); 145 } 146 return paused; 147 } 148 } 149 150 155 public void index() 156 { 157 160 StoreRef toIndex = getNextRef(); 161 if (toIndex != null) 162 { 163 LuceneIndexer indexer = luceneIndexerAndSearcherFactory.getIndexer(toIndex); 165 indexer.registerCallBack(this); 166 indexer.updateFullTextSearch(1000); 167 } 168 else 169 { 170 } 172 } 173 174 private synchronized StoreRef getNextRef() 175 { 176 if (paused || (pauseCount > 0)) 177 { 178 return null; 180 } 181 182 StoreRef nextStoreRef = null; 183 184 for (StoreRef ref : requiresIndex) 185 { 186 if (!indexing.contains(ref)) 187 { 188 nextStoreRef = ref; 189 } 190 } 191 192 if (nextStoreRef != null) 193 { 194 requiresIndex.remove(nextStoreRef); 195 indexing.add(nextStoreRef); 196 } 197 198 return nextStoreRef; 199 } 200 201 public void setLuceneIndexerAndSearcherFactory(LuceneIndexerAndSearcherFactory luceneIndexerAndSearcherFactory) 202 { 203 this.luceneIndexerAndSearcherFactory = luceneIndexerAndSearcherFactory; 204 } 205 206 public static void main(String [] args) throws InterruptedException 207 { 208 ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:alfresco/application-context.xml"); 209 } 210 } 211 | Popular Tags |