Skip to content

Apache Lucene open-source search software

LUCENE-1624: don't write a new empty commit when create=true and autoCommit=false on creating IndexWriter, unless dir has no index yet

Michael McCandlessbaa4034c0127
diff --git a/CHANGES.txt b/CHANGES.txt
index 4b0c7224a3c..3cbba3ff6c6 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -57,6 +57,11 @@ Changes in runtime behavior
compatible behavior) but in 3.0 will be hardwired to true. (Shon
Vella via Mike McCandless).
+ 4. LUCENE-1624: If you open IndexWriter with create=true and
+ autoCommit=false on an existing index, IndexWriter no longer
+ writes an empty commit when it's created. (Paul Taylor via Mike
+ McCandless)
+
API Changes
1. LUCENE-1419: Add expert API to set custom indexing chain. This API is
diff --git a/common-build.xml b/common-build.xml
index 9730308d996..1dec3cc14be 100644
--- a/common-build.xml
+++ b/common-build.xml
@@ -41,7 +41,7 @@
<property name="name" value="${ant.project.name}"/>
<property name="Name" value="Lucene"/>
<property name="version" value="2.9-dev"/>
- <property name="compatibility.tag" value="lucene_2_4_back_compat_tests_20090428"/>
+ <property name="compatibility.tag" value="lucene_2_4_back_compat_tests_20090501"/>
<property name="spec.version" value="${version}"/>
<property name="year" value="2000-${current.year}"/>
<property name="final.name" value="lucene-${name}-${version}"/>
diff --git a/src/java/org/apache/lucene/index/IndexWriter.java b/src/java/org/apache/lucene/index/IndexWriter.java
index 3296626dc50..f27be14256a 100644
--- a/src/java/org/apache/lucene/index/IndexWriter.java
+++ b/src/java/org/apache/lucene/index/IndexWriter.java
@@ -1445,13 +1445,27 @@ public class IndexWriter {
// against an index that's currently open for
// searching. In this case we write the next
// segments_N file with no segments:
+ boolean doCommit;
try {
segmentInfos.read(directory);
segmentInfos.clear();
+ doCommit = false;
} catch (IOException e) {
// Likely this means it's a fresh directory
+ doCommit = true;
+ }
+
+ if (autoCommit || doCommit) {
+ // Always commit if autoCommit=true, else only
+ // commit if there is no segments file in this dir
+ // already.
+ segmentInfos.commit(directory);
+ synced.addAll(segmentInfos.files(directory, true));
+ } else {
+ // Record that we have a change (zero out all
+ // segments) pending:
+ changeCount++;
}
- segmentInfos.commit(directory);
} else {
segmentInfos.read(directory);
diff --git a/src/test/org/apache/lucene/index/TestDeletionPolicy.java b/src/test/org/apache/lucene/index/TestDeletionPolicy.java
index 03793ecdff7..6f089337453 100644
--- a/src/test/org/apache/lucene/index/TestDeletionPolicy.java
+++ b/src/test/org/apache/lucene/index/TestDeletionPolicy.java
@@ -702,7 +702,7 @@ public class TestDeletionPolicy extends LuceneTestCase
assertEquals(1+3*(N+1), policy.numOnInit);
if (!autoCommit)
- assertEquals(2*(N+1), policy.numOnCommit);
+ assertEquals(3*(N+1), policy.numOnCommit);
IndexSearcher searcher = new IndexSearcher(dir);
ScoreDoc[] hits = searcher.search(query, null, 1000).scoreDocs;