Skip to content

Commit c176d57

Browse files
committed
Introduce CommandOptions and Options for all methods
1 parent 89385ec commit c176d57

File tree

60 files changed

+1513
-644
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+1513
-644
lines changed

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

Lines changed: 299 additions & 119 deletions
Large diffs are not rendered by default.

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
*/
5656
public class DataAPIClients {
5757

58-
/** Default Http endpoint for local deployment (http://localhost:8181). */
58+
/** Default Http endpoint for local deployment. */
5959
public static final String DEFAULT_ENDPOINT_LOCAL = "http://localhost:8181";
6060

6161
/**
@@ -76,7 +76,10 @@ private DataAPIClients() {}
7676
public static DataAPIClient createForLocal() {
7777
return new DataAPIClient(
7878
new TokenProviderStargateV2().getToken(),
79-
DataAPIOptions.builder().withDestination(DataAPIOptions.DataAPIDestination.CASSANDRA).build());
79+
DataAPIOptions.builder()
80+
.withDestination(DataAPIOptions.DataAPIDestination.CASSANDRA)
81+
.withObserver(new LoggingCommandObserver(DataAPIClient.class))
82+
.build());
8083
}
8184

8285
/**
@@ -92,9 +95,7 @@ public static DataAPIClient createForLocal() {
9295
*/
9396
public static Database createDefaultLocalDatabase() {
9497
Database db = createForLocal().getDatabase(DEFAULT_ENDPOINT_LOCAL, DEFAULT_NAMESPACE);
95-
db.registerListener("logger", new LoggingCommandObserver(Database.class));
9698
DataAPIDatabaseAdmin dbAdmin = (DataAPIDatabaseAdmin) db.getDatabaseAdmin();
97-
dbAdmin.registerListener("logger", new LoggingCommandObserver(Database.class));
9899
dbAdmin.createNamespace(DEFAULT_NAMESPACE);
99100
return db;
100101
}
@@ -153,6 +154,7 @@ public static DataAPIClient createForAstraDev(String token) {
153154
return new DataAPIClient(token, DataAPIOptions
154155
.builder()
155156
.withDestination(DataAPIOptions.DataAPIDestination.ASTRA_DEV)
157+
.withObserver(new LoggingCommandObserver(DataAPIClient.class))
156158
.build());
157159
}
158160

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

Lines changed: 79 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,17 @@
2020
* #L%
2121
*/
2222

23+
import com.datastax.astra.client.model.HttpClientOptions;
24+
import com.datastax.astra.internal.command.CommandObserver;
25+
import com.datastax.astra.internal.command.LoggingCommandObserver;
2326
import com.datastax.astra.internal.utils.Assert;
2427
import lombok.Getter;
2528
import lombok.Setter;
2629
import lombok.extern.slf4j.Slf4j;
2730

2831
import java.net.http.HttpClient;
32+
import java.util.Map;
33+
import java.util.TreeMap;
2934

3035
/**
3136
* Options to set up the client for DataApiClient.
@@ -52,10 +57,10 @@ public class DataAPIOptions {
5257
DataAPIOptions.class.getPackage().getImplementationVersion() : "dev";
5358

5459
/** Default timeout for initiating connection. */
55-
public static final int DEFAULT_CONNECT_TIMEOUT_SECONDS = 20;
60+
public static final int DEFAULT_CONNECT_TIMEOUT_MILLIS_SECONDS = 20;
5661

5762
/** Default timeout for initiating connection. */
58-
public static final int DEFAULT_REQUEST_TIMEOUT_SECONDS = 20;
63+
public static final int DEFAULT_REQUEST_TIMEOUT_MILLIS_SECONDS = 20000;
5964

6065
/** Default retry count. */
6166
public static final int DEFAULT_RETRY_COUNT = 3;
@@ -84,6 +89,11 @@ public class DataAPIOptions {
8489
/** The maximum number of documents that can be inserted in a single operation. */
8590
final int maxDocumentsInInsert;
8691

92+
/** The maximum number of documents that can be inserted in a single operation. */
93+
final String embeddingAPIKey;
94+
95+
final Map<String, CommandObserver> observers;
96+
8797
/**
8898
* Initializer for the builder.
8999
*
@@ -106,15 +116,17 @@ private DataAPIOptions(DataAPIClientOptionsBuilder builder) {
106116
this.maxDocumentCount = builder.maxDocumentCount;
107117
this.maxPageSize = builder.maxPageSize;
108118
this.maxDocumentsInInsert = builder.maxDocumentsInInsert;
119+
this.embeddingAPIKey = builder.embeddingAPIKey;
109120
this.httpClientOptions = new HttpClientOptions();
121+
this.observers = builder.observers;
110122
httpClientOptions.setHttpVersion(builder.httpVersion);
111123
httpClientOptions.setHttpRedirect(builder.httpRedirect);
112124
httpClientOptions.setRetryCount(builder.retryCount);
113125
httpClientOptions.setRetryDelay(builder.retryDelay);
114126
httpClientOptions.setUserAgentCallerName(builder.userAgentCallerName);
115127
httpClientOptions.setUserAgentCallerVersion(builder.userAgentCallerVersion);
116-
httpClientOptions.setConnectionRequestTimeoutInSeconds(builder.httpRequestTimeout);
117-
httpClientOptions.setResponseTimeoutInSeconds(builder.httpConnectTimeout);
128+
httpClientOptions.setConnectionRequestTimeoutInSeconds(builder.httpConnectTimeout);
129+
httpClientOptions.setMaxTimeMS(builder.maxTimeMS);
118130
httpClientOptions.setProxy(builder.httpProxy);
119131
}
120132

@@ -154,64 +166,6 @@ public enum DataAPIDestination {
154166
OTHERS
155167
}
156168

157-
/**
158-
* Options to set up http Client.
159-
*/
160-
@Getter @Setter
161-
public static class HttpClientOptions {
162-
163-
/** Default user agent. */
164-
public static final String DEFAULT_USER_AGENT = "stargate-sdk";
165-
166-
/** Default timeout for initiating connection. */
167-
public static final int DEFAULT_CONNECT_TIMEOUT_SECONDS = 20;
168-
169-
/** Default timeout for initiating connection. */
170-
public static final int DEFAULT_REQUEST_TIMEOUT_SECONDS = 20;
171-
172-
/** Default retry count. */
173-
public static final int DEFAULT_RETRY_COUNT = 3;
174-
175-
/** Default retry delay. */
176-
public static final int DEFAULT_RETRY_DELAY_MILLIS = 100;
177-
178-
/** Caller name in User agent. */
179-
String userAgentCallerName = DEFAULT_USER_AGENT;
180-
181-
/** Caller version in User agent. */
182-
String userAgentCallerVersion = HttpClientOptions.class.getPackage().getImplementationVersion() != null ?
183-
HttpClientOptions.class.getPackage().getImplementationVersion() : "dev";
184-
185-
/** Http Connection timeout. */
186-
long connectionRequestTimeoutInSeconds = DEFAULT_CONNECT_TIMEOUT_SECONDS;
187-
188-
/** Http Connection timeout. */
189-
long responseTimeoutInSeconds = DEFAULT_REQUEST_TIMEOUT_SECONDS;
190-
191-
/** Enable retry count. */
192-
int retryCount = DEFAULT_RETRY_COUNT;
193-
194-
/** How much to wait in between 2 calls. */
195-
int retryDelay = DEFAULT_RETRY_DELAY_MILLIS;
196-
197-
/** The http client could work through a proxy. */
198-
HttpProxy proxy;
199-
200-
/** Moving to HTTP/2. */
201-
HttpClient.Version httpVersion = HttpClient.Version.HTTP_2;
202-
203-
/** Redirect */
204-
HttpClient.Redirect httpRedirect = HttpClient.Redirect.NORMAL;
205-
206-
/**
207-
* Default constructor.
208-
*/
209-
public HttpClientOptions() {
210-
// left blanks as default values are set
211-
}
212-
213-
}
214-
215169
/**
216170
* Subclass to represent an http proxy.
217171
*/
@@ -254,10 +208,10 @@ public static class DataAPIClientOptionsBuilder {
254208
private String userAgentCallerVersion = DEFAULT_CALLER_VERSION;
255209

256210
/** Http Connection timeout. */
257-
private long httpRequestTimeout = DEFAULT_CONNECT_TIMEOUT_SECONDS;
211+
private long maxTimeMS = DEFAULT_CONNECT_TIMEOUT_MILLIS_SECONDS;
258212

259213
/** Http Connection timeout. */
260-
private long httpConnectTimeout = DEFAULT_REQUEST_TIMEOUT_SECONDS;
214+
private long httpConnectTimeout = DEFAULT_REQUEST_TIMEOUT_MILLIS_SECONDS;
261215

262216
/** Enable retry count. */
263217
private int retryCount = DEFAULT_RETRY_COUNT;
@@ -286,6 +240,12 @@ public static class DataAPIClientOptionsBuilder {
286240
/** The maximum number of documents that can be inserted in a single operation. */
287241
private int maxDocumentsInInsert = DEFAULT_MAX_CHUNKSIZE;
288242

243+
/** The embedding service API key can be provided at top level. */
244+
private String embeddingAPIKey;
245+
246+
/** Observers for the commands. */
247+
private final Map<String, CommandObserver> observers = new TreeMap<>();
248+
289249
/**
290250
* Default constructor.
291251
*/
@@ -295,7 +255,7 @@ public DataAPIClientOptionsBuilder() {
295255

296256
/**
297257
* Builder pattern, update caller information.
298-
*
258+
*o
299259
* @param callerName
300260
* caller name in the user agent
301261
* @param callerVersion
@@ -376,15 +336,28 @@ public DataAPIClientOptionsBuilder withHttpRetryCount(int retryCount) {
376336
}
377337

378338
/**
379-
* Builder pattern, update http connection Timeout
339+
* Builder pattern, update http request Timeout in millis
380340
*
381341
* @param connectTimeout
382-
* http connection timeout
342+
* http request timeout in millis
383343
* @return
384344
* self reference
385345
*/
386-
public DataAPIClientOptionsBuilder withHttpConnectTimeout(int connectTimeout) {
387-
this.httpRequestTimeout = connectTimeout;
346+
public DataAPIClientOptionsBuilder withMaxTimeMS(long connectTimeout) {
347+
this.maxTimeMS = connectTimeout;
348+
return this;
349+
}
350+
351+
/**
352+
* Builder pattern, update http connection Timeout
353+
*
354+
* @param embeddingAPIKey
355+
* embedding API Key
356+
* @return
357+
* self reference
358+
*/
359+
public DataAPIClientOptionsBuilder withEmbeddingAPIKey(String embeddingAPIKey) {
360+
this.embeddingAPIKey = embeddingAPIKey;
388361
return this;
389362
}
390363

@@ -396,7 +369,7 @@ public DataAPIClientOptionsBuilder withHttpConnectTimeout(int connectTimeout) {
396369
* @return
397370
* self reference
398371
*/
399-
public DataAPIClientOptionsBuilder withHttpRequestTimeout(int requestTimeout) {
372+
public DataAPIClientOptionsBuilder withHttpConnectTimeout(int requestTimeout) {
400373
this.httpConnectTimeout = requestTimeout;
401374
return this;
402375
}
@@ -540,6 +513,42 @@ public DataAPIClientOptionsBuilder withMaxDocumentsInInsert(int maxDocumentsInIn
540513
return this;
541514
}
542515

516+
/**
517+
* Allow to register a listener for the command.
518+
* @param name
519+
* name of the observer
520+
* @param observer
521+
* observer to register
522+
* @return
523+
* instance of the command options
524+
*/
525+
public DataAPIClientOptionsBuilder withObserver(String name, CommandObserver observer) {
526+
observers.put(name, observer);
527+
return this;
528+
}
529+
530+
/**
531+
* Register an observer with its className.
532+
*
533+
* @param observer
534+
* command observer
535+
* @return
536+
* instance of the command options
537+
*/
538+
public DataAPIClientOptionsBuilder withObserver(CommandObserver observer) {
539+
return withObserver(observer.getClass().getSimpleName(), observer);
540+
}
541+
542+
/**
543+
* Help to enable loggin requests.
544+
*
545+
* @return current reference
546+
*
547+
*/
548+
public DataAPIClientOptionsBuilder logRequests() {
549+
return withObserver(new LoggingCommandObserver(DataAPIClient.class));
550+
}
551+
543552
/**
544553
* Build the options.
545554
*

0 commit comments

Comments
 (0)