Apache Lucene open-source search software
LUCENE-8569: Never count soft-deletes if reader has no hard-deletes
Simon Willnauer56cb42d200dd
diff --git a/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java b/lucene/core/src/java/org/apache/lucene/index/IndexWriter.javaindex 028554b5eda..f8415829cfd 100644--- a/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java+++ b/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java@@ -69,6 +69,7 @@ import org.apache.lucene.util.ArrayUtil;import org.apache.lucene.util.Bits;import org.apache.lucene.util.BytesRef;import org.apache.lucene.util.Constants;+import org.apache.lucene.util.Counter;import org.apache.lucene.util.IOUtils;import org.apache.lucene.util.InfoStream;import org.apache.lucene.util.StringHelper;@@ -4352,6 +4353,36 @@ public class IndexWriter implements Closeable, TwoPhaseCommit, Accountable,}}+ private void countSoftDeletes(CodecReader reader, Bits wrappedLiveDocs, Bits hardLiveDocs, Counter softDeleteCounter,+ Counter hardDeleteCounter) throws IOException {+ int hardDeleteCount = 0;+ int softDeletesCount = 0;+ DocIdSetIterator softDeletedDocs = DocValuesFieldExistsQuery.getDocValuesDocIdSetIterator(config.getSoftDeletesField(), reader);+ if (softDeletedDocs != null) {+ int docId;+ while ((docId = softDeletedDocs.nextDoc()) != DocIdSetIterator.NO_MORE_DOCS) {+ if (wrappedLiveDocs == null || wrappedLiveDocs.get(docId)) {+ if (hardLiveDocs == null || hardLiveDocs.get(docId)) {+ softDeletesCount++;+ } else {+ hardDeleteCount++;+ }+ }+ }+ }+ softDeleteCounter.addAndGet(softDeletesCount);+ hardDeleteCounter.addAndGet(hardDeleteCount);+ }++ private boolean assertSoftDeletesCount(CodecReader reader, int expectedCount) throws IOException {+ Counter count = Counter.newCounter(false);+ Counter hardDeletes = Counter.newCounter(false);+ countSoftDeletes(reader, reader.getLiveDocs(), null, count, hardDeletes);+ assert count.get() == expectedCount : "soft-deletes count mismatch expected: "+ + expectedCount + " but actual: " + count.get() ;+ return true;+ }+/** Does the actual (time-consuming) work of the merge,* but without holding synchronized lock on IndexWriter* instance */@@ -4400,7 +4431,7 @@ public class IndexWriter implements Closeable, TwoPhaseCommit, Accountable,// Let the merge wrap readersList<CodecReader> mergeReaders = new ArrayList<>();- int softDeleteCount = 0;+ Counter softDeleteCount = Counter.newCounter(false);for (int r = 0; r < merge.readers.size(); r++) {SegmentReader reader = merge.readers.get(r);CodecReader wrappedReader = merge.wrapForMerge(reader);@@ -4408,34 +4439,31 @@ public class IndexWriter implements Closeable, TwoPhaseCommit, Accountable,if (softDeletesEnabled) {if (reader != wrappedReader) { // if we don't have a wrapped reader we won't preserve any soft-deletesBits hardLiveDocs = merge.hardLiveDocs.get(r);- Bits wrappedLiveDocs = wrappedReader.getLiveDocs();- int hardDeleteCount = 0;- DocIdSetIterator softDeletedDocs = DocValuesFieldExistsQuery.getDocValuesDocIdSetIterator(config.getSoftDeletesField(), wrappedReader);- if (softDeletedDocs != null) {- int docId;- while ((docId = softDeletedDocs.nextDoc()) != DocIdSetIterator.NO_MORE_DOCS) {- if (wrappedLiveDocs == null || wrappedLiveDocs.get(docId)) {- if (hardLiveDocs == null || hardLiveDocs.get(docId)) {- softDeleteCount++;- } else {- hardDeleteCount++;+ if (hardLiveDocs != null) { // we only need to do this accounting if we have mixed deletes+ Bits wrappedLiveDocs = wrappedReader.getLiveDocs();+ Counter hardDeleteCounter = Counter.newCounter(false);+ countSoftDeletes(wrappedReader, wrappedLiveDocs, hardLiveDocs, softDeleteCount, hardDeleteCounter);+ int hardDeleteCount = Math.toIntExact(hardDeleteCounter.get());+ // Wrap the wrapped reader again if we have excluded some hard-deleted docs+ if (hardDeleteCount > 0) {+ Bits liveDocs = wrappedLiveDocs == null ? hardLiveDocs : new Bits() {+ @Override+ public boolean get(int index) {+ return hardLiveDocs.get(index) && wrappedLiveDocs.get(index);}- }++ @Override+ public int length() {+ return hardLiveDocs.length();+ }+ };+ wrappedReader = FilterCodecReader.wrapLiveDocs(wrappedReader, liveDocs, wrappedReader.numDocs() - hardDeleteCount);}- }- // Wrap the wrapped reader again if we have excluded some hard-deleted docs- if (hardLiveDocs != null && hardDeleteCount > 0) {- Bits liveDocs = wrappedLiveDocs == null ? hardLiveDocs : new Bits() {- @Override- public boolean get(int index) {- return hardLiveDocs.get(index) && wrappedLiveDocs.get(index);- }- @Override- public int length() {- return hardLiveDocs.length();- }- };- wrappedReader = FilterCodecReader.wrapLiveDocs(wrappedReader, liveDocs, wrappedReader.numDocs() - hardDeleteCount);+ } else {+ final int carryOverSoftDeletes = reader.getSegmentInfo().getSoftDelCount() - wrappedReader.numDeletedDocs();+ assert carryOverSoftDeletes >= 0 : "carry-over soft-deletes must be positive";+ assert assertSoftDeletesCount(wrappedReader, carryOverSoftDeletes);+ softDeleteCount.addAndGet(carryOverSoftDeletes);}}}@@ -4445,7 +4473,7 @@ public class IndexWriter implements Closeable, TwoPhaseCommit, Accountable,merge.info.info, infoStream, dirWrapper,globalFieldNumberMap,context);- merge.info.setSoftDelCount(softDeleteCount);+ merge.info.setSoftDelCount(Math.toIntExact(softDeleteCount.get()));merge.checkAborted();merge.mergeStartNS = System.nanoTime();