Skip to content

Commit 4249eee

Browse files
committed
adding test
1 parent 1f685c9 commit 4249eee

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

server/src/test/java/org/elasticsearch/index/codec/vectors/IVFVectorsFormatTests.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,25 @@
1313
import org.apache.lucene.codecs.Codec;
1414
import org.apache.lucene.codecs.FilterCodec;
1515
import org.apache.lucene.codecs.KnnVectorsFormat;
16+
import org.apache.lucene.codecs.KnnVectorsReader;
17+
import org.apache.lucene.codecs.perfield.PerFieldKnnVectorsFormat;
18+
import org.apache.lucene.document.Document;
19+
import org.apache.lucene.document.KnnFloatVectorField;
20+
import org.apache.lucene.index.CodecReader;
21+
import org.apache.lucene.index.DirectoryReader;
22+
import org.apache.lucene.index.IndexReader;
23+
import org.apache.lucene.index.IndexWriter;
24+
import org.apache.lucene.index.LeafReader;
1625
import org.apache.lucene.index.VectorEncoding;
1726
import org.apache.lucene.index.VectorSimilarityFunction;
27+
import org.apache.lucene.store.Directory;
1828
import org.apache.lucene.tests.index.BaseKnnVectorsFormatTestCase;
1929
import org.apache.lucene.tests.util.TestUtil;
2030
import org.elasticsearch.common.logging.LogConfigurator;
31+
import org.elasticsearch.index.codec.vectors.reflect.OffHeapByteSizeUtils;
2132
import org.junit.Before;
2233

34+
import java.io.IOException;
2335
import java.util.List;
2436
import java.util.Locale;
2537

@@ -94,4 +106,26 @@ public void testLimits() {
94106
expectThrows(IllegalArgumentException.class, () -> new IVFVectorsFormat(MIN_VECTORS_PER_CLUSTER - 1));
95107
expectThrows(IllegalArgumentException.class, () -> new IVFVectorsFormat(MAX_VECTORS_PER_CLUSTER + 1));
96108
}
109+
110+
public void testSimpleOffHeapSize() throws IOException {
111+
float[] vector = randomVector(random().nextInt(12, 500));
112+
try (Directory dir = newDirectory(); IndexWriter w = new IndexWriter(dir, newIndexWriterConfig())) {
113+
Document doc = new Document();
114+
doc.add(new KnnFloatVectorField("f", vector, VectorSimilarityFunction.EUCLIDEAN));
115+
w.addDocument(doc);
116+
w.commit();
117+
try (IndexReader reader = DirectoryReader.open(w)) {
118+
LeafReader r = getOnlyLeafReader(reader);
119+
if (r instanceof CodecReader codecReader) {
120+
KnnVectorsReader knnVectorsReader = codecReader.getVectorReader();
121+
if (knnVectorsReader instanceof PerFieldKnnVectorsFormat.FieldsReader fieldsReader) {
122+
knnVectorsReader = fieldsReader.getFieldReader("f");
123+
}
124+
var fieldInfo = r.getFieldInfos().fieldInfo("f");
125+
var offHeap = OffHeapByteSizeUtils.getOffHeapByteSize(knnVectorsReader, fieldInfo);
126+
assertEquals(0, offHeap.size());
127+
}
128+
}
129+
}
130+
}
97131
}

0 commit comments

Comments
 (0)