|
13 | 13 | import org.apache.lucene.codecs.Codec; |
14 | 14 | import org.apache.lucene.codecs.FilterCodec; |
15 | 15 | 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; |
16 | 25 | import org.apache.lucene.index.VectorEncoding; |
17 | 26 | import org.apache.lucene.index.VectorSimilarityFunction; |
| 27 | +import org.apache.lucene.store.Directory; |
18 | 28 | import org.apache.lucene.tests.index.BaseKnnVectorsFormatTestCase; |
19 | 29 | import org.apache.lucene.tests.util.TestUtil; |
20 | 30 | import org.elasticsearch.common.logging.LogConfigurator; |
| 31 | +import org.elasticsearch.index.codec.vectors.reflect.OffHeapByteSizeUtils; |
21 | 32 | import org.junit.Before; |
22 | 33 |
|
| 34 | +import java.io.IOException; |
23 | 35 | import java.util.List; |
24 | 36 | import java.util.Locale; |
25 | 37 |
|
@@ -94,4 +106,26 @@ public void testLimits() { |
94 | 106 | expectThrows(IllegalArgumentException.class, () -> new IVFVectorsFormat(MIN_VECTORS_PER_CLUSTER - 1)); |
95 | 107 | expectThrows(IllegalArgumentException.class, () -> new IVFVectorsFormat(MAX_VECTORS_PER_CLUSTER + 1)); |
96 | 108 | } |
| 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 | + } |
97 | 131 | } |
0 commit comments