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/11/11 20:49:05 UTC

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

Author: shaie
Date: Thu Nov 11 19:49:04 2010
New Revision: 1034080

URL: http://svn.apache.org/viewvc?rev=1034080&view=rev
Log:
LUCENE-2753: IndexReader.listCommits should return a List and not an abstract Collection (3x)

Modified:
    lucene/dev/branches/branch_3x/lucene/CHANGES.txt
    lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/index/DirectoryReader.java
    lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/index/IndexCommit.java
    lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/index/IndexFileDeleter.java
    lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/index/IndexReader.java
    lucene/dev/branches/branch_3x/lucene/src/test/org/apache/lucene/index/TestIndexReader.java

Modified: lucene/dev/branches/branch_3x/lucene/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_3x/lucene/CHANGES.txt?rev=1034080&r1=1034079&r2=1034080&view=diff
==============================================================================
--- lucene/dev/branches/branch_3x/lucene/CHANGES.txt (original)
+++ lucene/dev/branches/branch_3x/lucene/CHANGES.txt Thu Nov 11 19:49:04 2010
@@ -74,6 +74,10 @@ Changes in backwards compatibility polic
   If you index empty fields and uses positions/offsets information on that
   fields, reindex is recommended. (David Smiley, Koji Sekiguchi)
 
+* LUCENE-2753: IndexReader and DirectoryReader .listCommits() now return a List
+  instead of a Collection, guaranteeing the commits are sorted from oldest to 
+  latest. (Shai Erera)
+  
 Changes in runtime behavior
 
 * LUCENE-1923: Made IndexReader.toString() produce something

Modified: lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/index/DirectoryReader.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/index/DirectoryReader.java?rev=1034080&r1=1034079&r2=1034080&view=diff
==============================================================================
--- lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/index/DirectoryReader.java (original)
+++ lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/index/DirectoryReader.java Thu Nov 11 19:49:04 2010
@@ -25,6 +25,7 @@ import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.HashSet;
+import java.util.List;
 
 import java.util.Map;
 import java.util.Set;
@@ -914,10 +915,10 @@ class DirectoryReader extends IndexReade
   }
 
   /** @see org.apache.lucene.index.IndexReader#listCommits */
-  public static Collection<IndexCommit> listCommits(Directory dir) throws IOException {
+  public static List<IndexCommit> listCommits(Directory dir) throws IOException {
     final String[] files = dir.listAll();
 
-    Collection<IndexCommit> commits = new ArrayList<IndexCommit>();
+    List<IndexCommit> commits = new ArrayList<IndexCommit>();
 
     SegmentInfos latest = new SegmentInfos();
     latest.read(dir);
@@ -954,6 +955,9 @@ class DirectoryReader extends IndexReade
       }
     }
 
+    // Ensure that the commit points are sorted in ascending order.
+    Collections.sort(commits);
+
     return commits;
   }
 

Modified: lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/index/IndexCommit.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/index/IndexCommit.java?rev=1034080&r1=1034079&r2=1034080&view=diff
==============================================================================
--- lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/index/IndexCommit.java (original)
+++ lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/index/IndexCommit.java Thu Nov 11 19:49:04 2010
@@ -20,6 +20,7 @@ package org.apache.lucene.index;
 import java.util.Collection;
 import java.util.Map;
 import java.io.IOException;
+
 import org.apache.lucene.store.Directory;
 
 /**
@@ -40,7 +41,7 @@ import org.apache.lucene.store.Directory
  * @lucene.experimental
 */
 
-public abstract class IndexCommit {
+public abstract class IndexCommit implements Comparable<IndexCommit> {
 
   /**
    * Get the segments file (<code>segments_N</code>) associated 
@@ -114,4 +115,16 @@ public abstract class IndexCommit {
    *  String -> String. */
   public abstract Map<String,String> getUserData() throws IOException;
   
+  public int compareTo(IndexCommit commit) {
+    long gen = getGeneration();
+    long comgen = commit.getGeneration();
+    if (gen < comgen) {
+      return -1;
+    } else if (gen > comgen) {
+      return 1;
+    } else {
+      return 0;
+    }
+  }
+
 }

Modified: lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/index/IndexFileDeleter.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/index/IndexFileDeleter.java?rev=1034080&r1=1034079&r2=1034080&view=diff
==============================================================================
--- lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/index/IndexFileDeleter.java (original)
+++ lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/index/IndexFileDeleter.java Thu Nov 11 19:49:04 2010
@@ -617,9 +617,8 @@ final class IndexFileDeleter {
    * equals.
    */
 
-  final private static class CommitPoint extends IndexCommit implements Comparable<CommitPoint> {
+  final private static class CommitPoint extends IndexCommit {
 
-    long gen;
     Collection<String> files;
     String segmentsFileName;
     boolean deleted;
@@ -638,7 +637,6 @@ final class IndexFileDeleter {
       version = segmentInfos.getVersion();
       generation = segmentInfos.getGeneration();
       files = Collections.unmodifiableCollection(segmentInfos.files(directory, true));
-      gen = segmentInfos.getGeneration();
       isOptimized = segmentInfos.size() == 1 && !segmentInfos.info(0).hasDeletions();
     }
 
@@ -699,14 +697,5 @@ final class IndexFileDeleter {
       return deleted;
     }
 
-    public int compareTo(CommitPoint commit) {
-      if (gen < commit.gen) {
-        return -1;
-      } else if (gen > commit.gen) {
-        return 1;
-      } else {
-        return 0;
-      }
-    }
   }
 }

Modified: lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/index/IndexReader.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/index/IndexReader.java?rev=1034080&r1=1034079&r2=1034080&view=diff
==============================================================================
--- lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/index/IndexReader.java (original)
+++ lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/index/IndexReader.java Thu Nov 11 19:49:04 2010
@@ -27,8 +27,8 @@ import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.Closeable;
-import java.util.Arrays;
 import java.util.Collection;
+import java.util.List;
 import java.util.Map;
 import java.util.concurrent.atomic.AtomicInteger;
 
@@ -1151,8 +1151,11 @@ public abstract class IndexReader implem
    *  the Directory, else this method throws {@link
    *  IndexNotFoundException}.  Note that if a commit is in
    *  progress while this method is running, that commit
-   *  may or may not be returned array.  */
-  public static Collection<IndexCommit> listCommits(Directory dir) throws IOException {
+   *  may or may not be returned.
+   *  
+   *  @return a sorted list of {@link IndexCommit}s, from oldest 
+   *  to latest. */
+  public static List<IndexCommit> listCommits(Directory dir) throws IOException {
     return DirectoryReader.listCommits(dir);
   }
 

Modified: lucene/dev/branches/branch_3x/lucene/src/test/org/apache/lucene/index/TestIndexReader.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_3x/lucene/src/test/org/apache/lucene/index/TestIndexReader.java?rev=1034080&r1=1034079&r2=1034080&view=diff
==============================================================================
--- lucene/dev/branches/branch_3x/lucene/src/test/org/apache/lucene/index/TestIndexReader.java (original)
+++ lucene/dev/branches/branch_3x/lucene/src/test/org/apache/lucene/index/TestIndexReader.java Thu Nov 11 19:49:04 2010
@@ -1802,4 +1802,30 @@ public class TestIndexReader extends Luc
     r.close();
     dir.close();
   }
+  
+  // LUCENE-2753
+  public void testListCommits() throws Exception {
+    Directory dir = newDirectory();
+    SnapshotDeletionPolicy sdp = new SnapshotDeletionPolicy(new KeepOnlyLastCommitDeletionPolicy());
+    IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig( 
+        TEST_VERSION_CURRENT, new WhitespaceAnalyzer(TEST_VERSION_CURRENT)).setIndexDeletionPolicy(sdp));
+    writer.addDocument(new Document());
+    writer.commit();
+    sdp.snapshot("c1");
+    writer.addDocument(new Document());
+    writer.commit();
+    sdp.snapshot("c2");
+    writer.addDocument(new Document());
+    writer.commit();
+    sdp.snapshot("c3");
+    writer.close();
+    List<IndexCommit> commits = IndexReader.listCommits(dir);
+    long currentGen = 0;
+    for (IndexCommit ic : commits) {
+      assertTrue("currentGen=" + currentGen + " commitGen=" + ic.getGeneration(), currentGen < ic.getGeneration());
+      currentGen = ic.getGeneration();
+    }
+    dir.close();
+  }
+  
 }