Skip to content

Commit 8dd3494

Browse files
committed
update
1 parent 932a04d commit 8dd3494

File tree

11 files changed

+151
-47
lines changed

11 files changed

+151
-47
lines changed

astra-db-java/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<groupId>com.datastax.astra</groupId>
55
<artifactId>astra-db-java</artifactId>
66
<name>Java Client Library for Data API</name>
7-
<version>1.4.1-SNAPSHOT</version>
7+
<version>1.4.3-SNAPSHOT</version>
88
<description>Implementation of a client to the Astra/Stargate Data API written in Java</description>
99
<url>https://github.com/datastax/astra-db-java</url>
1010
<inceptionYear>2024</inceptionYear>

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public class Database extends AbstractCommandRunner {
6262

6363

6464
/** Token to be used with the Database. */
65+
@Getter
6566
private final String token;
6667

6768
/** Api Endpoint for the API. */
@@ -174,7 +175,15 @@ public Database useNamespace(String namespace) {
174175
* database admin
175176
*/
176177
public DatabaseAdmin getDatabaseAdmin() {
177-
return getDatabaseAdmin(this.token);
178+
if (options.getDestination() != null) {
179+
if (options.getDestination() == DataAPIDestination.ASTRA ||
180+
options.getDestination() == DataAPIDestination.ASTRA_DEV ||
181+
options.getDestination() == DataAPIDestination.ASTRA_TEST) {
182+
AstraApiEndpoint endpoint = AstraApiEndpoint.parse(getApiEndpoint());
183+
return new AstraDBDatabaseAdmin(this);
184+
}
185+
}
186+
return new DataAPIDatabaseAdmin(this);
178187
}
179188

180189
/**
@@ -187,8 +196,8 @@ public DatabaseAdmin getDatabaseAdmin() {
187196
public DatabaseAdmin getDatabaseAdmin(String superUserToken) {
188197
if (options.getDestination() != null) {
189198
if (options.getDestination() == DataAPIDestination.ASTRA ||
190-
options.getDestination() == DataAPIDestination.ASTRA_DEV ||
191-
options.getDestination() == DataAPIDestination.ASTRA_TEST) {
199+
options.getDestination() == DataAPIDestination.ASTRA_DEV ||
200+
options.getDestination() == DataAPIDestination.ASTRA_TEST) {
192201
AstraApiEndpoint endpoint = AstraApiEndpoint.parse(getApiEndpoint());
193202
return new AstraDBDatabaseAdmin(superUserToken, endpoint.getDatabaseId(), endpoint.getEnv(), options);
194203
}

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

Lines changed: 63 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,30 +34,42 @@
3434
import java.util.UUID;
3535
import java.util.stream.Collectors;
3636

37+
import static com.datastax.astra.client.admin.AstraDBAdmin.DEFAULT_NAMESPACE;
38+
3739
/**
3840
* 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.
3941
*/
4042
public class AstraDBDatabaseAdmin implements DatabaseAdmin {
4143

42-
/**
43-
* Token used for the credentials
44-
*/
44+
/** Token used for the credentials. */
4545
final String token;
4646

47-
/**
48-
* Token used for the credentials
49-
*/
47+
/** Token used for the credentials. */
5048
final UUID databaseId;
5149

5250
/** Working environment. */
5351
final AstraEnvironment env;
5452

55-
/** Options to personalized http client other client options. */
56-
final DataAPIOptions options;
57-
5853
/** Client for Astra Devops Api. */
5954
final AstraDBOpsClient devopsDbClient;
6055

56+
/** Database if initialized from the DB. */
57+
protected com.datastax.astra.client.Database db;
58+
59+
/**
60+
* Initialize a database admin from token and database id.
61+
*
62+
* @param db
63+
* target database
64+
*/
65+
public AstraDBDatabaseAdmin(com.datastax.astra.client.Database db) {
66+
this.databaseId = UUID.fromString(db.getDbApiEndpoint().substring(8, 44));
67+
this.env = getEnvironment(db.getOptions().getDestination());
68+
this.token = db.getToken();
69+
this.db = db;
70+
this.devopsDbClient = new AstraDBOpsClient(token, this.env);
71+
}
72+
6173
/**
6274
* Initialize a database admin from token and database id.
6375
*
@@ -74,8 +86,8 @@ public AstraDBDatabaseAdmin(String token, UUID databaseId, AstraEnvironment env,
7486
this.env = env;
7587
this.token = token;
7688
this.databaseId = databaseId;
77-
this.options = options;
7889
this.devopsDbClient = new AstraDBOpsClient(token, this.env);
90+
this.db = new com.datastax.astra.client.Database(getApiEndpoint(), token, DEFAULT_NAMESPACE, options);
7991
}
8092

8193
/**
@@ -90,6 +102,27 @@ public Database getDatabaseInformations() {
90102
.orElseThrow(() -> new DatabaseNotFoundException(databaseId.toString()));
91103
}
92104

105+
/**
106+
* Extract environment from the destination.
107+
*
108+
* @param destination
109+
* destination to extract the environment from
110+
* @return
111+
* the environment
112+
*/
113+
private static AstraEnvironment getEnvironment(DataAPIOptions.DataAPIDestination destination) {
114+
switch (destination) {
115+
case ASTRA:
116+
return AstraEnvironment.PROD;
117+
case ASTRA_DEV:
118+
return AstraEnvironment.DEV;
119+
case ASTRA_TEST:
120+
return AstraEnvironment.TEST;
121+
default:
122+
throw new IllegalArgumentException("Unknown destination: " + destination);
123+
}
124+
}
125+
93126
// ------------------------------------------
94127
// ---- Crud on Namespaces (dataApi) ----
95128
// ------------------------------------------
@@ -115,7 +148,7 @@ private String getApiEndpoint() {
115148
* client to interact with database DML.
116149
*/
117150
public com.datastax.astra.client.Database getDatabase(String namespaceName) {
118-
return getDatabase(namespaceName, this.token);
151+
return db.useNamespace(namespaceName);
119152
}
120153

121154
/**
@@ -128,7 +161,7 @@ public com.datastax.astra.client.Database getDatabase(String namespaceName) {
128161
* client to interact with database DML.
129162
*/
130163
public com.datastax.astra.client.Database getDatabase(String namespaceName, String tokenUser) {
131-
return new com.datastax.astra.client.Database(getApiEndpoint(), tokenUser, namespaceName, options);
164+
return new com.datastax.astra.client.Database(getApiEndpoint(), tokenUser, namespaceName, db.getOptions());
132165
}
133166

134167
/** {@inheritDoc} */
@@ -142,7 +175,8 @@ public Set<String> listNamespaceNames() {
142175
/** {@inheritDoc} */
143176
@Override
144177
public FindEmbeddingProvidersResult findEmbeddingProviders() {
145-
DataAPIDatabaseAdmin admin = new DataAPIDatabaseAdmin(getApiEndpoint() + "/" + options.getApiVersion(), token, options);
178+
DataAPIDatabaseAdmin admin =
179+
new DataAPIDatabaseAdmin(getApiEndpoint() + "/" + db.getOptions().getApiVersion(), token, db.getOptions());
146180
return new FindEmbeddingProvidersResult(admin.findEmbeddingProviders().getEmbeddingProviders());
147181
}
148182

@@ -152,6 +186,15 @@ public void createNamespace(String namespace) {
152186
devopsDbClient.database(databaseId.toString()).keyspaces().create(namespace);
153187
}
154188

189+
/** {@inheritDoc} */
190+
@Override
191+
public void createNamespace(String namespace, boolean updateDbNamespace) {
192+
createNamespace(namespace);
193+
if (updateDbNamespace) {
194+
db.useNamespace(namespace);
195+
}
196+
}
197+
155198
/** {@inheritDoc} */
156199
@Override
157200
public void dropNamespace(String namespace) {
@@ -161,4 +204,11 @@ public void dropNamespace(String namespace) {
161204
// Left blank to parse output from a delete
162205
}
163206
}
207+
208+
/** {@inheritDoc} */
209+
@Override
210+
public com.datastax.astra.client.Database getDatabase() {
211+
return db;
212+
}
213+
164214
}

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

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import java.util.Set;
3939
import java.util.stream.Collectors;
4040

41+
import static com.datastax.astra.client.admin.AstraDBAdmin.DEFAULT_NAMESPACE;
4142
import static com.datastax.astra.internal.utils.AnsiUtils.green;
4243
import static com.datastax.astra.internal.utils.Assert.hasLength;
4344
import static com.datastax.astra.internal.utils.Assert.notNull;
@@ -52,14 +53,8 @@ public class DataAPIDatabaseAdmin extends AbstractCommandRunner implements Datab
5253
/** parameters names. */
5354
private static final String ARG_NAMESPACE = "namespaceName";
5455

55-
/** Version of the API. */
56-
protected final DataAPIOptions options;
57-
58-
/** Version of the API. */
59-
protected final String apiEndPoint;
60-
61-
/** Version of the API. */
62-
protected final String token;
56+
/** Database if initialized from the DB. */
57+
protected Database db;
6358

6459
/**
6560
* Initialize a database admin from token and database id.
@@ -72,14 +67,22 @@ public class DataAPIDatabaseAdmin extends AbstractCommandRunner implements Datab
7267
* list of options for the admin
7368
*/
7469
public DataAPIDatabaseAdmin(String apiEndpoint, String token, DataAPIOptions options) {
75-
this.apiEndPoint = apiEndpoint;
76-
this.token = token;
77-
this.options = options;
70+
this(new Database(apiEndpoint, token, DEFAULT_NAMESPACE, options));
71+
}
72+
73+
/**
74+
* Initialize a database admin from token and database id.
75+
*
76+
* @param db
77+
* current database instance
78+
*/
79+
public DataAPIDatabaseAdmin(Database db) {
80+
this.db = db;
7881
this.commandOptions = new CommandOptions<>()
79-
.token(token)
80-
.embeddingAuthProvider(options.getEmbeddingAuthProvider())
81-
.httpClientOptions(options.getHttpClientOptions());
82-
options.getObservers().forEach(this.commandOptions::registerObserver);
82+
.token(db.getToken())
83+
.embeddingAuthProvider(db.getOptions().getEmbeddingAuthProvider())
84+
.httpClientOptions(db.getOptions().getHttpClientOptions());
85+
db.getOptions().getObservers().forEach(this.commandOptions::registerObserver);
8386
}
8487

8588
// ------------------------------------------
@@ -104,25 +107,35 @@ public FindEmbeddingProvidersResult findEmbeddingProviders() {
104107
EmbeddingProvider.class));
105108
}
106109

110+
/** {@inheritDoc} */
111+
@Override
112+
public Database getDatabase() {
113+
return db;
114+
}
115+
107116
/** {@inheritDoc} */
108117
@Override
109118
public Database getDatabase(String namespaceName) {
110-
Assert.hasLength(namespaceName, ARG_NAMESPACE);
111-
return new Database(apiEndPoint, token, namespaceName, options);
119+
return db.useNamespace(namespaceName);
112120
}
113121

114122
/** {@inheritDoc} */
115123
@Override
116124
public Database getDatabase(String namespaceName, String userToken) {
117125
Assert.hasLength(namespaceName, ARG_NAMESPACE);
118126
Assert.hasLength(userToken, "userToken");
119-
return new Database(apiEndPoint, userToken, namespaceName, options);
127+
db = new Database(db.getDbApiEndpoint(), userToken, namespaceName, db.getOptions());
128+
return db;
120129
}
121130

122131
/** {@inheritDoc} */
123-
public void createNamespace(String namespace) {
132+
@Override
133+
public void createNamespace(String namespace, boolean updateDbNamespace) {
124134
Assert.hasLength(namespace, ARG_NAMESPACE);
125135
createNamespace(namespace, NamespaceOptions.simpleStrategy(1));
136+
if (updateDbNamespace) {
137+
db.useNamespace(namespace);
138+
}
126139
}
127140

128141
/**
@@ -157,7 +170,7 @@ public void dropNamespace(String namespace) {
157170
/** {@inheritDoc} */
158171
@Override
159172
protected String getApiEndpoint() {
160-
return apiEndPoint;
173+
return db.getDbApiEndpoint();
161174
}
162175

163176
/**

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -173,14 +173,12 @@ default CompletableFuture<Set<String>> listNamespaceNamesAsync() {
173173
Database getDatabase(String namespaceName, String userToken);
174174

175175
/**
176-
* Access the Database asscociated with this admin class.
176+
* Access the Database associated with this admin class.
177177
*
178178
* @return
179179
* associated database
180180
*/
181-
default Database getDatabase() {
182-
return getDatabase(DEFAULT_NAMESPACE);
183-
}
181+
Database getDatabase();
184182

185183
/**
186184
* Drops (deletes) the specified namespace from the database. This operation is idempotent; it will not
@@ -250,8 +248,20 @@ default void dropNamespaceAsync(String namespace) {
250248
*
251249
* @param namespace
252250
* current namespace.
251+
* @param updateDbNamespace
252+
* if the namespace should be updated in the database.
253253
*/
254-
void createNamespace(String namespace);
254+
void createNamespace(String namespace, boolean updateDbNamespace);
255+
256+
/**
257+
* Syntax Sugar, retro compatible.
258+
*
259+
* @param namespace
260+
* current namespace.
261+
**/
262+
default void createNamespace(String namespace) {
263+
createNamespace(namespace, false);
264+
}
255265

256266
/**
257267
* Create a Namespace providing a name.

astra-db-java/src/test/java/com/datastax/astra/test/integration/AbstractDatabaseAdminITTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,20 @@ void shouldCreateNamespaceDefault() throws InterruptedException {
7777
Thread.sleep(1000);
7878
}
7979
}
80+
8081
// Then
8182
assertThat(getDatabaseAdmin().namespaceExists("nsx")).isTrue();
8283
assertThat(getDatabaseAdmin().getDatabase("nsx")
8384
.getNamespaceName()).isEqualTo("nsx");
8485

86+
if (!getDatabaseAdmin().namespaceExists("nsx2")) {
87+
getDatabaseAdmin().createNamespace("nsx2", true);
88+
while (!getDatabaseAdmin().namespaceExists("nsx2")) {
89+
Thread.sleep(1000);
90+
}
91+
assertThat(getDatabaseAdmin().getDatabase().getNamespaceName()).isEqualTo("nx2");
92+
}
93+
8594
// Surface
8695
final DatabaseAdmin dbAdmin2 = getDatabaseAdmin();
8796
assertThatExceptionOfType(IllegalArgumentException.class)

examples/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<parent>
1111
<groupId>com.datastax.astra</groupId>
1212
<artifactId>astra-db-java-parent</artifactId>
13-
<version>1.4.1-SNAPSHOT</version>
13+
<version>1.4.3-SNAPSHOT</version>
1414
</parent>
1515

1616
<dependencies>

examples/src/main/java/com/datastax/astra/client/database_admin/CreateNamespace.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,8 @@ public static void main(String[] args) {
99

1010
// Create a new namespace
1111
db.getDatabaseAdmin().createNamespace("<namespace_name>");
12+
13+
// The database can be mutate on namespace creation
14+
db.getDatabaseAdmin().createNamespace("<namespace2_name>", true);
1215
}
1316
}

0 commit comments

Comments
 (0)