LUCENE-1325: add IndexCommit.isOptimized() method

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@673979 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael McCandless 2008-07-04 09:43:12 +00:00
parent 4099732195
commit 22f76f1ebf
6 changed files with 41 additions and 2 deletions

View File

@ -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

View File

@ -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;

View File

@ -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.
*/

View File

@ -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;
@ -596,6 +597,11 @@ final class IndexFileDeleter {
files.addAll(segmentInfo.files());
}
}
isOptimized = segmentInfos.size() == 1 && !segmentInfos.info(0).hasDeletions();
}
public boolean isOptimized() {
return isOptimized;
}
public String getSegmentsFileName() {

View File

@ -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)

View File

@ -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();