Skip to content

Commit 8b8418b

Browse files
committed
feat: extend WeaviateCollectionsClientAsync to support ORM methods
1 parent 35e6a1c commit 8b8418b

7 files changed

Lines changed: 238 additions & 28 deletions

File tree

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

Lines changed: 90 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ public WeaviateCollectionsClient(RestTransport restTransport, GrpcTransport grpc
2121
this.grpcTransport = grpcTransport;
2222
}
2323

24+
/**
25+
* Obtain a handle to send requests to a particular collection.
26+
* The returned object is thread-safe.
27+
*
28+
* @param cls Class that represents an object in the collection.
29+
* @return a handle for a collection with {@code Class<PropertiesT>}
30+
* properties.
31+
*/
2432
public <PropertiesT extends Record> CollectionHandle<PropertiesT> use(Class<PropertiesT> cls) {
2533
return use(CollectionDescriptor.ofClass(cls), CollectionHandleDefaults.none());
2634
}
@@ -29,6 +37,22 @@ public <PropertiesT extends Record> CollectionHandle<PropertiesT> use(Class<Prop
2937
* Obtain a handle to send requests to a particular collection.
3038
* The returned object is thread-safe.
3139
*
40+
* @param cls Class that represents an object in the collection.
41+
* @param fn Lamda expression for optional parameters.
42+
* @return a handle for a collection with {@code Class<PropertiesT>}
43+
* properties.
44+
*/
45+
public <PropertiesT extends Record> CollectionHandle<PropertiesT> use(
46+
Class<PropertiesT> cls,
47+
Function<CollectionHandleDefaults.Builder, ObjectBuilder<CollectionHandleDefaults>> fn) {
48+
return use(CollectionDescriptor.ofClass(cls), fn);
49+
}
50+
51+
/**
52+
* Obtain a handle to send requests to a particular collection.
53+
* The returned object is thread-safe.
54+
*
55+
* @param collectionName Name of the collection.
3256
* @return a handle for a collection with {@code Map<String, Object>}
3357
* properties.
3458
*/
@@ -40,6 +64,8 @@ public CollectionHandle<Map<String, Object>> use(String collectionName) {
4064
* Obtain a handle to send requests to a particular collection.
4165
* The returned object is thread-safe.
4266
*
67+
* @param collectionName Name of the collection.
68+
* @param fn Lamda expression for optional parameters.
4369
* @return a handle for a collection with {@code Map<String, Object>}
4470
* properties.
4571
*/
@@ -54,39 +80,90 @@ private <PropertiesT> CollectionHandle<PropertiesT> use(CollectionDescriptor<Pro
5480
return new CollectionHandle<>(restTransport, grpcTransport, collection, CollectionHandleDefaults.of(fn));
5581
}
5682

83+
/**
84+
* Create a new Weaviate collection with default configuration.
85+
*
86+
* <pre>{@code
87+
* // Define a record class that represents an object in collection.
88+
* record Song(
89+
* String title;
90+
* int yearReleased;
91+
* String[] genres;
92+
* ) {}
93+
*
94+
* client.collections.create(Song.class);
95+
* }</pre>
96+
*
97+
* @param cls Class that represents an object in the collection.
98+
* @return the configuration of the created collection.
99+
* @throws WeaviateApiException in case the server returned with an
100+
* error status code.
101+
* @throws IOException in case the request was not sent successfully
102+
* due to a malformed request, a networking error
103+
* or the server being unavailable.
104+
* @see io.weaviate.client6.v1.api.collections.annotations.Collection
105+
* @see io.weaviate.client6.v1.api.collections.annotations.Property
106+
*/
57107
public <PropertiesT extends Record> CollectionConfig create(Class<PropertiesT> cls) throws IOException {
58108
var collection = CollectionDescriptor.ofClass(cls);
59109
return create(CollectionConfig.of(collection.name(), collection.configFn()));
60110
}
61111

112+
/**
113+
* Create and configure a new Weaviate collection. See
114+
* {@link CollectionConfig.Builder} for available options.
115+
*
116+
* @param cls Class that represents an object in the collection.
117+
* @param fn Lamda expression for optional parameters.
118+
* @return the configuration of the created collection.
119+
* @throws WeaviateApiException in case the server returned with an
120+
* error status code.
121+
* @throws IOException in case the request was not sent successfully
122+
* due to a malformed request, a networking error
123+
* or the server being unavailable.
124+
* @see io.weaviate.client6.v1.api.collections.annotations.Collection
125+
* @see io.weaviate.client6.v1.api.collections.annotations.Property
126+
* @see WeaviateCollectionsClient#create(Class)
127+
*/
128+
public <PropertiesT extends Record> CollectionConfig create(
129+
Class<PropertiesT> cls,
130+
Function<CollectionConfig.Builder, ObjectBuilder<CollectionConfig>> fn) throws IOException {
131+
var collection = CollectionDescriptor.ofClass(cls);
132+
var configFn = ObjectBuilder.partial(fn, collection.configFn());
133+
return create(CollectionConfig.of(collection.name(), configFn));
134+
}
135+
62136
/**
63137
* Create a new Weaviate collection with default configuration.
64138
*
139+
* @param collectionName Collection name.
65140
* @return the configuration of the created collection.
66141
* @throws WeaviateApiException in case the server returned with an
67142
* error status code.
68143
* @throws IOException in case the request was not sent successfully
69144
* due to a malformed request, a networking error
70145
* or the server being unavailable.
71146
*/
72-
public CollectionConfig create(String name) throws IOException {
73-
return create(CollectionConfig.of(name));
147+
public CollectionConfig create(String collectionName) throws IOException {
148+
return create(CollectionConfig.of(collectionName));
74149
}
75150

76151
/**
77152
* Create and configure a new Weaviate collection. See
78153
* {@link CollectionConfig.Builder} for available options.
79154
*
155+
* @param collectionName Collection name.
156+
* @param fn Lamda expression for optional parameters.
80157
* @return the configuration of the created collection.
81158
* @throws WeaviateApiException in case the server returned with an
82159
* error status code.
83160
* @throws IOException in case the request was not sent successfully
84161
* due to a malformed request, a networking error
85162
* or the server being unavailable.
86163
*/
87-
public CollectionConfig create(String name,
164+
public CollectionConfig create(String collectionName,
88165
Function<CollectionConfig.Builder, ObjectBuilder<CollectionConfig>> fn) throws IOException {
89-
return create(CollectionConfig.of(name, fn));
166+
return create(CollectionConfig.of(collectionName, fn));
90167
}
91168

92169
/**
@@ -107,15 +184,16 @@ public CollectionConfig create(CollectionConfig collection) throws IOException {
107184
/**
108185
* Fetch Weaviate collection configuration.
109186
*
187+
* @param collectionName Collection name.
110188
* @return the collection configuration if one with this name exists.
111189
* @throws WeaviateApiException in case the server returned with an
112190
* error status code.
113191
* @throws IOException in case the request was not sent successfully
114192
* due to a malformed request, a networking error
115193
* or the server being unavailable.
116194
*/
117-
public Optional<CollectionConfig> getConfig(String name) throws IOException {
118-
return this.restTransport.performRequest(new GetConfigRequest(name), GetConfigRequest._ENDPOINT);
195+
public Optional<CollectionConfig> getConfig(String collectionName) throws IOException {
196+
return this.restTransport.performRequest(new GetConfigRequest(collectionName), GetConfigRequest._ENDPOINT);
119197
}
120198

121199
/**
@@ -135,14 +213,15 @@ public List<CollectionConfig> list() throws IOException {
135213
/**
136214
* Delete a Weaviate collection.
137215
*
216+
* @param collectionName Collection name.
138217
* @throws WeaviateApiException in case the server returned with an
139218
* error status code.
140219
* @throws IOException in case the request was not sent successfully
141220
* due to a malformed request, a networking error
142221
* or the server being unavailable.
143222
*/
144-
public void delete(String name) throws IOException {
145-
this.restTransport.performRequest(new DeleteCollectionRequest(name), DeleteCollectionRequest._ENDPOINT);
223+
public void delete(String collectionName) throws IOException {
224+
this.restTransport.performRequest(new DeleteCollectionRequest(collectionName), DeleteCollectionRequest._ENDPOINT);
146225
}
147226

148227
/**
@@ -163,13 +242,14 @@ public void deleteAll() throws IOException {
163242
/**
164243
* Check if a collection with this name exists.
165244
*
245+
* @param collectionName Collection name.
166246
* @throws WeaviateApiException in case the server returned with an
167247
* error status code.
168248
* @throws IOException in case the request was not sent successfully
169249
* due to a malformed request, a networking error
170250
* or the server being unavailable.
171251
*/
172-
public boolean exists(String name) throws IOException {
173-
return getConfig(name).isPresent();
252+
public boolean exists(String collectionName) throws IOException {
253+
return getConfig(collectionName).isPresent();
174254
}
175255
}

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

Lines changed: 135 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,46 +21,161 @@ public WeaviateCollectionsClientAsync(RestTransport restTransport, GrpcTransport
2121
this.grpcTransport = grpcTransport;
2222
}
2323

24+
/**
25+
* Obtain a handle to send requests to a particular collection.
26+
* The returned object is thread-safe.
27+
*
28+
* @param cls Class that represents an object in the collection.
29+
* @return a handle for a collection with {@code Class<PropertiesT>}
30+
* properties.
31+
*/
32+
public <PropertiesT extends Record> CollectionHandleAsync<PropertiesT> use(Class<PropertiesT> cls) {
33+
return use(CollectionDescriptor.ofClass(cls), CollectionHandleDefaults.none());
34+
}
35+
36+
/**
37+
* Obtain a handle to send requests to a particular collection.
38+
* The returned object is thread-safe.
39+
*
40+
* @param cls Class that represents an object in the collection.
41+
* @param fn Lamda expression for optional parameters.
42+
* @return a handle for a collection with {@code Class<PropertiesT>}
43+
* properties.
44+
*/
45+
public <PropertiesT extends Record> CollectionHandleAsync<PropertiesT> use(
46+
Class<PropertiesT> cls,
47+
Function<CollectionHandleDefaults.Builder, ObjectBuilder<CollectionHandleDefaults>> fn) {
48+
return use(CollectionDescriptor.ofClass(cls), fn);
49+
}
50+
51+
/**
52+
* Obtain a handle to send requests to a particular collection.
53+
* The returned object is thread-safe.
54+
*
55+
* @param collectionName Name of the collection.
56+
* @return a handle for a collection with {@code Map<String, Object>}
57+
* properties.
58+
*/
2459
public CollectionHandleAsync<Map<String, Object>> use(String collectionName) {
2560
return use(collectionName, CollectionHandleDefaults.none());
2661
}
2762

63+
/**
64+
* Obtain a handle to send requests to a particular collection.
65+
* The returned object is thread-safe.
66+
*
67+
* @param collectionName Name of the collection.
68+
* @param fn Lamda expression for optional parameters.
69+
* @return a handle for a collection with {@code Map<String, Object>}
70+
* properties.
71+
*/
2872
public CollectionHandleAsync<Map<String, Object>> use(
2973
String collectionName,
3074
Function<CollectionHandleDefaults.Builder, ObjectBuilder<CollectionHandleDefaults>> fn) {
31-
return new CollectionHandleAsync<>(
32-
restTransport,
33-
grpcTransport,
34-
CollectionDescriptor.ofMap(collectionName),
35-
CollectionHandleDefaults.of(fn));
75+
return use(CollectionDescriptor.ofMap(collectionName), fn);
76+
}
77+
78+
private <PropertiesT> CollectionHandleAsync<PropertiesT> use(CollectionDescriptor<PropertiesT> collection,
79+
Function<CollectionHandleDefaults.Builder, ObjectBuilder<CollectionHandleDefaults>> fn) {
80+
return new CollectionHandleAsync<>(restTransport, grpcTransport, collection, CollectionHandleDefaults.of(fn));
81+
}
82+
83+
/**
84+
* Create a new Weaviate collection with default configuration.
85+
*
86+
* <pre>{@code
87+
* // Define a record class that represents an object in collection.
88+
* record Song(
89+
* String title;
90+
* int yearReleased;
91+
* String[] genres;
92+
* ) {}
93+
*
94+
* client.collections.create(Song.class);
95+
* }</pre>
96+
*
97+
* @param cls Class that represents an object in the collection.
98+
* @see io.weaviate.client6.v1.api.collections.annotations.Collection
99+
* @see io.weaviate.client6.v1.api.collections.annotations.Property
100+
*/
101+
public <PropertiesT extends Record> CompletableFuture<CollectionConfig> create(Class<PropertiesT> cls) {
102+
var collection = CollectionDescriptor.ofClass(cls);
103+
return create(CollectionConfig.of(collection.name(), collection.configFn()));
104+
}
105+
106+
/**
107+
* Create and configure a new Weaviate collection. See
108+
* {@link CollectionConfig.Builder} for available options.
109+
*
110+
* @param cls Class that represents an object in the collection.
111+
* @param fn Lamda expression for optional parameters.
112+
* @see io.weaviate.client6.v1.api.collections.annotations.Collection
113+
* @see io.weaviate.client6.v1.api.collections.annotations.Property
114+
* @see WeaviateCollectionsClientAsync#create(Class)
115+
*/
116+
public <PropertiesT extends Record> CompletableFuture<CollectionConfig> create(Class<PropertiesT> cls,
117+
Function<CollectionConfig.Builder, ObjectBuilder<CollectionConfig>> fn) {
118+
var collection = CollectionDescriptor.ofClass(cls);
119+
var configFn = ObjectBuilder.partial(fn, collection.configFn());
120+
return create(CollectionConfig.of(collection.name(), configFn));
36121
}
37122

38-
public CompletableFuture<CollectionConfig> create(String name) {
39-
return create(CollectionConfig.of(name));
123+
/**
124+
* Create a new Weaviate collection with default configuration.
125+
*
126+
* @param collectionName Collection name.
127+
* @return the configuration of the created collection.
128+
*/
129+
public CompletableFuture<CollectionConfig> create(String collectionName) {
130+
return create(CollectionConfig.of(collectionName));
40131
}
41132

42-
public CompletableFuture<CollectionConfig> create(String name,
133+
/**
134+
* Create and configure a new Weaviate collection. See
135+
* {@link CollectionConfig.Builder} for available options.
136+
*
137+
* @param collectionName Collection name.
138+
* @param fn Lamda expression for optional parameters.
139+
*/
140+
public CompletableFuture<CollectionConfig> create(String collectionName,
43141
Function<CollectionConfig.Builder, ObjectBuilder<CollectionConfig>> fn) {
44-
return create(CollectionConfig.of(name, fn));
142+
return create(CollectionConfig.of(collectionName, fn));
45143
}
46144

145+
/**
146+
* Create a new Weaviate collection with {@link CollectionConfig}.
147+
*/
47148
public CompletableFuture<CollectionConfig> create(CollectionConfig collection) {
48149
return this.restTransport.performRequestAsync(new CreateCollectionRequest(collection),
49150
CreateCollectionRequest._ENDPOINT);
50151
}
51152

52-
public CompletableFuture<Optional<CollectionConfig>> getConfig(String name) {
53-
return this.restTransport.performRequestAsync(new GetConfigRequest(name), GetConfigRequest._ENDPOINT);
153+
/**
154+
* Fetch Weaviate collection configuration.
155+
*
156+
* @param collectionName Collection name.
157+
*/
158+
public CompletableFuture<Optional<CollectionConfig>> getConfig(String collectionName) {
159+
return this.restTransport.performRequestAsync(new GetConfigRequest(collectionName), GetConfigRequest._ENDPOINT);
54160
}
55161

56162
public CompletableFuture<List<CollectionConfig>> list() {
57163
return this.restTransport.performRequestAsync(new ListCollectionRequest(), ListCollectionRequest._ENDPOINT);
58164
}
59165

60-
public CompletableFuture<Void> delete(String name) {
61-
return this.restTransport.performRequestAsync(new DeleteCollectionRequest(name), DeleteCollectionRequest._ENDPOINT);
166+
/**
167+
* Delete a Weaviate collection.
168+
*
169+
* @param collectionName Collection name.
170+
*/
171+
public CompletableFuture<Void> delete(String collectionName) {
172+
return this.restTransport.performRequestAsync(new DeleteCollectionRequest(collectionName),
173+
DeleteCollectionRequest._ENDPOINT);
62174
}
63175

176+
/**
177+
* Delete all collections in Weaviate.
178+
*/
64179
public CompletableFuture<Void> deleteAll() throws IOException {
65180
return list().thenCompose(collections -> {
66181
var futures = collections.stream()
@@ -70,7 +185,12 @@ public CompletableFuture<Void> deleteAll() throws IOException {
70185
});
71186
}
72187

73-
public CompletableFuture<Boolean> exists(String name) {
74-
return getConfig(name).thenApply(Optional::isPresent);
188+
/**
189+
* Check if a collection with this name exists.
190+
*
191+
* @param collectionName Collection name.
192+
*/
193+
public CompletableFuture<Boolean> exists(String collectionName) {
194+
return getConfig(collectionName).thenApply(Optional::isPresent);
75195
}
76196
}

src/main/java/io/weaviate/client6/v1/api/collections/annotations/Collection.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
@Retention(RetentionPolicy.RUNTIME)
99
@Target(ElementType.TYPE)
1010
public @interface Collection {
11+
/** The name of the collection mapped by this class. */
1112
String value();
1213

14+
/** Collection description to add on creation. */
1315
String description() default "";
1416
}

0 commit comments

Comments
 (0)