From 9d7f667bea9328241fef009624ea37ec4bfe2930 Mon Sep 17 00:00:00 2001 From: dyma solovei Date: Mon, 23 Jun 2025 15:57:40 +0200 Subject: [PATCH] feat(sync): add list/deleteAll/exists methods to 'collections' namespace --- .../integration/CollectionsITest.java | 28 +++++++++++++++++++ .../collections/ListCollectionRequest.java | 21 ++++++++++++++ .../collections/ListCollectionResponse.java | 8 ++++++ .../WeaviateCollectionsClient.java | 15 ++++++++++ 4 files changed, 72 insertions(+) create mode 100644 src/main/java/io/weaviate/client6/v1/api/collections/ListCollectionRequest.java create mode 100644 src/main/java/io/weaviate/client6/v1/api/collections/ListCollectionResponse.java diff --git a/src/it/java/io/weaviate/integration/CollectionsITest.java b/src/it/java/io/weaviate/integration/CollectionsITest.java index 0b5b94fbe..be270a42f 100644 --- a/src/it/java/io/weaviate/integration/CollectionsITest.java +++ b/src/it/java/io/weaviate/integration/CollectionsITest.java @@ -86,4 +86,32 @@ public void testCrossReferences() throws IOException { .containsOnly(nsOnlineStores, nsMarkets); }); } + + @Test + public void testListDeleteAll() throws IOException { + var nsA = ns("A"); + var nsB = ns("B"); + var nsC = ns("C"); + + client.collections.create(nsA); + client.collections.create(nsB); + client.collections.create(nsC); + + Assertions.assertThat(client.collections.exists(nsA)).isTrue(); + Assertions.assertThat(client.collections.exists(nsB)).isTrue(); + Assertions.assertThat(client.collections.exists(nsC)).isTrue(); + Assertions.assertThat(client.collections.exists(ns("X"))).isFalse(); + + var all = client.collections.list(); + Assertions.assertThat(all) + .hasSizeGreaterThanOrEqualTo(3) + .extracting(WeaviateCollection::name) + .contains(nsA, nsB, nsC); + + client.collections.deleteAll(); + + all = client.collections.list(); + Assertions.assertThat(all.isEmpty()); + + } } diff --git a/src/main/java/io/weaviate/client6/v1/api/collections/ListCollectionRequest.java b/src/main/java/io/weaviate/client6/v1/api/collections/ListCollectionRequest.java new file mode 100644 index 000000000..2c7535743 --- /dev/null +++ b/src/main/java/io/weaviate/client6/v1/api/collections/ListCollectionRequest.java @@ -0,0 +1,21 @@ +package io.weaviate.client6.v1.api.collections; + +import java.util.Collections; +import java.util.List; + +import org.apache.hc.core5.http.HttpStatus; + +import com.google.gson.reflect.TypeToken; + +import io.weaviate.client6.v1.internal.json.JSON; +import io.weaviate.client6.v1.internal.rest.Endpoint; + +public record ListCollectionRequest() { + public static final Endpoint> _ENDPOINT = Endpoint.of( + request -> "GET", + request -> "/schema", + (gson, request) -> null, + request -> Collections.emptyMap(), + code -> code != HttpStatus.SC_SUCCESS, + (gson, response) -> JSON.deserialize(response, ListCollectionResponse.class).collections()); +} diff --git a/src/main/java/io/weaviate/client6/v1/api/collections/ListCollectionResponse.java b/src/main/java/io/weaviate/client6/v1/api/collections/ListCollectionResponse.java new file mode 100644 index 000000000..4523db1fe --- /dev/null +++ b/src/main/java/io/weaviate/client6/v1/api/collections/ListCollectionResponse.java @@ -0,0 +1,8 @@ +package io.weaviate.client6.v1.api.collections; + +import java.util.List; + +import com.google.gson.annotations.SerializedName; + +public record ListCollectionResponse(@SerializedName("classes") List collections) { +} diff --git a/src/main/java/io/weaviate/client6/v1/api/collections/WeaviateCollectionsClient.java b/src/main/java/io/weaviate/client6/v1/api/collections/WeaviateCollectionsClient.java index 0937572bf..c14c7ccfa 100644 --- a/src/main/java/io/weaviate/client6/v1/api/collections/WeaviateCollectionsClient.java +++ b/src/main/java/io/weaviate/client6/v1/api/collections/WeaviateCollectionsClient.java @@ -1,6 +1,7 @@ package io.weaviate.client6.v1.api.collections; import java.io.IOException; +import java.util.List; import java.util.Map; import java.util.Optional; import java.util.function.Function; @@ -41,7 +42,21 @@ public Optional getConfig(String name) throws IOException { return this.restTransport.performRequest(new GetConfigRequest(name), GetConfigRequest._ENDPOINT); } + public List list() throws IOException { + return this.restTransport.performRequest(new ListCollectionRequest(), ListCollectionRequest._ENDPOINT); + } + public void delete(String name) throws IOException { this.restTransport.performRequest(new DeleteCollectionRequest(name), DeleteCollectionRequest._ENDPOINT); } + + public void deleteAll() throws IOException { + for (var collection : list()) { + delete(collection.name()); + } + } + + public boolean exists(String name) throws IOException { + return getConfig(name).isPresent(); + } }