Skip to content

Add BSON Binary Subtype 9 support for vector storage and retrieval. #1528

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 25 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add tests and change validation message.
  • Loading branch information
vbabanin committed Oct 30, 2024
commit 3422dcdd0408645e83868729e1bf67b97c2cf8c3
18 changes: 8 additions & 10 deletions bson/src/main/org/bson/codecs/ContainerCodecHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import java.util.Arrays;
import java.util.UUID;

import static org.bson.internal.UuidHelper.isLegacyUUID;

/**
* Helper methods for Codec implementations for containers, e.g. {@code Map} and {@code Iterable}.
*/
Expand All @@ -48,7 +50,8 @@ static Object readValue(final BsonReader reader, final DecoderContext decoderCon

if (bsonType == BsonType.BINARY) {
byte binarySubType = reader.peekBinarySubType();
currentCodec = getBinarySubTypeCodec(reader,
currentCodec = getBinarySubTypeCodec(
reader,
uuidRepresentation,
registry, binarySubType,
currentCodec);
Expand All @@ -62,21 +65,17 @@ private static Codec<?> getBinarySubTypeCodec(final BsonReader reader,
final UuidRepresentation uuidRepresentation,
final CodecRegistry registry,
final byte binarySubType,
final Codec<?> currentTypeCodec) {
final Codec<?> binaryTypeCodec) {

if (binarySubType == BsonBinarySubType.VECTOR.getValue()) {
Codec<Vector> vectorCodec = registry.get(Vector.class, registry);
if (vectorCodec != null) {
return vectorCodec;
}
}

if (reader.peekBinarySize() == 16) {
} else if (reader.peekBinarySize() == 16) {
switch (binarySubType) {
case 3:
if (uuidRepresentation == UuidRepresentation.JAVA_LEGACY
|| uuidRepresentation == UuidRepresentation.C_SHARP_LEGACY
|| uuidRepresentation == UuidRepresentation.PYTHON_LEGACY) {
if (isLegacyUUID(uuidRepresentation)) {
return registry.get(UUID.class);
}
break;
Expand All @@ -90,7 +89,7 @@ private static Codec<?> getBinarySubTypeCodec(final BsonReader reader,
}
}

return currentTypeCodec;
return binaryTypeCodec;
}

static Codec<?> getCodec(final CodecRegistry codecRegistry, final Type type) {
Expand All @@ -104,7 +103,6 @@ static Codec<?> getCodec(final CodecRegistry codecRegistry, final Type type) {
}
}


private ContainerCodecHelper() {
}
}
6 changes: 6 additions & 0 deletions bson/src/main/org/bson/internal/UuidHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ public static UUID decodeBinaryToUuid(final byte[] data, final byte type, final
return new UUID(readLongFromArrayBigEndian(localData, 0), readLongFromArrayBigEndian(localData, 8));
}

public static boolean isLegacyUUID(final UuidRepresentation uuidRepresentation) {
return uuidRepresentation == UuidRepresentation.JAVA_LEGACY
|| uuidRepresentation == UuidRepresentation.C_SHARP_LEGACY
|| uuidRepresentation == UuidRepresentation.PYTHON_LEGACY;
}

private UuidHelper() {
}
}
2 changes: 1 addition & 1 deletion bson/src/main/org/bson/internal/vector/VectorHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public static byte[] encodeVectorToBinary(final Vector vector) {
* encodedVector is not mutated nor stored in the returned {@link Vector}.
*/
public static Vector decodeBinaryToVector(final byte[] encodedVector) {
isTrue("Vector encoded array length must be at least 2.", encodedVector.length >= METADATA_SIZE);
isTrue("Vector encoded array length must be at least 2, but found: " + encodedVector.length, encodedVector.length >= METADATA_SIZE);
Vector.DataType dataType = determineVectorDType(encodedVector[0]);
byte padding = encodedVector[1];
switch (dataType) {
Expand Down
14 changes: 14 additions & 0 deletions bson/src/test/unit/org/bson/BsonBinaryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,20 @@ void shouldThrowExceptionForInvalidFloatArrayLengthWhenDecode() {
thrown.getMessage());
}

@ParameterizedTest
@ValueSource(ints = {0, 1})
void shouldThrowExceptionWhenEncodedVectorLengthIsLessThenMetadataLength(final int encodedVectorLength) {
// given
byte[] invalidData = new byte[encodedVectorLength];

// when & Then
BsonInvalidOperationException thrown = assertThrows(BsonInvalidOperationException.class, () -> {
new BsonBinary(BsonBinarySubType.VECTOR, invalidData).asVector();
});
assertEquals("Vector encoded array length must be at least 2, but found: " + encodedVectorLength,
thrown.getMessage());
}

@ParameterizedTest
@ValueSource(bytes = {-1, 1})
void shouldThrowExceptionForInvalidFloatArrayPaddingWhenDecode(final byte invalidPadding) {
Expand Down