Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import scala.collection.mutable
import org.bson._
import org.bson.codecs.configuration.{ CodecRegistries, CodecRegistry }
import org.bson.codecs.{ Codec, DecoderContext, Encoder, EncoderContext }
import scala.collection.immutable.Vector

import org.mongodb.scala.bson.BsonNull

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import org.mongodb.scala.bson.annotations.{ BsonIgnore, BsonProperty }
import org.mongodb.scala.bson.codecs.Macros.{ createCodecProvider, createCodecProviderIgnoreNone }
import org.mongodb.scala.bson.codecs.Registry.DEFAULT_CODEC_REGISTRY
import org.mongodb.scala.bson.collection.immutable.Document
import scala.collection.immutable.Vector

import scala.collection.JavaConverters._
import scala.reflect.ClassTag
Expand Down
32 changes: 32 additions & 0 deletions bson/src/main/org/bson/BsonBinary.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@

import org.bson.assertions.Assertions;
import org.bson.internal.UuidHelper;
import org.bson.internal.vector.VectorHelper;

import java.util.Arrays;
import java.util.UUID;

import static org.bson.internal.vector.VectorHelper.encodeVectorToBinary;

/**
* A representation of the BSON Binary type. Note that for performance reasons instances of this class are not immutable,
* so care should be taken to only modify the underlying byte array if you know what you're doing, or else make a defensive copy.
Expand Down Expand Up @@ -89,6 +92,20 @@ public BsonBinary(final UUID uuid) {
this(uuid, UuidRepresentation.STANDARD);
}

/**
* Construct a Type 9 BsonBinary from the given Vector.
*
* @param vector the {@link Vector}
* @since BINARY_VECTOR
*/
public BsonBinary(final Vector vector) {
if (vector == null) {
throw new IllegalArgumentException("Vector must not be null");
}
this.data = encodeVectorToBinary(vector);
type = BsonBinarySubType.VECTOR.getValue();
}

/**
* Construct a new instance from the given UUID and UuidRepresentation
*
Expand Down Expand Up @@ -127,6 +144,21 @@ public UUID asUuid() {
return UuidHelper.decodeBinaryToUuid(this.data.clone(), this.type, UuidRepresentation.STANDARD);
}

/**
* Returns the binary as a {@link Vector}. The binary type must be 9.
*
* @return the vector
* @throws IllegalArgumentException if the binary subtype is not {@link BsonBinarySubType#VECTOR}.
* @since BINARY_VECTOR
*/
public Vector asVector() {
if (!BsonBinarySubType.isVector(type)) {
throw new BsonInvalidOperationException("type must be a Vector subtype.");
}

return VectorHelper.decodeBinaryToVector(this.data);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[just a note]

We talked about this being a potential performance issue if an application has to read a Vector from MongoDB only to pass it again to MongoDB (for example, when searching). If such an issue is reported, it can be solved by creating a Vector that is lazily decodable. VectorHelper.encodeVectorToBinary for such a Vector would be free and not copy data.

}

/**
* Returns the binary as a UUID.
*
Expand Down
22 changes: 17 additions & 5 deletions bson/src/main/org/bson/BsonBinarySubType.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package org.bson;

/**
* The Binary subtype
* The Binary subtype.
*
* @since 3.0
*/
Expand Down Expand Up @@ -60,7 +60,7 @@ public enum BsonBinarySubType {
ENCRYPTED((byte) 0x06),

/**
* Columnar data
* Columnar data.
*
* @since 4.4
*/
Expand All @@ -73,6 +73,14 @@ public enum BsonBinarySubType {
*/
SENSITIVE((byte) 0x08),

/**
* Vector data.
*
* @since BINARY_VECTOR
* @see Vector
*/
VECTOR((byte) 0x09),

/**
* User defined binary data.
*/
Expand All @@ -81,16 +89,20 @@ public enum BsonBinarySubType {
private final byte value;

/**
* Returns true if the given value is a UUID subtype
* Returns true if the given value is a UUID subtype.
*
* @param value the subtype value as a byte
* @return true if value is a UUID subtype
* @param value the subtype value as a byte.
* @return true if value is a UUID subtype.
* @since 3.4
*/
public static boolean isUuid(final byte value) {
return value == UUID_LEGACY.getValue() || value == UUID_STANDARD.getValue();
}

public static boolean isVector(final byte value) {
return value == VECTOR.getValue();
}

BsonBinarySubType(final byte value) {
this.value = value;
}
Expand Down
84 changes: 84 additions & 0 deletions bson/src/main/org/bson/Float32Vector.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright 2008-present MongoDB, Inc.
*
* Licensed 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.bson;

import org.bson.types.Binary;

import java.util.Arrays;

import static org.bson.assertions.Assertions.assertNotNull;

/**
* Represents a vector of 32-bit floating-point numbers, where each element in the vector is a float.
* <p>
* The {@link Float32Vector} is used to store and retrieve data efficiently using the BSON Binary Subtype 9 format.
*
* @mongodb.server.release 6.0
* @see Vector#floatVector(float[])
* @see BsonBinary#BsonBinary(Vector)
* @see BsonBinary#asVector()
* @see Binary#Binary(Vector)
* @see Binary#asVector()
* @since BINARY_VECTOR
*/
public final class Float32Vector extends Vector {

private final float[] vectorData;

Float32Vector(final float[] vectorData) {
super(DataType.FLOAT32);
this.vectorData = assertNotNull(vectorData);
}

/**
* Retrieve the underlying float array representing this {@link Float32Vector}, where each float
* represents an element of a vector.
* <p>
* NOTE: The underlying float array is not copied; changes to the returned array will be reflected in this instance.
*
* @return the underlying float array representing this {@link Float32Vector} vector.
*/
public float[] getVectorArray() {
return assertNotNull(vectorData);
}

@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (!(o instanceof Float32Vector)) {
return false;
}

Float32Vector that = (Float32Vector) o;
return Arrays.equals(vectorData, that.vectorData);
}

@Override
public int hashCode() {
return Arrays.hashCode(vectorData);
}

@Override
public String toString() {
return "Float32Vector{"
+ "vectorData=" + Arrays.toString(vectorData)
+ ", vectorType=" + getDataType()
+ '}';
}
}
84 changes: 84 additions & 0 deletions bson/src/main/org/bson/Int8Vector.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright 2008-present MongoDB, Inc.
*
* Licensed 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.bson;

import org.bson.types.Binary;

import java.util.Arrays;

import static org.bson.assertions.Assertions.assertNotNull;

/**
* Represents a vector of 8-bit signed integers, where each element in the vector is a byte.
* <p>
* The {@link Int8Vector} is used to store and retrieve data efficiently using the BSON Binary Subtype 9 format.
*
* @mongodb.server.release 6.0
* @see Vector#int8Vector(byte[])
* @see BsonBinary#BsonBinary(Vector)
* @see BsonBinary#asVector()
* @see Binary#Binary(Vector)
* @see Binary#asVector()
* @since BINARY_VECTOR
*/
public final class Int8Vector extends Vector {

private byte[] vectorData;

Int8Vector(final byte[] vectorData) {
super(DataType.INT8);
this.vectorData = assertNotNull(vectorData);
}

/**
* Retrieve the underlying byte array representing this {@link Int8Vector} vector, where each byte represents
* an element of a vector.
* <p>
* NOTE: The underlying byte array is not copied; changes to the returned array will be reflected in this instance.
*
* @return the underlying byte array representing this {@link Int8Vector} vector.
*/
public byte[] getVectorArray() {
return assertNotNull(vectorData);
}

@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (!(o instanceof Int8Vector)) {
return false;
}

Int8Vector that = (Int8Vector) o;
return Arrays.equals(vectorData, that.vectorData);
}

@Override
public int hashCode() {
return Arrays.hashCode(vectorData);
}

@Override
public String toString() {
return "Int8Vector{"
+ "vectorData=" + Arrays.toString(vectorData)
+ ", vectorType=" + getDataType()
+ '}';
}
}
Loading