From d8c8c0e2c0f970c18bf8730e5c381efdba54f095 Mon Sep 17 00:00:00 2001 From: Michael McCandless Date: Wed, 28 May 2008 11:00:33 +0000 Subject: [PATCH] LUCENE-1288: add getVersion, getGeneration to IndexCommit git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@660904 13f79535-47bb-0310-9956-ffa450edef68 --- CHANGES.txt | 5 +++++ .../apache/lucene/index/DirectoryIndexReader.java | 10 ++++++++++ src/java/org/apache/lucene/index/IndexCommit.java | 13 +++++++++++++ .../org/apache/lucene/index/IndexFileDeleter.java | 12 ++++++++++++ .../apache/lucene/index/SnapshotDeletionPolicy.java | 6 ++++++ .../org/apache/lucene/index/TestDeletionPolicy.java | 11 +++++++++-- 6 files changed, 55 insertions(+), 2 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index a4e8d8427fe..d1c4f5cba76 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -71,6 +71,11 @@ API Changes and remove all references to these classes from the core. Also update demos and tutorials. (Michael Busch) +10. LUCENE-1288: Add getVersion() and getGeneration() to IndexCommit. + getVersion() returns the same value that IndexReader.getVersion() + returns when the reader is opened on the same commit. (Jason + Rutherglen via Mike McCandless) + Bug fixes 1. LUCENE-1134: Fixed BooleanQuery.rewrite to only optimize a single diff --git a/src/java/org/apache/lucene/index/DirectoryIndexReader.java b/src/java/org/apache/lucene/index/DirectoryIndexReader.java index eba01233c02..316d1924599 100644 --- a/src/java/org/apache/lucene/index/DirectoryIndexReader.java +++ b/src/java/org/apache/lucene/index/DirectoryIndexReader.java @@ -343,6 +343,8 @@ abstract class DirectoryIndexReader extends IndexReader { private String segmentsFileName; Collection files; Directory dir; + long generation; + long version; ReaderCommit(SegmentInfos infos, Directory dir) throws IOException { segmentsFileName = infos.getCurrentSegmentFileName(); @@ -355,6 +357,8 @@ abstract class DirectoryIndexReader extends IndexReader { if (info.dir == dir) files.addAll(info.files()); } + version = infos.getVersion(); + generation = infos.getGeneration(); } public String getSegmentsFileName() { return segmentsFileName; @@ -365,6 +369,12 @@ abstract class DirectoryIndexReader extends IndexReader { public Directory getDirectory() { return dir; } + public long getVersion() { + return version; + } + public long getGeneration() { + return generation; + } } /** diff --git a/src/java/org/apache/lucene/index/IndexCommit.java b/src/java/org/apache/lucene/index/IndexCommit.java index e15fd790f8b..e0d40c9d6cf 100644 --- a/src/java/org/apache/lucene/index/IndexCommit.java +++ b/src/java/org/apache/lucene/index/IndexCommit.java @@ -89,4 +89,17 @@ public abstract class IndexCommit implements IndexCommitPoint { public int hashCode() { return getDirectory().hashCode() + getSegmentsFileName().hashCode(); } + + /** Returns the version for this IndexCommit. This is the + same value that {@link IndexReader#getVersion} would + return if it were opened on this commit. */ + public long getVersion() { + throw new UnsupportedOperationException("This IndexCommit does not support this method."); + } + + /** Returns the generation (the _N in segments_N) for this + IndexCommit */ + public long getGeneration() { + throw new UnsupportedOperationException("This IndexCommit does not support this method."); + } } diff --git a/src/java/org/apache/lucene/index/IndexFileDeleter.java b/src/java/org/apache/lucene/index/IndexFileDeleter.java index 863a508c627..b21706d4f63 100644 --- a/src/java/org/apache/lucene/index/IndexFileDeleter.java +++ b/src/java/org/apache/lucene/index/IndexFileDeleter.java @@ -577,11 +577,15 @@ final class IndexFileDeleter { boolean deleted; Directory directory; Collection commitsToDelete; + long version; + long generation; public CommitPoint(Collection commitsToDelete, Directory directory, SegmentInfos segmentInfos) throws IOException { this.directory = directory; this.commitsToDelete = commitsToDelete; segmentsFileName = segmentInfos.getCurrentSegmentFileName(); + version = segmentInfos.getVersion(); + generation = segmentInfos.getGeneration(); int size = segmentInfos.size(); files = new ArrayList(size); files.add(segmentsFileName); @@ -606,6 +610,14 @@ final class IndexFileDeleter { return directory; } + public long getVersion() { + return version; + } + + public long getGeneration() { + return generation; + } + /** * Called only be the deletion policy, to remove this * commit point from the index. diff --git a/src/java/org/apache/lucene/index/SnapshotDeletionPolicy.java b/src/java/org/apache/lucene/index/SnapshotDeletionPolicy.java index 3745f1cfa04..43b8557f4d1 100644 --- a/src/java/org/apache/lucene/index/SnapshotDeletionPolicy.java +++ b/src/java/org/apache/lucene/index/SnapshotDeletionPolicy.java @@ -109,6 +109,12 @@ public class SnapshotDeletionPolicy implements IndexDeletionPolicy { cp.delete(); } } + public long getVersion() { + return cp.getVersion(); + } + public long getGeneration() { + return cp.getGeneration(); + } } private List wrapCommits(List commits) { diff --git a/src/test/org/apache/lucene/index/TestDeletionPolicy.java b/src/test/org/apache/lucene/index/TestDeletionPolicy.java index c2473dd72ed..366cdd72a6d 100644 --- a/src/test/org/apache/lucene/index/TestDeletionPolicy.java +++ b/src/test/org/apache/lucene/index/TestDeletionPolicy.java @@ -42,10 +42,17 @@ import org.apache.lucene.util.LuceneTestCase; public class TestDeletionPolicy extends LuceneTestCase { private void verifyCommitOrder(List commits) { - long last = SegmentInfos.generationFromSegmentsFileName(((IndexCommit) commits.get(0)).getSegmentsFileName()); + final IndexCommit firstCommit = ((IndexCommit) commits.get(0)); + long last = SegmentInfos.generationFromSegmentsFileName(firstCommit.getSegmentsFileName()); + assertEquals(last, firstCommit.getGeneration()); + long lastVersion = firstCommit.getVersion(); for(int i=1;i last); + assertTrue("SegmentInfos versions are out-of-order", nowVersion > lastVersion); + assertEquals(now, commit.getGeneration()); last = now; } }