|
| 1 | +package io.weaviate.client.v1.async.schema.api; |
| 2 | + |
| 3 | +import java.util.Collections; |
| 4 | +import java.util.HashMap; |
| 5 | +import java.util.Map; |
| 6 | +import java.util.concurrent.CompletableFuture; |
| 7 | +import java.util.concurrent.CompletionException; |
| 8 | +import java.util.concurrent.ExecutionException; |
| 9 | +import java.util.concurrent.Future; |
| 10 | + |
| 11 | +import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient; |
| 12 | +import org.apache.hc.core5.concurrent.FutureCallback; |
| 13 | +import org.apache.hc.core5.http.ContentType; |
| 14 | +import org.apache.hc.core5.http.HttpResponse; |
| 15 | + |
| 16 | +import io.weaviate.client.Config; |
| 17 | +import io.weaviate.client.base.AsyncBaseClient; |
| 18 | +import io.weaviate.client.base.AsyncClientResult; |
| 19 | +import io.weaviate.client.base.Response; |
| 20 | +import io.weaviate.client.base.Result; |
| 21 | +import io.weaviate.client.base.http.async.ResponseParser; |
| 22 | +import io.weaviate.client.base.util.UrlEncoder; |
| 23 | +import io.weaviate.client.v1.auth.provider.AccessTokenProvider; |
| 24 | +import io.weaviate.client.v1.schema.model.WeaviateClass; |
| 25 | +import io.weaviate.client.v1.schema.model.WeaviateClass.VectorConfig; |
| 26 | + |
| 27 | +public class VectorAdder extends AsyncBaseClient<Boolean> implements AsyncClientResult<Boolean> { |
| 28 | + private final ClassGetter getter; |
| 29 | + |
| 30 | + private String className; |
| 31 | + private Map<String, VectorConfig> addedVectors = new HashMap<>(); |
| 32 | + |
| 33 | + public VectorAdder(CloseableHttpAsyncClient client, Config config, AccessTokenProvider tokenProvider) { |
| 34 | + super(client, config, tokenProvider); |
| 35 | + this.getter = new ClassGetter(client, config, tokenProvider); |
| 36 | + } |
| 37 | + |
| 38 | + public VectorAdder withClassName(String className) { |
| 39 | + this.className = className; |
| 40 | + return this; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Add a named vectors. This method can be chained to add multiple named vectors |
| 45 | + * without using a {@link Map}. |
| 46 | + */ |
| 47 | + public VectorAdder withVectorConfig(String name, VectorConfig vector) { |
| 48 | + this.addedVectors.put(name, vector); |
| 49 | + return this; |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Add all vectors from the map. This will overwrite any vectors added |
| 54 | + * previously. |
| 55 | + */ |
| 56 | + public VectorAdder withVectorConfig(Map<String, VectorConfig> vectors) { |
| 57 | + this.addedVectors = Collections.unmodifiableMap(vectors); |
| 58 | + return this; |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public Future<Result<Boolean>> run(FutureCallback<Result<Boolean>> callback) { |
| 63 | + CompletableFuture<Result<WeaviateClass>> getClass = CompletableFuture.supplyAsync(() -> { |
| 64 | + try { |
| 65 | + return getter.withClassName(className).run().get(); |
| 66 | + } catch (InterruptedException | ExecutionException e) { |
| 67 | + throw new CompletionException(e); |
| 68 | + } |
| 69 | + }); |
| 70 | + CompletableFuture<Result<Boolean>> addVectors = getClass.<Result<Boolean>>thenApplyAsync(result -> { |
| 71 | + if (result.getError() != null) { |
| 72 | + return result.toErrorResult(); |
| 73 | + } |
| 74 | + WeaviateClass cls = result.getResult(); |
| 75 | + addedVectors.entrySet().stream() |
| 76 | + .forEach(vector -> cls.getVectorConfig() |
| 77 | + .putIfAbsent(vector.getKey(), vector.getValue())); |
| 78 | + |
| 79 | + String path = String.format("/schema/%s", UrlEncoder.encodePathParam(className)); |
| 80 | + try { |
| 81 | + return sendPutRequest(path, cls, callback, new ResponseParser<Boolean>() { |
| 82 | + |
| 83 | + @Override |
| 84 | + public Result<Boolean> parse(HttpResponse response, String body, ContentType contentType) { |
| 85 | + Response<WeaviateClass> resp = this.serializer.toResponse(response.getCode(), body, WeaviateClass.class); |
| 86 | + return new Result<>(response.getCode(), response.getCode() <= 299, resp.getErrors()); |
| 87 | + } |
| 88 | + }).get(); |
| 89 | + } catch (InterruptedException | ExecutionException e) { |
| 90 | + throw new CompletionException(e); |
| 91 | + } |
| 92 | + }); |
| 93 | + |
| 94 | + return addVectors; |
| 95 | + } |
| 96 | +} |
0 commit comments