Apache Lucene open-source search software
LUCENE-2592: merge from 3x
Shai Ererabc6ec5d0fdbf
diff --git a/lucene/src/java/org/apache/lucene/index/PersistentSnapshotDeletionPolicy.java b/lucene/src/java/org/apache/lucene/index/PersistentSnapshotDeletionPolicy.javaindex e4f1c66718f..fc09266c377 100644--- a/lucene/src/java/org/apache/lucene/index/PersistentSnapshotDeletionPolicy.java+++ b/lucene/src/java/org/apache/lucene/index/PersistentSnapshotDeletionPolicy.java@@ -18,7 +18,9 @@ package org.apache.lucene.index;*/import java.io.IOException;+import java.util.HashMap;import java.util.List;+import java.util.Map;import java.util.Map.Entry;import org.apache.lucene.document.Document;@@ -55,6 +57,38 @@ public class PersistentSnapshotDeletionPolicy extends SnapshotDeletionPolicy {// The index writer which maintains the snapshots metadataprivate final IndexWriter writer;+ /**+ * Reads the snapshots information from the given {@link Directory}. This+ * method does can be used if the snapshots information is needed, however you+ * cannot instantiate the deletion policy (because e.g., some other process+ * keeps a lock on the snapshots directory).+ */+ public static Map<String, String> readSnapshotsInfo(Directory dir) throws IOException {+ IndexReader r = IndexReader.open(dir, true);+ Map<String, String> snapshots = new HashMap<String, String>();+ try {+ int numDocs = r.numDocs();+ // index is allowed to have exactly one document or 0.+ if (numDocs == 1) {+ Document doc = r.document(r.maxDoc() - 1);+ Field sid = doc.getField(SNAPSHOTS_ID);+ if (sid == null) {+ throw new IllegalStateException("directory is not a valid snapshots store!");+ }+ doc.removeField(SNAPSHOTS_ID);+ for (Fieldable f : doc.getFields()) {+ snapshots.put(f.name(), f.stringValue());+ }+ } else if (numDocs != 0) {+ throw new IllegalStateException(+ "should be at most 1 document in the snapshots directory: " + numDocs);+ }+ } finally {+ r.close();+ }+ return snapshots;+ }+/*** {@link PersistentSnapshotDeletionPolicy} wraps another* {@link IndexDeletionPolicy} to enable flexible snapshotting.@@ -91,28 +125,8 @@ public class PersistentSnapshotDeletionPolicy extends SnapshotDeletionPolicy {// Initializes the snapshots information. This code should basically run// only if mode != CREATE, but if it is, it's no harm as we only open the// reader once and immediately close it.- IndexReader r = writer.getReader();- try {- int numDocs = r.numDocs();- // index is allowed to have exactly one document or 0.- if (numDocs == 1) {- Document doc = r.document(r.maxDoc() - 1);- Field sid = doc.getField(SNAPSHOTS_ID);- if (sid == null) {- writer.close();- throw new IllegalStateException("directory is not a valid snapshots store!");- }- doc.removeField(SNAPSHOTS_ID);- for (Fieldable f : doc.getFields()) {- registerSnapshotInfo(f.name(), f.stringValue(), null);- }- } else if (numDocs != 0) {- writer.close();- throw new IllegalStateException(- "should be at most 1 document in the snapshots directory: " + numDocs);- }- } finally {- r.close();+ for (Entry<String, String> e : readSnapshotsInfo(dir).entrySet()) {+ registerSnapshotInfo(e.getKey(), e.getValue(), null);}}diff --git a/lucene/src/test/org/apache/lucene/index/TestPersistentSnapshotDeletionPolicy.java b/lucene/src/test/org/apache/lucene/index/TestPersistentSnapshotDeletionPolicy.javaindex cb6e6308d08..3a93fe72da8 100644--- a/lucene/src/test/org/apache/lucene/index/TestPersistentSnapshotDeletionPolicy.java+++ b/lucene/src/test/org/apache/lucene/index/TestPersistentSnapshotDeletionPolicy.java@@ -26,6 +26,7 @@ import java.util.Map.Entry;import org.apache.lucene.document.Document;import org.apache.lucene.index.IndexWriterConfig.OpenMode;import org.apache.lucene.store.Directory;+import org.apache.lucene.store.LockObtainFailedException;import org.junit.After;import org.junit.Before;import org.junit.Test;@@ -101,7 +102,6 @@ public class TestPersistentSnapshotDeletionPolicy extends TestSnapshotDeletionPoIndexWriter writer = new IndexWriter(snapshotDir, getConfig(random, null));writer.addDocument(new Document());writer.close();- PersistentSnapshotDeletionPolicy dp = null;try {new PersistentSnapshotDeletionPolicy(new KeepOnlyLastCommitDeletionPolicy(), snapshotDir, OpenMode.APPEND,@@ -153,4 +153,32 @@ public class TestPersistentSnapshotDeletionPolicy extends TestSnapshotDeletionPodir.close();}+ @Test+ public void testStaticRead() throws Exception {+ // While PSDP is open, it keeps a lock on the snapshots directory and thus+ // prevents reading the snapshots information. This test checks that the+ // static read method works.+ int numSnapshots = 1;+ Directory dir = newDirectory(random);+ PersistentSnapshotDeletionPolicy psdp = (PersistentSnapshotDeletionPolicy) getDeletionPolicy();+ IndexWriter writer = new IndexWriter(dir, getConfig(random, psdp));+ prepareIndexAndSnapshots(psdp, writer, numSnapshots, "snapshot");+ writer.close();+ dir.close();++ try {+ // This should fail, since the snapshots directory is locked - we didn't close it !+ new PersistentSnapshotDeletionPolicy(+ new KeepOnlyLastCommitDeletionPolicy(), snapshotDir, OpenMode.APPEND,+ TEST_VERSION_CURRENT);+ fail("should not have reached here - the snapshots directory should be locked!");+ } catch (LockObtainFailedException e) {+ // expected+ }++ // Reading the snapshots info should succeed though+ Map<String, String> snapshots = PersistentSnapshotDeletionPolicy.readSnapshotsInfo(snapshotDir);+ assertEquals("expected " + numSnapshots + " snapshots, got " + snapshots.size(), numSnapshots, snapshots.size());+ }+}