Skip to content

Commit 39aa276

Browse files
committed
Preparing 1.5
1 parent 4473041 commit 39aa276

28 files changed

+728
-108
lines changed

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

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@
6464
import com.datastax.astra.internal.api.ApiResponse;
6565
import com.datastax.astra.internal.command.AbstractCommandRunner;
6666
import com.datastax.astra.internal.command.CommandObserver;
67-
import com.datastax.astra.internal.command.LoggingCommandObserver;
6867
import com.datastax.astra.internal.utils.Assert;
6968
import com.datastax.astra.internal.utils.JsonUtils;
7069
import lombok.Getter;
@@ -280,9 +279,35 @@ protected Collection(Database db, String collectionName, CommandOptions<?> comma
280279
* System.out.println("The collection belongs to the namespace: " + namespaceName);
281280
* }
282281
* </pre>
282+
* @deprecated use {@link #getKeyspaceName()} instead
283283
*/
284+
@Deprecated
284285
public String getNamespaceName() {
285-
return getDatabase().getNamespaceName();
286+
return getDatabase().getKeyspaceName();
287+
}
288+
289+
/**
290+
* Retrieves the name of the parent keyspace associated with this collection. A keyspace in
291+
* this context typically refers to a higher-level categorization or grouping mechanism within
292+
* the database that encompasses one or more collections. This method allows for identifying
293+
* the broader context in which this collection exists, which can be useful for operations
294+
* requiring knowledge of the database structure or for dynamic database interaction patterns.
295+
*
296+
* @return A {@code String} representing the name of the parent keyspace of the current
297+
* collection. This name serves as an identifier for the keyspace and can be used
298+
* to navigate or query the database structure.
299+
*
300+
* <p>Example usage:</p>
301+
* <pre>
302+
* {@code
303+
* Collection myCollection = ... // assume myCollection is already initialized
304+
* String keyspaceName = myCollection.getKeyspaceName();
305+
* System.out.println("The collection belongs to the keyspace: " + namespaceName);
306+
* }
307+
* </pre>
308+
*/
309+
public String getKeyspaceName() {
310+
return getDatabase().getKeyspaceName();
286311
}
287312

288313
/**

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

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
import lombok.extern.slf4j.Slf4j;
4141

4242
import java.util.Map;
43-
import java.util.Objects;
4443
import java.util.stream.Stream;
4544

4645
import static com.datastax.astra.internal.utils.AnsiUtils.green;
@@ -75,7 +74,7 @@ public class Database extends AbstractCommandRunner {
7574

7675
/** Current Namespace information.*/
7776
@Getter
78-
private String namespaceName;
77+
private String keyspaceName;
7978

8079
/**
8180
* This core endpoint could be used for admin operations.
@@ -115,17 +114,17 @@ public Database(String apiEndpoint, String token, String namespace) {
115114
* api endpoint
116115
* @param token
117116
* api token
118-
* @param namespace
119-
* namespace
117+
* @param keyspace
118+
* keyspace
120119
* @param options
121120
* setup of the clients with options
122121
*/
123-
public Database(String apiEndpoint, String token, String namespace, DataAPIOptions options) {
122+
public Database(String apiEndpoint, String token, String keyspace, DataAPIOptions options) {
124123
hasLength(apiEndpoint, "endpoint");
125124
hasLength(token, "token");
126-
hasLength(namespace, "namespace");
125+
hasLength(keyspace, "keyspace");
127126
notNull(options, "options");
128-
this.namespaceName = namespace;
127+
this.keyspaceName = keyspace;
129128
this.token = token;
130129
this.options = options;
131130
this.dbApiEndpoint = apiEndpoint;
@@ -159,9 +158,24 @@ public Database(String apiEndpoint, String token, String namespace, DataAPIOptio
159158
* current namespace
160159
* @return
161160
* the database
161+
* @deprecated use {@link #useKeyspace(String)}
162162
*/
163+
@Deprecated
163164
public Database useNamespace(String namespace) {
164-
this.namespaceName = namespace;
165+
return useKeyspace(namespace);
166+
}
167+
168+
/**
169+
* This mutates the keyspace to be used.
170+
*
171+
* @param keyspace
172+
* current keyspace
173+
* @return
174+
* the database
175+
*/
176+
@Deprecated
177+
public Database useKeyspace(String keyspace) {
178+
this.keyspaceName = keyspace;
165179
return this;
166180
}
167181

@@ -319,7 +333,7 @@ public <T> Collection<T> getCollection(String collectionName, CommandOptions<?>
319333
* Drops this namespace
320334
*/
321335
public void drop() {
322-
getDatabaseAdmin().dropNamespace(getNamespaceName());
336+
getDatabaseAdmin().dropNamespace(getKeyspaceName());
323337
}
324338

325339
/**
@@ -495,7 +509,7 @@ protected String getApiEndpoint() {
495509
.append("/")
496510
.append(options.getApiVersion())
497511
.append("/")
498-
.append(namespaceName)
512+
.append(keyspaceName)
499513
.toString();
500514
}
501515

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

Lines changed: 25 additions & 8 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.model.DatabaseInfo;
2525
import com.datastax.astra.internal.api.AstraApiEndpoint;
26+
import com.datastax.astra.internal.command.LoggingCommandObserver;
2627
import com.datastax.astra.internal.utils.Assert;
2728
import com.dtsx.astra.sdk.db.AstraDBOpsClient;
2829
import com.dtsx.astra.sdk.db.DbOpsClient;
@@ -33,12 +34,16 @@
3334
import com.dtsx.astra.sdk.db.exception.DatabaseNotFoundException;
3435
import com.dtsx.astra.sdk.utils.AstraEnvironment;
3536
import com.dtsx.astra.sdk.utils.AstraRc;
37+
import com.dtsx.astra.sdk.utils.observability.ApiRequestObserver;
38+
import com.dtsx.astra.sdk.utils.observability.LoggingRequestObserver;
3639
import lombok.NonNull;
3740
import lombok.extern.slf4j.Slf4j;
3841

3942
import java.net.http.HttpClient;
4043
import java.time.Duration;
44+
import java.util.HashMap;
4145
import java.util.List;
46+
import java.util.Map;
4247
import java.util.Optional;
4348
import java.util.UUID;
4449
import java.util.stream.Collectors;
@@ -64,8 +69,12 @@ public class AstraDBAdmin {
6469
public static final String TOKEN_HEADER_PARAM = "X-Token";
6570

6671
/** Default keyspace (same created by the ui). */
72+
@Deprecated
6773
public static final String DEFAULT_NAMESPACE = "default_keyspace";
6874

75+
/** Default keyspace (same created by the ui). */
76+
public static final String DEFAULT_KEYSPACE = "default_keyspace";
77+
6978
/** Client for Astra Devops Api. */
7079
final AstraDBOpsClient devopsDbClient;
7180

@@ -112,7 +121,15 @@ public AstraDBAdmin(String token, AstraEnvironment env, DataAPIOptions options)
112121
this.token = token;
113122
this.env = env;
114123
this.dataAPIOptions = options;
115-
this.devopsDbClient = new AstraDBOpsClient(token, this.env);
124+
if (options.getObservers() != null) {
125+
Map<String, ApiRequestObserver> devopsObservers = new HashMap<>();
126+
if (options.getObservers().keySet().contains(LoggingCommandObserver.class.getSimpleName())) {
127+
devopsObservers.put("logging", new LoggingRequestObserver(AstraDBAdmin.class));
128+
}
129+
this.devopsDbClient = new AstraDBOpsClient(token, this.env, devopsObservers);
130+
} else {
131+
this.devopsDbClient = new AstraDBOpsClient(token, this.env);
132+
}
116133

117134
// Local Agent for Resume
118135
HttpClient.Builder httpClientBuilder = HttpClient.newBuilder();
@@ -234,7 +251,7 @@ public DatabaseAdmin createDatabase(String name, CloudProviderType cloud, String
234251
.name(name)
235252
.cloudProvider(cloud)
236253
.cloudRegion(cloudRegion)
237-
.keyspace(DEFAULT_NAMESPACE)
254+
.keyspace(DEFAULT_KEYSPACE)
238255
.withVector().build()));
239256
log.info("Database {} is starting (id={}): it will take about a minute please wait...", name, newDbId);
240257
if (waitForDb) {
@@ -320,21 +337,21 @@ public DatabaseInfo getDatabaseInfo(@NonNull UUID id) {
320337
*
321338
* @param databaseId
322339
* database identifier
323-
* @param namespace
324-
* target namespace name
340+
* @param keyspace
341+
* target keyspace name
325342
* @return
326343
* database client
327344
*/
328-
public com.datastax.astra.client.Database getDatabase(UUID databaseId, String namespace) {
345+
public com.datastax.astra.client.Database getDatabase(UUID databaseId, String keyspace) {
329346
Assert.notNull(databaseId, "databaseId");
330-
Assert.hasLength(namespace, "namespace");
347+
Assert.hasLength(keyspace, "keyspace");
331348
String databaseRegion = devopsDbClient
332349
.findById(databaseId.toString())
333350
.map(db -> db.getInfo().getRegion())
334351
.orElseThrow(() -> new DatabaseNotFoundException(databaseId.toString()));
335352
return new com.datastax.astra.client.Database(
336353
new AstraApiEndpoint(databaseId, databaseRegion, env).getApiEndPoint(),
337-
token,namespace, dataAPIOptions) {
354+
token,keyspace, dataAPIOptions) {
338355
};
339356
}
340357

@@ -347,7 +364,7 @@ public com.datastax.astra.client.Database getDatabase(UUID databaseId, String na
347364
* database client
348365
*/
349366
public com.datastax.astra.client.Database getDatabase(UUID databaseId) {
350-
return getDatabase(databaseId, DEFAULT_NAMESPACE);
367+
return getDatabase(databaseId, DEFAULT_KEYSPACE);
351368
}
352369

353370
/**

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

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.dtsx.astra.sdk.db.domain.Database;
2929
import com.dtsx.astra.sdk.db.exception.DatabaseNotFoundException;
3030
import com.dtsx.astra.sdk.utils.AstraEnvironment;
31+
import lombok.extern.slf4j.Slf4j;
3132

3233
import java.util.Map;
3334
import java.util.Set;
@@ -39,6 +40,7 @@
3940
/**
4041
* Implementation of the DatabaseAdmin interface for Astra. To create the namespace the devops APi is leverage. To use this class a higher token permission is required.
4142
*/
43+
@Slf4j
4244
public class AstraDBDatabaseAdmin implements DatabaseAdmin {
4345

4446
/** Token used for the credentials. */
@@ -97,6 +99,7 @@ public AstraDBDatabaseAdmin(String token, UUID databaseId, AstraEnvironment env,
9799
* list of db matching the criteria
98100
*/
99101
public Database getDatabaseInformations() {
102+
log.debug("getDatabaseInformations");
100103
return devopsDbClient
101104
.findById(databaseId.toString())
102105
.orElseThrow(() -> new DatabaseNotFoundException(databaseId.toString()));
@@ -142,31 +145,40 @@ private String getApiEndpoint() {
142145
/**
143146
* Access teh database with the default token.
144147
*
145-
* @param namespaceName The name of the namespace (or keyspace) to retrieve. This parameter should match the
148+
* @param keyspace The name of the namespace (or keyspace) to retrieve. This parameter should match the
146149
* exact name of the namespace as it exists in the database.
147150
* @return
148151
* client to interact with database DML.
149152
*/
150-
public com.datastax.astra.client.Database getDatabase(String namespaceName) {
151-
return db.useNamespace(namespaceName);
153+
public com.datastax.astra.client.Database getDatabase(String keyspace) {
154+
return db.useNamespace(keyspace);
152155
}
153156

154157
/**
155158
* Access teh database with the specialized token.
156159
*
157-
* @param namespaceName The name of the namespace (or keyspace) to retrieve. This parameter should match the
160+
* @param keyspace The name of the namespace (or keyspace) to retrieve. This parameter should match the
158161
* exact name of the namespace as it exists in the database.
159162
* @param tokenUser token with reduce privileges compared to admin token in order to do dml options (CRUD).
160163
* @return
161164
* client to interact with database DML.
162165
*/
163-
public com.datastax.astra.client.Database getDatabase(String namespaceName, String tokenUser) {
164-
return new com.datastax.astra.client.Database(getApiEndpoint(), tokenUser, namespaceName, db.getOptions());
166+
public com.datastax.astra.client.Database getDatabase(String keyspace, String tokenUser) {
167+
return new com.datastax.astra.client.Database(getApiEndpoint(), tokenUser, keyspace, db.getOptions());
165168
}
166169

167170
/** {@inheritDoc} */
168171
@Override
169172
public Set<String> listNamespaceNames() {
173+
log.debug("listNamespaceNames");
174+
return devopsDbClient
175+
.database(databaseId.toString())
176+
.keyspaces().findAll();
177+
}
178+
179+
@Override
180+
public Set<String> listKeyspaceNames() {
181+
log.debug("listKeyspaceNames");
170182
return devopsDbClient
171183
.database(databaseId.toString())
172184
.keyspaces().findAll();
@@ -175,6 +187,7 @@ public Set<String> listNamespaceNames() {
175187
/** {@inheritDoc} */
176188
@Override
177189
public FindEmbeddingProvidersResult findEmbeddingProviders() {
190+
log.debug("findEmbeddingProviders");
178191
DataAPIDatabaseAdmin admin =
179192
new DataAPIDatabaseAdmin(getApiEndpoint() + "/" + db.getOptions().getApiVersion(), token, db.getOptions());
180193
return new FindEmbeddingProvidersResult(admin.findEmbeddingProviders().getEmbeddingProviders());
@@ -183,21 +196,39 @@ public FindEmbeddingProvidersResult findEmbeddingProviders() {
183196
/** {@inheritDoc} */
184197
@Override
185198
public void createNamespace(String namespace) {
199+
log.debug("createNamespace");
186200
devopsDbClient.database(databaseId.toString()).keyspaces().create(namespace);
187201
}
188202

189203
/** {@inheritDoc} */
190204
@Override
191-
public void createNamespace(String namespace, boolean updateDbNamespace) {
192-
createNamespace(namespace);
193-
if (updateDbNamespace) {
194-
db.useNamespace(namespace);
205+
public void createNamespace(String keyspace, boolean updateDBKeyspace) {
206+
createNamespace(keyspace);
207+
if (updateDBKeyspace) {
208+
db.useNamespace(keyspace);
195209
}
196210
}
197211

212+
@Override
213+
public void createKeyspace(String keyspace, boolean updateDBKeyspace) {
214+
log.debug("createKeyspace");
215+
devopsDbClient.database(databaseId.toString()).keyspaces().create(keyspace);
216+
}
217+
198218
/** {@inheritDoc} */
199219
@Override
200220
public void dropNamespace(String namespace) {
221+
log.debug("dropNamespace");
222+
try {
223+
devopsDbClient.database(databaseId.toString()).keyspaces().delete(namespace);
224+
} catch(NullPointerException e) {
225+
// Left blank to parse output from a delete
226+
}
227+
}
228+
229+
@Override
230+
public void dropKeyspace(String namespace) {
231+
log.debug("dropKeyspace");
201232
try {
202233
devopsDbClient.database(databaseId.toString()).keyspaces().delete(namespace);
203234
} catch(NullPointerException e) {

0 commit comments

Comments
 (0)