Advertisement
rzulf

LuceneStoreRecovery

Jan 18th, 2013
458
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.01 KB | None | 0 0
  1.  
  2. import org.apache.log4j.Logger;
  3. import org.apache.lucene.document.Document;
  4. import org.apache.lucene.index.CheckIndex;
  5. import org.apache.lucene.index.SegmentInfo;
  6. import org.apache.lucene.index.SegmentInfos;
  7. import org.apache.lucene.index.SegmentReader;
  8. import org.apache.lucene.store.FSDirectory;
  9.  
  10. import java.io.EOFException;
  11. import java.io.File;
  12. import java.io.IOException;
  13.  
  14. /**
  15.  * User: michal@senti1.com
  16.  * Date: 14.01.13
  17.  * Time: 19:03
  18.  */
  19. public abstract class LuceneStoreRecovery {
  20.  
  21.     private static Logger log = Logger.getLogger(LuceneStoreRecovery.class);
  22.  
  23.     public void recoverSegmentStore(String indexPath, String segmentName) throws IOException {
  24.         SegmentInfos sis = new SegmentInfos();
  25.         CheckIndex.Status result = new CheckIndex.Status();
  26.         result.dir = FSDirectory.open(new File(indexPath));
  27.  
  28.         int recoveredCount = 0;
  29.         try {
  30.             sis.read(result.dir);
  31.         } catch (IOException e) {
  32.             log.error(e.getClass().getSimpleName() + " was thrown!", e);
  33.         }
  34.         for (int i = 0; i < sis.size(); i++) {
  35.  
  36.             SegmentInfo info = sis.info(i);
  37.             if (!info.name.equals(segmentName)) {
  38.                 continue;
  39.             }
  40.  
  41.             SegmentReader reader = SegmentReader.get(true, info, 1);
  42.             CheckIndex.Status.StoredFieldStatus status = new CheckIndex.Status.StoredFieldStatus();
  43.             for (int j = 0; j < info.docCount; ++j) {
  44.                 Document doc = null;
  45.                 try {
  46.                     doc = reader.document(j);
  47.                     log.info("Recovered:" + doc.toString());
  48.                     if (!reader.isDeleted(j)) {
  49.                         handleDocument(doc);
  50.                         recoveredCount++;
  51.                         status.docCount++;
  52.                         status.totFields += doc.getFields().size();
  53.                     }
  54.                 } catch (EOFException eof) {
  55.                     log.error(eof.getClass().getSimpleName() + " was thrown for docId = " + j + "/" + info.docCount + ". Exiting recovery");
  56.                     break;
  57.                 } catch (IOException e) {
  58.                     log.error(e.getClass().getSimpleName() + " was thrown for docId = " + j);
  59.                 }
  60.             }
  61.         }
  62.         log.info(recoveredCount + " documents were recovered");
  63.     }
  64.  
  65.     public abstract void handleDocument(Document doc);
  66.  
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement