Skip to content

Commit

Permalink
update stargate
Browse files Browse the repository at this point in the history
  • Loading branch information
clun committed Feb 27, 2024
1 parent 7b59297 commit e0c2b07
Show file tree
Hide file tree
Showing 34 changed files with 576 additions and 167 deletions.
24 changes: 6 additions & 18 deletions astra-db-client/src/main/java/com/dtsx/astra/sdk/AstraDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
import io.stargate.sdk.data.NamespaceClient;
import io.stargate.sdk.data.domain.CollectionDefinition;
import io.stargate.sdk.data.domain.SimilarityMetric;
import io.stargate.sdk.http.RetryHttpClient;
import io.stargate.sdk.http.ServiceHttp;
import io.stargate.sdk.http.domain.UserAgentChunk;
import lombok.Getter;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -94,6 +92,8 @@ public AstraDB(@NonNull String token, @NonNull String apiEndpoint, @NonNull Stri
jsonDeploy.addDatacenterServices("default", new ServiceHttp("json", apiEndpoint, apiEndpoint));
this.apiClient = new DataApiClient(jsonDeploy);
this.nsClient = apiClient.namespace(keyspace);
String version = AstraDB.class.getPackage().getImplementationVersion();
AstraDBAdmin.setCallerName(AstraDBAdmin.USER_AGENT, (null != version) ? version : "dev");
}

/**
Expand Down Expand Up @@ -158,6 +158,7 @@ public AstraDB(@NonNull String token, @NonNull UUID databaseId, String region, @
.findById(databaseId.toString())
.orElseThrow(() -> new DatabaseNotFoundException(databaseId.toString()));

// If no region is provided, we use the default region of the DB
if (region == null) region = db.getInfo().getRegion();

ServiceDeployment<ServiceHttp> jsonDeploy = new ServiceDeployment<>();
Expand All @@ -181,21 +182,8 @@ public AstraDB(@NonNull String token, @NonNull UUID databaseId, String region, @
this.nsClient = apiClient.namespace(keyspace);
}

// --------------------------
// --- User Agent ----
// --------------------------

/**
* Allow user to set the client name
*
* @param clientName
* client name
* @param version
* client version
*/
public void setClientName(String clientName, String version) {
RetryHttpClient.getInstance().pushUserAgent(new UserAgentChunk(clientName, version));
}


// --------------------------
// --- Find, FindAll ----
Expand Down Expand Up @@ -433,7 +421,7 @@ public void changeKeyspace(String keyspace) {
* @return
* storeName client
*/
public AstraDBCollection collection(@NonNull String storeName) {
public AstraDBCollection getCollection(@NonNull String storeName) {
return new AstraDBCollection(nsClient.collection(storeName));
}

Expand All @@ -449,7 +437,7 @@ public AstraDBCollection collection(@NonNull String storeName) {
* @param <T>
* type of the bean in use
*/
public <T> AstraDBRepository<T> collectionRepository(@NonNull String storeName, Class<T> clazz) {
public <T> AstraDBRepository<T> getCollection(@NonNull String storeName, Class<T> clazz) {
return new AstraDBRepository<>(nsClient.collectionRepository(storeName, clazz));
}

Expand Down
71 changes: 55 additions & 16 deletions astra-db-client/src/main/java/com/dtsx/astra/sdk/AstraDBAdmin.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.dtsx.astra.sdk.utils.AstraEnvironment;
import com.dtsx.astra.sdk.utils.AstraRc;
import io.stargate.sdk.data.DataApiClient;
import io.stargate.sdk.http.RetryHttpClient;
import lombok.Getter;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -38,6 +39,8 @@
@Slf4j
public class AstraDBAdmin {

public static final String USER_AGENT = "astra-db-java";

/** Default timeout for initiating connection. */
public static final int CONNECT_TIMEOUT_SECONDS = 20;

Expand Down Expand Up @@ -114,6 +117,32 @@ public AstraDBAdmin(String token, AstraEnvironment env) {
.version(Version.HTTP_2)
.connectTimeout(Duration.ofSeconds(CONNECT_TIMEOUT_SECONDS))
.build();
String version = AstraDBAdmin.class.getPackage().getImplementationVersion();
AstraDBAdmin.setCallerName(AstraDBAdmin.USER_AGENT, (null != version) ? version : "dev");
}

// --------------------------
// --- User Agent ----
// --------------------------

/**
* Allow user to set the client name
*
* @param callerName
* client name
* @param callerVersion
* client version
*/
public static void setCallerName(String callerName, String callerVersion) {
RetryHttpClient.getInstance().pushUserAgent(callerName, callerVersion);
}

// --------------------
// -- Watch ---
// --------------------

public void watch() {
throw new UnsupportedOperationException("As we connect to a HTTP apis without hooks, no watch is possible.");
}

// --------------------
Expand Down Expand Up @@ -172,13 +201,23 @@ public void deleteKeyspace(UUID databaseId, String keyspaceName) {
// -- Databases ---
// --------------------

/**
* List available database names.
*
* @return
* list of database names
*/
public List<String> listDatabaseNames() {
return listDatabases().map(db -> db.getInfo().getName()).collect(Collectors.toList());
}

/**
* List active databases with vector enabled in current organization.
*
* @return
* active databases list
*/
public Stream<Database> findAllDatabases() {
public Stream<Database> listDatabases() {
return devopsDbClient
.findAllNonTerminated()
.filter(db -> db.getInfo().getDbType() != null);
Expand Down Expand Up @@ -211,7 +250,7 @@ public UUID createDatabase(@NonNull String name) {
* database identifier
*/
public UUID createDatabase(@NonNull String name, @NonNull CloudProviderType cloud, @NonNull String cloudRegion) {
Optional<Database> optDb = findDatabaseByName(name).findFirst();
Optional<Database> optDb = getDatabaseInformations(name).findFirst();
// Handling all cases for the user
if (optDb.isPresent()) {
Database db = optDb.get();
Expand Down Expand Up @@ -255,8 +294,8 @@ public UUID createDatabase(@NonNull String name, @NonNull CloudProviderType clou
* @return
* if the db has been deleted
*/
public boolean deleteDatabaseByName(@NonNull String name) {
Optional<Database> opDb = findDatabaseByName(name).findFirst();
public boolean dropDatabase(@NonNull String name) {
Optional<Database> opDb = getDatabaseInformations(name).findFirst();
opDb.ifPresent(db -> devopsDbClient.database(db.getId()).delete());
return opDb.isPresent();
}
Expand All @@ -269,8 +308,8 @@ public boolean deleteDatabaseByName(@NonNull String name) {
* @return
* if the db has been deleted
*/
public boolean deleteDatabaseById(@NonNull UUID databaseId) {
if (findDatabaseById(databaseId).isPresent()) {
public boolean dropDatabase(@NonNull UUID databaseId) {
if (getDatabaseInformations(databaseId).isPresent()) {
devopsDbClient.database(databaseId.toString()).delete();
return true;
}
Expand All @@ -285,9 +324,9 @@ public boolean deleteDatabaseById(@NonNull UUID databaseId) {
* @return
* list of db matching the criteria
*/
public Stream<Database> findDatabaseByName(String name) {
public Stream<Database> getDatabaseInformations(String name) {
Assert.hasLength(name, "Database name");
return findAllDatabases().filter(db->name.equals(db.getInfo().getName()));
return listDatabases().filter(db->name.equals(db.getInfo().getName()));
}

/**
Expand All @@ -299,7 +338,7 @@ public Stream<Database> findDatabaseByName(String name) {
* if the database exists
*/
public boolean isDatabaseExists(String name) {
return findDatabaseByName(name).findFirst().isPresent();
return getDatabaseInformations(name).findFirst().isPresent();
}

/**
Expand All @@ -310,7 +349,7 @@ public boolean isDatabaseExists(String name) {
* @return
* list of db matching the criteria
*/
public Optional<Database> findDatabaseById(@NonNull UUID id) {
public Optional<Database> getDatabaseInformations(@NonNull UUID id) {
Assert.notNull(id, "Database identifier should not be null");
return devopsDbClient.findById(id.toString());
}
Expand All @@ -323,15 +362,15 @@ public Optional<Database> findDatabaseById(@NonNull UUID id) {
* @return
* database client
*/
public AstraDB database(@NonNull String databaseName) {
List<Database> dbs = findDatabaseByName(databaseName).collect(Collectors.toList());
public AstraDB getDatabase(@NonNull String databaseName) {
List<Database> dbs = getDatabaseInformations(databaseName).collect(Collectors.toList());
if (dbs.isEmpty()) {
throw new DatabaseNotFoundException(databaseName);
}
if (dbs.size() > 1) {
throw new IllegalStateException("More than one database exists with the same name, use id.");
}
return database(UUID.fromString(dbs.get(0).getId()));
return getDatabase(UUID.fromString(dbs.get(0).getId()));
}

// --------------------
Expand All @@ -346,7 +385,7 @@ public AstraDB database(@NonNull String databaseName) {
* @return
* database client
*/
public AstraDB database(UUID databaseId) {
public AstraDB getDatabase(UUID databaseId) {
return new AstraDB(token, databaseId, null, env, AstraDBAdmin.DEFAULT_KEYSPACE);
}

Expand Down Expand Up @@ -423,7 +462,7 @@ private void resumeDb(Database db) {
* database client
*/
public DataApiClient getDataApiClient(@NonNull String databaseName) {
return database(databaseName).getApiClient();
return getDatabase(databaseName).getApiClient();
}

/**
Expand All @@ -435,7 +474,7 @@ public DataApiClient getDataApiClient(@NonNull String databaseName) {
* database client
*/
public DataApiClient getDataApiClient(@NonNull UUID databaseId) {
return database(databaseId).getApiClient();
return getDatabase(databaseId).getApiClient();
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.dtsx.astra.sdk;

public class AstraDBClients {

public static AstraDBAdmin create(String token) {
return new AstraDBAdmin(token);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ public class AstraDBCollection {
this.collectionClient = collectionClient;
}

/**
* Access the name of the collection.
*
* @return
* collection name
*/
public String getName() {
return collectionClient.getCollection();
}

// --------------------------
// --- Insert One ----
// --------------------------
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.dtsx.astra.sdk.cassio;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.UUID;

/**
* Default Constructor.
*/
@Data
@AllArgsConstructor
public class ClusteredRecord {

/** Partition id. */
String partitionId;

/** Row identifier. */
UUID rowId;

/** Text body. */
String body;

/**
* Record for a clustered table.
*/
public ClusteredRecord() {}

}
Loading

0 comments on commit e0c2b07

Please sign in to comment.