Index: lucene/CHANGES.txt =================================================================== --- lucene/CHANGES.txt (revision 1033824) +++ lucene/CHANGES.txt (working copy) @@ -74,6 +74,10 @@ 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 Index: lucene/src/test/org/apache/lucene/index/TestIndexReader.java =================================================================== --- lucene/src/test/org/apache/lucene/index/TestIndexReader.java (revision 1033824) +++ lucene/src/test/org/apache/lucene/index/TestIndexReader.java (working copy) @@ -1802,4 +1802,30 @@ 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 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(); + } + } Index: lucene/src/java/org/apache/lucene/index/IndexCommit.java =================================================================== --- lucene/src/java/org/apache/lucene/index/IndexCommit.java (revision 1033824) +++ lucene/src/java/org/apache/lucene/index/IndexCommit.java (working copy) @@ -20,6 +20,7 @@ import java.util.Collection; import java.util.Map; import java.io.IOException; + import org.apache.lucene.store.Directory; /** @@ -40,7 +41,7 @@ * @lucene.experimental */ -public abstract class IndexCommit { +public abstract class IndexCommit implements Comparable { /** * Get the segments file (segments_N) associated @@ -114,4 +115,16 @@ * String -> String. */ public abstract Map 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; + } + } + } Index: lucene/src/java/org/apache/lucene/index/DirectoryReader.java =================================================================== --- lucene/src/java/org/apache/lucene/index/DirectoryReader.java (revision 1033824) +++ lucene/src/java/org/apache/lucene/index/DirectoryReader.java (working copy) @@ -25,6 +25,7 @@ import java.util.Collections; import java.util.HashMap; import java.util.HashSet; +import java.util.List; import java.util.Map; import java.util.Set; @@ -912,10 +913,10 @@ } /** @see org.apache.lucene.index.IndexReader#listCommits */ - public static Collection listCommits(Directory dir) throws IOException { + public static List listCommits(Directory dir) throws IOException { final String[] files = dir.listAll(); - Collection commits = new ArrayList(); + List commits = new ArrayList(); SegmentInfos latest = new SegmentInfos(); latest.read(dir); @@ -952,6 +953,9 @@ } } + // Ensure that the commit points are sorted in ascending order. + Collections.sort(commits); + return commits; } Index: lucene/src/java/org/apache/lucene/index/IndexReader.java =================================================================== --- lucene/src/java/org/apache/lucene/index/IndexReader.java (revision 1033824) +++ lucene/src/java/org/apache/lucene/index/IndexReader.java (working copy) @@ -27,8 +27,8 @@ 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 @@ * 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 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 listCommits(Directory dir) throws IOException { return DirectoryReader.listCommits(dir); } Index: lucene/src/java/org/apache/lucene/index/IndexFileDeleter.java =================================================================== --- lucene/src/java/org/apache/lucene/index/IndexFileDeleter.java (revision 1033824) +++ lucene/src/java/org/apache/lucene/index/IndexFileDeleter.java (working copy) @@ -612,9 +612,8 @@ * equals. */ - final private static class CommitPoint extends IndexCommit implements Comparable { + final private static class CommitPoint extends IndexCommit { - long gen; Collection files; String segmentsFileName; boolean deleted; @@ -633,7 +632,6 @@ version = segmentInfos.getVersion(); generation = segmentInfos.getGeneration(); files = Collections.unmodifiableCollection(segmentInfos.files(directory, true)); - gen = segmentInfos.getGeneration(); isOptimized = segmentInfos.size() == 1 && !segmentInfos.info(0).hasDeletions(); } @@ -689,14 +687,5 @@ return deleted; } - public int compareTo(CommitPoint commit) { - if (gen < commit.gen) { - return -1; - } else if (gen > commit.gen) { - return 1; - } else { - return 0; - } - } } }