Skip to content

Commit d5781cd

Browse files
committed
objectID
1 parent 547c473 commit d5781cd

25 files changed

+344
-147
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -322,10 +322,10 @@ private InsertOneResult _insertOne(Document document) {
322322
Command insertOne = Command
323323
.create("insertOne")
324324
.withDocument(document);
325+
325326
ApiResponse res = runCommand(insertOne);
326-
return new InsertOneResult(res
327-
.getStatusKeyAsList("insertedIds", Object.class)
328-
.get(0));
327+
// Object
328+
return new InsertOneResult(res.getStatusKeyAsList("insertedIds", Object.class).get(0));
329329
}
330330

331331
/**

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

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

3+
import com.datastax.astra.client.observer.LoggerCommandObserver;
4+
import com.datastax.astra.internal.DataAPIDatabaseAdmin;
35
import com.datastax.astra.internal.auth.StargateAuthenticationService;
46

7+
import static com.datastax.astra.client.AstraDBAdmin.DEFAULT_NAMESPACE;
8+
59
/**
610
* Initialization of the client in a Static way.
711
*/
@@ -27,12 +31,21 @@ private DataAPIClients() {}
2731
/**
2832
* Create from an Endpoint only
2933
*/
30-
public static DataAPIClient localStargate() {
34+
public static DataAPIClient localClient() {
3135
return new DataAPIClient(
3236
new StargateAuthenticationService().getToken(),
3337
DataAPIOptions.builder().withDestination(DataAPIDestination.CASSANDRA).build());
3438
}
3539

40+
public static Database localDatabase() {
41+
Database db = localClient().getDatabase(DEFAULT_ENDPOINT, DEFAULT_NAMESPACE);
42+
db.registerListener("logger", new LoggerCommandObserver(Database.class));
43+
DataAPIDatabaseAdmin dbAdmin = (DataAPIDatabaseAdmin) db.getDatabaseAdmin();
44+
dbAdmin.registerListener("logger", new LoggerCommandObserver(Database.class));
45+
dbAdmin.createNamespace(DEFAULT_NAMESPACE);
46+
return db;
47+
}
48+
3649
public static DataAPIClient astra(String token) {
3750
return new DataAPIClient(token, DataAPIOptions
3851
.builder()

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ protected String getApiEndpoint() {
331331
/** {@inheritDoc} */
332332
@Override
333333
protected String getToken() {
334-
return null;
334+
return token;
335335
}
336336

337337
/** {@inheritDoc} */

src/main/java/com/datastax/astra/client/model/collections/CollectionOptions.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class CollectionOptions {
1515
/**
1616
* Defaulting to Object I
1717
*/
18-
private CollectionIdTypes defaultId;
18+
private Map<String, CollectionIdTypes> defaultId;
1919

2020
/**
2121
* Vector options.
@@ -216,7 +216,9 @@ public CollectionOptions build() {
216216
CollectionOptions req = new CollectionOptions();
217217
req.vector = this.vector;
218218
req.indexing = this.indexing;
219-
req.defaultId = this.defaultId;
219+
if (defaultId != null) {
220+
req.defaultId = Map.of("type", this.defaultId);
221+
}
220222
return req;
221223
}
222224
}

src/main/java/com/datastax/astra/internal/AbstractCommandRunner.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,12 @@ public ApiResponse runCommand(Command command) {
6565
String jsonCommand = JsonUtils.marshallForDataApi(command);
6666
ApiResponseHttp httpRes = getHttpClient().POST(getApiEndpoint(), getToken(), jsonCommand);
6767
executionInfo.withHttpResponse(httpRes);
68-
6968
ApiResponse jsonRes = JsonUtils.unmarshallBeanForDataApi(httpRes.getBody(), ApiResponse.class);
7069
executionInfo.withApiResponse(jsonRes);
71-
7270
// Encapsulate Errors
7371
if (jsonRes.getErrors() != null) {
7472
throw new DataApiResponseException(Collections.singletonList(executionInfo.build()));
7573
}
76-
7774
return jsonRes;
7875
} finally {
7976
// Notify the observers

src/main/java/com/datastax/astra/internal/DataAPIDatabaseAdmin.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.datastax.astra.client.model.Command;
77
import com.datastax.astra.client.model.namespaces.CreateNamespaceOptions;
88
import com.datastax.astra.internal.http.HttpClientOptions;
9+
import com.datastax.astra.internal.utils.Assert;
910
import lombok.Getter;
1011
import lombok.extern.slf4j.Slf4j;
1112

@@ -31,6 +32,7 @@ public class DataAPIDatabaseAdmin extends AbstractCommandRunner implements Datab
3132
/** Version of the API. */
3233
protected final String token;
3334

35+
/** Endpoint for a Database in particular. */
3436
private final String apiEndpointDatabase;
3537

3638
/**
@@ -45,11 +47,7 @@ public DataAPIDatabaseAdmin(String apiEndpoint, String token, DataAPIOptions opt
4547
this.apiEndPoint = apiEndpoint;
4648
this.token = token;
4749
this.options = options;
48-
StringBuilder dbApiEndPointBuilder = new StringBuilder(apiEndpoint);
49-
dbApiEndPointBuilder
50-
.append("/")
51-
.append(options.getApiVersion());
52-
this.apiEndpointDatabase = dbApiEndPointBuilder.toString();
50+
this.apiEndpointDatabase = apiEndpoint + "/" + options.getApiVersion();
5351
}
5452

5553
// ------------------------------------------
@@ -63,18 +61,24 @@ public Stream<String> listNamespaceNames() {
6361
return runCommand(cmd).getStatusKeyAsStringStream("namespaces");
6462
}
6563

64+
/** {@inheritDoc} */
6665
@Override
6766
public Database getDatabase(String namespaceName) {
68-
return null;
67+
Assert.hasLength(namespaceName, "namespaceName");
68+
return new Database(apiEndPoint, token, namespaceName, options);
6969
}
7070

71+
/** {@inheritDoc} */
7172
@Override
7273
public Database getDatabase(String namespaceName, String userToken) {
73-
return null;
74+
Assert.hasLength(namespaceName, "namespaceName");
75+
Assert.hasLength(userToken, "userToken");
76+
return new Database(apiEndPoint, userToken, namespaceName, options);
7477
}
7578

7679
/** {@inheritDoc} */
7780
public void createNamespace(String namespace) {
81+
Assert.hasLength(namespace, "namespace");
7882
createNamespace(namespace, CreateNamespaceOptions.simpleStrategy(1));
7983
}
8084

src/main/java/com/datastax/astra/internal/serialization/CustomUuidv6Serializer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public CustomUuidv6Serializer(Class<UUIDv6> t) {
3232
@Override
3333
public void serialize(UUIDv6 uuidv6, JsonGenerator gen, SerializerProvider provider) throws IOException {
3434
gen.writeStartObject();
35-
gen.writeStringField("$uuidv6", uuidv6.toString());
35+
gen.writeStringField("$uuid", uuidv6.toString());
3636
gen.writeEndObject();
3737
}
3838
}

src/main/java/com/datastax/astra/internal/serialization/CustomUuidv7Serializer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public CustomUuidv7Serializer(Class<UUIDv7> t) {
3232
@Override
3333
public void serialize(UUIDv7 uuidv7, JsonGenerator gen, SerializerProvider provider) throws IOException {
3434
gen.writeStartObject();
35-
gen.writeStringField("$uuidv7", uuidv7.toString());
35+
gen.writeStringField("$uuid", uuidv7.toString());
3636
gen.writeEndObject();
3737
}
3838
}

src/main/java/com/datastax/astra/internal/utils/JsonUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,10 @@ public static synchronized ObjectMapper getDataApiObjectMapper() {
101101
module.addDeserializer(UUID.class, new CustomUuidDeserializer());
102102
// UUIDv6
103103
module.addSerializer(UUIDv6.class, new CustomUuidv6Serializer());
104-
module.addDeserializer(UUIDv6.class, new CustomUuidv6Deserializer());
104+
//module.addDeserializer(UUIDv6.class, new CustomUuidv6Deserializer());
105105
// UUIDv7
106106
module.addSerializer(UUIDv7.class, new CustomUuidv7Serializer());
107-
module.addDeserializer(UUIDv7.class, new CustomUuidv7Deserializer());
107+
//module.addDeserializer(UUIDv7.class, new CustomUuidv7Deserializer());
108108
// ObjectId
109109
module.addSerializer(ObjectId.class, new CustomObjectIdSerializer());
110110
module.addDeserializer(ObjectId.class, new CustomObjectIdDeserializer());

src/test/java/com/datastax/astra/AstraDBTestSupport.java

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,26 @@ public class AstraDBTestSupport {
2323
*/
2424
public static final String DATABASE_NAME = "astra_db_client";
2525

26+
/**
27+
* Initialize the Test database on an Astra Environment.
28+
*
29+
* @param env
30+
* target environment
31+
* @param cloud
32+
* target cloud
33+
* @param region
34+
* target region
35+
* @return
36+
* the database instance
37+
*/
38+
public static Database initializeDb(AstraEnvironment env, CloudProviderType cloud, String region) {
39+
log.info("Working in environment '{}'", env.name());
40+
AstraDBAdmin client = getAstraDBClient(env);
41+
UUID databaseId = client.createDatabase(DATABASE_NAME, cloud, region);
42+
log.info("Working with api Endpoint '{}'", ApiLocator.getApiJsonEndpoint(env, databaseId.toString(), region));
43+
return client.getDatabase(databaseId);
44+
}
45+
2646
/**
2747
* Access AstraDBAdmin for different environment (to create DB).
2848
*
@@ -47,27 +67,7 @@ public static AstraDBAdmin getAstraDBClient(AstraEnvironment env) {
4767
.getAdmin();
4868
default:
4969
throw new IllegalArgumentException("Invalid Environment");
50-
}
51-
}
52-
53-
/**
54-
* Initialize the Test database on an Astra Environment.
55-
*
56-
* @param env
57-
* target environment
58-
* @param cloud
59-
* target cloud
60-
* @param region
61-
* target region
62-
* @return
63-
* the database instance
64-
*/
65-
public static Database initializeDb(AstraEnvironment env, CloudProviderType cloud, String region) {
66-
log.info("Working in environment '{}'", env.name());
67-
AstraDBAdmin client = getAstraDBClient(env);
68-
UUID databaseId = client.createDatabase(DATABASE_NAME, cloud, region);
69-
log.info("Working with api Endpoint '{}'", ApiLocator.getApiJsonEndpoint(env, databaseId.toString(), region));
70-
return client.getDatabase(databaseId);
70+
}
7171
}
7272

7373

0 commit comments

Comments
 (0)