Advertisement
rzulf

LuceneStoreRecovery

Jan 18th, 2013
485
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.  * Date: 14.01.13
  16.  * Time: 19:03
  17.  */
  18. public abstract class LuceneStoreRecovery {
  19.  
  20.     private static Logger log = Logger.getLogger(LuceneStoreRecovery.class);
  21.  
  22.     public void recoverSegmentStore(String indexPath, String segmentName) throws IOException {
  23.         SegmentInfos sis = new SegmentInfos();
  24.         CheckIndex.Status result = new CheckIndex.Status();
  25.         result.dir = FSDirectory.open(new File(indexPath));
  26.  
  27.         int recoveredCount = 0;
  28.         try {
  29.             sis.read(result.dir);
  30.         } catch (IOException e) {
  31.             log.error(e.getClass().getSimpleName() + " was thrown!", e);
  32.         }
  33.         for (int i = 0; i < sis.size(); i++) {
  34.  
  35.             SegmentInfo info = sis.info(i);
  36.             if (!info.name.equals(segmentName)) {
  37.                 continue;
  38.             }
  39.  
  40.             SegmentReader reader = SegmentReader.get(true, info, 1);
  41.             CheckIndex.Status.StoredFieldStatus status = new CheckIndex.Status.StoredFieldStatus();
  42.             for (int j = 0; j < info.docCount; ++j) {
  43.                 Document doc = null;
  44.                 try {
  45.                     doc = reader.document(j);
  46.                     log.info("Recovered:" + doc.toString());
  47.                     if (!reader.isDeleted(j)) {
  48.                         handleDocument(doc);
  49.                         recoveredCount++;
  50.                         status.docCount++;
  51.                         status.totFields += doc.getFields().size();
  52.                     }
  53.                 } catch (EOFException eof) {
  54.                     log.error(eof.getClass().getSimpleName() + " was thrown for docId = " + j + "/" + info.docCount + ". Exiting recovery");
  55.                     break;
  56.                 } catch (IOException e) {
  57.                     log.error(e.getClass().getSimpleName() + " was thrown for docId = " + j);
  58.                 }
  59.             }
  60.         }
  61.         log.info(recoveredCount + " documents were recovered");
  62.     }
  63.  
  64.     public abstract void handleDocument(Document doc);
  65.  
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement