Skip to content

Commit 1bc969e

Browse files
committed
more tests
1 parent bf8e11d commit 1bc969e

File tree

20 files changed

+652
-299
lines changed

20 files changed

+652
-299
lines changed

astra-db-java/src/main/java/com/datastax/astra/client/Database.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ public Database(String apiEndpoint, String token, String namespace, DataAPIOptio
126126
this.namespaceName = namespace;
127127
this.token = token;
128128
this.options = options;
129+
129130
// Adding version number if needed
130131
this.databaseAdminEndpoint = apiEndpoint.endsWith(options.getApiVersion()) ?
131132
apiEndpoint :
@@ -451,7 +452,7 @@ public <T> Collection<T> createCollection(String collectionName, CollectionOptio
451452
public void dropCollection(String collectionName) {
452453
runCommand(Command
453454
.create("deleteCollection")
454-
.append("name", collectionName), new CommandOptions());
455+
.append("name", collectionName), commandOptions);
455456
log.info("Collection '" + green("{}") + "' has been deleted", collectionName);
456457
}
457458

astra-db-java/src/main/java/com/datastax/astra/client/admin/AstraDBDatabaseAdmin.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@
2121
*/
2222

2323
import com.datastax.astra.client.DataAPIOptions;
24+
import com.datastax.astra.client.model.EmbeddingProvider;
2425
import com.datastax.astra.internal.api.AstraApiEndpoint;
2526
import com.dtsx.astra.sdk.db.AstraDBOpsClient;
2627
import com.dtsx.astra.sdk.db.domain.Database;
2728
import com.dtsx.astra.sdk.db.exception.DatabaseNotFoundException;
2829
import com.dtsx.astra.sdk.utils.AstraEnvironment;
2930

31+
import java.util.Map;
3032
import java.util.Set;
3133
import java.util.UUID;
3234

@@ -135,6 +137,12 @@ public Set<String> listNamespaceNames() {
135137
.keyspaces().findAll();
136138
}
137139

140+
/** {@inheritDoc} */
141+
@Override
142+
public Map<String, EmbeddingProvider> listEmbeddingProviders() {
143+
return new DataAPIDatabaseAdmin(getApiEndpoint() + "/" + options.getApiVersion(), token, options).listEmbeddingProviders();
144+
}
145+
138146
/** {@inheritDoc} */
139147
@Override
140148
public void createNamespace(String namespace) {

astra-db-java/src/main/java/com/datastax/astra/client/admin/DataAPIDatabaseAdmin.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.datastax.astra.client.DataAPIOptions;
2424
import com.datastax.astra.client.Database;
2525
import com.datastax.astra.client.model.CommandOptions;
26+
import com.datastax.astra.client.model.EmbeddingProvider;
2627
import com.datastax.astra.client.model.HttpClientOptions;
2728
import com.datastax.astra.internal.command.AbstractCommandRunner;
2829
import com.datastax.astra.client.model.Command;
@@ -32,6 +33,7 @@
3233
import lombok.Getter;
3334
import lombok.extern.slf4j.Slf4j;
3435

36+
import java.util.Map;
3537
import java.util.Set;
3638
import java.util.stream.Collectors;
3739

@@ -92,6 +94,13 @@ public Set<String> listNamespaceNames() {
9294
.collect(Collectors.toSet());
9395
}
9496

97+
/** {@inheritDoc} */
98+
@Override
99+
public Map<String, EmbeddingProvider> listEmbeddingProviders() {
100+
return runCommand(Command.create("findEmbeddingProviders"))
101+
.getStatusKeyAsMap("embeddingProviders", EmbeddingProvider.class);
102+
}
103+
95104
/** {@inheritDoc} */
96105
@Override
97106
public Database getDatabase(String namespaceName) {

astra-db-java/src/main/java/com/datastax/astra/client/admin/DatabaseAdmin.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222

2323
import com.datastax.astra.client.Database;
2424
import com.datastax.astra.client.model.CommandRunner;
25+
import com.datastax.astra.client.model.EmbeddingProvider;
2526

27+
import java.util.Map;
2628
import java.util.Set;
2729
import java.util.concurrent.CompletableFuture;
2830
import java.util.stream.Stream;
@@ -81,6 +83,22 @@ public interface DatabaseAdmin {
8183
*/
8284
Set<String> listNamespaceNames();
8385

86+
/**
87+
* Retrieve the list of embedding providers available in the current database. Embedding providers are services
88+
* that provide embeddings for text, images, or other data types. This method returns a map of provider names to
89+
* {@link EmbeddingProvider} instances, allowing applications to access and utilize the embedding services.
90+
*
91+
* <p>Example usage:</p>
92+
* <pre>
93+
* {@code
94+
* // Assuming 'client' is an instance of DataApiClient
95+
* Map<String, EmbeddingProvider> providers = client.listEmbeddingProviders());
96+
* }
97+
* </pre>
98+
* @return
99+
*/
100+
Map<String, EmbeddingProvider> listEmbeddingProviders();
101+
84102
/**
85103
* Asynchronously retrieves a stream of namespace names available in the current database. This method facilitates
86104
* non-blocking operations by allowing the application to continue executing other tasks while the list of namespace

astra-db-java/src/main/java/com/datastax/astra/client/model/CollectionOptions.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,9 +348,28 @@ public CollectionOptionsBuilder vector(int dimension, @NonNull SimilarityMetric
348348
* self reference
349349
*/
350350
public CollectionOptionsBuilder vectorize(String provider, String modeName) {
351+
return vectorize(provider, modeName, null);
352+
}
353+
354+
/**
355+
* Enable Vectorization within the collection.
356+
*
357+
* @param provider
358+
* provider Name (LLM)
359+
* @param modeName
360+
* mode name
361+
* @param keyName
362+
* name of the key in the system
363+
* @return
364+
* self reference
365+
*/
366+
public CollectionOptionsBuilder vectorize(String provider, String modeName, String keyName) {
351367
Service embeddingService = new Service();
352368
embeddingService.setProvider(provider);
353369
embeddingService.setModelName(modeName);
370+
if (keyName != null) {
371+
embeddingService.setAuthentication(Map.of("providerKey", keyName + ".providerKey"));
372+
}
354373
getVector().setService(embeddingService);
355374
return this;
356375
}
@@ -364,11 +383,13 @@ public CollectionOptionsBuilder vectorize(String provider, String modeName) {
364383
* mode name
365384
* @param parameters
366385
* expected parameters for vectorize
386+
* @param keyName
387+
* name of the key in the system
367388
* @return
368389
* self reference
369390
*/
370-
public CollectionOptionsBuilder vectorize(String provider, String modeName, Map<String, Object> parameters) {
371-
vectorize(provider, modeName);
391+
public CollectionOptionsBuilder vectorize(String provider, String modeName, String keyName, Map<String, Object> parameters) {
392+
vectorize(provider, modeName, keyName);
372393
getVector().getService().setParameters(parameters);
373394
return this;
374395
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package com.datastax.astra.client.model;
2+
3+
import lombok.Data;
4+
import lombok.NoArgsConstructor;
5+
6+
import java.util.List;
7+
import java.util.Map;
8+
import java.util.Optional;
9+
10+
@Data
11+
@NoArgsConstructor
12+
public class EmbeddingProvider {
13+
14+
/** Keys for the supported authentication methods */
15+
public static final String AUTHENTICATION_METHOD_NONE = "NONE";
16+
17+
/** Keys for the supported authentication methods */
18+
public static final String AUTHENTICATION_METHOD_SHARED_SECRET = "SHARED_SECRET";
19+
20+
/** Keys for the supported authentication methods */
21+
public static final String AUTHENTICATION_METHOD_HEADER = "HEADER";
22+
23+
/** Display name of the provider */
24+
private String displayName;
25+
26+
/** Display name of the provider */
27+
private String url;
28+
29+
/** Authentication */
30+
private Map<String, AuthenticationMethod> supportedAuthentication;
31+
32+
/** Parameters fo the Servuce */
33+
private List<Parameter> parameters;
34+
35+
/** List of models support. */
36+
private List<Model> models;
37+
38+
/**
39+
* Accessor for the Share Secret Authentication.
40+
*
41+
* @return
42+
* shared Secret authentication
43+
*/
44+
public Optional<AuthenticationMethod> getSharedSecretAuthentication() {
45+
return Optional.ofNullable(supportedAuthentication.get(AUTHENTICATION_METHOD_SHARED_SECRET));
46+
}
47+
48+
/**
49+
* Accessor for the Share Secret Authentication.
50+
*
51+
* @return
52+
* shared Secret authentication
53+
*/
54+
public Optional<AuthenticationMethod> getHeaderAuthentication() {
55+
return Optional.ofNullable(supportedAuthentication.get(AUTHENTICATION_METHOD_HEADER));
56+
}
57+
58+
/**
59+
* Model for the service.
60+
*/
61+
@Data
62+
@NoArgsConstructor
63+
public static class Model {
64+
private String name;
65+
private Integer vectorDimension;
66+
private List<Parameter> parameters;
67+
}
68+
69+
/**
70+
* Authentication method.
71+
*/
72+
@Data
73+
@NoArgsConstructor
74+
public static class AuthenticationMethod {
75+
private boolean enabled;
76+
private List<Token> tokens;
77+
}
78+
79+
/**
80+
* Token method.
81+
*/
82+
@Data
83+
@NoArgsConstructor
84+
public static class Token {
85+
private String forwarded;
86+
private String accepted;
87+
}
88+
89+
/**
90+
* Parameters for the service.
91+
*/
92+
@Data
93+
@NoArgsConstructor
94+
public static class Parameter {
95+
private String name;
96+
private String type;
97+
private boolean required;
98+
private String defaultValue;
99+
private Validation validation;
100+
private String help;
101+
}
102+
103+
/**
104+
* Validation Component for the parameter.
105+
*/
106+
@Data
107+
@NoArgsConstructor
108+
public static class Validation {
109+
private List<Integer> numericRange;
110+
}
111+
112+
113+
114+
}

astra-db-java/src/main/java/com/datastax/astra/internal/api/ApiResponse.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.io.Serializable;
3232
import java.util.ArrayList;
3333
import java.util.List;
34+
import java.util.Map;
3435
import java.util.stream.Stream;
3536

3637
/**
@@ -100,4 +101,20 @@ public <T> List<T> getStatusKeyAsList(@NonNull String key, Class<T> targetClass)
100101
.constructCollectionType(List.class, targetClass));
101102
}
102103

104+
/**
105+
* Retrieves a list of objects from the 'status' map based on the provided key, casting them to the specified class.
106+
* This method is suitable for cases where the status information contains lists of objects under a single key.
107+
*
108+
* @param key The key for which to retrieve the list.
109+
* @param targetClass The class to which the objects in the list should be cast.
110+
* @param <T> The type of the objects in the list to be returned.
111+
* @return The list of objects associated with the specified key, cast to the specified class; {@code null} if the key does not exist.
112+
*/
113+
public <T> Map<String, T> getStatusKeyAsMap(@NonNull String key, Class<T> targetClass) {
114+
Assert.isTrue(status.containsKey(key), "Key not found in status map");
115+
return JsonUtils.getDataApiObjectMapper().convertValue(status.get(key),
116+
JsonUtils.getDataApiObjectMapper().getTypeFactory()
117+
.constructMapType(Map.class, String.class, targetClass));
118+
}
119+
103120
}

astra-db-java/src/main/java/com/datastax/astra/internal/command/AbstractCommandRunner.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import com.datastax.astra.client.model.Command;
2525
import com.datastax.astra.client.model.CommandOptions;
2626
import com.datastax.astra.client.model.CommandRunner;
27-
import com.datastax.astra.client.model.HttpClientOptions;
2827
import com.datastax.astra.internal.api.ApiResponse;
2928
import com.datastax.astra.internal.api.ApiResponseHttp;
3029
import com.datastax.astra.internal.http.RetryHttpClient;
@@ -42,6 +41,7 @@
4241
import java.util.ArrayList;
4342
import java.util.Collections;
4443
import java.util.List;
44+
import java.util.Map;
4545
import java.util.UUID;
4646
import java.util.concurrent.CompletableFuture;
4747
import java.util.function.Consumer;
@@ -97,7 +97,11 @@ public ApiResponse runCommand(Command command, CommandOptions<?> overridingOptio
9797
// === OBSERVERS ===
9898
List<CommandObserver> observers = new ArrayList<>(commandOptions.getObservers().values());
9999
if (overridingOptions != null && overridingOptions.getObservers() != null) {
100-
observers.addAll(overridingOptions.getObservers().values());
100+
for (Map.Entry<String, CommandObserver> observer : overridingOptions.getObservers().entrySet()) {
101+
if (!commandOptions.getObservers().containsKey(observer.getKey())) {
102+
observers.add(observer.getValue());
103+
}
104+
}
101105
}
102106

103107
// === TOKEN ===

0 commit comments

Comments
 (0)