Skip to content

Commit b10bdd1

Browse files
committed
table support
1 parent 8fa5104 commit b10bdd1

26 files changed

+1815
-530
lines changed

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

Lines changed: 175 additions & 85 deletions
Large diffs are not rendered by default.

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

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

23-
import com.datastax.astra.client.admin.DataAPIDatabaseAdmin;
2423
import com.datastax.astra.client.core.auth.UsernamePasswordTokenProvider;
2524
import com.datastax.astra.client.core.options.DataAPIClientOptions;
2625
import com.datastax.astra.client.databases.Database;
27-
import com.datastax.astra.internal.command.LoggingCommandObserver;
2826

2927
import static com.datastax.astra.client.core.options.DataAPIClientOptions.DEFAULT_KEYSPACE;
3028

@@ -42,10 +40,10 @@
4240
* <pre>
4341
* {@code
4442
* // Get you the client for a local deployment of Data API
45-
* DataAPIClient devClient = DataAPIClients.localClient();
43+
* DataAPIClient devClient = DataAPIClients.local();
4644
*
4745
* // Get you the database for a local deployment of Data API
48-
* DataAPIClient devClient = DataAPIClients.localDatabase();
46+
* DataAPIClient devClient = DataAPIClients.astraDev("token");
4947
*
5048
* // Default target environment Astra Production
5149
* DataAPIClient devClient = DataAPIClients.astra("token");
@@ -66,23 +64,92 @@ public class DataAPIClients {
6664
private DataAPIClients() {}
6765

6866
/**
69-
* Creates and configures a {@link DataAPIClient} for interaction with a local instance of Stargate, a
70-
* data gateway for working with Apache Cassandra®. This method is specifically designed for scenarios
71-
* where the application is intended to communicate with a Stargate instance running locally, facilitating
72-
* development and testing workflows by providing easy access to local database resources.
67+
* Creates and configures a {@link DataAPIClient} for interaction with a local instance of DataAPI,
68+
* a data gateway that facilitates working with Apache Cassandra®. This method is tailored for
69+
* development and testing workflows, enabling simplified and efficient access to local database
70+
* resources without the need for extensive configuration.
7371
*
74-
* @return A fully configured {@link DataAPIClient} ready for interacting with the local Stargate instance, equipped
75-
* with the necessary authentication token and targeting options for Cassandra. This client abstracts away
76-
* the complexities of direct database communication, providing a simplified interface for data operations.
72+
* <p>The returned {@link DataAPIClient} is preconfigured with:
73+
* <ul>
74+
* <li>An authentication token from {@link UsernamePasswordTokenProvider}.</li>
75+
* <li>A destination set to {@code DataAPIDestination.CASSANDRA}.</li>
76+
* <li>Feature flags for tables enabled.</li>
77+
* <li>Request logging enabled.</li>
78+
* </ul>
79+
*
80+
* @return A fully configured {@link DataAPIClient} ready for interacting with the local DataAPI instance.
81+
* This client provides a streamlined interface for executing data operations, abstracting away
82+
* the complexity of direct database interactions.
83+
*
84+
* <p>Example usage:</p>
85+
* <pre>
86+
* {@code
87+
* DataAPIClient client = DataAPIClients.local();
88+
* }
89+
* </pre>
7790
*/
78-
public static DataAPIClient local() {
91+
public static DataAPIClient clientCassandra() {
92+
return clientCassandra(
93+
UsernamePasswordTokenProvider.DEFAULT_USERNAME,
94+
UsernamePasswordTokenProvider.DEFAULT_CREDENTIALS);
95+
}
96+
97+
public static DataAPIClient clientCassandra(String username, String password) {
7998
return new DataAPIClient(
80-
new UsernamePasswordTokenProvider().getToken(),
99+
new UsernamePasswordTokenProvider(username, password).getToken(),
81100
new DataAPIClientOptions()
82101
.destination(DataAPIDestination.CASSANDRA)
83102
.enableFeatureFlagTables()
84-
.logRequests()
85-
.addObserver(new LoggingCommandObserver(DataAPIClient.class)));
103+
.logRequests());
104+
}
105+
106+
public static DataAPIClient clientHCD() {
107+
return clientHCD(
108+
UsernamePasswordTokenProvider.DEFAULT_USERNAME,
109+
UsernamePasswordTokenProvider.DEFAULT_CREDENTIALS);
110+
}
111+
112+
public static DataAPIClient clientHCD(String username, String password) {
113+
return new DataAPIClient(
114+
new UsernamePasswordTokenProvider(username, password).getToken(),
115+
new DataAPIClientOptions()
116+
.destination(DataAPIDestination.HCD)
117+
.enableFeatureFlagTables()
118+
.logRequests());
119+
}
120+
121+
/**
122+
* Creates and configures a {@link Database} client specifically designed for interaction with a local instance
123+
* of the Data API and Cassandra. This method simplifies the setup process by combining the creation of a {@link DataAPIClient}
124+
* with the integration of a {@link Database} abstraction. It is tailored for local development and testing,
125+
* enabling seamless interaction with Apache Cassandra® through Stargate with minimal configuration.
126+
*
127+
* <p>Upon creation, this method ensures that a default keyspace is available in the local Stargate instance
128+
* by automatically invoking {@link com.datastax.astra.client.admin.DatabaseAdmin#createKeyspace(String)}. This guarantees that developers
129+
* have a ready-to-use environment for executing database operations during their development or testing workflows.
130+
*
131+
* <p>The returned {@link Database} client is preconfigured with:
132+
* <ul>
133+
* <li>A connection to the default local Stargate endpoint.</li>
134+
* <li>An automatically created keyspace, identified by {@code DEFAULT_KEYSPACE}.</li>
135+
* </ul>
136+
* This setup allows developers to focus on application logic rather than database configuration or connectivity.
137+
*
138+
* @return A {@link Database} client configured for use with a local Stargate instance, including a default
139+
* keyspace for immediate interaction. This client abstracts database connectivity and administrative tasks,
140+
* streamlining development workflows.
141+
*
142+
* <p>Example usage:</p>
143+
* <pre>
144+
* {@code
145+
* Database db = localDbWithDefaultKeyspace();
146+
* }
147+
* </pre>
148+
*/
149+
public static Database localDbWithDefaultKeyspace() {
150+
Database db = clientCassandra().getDatabase(DEFAULT_ENDPOINT_LOCAL);
151+
db.getDatabaseAdmin().createKeyspace(DEFAULT_KEYSPACE);
152+
return db;
86153
}
87154

88155
/**
@@ -105,14 +172,14 @@ public static DataAPIClient local() {
105172
* }
106173
* </pre>
107174
*/
108-
public static DataAPIClient astra(String token) {
175+
public static DataAPIClient astraDev(String token) {
109176
return new DataAPIClient(token, new DataAPIClientOptions()
110-
.destination(DataAPIDestination.ASTRA)
111-
.addObserver(new LoggingCommandObserver(DataAPIClient.class)));
177+
.destination(DataAPIDestination.ASTRA_DEV)
178+
.logRequests());
112179
}
113180

114181
/**
115-
* Creates a {@link DataAPIClient} configured for interacting with Astra in a development environment. This
182+
* Creates a {@link DataAPIClient} configured for interacting with Astra in a production environment. This
116183
* method simplifies the setup of a client specifically tailored for development purposes, where you might
117184
* need different configurations or less stringent security measures compared to a production environment.
118185
* The client is configured to target Astra's development environment, ensuring that operations do not
@@ -126,59 +193,52 @@ public static DataAPIClient astra(String token) {
126193
* <p>Example usage:</p>
127194
* <pre>
128195
* {@code
129-
* DataAPIClient devClient = DataAPIClients.astraDev("your_astra_dev_token");
196+
* DataAPIClient devClient = DataAPIClients.astra("your_astra_dev_token");
130197
* // Utilize devClient for development database operations
131198
* }
132199
* </pre>
133200
*/
134-
public static DataAPIClient astraDev(String token) {
201+
public static DataAPIClient astra(String token) {
135202
return new DataAPIClient(token, new DataAPIClientOptions()
136-
.destination(DataAPIDestination.ASTRA_DEV)
137-
.addObserver(new LoggingCommandObserver(DataAPIClient.class)));
203+
.destination(DataAPIDestination.ASTRA)
204+
.logRequests());
138205
}
139206

207+
208+
140209
/**
141210
* Creates a {@link DataAPIClient} specifically configured for interacting with Astra in a test environment.
142-
* This setup is ideal for testing scenarios, where isolation from development and production environments
143-
* is critical to ensure the integrity and stability of test results. By directing the client to Astra's
144-
* test environment, it facilitates safe, isolated testing of database interactions without risking the
145-
* alteration of development or production data.
211+
* This method is designed for testing scenarios, providing an isolated environment to safely execute
212+
* database operations without impacting development or production data.
213+
*
214+
* <p>The returned {@link DataAPIClient} is preconfigured to:
215+
* <ul>
216+
* <li>Authenticate using the provided test-specific token.</li>
217+
* <li>Target the {@code DataAPIDestination.ASTRA_TEST} environment.</li>
218+
* <li>Enable request logging for better visibility during test operations.</li>
219+
* </ul>
146220
*
147-
* @param token The authentication token required for accessing Astra's test environment. Ensure that this
148-
* token is designated for testing purposes to prevent unintended access to or effects on
149-
* non-test data and resources.
150-
* @return A {@link DataAPIClient} instance specifically for use in testing scenarios with Astra, equipped
151-
* with the necessary authentication token and configured to target the test environment.
221+
* This setup ensures that all database interactions are restricted to Astra's test environment,
222+
* preserving the integrity of other environments while facilitating thorough testing.
223+
*
224+
* @param token The authentication token required for accessing Astra's test environment. It is important
225+
* to use a token that is explicitly designated for testing purposes to avoid unintended
226+
* access to production or development resources.
227+
* @return A {@link DataAPIClient} instance configured for testing with Astra, equipped with the provided
228+
* authentication token and targeting the test environment.
152229
*
153230
* <p>Example usage:</p>
154231
* <pre>
155232
* {@code
156233
* DataAPIClient testClient = DataAPIClients.astraTest("your_astra_test_token");
157-
* // Execute test database operations with testClient
234+
* testClient.execute(query -> query.cql("SELECT * FROM test_table").execute());
158235
* }
159236
* </pre>
160237
*/
161238
public static DataAPIClient astraTest(String token) {
162239
return new DataAPIClient(token, new DataAPIClientOptions()
163240
.destination(DataAPIDestination.ASTRA_TEST)
164-
.addObserver(new LoggingCommandObserver(DataAPIClient.class)));
165-
}
166-
167-
/**
168-
* Creates and configures a {@link Database} client specifically designed for interaction with a local instance
169-
* of Stargate. This method streamlines the process of setting up a client for local database interactions,
170-
* encapsulating both the creation of a {@link DataAPIClient} and its integration within a {@link Database}
171-
* abstraction. This setup is ideal for local development and testing, providing a straightforward path to
172-
* interact with Cassandra through Stargate with minimal setup.
173-
*
174-
* @return A {@link Database} client ready for use with a local Stargate instance, fully configured for immediate
175-
* interaction with the database. This client enables developers to focus on their application logic rather
176-
* than the intricacies of database connectivity and command execution.
177-
*/
178-
public static Database defaultLocalDatabase() {
179-
Database db = local().getDatabase(DEFAULT_ENDPOINT_LOCAL);
180-
db.getDatabaseAdmin().createKeyspace(DEFAULT_KEYSPACE);
181-
return db;
241+
.logRequests());
182242
}
183243

184244
}

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,24 @@
2323
import com.datastax.astra.client.core.commands.BaseOptions;
2424
import com.datastax.astra.client.core.commands.CommandType;
2525
import com.datastax.astra.client.core.options.DataAPIClientOptions;
26+
import com.datastax.astra.internal.serdes.DataAPISerializer;
2627
import com.datastax.astra.internal.serdes.DatabaseSerializer;
27-
import com.datastax.astra.internal.utils.Assert;
2828
import lombok.Setter;
2929
import lombok.experimental.Accessors;
3030

31-
import static com.datastax.astra.client.databases.Database.DEFAULT_DATABASE_SERIALIZER;
32-
3331
@Setter
3432
@Accessors(fluent = true, chain = true)
3533
public class AdminOptions extends BaseOptions<AdminOptions> {
3634

35+
/** Serializer for the Collections. */
36+
private static final DataAPISerializer DEFAULT_SERIALIZER = new DatabaseSerializer();
37+
3738
public AdminOptions() {
3839
this(null, null);
3940
}
4041

4142
public AdminOptions(String token, DataAPIClientOptions options) {
42-
super(token, CommandType.DATABASE_ADMIN, DEFAULT_DATABASE_SERIALIZER, options);
43+
super(token, CommandType.DATABASE_ADMIN, DEFAULT_SERIALIZER, options);
4344
}
4445

4546
}

astra-db-java/src/main/java/com/datastax/astra/client/core/commands/BaseOptions.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@
2626
import com.datastax.astra.client.core.options.TimeoutOptions;
2727
import com.datastax.astra.internal.command.CommandObserver;
2828
import com.datastax.astra.internal.serdes.DataAPISerializer;
29+
import com.datastax.astra.internal.utils.Assert;
2930
import com.fasterxml.jackson.annotation.JsonIgnore;
3031
import lombok.NoArgsConstructor;
3132

33+
import java.time.Duration;
3234
import java.util.Map;
3335

3436
/**
@@ -130,6 +132,19 @@ public T timeout(long timeoutMillis) {
130132
return timeout(timeoutMillis, getCommandType());
131133
}
132134

135+
/**
136+
* Provide the command type. The nature of the command will determine the timeout.
137+
*
138+
* @param duration
139+
* timeout for the request
140+
* @return
141+
* service key
142+
*/
143+
public T timeout(Duration duration) {
144+
Assert.notNull(duration, "duration");
145+
return timeout(duration.toMillis(), getCommandType());
146+
}
147+
133148
// --------------------------------------------
134149
// ----- Setters -----
135150
// --------------------------------------------

0 commit comments

Comments
 (0)