From 22f76f1ebf65d89cb1b04c9740918c8843ac0d5e Mon Sep 17 00:00:00 2001 From: Michael McCandless Date: Fri, 4 Jul 2008 09:43:12 +0000 Subject: [PATCH] LUCENE-1325: add IndexCommit.isOptimized() method git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@673979 13f79535-47bb-0310-9956-ffa450edef68 --- CHANGES.txt | 3 +++ .../org/apache/lucene/index/DirectoryIndexReader.java | 6 ++++++ src/java/org/apache/lucene/index/IndexCommit.java | 7 +++++++ src/java/org/apache/lucene/index/IndexFileDeleter.java | 8 +++++++- .../org/apache/lucene/index/TestDeletionPolicy.java | 9 ++++++++- src/test/org/apache/lucene/index/TestIndexReader.java | 10 ++++++++++ 6 files changed, 41 insertions(+), 2 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index d94f3882c8b..6045690a2d5 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -83,6 +83,9 @@ API Changes DeletionPolicy that keeps more than the last commit around. (Jason Rutherglen via Mike McCandless) +12. LUCENE-1325: Added IndexCommit.isOptimized(). (Shalin Shekhar + Mangar 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 eba9d7c577c..5d5acb8a04c 100644 --- a/src/java/org/apache/lucene/index/DirectoryIndexReader.java +++ b/src/java/org/apache/lucene/index/DirectoryIndexReader.java @@ -360,6 +360,7 @@ abstract class DirectoryIndexReader extends IndexReader { Directory dir; long generation; long version; + final boolean isOptimized; ReaderCommit(SegmentInfos infos, Directory dir) throws IOException { segmentsFileName = infos.getCurrentSegmentFileName(); @@ -374,6 +375,11 @@ abstract class DirectoryIndexReader extends IndexReader { } version = infos.getVersion(); generation = infos.getGeneration(); + isOptimized = infos.size() == 1 && !infos.info(0).hasDeletions(); + } + + public boolean isOptimized() { + return isOptimized; } public String getSegmentsFileName() { return segmentsFileName; diff --git a/src/java/org/apache/lucene/index/IndexCommit.java b/src/java/org/apache/lucene/index/IndexCommit.java index 2e9936a5c50..8e567f553c8 100644 --- a/src/java/org/apache/lucene/index/IndexCommit.java +++ b/src/java/org/apache/lucene/index/IndexCommit.java @@ -74,6 +74,13 @@ public abstract class IndexCommit implements IndexCommitPoint { throw new UnsupportedOperationException("This IndexCommit does not support this method."); } + /** + * Returns true if this commit is an optimized index. + */ + public boolean isOptimized() { + throw new UnsupportedOperationException("This IndexCommit does not support this method."); + } + /** * Two IndexCommits are equal if both their Directory and versions are equal. */ diff --git a/src/java/org/apache/lucene/index/IndexFileDeleter.java b/src/java/org/apache/lucene/index/IndexFileDeleter.java index b21706d4f63..7e0a11d74aa 100644 --- a/src/java/org/apache/lucene/index/IndexFileDeleter.java +++ b/src/java/org/apache/lucene/index/IndexFileDeleter.java @@ -579,6 +579,7 @@ final class IndexFileDeleter { Collection commitsToDelete; long version; long generation; + final boolean isOptimized; public CommitPoint(Collection commitsToDelete, Directory directory, SegmentInfos segmentInfos) throws IOException { this.directory = directory; @@ -595,7 +596,12 @@ final class IndexFileDeleter { if (segmentInfo.dir == directory) { files.addAll(segmentInfo.files()); } - } + } + isOptimized = segmentInfos.size() == 1 && !segmentInfos.info(0).hasDeletions(); + } + + public boolean isOptimized() { + return isOptimized; } public String getSegmentsFileName() { diff --git a/src/test/org/apache/lucene/index/TestDeletionPolicy.java b/src/test/org/apache/lucene/index/TestDeletionPolicy.java index 97901b2f604..278af0462e1 100644 --- a/src/test/org/apache/lucene/index/TestDeletionPolicy.java +++ b/src/test/org/apache/lucene/index/TestDeletionPolicy.java @@ -61,11 +61,16 @@ public class TestDeletionPolicy extends LuceneTestCase class KeepAllDeletionPolicy implements IndexDeletionPolicy { int numOnInit; int numOnCommit; + Directory dir; public void onInit(List commits) { verifyCommitOrder(commits); numOnInit++; } - public void onCommit(List commits) { + public void onCommit(List commits) throws IOException { + IndexCommit lastCommit = (IndexCommit) commits.get(commits.size()-1); + IndexReader r = IndexReader.open(dir); + assertEquals("lastCommit.isOptimized()=" + lastCommit.isOptimized() + " vs IndexReader.isOptimized=" + r.isOptimized(), r.isOptimized(), lastCommit.isOptimized()); + r.close(); verifyCommitOrder(commits); numOnCommit++; } @@ -263,10 +268,12 @@ public class TestDeletionPolicy extends LuceneTestCase KeepAllDeletionPolicy policy = new KeepAllDeletionPolicy(); Directory dir = new RAMDirectory(); + policy.dir = dir; IndexWriter writer = new IndexWriter(dir, autoCommit, new WhitespaceAnalyzer(), true, policy, IndexWriter.MaxFieldLength.LIMITED); writer.setMaxBufferedDocs(10); writer.setUseCompoundFile(useCompoundFile); + writer.setMergeScheduler(new SerialMergeScheduler()); for(int i=0;i<107;i++) { addDoc(writer); if (autoCommit && i%10 == 0) diff --git a/src/test/org/apache/lucene/index/TestIndexReader.java b/src/test/org/apache/lucene/index/TestIndexReader.java index 6b188f12152..6678a4961a6 100644 --- a/src/test/org/apache/lucene/index/TestIndexReader.java +++ b/src/test/org/apache/lucene/index/TestIndexReader.java @@ -1315,6 +1315,16 @@ public class TestIndexReader extends LuceneTestCase IndexReader r2 = r.reopen(); assertFalse(c.equals(r2.getIndexCommit())); + assertFalse(r2.getIndexCommit().isOptimized()); + r2.close(); + + writer = new IndexWriter(d, new StandardAnalyzer(), false, IndexWriter.MaxFieldLength.LIMITED); + writer.optimize(); + writer.close(); + + r2 = r.reopen(); + assertTrue(r2.getIndexCommit().isOptimized()); + r.close(); r2.close(); d.close();