24
24
import org .springframework .util .ObjectUtils ;
25
25
26
26
/**
27
- * MongoDB-specific extension to {@link Vector} based on Mongo's {@link BinaryVector}. Note that only float32 and int8
28
- * variants can be represented as floating-point numbers. int1 returns an all-zero array for {@link #toFloatArray()} and
29
- * {@link #toDoubleArray()}.
27
+ * MongoDB-specific extension to {@link Vector} based on Mongo's {@link BinaryVector}. Note that only {@code float32}
28
+ * and {@code int8} variants can be represented as floating-point numbers. {@code int1} throws
29
+ * {@link UnsupportedOperationException} when calling {@link #toFloatArray()} and {@link #toDoubleArray()}.
30
30
*
31
31
* @author Mark Paluch
32
32
* @since 4.5
@@ -40,15 +40,65 @@ public class MongoVector implements Vector {
40
40
}
41
41
42
42
/**
43
- * Creates a new {@link MongoVector} from the given {@link BinaryVector}.
43
+ * Creates a new binary {@link MongoVector} using the given {@link BinaryVector}.
44
44
*
45
45
* @param v binary vector representation.
46
- * @return the {@link MongoVector} for the given vector values .
46
+ * @return the {@link MongoVector} wrapping {@link BinaryVector} .
47
47
*/
48
48
public static MongoVector of (BinaryVector v ) {
49
49
return new MongoVector (v );
50
50
}
51
51
52
+ /**
53
+ * Creates a new binary {@link MongoVector} using the given {@code data}.
54
+ * <p>
55
+ * A {@link BinaryVector.DataType#INT8} vector is a vector of 8-bit signed integers where each byte in the vector
56
+ * represents an element of a vector, with values in the range {@code [-128, 127]}.
57
+ * <p>
58
+ * NOTE: The byte array is not copied; changes to the provided array will be referenced in the created
59
+ * {@code MongoVector} instance.
60
+ *
61
+ * @param data the byte array representing the {@link BinaryVector.DataType#INT8} vector data.
62
+ * @return the {@link MongoVector} containing the given vector values to be represented as binary {@code int8}.
63
+ */
64
+ public static MongoVector ofInt8 (byte [] data ) {
65
+ return of (BinaryVector .int8Vector (data ));
66
+ }
67
+
68
+ /**
69
+ * Creates a new binary {@link MongoVector} using the given {@code data}.
70
+ * <p>
71
+ * A {@link BinaryVector.DataType#FLOAT32} vector is a vector of floating-point numbers, where each element in the
72
+ * vector is a {@code float}.
73
+ * <p>
74
+ * NOTE: The float array is not copied; changes to the provided array will be referenced in the created
75
+ * {@code MongoVector} instance.
76
+ *
77
+ * @param data the float array representing the {@link BinaryVector.DataType#FLOAT32} vector data.
78
+ * @return the {@link MongoVector} containing the given vector values to be represented as binary {@code float32}.
79
+ */
80
+ public static MongoVector ofFloat (float ... data ) {
81
+ return of (BinaryVector .floatVector (data ));
82
+ }
83
+
84
+ /**
85
+ * Creates a new binary {@link MongoVector} from the given {@link Vector}.
86
+ * <p>
87
+ * A {@link BinaryVector.DataType#FLOAT32} vector is a vector of floating-point numbers, where each element in the
88
+ * vector is a {@code float}. The given {@link Vector} must be able to return a {@link Vector#toFloatArray() float}
89
+ * array.
90
+ * <p>
91
+ * NOTE: The float array is not copied; changes to the provided array will be referenced in the created
92
+ * {@code MongoVector} instance.
93
+ *
94
+ * @param v the
95
+ * @return the {@link MongoVector} using vector values from the given {@link Vector} to be represented as binary
96
+ * float32.
97
+ */
98
+ public static MongoVector fromFloat (Vector v ) {
99
+ return of (BinaryVector .floatVector (v .toFloatArray ()));
100
+ }
101
+
52
102
@ Override
53
103
public Class <? extends Number > getType () {
54
104
@@ -90,6 +140,11 @@ public int size() {
90
140
return 0 ;
91
141
}
92
142
143
+ /**
144
+ * {@inheritDoc}
145
+ *
146
+ * @throws UnsupportedOperationException if the underlying data type is {@code int1} {@link PackedBitBinaryVector}.
147
+ */
93
148
@ Override
94
149
public float [] toFloatArray () {
95
150
@@ -102,14 +157,22 @@ public float[] toFloatArray() {
102
157
103
158
if (v instanceof Int8BinaryVector i ) {
104
159
105
- float [] result = new float [i .getData ().length ];
106
- System .arraycopy (i .getData (), 0 , result , 0 , result .length );
160
+ byte [] data = i .getData ();
161
+ float [] result = new float [data .length ];
162
+ for (int j = 0 ; j < data .length ; j ++) {
163
+ result [j ] = data [j ];
164
+ }
107
165
return result ;
108
166
}
109
167
110
- return new float [ size ()] ;
168
+ throw new UnsupportedOperationException ( "Cannot return float array for " + v . getClass ()) ;
111
169
}
112
170
171
+ /**
172
+ * {@inheritDoc}
173
+ *
174
+ * @throws UnsupportedOperationException if the underlying data type is {@code int1} {@link PackedBitBinaryVector}.
175
+ */
113
176
@ Override
114
177
public double [] toDoubleArray () {
115
178
@@ -126,12 +189,15 @@ public double[] toDoubleArray() {
126
189
127
190
if (v instanceof Int8BinaryVector i ) {
128
191
129
- double [] result = new double [i .getData ().length ];
130
- System .arraycopy (i .getData (), 0 , result , 0 , result .length );
192
+ byte [] data = i .getData ();
193
+ double [] result = new double [data .length ];
194
+ for (int j = 0 ; j < data .length ; j ++) {
195
+ result [j ] = data [j ];
196
+ }
131
197
return result ;
132
198
}
133
199
134
- return new double [ size ()] ;
200
+ throw new UnsupportedOperationException ( "Cannot return double array for " + v . getClass ()) ;
135
201
}
136
202
137
203
@ Override
0 commit comments