Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.apache.log4j.Logger;
- import org.apache.lucene.document.Document;
- import org.apache.lucene.index.CheckIndex;
- import org.apache.lucene.index.SegmentInfo;
- import org.apache.lucene.index.SegmentInfos;
- import org.apache.lucene.index.SegmentReader;
- import org.apache.lucene.store.FSDirectory;
- import java.io.EOFException;
- import java.io.File;
- import java.io.IOException;
- /**
- * User: michal@senti1.com
- * Date: 14.01.13
- * Time: 19:03
- */
- public abstract class LuceneStoreRecovery {
- private static Logger log = Logger.getLogger(LuceneStoreRecovery.class);
- public void recoverSegmentStore(String indexPath, String segmentName) throws IOException {
- SegmentInfos sis = new SegmentInfos();
- CheckIndex.Status result = new CheckIndex.Status();
- result.dir = FSDirectory.open(new File(indexPath));
- int recoveredCount = 0;
- try {
- sis.read(result.dir);
- } catch (IOException e) {
- log.error(e.getClass().getSimpleName() + " was thrown!", e);
- }
- for (int i = 0; i < sis.size(); i++) {
- SegmentInfo info = sis.info(i);
- if (!info.name.equals(segmentName)) {
- continue;
- }
- SegmentReader reader = SegmentReader.get(true, info, 1);
- CheckIndex.Status.StoredFieldStatus status = new CheckIndex.Status.StoredFieldStatus();
- for (int j = 0; j < info.docCount; ++j) {
- Document doc = null;
- try {
- doc = reader.document(j);
- log.info("Recovered:" + doc.toString());
- if (!reader.isDeleted(j)) {
- handleDocument(doc);
- recoveredCount++;
- status.docCount++;
- status.totFields += doc.getFields().size();
- }
- } catch (EOFException eof) {
- log.error(eof.getClass().getSimpleName() + " was thrown for docId = " + j + "/" + info.docCount + ". Exiting recovery");
- break;
- } catch (IOException e) {
- log.error(e.getClass().getSimpleName() + " was thrown for docId = " + j);
- }
- }
- }
- log.info(recoveredCount + " documents were recovered");
- }
- public abstract void handleDocument(Document doc);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement