|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the "Elastic License |
| 4 | + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side |
| 5 | + * Public License v 1"; you may not use this file except in compliance with, at |
| 6 | + * your election, the "Elastic License 2.0", the "GNU Affero General Public |
| 7 | + * License v3.0 only", or the "Server Side Public License, v 1". |
| 8 | + */ |
| 9 | + |
| 10 | +package org.elasticsearch.index.codec.vectors.reflect; |
| 11 | + |
| 12 | +import org.apache.lucene.codecs.lucene95.HasIndexSlice; |
| 13 | +import org.apache.lucene.codecs.lucene99.Lucene99ScalarQuantizedVectorsWriter; |
| 14 | +import org.apache.lucene.index.FloatVectorValues; |
| 15 | +import org.apache.lucene.util.hnsw.CloseableRandomVectorScorerSupplier; |
| 16 | +import org.apache.lucene.util.hnsw.RandomVectorScorerSupplier; |
| 17 | +import org.elasticsearch.simdvec.QuantizedByteVectorValuesAccess; |
| 18 | + |
| 19 | +import java.lang.invoke.MethodHandles; |
| 20 | +import java.lang.invoke.VarHandle; |
| 21 | + |
| 22 | +public class VectorsFormatReflectionUtils { |
| 23 | + private static final VarHandle FLOAT_SUPPLIER_HANDLE; |
| 24 | + private static final VarHandle BYTE_SUPPLIER_HANDLE; |
| 25 | + private static final VarHandle FLOAT_VECTORS_HANDLE; |
| 26 | + |
| 27 | + private static final Class<?> FLAT_CLOSEABLE_RANDOM_VECTOR_SCORER_SUPPLIER_CLASS; |
| 28 | + private static final Class<?> SCALAR_QUANTIZED_CLOSEABLE_RANDOM_VECTOR_SCORER_SUPPLIER_CLASS; |
| 29 | + private static final Class<?> FLOAT_SCORING_SUPPLIER_CLASS; |
| 30 | + static { |
| 31 | + try { |
| 32 | + FLAT_CLOSEABLE_RANDOM_VECTOR_SCORER_SUPPLIER_CLASS = Class.forName( |
| 33 | + "org.apache.lucene.codecs.lucene99.Lucene99FlatVectorsWriter$FlatCloseableRandomVectorScorerSupplier" |
| 34 | + ); |
| 35 | + var lookup = MethodHandles.privateLookupIn(FLAT_CLOSEABLE_RANDOM_VECTOR_SCORER_SUPPLIER_CLASS, MethodHandles.lookup()); |
| 36 | + FLOAT_SUPPLIER_HANDLE = lookup.findVarHandle( |
| 37 | + FLAT_CLOSEABLE_RANDOM_VECTOR_SCORER_SUPPLIER_CLASS, |
| 38 | + "supplier", |
| 39 | + RandomVectorScorerSupplier.class |
| 40 | + ); |
| 41 | + |
| 42 | + FLOAT_SCORING_SUPPLIER_CLASS = Class.forName( |
| 43 | + "org.apache.lucene.internal.vectorization.Lucene99MemorySegmentFloatVectorScorerSupplier" |
| 44 | + ); |
| 45 | + lookup = MethodHandles.privateLookupIn(FLOAT_SCORING_SUPPLIER_CLASS, MethodHandles.lookup()); |
| 46 | + FLOAT_VECTORS_HANDLE = lookup.findVarHandle(FLOAT_SCORING_SUPPLIER_CLASS, "values", FloatVectorValues.class); |
| 47 | + |
| 48 | + SCALAR_QUANTIZED_CLOSEABLE_RANDOM_VECTOR_SCORER_SUPPLIER_CLASS = Class.forName( |
| 49 | + Lucene99ScalarQuantizedVectorsWriter.class.getCanonicalName() + "$ScalarQuantizedCloseableRandomVectorScorerSupplier" |
| 50 | + ); |
| 51 | + lookup = MethodHandles.privateLookupIn(SCALAR_QUANTIZED_CLOSEABLE_RANDOM_VECTOR_SCORER_SUPPLIER_CLASS, MethodHandles.lookup()); |
| 52 | + BYTE_SUPPLIER_HANDLE = lookup.findVarHandle( |
| 53 | + SCALAR_QUANTIZED_CLOSEABLE_RANDOM_VECTOR_SCORER_SUPPLIER_CLASS, |
| 54 | + "supplier", |
| 55 | + RandomVectorScorerSupplier.class |
| 56 | + ); |
| 57 | + |
| 58 | + } catch (IllegalAccessException e) { |
| 59 | + throw new AssertionError("should not happen, check opens", e); |
| 60 | + } catch (ReflectiveOperationException e) { |
| 61 | + throw new AssertionError(e); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + public static RandomVectorScorerSupplier getFlatRandomVectorScorerInnerSupplier(CloseableRandomVectorScorerSupplier scorerSupplier) { |
| 66 | + if (scorerSupplier.getClass().equals(FLAT_CLOSEABLE_RANDOM_VECTOR_SCORER_SUPPLIER_CLASS)) { |
| 67 | + return (RandomVectorScorerSupplier) FLOAT_SUPPLIER_HANDLE.get(scorerSupplier); |
| 68 | + } |
| 69 | + return null; |
| 70 | + } |
| 71 | + |
| 72 | + public static RandomVectorScorerSupplier getScalarQuantizedRandomVectorScorerInnerSupplier( |
| 73 | + CloseableRandomVectorScorerSupplier scorerSupplier |
| 74 | + ) { |
| 75 | + if (scorerSupplier.getClass().equals(SCALAR_QUANTIZED_CLOSEABLE_RANDOM_VECTOR_SCORER_SUPPLIER_CLASS)) { |
| 76 | + return (RandomVectorScorerSupplier) BYTE_SUPPLIER_HANDLE.get(scorerSupplier); |
| 77 | + } |
| 78 | + return null; |
| 79 | + } |
| 80 | + |
| 81 | + public static HasIndexSlice getFloatScoringSupplierVectorOrNull(RandomVectorScorerSupplier scorerSupplier) { |
| 82 | + if (FLOAT_SCORING_SUPPLIER_CLASS.isAssignableFrom(scorerSupplier.getClass())) { |
| 83 | + var vectorValues = FLOAT_VECTORS_HANDLE.get(scorerSupplier); |
| 84 | + if (vectorValues instanceof HasIndexSlice indexSlice) { |
| 85 | + return indexSlice; |
| 86 | + } |
| 87 | + } |
| 88 | + return null; |
| 89 | + } |
| 90 | + |
| 91 | + public static HasIndexSlice getByteScoringSupplierVectorOrNull(RandomVectorScorerSupplier scorerSupplier) { |
| 92 | + if (scorerSupplier instanceof QuantizedByteVectorValuesAccess quantizedByteVectorValuesAccess) { |
| 93 | + return quantizedByteVectorValuesAccess.get(); |
| 94 | + } |
| 95 | + return null; |
| 96 | + } |
| 97 | +} |
0 commit comments