You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by sh...@apache.org on 2010/08/21 20:49:58 UTC

svn commit: r987811 - in /lucene/dev/branches/branch_3x: ./ lucene/src/java/org/apache/lucene/index/PersistentSnapshotDeletionPolicy.java lucene/src/test/org/apache/lucene/index/TestPersistentSnapshotDeletionPolicy.java

Author: shaie
Date: Sat Aug 21 18:49:57 2010
New Revision: 987811

URL: http://svn.apache.org/viewvc?rev=987811&view=rev
Log:
LUCENE-2592: Add static readSnapshotsInfo to PersistentSnapshotDeletionPolicy

Modified:
    lucene/dev/branches/branch_3x/   (props changed)
    lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/index/PersistentSnapshotDeletionPolicy.java
    lucene/dev/branches/branch_3x/lucene/src/test/org/apache/lucene/index/TestPersistentSnapshotDeletionPolicy.java

Propchange: lucene/dev/branches/branch_3x/
------------------------------------------------------------------------------
--- svn:ignore (original)
+++ svn:ignore Sat Aug 21 18:49:57 2010
@@ -1,3 +1,4 @@
+classes
 build
 dist
 *~

Modified: lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/index/PersistentSnapshotDeletionPolicy.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/index/PersistentSnapshotDeletionPolicy.java?rev=987811&r1=987810&r2=987811&view=diff
==============================================================================
--- lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/index/PersistentSnapshotDeletionPolicy.java (original)
+++ lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/index/PersistentSnapshotDeletionPolicy.java Sat Aug 21 18:49:57 2010
@@ -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;
@@ -56,6 +58,38 @@ public class PersistentSnapshotDeletionP
   private 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 PersistentSnapshotDeletionP
     // 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);
     }
   }
 

Modified: lucene/dev/branches/branch_3x/lucene/src/test/org/apache/lucene/index/TestPersistentSnapshotDeletionPolicy.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_3x/lucene/src/test/org/apache/lucene/index/TestPersistentSnapshotDeletionPolicy.java?rev=987811&r1=987810&r2=987811&view=diff
==============================================================================
--- lucene/dev/branches/branch_3x/lucene/src/test/org/apache/lucene/index/TestPersistentSnapshotDeletionPolicy.java (original)
+++ lucene/dev/branches/branch_3x/lucene/src/test/org/apache/lucene/index/TestPersistentSnapshotDeletionPolicy.java Sat Aug 21 18:49:57 2010
@@ -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 TestPersistentSnapshotDelet
     IndexWriter 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 TestPersistentSnapshotDelet
     dir.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());
+  }
+  
 }