org.apache.lucene.index.IndexWriterConfig.OpenMode#APPEND源码实例Demo

下面列出了org.apache.lucene.index.IndexWriterConfig.OpenMode#APPEND 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

/**
 * {@link PersistentSnapshotDeletionPolicy} wraps another
 * {@link IndexDeletionPolicy} to enable flexible snapshotting.
 * 
 * @param primary
 *          the {@link IndexDeletionPolicy} that is used on non-snapshotted
 *          commits. Snapshotted commits, by definition, are not deleted until
 *          explicitly released via {@link #release}.
 * @param dir
 *          the {@link Directory} which will be used to persist the snapshots
 *          information.
 * @param mode
 *          specifies whether a new index should be created, deleting all
 *          existing snapshots information (immediately), or open an existing
 *          index, initializing the class with the snapshots information.
 */
public PersistentSnapshotDeletionPolicy(IndexDeletionPolicy primary,
    Directory dir, OpenMode mode) throws IOException {
  super(primary);

  this.dir = dir;

  if (mode == OpenMode.CREATE) {
    clearPriorSnapshots();
  }

  loadPriorSnapshots();

  if (mode == OpenMode.APPEND && nextWriteGen == 0) {
    throw new IllegalStateException("no snapshots stored in this directory");
  }
}
 
@Test
public void testSnapshotRelease() throws Exception {
  Directory dir = newDirectory();
  IndexWriter writer = new IndexWriter(dir, getConfig(random(), getDeletionPolicy(dir)));
  PersistentSnapshotDeletionPolicy psdp = (PersistentSnapshotDeletionPolicy) writer.getConfig().getIndexDeletionPolicy();
  prepareIndexAndSnapshots(psdp, writer, 1);
  writer.close();

  psdp.release(snapshots.get(0));

  psdp = new PersistentSnapshotDeletionPolicy(
      new KeepOnlyLastCommitDeletionPolicy(), dir, OpenMode.APPEND);
  assertEquals("Should have no snapshots !", 0, psdp.getSnapshotCount());
  dir.close();
}
 
@Test
public void testSnapshotReleaseByGeneration() throws Exception {
  Directory dir = newDirectory();
  IndexWriter writer = new IndexWriter(dir, getConfig(random(), getDeletionPolicy(dir)));
  PersistentSnapshotDeletionPolicy psdp = (PersistentSnapshotDeletionPolicy) writer.getConfig().getIndexDeletionPolicy();
  prepareIndexAndSnapshots(psdp, writer, 1);
  writer.close();

  psdp.release(snapshots.get(0).getGeneration());

  psdp = new PersistentSnapshotDeletionPolicy(
      new KeepOnlyLastCommitDeletionPolicy(), dir, OpenMode.APPEND);
  assertEquals("Should have no snapshots !", 0, psdp.getSnapshotCount());
  dir.close();
}
 
源代码4 项目: lucene-solr   文件: SolrSnapshotMetaDataManager.java
/**
 * A constructor.
 *
 * @param dir The directory where the snapshot meta-data is stored.
 * @param mode CREATE If previous meta-data should be erased.
 *             APPEND If previous meta-data should be read and updated.
 *             CREATE_OR_APPEND Creates a new meta-data structure if one does not exist
 *                              Updates the existing structure if one exists.
 * @throws IOException in case of errors.
 */
public SolrSnapshotMetaDataManager(SolrCore solrCore, Directory dir, OpenMode mode) throws IOException {
  this.solrCore = solrCore;
  this.dir = dir;

  if (mode == OpenMode.CREATE) {
    deleteSnapshotMetadataFiles();
  }

  loadFromSnapshotMetadataFile();

  if (mode == OpenMode.APPEND && nextWriteGen == 0) {
    throw new IllegalStateException("no snapshots stored in this directory");
  }
}
 
@Test
public void testExistingSnapshots() throws Exception {
  int numSnapshots = 3;
  MockDirectoryWrapper dir = newMockDirectory();
  IndexWriter writer = new IndexWriter(dir, getConfig(random(), getDeletionPolicy(dir)));
  PersistentSnapshotDeletionPolicy psdp = (PersistentSnapshotDeletionPolicy) writer.getConfig().getIndexDeletionPolicy();
  assertNull(psdp.getLastSaveFile());
  prepareIndexAndSnapshots(psdp, writer, numSnapshots);
  assertNotNull(psdp.getLastSaveFile());
  writer.close();

  // Make sure only 1 save file exists:
  int count = 0;
  for(String file : dir.listAll()) {
    if (file.startsWith(PersistentSnapshotDeletionPolicy.SNAPSHOTS_PREFIX)) {
      count++;
    }
  }
  assertEquals(1, count);

  // Make sure we fsync:
  dir.crash();
  dir.clearCrash();

  // Re-initialize and verify snapshots were persisted
  psdp = new PersistentSnapshotDeletionPolicy(
      new KeepOnlyLastCommitDeletionPolicy(), dir, OpenMode.APPEND);

  writer = new IndexWriter(dir, getConfig(random(), psdp));
  psdp = (PersistentSnapshotDeletionPolicy) writer.getConfig().getIndexDeletionPolicy();

  assertEquals(numSnapshots, psdp.getSnapshots().size());
  assertEquals(numSnapshots, psdp.getSnapshotCount());
  assertSnapshotExists(dir, psdp, numSnapshots, false);

  writer.addDocument(new Document());
  writer.commit();
  snapshots.add(psdp.snapshot());
  assertEquals(numSnapshots+1, psdp.getSnapshots().size());
  assertEquals(numSnapshots+1, psdp.getSnapshotCount());
  assertSnapshotExists(dir, psdp, numSnapshots+1, false);

  writer.close();
  dir.close();
}