Skip to content

Apache Lucene open-source search software

LUCENE-2753: port 3x to trunk

Shai Ererae71363700caf
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 2cf134f2706..404785995fb 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -396,6 +396,10 @@ Changes in backwards compatibility policy
* LUCENE-2733: Removed public constructors of utility classes with only static
methods to prevent instantiation. (Uwe Schindler)
+* 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
diff --git a/lucene/src/java/org/apache/lucene/index/DirectoryReader.java b/lucene/src/java/org/apache/lucene/index/DirectoryReader.java
index 2a05bbfe8bc..64f77e0503c 100644
--- a/lucene/src/java/org/apache/lucene/index/DirectoryReader.java
+++ b/lucene/src/java/org/apache/lucene/index/DirectoryReader.java
@@ -940,15 +940,15 @@ class DirectoryReader extends IndexReader implements Cloneable {
}
/** @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 {
return listCommits(dir, CodecProvider.getDefault());
}
/** @see org.apache.lucene.index.IndexReader#listCommits */
- public static Collection<IndexCommit> listCommits(Directory dir, CodecProvider codecs) throws IOException {
+ public static List<IndexCommit> listCommits(Directory dir, CodecProvider codecs) throws IOException {
final String[] files = dir.listAll();
- Collection<IndexCommit> commits = new ArrayList<IndexCommit>();
+ List<IndexCommit> commits = new ArrayList<IndexCommit>();
SegmentInfos latest = new SegmentInfos(codecs);
latest.read(dir, codecs);
@@ -985,6 +985,9 @@ class DirectoryReader extends IndexReader implements Cloneable {
}
}
+ // Ensure that the commit points are sorted in ascending order.
+ Collections.sort(commits);
+
return commits;
}
diff --git a/lucene/src/java/org/apache/lucene/index/IndexCommit.java b/lucene/src/java/org/apache/lucene/index/IndexCommit.java
index 26318c430ea..908c0953b30 100644
--- a/lucene/src/java/org/apache/lucene/index/IndexCommit.java
+++ b/lucene/src/java/org/apache/lucene/index/IndexCommit.java
@@ -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;
+ }
+ }
+
}
diff --git a/lucene/src/java/org/apache/lucene/index/IndexFileDeleter.java b/lucene/src/java/org/apache/lucene/index/IndexFileDeleter.java
index 2aaafb6826a..325eec45089 100644
--- a/lucene/src/java/org/apache/lucene/index/IndexFileDeleter.java
+++ b/lucene/src/java/org/apache/lucene/index/IndexFileDeleter.java
@@ -620,9 +620,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;
@@ -641,7 +640,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();
}
@@ -702,14 +700,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;
- }
- }
}
}
diff --git a/lucene/src/java/org/apache/lucene/index/IndexReader.java b/lucene/src/java/org/apache/lucene/index/IndexReader.java
index da781345af9..73266e8831d 100644
--- a/lucene/src/java/org/apache/lucene/index/IndexReader.java
+++ b/lucene/src/java/org/apache/lucene/index/IndexReader.java
@@ -32,8 +32,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;
@@ -1341,8 +1341,11 @@ public abstract class IndexReader implements Cloneable,Closeable {
* 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);
}
diff --git a/lucene/src/test/org/apache/lucene/index/TestIndexReader.java b/lucene/src/test/org/apache/lucene/index/TestIndexReader.java
index bf16cb06d69..3bb34243066 100644
--- a/lucene/src/test/org/apache/lucene/index/TestIndexReader.java
+++ b/lucene/src/test/org/apache/lucene/index/TestIndexReader.java
@@ -1813,4 +1813,29 @@ public class TestIndexReader extends LuceneTestCase
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, null).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();
+ long currentGen = 0;
+ for (IndexCommit ic : IndexReader.listCommits(dir)) {
+ assertTrue("currentGen=" + currentGen + " commitGen=" + ic.getGeneration(), currentGen < ic.getGeneration());
+ currentGen = ic.getGeneration();
+ }
+ dir.close();
+ }
+
}