Skip to content

Commit a7a6cd7

Browse files
authored
Merge pull request #394 from weaviate/v6-collections-list-delete-exists
v6: list, deleteAll, exists methods for `collections` namespace
2 parents 071101c + 9d7f667 commit a7a6cd7

File tree

4 files changed

+72
-0
lines changed

4 files changed

+72
-0
lines changed

src/it/java/io/weaviate/integration/CollectionsITest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,32 @@ public void testCrossReferences() throws IOException {
8686
.containsOnly(nsOnlineStores, nsMarkets);
8787
});
8888
}
89+
90+
@Test
91+
public void testListDeleteAll() throws IOException {
92+
var nsA = ns("A");
93+
var nsB = ns("B");
94+
var nsC = ns("C");
95+
96+
client.collections.create(nsA);
97+
client.collections.create(nsB);
98+
client.collections.create(nsC);
99+
100+
Assertions.assertThat(client.collections.exists(nsA)).isTrue();
101+
Assertions.assertThat(client.collections.exists(nsB)).isTrue();
102+
Assertions.assertThat(client.collections.exists(nsC)).isTrue();
103+
Assertions.assertThat(client.collections.exists(ns("X"))).isFalse();
104+
105+
var all = client.collections.list();
106+
Assertions.assertThat(all)
107+
.hasSizeGreaterThanOrEqualTo(3)
108+
.extracting(WeaviateCollection::name)
109+
.contains(nsA, nsB, nsC);
110+
111+
client.collections.deleteAll();
112+
113+
all = client.collections.list();
114+
Assertions.assertThat(all.isEmpty());
115+
116+
}
89117
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package io.weaviate.client6.v1.api.collections;
2+
3+
import java.util.Collections;
4+
import java.util.List;
5+
6+
import org.apache.hc.core5.http.HttpStatus;
7+
8+
import com.google.gson.reflect.TypeToken;
9+
10+
import io.weaviate.client6.v1.internal.json.JSON;
11+
import io.weaviate.client6.v1.internal.rest.Endpoint;
12+
13+
public record ListCollectionRequest() {
14+
public static final Endpoint<ListCollectionRequest, List<WeaviateCollection>> _ENDPOINT = Endpoint.of(
15+
request -> "GET",
16+
request -> "/schema",
17+
(gson, request) -> null,
18+
request -> Collections.emptyMap(),
19+
code -> code != HttpStatus.SC_SUCCESS,
20+
(gson, response) -> JSON.deserialize(response, ListCollectionResponse.class).collections());
21+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package io.weaviate.client6.v1.api.collections;
2+
3+
import java.util.List;
4+
5+
import com.google.gson.annotations.SerializedName;
6+
7+
public record ListCollectionResponse(@SerializedName("classes") List<WeaviateCollection> collections) {
8+
}

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

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

33
import java.io.IOException;
4+
import java.util.List;
45
import java.util.Map;
56
import java.util.Optional;
67
import java.util.function.Function;
@@ -41,7 +42,21 @@ public Optional<WeaviateCollection> getConfig(String name) throws IOException {
4142
return this.restTransport.performRequest(new GetConfigRequest(name), GetConfigRequest._ENDPOINT);
4243
}
4344

45+
public List<WeaviateCollection> list() throws IOException {
46+
return this.restTransport.performRequest(new ListCollectionRequest(), ListCollectionRequest._ENDPOINT);
47+
}
48+
4449
public void delete(String name) throws IOException {
4550
this.restTransport.performRequest(new DeleteCollectionRequest(name), DeleteCollectionRequest._ENDPOINT);
4651
}
52+
53+
public void deleteAll() throws IOException {
54+
for (var collection : list()) {
55+
delete(collection.name());
56+
}
57+
}
58+
59+
public boolean exists(String name) throws IOException {
60+
return getConfig(name).isPresent();
61+
}
4762
}

0 commit comments

Comments
 (0)