Skip to content

Commit

Permalink
Avoid allocating liveDocs for no soft-deletes (apache#13895)
Browse files Browse the repository at this point in the history
This is a continuation of apache#13588, where we avoided allocating liveDocs 
for segments that have the __soft_deletes field but no values in it.
However, that PR only addressed the reading side. This change fixes the
writing scenario with IndexWriter.

Relates apache#13588
  • Loading branch information
dnhatn authored Oct 13, 2024
1 parent 0368614 commit 9d4bba3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,14 @@ void onNewReader(CodecReader reader, SegmentCommitInfo info) throws IOException
hardDeletes.onNewReader(reader, info);
// only re-calculate this if we haven't seen this generation
if (dvGeneration < info.getDocValuesGen()) {
final DocIdSetIterator iterator =
FieldExistsQuery.getDocValuesDocIdSetIterator(field, reader);
int newDelCount;
if (iterator
!= null) { // nothing is deleted we don't have a soft deletes field in this segment
assert info.info.maxDoc() > 0 : "maxDoc is 0";
final int newDelCount;
var iterator = FieldExistsQuery.getDocValuesDocIdSetIterator(field, reader);
if (iterator != null && iterator.nextDoc() != DocIdSetIterator.NO_MORE_DOCS) {
iterator = FieldExistsQuery.getDocValuesDocIdSetIterator(field, reader);
newDelCount = applySoftDeletes(iterator, getMutableBits());
assert newDelCount >= 0 : " illegal pending delete count: " + newDelCount;
} else {
// nothing is deleted we don't have a soft deletes field in this segment
newDelCount = 0;
}
assert info.getSoftDelCount() == newDelCount
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,12 @@ public void testAvoidWrappingReadersWithoutSoftDeletes() throws Exception {
softDeletesField, MatchNoDocsQuery::new, mergePolicy));
writer.forceMerge(1);
try (DirectoryReader reader = DirectoryReader.open(writer)) {
for (LeafReaderContext leafContext : reader.leaves()) {
assertThat(leafContext.reader(), instanceOf(SegmentReader.class));
SegmentReader segmentReader = (SegmentReader) leafContext.reader();
assertNull(segmentReader.getLiveDocs());
assertNull(segmentReader.getHardLiveDocs());
}
SoftDeletesDirectoryReaderWrapper wrapped =
new SoftDeletesDirectoryReaderWrapper(reader, softDeletesField);
assertEquals(numDocs, wrapped.numDocs());
Expand Down

0 comments on commit 9d4bba3

Please sign in to comment.