|
| 1 | +package io.weaviate.client6.v1.api.collections.data; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.util.Arrays; |
| 5 | + |
| 6 | +import com.google.gson.TypeAdapter; |
| 7 | +import com.google.gson.stream.JsonReader; |
| 8 | +import com.google.gson.stream.JsonWriter; |
| 9 | + |
| 10 | +import io.weaviate.client6.v1.api.collections.WeaviateObject; |
| 11 | + |
| 12 | +public record BatchReference(String fromCollection, String fromProperty, String fromUuid, Reference reference) { |
| 13 | + |
| 14 | + public static BatchReference[] objects(WeaviateObject<?, ?, ?> fromObject, String fromProperty, |
| 15 | + WeaviateObject<?, ?, ?>... toObjects) { |
| 16 | + return Arrays.stream(toObjects) |
| 17 | + .map(to -> new BatchReference( |
| 18 | + fromObject.collection(), fromProperty, fromObject.metadata().uuid(), |
| 19 | + Reference.object(to))) |
| 20 | + .toArray(BatchReference[]::new); |
| 21 | + } |
| 22 | + |
| 23 | + public static BatchReference[] uuids(WeaviateObject<?, ?, ?> fromObject, String fromProperty, |
| 24 | + String... toUuids) { |
| 25 | + return Arrays.stream(toUuids) |
| 26 | + .map(to -> new BatchReference( |
| 27 | + fromObject.collection(), fromProperty, fromObject.metadata().uuid(), |
| 28 | + Reference.uuids(to))) |
| 29 | + .toArray(BatchReference[]::new); |
| 30 | + } |
| 31 | + |
| 32 | + public static final TypeAdapter<BatchReference> TYPE_ADAPTER = new TypeAdapter<BatchReference>() { |
| 33 | + |
| 34 | + @Override |
| 35 | + public void write(JsonWriter out, BatchReference value) throws IOException { |
| 36 | + out.beginObject(); |
| 37 | + |
| 38 | + out.name("from"); |
| 39 | + out.value(Reference.toBeacon(value.fromCollection, value.fromProperty, value.fromUuid)); |
| 40 | + |
| 41 | + out.name("to"); |
| 42 | + out.value(Reference.toBeacon(value.reference.collection(), value.reference.uuids().get(0))); |
| 43 | + |
| 44 | + // TODO: add tenant |
| 45 | + |
| 46 | + out.endObject(); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public BatchReference read(JsonReader in) throws IOException { |
| 51 | + String fromCollection = null; |
| 52 | + String fromProperty = null; |
| 53 | + String fromUuid = null; |
| 54 | + Reference toReference = null; |
| 55 | + |
| 56 | + in.beginObject(); |
| 57 | + while (in.hasNext()) { |
| 58 | + switch (in.nextName()) { |
| 59 | + |
| 60 | + case "from": { |
| 61 | + var beacon = in.nextString(); |
| 62 | + beacon = beacon.replaceFirst("weaviate://localhost/", ""); |
| 63 | + |
| 64 | + var parts = beacon.split("/"); |
| 65 | + fromCollection = parts[0]; |
| 66 | + fromUuid = parts[1]; |
| 67 | + fromProperty = parts[2]; |
| 68 | + break; |
| 69 | + } |
| 70 | + |
| 71 | + case "to": { |
| 72 | + String collection = null; |
| 73 | + String id = null; |
| 74 | + |
| 75 | + var beacon = in.nextString(); |
| 76 | + beacon = beacon.replaceFirst("weaviate://localhost/", ""); |
| 77 | + if (beacon.contains("/")) { |
| 78 | + var parts = beacon.split("/"); |
| 79 | + collection = parts[0]; |
| 80 | + id = parts[1]; |
| 81 | + } else { |
| 82 | + id = beacon; |
| 83 | + } |
| 84 | + toReference = new Reference(collection, id); |
| 85 | + break; |
| 86 | + } |
| 87 | + |
| 88 | + // case "tenant": |
| 89 | + // switch (in.peek()) { |
| 90 | + // case STRING: |
| 91 | + // in.nextString(); |
| 92 | + // case NULL: |
| 93 | + // in.nextNull(); |
| 94 | + // default: |
| 95 | + // // We don't expect anything else |
| 96 | + // } |
| 97 | + // System.out.println("processed tenant"); |
| 98 | + // break; |
| 99 | + // default: |
| 100 | + // in.skipValue(); |
| 101 | + } |
| 102 | + } |
| 103 | + in.endObject(); |
| 104 | + |
| 105 | + return new BatchReference(fromCollection, fromProperty, fromUuid, toReference); |
| 106 | + } |
| 107 | + }.nullSafe(); |
| 108 | +} |
0 commit comments