Skip to content

Commit aaff8b6

Browse files
committed
code qualit
1 parent 1b4d4d7 commit aaff8b6

File tree

88 files changed

+871
-948
lines changed

Some content is hidden

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

88 files changed

+871
-948
lines changed

README.MD

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,26 @@
22
# Java Client for Data API
33

44
[![License Apache2](https://img.shields.io/hexpm/l/plug.svg)](http://www.apache.org/licenses/LICENSE-2.0)
5-
[![Lines of Code](https://sonarcloud.io/api/project_badges/measure?project=clun_astra-db-java&metric=ncloc)](https://sonarcloud.io/summary/new_code?id=clun_astra-db-java)
5+
[![Maven Central](https://img.shields.io/maven-central/v/com.datastax.astra/astra-db-java)](https://search.maven.org/artifact/com.datastax.astra/astra-db-java)
66

77
[![Security Rating](https://sonarcloud.io/api/project_badges/measure?project=clun_astra-db-java&metric=security_rating)](https://sonarcloud.io/summary/new_code?id=clun_db-java)
88
[![Maintainability Rating](https://sonarcloud.io/api/project_badges/measure?project=clun_astra-db-java&metric=sqale_rating)](https://sonarcloud.io/summary/overall?id=clun_astra-db-java)
99
[![Reliability Rating](https://sonarcloud.io/api/project_badges/measure?project=clun_astra-db-java&metric=reliability_rating)](https://sonarcloud.io/summary/new_code?id=clun_astra-db-java)
1010

1111
[![Bugs](https://sonarcloud.io/api/project_badges/measure?project=clun_astra-db-java&metric=bugs)](https://sonarcloud.io/summary/new_code?id=clun_astra-db-java)
1212
[![Vulnerabilities](https://sonarcloud.io/api/project_badges/measure?project=clun_astra-db-java&metric=vulnerabilities)](https://sonarcloud.io/summary/overall?id=clun_astra-db-java)
13-
![GitHub issues](https://img.shields.io/github/issues/datastax/astra-db-java)
1413
[![Duplicated Lines (%)](https://sonarcloud.io/api/project_badges/measure?project=clun_astra-db-java&metric=duplicated_lines_density)](https://sonarcloud.io/summary/new_code?id=clun_astra-db-java)
1514

15+
[![Lines of Code](https://sonarcloud.io/api/project_badges/measure?project=clun_astra-db-java&metric=ncloc)](https://sonarcloud.io/summary/new_code?id=clun_astra-db-java)
1616
[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=clun_astra-db-java&metric=coverage)](https://sonarcloud.io/summary/new_code?id=clun_astra-db-java)
1717

1818

19-
[![Maven Central](https://img.shields.io/maven-central/v/com.datastax.astra/astra-db-java)](https://search.maven.org/artifact/com.datastax.astra/astra-db-java)
20-
21-
22-
2319
This client library provides a simplified way to interact with Data API for AstraDB or local instances. For detailed documentation, each operation comes with a detailed description and examples.
2420

2521
- [`astra-db-ts`](https://github.com/datastax/astra-db-ts) is the equivalent for typescript
2622
- [`astrapy`](https://github.com/datastax/astrapy) is the equivalent in python
2723

2824
This library is under development and not yet available in Maven Central. You can build it locally and install it in your local repository.
29-
![Latest Release](https://img.shields.io/github/v/release/datastax/astra-db-java)
3025

3126

3227
## 1. Local Installation

astra-db-java/pom.xml

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,17 @@
1717
<!-- Sonar -->
1818
<sonar.organization>clun-datastax</sonar.organization>
1919
<sonar.host.url>https://sonarcloud.io</sonar.host.url>
20+
<required-maven.version>3.6.3</required-maven.version>
2021

2122
<!-- Third Party Libraries -->
2223
<devops-sdk.version>1.2.7</devops-sdk.version>
2324
<slf4j.version>2.0.9</slf4j.version>
24-
<logback.version>1.5.0</logback.version>
25-
<jackson.version>2.16.2</jackson.version>
25+
<logback.version>1.5.3</logback.version>
26+
<jackson.version>2.17.0</jackson.version>
2627
<lombok.version>1.18.32</lombok.version>
2728
<retry4j.version>0.15.0</retry4j.version>
29+
<awaitility.version>4.2.1</awaitility.version>
30+
<mockwebserver.version>4.12.0</mockwebserver.version>
2831
<uuid-generator.version>5.0.0</uuid-generator.version>
2932

3033
<!-- Test -->
@@ -35,19 +38,20 @@
3538
<!-- Maven -->
3639
<maven.plugin.compiler.source>11</maven.plugin.compiler.source>
3740
<maven.plugin.compiler.target>11</maven.plugin.compiler.target>
38-
<version.maven.plugin.compiler>3.11.0</version.maven.plugin.compiler>
41+
<version.maven.plugin.compiler>3.13.0</version.maven.plugin.compiler>
3942
<version.maven.plugin.coveralls>4.3.0</version.maven.plugin.coveralls>
40-
<version.maven.plugin.dependency>3.6.0</version.maven.plugin.dependency>
41-
<version.maven.plugin.gpg>3.1.0</version.maven.plugin.gpg>
43+
<version.maven.plugin.dependency>3.6.1</version.maven.plugin.dependency>
44+
<version.maven.plugin.enforcer>3.4.1</version.maven.plugin.enforcer>
45+
<version.maven.plugin.gpg>3.2.2</version.maven.plugin.gpg>
4246
<version.maven.plugin.jacoco>0.8.11</version.maven.plugin.jacoco>
43-
<version.maven.plugin.javadoc>3.5.0</version.maven.plugin.javadoc>
47+
<version.maven.plugin.javadoc>3.6.3</version.maven.plugin.javadoc>
4448
<version.maven.plugin.jar>3.3.0</version.maven.plugin.jar>
45-
<version.maven.plugin.license>2.3.0</version.maven.plugin.license>
49+
<version.maven.plugin.license>2.4.0</version.maven.plugin.license>
4650
<version.maven.plugin.nexus>1.6.13</version.maven.plugin.nexus>
4751
<version.maven.plugin.release>3.0.1</version.maven.plugin.release>
4852
<version.maven.plugin.resources>3.3.1</version.maven.plugin.resources>
4953
<version.maven.plugin.source>3.3.0</version.maven.plugin.source>
50-
<version.maven.plugin.surefire>3.1.2</version.maven.plugin.surefire>
54+
<version.maven.plugin.surefire>3.2.5</version.maven.plugin.surefire>
5155

5256
</properties>
5357

@@ -132,13 +136,19 @@
132136
<version>${assertj.version}</version>
133137
<scope>test</scope>
134138
</dependency>
135-
136139
<dependency>
137140
<groupId>com.squareup.okhttp3</groupId>
138141
<artifactId>mockwebserver</artifactId>
139-
<version>4.12.0</version>
142+
<version>${mockwebserver.version}</version>
140143
<scope>test</scope>
141144
</dependency>
145+
<dependency>
146+
<groupId>org.awaitility</groupId>
147+
<artifactId>awaitility</artifactId>
148+
<version>${awaitility.version}</version>
149+
<scope>test</scope>
150+
</dependency>
151+
142152

143153
</dependencies>
144154

@@ -203,6 +213,27 @@
203213
</configuration>
204214
</plugin>
205215

216+
<plugin>
217+
<groupId>org.apache.maven.plugins</groupId>
218+
<artifactId>maven-enforcer-plugin</artifactId>
219+
<version>${version.maven.plugin.enforcer}</version>
220+
<executions>
221+
<execution>
222+
<id>enforce-maven</id>
223+
<goals>
224+
<goal>enforce</goal>
225+
</goals>
226+
<configuration>
227+
<rules>
228+
<requireMavenVersion>
229+
<version>${required-maven.version}</version>
230+
</requireMavenVersion>
231+
</rules>
232+
</configuration>
233+
</execution>
234+
</executions>
235+
</plugin>
236+
206237
<plugin>
207238
<groupId>org.apache.maven.plugins</groupId>
208239
<artifactId>maven-surefire-plugin</artifactId>

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

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@
2929
import com.datastax.astra.client.model.CollectionInfo;
3030
import com.datastax.astra.client.model.CollectionOptions;
3131
import com.datastax.astra.client.model.Command;
32+
import com.datastax.astra.client.model.DataAPIKeywords;
3233
import com.datastax.astra.client.model.DeleteOneOptions;
3334
import com.datastax.astra.client.model.DeleteResult;
3435
import com.datastax.astra.client.model.DistinctIterable;
3536
import com.datastax.astra.client.model.Document;
3637
import com.datastax.astra.client.model.Filter;
37-
import com.datastax.astra.client.model.DataAPIKeywords;
3838
import com.datastax.astra.client.model.Filters;
3939
import com.datastax.astra.client.model.FindIterable;
4040
import com.datastax.astra.client.model.FindOneAndDeleteOptions;
@@ -81,7 +81,8 @@
8181
import java.util.concurrent.atomic.AtomicInteger;
8282
import java.util.stream.Collectors;
8383

84-
import static com.datastax.astra.client.model.CollectionIdTypes.UUIDV6;
84+
import static com.datastax.astra.client.exception.DataApiException.ERROR_CODE_INTERRUPTED;
85+
import static com.datastax.astra.client.exception.DataApiException.ERROR_CODE_TIMEOUT;
8586
import static com.datastax.astra.internal.utils.AnsiUtils.cyan;
8687
import static com.datastax.astra.internal.utils.AnsiUtils.green;
8788
import static com.datastax.astra.internal.utils.AnsiUtils.magenta;
@@ -408,7 +409,7 @@ public String getName() {
408409
*/
409410
public final InsertOneResult insertOne(T document) {
410411
Assert.notNull(document, DOCUMENT);
411-
return _insertOne(JsonUtils.convertValue(document, Document.class));
412+
return internalInsertOne(JsonUtils.convertValue(document, Document.class));
412413
}
413414

414415
/**
@@ -482,7 +483,7 @@ public final CompletableFuture<InsertOneResult> insertOneAsync(T document) {
482483
public final InsertOneResult insertOne(T document, float[] embeddings) {
483484
Assert.notNull(document, DOCUMENT);
484485
Assert.notNull(embeddings, ARG_EMBEDDINGS);
485-
return _insertOne(JsonUtils.convertValue(document, Document.class).vector(embeddings));
486+
return internalInsertOne(JsonUtils.convertValue(document, Document.class).vector(embeddings));
486487
}
487488

488489
/**
@@ -564,7 +565,7 @@ public final CompletableFuture<InsertOneResult> insertOneAsync(T document, float
564565
public final InsertOneResult insertOne(T document, String vectorize) {
565566
Assert.notNull(document, DOCUMENT);
566567
Assert.hasLength(vectorize, ARG_VECTORIZE);
567-
return _insertOne(JsonUtils.convertValue(document, Document.class).vectorize(vectorize));
568+
return internalInsertOne(JsonUtils.convertValue(document, Document.class).vectorize(vectorize));
568569
}
569570

570571
/**
@@ -617,7 +618,7 @@ public final CompletableFuture<InsertOneResult> insertOneAsync(T document, Strin
617618
* @return
618619
* object wrapping the returned identifier
619620
*/
620-
private InsertOneResult _insertOne(Document document) {
621+
private InsertOneResult internalInsertOne(Document document) {
621622
Assert.notNull(document, DOCUMENT);
622623
Command insertOne = Command.create("insertOne").withDocument(document);
623624
Object documentId = runCommand(insertOne)
@@ -753,16 +754,14 @@ public InsertManyResult insertMany(List<? extends T> documents, InsertManyOption
753754
log.debug(magenta(".[total insertMany.responseTime]") + "=" + yellow("{}") + " millis.",
754755
System.currentTimeMillis() - start);
755756
} else {
756-
throw new TimeoutException("Request did not complete withing ");
757+
throw new DataApiException(ERROR_CODE_TIMEOUT, "Request did not complete withing ");
757758
}
758759
} catch (InterruptedException | ExecutionException e) {
759760
if (e.getCause() instanceof DataApiException) {
760761
throw (DataApiException) e.getCause();
761762
}
762763
Thread.currentThread().interrupt();
763-
throw new RuntimeException("Thread was interrupted while waiting", e);
764-
} catch (TimeoutException e) {
765-
throw new RuntimeException("Operation timed out", e);
764+
throw new DataApiException(ERROR_CODE_INTERRUPTED, "Thread was interrupted while waiting", e);
766765
}
767766
return finalResult;
768767
}
@@ -1347,7 +1346,8 @@ public int countDocuments(int upperBound) throws TooManyDocumentsToCountExceptio
13471346
* @throws TooManyDocumentsToCountException
13481347
* If the number of documents counted exceeds the provided limit.
13491348
*/
1350-
public int countDocuments(Filter filter, int upperBound) throws TooManyDocumentsToCountException {
1349+
public int countDocuments(Filter filter, int upperBound)
1350+
throws TooManyDocumentsToCountException {
13511351
// Argument Validation
13521352
if (upperBound<1 || upperBound> DataAPIOptions.getMaxDocumentCount()) {
13531353
throw new IllegalArgumentException("UpperBound limit should be in between 1 and " + DataAPIOptions.getMaxDocumentCount());
@@ -1762,7 +1762,6 @@ public UpdateResult updateMany(Filter filter, Update update) {
17621762
public UpdateResult updateMany(Filter filter, Update update, UpdateManyOptions options) {
17631763
notNull(update, ARG_UPDATE);
17641764
notNull(options, ARG_OPTIONS);
1765-
boolean moreData = true;
17661765
String nextPageState = null;
17671766
UpdateResult result = new UpdateResult();
17681767
result.setMatchedCount(0);
@@ -1866,12 +1865,13 @@ public BulkWriteResult bulkWrite(List<Command> commands, BulkWriteOptions option
18661865
if (options.getConcurrency() > 1 && options.isOrdered()) {
18671866
throw new IllegalArgumentException("Cannot run ordered bulk_write concurrently.");
18681867
}
1869-
BulkWriteResult result = new BulkWriteResult(0);
1870-
result = new BulkWriteResult(commands.size());
1868+
BulkWriteResult result = new BulkWriteResult(commands.size());
18711869
if (options.isOrdered()) {
1872-
result.setResponses(commands.stream().map(this::runCommand).collect(Collectors.toList()));
1870+
result.setResponses(commands
1871+
.stream()
1872+
.map(this::runCommand)
1873+
.collect(Collectors.toList()));
18731874
} else {
1874-
18751875
try {
18761876
ExecutorService executor = Executors.newFixedThreadPool(options.getConcurrency());
18771877
List<Future<ApiResponse>> futures = new ArrayList<>();

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

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,17 @@
7070
*/
7171
public class DataAPIClient {
7272

73+
/** parameters names. */
74+
private static final String ARG_NAMESPACE = "namespace";
75+
/** parameters names. */
76+
private static final String ARG_OPTIONS = "options";
77+
/** parameters names. */
78+
private static final String ARG_TOKEN = "token";
79+
/** parameters names. */
80+
private static final String ARG_DATABASE_ID = "databaseId";
81+
/** parameters names. */
82+
private static final String ARG_REGION = "region";
83+
7384
/**
7485
* The authentication token used as credentials in HTTP requests, specifically as the Authorization bearer token.
7586
* This token is crucial for accessing and interacting with Astra environments, where it plays a role in determining
@@ -177,8 +188,8 @@ public DataAPIClient(String token) {
177188
* @throws IllegalArgumentException if the token is empty or null, or if the options are null.
178189
*/
179190
public DataAPIClient(String token, DataAPIOptions options) {
180-
Assert.hasLength(token, "token");
181-
Assert.notNull(options, "options");
191+
Assert.hasLength(token, ARG_TOKEN);
192+
Assert.notNull(options, ARG_OPTIONS);
182193
this.token = token;
183194
this.options = options;
184195
}
@@ -323,8 +334,8 @@ private AstraEnvironment getAstraEnvironment() {
323334
* data manipulation and query operations.
324335
*/
325336
public Database getDatabase(UUID databaseId, String namespace) {
326-
Assert.notNull(databaseId, "databaseId");
327-
Assert.hasLength(namespace, "namespace");
337+
Assert.notNull(databaseId, ARG_DATABASE_ID);
338+
Assert.hasLength(namespace, ARG_NAMESPACE);
328339
return new Database(new AstraApiEndpoint(databaseId,
329340
getAdmin().getDatabaseInfo(databaseId).getRegion(),
330341
getAstraEnvironment()).getApiEndPoint(),
@@ -344,9 +355,9 @@ public Database getDatabase(UUID databaseId, String namespace) {
344355
* data manipulation and query operations.
345356
*/
346357
public Database getDatabase(UUID databaseId, String namespace, String region) {
347-
Assert.notNull(databaseId, "databaseId");
348-
Assert.hasLength(namespace, "namespace");
349-
Assert.hasLength(region, "namespace");
358+
Assert.notNull(databaseId, ARG_DATABASE_ID);
359+
Assert.hasLength(namespace, ARG_NAMESPACE);
360+
Assert.hasLength(region, ARG_REGION);
350361
return new Database(new AstraApiEndpoint(databaseId, region,
351362
getAstraEnvironment()).getApiEndPoint(),
352363
this.token, namespace);

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,7 @@ public static DataAPIClientOptionsBuilder builder() {
9292
private DataAPIOptions(DataAPIClientOptionsBuilder builder) {
9393
this.apiVersion = builder.apiVersion;
9494
this.destination = builder.destination;
95-
96-
HttpClientOptions httpClientOptions = new HttpClientOptions();
95+
this.httpClientOptions = new HttpClientOptions();
9796
httpClientOptions.setHttpVersion(builder.httpVersion);
9897
httpClientOptions.setHttpRedirect(builder.httpRedirect);
9998
httpClientOptions.setRetryCount(builder.retryCount);
@@ -103,7 +102,6 @@ private DataAPIOptions(DataAPIClientOptionsBuilder builder) {
103102
httpClientOptions.setConnectionRequestTimeoutInSeconds(builder.httpRequestTimeout);
104103
httpClientOptions.setResponseTimeoutInSeconds(builder.httpConnectTimeout);
105104
httpClientOptions.setProxy(builder.httpProxy);
106-
this.httpClientOptions = httpClientOptions;
107105
}
108106

109107
/**

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -246,12 +246,12 @@ public Collection<Document> getCollection(String collectionName) {
246246
* the name of the collection to return
247247
* @param documentClass
248248
* the default class to cast any documents returned from the database into.
249-
* @param <DOC>
249+
* @param <T>
250250
* the type of the class to use instead of {@code Document}.
251251
* @return
252252
* the collection
253253
*/
254-
public <DOC> Collection<DOC> getCollection(String collectionName, @NonNull Class<DOC> documentClass) {
254+
public <T> Collection<T> getCollection(String collectionName, @NonNull Class<T> documentClass) {
255255
hasLength(collectionName, "collectionName");
256256
notNull(documentClass, "documentClass");
257257
return new Collection<>(this, collectionName, documentClass);
@@ -301,12 +301,12 @@ public Collection<Document> createCollection(String collectionName, int dimensio
301301
* vector metric
302302
* @param documentClass
303303
* class of document to return
304-
* @param <DOC>
304+
* @param <T>
305305
* working class for the document
306306
* @return
307307
* the instance of collection
308308
*/
309-
public <DOC> Collection<DOC> createCollection(String collectionName, int dimension, SimilarityMetric metric, Class<DOC> documentClass) {
309+
public <T> Collection<T> createCollection(String collectionName, int dimension, SimilarityMetric metric, Class<T> documentClass) {
310310
return createCollection(collectionName, CollectionOptions.builder()
311311
.vectorDimension(dimension)
312312
.vectorSimilarity(metric)
@@ -320,11 +320,11 @@ public <DOC> Collection<DOC> createCollection(String collectionName, int dimensi
320320
* the name for the new collection to create
321321
* @param documentClass
322322
* class of document to return
323-
* @param <DOC>
323+
* @param <T>
324324
* working class for the document
325325
* @return the collection
326326
*/
327-
public <DOC> Collection<DOC> createCollection(String collectionName, Class<DOC> documentClass) {
327+
public <T> Collection<T> createCollection(String collectionName, Class<T> documentClass) {
328328
return createCollection(collectionName, null, documentClass);
329329
}
330330

@@ -350,11 +350,11 @@ public Collection<Document> createCollection(String collectionName, CollectionOp
350350
* various options for creating the collection
351351
* @param documentClass
352352
* the default class to cast any documents returned from the database into.
353-
* @param <DOC>
353+
* @param <T>
354354
* working class for the document
355355
* @return the collection
356356
*/
357-
public <DOC> Collection<DOC> createCollection(String collectionName, CollectionOptions collectionOptions, Class<DOC> documentClass) {
357+
public <T> Collection<T> createCollection(String collectionName, CollectionOptions collectionOptions, Class<T> documentClass) {
358358
hasLength(collectionName, "collectionName");
359359
notNull(documentClass, "documentClass");
360360
Command createCollection = Command

0 commit comments

Comments
 (0)