Class StorageHelpers
java.lang.Object
com.apple.foundationdb.async.common.StorageHelpers
Static helpers for the storage layer shared by the vector index implementations: (de)serializing vectors to and
from their
Tuple and byte representations, and appending to, consuming,
aggregating, and clearing the set of sampled vectors held in a subspace. Not instantiable.-
Method Summary
Modifier and TypeMethodDescriptionstatic AggregatedVectoraggregateVectors(Iterable<AggregatedVector> vectors) Combines a collection ofAggregatedVectors into a single aggregate by summing their partial vectors and counts.static voidappendSampledVector(Transaction transaction, SplittableRandom random, boolean deterministicRandomness, Subspace prefixSubspace, int partialCount, Transformed<RealVector> vector, OnKeyValueWriteListener onWriteListener) Appends a single sampled vector to the sample buffer in the given subspace, under a key derived from its partial count and a fresh (optionally deterministic) random id.static byte[]bytesFromVector(Transformed<RealVector> transformedVector) Returns the raw byte representation of a transformed vector's underlying data, as stored in the database.static CompletableFuture<List<AggregatedVector>> consumeSampledVectors(Transaction transaction, Subspace prefixSubspace, int numMaxVectors, OnKeyValueReadListener onReadListener) Reads up tonumMaxVectorssampled vectors from the given subspace and removes them as it goes, draining that portion of the sample buffer.static voiddeleteAllSampledVectors(Transaction transaction, Subspace prefixSubspace, OnKeyValueWriteListener onWriteListener) Clears all sampled vectors held in the given subspace.static TupletupleFromVector(RealVector vector) Converts aRealVectorinto aTuple.static TupletupleFromVector(Transformed<RealVector> vector) Converts a transformed vector into a tuple.static RealVectorvectorFromBytes(VectorEncodingConfig config, byte[] vectorBytes) Creates aRealVectorfrom a byte array.static RealVectorvectorFromTuple(VectorEncodingConfig config, Tuple vectorTuple) Creates aRealVectorfrom a givenTuple.
-
Method Details
-
consumeSampledVectors
@Nonnull public static CompletableFuture<List<AggregatedVector>> consumeSampledVectors(@Nonnull Transaction transaction, @Nonnull Subspace prefixSubspace, int numMaxVectors, @Nonnull OnKeyValueReadListener onReadListener) Reads up tonumMaxVectorssampled vectors from the given subspace and removes them as it goes, draining that portion of the sample buffer. Each consumed key is read on a snapshot, registered as a read conflict, cleared, and reported to the listener, so the transaction conflicts only on the keys actually consumed rather than on the whole range.- Parameters:
transaction- the transaction to read and clear withinprefixSubspace- the subspace holding the sampled vectorsnumMaxVectors- the maximum number of sampled vectors to consumeonReadListener- the listener notified of each key/value read- Returns:
- a future completing with the consumed sampled vectors
-
aggregateVectors
@Nullable public static AggregatedVector aggregateVectors(@Nonnull Iterable<AggregatedVector> vectors) Combines a collection ofAggregatedVectors into a single aggregate by summing their partial vectors and counts.- Parameters:
vectors- the partial aggregates to combine- Returns:
- the combined aggregate, or
nullifvectorscontributes no elements
-
appendSampledVector
public static void appendSampledVector(@Nonnull Transaction transaction, @Nonnull SplittableRandom random, boolean deterministicRandomness, @Nonnull Subspace prefixSubspace, int partialCount, @Nonnull Transformed<RealVector> vector, @Nonnull OnKeyValueWriteListener onWriteListener) Appends a single sampled vector to the sample buffer in the given subspace, under a key derived from its partial count and a fresh (optionally deterministic) random id.- Parameters:
transaction- the transaction to write withinrandom- the random source for the key's id whendeterministicRandomnessis setdeterministicRandomness- whether to derive the key's id deterministically rather than randomlyprefixSubspace- the subspace holding the sampled vectorspartialCount- the number of vectors this sample aggregatesvector- the sampled (partial) vector to storeonWriteListener- the listener notified of the written key/value
-
deleteAllSampledVectors
public static void deleteAllSampledVectors(@Nonnull Transaction transaction, @Nonnull Subspace prefixSubspace, @Nonnull OnKeyValueWriteListener onWriteListener) Clears all sampled vectors held in the given subspace.- Parameters:
transaction- the transaction to clear withinprefixSubspace- the subspace holding the sampled vectorsonWriteListener- the listener notified of the cleared range
-
tupleFromVector
Converts a transformed vector into a tuple.- Parameters:
vector- a transformed vector- Returns:
- a new, non-null
Tupleinstance representing the contents of the underlying vector.
-
tupleFromVector
Converts aRealVectorinto aTuple.This method first serializes the given vector into a byte array using the
RealVector.getRawData()getter method. It then creates aTuplefrom the resulting byte array.- Parameters:
vector- theRealVectorto convert. Cannot be null.- Returns:
- a new, non-null
Tupleinstance representing the contents of the vector.
-
bytesFromVector
Returns the raw byte representation of a transformed vector's underlying data, as stored in the database.- Parameters:
transformedVector- the transformed vector whose underlying raw bytes are returned- Returns:
- the raw bytes of the underlying vector
-
vectorFromTuple
@Nonnull public static RealVector vectorFromTuple(@Nonnull VectorEncodingConfig config, @Nonnull Tuple vectorTuple) Creates aRealVectorfrom a givenTuple.This method assumes the vector data is stored as a byte array at the first. position (index 0) of the tuple. It extracts this byte array and then delegates to the
vectorFromBytes(VectorEncodingConfig, byte[])method for the actual conversion.- Parameters:
config- an HNSW configurationvectorTuple- the tuple containing the vector data as a byte array at index 0. Must not benull.- Returns:
- a new
RealVectorinstance created from the tuple's data. This method never returnsnull.
-
vectorFromBytes
@Nonnull public static RealVector vectorFromBytes(@Nonnull VectorEncodingConfig config, @Nonnull byte[] vectorBytes) Creates aRealVectorfrom a byte array.This method reads the leading vector-type byte and, based on it, delegates to the appropriate decoder:
EncodedRealVectorfor RaBitQ-encoded vectors, otherwiseRealVector.fromBytes(byte[]).- Parameters:
config- a vector-encoding configurationvectorBytes- the non-null byte array to convert.- Returns:
- a new
RealVectorinstance created from the byte array.
-