salvatorecampagna commented on code in PR #16623:
URL: https://github.com/apache/lucene/pull/16623#discussion_r4295335928


##########
lucene/core/src/java/org/apache/lucene/util/FixedBitSet.java:
##########
@@ -871,9 +871,15 @@ public static FixedBitSet copyOf(Bits bits) {
       int length = bits.length();
       FixedBitSet bitSet = new FixedBitSet(length);
       bitSet.set(0, length);
-      for (int i = 0; i < length; ++i) {
-        if (bits.get(i) == false) {
-          bitSet.clear(i);
+      if (bits instanceof LiveDocs) {

Review Comment:
   This assumes every `LiveDocs` overrides `applyMask` with a bulk 
implementation, but `LiveDocs` does not document that expectation. 
`AssertingLeafReader.AssertingLiveDocs` does not override it, so it takes the 
default, which the comment below calls several times slower than the loop. That 
only affects tests today.
   
   Nit: should `AssertingLiveDocs` override `applyMask` and keep its thread 
check, and should `LiveDocs` say implementations are expected to override it? 
The wrapper part could be a follow up.



##########
lucene/core/src/test/org/apache/lucene/util/TestLiveDocsCopyOf.java:
##########
@@ -0,0 +1,142 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.lucene.util;
+
+import org.apache.lucene.tests.util.LuceneTestCase;
+
+/** Tests for {@link FixedBitSet#copyOf(Bits)} on {@link LiveDocs} 
implementations. */
+public class TestLiveDocsCopyOf extends LuceneTestCase {
+
+  public void testDenseLiveDocsCopyOf() {
+    int maxDoc = atLeast(1000);
+    FixedBitSet liveBits = new FixedBitSet(maxDoc);
+    liveBits.set(0, maxDoc);
+    // Delete some known positions
+    liveBits.clear(0);
+    liveBits.clear(42);
+    liveBits.clear(maxDoc - 1);
+
+    DenseLiveDocs dense = DenseLiveDocs.builder(liveBits, maxDoc).build();
+    FixedBitSet copy = FixedBitSet.copyOf(dense);
+
+    assertEquals(maxDoc, copy.length());
+    for (int i = 0; i < maxDoc; i++) {
+      assertEquals("mismatch at doc " + i, dense.get(i), copy.get(i));
+    }
+
+    // PendingDeletes mutates the copy and publishes it, so it must not share 
state with the source
+    copy.clear(100);
+    assertTrue(dense.get(100));
+  }
+
+  public void testSparseLiveDocsCopyOf() {
+    int maxDoc = atLeast(1000);
+    SparseFixedBitSet deletedDocs = new SparseFixedBitSet(maxDoc);
+    deletedDocs.set(0);
+    deletedDocs.set(42);
+    deletedDocs.set(maxDoc - 1);
+
+    SparseLiveDocs sparse = SparseLiveDocs.builder(deletedDocs, 
maxDoc).build();
+    FixedBitSet copy = FixedBitSet.copyOf(sparse);
+
+    assertEquals(maxDoc, copy.length());
+    for (int i = 0; i < maxDoc; i++) {
+      assertEquals("mismatch at doc " + i, sparse.get(i), copy.get(i));
+    }
+
+    copy.clear(100);
+    assertTrue(sparse.get(100));
+  }
+
+  public void testBoundarySizes() {
+    // Word (64) and SparseFixedBitSet block (4096) boundaries
+    for (int maxDoc : new int[] {1, 63, 64, 65, 4095, 4096, 4097}) {
+      int[] allDocs = new int[maxDoc];
+      for (int i = 0; i < maxDoc; i++) {
+        allDocs[i] = i;
+      }
+      assertCopyOf(maxDoc, allDocs);
+      assertCopyOf(maxDoc, new int[] {random().nextInt(maxDoc)});
+      assertCopyOf(maxDoc, new int[] {maxDoc - 1});
+    }
+  }
+
+  private static void assertCopyOf(int maxDoc, int[] deleted) {
+    FixedBitSet liveBits = new FixedBitSet(maxDoc);
+    liveBits.set(0, maxDoc);
+    SparseFixedBitSet deletedDocs = new SparseFixedBitSet(maxDoc);
+    FixedBitSet reference = new FixedBitSet(maxDoc);
+    reference.set(0, maxDoc);
+    for (int doc : deleted) {
+      liveBits.clear(doc);
+      deletedDocs.set(doc);
+      reference.clear(doc);
+    }
+
+    DenseLiveDocs dense = DenseLiveDocs.builder(liveBits, maxDoc).build();
+    SparseLiveDocs sparse = SparseLiveDocs.builder(deletedDocs, 
maxDoc).build();
+
+    assertEquals(reference, FixedBitSet.copyOf(dense));
+    assertEquals(reference, FixedBitSet.copyOf(sparse));
+  }
+
+  public void testCopyOfPaddedDenseLiveDocsPreservesLength() {

Review Comment:
   Should we also cover a padded SparseLiveDocs, like the dense case? It would 
catch a sparse specific copy that sizes the result from the backing set, which 
is what clone() did for dense.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to