Skip to content

Rename Vector class names. #1595

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 4 commits into from
Jan 8, 2025
Merged
Changes from all commits
Commits
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
@@ -20,23 +20,19 @@
import static org.bson.assertions.Assertions.notNull;

/**
* Represents a vector that is stored and retrieved using the BSON Binary Subtype 9 format.
* This class supports multiple vector {@link DataType}'s and provides static methods to create
* vectors.
* <p>
* Vectors are densely packed arrays of numbers, all the same type, which are stored efficiently
* in BSON using a binary format.
* Binary Vectors are densely packed arrays of numbers, all the same type, which are stored and retrieved efficiently using the BSON Binary
* Subtype 9 format. This class supports multiple vector {@link DataType}'s and provides static methods to create vectors.
* <p>
* <b>NOTE:</b> This class should be treated as <b>sealed</b>: it must not be extended or implemented by consumers of the library.
*
* @mongodb.server.release 6.0
* @see BsonBinary
* @since 5.3
*/
public abstract class Vector {
public abstract class BinaryVector {
private final DataType dataType;

Vector(final DataType dataType) {
BinaryVector(final DataType dataType) {
this.dataType = dataType;
}

@@ -56,18 +52,18 @@ public abstract class Vector {
* </pre>
* <p>
* NOTE: The byte array `data` is not copied; changes to the provided array will be reflected
* in the created {@link PackedBitVector} instance.
* in the created {@link PackedBitBinaryVector} instance.
*
* @param data The byte array representing the packed bit vector data. Each byte can store 8 bits.
* @param padding The number of least-significant bits (0 to 7) to ignore in the final byte of the vector data.
* @return A {@link PackedBitVector} instance with the {@link DataType#PACKED_BIT} data type.
* @return A {@link PackedBitBinaryVector} instance with the {@link DataType#PACKED_BIT} data type.
* @throws IllegalArgumentException If the padding value is greater than 7.
*/
public static PackedBitVector packedBitVector(final byte[] data, final byte padding) {
public static PackedBitBinaryVector packedBitVector(final byte[] data, final byte padding) {
notNull("data", data);
isTrueArgument("Padding must be between 0 and 7 bits. Provided padding: " + padding, padding >= 0 && padding <= 7);
isTrueArgument("Padding must be 0 if vector is empty. Provided padding: " + padding, padding == 0 || data.length > 0);
return new PackedBitVector(data, padding);
return new PackedBitBinaryVector(data, padding);
}

/**
@@ -77,14 +73,14 @@ public static PackedBitVector packedBitVector(final byte[] data, final byte padd
* with values in the range [-128, 127].</p>
* <p>
* NOTE: The byte array `data` is not copied; changes to the provided array will be reflected
* in the created {@link Int8Vector} instance.
* in the created {@link Int8BinaryVector} instance.
*
* @param data The byte array representing the {@link DataType#INT8} vector data.
* @return A {@link Int8Vector} instance with the {@link DataType#INT8} data type.
* @return A {@link Int8BinaryVector} instance with the {@link DataType#INT8} data type.
*/
public static Int8Vector int8Vector(final byte[] data) {
public static Int8BinaryVector int8Vector(final byte[] data) {
notNull("data", data);
return new Int8Vector(data);
return new Int8BinaryVector(data);
}

/**
@@ -93,50 +89,50 @@ public static Int8Vector int8Vector(final byte[] data) {
* A {@link DataType#FLOAT32} vector is a vector of floating-point numbers, where each element in the vector is a float.</p>
* <p>
* NOTE: The float array `data` is not copied; changes to the provided array will be reflected
* in the created {@link Float32Vector} instance.
* in the created {@link Float32BinaryVector} instance.
*
* @param data The float array representing the {@link DataType#FLOAT32} vector data.
* @return A {@link Float32Vector} instance with the {@link DataType#FLOAT32} data type.
* @return A {@link Float32BinaryVector} instance with the {@link DataType#FLOAT32} data type.
*/
public static Float32Vector floatVector(final float[] data) {
public static Float32BinaryVector floatVector(final float[] data) {
notNull("data", data);
return new Float32Vector(data);
return new Float32BinaryVector(data);
}

/**
* Returns the {@link PackedBitVector}.
* Returns the {@link PackedBitBinaryVector}.
*
* @return {@link PackedBitVector}.
* @return {@link PackedBitBinaryVector}.
* @throws IllegalStateException if this vector is not of type {@link DataType#PACKED_BIT}. Use {@link #getDataType()} to check the vector
* type before calling this method.
*/
public PackedBitVector asPackedBitVector() {
public PackedBitBinaryVector asPackedBitVector() {
ensureType(DataType.PACKED_BIT);
return (PackedBitVector) this;
return (PackedBitBinaryVector) this;
}

/**
* Returns the {@link Int8Vector}.
* Returns the {@link Int8BinaryVector}.
*
* @return {@link Int8Vector}.
* @return {@link Int8BinaryVector}.
* @throws IllegalStateException if this vector is not of type {@link DataType#INT8}. Use {@link #getDataType()} to check the vector
* type before calling this method.
*/
public Int8Vector asInt8Vector() {
public Int8BinaryVector asInt8Vector() {
ensureType(DataType.INT8);
return (Int8Vector) this;
return (Int8BinaryVector) this;
}

/**
* Returns the {@link Float32Vector}.
* Returns the {@link Float32BinaryVector}.
*
* @return {@link Float32Vector}.
* @return {@link Float32BinaryVector}.
* @throws IllegalStateException if this vector is not of type {@link DataType#FLOAT32}. Use {@link #getDataType()} to check the vector
* type before calling this method.
*/
public Float32Vector asFloat32Vector() {
public Float32BinaryVector asFloat32Vector() {
ensureType(DataType.FLOAT32);
return (Float32Vector) this;
return (Float32BinaryVector) this;
}

/**
16 changes: 8 additions & 8 deletions bson/src/main/org/bson/BsonBinary.java
Original file line number Diff line number Diff line change
@@ -18,12 +18,12 @@

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

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

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

/**
* A representation of the BSON Binary type. Note that for performance reasons instances of this class are not immutable,
@@ -93,12 +93,12 @@ public BsonBinary(final UUID uuid) {
}

/**
* Constructs a {@linkplain BsonBinarySubType#VECTOR subtype 9} {@link BsonBinary} from the given {@link Vector}.
* Constructs a {@linkplain BsonBinarySubType#VECTOR subtype 9} {@link BsonBinary} from the given {@link BinaryVector}.
*
* @param vector the {@link Vector}
* @param vector the {@link BinaryVector}
* @since 5.3
*/
public BsonBinary(final Vector vector) {
public BsonBinary(final BinaryVector vector) {
if (vector == null) {
throw new IllegalArgumentException("Vector must not be null");
}
@@ -145,18 +145,18 @@ public UUID asUuid() {
}

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

return VectorHelper.decodeBinaryToVector(this.data);
return BinaryVectorHelper.decodeBinaryToVector(this.data);
}

/**
2 changes: 1 addition & 1 deletion bson/src/main/org/bson/BsonBinarySubType.java
Original file line number Diff line number Diff line change
@@ -78,7 +78,7 @@ public enum BsonBinarySubType {
*
* @mongodb.server.release 6.0
* @since 5.3
* @see Vector
* @see BinaryVector
*/
VECTOR((byte) 0x09),

Original file line number Diff line number Diff line change
@@ -23,30 +23,30 @@
/**
* 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.
* The {@link Float32BinaryVector} 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 BinaryVector#floatVector(float[])
* @see BsonBinary#BsonBinary(BinaryVector)
* @see BsonBinary#asVector()
* @since 5.3
*/
public final class Float32Vector extends Vector {
public final class Float32BinaryVector extends BinaryVector {

private final float[] data;

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

/**
* Retrieve the underlying float array representing this {@link Float32Vector}, where each float
* Retrieve the underlying float array representing this {@link Float32BinaryVector}, 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.
* @return the underlying float array representing this {@link Float32BinaryVector} vector.
*/
public float[] getData() {
return assertNotNull(data);
@@ -60,7 +60,7 @@ public boolean equals(final Object o) {
if (o == null || getClass() != o.getClass()) {
return false;
}
Float32Vector that = (Float32Vector) o;
Float32BinaryVector that = (Float32BinaryVector) o;
return Arrays.equals(data, that.data);
}

Original file line number Diff line number Diff line change
@@ -24,30 +24,30 @@
/**
* 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.
* The {@link Int8BinaryVector} 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 BinaryVector#int8Vector(byte[])
* @see BsonBinary#BsonBinary(BinaryVector)
* @see BsonBinary#asVector()
* @since 5.3
*/
public final class Int8Vector extends Vector {
public final class Int8BinaryVector extends BinaryVector {

private byte[] data;

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

/**
* Retrieve the underlying byte array representing this {@link Int8Vector} vector, where each byte represents
* Retrieve the underlying byte array representing this {@link Int8BinaryVector} 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.
* @return the underlying byte array representing this {@link Int8BinaryVector} vector.
*/
public byte[] getData() {
return assertNotNull(data);
@@ -61,7 +61,7 @@ public boolean equals(final Object o) {
if (o == null || getClass() != o.getClass()) {
return false;
}
Int8Vector that = (Int8Vector) o;
Int8BinaryVector that = (Int8BinaryVector) o;
return Objects.deepEquals(data, that.data);
}

Original file line number Diff line number Diff line change
@@ -24,33 +24,33 @@
/**
* Represents a packed bit vector, where each element of the vector is represented by a single bit (0 or 1).
* <p>
* The {@link PackedBitVector} is used to store data efficiently using the BSON Binary Subtype 9 format.
* The {@link PackedBitBinaryVector} is used to store data efficiently using the BSON Binary Subtype 9 format.
*
* @mongodb.server.release 6.0
* @see Vector#packedBitVector(byte[], byte)
* @see BsonBinary#BsonBinary(Vector)
* @see BinaryVector#packedBitVector(byte[], byte)
* @see BsonBinary#BsonBinary(BinaryVector)
* @see BsonBinary#asVector()
* @since 5.3
*/
public final class PackedBitVector extends Vector {
public final class PackedBitBinaryVector extends BinaryVector {

private final byte padding;
private final byte[] data;

PackedBitVector(final byte[] data, final byte padding) {
PackedBitBinaryVector(final byte[] data, final byte padding) {
super(DataType.PACKED_BIT);
this.data = assertNotNull(data);
this.padding = padding;
}

/**
* Retrieve the underlying byte array representing this {@link PackedBitVector} vector, where
* Retrieve the underlying byte array representing this {@link PackedBitBinaryVector} vector, where
* each bit represents an element of the vector (either 0 or 1).
* <p>
* Note that the {@linkplain #getPadding() padding value} should be considered when interpreting the final byte of the array,
* as it indicates how many least-significant bits are to be ignored.
*
* @return the underlying byte array representing this {@link PackedBitVector} vector.
* @return the underlying byte array representing this {@link PackedBitBinaryVector} vector.
* @see #getPadding()
*/
public byte[] getData() {
@@ -81,7 +81,7 @@ public boolean equals(final Object o) {
if (o == null || getClass() != o.getClass()) {
return false;
}
PackedBitVector that = (PackedBitVector) o;
PackedBitBinaryVector that = (PackedBitBinaryVector) o;
return padding == that.padding && Arrays.equals(data, that.data);
}

Original file line number Diff line number Diff line change
@@ -21,21 +21,21 @@
import org.bson.BsonInvalidOperationException;
import org.bson.BsonReader;
import org.bson.BsonWriter;
import org.bson.Vector;
import org.bson.BinaryVector;

/**
* Encodes and decodes {@link Vector} objects.
* Encodes and decodes {@link BinaryVector} objects.
*
*/
final class VectorCodec implements Codec<Vector> {
final class BinaryVectorCodec implements Codec<BinaryVector> {

@Override
public void encode(final BsonWriter writer, final Vector vectorToEncode, final EncoderContext encoderContext) {
public void encode(final BsonWriter writer, final BinaryVector vectorToEncode, final EncoderContext encoderContext) {
writer.writeBinaryData(new BsonBinary(vectorToEncode));
}

@Override
public Vector decode(final BsonReader reader, final DecoderContext decoderContext) {
public BinaryVector decode(final BsonReader reader, final DecoderContext decoderContext) {
byte subType = reader.peekBinarySubType();

if (subType != BsonBinarySubType.VECTOR.getValue()) {
@@ -48,8 +48,8 @@ public Vector decode(final BsonReader reader, final DecoderContext decoderContex
}

@Override
public Class<Vector> getEncoderClass() {
return Vector.class;
public Class<BinaryVector> getEncoderClass() {
return BinaryVector.class;
}
}

4 changes: 2 additions & 2 deletions bson/src/main/org/bson/codecs/ContainerCodecHelper.java
Original file line number Diff line number Diff line change
@@ -21,7 +21,7 @@
import org.bson.BsonType;
import org.bson.Transformer;
import org.bson.UuidRepresentation;
import org.bson.Vector;
import org.bson.BinaryVector;
import org.bson.codecs.configuration.CodecConfigurationException;
import org.bson.codecs.configuration.CodecRegistry;

@@ -68,7 +68,7 @@ private static Codec<?> getBinarySubTypeCodec(final BsonReader reader,
final Codec<?> binaryTypeCodec) {

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