|
1 | 1 | package io.weaviate.client6.v1.api.collections.data; |
2 | 2 |
|
3 | 3 | import java.io.IOException; |
| 4 | +import java.util.Collection; |
4 | 5 | import java.util.concurrent.CompletableFuture; |
5 | 6 | import java.util.function.Function; |
6 | 7 |
|
@@ -38,4 +39,50 @@ public CompletableFuture<Void> delete(String uuid) { |
38 | 39 | return this.restTransport.performRequestAsync(new DeleteObjectRequest(collectionDescriptor.name(), uuid), |
39 | 40 | DeleteObjectRequest._ENDPOINT); |
40 | 41 | } |
| 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 | + } |
41 | 88 | } |
0 commit comments