|
|
@@ -18,10 +18,13 @@ import org.apache.lucene.index.IndexWriter;
|
|
|
import org.apache.lucene.index.IndexWriterConfig;
|
|
|
import org.apache.lucene.index.LeafReaderContext;
|
|
|
import org.apache.lucene.index.NoMergePolicy;
|
|
|
+import org.apache.lucene.search.DocIdSetIterator;
|
|
|
import org.apache.lucene.search.IndexSearcher;
|
|
|
import org.apache.lucene.search.Query;
|
|
|
import org.apache.lucene.store.Directory;
|
|
|
import org.apache.lucene.util.BitSet;
|
|
|
+import org.apache.lucene.util.BitSetIterator;
|
|
|
+import org.apache.lucene.util.FixedBitSet;
|
|
|
import org.elasticsearch.client.Client;
|
|
|
import org.elasticsearch.common.CheckedBiConsumer;
|
|
|
import org.elasticsearch.common.CheckedConsumer;
|
|
|
@@ -59,6 +62,7 @@ import java.util.concurrent.atomic.AtomicReference;
|
|
|
|
|
|
import static java.util.Collections.emptyMap;
|
|
|
import static org.hamcrest.Matchers.equalTo;
|
|
|
+import static org.hamcrest.Matchers.instanceOf;
|
|
|
import static org.hamcrest.Matchers.is;
|
|
|
import static org.hamcrest.Matchers.not;
|
|
|
import static org.hamcrest.Matchers.notNullValue;
|
|
|
@@ -464,6 +468,43 @@ public class DocumentSubsetBitsetCacheTests extends ESTestCase {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ public void testRoleBitSets() throws Exception {
|
|
|
+ int maxDocs = randomIntBetween(1, 1024);
|
|
|
+ int numDocs = 0;
|
|
|
+ FixedBitSet matches = new FixedBitSet(maxDocs);
|
|
|
+ for (int i = 0; i < maxDocs; i++) {
|
|
|
+ if (numDocs < maxDocs && randomBoolean()) {
|
|
|
+ numDocs ++;
|
|
|
+ matches.set(i);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ DocIdSetIterator it = new BitSetIterator(matches, randomIntBetween(0, numDocs));
|
|
|
+ BitSet bitSet = DocumentSubsetBitsetCache.bitSetFromDocIterator(it, maxDocs);
|
|
|
+ assertThat(bitSet.cardinality(), equalTo(numDocs));
|
|
|
+ assertThat(bitSet.length(), equalTo(maxDocs));
|
|
|
+ for (int i = 0; i < maxDocs; i++) {
|
|
|
+ assertThat(bitSet.get(i), equalTo(matches.get(i)));
|
|
|
+ assertThat(bitSet.nextSetBit(i), equalTo(matches.nextSetBit(i)));
|
|
|
+ assertThat(bitSet.prevSetBit(i), equalTo(matches.prevSetBit(i)));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testMatchAllRoleBitSet() throws Exception {
|
|
|
+ int maxDocs = randomIntBetween(1, 128);
|
|
|
+ FixedBitSet matches = new FixedBitSet(maxDocs);
|
|
|
+ for (int i = 0; i < maxDocs; i++) {
|
|
|
+ matches.set(i);
|
|
|
+ }
|
|
|
+ DocIdSetIterator it = new BitSetIterator(matches, randomNonNegativeLong());
|
|
|
+ BitSet bitSet = DocumentSubsetBitsetCache.bitSetFromDocIterator(it, maxDocs);
|
|
|
+ assertThat(bitSet, instanceOf(MatchAllRoleBitSet.class));
|
|
|
+ for (int i = 0; i < maxDocs; i++) {
|
|
|
+ assertTrue(bitSet.get(i));
|
|
|
+ assertThat(bitSet.nextSetBit(i), equalTo(matches.nextSetBit(i)));
|
|
|
+ assertThat(bitSet.prevSetBit(i), equalTo(matches.prevSetBit(i)));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
private void runTestOnIndex(CheckedBiConsumer<QueryShardContext, LeafReaderContext, Exception> body) throws Exception {
|
|
|
runTestOnIndices(1, ctx -> {
|
|
|
final TestIndexContext indexContext = ctx.get(0);
|