Skip to content

Commit

Permalink
increase coverage
Browse files Browse the repository at this point in the history
Signed-off-by: bowenlan-amzn <[email protected]>
  • Loading branch information
bowenlan-amzn committed Jan 16, 2025
1 parent d7db6da commit 97ee7e0
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ public int hashCode() {

@Override
public long ramBytesUsed() {
return RamUsageEstimator.shallowSizeOfInstance(BitmapDocValuesQuery.class) + RamUsageEstimator.sizeOfObject(field)
+ RamUsageEstimator.sizeOfObject(bitmap);
return RamUsageEstimator.shallowSizeOfInstance(BitmapIndexQuery.class) + RamUsageEstimator.sizeOf(field) + bitmap
.getLongSizeInBytes();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ public int hashCode() {

@Override
public long ramBytesUsed() {
return RamUsageEstimator.shallowSizeOfInstance(BitmapIndexQuery.class) + RamUsageEstimator.sizeOfObject(field) + RamUsageEstimator
.sizeOfObject(bitmap);
return RamUsageEstimator.shallowSizeOfInstance(BitmapIndexQuery.class) + RamUsageEstimator.sizeOf(field) + bitmap
.getLongSizeInBytes();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -970,6 +970,9 @@ public void testBitmapQuery() throws IOException {
ft = new NumberFieldType("field", NumberType.INTEGER, false, false, true, true, null, Collections.emptyMap());
assertEquals(new BitmapDocValuesQuery("field", r), ft.bitmapQuery(bitmap));

ft = new NumberFieldType("field", NumberType.INTEGER, true, false, false, true, null, Collections.emptyMap());
assertEquals(new BitmapIndexQuery("field", r), ft.bitmapQuery(bitmap));

Directory dir = newDirectory();
IndexWriter w = new IndexWriter(dir, new IndexWriterConfig());
DirectoryReader reader = DirectoryReader.open(w);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
import org.apache.lucene.index.SortedNumericDocValues;
import org.apache.lucene.search.DocIdSetIterator;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.MatchNoDocsQuery;
import org.apache.lucene.search.ScoreMode;
import org.apache.lucene.search.Scorer;
import org.apache.lucene.search.ScorerSupplier;
import org.apache.lucene.search.Weight;
import org.apache.lucene.store.Directory;
import org.opensearch.common.Randomness;
Expand Down Expand Up @@ -49,6 +51,7 @@ public class BitmapIndexQueryTests extends OpenSearchTestCase {
public void initSearcher() throws IOException {
dir = newDirectory();
w = new IndexWriter(dir, newIndexWriterConfig());
reader = DirectoryReader.open(w);
}

@After
Expand Down Expand Up @@ -179,4 +182,57 @@ public void testRandomDocumentsAndQueries() throws IOException {
assertEquals(expected, actual);
}

public void testCheckArgsNullBitmap() {
/**
* Test that checkArgs throws IllegalArgumentException when bitmap is null
*/
assertThrows(IllegalArgumentException.class, () -> BitmapIndexQuery.checkArgs("field", null));
}

public void testCheckArgsNullField() {
/**
* Test that checkArgs throws IllegalArgumentException when field is null
*/
RoaringBitmap bitmap = new RoaringBitmap();
assertThrows(IllegalArgumentException.class, () -> BitmapIndexQuery.checkArgs(null, bitmap));
}

public void testCheckArgsWithNullBitmap() {
assertThrows(IllegalArgumentException.class, () -> { BitmapIndexQuery.checkArgs("product_id", null); });
}

public void testCheckArgsWithNullFieldAndBitmap() {
IllegalArgumentException exception = expectThrows(
IllegalArgumentException.class,
() -> { BitmapIndexQuery.checkArgs(null, null); }
);
assertEquals("field must not be null", exception.getMessage());
}

public void testCreateWeight() throws IOException {
Document d = new Document();
d.add(new IntField("product_id", 4, Field.Store.NO));
w.addDocument(d);

w.commit();
reader = DirectoryReader.open(w);
searcher = newSearcher(reader);
RoaringBitmap bitmap = new RoaringBitmap();
bitmap.add(1);
BitmapIndexQuery query = new BitmapIndexQuery("product_id", bitmap);
Weight weight = query.createWeight(searcher, ScoreMode.COMPLETE_NO_SCORES, 1f);
assertNotNull(weight);
Scorer scorer = weight.scorer(reader.leaves().get(0));
assertNotNull(scorer);
ScorerSupplier supplier = weight.scorerSupplier(reader.leaves().get(0));
assertNotNull(supplier);
long cost = supplier.cost();
assertEquals(20, cost);
}

public void testRewrite() throws IOException {
RoaringBitmap bitmap = new RoaringBitmap();
BitmapIndexQuery query = new BitmapIndexQuery("product_id", bitmap);
assertEquals(new MatchNoDocsQuery(), query.rewrite(searcher));
}
}

0 comments on commit 97ee7e0

Please sign in to comment.