Skip to content

Commit 41f8cd9

Browse files
committed
update createKeyspace
1 parent 4946ad5 commit 41f8cd9

File tree

21 files changed

+421
-114
lines changed

21 files changed

+421
-114
lines changed

astra-db-java-tools/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<parent>
88
<groupId>com.datastax.astra</groupId>
99
<artifactId>astra-db-java-parent</artifactId>
10-
<version>2.0.0-PREVIEW1-SNAPSHOT</version>
10+
<version>2.0.0-PREVIEW2</version>
1111
</parent>
1212

1313
<dependencies>

astra-db-java-tools/src/main/java/com/datastax/astra/tool/loader/rag/RagRepository.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@
77
import com.datastax.astra.client.core.vectorize.VectorServiceOptions;
88
import com.datastax.astra.client.databases.Database;
99
import com.datastax.astra.client.databases.DatabaseOptions;
10+
import com.datastax.astra.client.databases.commands.options.CreateKeyspaceOptions;
1011
import com.datastax.astra.client.databases.definition.DatabaseInfo;
12+
import com.datastax.astra.client.databases.definition.keyspaces.KeyspaceDefinition;
1113
import com.datastax.astra.client.tables.Table;
1214
import com.datastax.astra.client.tables.commands.options.CreateTableOptions;
1315
import com.datastax.astra.client.tables.commands.options.CreateVectorIndexOptions;
1416
import com.datastax.astra.client.tables.definition.rows.Row;
15-
import com.datastax.astra.internal.utils.Utils;
1617
import com.datastax.astra.tool.loader.rag.ingestion.RagEmbeddingsModels;
1718
import com.datastax.astra.tool.loader.rag.ingestion.RagIngestionConfig;
1819
import com.datastax.astra.tool.loader.rag.ingestion.RagIngestionJob;
@@ -24,6 +25,8 @@
2425
import java.util.Optional;
2526
import java.util.UUID;
2627

28+
import static com.datastax.astra.internal.utils.Assert.notNull;
29+
2730
@Slf4j
2831
public class RagRepository {
2932

@@ -53,7 +56,9 @@ public Database getOrCreateDatabase(UUID tenantId) {
5356
log.info("Database {} does not exists and will be created.", tenantId.toString());
5457
DatabaseAdmin dbAdmin = astraDBAdmin
5558
.createDatabase(tenantId.toString(), cloudProvider, cloudRegion);
56-
dbAdmin.createKeyspace(keyspace, true);
59+
dbAdmin.createKeyspace(
60+
new KeyspaceDefinition().name(keyspace),
61+
new CreateKeyspaceOptions().updateDBKeyspace(true));
5762
return dbAdmin.getDatabase(keyspace);
5863
}
5964
log.info("Database {} already exists.", tenantId);
@@ -128,8 +133,7 @@ public Table<RagStore> getTableRagStore(UUID tenantId, RagIngestionConfig config
128133
}
129134

130135
public Table<RagStore> getTableRagStore(Database db, String provider, String model, int dimension, VectorServiceOptions options) {
131-
Utils.hasLength(provider);
132-
Utils.hasLength(model);
136+
notNull(provider, "provider");
133137
String tableName = RagStore.getTableName(provider, model);
134138
db.useKeyspace(keyspace);
135139

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public class AdminOptions extends BaseOptions<AdminOptions> {
5858
* Serializer for the collections.
5959
* Defaults to {@link DatabaseSerializer}.
6060
*/
61-
private static final DataAPISerializer DEFAULT_SERIALIZER = new DatabaseSerializer();
61+
public static final DataAPISerializer DEFAULT_SERIALIZER = new DatabaseSerializer();
6262

6363
/**
6464
* Serializer for the collections.

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

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,17 @@
2323
import com.datastax.astra.client.DataAPIDestination;
2424
import com.datastax.astra.client.core.options.BaseOptions;
2525
import com.datastax.astra.client.core.options.DataAPIClientOptions;
26+
import com.datastax.astra.client.databases.commands.options.CreateKeyspaceOptions;
27+
import com.datastax.astra.client.databases.commands.options.DropKeyspaceOptions;
2628
import com.datastax.astra.client.databases.commands.results.FindEmbeddingProvidersResult;
2729
import com.datastax.astra.client.databases.DatabaseOptions;
2830
import com.datastax.astra.client.databases.commands.results.FindRerankingProvidersResult;
31+
import com.datastax.astra.client.databases.definition.keyspaces.KeyspaceDefinition;
2932
import com.datastax.astra.internal.api.AstraApiEndpoint;
3033
import com.datastax.astra.internal.command.AbstractCommandRunner;
3134
import com.datastax.astra.internal.utils.Assert;
3235
import com.dtsx.astra.sdk.db.AstraDBOpsClient;
36+
import com.dtsx.astra.sdk.db.DbKeyspacesClient;
3337
import com.dtsx.astra.sdk.db.domain.Database;
3438
import com.dtsx.astra.sdk.db.exception.DatabaseNotFoundException;
3539
import com.dtsx.astra.sdk.utils.AstraEnvironment;
@@ -203,26 +207,36 @@ public FindRerankingProvidersResult findRerankingProviders() {
203207

204208
/** {@inheritDoc} */
205209
@Override
206-
public void createKeyspace(String keyspace, boolean updateDBKeyspace) {
207-
log.debug("createKeyspace");
208-
devopsDbClient.database(databaseId.toString()).keyspaces().create(keyspace);
210+
public void createKeyspace(String keyspace) {
211+
createKeyspace(
212+
new KeyspaceDefinition().name(keyspace),
213+
new CreateKeyspaceOptions().ifNotExists(true));
214+
}
215+
216+
@Override
217+
public void createKeyspace(KeyspaceDefinition keyspace, CreateKeyspaceOptions options) {
218+
String keyspaceName = keyspace.getName();
219+
DbKeyspacesClient ks = devopsDbClient.database(databaseId.toString()).keyspaces();
220+
if (!ks.exist(keyspaceName) && options.isIfNotExists()) {
221+
ks.create(keyspaceName);
222+
}
209223
}
210224

211225
/** {@inheritDoc} */
212226
@Override
213227
public void dropKeyspace(String keyspace) {
214228
log.debug("dropKeyspace");
215-
try {
216-
devopsDbClient.database(databaseId.toString()).keyspaces().delete(keyspace);
217-
} catch(NullPointerException e) {
218-
// Left blank to parse output from a delete
219-
}
229+
dropKeyspace(keyspace, new DropKeyspaceOptions().ifExists(true));
220230
}
221231

232+
/** {@inheritDoc} */
222233
@Override
223-
public void dropKeyspace(String keyspace, BaseOptions<?> options) {
234+
public void dropKeyspace(String keyspace, DropKeyspaceOptions options) {
224235
log.warn("CommandOptions are not supported for dropKeyspace in Astra MODE");
225-
dropKeyspace(keyspace);
236+
DbKeyspacesClient ks = devopsDbClient.database(databaseId.toString()).keyspaces();
237+
if (ks.exist(keyspace) && options.isIfExists()) {
238+
ks.delete(keyspace);
239+
}
226240
}
227241

228242
/** {@inheritDoc} */

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

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

23-
import com.datastax.astra.client.core.options.BaseOptions;
2423
import com.datastax.astra.client.core.commands.Command;
2524
import com.datastax.astra.client.core.commands.CommandType;
2625
import com.datastax.astra.client.core.rerank.RerankProvider;
27-
import com.datastax.astra.client.databases.commands.results.FindEmbeddingProvidersResult;
2826
import com.datastax.astra.client.core.vectorize.EmbeddingProvider;
2927
import com.datastax.astra.client.databases.Database;
28+
import com.datastax.astra.client.databases.commands.options.CreateKeyspaceOptions;
29+
import com.datastax.astra.client.databases.commands.options.DropKeyspaceOptions;
30+
import com.datastax.astra.client.databases.commands.results.FindEmbeddingProvidersResult;
3031
import com.datastax.astra.client.databases.commands.results.FindRerankingProvidersResult;
32+
import com.datastax.astra.client.databases.definition.keyspaces.KeyspaceDefinition;
3133
import com.datastax.astra.client.databases.definition.keyspaces.KeyspaceOptions;
3234
import com.datastax.astra.internal.api.DataAPIResponse;
3335
import com.datastax.astra.internal.command.AbstractCommandRunner;
3436
import com.datastax.astra.internal.serdes.DataAPISerializer;
3537
import com.datastax.astra.internal.serdes.collections.DocumentSerializer;
36-
import com.datastax.astra.internal.utils.Assert;
3738
import lombok.Getter;
3839
import lombok.extern.slf4j.Slf4j;
3940

@@ -139,14 +140,23 @@ public Database getDatabase(String keyspace, String userToken) {
139140
.keyspace(keyspace));
140141
}
141142

142-
/** {@inheritDoc} */
143143
@Override
144-
public void createKeyspace(String keyspace, boolean updateDBKeyspace) {
145-
Assert.hasLength(keyspace, ARG_KEYSPACE);
146-
createKeyspace(keyspace, KeyspaceOptions.simpleStrategy(1));
147-
if (updateDBKeyspace) {
148-
db.useKeyspace(keyspace);
144+
public void createKeyspace(KeyspaceDefinition keyspace, CreateKeyspaceOptions options) {
145+
notNull(keyspace, ARG_KEYSPACE);
146+
hasLength(keyspace.getName(), ARG_KEYSPACE);
147+
Command createKeyspace = Command
148+
.create("createKeyspace")
149+
.append("name", keyspace.getName());
150+
if (keyspace.getReplication() != null) {
151+
KeyspaceOptions keyspaceOptions = new KeyspaceOptions();
152+
keyspaceOptions.setReplication(keyspace.getReplication());
153+
createKeyspace.withOptions(keyspaceOptions);
154+
}
155+
runCommand(createKeyspace);
156+
if (options != null && options.isUpdateDBKeyspace()) {
157+
db.useKeyspace(keyspace.getName());
149158
}
159+
log.info("Keyspace '" + green("{}") + "' has been created", keyspace.getName());
150160
}
151161

152162
/**
@@ -169,7 +179,7 @@ public void createKeyspace(String keyspace, KeyspaceOptions options) {
169179
}
170180

171181
@Override
172-
public void dropKeyspace(String keyspace, BaseOptions<?> options) {
182+
public void dropKeyspace(String keyspace, DropKeyspaceOptions options) {
173183
hasLength(keyspace, ARG_KEYSPACE);
174184
Command dropNamespace = Command
175185
.create("dropKeyspace")

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

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,12 @@
2525
import com.datastax.astra.client.databases.Database;
2626
import com.datastax.astra.client.core.commands.CommandRunner;
2727
import com.datastax.astra.client.core.vectorize.EmbeddingProvider;
28+
import com.datastax.astra.client.databases.commands.options.CreateKeyspaceOptions;
29+
import com.datastax.astra.client.databases.commands.options.DropKeyspaceOptions;
2830
import com.datastax.astra.client.databases.commands.results.FindEmbeddingProvidersResult;
2931
import com.datastax.astra.client.databases.commands.results.FindRerankingProvidersResult;
32+
import com.datastax.astra.client.databases.definition.keyspaces.KeyspaceDefinition;
33+
import com.datastax.astra.client.databases.definition.keyspaces.KeyspaceInformation;
3034
import com.datastax.astra.internal.utils.Assert;
3135

3236
import java.util.Set;
@@ -240,7 +244,28 @@ default void dropKeyspace(String keyspace) {
240244
* keyspace does not exist, ensuring consistent behavior.
241245
* @param options The options to use for the operation.
242246
*/
243-
void dropKeyspace(String keyspace, BaseOptions<?> options);
247+
void dropKeyspace(String keyspace, DropKeyspaceOptions options);
248+
249+
/**
250+
* Asynchronously drops (deletes) the specified keyspace from the database. This operation is idempotent, meaning
251+
* it will not produce an error if the keyspace does not exist. Performing this operation asynchronously ensures
252+
* that the calling thread remains responsive, and can be particularly useful for applications that require high
253+
* availability and cannot afford to block on potentially long-running operations. Just like its synchronous counterpart,
254+
* this method should be used with caution as dropping a keyspace will remove all associated data, collections,
255+
* or tables, and this action is irreversible.
256+
*
257+
* This example illustrates the non-blocking nature of dropping a keyspace. It demonstrates the method's utility in
258+
* maintaining application responsiveness, even when performing potentially long-running database operations.
259+
*
260+
* @param keyspace The name of the keyspace to be dropped. This is the target keyspace that will be deleted.
261+
* The asynchronous nature of this method means that it will execute without blocking the calling
262+
* thread, regardless of whether the keyspace exists or not, ensuring a consistent and responsive
263+
* application behavior.
264+
* @param options The options to use for the operation to drop the keyspace like a timeout or if exists.
265+
*/
266+
default void dropKeyspaceAsync(String keyspace, DropKeyspaceOptions options) {
267+
CompletableFuture.runAsync(() -> dropKeyspace(keyspace, options));
268+
}
244269

245270
/**
246271
* Asynchronously drops (deletes) the specified keyspace from the database. This operation is idempotent, meaning
@@ -263,25 +288,35 @@ default void dropKeyspaceAsync(String keyspace) {
263288
}
264289

265290
/**
266-
* Create a Keyspace providing a name.
291+
* Syntax Sugar, retro compatible.
267292
*
268293
* @param keyspace
269-
* current keyspace.
270-
* @param updateDBKeyspace
271-
* if the keyspace should be updated in the database.
272-
*/
273-
void createKeyspace(String keyspace, boolean updateDBKeyspace);
294+
* current namespace.
295+
**/
296+
default void createKeyspace(String keyspace) {
297+
createKeyspace(new KeyspaceDefinition().name(keyspace), new CreateKeyspaceOptions());
298+
}
274299

275300
/**
276301
* Syntax Sugar, retro compatible.
277302
*
278303
* @param keyspace
279304
* current namespace.
280305
**/
281-
default void createKeyspace(String keyspace) {
282-
createKeyspace(keyspace, false);
306+
default void createKeyspace(KeyspaceDefinition keyspace) {
307+
createKeyspace(keyspace, new CreateKeyspaceOptions());
283308
}
284309

310+
/**
311+
* Create a Keyspace providing a name.
312+
*
313+
* @param keyspace
314+
* keyspace definition
315+
* @param options
316+
* options to create the keyspace
317+
*/
318+
void createKeyspace(KeyspaceDefinition keyspace, CreateKeyspaceOptions options);
319+
285320
/**
286321
* Create a keyspace providing a name.
287322
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.datastax.astra.client.databases.commands.options;
2+
3+
/*-
4+
* #%L
5+
* Data API Java Client
6+
* --
7+
* Copyright (C) 2024 DataStax
8+
* --
9+
* Licensed under the Apache License, Version 2.0
10+
* You may not use this file except in compliance with the License.
11+
* You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS,
17+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
* See the License for the specific language governing permissions and
19+
* limitations under the License.
20+
* #L%
21+
*/
22+
23+
import com.datastax.astra.client.admin.AdminOptions;
24+
import com.datastax.astra.client.core.commands.CommandType;
25+
import com.datastax.astra.client.core.options.BaseOptions;
26+
import com.datastax.astra.client.core.options.DataAPIClientOptions;
27+
import lombok.Getter;
28+
import lombok.Setter;
29+
import lombok.experimental.Accessors;
30+
31+
import static com.datastax.astra.client.tables.Table.DEFAULT_TABLE_SERIALIZER;
32+
33+
/**
34+
* Set of options used when creating a table
35+
*/
36+
@Setter
37+
@Accessors(fluent = true, chain = true)
38+
public class CreateKeyspaceOptions extends BaseOptions<CreateKeyspaceOptions> {
39+
40+
/** Improve syntax. */
41+
public static final CreateKeyspaceOptions IF_NOT_EXISTS = new CreateKeyspaceOptions().ifNotExists(true);
42+
43+
/**
44+
* Condition to upsert the table.
45+
*/
46+
boolean ifNotExists = true;
47+
48+
/**
49+
* Change the keyspace in the database.
50+
*/
51+
boolean updateDBKeyspace = false;
52+
53+
/**
54+
* Default constructor
55+
*/
56+
public CreateKeyspaceOptions() {
57+
super(null, CommandType.DATABASE_ADMIN, AdminOptions.DEFAULT_SERIALIZER, null);
58+
}
59+
60+
/**
61+
* Gets ifNotExists
62+
*
63+
* @return value of ifNotExists
64+
*/
65+
public boolean isIfNotExists() {
66+
return ifNotExists;
67+
}
68+
69+
/**
70+
* Gets updateDBKeyspace
71+
*
72+
* @return value of updateDBKeyspace
73+
*/
74+
public boolean isUpdateDBKeyspace() {
75+
return updateDBKeyspace;
76+
}
77+
}

0 commit comments

Comments
 (0)