Skip to content

Commit c08ac94

Browse files
committed
Fix compiltation
1 parent 4cfe393 commit c08ac94

34 files changed

+425
-221
lines changed

src/main/java/com/datastax/astra/client/AstraDBAdmin.java

Lines changed: 84 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.datastax.astra.internal.astra.AstraApiEndpoint;
44
import com.datastax.astra.internal.astra.AstraDBDatabaseAdmin;
5+
import com.datastax.astra.internal.utils.Assert;
56
import com.dtsx.astra.sdk.db.AstraDBOpsClient;
67
import com.dtsx.astra.sdk.db.DbOpsClient;
78
import com.dtsx.astra.sdk.db.domain.CloudProviderType;
@@ -10,7 +11,6 @@
1011
import com.dtsx.astra.sdk.db.domain.DatabaseStatusType;
1112
import com.dtsx.astra.sdk.db.exception.DatabaseNotFoundException;
1213
import com.dtsx.astra.sdk.utils.ApiLocator;
13-
import com.dtsx.astra.sdk.utils.Assert;
1414
import com.dtsx.astra.sdk.utils.AstraEnvironment;
1515
import com.dtsx.astra.sdk.utils.AstraRc;
1616
import lombok.NonNull;
@@ -54,7 +54,7 @@ public class AstraDBAdmin {
5454
final AstraDBOpsClient devopsDbClient;
5555

5656
/** Options to personalized http client other client options. */
57-
final DataAPIClientOptions dataAPIClientOptions;
57+
final DataAPIOptions dataAPIOptions;
5858

5959
/** Astra Environment. */
6060
final AstraEnvironment env;
@@ -87,10 +87,13 @@ public class AstraDBAdmin {
8787
* @param options
8888
* options for client
8989
*/
90-
AstraDBAdmin(String token, AstraEnvironment env, DataAPIClientOptions options) {
90+
AstraDBAdmin(String token, AstraEnvironment env, DataAPIOptions options) {
91+
Assert.hasLength(token, "token");
92+
Assert.notNull(env, "environment");
93+
Assert.notNull(options, "options");
9194
this.token = token;
9295
this.env = env;
93-
this.dataAPIClientOptions = options;
96+
this.dataAPIOptions = options;
9497
this.devopsDbClient = new AstraDBOpsClient(token, this.env);
9598

9699
// Local Agent for Resume
@@ -114,6 +117,31 @@ public List<String> listDatabaseNames() {
114117
return listDatabases().map(db -> db.getInfo().getName()).collect(Collectors.toList());
115118
}
116119

120+
/**
121+
* Return true if the database exists.
122+
* @param name
123+
* database identifiers
124+
* @return
125+
* if the database exits or not
126+
*/
127+
public boolean databaseExists(String name) {
128+
Assert.hasLength(name, "name");
129+
return listDatabaseNames().contains(name);
130+
}
131+
132+
/**
133+
* Return true if the database exists.
134+
*
135+
* @param id
136+
* database identifiers
137+
* @return
138+
* if the database exits or not
139+
*/
140+
public boolean databaseExists(UUID id) {
141+
Assert.notNull(id, "id");
142+
return devopsDbClient.findById(id.toString()).isPresent();
143+
}
144+
117145
/**
118146
* List active databases with vector enabled in current organization.
119147
*
@@ -134,7 +162,8 @@ public Stream<Database> listDatabases() {
134162
* @return
135163
* database identifier
136164
*/
137-
public UUID createDatabase(@NonNull String name) {
165+
public UUID createDatabase(String name) {
166+
Assert.hasLength(name, "name");
138167
return createDatabase(name, FREE_TIER_CLOUD, FREE_TIER_CLOUD_REGION);
139168
}
140169

@@ -152,7 +181,10 @@ public UUID createDatabase(@NonNull String name) {
152181
* @return
153182
* database identifier
154183
*/
155-
public UUID createDatabase(@NonNull String name, @NonNull CloudProviderType cloud, @NonNull String cloudRegion) {
184+
public UUID createDatabase(String name, CloudProviderType cloud, String cloudRegion, boolean waitForDb) {
185+
Assert.hasLength(name, "name");
186+
Assert.notNull(cloud, "cloud");
187+
Assert.hasLength(cloudRegion, "cloudRegion");
156188
Optional<Database> optDb = listDatabases().filter(db->name.equals(db.getInfo().getName())).findFirst();
157189
// Handling all cases for the user
158190
if (optDb.isPresent()) {
@@ -166,12 +198,16 @@ public UUID createDatabase(@NonNull String name, @NonNull CloudProviderType clou
166198
case PENDING:
167199
case RESUMING:
168200
log.info("Database {} already exists and is in {} state, waiting for it to be ACTIVE", name, db.getStatus());
169-
waitForDatabase(devopsDbClient.database(db.getId()));
201+
if (waitForDb) {
202+
waitForDatabase(devopsDbClient.database(db.getId()));
203+
}
170204
return UUID.fromString(db.getId());
171205
case HIBERNATED:
172206
log.info("Database {} is in {} state, resuming...", name, db.getStatus());
173207
resumeDb(db);
174-
waitForDatabase(devopsDbClient.database(db.getId()));
208+
if (waitForDb) {
209+
waitForDatabase(devopsDbClient.database(db.getId()));
210+
}
175211
return UUID.fromString(db.getId());
176212
default:
177213
throw new IllegalStateException("Database already exist but cannot be activate");
@@ -185,10 +221,30 @@ public UUID createDatabase(@NonNull String name, @NonNull CloudProviderType clou
185221
.keyspace(DEFAULT_NAMESPACE)
186222
.withVector().build()));
187223
log.info("Database {} is starting (id={}): it will take about a minute please wait...", name, newDbId);
188-
waitForDatabase(devopsDbClient.database(newDbId.toString()));
224+
if (waitForDb) {
225+
waitForDatabase(devopsDbClient.database(newDbId.toString()));
226+
}
189227
return newDbId;
190228
}
191229

230+
/**
231+
* Create new database with a name on the specified cloud provider and region.
232+
* If the database with same name already exists it will be resumed if not active.
233+
* The method will wait for the database to be active.
234+
*
235+
* @param name
236+
* database name
237+
* @param cloud
238+
* cloud provider
239+
* @param cloudRegion
240+
* cloud region
241+
* @return
242+
* database identifier
243+
*/
244+
public UUID createDatabase(String name, CloudProviderType cloud, String cloudRegion) {
245+
return createDatabase(name, cloud, cloudRegion, true);
246+
}
247+
192248
/**
193249
* Delete a Database if exists from its name
194250
*
@@ -198,9 +254,22 @@ public UUID createDatabase(@NonNull String name, @NonNull CloudProviderType clou
198254
* if the db has been deleted
199255
*/
200256
public boolean dropDatabase(@NonNull UUID databaseId) {
257+
Assert.notNull(databaseId, "Database identifier");
258+
boolean exists = databaseExists(databaseId);
201259
getDatabaseInformations(databaseId);
202260
devopsDbClient.database(databaseId.toString()).delete();
203-
return true;
261+
return exists;
262+
}
263+
264+
public boolean dropDatabase(@NonNull String databaseName) {
265+
Assert.hasLength(databaseName, "database");
266+
com.datastax.astra.internal.utils.Assert.hasLength(databaseName, "Database ");
267+
Optional<Database> db = listDatabases().filter(d -> d.getInfo().getName().equals(databaseName)).findFirst();
268+
if (db.isPresent()) {
269+
devopsDbClient.database(db.get().getId()).delete();
270+
return true;
271+
}
272+
return false;
204273
}
205274

206275
/**
@@ -230,13 +299,15 @@ public Database getDatabaseInformations(@NonNull UUID id) {
230299
* database client
231300
*/
232301
public com.datastax.astra.client.Database getDatabase(UUID databaseId, String namespace) {
302+
Assert.notNull(databaseId, "databaseId");
303+
Assert.hasLength(namespace, "namespace");
233304
String databaseRegion = devopsDbClient
234305
.findById(databaseId.toString())
235306
.map(db -> db.getInfo().getRegion())
236307
.orElseThrow(() -> new DatabaseNotFoundException(databaseId.toString()));
237308
return new com.datastax.astra.client.Database(
238309
new AstraApiEndpoint(databaseId, databaseRegion, env).getApiEndPoint(),
239-
token,namespace, dataAPIClientOptions) {
310+
token,namespace, dataAPIOptions) {
240311
};
241312
}
242313

@@ -261,7 +332,8 @@ public com.datastax.astra.client.Database getDatabase(UUID databaseId) {
261332
* database client
262333
*/
263334
public AstraDBDatabaseAdmin getDatabaseAdmin(UUID databaseId) {
264-
return new AstraDBDatabaseAdmin(token, databaseId, env, dataAPIClientOptions);
335+
Assert.notNull(databaseId, "databaseId");
336+
return new AstraDBDatabaseAdmin(token, databaseId, env, dataAPIOptions);
265337
}
266338

267339
/**

src/main/java/com/datastax/astra/client/Collection.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -358,8 +358,8 @@ public InsertManyResult insertMany(List<? extends DOC> documents, InsertManyOpti
358358
if (options.getConcurrency() > 1 && options.isOrdered()) {
359359
throw new IllegalArgumentException("Cannot run ordered insert_many concurrently.");
360360
}
361-
if (options.getChunkSize() > DataAPIClientOptions.getMaxDocumentsInInsert()) {
362-
throw new IllegalArgumentException("Cannot insert more than " + DataAPIClientOptions.getMaxDocumentsInInsert() + " at a time.");
361+
if (options.getChunkSize() > DataAPIOptions.getMaxDocumentsInInsert()) {
362+
throw new IllegalArgumentException("Cannot insert more than " + DataAPIOptions.getMaxDocumentsInInsert() + " at a time.");
363363
}
364364
long start = System.currentTimeMillis();
365365
ExecutorService executor = Executors.newFixedThreadPool(options.getConcurrency());
@@ -533,6 +533,18 @@ public FindIterable<DOC> find(Filter filter) {
533533
return find(filter, new FindOptions());
534534
}
535535

536+
/**
537+
* Finds all documents in the collection.
538+
*
539+
* @param filter
540+
* the query filter
541+
* @return
542+
* the find iterable interface
543+
*/
544+
public FindIterable<DOC> find(Filter filter, float[] vector, int limit) {
545+
return find(filter, new FindOptions().sortingByVector(vector).limit(limit));
546+
}
547+
536548
/**
537549
* Finds all documents in the collection.
538550
*
@@ -560,7 +572,7 @@ public Page<DOC> findPage(Filter filter, FindOptions options) {
560572

561573
ApiResponse apiResponse = runCommand(findCommand);
562574

563-
return new Page<>(DataAPIClientOptions.getMaxPageSize(),
575+
return new Page<>(DataAPIOptions.getMaxPageSize(),
564576
apiResponse.getData().getNextPageState(),
565577
apiResponse.getData().getDocuments()
566578
.stream()
@@ -665,8 +677,8 @@ public int countDocuments(int upperBound) throws TooManyDocumentsToCountExceptio
665677
*/
666678
public int countDocuments(Filter filter, int upperBound) throws TooManyDocumentsToCountException {
667679
// Argument Validation
668-
if (upperBound<1 || upperBound> DataAPIClientOptions.getMaxDocumentCount()) {
669-
throw new IllegalArgumentException("UpperBound limit should be in between 1 and " + DataAPIClientOptions.getMaxDocumentCount());
680+
if (upperBound<1 || upperBound> DataAPIOptions.getMaxDocumentCount()) {
681+
throw new IllegalArgumentException("UpperBound limit should be in between 1 and " + DataAPIOptions.getMaxDocumentCount());
670682
}
671683
// Build command
672684
Command command = new Command("countDocuments").withFilter(filter);

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ public class DataAPIClient {
1515
private final String token;
1616

1717
/** Options to setup the client. */
18-
private final DataAPIClientOptions options;
18+
private final DataAPIOptions options;
1919

2020
public DataAPIClient(String token) {
21-
this(token, DataAPIClientOptions.builder().build());
21+
this(token, DataAPIOptions.builder().build());
2222
}
2323

24-
public DataAPIClient(String token, DataAPIClientOptions options) {
24+
public DataAPIClient(String token, DataAPIOptions options) {
2525
Assert.hasLength(token, "token");
2626
Assert.notNull(options, "options");
2727
this.token = token;

src/main/java/com/datastax/astra/client/DataAPIClientOptions.java renamed to src/main/java/com/datastax/astra/client/DataAPIOptions.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* Options to set up the client for DataApiClient.
1111
*/
1212
@Getter
13-
public class DataAPIClientOptions {
13+
public class DataAPIOptions {
1414

1515
/** Number of documents for a count. */
1616
static final int MAX_DOCUMENTS_COUNT = 1000;
@@ -26,8 +26,8 @@ public class DataAPIClientOptions {
2626

2727
/** Default user agent. */
2828
public static final String DEFAULT_CALLER_VERSION =
29-
DataAPIClientOptions.class.getPackage().getImplementationVersion() != null ?
30-
DataAPIClientOptions.class.getPackage().getImplementationVersion() : "dev";
29+
DataAPIOptions.class.getPackage().getImplementationVersion() != null ?
30+
DataAPIOptions.class.getPackage().getImplementationVersion() : "dev";
3131

3232
/** Default timeout for initiating connection. */
3333
public static final int DEFAULT_CONNECT_TIMEOUT_SECONDS = 20;
@@ -54,7 +54,7 @@ public static DataAPIClientOptionsBuilder builder() {
5454
return new DataAPIClientOptionsBuilder();
5555
}
5656

57-
private DataAPIClientOptions(DataAPIClientOptionsBuilder builder) {
57+
private DataAPIOptions(DataAPIClientOptionsBuilder builder) {
5858
this.apiVersion = builder.apiVersion;
5959
this.destination = builder.destination;
6060
this.httpClientOptions = HttpClientOptions.builder()
@@ -188,8 +188,8 @@ public DataAPIClientOptionsBuilder withHttpRetryDelayMillis(int retryDelay) {
188188
return this;
189189
}
190190

191-
public DataAPIClientOptions build() {
192-
return new DataAPIClientOptions(this);
191+
public DataAPIOptions build() {
192+
return new DataAPIOptions(this);
193193
}
194194

195195

src/main/java/com/datastax/astra/client/DataApiClients.java

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,6 @@
11
package com.datastax.astra.client;
22

3-
import com.datastax.astra.internal.DataAPIDatabaseAdmin;
4-
import com.datastax.astra.internal.auth.FixedTokenAuthenticationService;
53
import com.datastax.astra.internal.auth.StargateAuthenticationService;
6-
import com.datastax.astra.internal.auth.TokenProvider;
7-
import com.datastax.astra.internal.http.HttpClientOptions;
8-
9-
import com.datastax.astra.internal.utils.Assert;
10-
import lombok.NonNull;
11-
12-
import java.util.Collections;
134

145
/**
156
* Initialization of the client in a Static way.
@@ -39,25 +30,25 @@ private DataApiClients() {}
3930
public static DataAPIClient localStargate() {
4031
return new DataAPIClient(
4132
new StargateAuthenticationService().getToken(),
42-
DataAPIClientOptions.builder().withDestination(DataAPIDestination.CASSANDRA).build());
33+
DataAPIOptions.builder().withDestination(DataAPIDestination.CASSANDRA).build());
4334
}
4435

4536
public static DataAPIClient astra(String token) {
46-
return new DataAPIClient(token, DataAPIClientOptions
37+
return new DataAPIClient(token, DataAPIOptions
4738
.builder()
4839
.withDestination(DataAPIDestination.ASTRA)
4940
.build());
5041
}
5142

5243
public static DataAPIClient astraDev(String token) {
53-
return new DataAPIClient(token, DataAPIClientOptions
44+
return new DataAPIClient(token, DataAPIOptions
5445
.builder()
5546
.withDestination(DataAPIDestination.ASTRA_DEV)
5647
.build());
5748
}
5849

5950
public static DataAPIClient astraTest(String token) {
60-
return new DataAPIClient(token, DataAPIClientOptions
51+
return new DataAPIClient(token, DataAPIOptions
6152
.builder()
6253
.withDestination(DataAPIDestination.ASTRA_TEST)
6354
.build());

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public class Database extends AbstractCommandRunner {
4949

5050
/** Options to setup the client. */
5151
@Getter
52-
private final DataAPIClientOptions options;
52+
private final DataAPIOptions options;
5353

5454
/**
5555
* Initialization with endpoint and apikey.
@@ -60,7 +60,7 @@ public class Database extends AbstractCommandRunner {
6060
* api endpoint
6161
*/
6262
public Database(String apiEndpoint, String token) {
63-
this(apiEndpoint, token, AstraDBAdmin.DEFAULT_NAMESPACE, DataAPIClientOptions.builder().build());
63+
this(apiEndpoint, token, AstraDBAdmin.DEFAULT_NAMESPACE, DataAPIOptions.builder().build());
6464
}
6565

6666
/**
@@ -74,7 +74,7 @@ public Database(String apiEndpoint, String token) {
7474
* namespace
7575
*/
7676
public Database(String apiEndpoint, String token, String namespace) {
77-
this(apiEndpoint, token, namespace, DataAPIClientOptions.builder().build());
77+
this(apiEndpoint, token, namespace, DataAPIOptions.builder().build());
7878
}
7979

8080
/**
@@ -89,7 +89,7 @@ public Database(String apiEndpoint, String token, String namespace) {
8989
* @param options
9090
* setup of the clients with options
9191
*/
92-
public Database(String apiEndpoint, String token, String namespace, DataAPIClientOptions options) {
92+
public Database(String apiEndpoint, String token, String namespace, DataAPIOptions options) {
9393
hasLength(apiEndpoint, "endpoint");
9494
hasLength(token, "token");
9595
hasLength(namespace, "namespace");

src/main/java/com/datastax/astra/client/DatabaseAdmin.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.datastax.astra.client;
22

3-
import com.datastax.astra.client.model.namespaces.CreateNamespaceOptions;
3+
import com.datastax.astra.client.model.CommandRunner;
44

55
import java.util.concurrent.CompletableFuture;
66
import java.util.stream.Stream;

0 commit comments

Comments
 (0)