Skip to content

Commit bc821a2

Browse files
committed
feat: add data.replace method
1 parent 08106d4 commit bc821a2

4 files changed

Lines changed: 105 additions & 9 deletions

File tree

src/it/java/io/weaviate/integration/DataITest.java

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,30 @@ public void testReferences_AddReplaceDelete() throws IOException {
180180
}
181181

182182
@Test
183-
public void testReplace() {
184-
// Replace (PUT):
185-
// properties, reference, vectors
183+
public void testReplace() throws IOException {
184+
// Arrange
185+
var nsBooks = ns("Books");
186+
187+
client.collections.create(nsBooks,
188+
collection -> collection
189+
.properties(Property.text("title"), Property.integer("year")));
190+
191+
// Add 1 book with 'title' only.
192+
var books = client.collections.use(nsBooks);
193+
var ivanhoe = books.data.insert(Map.of("title", "ivanhoe"));
194+
195+
// Act
196+
books.data.replace(ivanhoe.metadata().uuid(),
197+
replace -> replace.properties(Map.of("year", 1819)));
198+
199+
// Assert
200+
var replacedIvanhoe = books.query.byId(ivanhoe.metadata().uuid());
201+
202+
Assertions.assertThat(replacedIvanhoe).get()
203+
.as("has ONLY year property")
204+
.extracting(WeaviateObject::properties, InstanceOfAssertFactories.MAP)
205+
.doesNotContain(Map.entry("title", "ivanhoe"))
206+
.contains(Map.entry("year", 1819L));
186207
}
187208

188209
@Test
@@ -231,9 +252,9 @@ public void testUpdate() throws IOException {
231252
Assertions.assertThat(updIvanhoe).get()
232253
.satisfies(book -> {
233254
Assertions.assertThat(book)
234-
.as("has year property")
255+
.as("has both year and title property")
235256
.extracting(WeaviateObject::properties, InstanceOfAssertFactories.MAP)
236-
.contains(Map.entry("year", 1819L));
257+
.contains(Map.entry("title", "ivanhoe"), Map.entry("year", 1819L));
237258

238259
Assertions.assertThat(book)
239260
.as("has reference to Authors")
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package io.weaviate.client6.v1.api.collections.data;
2+
3+
import java.util.Collections;
4+
import java.util.function.Function;
5+
6+
import com.google.gson.reflect.TypeToken;
7+
8+
import io.weaviate.client6.v1.api.collections.ObjectMetadata;
9+
import io.weaviate.client6.v1.api.collections.Vectors;
10+
import io.weaviate.client6.v1.api.collections.WeaviateObject;
11+
import io.weaviate.client6.v1.internal.ObjectBuilder;
12+
import io.weaviate.client6.v1.internal.json.JSON;
13+
import io.weaviate.client6.v1.internal.orm.CollectionDescriptor;
14+
import io.weaviate.client6.v1.internal.rest.Endpoint;
15+
16+
public record ReplaceObjectRequest<T>(WeaviateObject<T, Reference, ObjectMetadata> object) {
17+
18+
static final <T> Endpoint<ReplaceObjectRequest<T>, Void> endpoint(CollectionDescriptor<T> collectionDescriptor) {
19+
return Endpoint.of(
20+
request -> "PUT",
21+
request -> "/objects/" + collectionDescriptor.name() + "/" + request.object.metadata().uuid(),
22+
(gson, request) -> JSON.serialize(request.object, TypeToken.getParameterized(
23+
WeaviateObject.class, collectionDescriptor.typeToken().getType(), Reference.class, ObjectMetadata.class)),
24+
request -> Collections.emptyMap(),
25+
code -> code != 200,
26+
(gson, response) -> null);
27+
}
28+
29+
public static <T> ReplaceObjectRequest<T> of(String collectionName, String uuid,
30+
Function<ReplaceObjectRequest.Builder<T>, ObjectBuilder<ReplaceObjectRequest<T>>> fn) {
31+
return fn.apply(new Builder<>(collectionName, uuid)).build();
32+
}
33+
34+
public ReplaceObjectRequest(Builder<T> builder) {
35+
this(builder.object.build());
36+
}
37+
38+
public static class Builder<T> implements ObjectBuilder<ReplaceObjectRequest<T>> {
39+
private final WeaviateObject.Builder<T, Reference, ObjectMetadata> object = new WeaviateObject.Builder<>();
40+
private final ObjectMetadata.Builder metadata = new ObjectMetadata.Builder();
41+
42+
public Builder(String collectionName, String uuid) {
43+
this.object.collection(collectionName);
44+
this.metadata.uuid(uuid);
45+
}
46+
47+
public Builder<T> properties(T properties) {
48+
this.object.properties(properties);
49+
return this;
50+
}
51+
52+
public Builder<T> vectors(Vectors vectors) {
53+
this.metadata.vectors(vectors);
54+
return this;
55+
}
56+
57+
public Builder<T> reference(String property, Reference... references) {
58+
this.object.reference(property, references);
59+
return this;
60+
}
61+
62+
@Override
63+
public ReplaceObjectRequest<T> build() {
64+
this.object.metadata(this.metadata.build());
65+
return new ReplaceObjectRequest<>(this);
66+
}
67+
}
68+
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ static final <T> Endpoint<UpdateObjectRequest<T>, Void> endpoint(CollectionDescr
2626
(gson, response) -> null);
2727
}
2828

29-
public static <T> UpdateObjectRequest<T> of(String uuid,
29+
public static <T> UpdateObjectRequest<T> of(String collectionName, String uuid,
3030
Function<UpdateObjectRequest.Builder<T>, ObjectBuilder<UpdateObjectRequest<T>>> fn) {
31-
return fn.apply(new Builder<>(uuid)).build();
31+
return fn.apply(new Builder<>(collectionName, uuid)).build();
3232
}
3333

3434
public UpdateObjectRequest(Builder<T> builder) {
@@ -39,7 +39,8 @@ public static class Builder<T> implements ObjectBuilder<UpdateObjectRequest<T>>
3939
private final WeaviateObject.Builder<T, Reference, ObjectMetadata> object = new WeaviateObject.Builder<>();
4040
private final ObjectMetadata.Builder metadata = new ObjectMetadata.Builder();
4141

42-
public Builder(String uuid) {
42+
public Builder(String collectionName, String uuid) {
43+
this.object.collection(collectionName);
4344
this.metadata.uuid(uuid);
4445
}
4546

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,16 @@ public boolean exists(String uuid) throws IOException {
4343

4444
public void update(String uuid, Function<UpdateObjectRequest.Builder<T>, ObjectBuilder<UpdateObjectRequest<T>>> fn)
4545
throws IOException {
46-
this.restTransport.performRequest(UpdateObjectRequest.of(uuid, fn),
46+
this.restTransport.performRequest(UpdateObjectRequest.of(collectionDescriptor.name(), uuid, fn),
4747
UpdateObjectRequest.endpoint(collectionDescriptor));
4848
}
4949

50+
public void replace(String uuid, Function<ReplaceObjectRequest.Builder<T>, ObjectBuilder<ReplaceObjectRequest<T>>> fn)
51+
throws IOException {
52+
this.restTransport.performRequest(ReplaceObjectRequest.of(collectionDescriptor.name(), uuid, fn),
53+
ReplaceObjectRequest.endpoint(collectionDescriptor));
54+
}
55+
5056
public void delete(String uuid) throws IOException {
5157
this.restTransport.performRequest(new DeleteObjectRequest(collectionDescriptor.name(), uuid),
5258
DeleteObjectRequest._ENDPOINT);

0 commit comments

Comments
 (0)