Skip to content

Commit fcbaaea

Browse files
committed
refactor: remove Vectors.Builder in favour of a merge-constructor
This makes for a cleaner syntax in obj -> obj.vectors(Vectors.of(...), Vectors.of(named, ...));
1 parent 4af5d37 commit fcbaaea

7 files changed

Lines changed: 20 additions & 38 deletions

File tree

src/main/java/io/weaviate/client6/v1/api/collections/ObjectMetadata.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ public Builder uuid(String uuid) {
3636
return this;
3737
}
3838

39-
/** Attach custom vector to the object.. */
40-
public Builder vectors(Vectors vectors) {
41-
this.vectors = vectors;
39+
/** Attach custom vectors to the object.. */
40+
public Builder vectors(Vectors... vectors) {
41+
this.vectors = new Vectors(vectors);
4242
return this;
4343
}
4444

src/main/java/io/weaviate/client6/v1/api/collections/Vectors.java

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import java.util.Collections;
55
import java.util.HashMap;
66
import java.util.Map;
7-
import java.util.function.Function;
87

98
import com.google.gson.Gson;
109
import com.google.gson.JsonArray;
@@ -48,14 +47,6 @@ public static Vectors of(String name, float[][] vector) {
4847
return new Vectors(name, vector);
4948
}
5049

51-
public static Vectors of(Function<Builder, ObjectBuilder<Vectors>> fn) {
52-
return fn.apply(new Builder()).build();
53-
}
54-
55-
public Vectors(Builder builder) {
56-
this.namedVectors = builder.namedVectors;
57-
}
58-
5950
/**
6051
* Create a single named vector.
6152
*
@@ -84,23 +75,13 @@ private Vectors(Map<String, Object> namedVectors) {
8475
this.namedVectors = namedVectors;
8576
}
8677

87-
public static class Builder implements ObjectBuilder<Vectors> {
88-
private final Map<String, Object> namedVectors = new HashMap<>();
89-
90-
public Builder vector(String name, float[] vector) {
91-
this.namedVectors.put(name, vector);
92-
return this;
93-
}
94-
95-
public Builder vector(String name, float[][] vector) {
96-
this.namedVectors.put(name, vector);
97-
return this;
98-
}
99-
100-
@Override
101-
public Vectors build() {
102-
return new Vectors(this);
78+
/** Merge all vectors in a single vector map. */
79+
public Vectors(Vectors... vectors) {
80+
var namedVectors = new HashMap<String, Object>();
81+
for (var vec : vectors) {
82+
namedVectors.putAll(vec.asMap());
10383
}
84+
this.namedVectors = namedVectors;
10485
}
10586

10687
/**

src/main/java/io/weaviate/client6/v1/api/collections/data/InsertObjectRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public Builder<T> uuid(String uuid) {
6363
return this;
6464
}
6565

66-
public Builder<T> vectors(Vectors vectors) {
66+
public Builder<T> vectors(Vectors... vectors) {
6767
this.metadata.vectors(vectors);
6868
return this;
6969
}

src/main/java/io/weaviate/client6/v1/api/collections/data/ReplaceObjectRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public Builder<T> properties(T properties) {
5454
return this;
5555
}
5656

57-
public Builder<T> vectors(Vectors vectors) {
57+
public Builder<T> vectors(Vectors... vectors) {
5858
this.metadata.vectors(vectors);
5959
return this;
6060
}

src/main/java/io/weaviate/client6/v1/api/collections/data/UpdateObjectRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public Builder<T> properties(T properties) {
5454
return this;
5555
}
5656

57-
public Builder<T> vectors(Vectors vectors) {
57+
public Builder<T> vectors(Vectors... vectors) {
5858
this.metadata.vectors(vectors);
5959
return this;
6060
}

src/main/java/io/weaviate/client6/v1/api/collections/query/QueryRequest.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,22 +177,23 @@ private static <T> WeaviateObject<T, Object, ObjectMetadata> unmarshalWithRefere
177177
var metadataBuilder = new ObjectMetadata.Builder()
178178
.uuid(metadataResult.getId());
179179

180-
var vectors = new Vectors.Builder();
180+
var vectors = new Vectors[metadataResult.getVectorsList().size()];
181+
var i = 0;
181182
for (final var vector : metadataResult.getVectorsList()) {
182183
var vectorName = vector.getName();
183184
var vbytes = vector.getVectorBytes();
184185
switch (vector.getType()) {
185186
case VECTOR_TYPE_SINGLE_FP32:
186-
vectors.vector(vectorName, ByteStringUtil.decodeVectorSingle(vbytes));
187+
vectors[i++] = Vectors.of(vectorName, ByteStringUtil.decodeVectorSingle(vbytes));
187188
break;
188189
case VECTOR_TYPE_MULTI_FP32:
189-
vectors.vector(vectorName, ByteStringUtil.decodeVectorMulti(vbytes));
190+
vectors[i++] = Vectors.of(vectorName, ByteStringUtil.decodeVectorMulti(vbytes));
190191
break;
191192
default:
192193
continue;
193194
}
194195
}
195-
metadataBuilder.vectors(vectors.build());
196+
metadataBuilder.vectors(vectors);
196197
metadata = metadataBuilder.build();
197198
}
198199

src/test/java/io/weaviate/client6/v1/internal/json/JSONTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,9 @@ public static Object[][] testCases() {
210210
},
211211
{
212212
Vectors.class,
213-
Vectors.of(named -> named
214-
.vector("1d", new float[] { 1f, 2f })
215-
.vector("2d", new float[][] { { 1f, 2f }, { 3f, 4f } })),
213+
new Vectors(
214+
Vectors.of("1d", new float[] { 1f, 2f }),
215+
Vectors.of("2d", new float[][] { { 1f, 2f }, { 3f, 4f } })),
216216
"{\"1d\": [1.0, 2.0], \"2d\": [[1.0, 2.0], [3.0, 4.0]]}",
217217
(CustomAssert) JSONTest::compareVectors,
218218
},

0 commit comments

Comments
 (0)