forked from apache/lucene
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VectorSandboxVamanaVectorsReader.java
873 lines (776 loc) · 28.6 KB
/
VectorSandboxVamanaVectorsReader.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
/*
* 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.codecs.vectorsandbox;
import static org.apache.lucene.search.DocIdSetIterator.NO_MORE_DOCS;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.Map;
import org.apache.lucene.codecs.CodecUtil;
import org.apache.lucene.codecs.KnnVectorsReader;
import org.apache.lucene.codecs.VamanaGraphProvider;
import org.apache.lucene.index.ByteVectorValues;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.FieldInfo;
import org.apache.lucene.index.FieldInfos;
import org.apache.lucene.index.FloatVectorValues;
import org.apache.lucene.index.IndexFileNames;
import org.apache.lucene.index.SegmentReadState;
import org.apache.lucene.index.VectorEncoding;
import org.apache.lucene.index.VectorSimilarityFunction;
import org.apache.lucene.search.KnnCollector;
import org.apache.lucene.store.ChecksumIndexInput;
import org.apache.lucene.store.DataInput;
import org.apache.lucene.store.IndexInput;
import org.apache.lucene.store.RandomAccessInput;
import org.apache.lucene.util.Accountable;
import org.apache.lucene.util.Bits;
import org.apache.lucene.util.IOUtils;
import org.apache.lucene.util.RamUsageEstimator;
import org.apache.lucene.util.ScalarQuantizer;
import org.apache.lucene.util.packed.DirectMonotonicReader;
import org.apache.lucene.util.vamana.OrdinalTranslatedKnnCollector;
import org.apache.lucene.util.vamana.RandomAccessVectorValues;
import org.apache.lucene.util.vamana.RandomVectorScorer;
import org.apache.lucene.util.vamana.VamanaGraph;
import org.apache.lucene.util.vamana.VamanaGraphSearcher;
/**
* Reads vectors from the index segments along with index data structures supporting KNN search.
*
* @lucene.experimental
*/
public final class VectorSandboxVamanaVectorsReader extends KnnVectorsReader
implements QuantizedVectorsReader, VamanaGraphProvider {
private static final long SHALLOW_SIZE =
RamUsageEstimator.shallowSizeOfInstance(VectorSandboxVamanaVectorsFormat.class);
private final FieldInfos fieldInfos;
private final Map<String, FieldEntry> fields = new HashMap<>();
private final IndexInput vectorData;
private final IndexInput vectorIndex;
private final IndexInput quantizedVectorData;
private final VectorSandboxScalarQuantizedVectorsReader quantizedVectorsReader;
VectorSandboxVamanaVectorsReader(SegmentReadState state) throws IOException {
this.fieldInfos = state.fieldInfos;
int versionMeta = readMetadata(state);
boolean success = false;
try {
vectorData =
openDataInput(
state,
versionMeta,
VectorSandboxVamanaVectorsFormat.VECTOR_DATA_EXTENSION,
VectorSandboxVamanaVectorsFormat.VECTOR_DATA_CODEC_NAME);
vectorIndex =
openDataInput(
state,
versionMeta,
VectorSandboxVamanaVectorsFormat.VECTOR_INDEX_EXTENSION,
VectorSandboxVamanaVectorsFormat.VECTOR_INDEX_CODEC_NAME);
if (fields.values().stream().anyMatch(FieldEntry::hasQuantizedVectors)) {
quantizedVectorData =
openDataInput(
state,
versionMeta,
VectorSandboxScalarQuantizedVectorsFormat.QUANTIZED_VECTOR_DATA_EXTENSION,
VectorSandboxScalarQuantizedVectorsFormat.QUANTIZED_VECTOR_DATA_CODEC_NAME);
quantizedVectorsReader = new VectorSandboxScalarQuantizedVectorsReader(quantizedVectorData);
} else {
quantizedVectorData = null;
quantizedVectorsReader = null;
}
success = true;
} finally {
if (success == false) {
IOUtils.closeWhileHandlingException(this);
}
}
}
private int readMetadata(SegmentReadState state) throws IOException {
String metaFileName =
IndexFileNames.segmentFileName(
state.segmentInfo.name,
state.segmentSuffix,
VectorSandboxVamanaVectorsFormat.META_EXTENSION);
int versionMeta = -1;
try (ChecksumIndexInput meta = state.directory.openChecksumInput(metaFileName)) {
Throwable priorE = null;
try {
versionMeta =
CodecUtil.checkIndexHeader(
meta,
VectorSandboxVamanaVectorsFormat.META_CODEC_NAME,
VectorSandboxVamanaVectorsFormat.VERSION_START,
VectorSandboxVamanaVectorsFormat.VERSION_CURRENT,
state.segmentInfo.getId(),
state.segmentSuffix);
readFields(meta, state.fieldInfos);
} catch (Throwable exception) {
priorE = exception;
} finally {
CodecUtil.checkFooter(meta, priorE);
}
}
return versionMeta;
}
private static IndexInput openDataInput(
SegmentReadState state, int versionMeta, String fileExtension, String codecName)
throws IOException {
String fileName =
IndexFileNames.segmentFileName(state.segmentInfo.name, state.segmentSuffix, fileExtension);
IndexInput in = state.directory.openInput(fileName, state.context);
boolean success = false;
try {
int versionVectorData =
CodecUtil.checkIndexHeader(
in,
codecName,
VectorSandboxVamanaVectorsFormat.VERSION_START,
VectorSandboxVamanaVectorsFormat.VERSION_CURRENT,
state.segmentInfo.getId(),
state.segmentSuffix);
if (versionMeta != versionVectorData) {
throw new CorruptIndexException(
"Format versions mismatch: meta="
+ versionMeta
+ ", "
+ codecName
+ "="
+ versionVectorData,
in);
}
CodecUtil.retrieveChecksum(in);
success = true;
return in;
} finally {
if (success == false) {
IOUtils.closeWhileHandlingException(in);
}
}
}
private void readFields(ChecksumIndexInput meta, FieldInfos infos) throws IOException {
for (int fieldNumber = meta.readInt(); fieldNumber != -1; fieldNumber = meta.readInt()) {
FieldInfo info = infos.fieldInfo(fieldNumber);
if (info == null) {
throw new CorruptIndexException("Invalid field number: " + fieldNumber, meta);
}
FieldEntry fieldEntry = readField(meta);
validateFieldEntry(info, fieldEntry);
fields.put(info.name, fieldEntry);
}
}
private void validateFieldEntry(FieldInfo info, FieldEntry fieldEntry) {
int dimension = info.getVectorDimension();
if (dimension != fieldEntry.dimension) {
throw new IllegalStateException(
"Inconsistent vector dimension for field=\""
+ info.name
+ "\"; "
+ dimension
+ " != "
+ fieldEntry.dimension);
}
if (fieldEntry.hasQuantizedVectors()) {
int byteSize =
switch (info.getVectorEncoding()) {
case BYTE -> Byte.BYTES;
case FLOAT32 -> Float.BYTES;
};
long vectorBytes = Math.multiplyExact((long) dimension, byteSize);
long numBytes = Math.multiplyExact(vectorBytes, fieldEntry.size);
if (numBytes != fieldEntry.vectorDataLength) {
throw new IllegalStateException(
"Vector data length "
+ fieldEntry.vectorDataLength
+ " not matching size="
+ fieldEntry.size
+ " * dim="
+ dimension
+ " * byteSize="
+ byteSize
+ " = "
+ numBytes);
}
VectorSandboxScalarQuantizedVectorsReader.validateFieldEntry(
info, fieldEntry.dimension, fieldEntry.size, fieldEntry.quantizedVectorDataLength);
}
}
private VectorSimilarityFunction readSimilarityFunction(DataInput input) throws IOException {
int similarityFunctionId = input.readInt();
if (similarityFunctionId < 0
|| similarityFunctionId >= VectorSimilarityFunction.values().length) {
throw new CorruptIndexException(
"Invalid similarity function id: " + similarityFunctionId, input);
}
return VectorSimilarityFunction.values()[similarityFunctionId];
}
private VectorEncoding readVectorEncoding(DataInput input) throws IOException {
int encodingId = input.readInt();
if (encodingId < 0 || encodingId >= VectorEncoding.values().length) {
throw new CorruptIndexException("Invalid vector encoding id: " + encodingId, input);
}
return VectorEncoding.values()[encodingId];
}
private FieldEntry readField(IndexInput input) throws IOException {
VectorEncoding vectorEncoding = readVectorEncoding(input);
VectorSimilarityFunction similarityFunction = readSimilarityFunction(input);
return new FieldEntry(input, vectorEncoding, similarityFunction);
}
@Override
public long ramBytesUsed() {
return VectorSandboxVamanaVectorsReader.SHALLOW_SIZE
+ RamUsageEstimator.sizeOfMap(
fields, RamUsageEstimator.shallowSizeOfInstance(FieldEntry.class));
}
@Override
public void checkIntegrity() throws IOException {
CodecUtil.checksumEntireFile(vectorData);
CodecUtil.checksumEntireFile(vectorIndex);
if (quantizedVectorsReader != null) {
quantizedVectorsReader.checkIntegrity();
}
}
@Override
public FloatVectorValues getFloatVectorValues(String field) throws IOException {
FieldEntry fieldEntry = fields.get(field);
if (fieldEntry.vectorEncoding != VectorEncoding.FLOAT32) {
throw new IllegalArgumentException(
"field=\""
+ field
+ "\" is encoded as: "
+ fieldEntry.vectorEncoding
+ " expected: "
+ VectorEncoding.FLOAT32);
}
if (!fieldEntry.isQuantized) {
return InGraphOffHeapFloatVectorValues.load(fieldEntry, vectorIndex);
}
return OffHeapFloatVectorValues.load(
fieldEntry.ordToDoc,
fieldEntry.vectorEncoding,
fieldEntry.dimension,
fieldEntry.vectorDataOffset,
fieldEntry.vectorDataLength,
vectorData);
}
@Override
public ByteVectorValues getByteVectorValues(String field) throws IOException {
FieldEntry fieldEntry = fields.get(field);
if (fieldEntry.vectorEncoding != VectorEncoding.BYTE) {
throw new IllegalArgumentException(
"field=\""
+ field
+ "\" is encoded as: "
+ fieldEntry.vectorEncoding
+ " expected: "
+ VectorEncoding.FLOAT32);
}
if (!fieldEntry.isQuantized) {
return InGraphOffHeapByteVectorValues.load(fieldEntry, vectorIndex);
}
return OffHeapByteVectorValues.load(
fieldEntry.ordToDoc,
fieldEntry.vectorEncoding,
fieldEntry.dimension,
fieldEntry.vectorDataOffset,
fieldEntry.vectorDataLength,
vectorData);
}
@Override
public void search(String field, float[] target, KnnCollector knnCollector, Bits acceptDocs)
throws IOException {
FieldEntry fieldEntry = fields.get(field);
if (fieldEntry.size() == 0
|| knnCollector.k() == 0
|| fieldEntry.vectorEncoding != VectorEncoding.FLOAT32) {
return;
}
if (fieldEntry.hasQuantizedVectors()) {
InGraphOffHeapQuantizedByteVectorValues vectorValues = InGraphOffHeapQuantizedByteVectorValues.load(
fieldEntry, vectorIndex);
RandomVectorScorer scorer =
new ScalarQuantizedRandomVectorScorer(
fieldEntry.similarityFunction, fieldEntry.scalarQuantizer, vectorValues, target);
VamanaGraphSearcher.search(
scorer,
new OrdinalTranslatedKnnCollector(knnCollector, vectorValues::ordToDoc),
getGraph(fieldEntry),
// FIXME: support filtered
// vectorValues.getAcceptOrds(acceptDocs));
acceptDocs);
} else {
InGraphOffHeapFloatVectorValues vectorValues =
InGraphOffHeapFloatVectorValues.load(fieldEntry, vectorIndex);
RandomVectorScorer scorer =
RandomVectorScorer.createFloats(vectorValues, fieldEntry.similarityFunction, target);
VamanaGraphSearcher.search(
scorer,
new OrdinalTranslatedKnnCollector(knnCollector, vectorValues::ordToDoc),
getGraph(fieldEntry),
// FIXME: support filtered
// vectorValues.getAcceptOrds(acceptDocs));
acceptDocs);
}
}
@Override
public void search(String field, byte[] target, KnnCollector knnCollector, Bits acceptDocs)
throws IOException {
FieldEntry fieldEntry = fields.get(field);
if (fieldEntry.size() == 0
|| knnCollector.k() == 0
|| fieldEntry.vectorEncoding != VectorEncoding.BYTE) {
return;
}
InGraphOffHeapByteVectorValues vectorValues =
InGraphOffHeapByteVectorValues.load(fieldEntry, vectorIndex);
RandomVectorScorer scorer =
RandomVectorScorer.createBytes(vectorValues, fieldEntry.similarityFunction, target);
VamanaGraphSearcher.search(
scorer,
new OrdinalTranslatedKnnCollector(knnCollector, vectorValues::ordToDoc),
getGraph(fieldEntry),
// FIXME: support filtered
// vectorValues.getAcceptOrds(acceptDocs));
acceptDocs);
}
@Override
public VamanaGraph getGraph(String field) throws IOException {
FieldInfo info = fieldInfos.fieldInfo(field);
if (info == null) {
throw new IllegalArgumentException("No such field '" + field + "'");
}
FieldEntry entry = fields.get(field);
if (entry != null && entry.vectorIndexLength > 0) {
return getGraph(entry);
} else {
return VamanaGraph.EMPTY;
}
}
private VamanaGraph getGraph(FieldEntry entry) throws IOException {
return new OffHeapVamanaGraph(entry, vectorIndex);
}
@Override
public void close() throws IOException {
IOUtils.close(vectorData, vectorIndex, quantizedVectorData);
}
@Override
public QuantizedByteVectorValues getQuantizedVectorValues(String field)
throws IOException {
FieldEntry fieldEntry = fields.get(field);
if (fieldEntry == null || fieldEntry.hasQuantizedVectors() == false) {
return null;
}
assert quantizedVectorsReader != null && fieldEntry.quantizedOrdToDoc != null;
return InGraphOffHeapQuantizedByteVectorValues.load(fieldEntry, vectorIndex);
}
@Override
public ScalarQuantizer getQuantizationState(String fieldName) {
FieldEntry field = fields.get(fieldName);
if (field == null || field.hasQuantizedVectors() == false) {
return null;
}
return field.scalarQuantizer;
}
static class FieldEntry implements Accountable {
private static final long SHALLOW_SIZE =
RamUsageEstimator.shallowSizeOfInstance(FieldEntry.class);
final VectorSimilarityFunction similarityFunction;
final VectorEncoding vectorEncoding;
final long vectorDataOffset;
final long vectorDataLength;
final long vectorIndexOffset;
final long vectorIndexLength;
final int M;
final int entryNode;
final int dimension;
final int size;
final DirectMonotonicReader.Meta offsetsMeta;
final long offsetsOffset;
final int offsetsBlockShift;
final long offsetsLength;
final OrdToDocDISIReaderConfiguration ordToDoc;
final float configuredQuantile, lowerQuantile, upperQuantile;
final long quantizedVectorDataOffset, quantizedVectorDataLength;
final ScalarQuantizer scalarQuantizer;
final boolean isQuantized;
final OrdToDocDISIReaderConfiguration quantizedOrdToDoc;
FieldEntry(
IndexInput input,
VectorEncoding vectorEncoding,
VectorSimilarityFunction similarityFunction)
throws IOException {
this.similarityFunction = similarityFunction;
this.vectorEncoding = vectorEncoding;
this.isQuantized = input.readByte() == 1;
// Has int8 quantization
if (isQuantized) {
configuredQuantile = Float.intBitsToFloat(input.readInt());
lowerQuantile = Float.intBitsToFloat(input.readInt());
upperQuantile = Float.intBitsToFloat(input.readInt());
quantizedVectorDataOffset = input.readVLong();
quantizedVectorDataLength = input.readVLong();
scalarQuantizer = new ScalarQuantizer(lowerQuantile, upperQuantile, configuredQuantile);
} else {
configuredQuantile = -1;
lowerQuantile = -1;
upperQuantile = -1;
quantizedVectorDataOffset = -1;
quantizedVectorDataLength = -1;
scalarQuantizer = null;
}
vectorDataOffset = input.readVLong();
vectorDataLength = input.readVLong();
vectorIndexOffset = input.readVLong();
vectorIndexLength = input.readVLong();
dimension = input.readVInt();
size = input.readInt();
if (isQuantized) {
quantizedOrdToDoc = OrdToDocDISIReaderConfiguration.fromStoredMeta(input, size);
} else {
quantizedOrdToDoc = null;
}
ordToDoc = OrdToDocDISIReaderConfiguration.fromStoredMeta(input, size);
// read node offsets
M = input.readVInt();
if (size > 0) {
entryNode = input.readVInt();
offsetsOffset = input.readLong();
offsetsBlockShift = input.readVInt();
offsetsMeta = DirectMonotonicReader.loadMeta(input, size, offsetsBlockShift);
offsetsLength = input.readLong();
} else {
entryNode = -1;
offsetsOffset = 0;
offsetsBlockShift = 0;
offsetsMeta = null;
offsetsLength = 0;
}
}
int size() {
return size;
}
boolean hasQuantizedVectors() {
return isQuantized;
}
@Override
public long ramBytesUsed() {
return SHALLOW_SIZE
+ RamUsageEstimator.sizeOf(ordToDoc)
+ (quantizedOrdToDoc == null ? 0 : RamUsageEstimator.sizeOf(quantizedOrdToDoc))
+ RamUsageEstimator.sizeOf(offsetsMeta);
}
}
/**
* Read the nearest-neighbors graph from the index input
*/
private static final class OffHeapVamanaGraph extends VamanaGraph {
final IndexInput dataIn;
final int entryNode;
final int size;
final int dimensions;
final VectorEncoding encoding;
int arcCount;
int arcUpTo;
int arc;
private final DirectMonotonicReader graphNodeOffsets;
// Allocated to be M to track the current neighbors being explored
private final int[] currentNeighborsBuffer;
private final int vectorSize;
OffHeapVamanaGraph(FieldEntry entry, IndexInput vectorIndex) throws IOException {
this.dataIn =
vectorIndex.slice("graph-data", entry.vectorIndexOffset, entry.vectorIndexLength);
this.entryNode = entry.entryNode;
this.size = entry.size();
this.dimensions = entry.dimension;
this.encoding = entry.vectorEncoding;
final RandomAccessInput addressesData =
vectorIndex.randomAccessSlice(entry.offsetsOffset, entry.offsetsLength);
this.graphNodeOffsets = DirectMonotonicReader.getInstance(entry.offsetsMeta, addressesData);
this.currentNeighborsBuffer = new int[entry.M];
this.vectorSize =
entry.isQuantized ? this.dimensions + Float.BYTES
: this.dimensions * this.encoding.byteSize;
}
@Override
public void seek(int targetOrd) throws IOException {
assert targetOrd >= 0;
// unsafe; no bounds checking
// seek to the [vector | adjacency list] for this ordinal, then seek past the vector.
var targetOffset = graphNodeOffsets.get(targetOrd);
dataIn.seek(targetOffset + this.vectorSize);
arcCount = dataIn.readVInt();
if (arcCount > 0) {
currentNeighborsBuffer[0] = dataIn.readVInt();
for (int i = 1; i < arcCount; i++) {
currentNeighborsBuffer[i] = currentNeighborsBuffer[i - 1] + dataIn.readVInt();
}
}
arc = -1;
arcUpTo = 0;
}
@Override
public int size() {
return size;
}
@Override
public int nextNeighbor() throws IOException {
if (arcUpTo >= arcCount) {
return NO_MORE_DOCS;
}
arc = currentNeighborsBuffer[arcUpTo];
++arcUpTo;
return arc;
}
@Override
public int entryNode() throws IOException {
return entryNode;
}
@Override
public NodesIterator getNodes() {
return new ArrayNodesIterator(size());
}
}
private static class InGraphOffHeapFloatVectorValues extends FloatVectorValues
implements RandomAccessVectorValues<float[]> {
final IndexInput dataIn;
private final int size;
private final int dimensions;
private final DirectMonotonicReader graphNodeOffsets;
private int lastOrd = -1;
private int doc = -1;
private final float[] value;
static InGraphOffHeapFloatVectorValues load(FieldEntry entry, IndexInput vectorIndex)
throws IOException {
IndexInput slicedInput =
vectorIndex.slice("graph-data", entry.vectorIndexOffset, entry.vectorIndexLength);
RandomAccessInput addressesData =
vectorIndex.randomAccessSlice(entry.offsetsOffset, entry.offsetsLength);
DirectMonotonicReader graphNodeOffsets =
DirectMonotonicReader.getInstance(entry.offsetsMeta, addressesData);
return new InGraphOffHeapFloatVectorValues(
slicedInput, entry.size, entry.dimension, graphNodeOffsets);
}
InGraphOffHeapFloatVectorValues(
IndexInput vectorIndex, int size, int dimensions, DirectMonotonicReader graphNodeOffsets) {
this.dataIn = vectorIndex;
this.size = size;
this.dimensions = dimensions;
this.graphNodeOffsets = graphNodeOffsets;
this.value = new float[dimensions];
}
@Override
public int size() {
return size;
}
@Override
public int dimension() {
return dimensions;
}
@Override
public float[] vectorValue(int targetOrd) throws IOException {
if (lastOrd == targetOrd) {
return value;
}
// unsafe; no bounds checking
long targetOffset = graphNodeOffsets.get(targetOrd);
dataIn.seek(targetOffset);
dataIn.readFloats(value, 0, dimensions);
lastOrd = targetOrd;
return value;
}
@Override
public RandomAccessVectorValues<float[]> copy() throws IOException {
return new InGraphOffHeapFloatVectorValues(
this.dataIn.clone(), this.size, this.dimensions, this.graphNodeOffsets);
}
@Override
public float[] vectorValue() throws IOException {
return vectorValue(doc);
}
@Override
public int docID() {
return doc;
}
@Override
public int nextDoc() throws IOException {
return advance(doc + 1);
}
@Override
public int advance(int target) throws IOException {
assert docID() < target;
if (target >= size) {
return doc = NO_MORE_DOCS;
}
return doc = target;
}
}
private static class InGraphOffHeapQuantizedByteVectorValues extends
QuantizedByteVectorValues implements RandomAccessQuantizedByteVectorValues {
final IndexInput dataIn;
private final int size;
private final int dimensions;
private final DirectMonotonicReader graphNodeOffsets;
protected final byte[] binaryValue;
protected final ByteBuffer byteBuffer;
private int lastOrd = -1;
private int doc = -1;
protected final float[] scoreCorrectionConstant = new float[1];
static InGraphOffHeapQuantizedByteVectorValues load(FieldEntry entry, IndexInput vectorIndex)
throws IOException {
IndexInput slicedInput =
vectorIndex.slice("graph-data", entry.vectorIndexOffset, entry.vectorIndexLength);
RandomAccessInput addressesData =
vectorIndex.randomAccessSlice(entry.offsetsOffset, entry.offsetsLength);
DirectMonotonicReader graphNodeOffsets =
DirectMonotonicReader.getInstance(entry.offsetsMeta, addressesData);
return new InGraphOffHeapQuantizedByteVectorValues(
slicedInput, entry.size, entry.dimension, graphNodeOffsets);
}
InGraphOffHeapQuantizedByteVectorValues(
IndexInput vectorIndex, int size, int dimensions, DirectMonotonicReader graphNodeOffsets) {
this.dataIn = vectorIndex;
this.size = size;
this.dimensions = dimensions;
this.graphNodeOffsets = graphNodeOffsets;
this.byteBuffer = ByteBuffer.allocate(dimensions);
this.binaryValue = byteBuffer.array();
}
@Override
public int dimension() {
return dimensions;
}
@Override
public int size() {
return size;
}
@Override
public byte[] vectorValue(int targetOrd) throws IOException {
if (lastOrd == targetOrd) {
return binaryValue;
}
// unsafe; no bounds checking
long targetOffset = graphNodeOffsets.get(targetOrd);
dataIn.seek(targetOffset);
dataIn.readBytes(byteBuffer.array(), byteBuffer.arrayOffset(), dimensions);
dataIn.readFloats(scoreCorrectionConstant, 0, 1);
lastOrd = targetOrd;
return binaryValue;
}
@Override
public float getScoreCorrectionConstant() {
return scoreCorrectionConstant[0];
}
@Override
public RandomAccessQuantizedByteVectorValues copy() throws IOException {
return new InGraphOffHeapQuantizedByteVectorValues(this.dataIn.clone(), this.size,
this.dimensions, this.graphNodeOffsets);
}
@Override
public byte[] vectorValue() throws IOException {
return vectorValue(doc);
}
@Override
public int docID() {
return doc;
}
@Override
public int nextDoc() throws IOException {
return advance(doc + 1);
}
@Override
public int advance(int target) throws IOException {
assert docID() < target;
if (target >= size) {
return doc = NO_MORE_DOCS;
}
return doc = target;
}
}
private static class InGraphOffHeapByteVectorValues extends ByteVectorValues
implements RandomAccessVectorValues<byte[]> {
final IndexInput dataIn;
private final int size;
private final int dimensions;
private final DirectMonotonicReader graphNodeOffsets;
private int lastOrd = -1;
private int doc = -1;
private final byte[] value;
static InGraphOffHeapByteVectorValues load(FieldEntry entry, IndexInput vectorIndex)
throws IOException {
IndexInput slicedInput =
vectorIndex.slice("graph-data", entry.vectorIndexOffset, entry.vectorIndexLength);
RandomAccessInput addressesData =
vectorIndex.randomAccessSlice(entry.offsetsOffset, entry.offsetsLength);
DirectMonotonicReader graphNodeOffsets =
DirectMonotonicReader.getInstance(entry.offsetsMeta, addressesData);
return new InGraphOffHeapByteVectorValues(
slicedInput, entry.size, entry.dimension, graphNodeOffsets);
}
InGraphOffHeapByteVectorValues(
IndexInput vectorIndex, int size, int dimensions, DirectMonotonicReader graphNodeOffsets) {
this.dataIn = vectorIndex;
this.size = size;
this.dimensions = dimensions;
this.graphNodeOffsets = graphNodeOffsets;
this.value = new byte[dimensions];
}
@Override
public int size() {
return size;
}
@Override
public int dimension() {
return dimensions;
}
@Override
public byte[] vectorValue(int targetOrd) throws IOException {
if (lastOrd == targetOrd) {
return value;
}
// unsafe; no bounds checking
long targetOffset = graphNodeOffsets.get(targetOrd);
dataIn.seek(targetOffset);
dataIn.readBytes(value, 0, dimensions);
lastOrd = targetOrd;
return value;
}
@Override
public RandomAccessVectorValues<byte[]> copy() throws IOException {
return new InGraphOffHeapByteVectorValues(
this.dataIn.clone(), this.size, this.dimensions, this.graphNodeOffsets);
}
@Override
public byte[] vectorValue() throws IOException {
return vectorValue(doc);
}
@Override
public int docID() {
return doc;
}
@Override
public int nextDoc() throws IOException {
return advance(doc + 1);
}
@Override
public int advance(int target) throws IOException {
assert docID() < target;
if (target >= size) {
return doc = NO_MORE_DOCS;
}
return doc = target;
}
}
}