Index: CHANGES.txt =================================================================== --- CHANGES.txt (revision 671218) +++ CHANGES.txt (working copy) @@ -83,6 +83,9 @@ 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 Index: src/test/org/apache/lucene/index/TestIndexReader.java =================================================================== --- src/test/org/apache/lucene/index/TestIndexReader.java (revision 671218) +++ src/test/org/apache/lucene/index/TestIndexReader.java (working copy) @@ -1315,6 +1315,16 @@ 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(); Index: src/test/org/apache/lucene/index/TestDeletionPolicy.java =================================================================== --- src/test/org/apache/lucene/index/TestDeletionPolicy.java (revision 671218) +++ src/test/org/apache/lucene/index/TestDeletionPolicy.java (working copy) @@ -61,11 +61,16 @@ 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 @@ 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) Index: src/java/org/apache/lucene/index/DirectoryIndexReader.java =================================================================== --- src/java/org/apache/lucene/index/DirectoryIndexReader.java (revision 671218) +++ src/java/org/apache/lucene/index/DirectoryIndexReader.java (working copy) @@ -360,6 +360,7 @@ Directory dir; long generation; long version; + final boolean isOptimized; ReaderCommit(SegmentInfos infos, Directory dir) throws IOException { segmentsFileName = infos.getCurrentSegmentFileName(); @@ -374,7 +375,12 @@ } version = infos.getVersion(); generation = infos.getGeneration(); + isOptimized = infos.size() == 1 && !infos.info(0).hasDeletions(); } + + public boolean isOptimized() { + return isOptimized; + } public String getSegmentsFileName() { return segmentsFileName; } Index: src/java/org/apache/lucene/index/IndexCommit.java =================================================================== --- src/java/org/apache/lucene/index/IndexCommit.java (revision 671218) +++ src/java/org/apache/lucene/index/IndexCommit.java (working copy) @@ -75,6 +75,13 @@ } /** + * 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. */ public boolean equals(Object other) { Index: src/java/org/apache/lucene/index/IndexFileDeleter.java =================================================================== --- src/java/org/apache/lucene/index/IndexFileDeleter.java (revision 671218) +++ src/java/org/apache/lucene/index/IndexFileDeleter.java (working copy) @@ -579,6 +579,7 @@ Collection commitsToDelete; long version; long generation; + final boolean isOptimized; public CommitPoint(Collection commitsToDelete, Directory directory, SegmentInfos segmentInfos) throws IOException { this.directory = directory; @@ -595,9 +596,14 @@ if (segmentInfo.dir == directory) { files.addAll(segmentInfo.files()); } - } + } + isOptimized = segmentInfos.size() == 1 && !segmentInfos.info(0).hasDeletions(); } + public boolean isOptimized() { + return isOptimized; + } + public String getSegmentsFileName() { return segmentsFileName; }