Skip to content

Commit 60da92b

Browse files
committed
All Collections tests passing
1 parent b5f94eb commit 60da92b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+930
-520
lines changed

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

Lines changed: 12 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,14 @@
2525
import com.datastax.astra.client.admin.DatabaseAdmin;
2626
import com.datastax.astra.client.core.options.DataAPIOptions;
2727
import com.datastax.astra.client.databases.Database;
28+
import com.datastax.astra.client.exception.InvalidEnvironmentException;
2829
import com.datastax.astra.internal.api.AstraApiEndpoint;
2930
import com.datastax.astra.internal.utils.Assert;
30-
import com.dtsx.astra.sdk.utils.AstraEnvironment;
3131

32-
import java.util.Optional;
3332
import java.util.UUID;
3433

35-
import static com.datastax.astra.client.DataAPIDestination.ASTRA;
36-
import static com.datastax.astra.client.DataAPIDestination.ASTRA_DEV;
37-
import static com.datastax.astra.client.DataAPIDestination.ASTRA_TEST;
3834
import static com.datastax.astra.client.admin.AstraDBAdmin.DEFAULT_KEYSPACE;
35+
import static com.datastax.astra.client.exception.InvalidEnvironmentException.throwErrorRestrictedAstra;
3936

4037
/**
4138
* Serves as the primary entry point to the Data API client, offering streamlined access to the functionalities
@@ -256,42 +253,10 @@ public AstraDBAdmin getAdmin() {
256253
* @throws SecurityException if the provided {@code superUserToken} lacks the necessary privileges for administrative operations.
257254
*/
258255
public AstraDBAdmin getAdmin(String superUserToken) {
259-
return new AstraDBAdmin(superUserToken, getAstraEnvironment(), options);
260-
}
261-
262-
/**
263-
* Find the Astra Environment from the destination provided in the initial Optional. It will help
264-
* shaping the Api endpoint to spawn sub database objects.
265-
*
266-
* @return
267-
* astra environment if found
268-
*/
269-
private Optional<AstraEnvironment> findAstraEnvironment() {
270-
if (options.getDestination() != null) {
271-
switch (options.getDestination()) {
272-
case ASTRA:
273-
return Optional.of(AstraEnvironment.PROD);
274-
case ASTRA_DEV:
275-
return Optional.of(AstraEnvironment.DEV);
276-
case ASTRA_TEST:
277-
return Optional.of(AstraEnvironment.TEST);
278-
}
256+
if (!options.isAstra()) {
257+
throwErrorRestrictedAstra("getAdmin()", options.getDestination());
279258
}
280-
return Optional.empty();
281-
}
282-
283-
/**
284-
* Access the Astra environment or throw an error. Some operation like getAdmin would anly work if the options
285-
* are set to Astra. If this is not the case a {@link IllegalArgumentException is thrown.}
286-
*
287-
* @return
288-
* current astra environment
289-
* @throws IllegalArgumentException
290-
* the destination was not set for Astra in initial options.
291-
*/
292-
private AstraEnvironment getAstraEnvironment() {
293-
return findAstraEnvironment()
294-
.orElseThrow(() -> new IllegalArgumentException("'destination' should be ASTRA* to use the AstraDBAdmin"));
259+
return new AstraDBAdmin(superUserToken, options);
295260
}
296261

297262
// --------------------------------------------------
@@ -331,16 +296,12 @@ private AstraEnvironment getAstraEnvironment() {
331296
public Database getDatabase(UUID databaseId, String keyspace) {
332297
Assert.notNull(databaseId, ARG_DATABASE_ID);
333298
Assert.hasLength(keyspace, ARG_KEYSPACE);
334-
if (options.getDestination() != ASTRA &&
335-
options.getDestination() != ASTRA_DEV &&
336-
options.getDestination() != ASTRA_TEST) {
337-
throw new IllegalArgumentException("DataAPIOptions.destination " +
338-
"should be set to one of ASTRA* " +
339-
"to retrieve a database from an id.");
299+
if (!options.isAstra()) {
300+
throwErrorRestrictedAstra("getDatabase(id,keyspace)", options.getDestination());
340301
}
341302
return new Database(new AstraApiEndpoint(databaseId,
342303
getAdmin().getDatabaseInfo(databaseId).getRegion(),
343-
getAstraEnvironment()).getApiEndPoint(),
304+
options.getAstraEnvironment()).getApiEndPoint(),
344305
this.token, keyspace, options);
345306
}
346307

@@ -360,8 +321,11 @@ public Database getDatabase(UUID databaseId, String keyspace, String region) {
360321
Assert.notNull(databaseId, ARG_DATABASE_ID);
361322
Assert.hasLength(keyspace, ARG_KEYSPACE);
362323
Assert.hasLength(region, ARG_REGION);
324+
if (!options.isAstra()) {
325+
throwErrorRestrictedAstra("getDatabase(dbId,keyspace,region)", options.getDestination());
326+
}
363327
return new Database(new AstraApiEndpoint(databaseId, region,
364-
getAstraEnvironment()).getApiEndPoint(),
328+
options.getAstraEnvironment()).getApiEndPoint(),
365329
this.token, keyspace, options);
366330
}
367331

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

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import com.datastax.astra.client.core.options.DataAPIOptions;
2424
import com.datastax.astra.client.databases.DatabaseInfo;
25+
import com.datastax.astra.client.exception.InvalidEnvironmentException;
2526
import com.datastax.astra.internal.api.AstraApiEndpoint;
2627
import com.datastax.astra.internal.command.LoggingCommandObserver;
2728
import com.datastax.astra.internal.utils.Assert;
@@ -40,13 +41,15 @@
4041
import lombok.extern.slf4j.Slf4j;
4142

4243
import java.net.http.HttpClient;
44+
import java.time.Duration;
4345
import java.util.HashMap;
4446
import java.util.List;
4547
import java.util.Map;
4648
import java.util.Optional;
4749
import java.util.UUID;
4850
import java.util.stream.Collectors;
4951

52+
import static com.datastax.astra.client.exception.InvalidEnvironmentException.throwErrorRestrictedAstra;
5053
import static com.datastax.astra.internal.utils.AnsiUtils.green;
5154
import static com.dtsx.astra.sdk.utils.Utils.readEnvVariable;
5255

@@ -77,9 +80,6 @@ public class AstraDBAdmin {
7780
/** Options to personalized http client other client options. */
7881
final DataAPIOptions dataAPIOptions;
7982

80-
/** Astra Environment. */
81-
final AstraEnvironment env;
82-
8383
/** Astra Token (credentials). */
8484
final String token;
8585

@@ -107,30 +107,26 @@ public class AstraDBAdmin {
107107
* authentication token
108108
* @param options
109109
* options for client
110-
* @param env
111-
* astra environment
112110
*/
113-
public AstraDBAdmin(String token, AstraEnvironment env, DataAPIOptions options) {
111+
public AstraDBAdmin(String token, DataAPIOptions options) {
114112
Assert.hasLength(token, "token");
115-
Assert.notNull(env, "environment");
116113
Assert.notNull(options, "options");
117114
this.token = token;
118-
this.env = env;
119115
this.dataAPIOptions = options;
120116
if (options.getObservers() != null) {
121117
Map<String, ApiRequestObserver> devopsObservers = new HashMap<>();
122118
if (options.getObservers().containsKey(LoggingCommandObserver.class.getSimpleName())) {
123119
devopsObservers.put("logging", new LoggingRequestObserver(AstraDBAdmin.class));
124120
}
125-
this.devopsDbClient = new AstraDBOpsClient(token, this.env, devopsObservers);
121+
this.devopsDbClient = new AstraDBOpsClient(token, options.getAstraEnvironment(), devopsObservers);
126122
} else {
127-
this.devopsDbClient = new AstraDBOpsClient(token, this.env);
123+
this.devopsDbClient = new AstraDBOpsClient(token, options.getAstraEnvironment());
128124
}
129125

130126
// Local Agent for Resume
131127
HttpClient.Builder httpClientBuilder = HttpClient.newBuilder();
132128
httpClientBuilder.version(options.getHttpClientOptions().getHttpVersion());
133-
httpClientBuilder.connectTimeout(options.getHttpClientOptions().getConnectionTimeout());
129+
httpClientBuilder.connectTimeout(Duration.ofMillis(options.getTimeoutOptions().connectTimeoutMillis()));
134130
this.httpClient = httpClientBuilder.build();
135131
}
136132

@@ -341,13 +337,16 @@ public DatabaseInfo getDatabaseInfo(@NonNull UUID id) {
341337
public com.datastax.astra.client.databases.Database getDatabase(UUID databaseId, String keyspace) {
342338
Assert.notNull(databaseId, "databaseId");
343339
Assert.hasLength(keyspace, "keyspace");
340+
if (!dataAPIOptions.isAstra()) {
341+
throwErrorRestrictedAstra("getDatabase(id, keyspace)", dataAPIOptions.getDestination());
342+
}
344343
String databaseRegion = devopsDbClient
345344
.findById(databaseId.toString())
346345
.map(db -> db.getInfo().getRegion())
347346
.orElseThrow(() -> new DatabaseNotFoundException(databaseId.toString()));
348347
return new com.datastax.astra.client.databases.Database(
349-
new AstraApiEndpoint(databaseId, databaseRegion, env).getApiEndPoint(),
350-
token,keyspace, dataAPIOptions) {
348+
new AstraApiEndpoint(databaseId, databaseRegion, dataAPIOptions.getAstraEnvironment()).getApiEndPoint(),
349+
token, keyspace, dataAPIOptions) {
351350
};
352351
}
353352

@@ -373,7 +372,7 @@ public com.datastax.astra.client.databases.Database getDatabase(UUID databaseId)
373372
*/
374373
public AstraDBDatabaseAdmin getDatabaseAdmin(UUID databaseId) {
375374
Assert.notNull(databaseId, "databaseId");
376-
return new AstraDBDatabaseAdmin(token, databaseId, env, dataAPIOptions);
375+
return new AstraDBDatabaseAdmin(token, databaseId, dataAPIOptions);
377376
}
378377

379378
/**

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

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
*/
2222

2323
import com.datastax.astra.client.DataAPIDestination;
24+
import com.datastax.astra.client.core.commands.CommandOptions;
2425
import com.datastax.astra.client.core.options.DataAPIOptions;
2526
import com.datastax.astra.client.core.results.FindEmbeddingProvidersResult;
2627
import com.datastax.astra.internal.api.AstraApiEndpoint;
@@ -48,8 +49,8 @@ public class AstraDBDatabaseAdmin implements DatabaseAdmin {
4849
/** Token used for the credentials. */
4950
final UUID databaseId;
5051

51-
/** Working environment. */
52-
final AstraEnvironment env;
52+
/** Data Api Options. */
53+
final DataAPIOptions dataAPIOptions;
5354

5455
/** Client for Astra Devops Api. */
5556
final AstraDBOpsClient devopsDbClient;
@@ -64,11 +65,11 @@ public class AstraDBDatabaseAdmin implements DatabaseAdmin {
6465
* target database
6566
*/
6667
public AstraDBDatabaseAdmin(com.datastax.astra.client.databases.Database db) {
67-
this.databaseId = UUID.fromString(db.getDbApiEndpoint().substring(8, 44));
68-
this.env = getEnvironment(db.getOptions().getDestination());
69-
this.token = db.getToken();
70-
this.db = db;
71-
this.devopsDbClient = new AstraDBOpsClient(token, this.env);
68+
this.databaseId = UUID.fromString(db.getDbApiEndpoint().substring(8, 44));
69+
this.dataAPIOptions = db.getOptions();
70+
this.token = db.getToken();
71+
this.db = db;
72+
this.devopsDbClient = new AstraDBOpsClient(token, dataAPIOptions.getAstraEnvironment());
7273
}
7374

7475
/**
@@ -78,16 +79,14 @@ public AstraDBDatabaseAdmin(com.datastax.astra.client.databases.Database db) {
7879
* token value
7980
* @param databaseId
8081
* database identifier
81-
* @param env
82-
* working environment
8382
* @param options
8483
* options used to initialize the http client
8584
*/
86-
public AstraDBDatabaseAdmin(String token, UUID databaseId, AstraEnvironment env, DataAPIOptions options) {
87-
this.env = env;
85+
public AstraDBDatabaseAdmin(String token, UUID databaseId, DataAPIOptions options) {
8886
this.token = token;
8987
this.databaseId = databaseId;
90-
this.devopsDbClient = new AstraDBOpsClient(token, this.env);
88+
this.dataAPIOptions = options;
89+
this.devopsDbClient = new AstraDBOpsClient(token, options.getAstraEnvironment());
9190
this.db = new com.datastax.astra.client.databases.Database(getApiEndpoint(), token, DEFAULT_KEYSPACE, options);
9291
}
9392

@@ -137,7 +136,7 @@ private static AstraEnvironment getEnvironment(DataAPIDestination destination) {
137136
*/
138137
private String getApiEndpoint() {
139138
return new AstraApiEndpoint(databaseId,
140-
getDatabaseInformations().getInfo().getRegion(), env)
139+
getDatabaseInformations().getInfo().getRegion(), dataAPIOptions.getAstraEnvironment())
141140
.getApiEndPoint();
142141
}
143142

@@ -199,6 +198,12 @@ public void dropKeyspace(String keyspace) {
199198
}
200199
}
201200

201+
@Override
202+
public void dropKeyspace(String namespace, CommandOptions<?> options) {
203+
log.warn("CommandOptions are not supported for dropKeyspace in Astra MODE");
204+
dropKeyspace(namespace);
205+
}
206+
202207
/** {@inheritDoc} */
203208
@Override
204209
public com.datastax.astra.client.databases.Database getDatabase() {

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

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
* #L%
2121
*/
2222

23+
import com.datastax.astra.client.core.commands.CommandType;
2324
import com.datastax.astra.client.core.options.DataAPIOptions;
2425
import com.datastax.astra.client.databases.Database;
2526
import com.datastax.astra.client.core.commands.Command;
@@ -82,12 +83,9 @@ public DataAPIDatabaseAdmin(String apiEndpoint, String token, DataAPIOptions opt
8283
*/
8384
public DataAPIDatabaseAdmin(Database db) {
8485
this.db = db;
85-
86-
this.commandOptions = new CommandOptions<>()
87-
.token(db.getToken())
88-
.embeddingAuthProvider(db.getOptions().getEmbeddingAuthProvider())
89-
.httpClientOptions(db.getOptions().getHttpClientOptions());
90-
db.getOptions().getObservers().forEach(this.commandOptions::registerObserver);
86+
this.commandOptions = new CommandOptions<>(db.getOptions());
87+
this.commandOptions.token(db.getToken());
88+
this.commandOptions.commandType(CommandType.KEYSPACE_ADMIN);
9189
}
9290

9391
// ------------------------------------------
@@ -163,12 +161,12 @@ public void createKeyspace(String keyspace, KeyspaceOptions options) {
163161
}
164162

165163
@Override
166-
public void dropKeyspace(String keyspace) {
164+
public void dropKeyspace(String keyspace, CommandOptions<?> options) {
167165
hasLength(keyspace, ARG_KEYSPACE);
168166
Command dropNamespace = Command
169167
.create("dropKeyspace")
170168
.append("name", keyspace);
171-
runCommand(dropNamespace);
169+
runCommand(dropNamespace, options);
172170
log.info("Keyspace '" + green("{}") + "' has been deleted", keyspace);
173171
}
174172

@@ -184,8 +182,6 @@ protected DataAPISerializer getSerializer() {
184182
return SERIALIZER;
185183
}
186184

187-
188-
189185
/**
190186
* Register a listener to execute commands on the collection. Please now use {@link CommandOptions}.
191187
*

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
* #L%
2121
*/
2222

23+
import com.datastax.astra.client.core.commands.CommandOptions;
2324
import com.datastax.astra.client.databases.Database;
2425
import com.datastax.astra.client.core.commands.CommandRunner;
2526
import com.datastax.astra.client.core.vectorize.EmbeddingProvider;
@@ -202,7 +203,11 @@ default CompletableFuture<Set<String>> listKeyspacesNamesAsync() {
202203
* that should be deleted. The operation will proceed silently and without error even if the
203204
* keyspace does not exist, ensuring consistent behavior.
204205
*/
205-
void dropKeyspace(String namespace);
206+
default void dropKeyspace(String namespace) {
207+
dropKeyspace(namespace, null);
208+
}
209+
210+
void dropKeyspace(String namespace, CommandOptions<?> options);
206211

207212
/**
208213
* Asynchronously drops (deletes) the specified keyspace from the database. This operation is idempotent, meaning

0 commit comments

Comments
 (0)