Skip to content

Commit 870c5a4

Browse files
committed
feat: add sync features to async client
1 parent a7a6cd7 commit 870c5a4

2 files changed

Lines changed: 66 additions & 0 deletions

File tree

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.weaviate.client6.v1.api.collections;
22

3+
import java.io.IOException;
4+
import java.util.List;
35
import java.util.Map;
46
import java.util.Optional;
57
import java.util.concurrent.CompletableFuture;
@@ -42,7 +44,24 @@ public CompletableFuture<Optional<WeaviateCollection>> getConfig(String name) {
4244
return this.restTransport.performRequestAsync(new GetConfigRequest(name), GetConfigRequest._ENDPOINT);
4345
}
4446

47+
public CompletableFuture<List<WeaviateCollection>> list() {
48+
return this.restTransport.performRequestAsync(new ListCollectionRequest(), ListCollectionRequest._ENDPOINT);
49+
}
50+
4551
public CompletableFuture<Void> delete(String name) {
4652
return this.restTransport.performRequestAsync(new DeleteCollectionRequest(name), DeleteCollectionRequest._ENDPOINT);
4753
}
54+
55+
public CompletableFuture<Void> deleteAll() throws IOException {
56+
return list().thenCompose(collections -> {
57+
var futures = collections.stream()
58+
.map(collection -> delete(collection.name()))
59+
.toArray(CompletableFuture[]::new);
60+
return CompletableFuture.allOf(futures);
61+
});
62+
}
63+
64+
public CompletableFuture<Boolean> exists(String name) {
65+
return getConfig(name).thenApply(Optional::isPresent);
66+
}
4867
}

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.weaviate.client6.v1.api.collections.data;
22

33
import java.io.IOException;
4+
import java.util.Collection;
45
import java.util.concurrent.CompletableFuture;
56
import java.util.function.Function;
67

@@ -38,4 +39,50 @@ public CompletableFuture<Void> delete(String uuid) {
3839
return this.restTransport.performRequestAsync(new DeleteObjectRequest(collectionDescriptor.name(), uuid),
3940
DeleteObjectRequest._ENDPOINT);
4041
}
42+
43+
public CompletableFuture<Void> referenceAdd(String fromUuid, String fromProperty, Reference reference) {
44+
return forEachAsync(reference.uuids(), uuid -> {
45+
var singleRef = new Reference(reference.collection(), (String) uuid);
46+
return this.restTransport.performRequestAsync(new ReferenceAddRequest(fromUuid, fromProperty, singleRef),
47+
ReferenceAddRequest.endpoint(collectionDescriptor));
48+
});
49+
}
50+
51+
public CompletableFuture<Void> referenceDelete(String fromUuid, String fromProperty, Reference reference) {
52+
return forEachAsync(reference.uuids(), uuid -> {
53+
var singleRef = new Reference(reference.collection(), (String) uuid);
54+
return this.restTransport.performRequestAsync(new ReferenceDeleteRequest(fromUuid, fromProperty, singleRef),
55+
ReferenceDeleteRequest.endpoint(collectionDescriptor));
56+
});
57+
}
58+
59+
public CompletableFuture<Void> referenceReplace(String fromUuid, String fromProperty, Reference reference) {
60+
return forEachAsync(reference.uuids(), uuid -> {
61+
var singleRef = new Reference(reference.collection(), (String) uuid);
62+
return this.restTransport.performRequestAsync(new ReferenceReplaceRequest(fromUuid, fromProperty, singleRef),
63+
ReferenceReplaceRequest.endpoint(collectionDescriptor));
64+
});
65+
}
66+
67+
/**
68+
* Spawn execution {@code fn} for each of the {@code elements} and return a
69+
* flattened {@link CompletableFuture#allOf}.
70+
*
71+
* <p>
72+
* Usage:
73+
*
74+
* <pre>{@code
75+
* // With elements immediately available
76+
* forEachAsync(myElements, element -> doNetworkIo(element));
77+
*
78+
* // Chain to another CompletableFuture
79+
* fetch(request).thenCompose(elements -> forEachAsync(...));
80+
* }</pre>
81+
*/
82+
private static <T> CompletableFuture<Void> forEachAsync(Collection<T> elements,
83+
Function<T, CompletableFuture<?>> fn) {
84+
var futures = elements.stream().map(el -> fn.apply(el))
85+
.toArray(CompletableFuture[]::new);
86+
return CompletableFuture.allOf(futures);
87+
}
4188
}

0 commit comments

Comments
 (0)