Skip to content

Commit 989a62a

Browse files
committed
Rework of options with builders and fluent APIS
1 parent 9200422 commit 989a62a

26 files changed

+1061
-1314
lines changed

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

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222

2323
import com.datastax.astra.client.exception.DataAPIFaultyResponseException;
2424
import com.datastax.astra.client.exception.DataApiException;
25-
import com.datastax.astra.client.exception.DataApiResponseException;
2625
import com.datastax.astra.client.exception.TooManyDocumentsToCountException;
2726
import com.datastax.astra.client.model.BulkWriteOptions;
2827
import com.datastax.astra.client.model.BulkWriteResult;
@@ -48,6 +47,7 @@
4847
import com.datastax.astra.client.model.ObjectId;
4948
import com.datastax.astra.client.model.Page;
5049
import com.datastax.astra.client.model.ReplaceOneOptions;
50+
import com.datastax.astra.client.model.ReturnDocument;
5151
import com.datastax.astra.client.model.UUIDv6;
5252
import com.datastax.astra.client.model.UUIDv7;
5353
import com.datastax.astra.client.model.Update;
@@ -788,7 +788,7 @@ public CompletableFuture<InsertManyResult > insertManyAsync(List<? extends T> do
788788
* @throws RuntimeException if there is an error in merging the results of concurrent insertions.
789789
*/
790790
public InsertManyResult insertMany(List<? extends T> documents) {
791-
return insertMany(documents, InsertManyOptions.builder().build());
791+
return insertMany(documents, new InsertManyOptions());
792792
}
793793

794794
/**
@@ -902,7 +902,7 @@ private Callable<InsertManyResult> getInsertManyResultCallable(List<? extends T>
902902
* that retrieval operations can be performed safely without the concern of {@link java.util.NoSuchElementException}.
903903
*/
904904
public Optional<T> findOne(Filter filter) {
905-
return findOne(filter, FindOneOptions.builder().build());
905+
return findOne(filter, new FindOneOptions());
906906
}
907907

908908
/**
@@ -1087,7 +1087,7 @@ public FindIterable<T> find(Filter filter, FindOptions options) {
10871087
* @return A {@link FindIterable} for iterating over all documents in the collection.
10881088
*/
10891089
public FindIterable<T> find() {
1090-
return find(null, FindOptions.builder().build());
1090+
return find(null, new FindOptions());
10911091
}
10921092

10931093
/**
@@ -1101,7 +1101,7 @@ public FindIterable<T> find() {
11011101
* @return A {@link FindIterable} for iterating over the documents that match the filter.
11021102
*/
11031103
public FindIterable<T> find(Filter filter) {
1104-
return find(filter, FindOptions.builder().build());
1104+
return find(filter, new FindOptions());
11051105
}
11061106

11071107
/**
@@ -1119,10 +1119,7 @@ public FindIterable<T> find(Filter filter) {
11191119
* @return A {@link FindIterable} for iterating over the sorted and limited documents.
11201120
*/
11211121
public FindIterable<T> find(Filter filter, float[] vector, int limit) {
1122-
return find(filter, FindOptions.builder()
1123-
.vector(vector)
1124-
.limit(limit)
1125-
.build());
1122+
return find(filter, FindOptions.Builder.vector(vector).limit(limit));
11261123
}
11271124

11281125
/**
@@ -1138,10 +1135,7 @@ public FindIterable<T> find(Filter filter, float[] vector, int limit) {
11381135
* @return A {@link FindIterable} for iterating over the sorted and limited documents.
11391136
*/
11401137
public FindIterable<T> find(Filter filter, String vectorize, int limit) {
1141-
return find(filter, FindOptions.builder()
1142-
.vectorize(vectorize)
1143-
.limit(limit)
1144-
.build());
1138+
return find(filter, FindOptions.Builder.vectorize(vectorize).limit(limit));
11451139
}
11461140

11471141
/**
@@ -1358,7 +1352,7 @@ public int countDocuments(Filter filter, int upperBound) throws TooManyDocuments
13581352
*
13591353
*/
13601354
public DeleteResult deleteOne(Filter filter) {
1361-
return deleteOne(filter, DeleteOneOptions.builder().build());
1355+
return deleteOne(filter, new DeleteOneOptions());
13621356
}
13631357

13641358
/**
@@ -1487,7 +1481,7 @@ public Optional<T> findOneAndReplace(Filter filter, T replacement, FindOneAndRep
14871481
.withProjection(options.getProjection())
14881482
.withOptions(new Document()
14891483
.appendIfNotNull("upsert", options.getUpsert())
1490-
.appendIfNotNull("returnDocument", options.getReturnDocument().name())
1484+
.appendIfNotNull("returnDocument", options.getReturnDocument())
14911485
);
14921486

14931487
ApiResponse res = runCommand(findOneAndReplace);
@@ -1535,7 +1529,7 @@ public UpdateResult replaceOne(Filter filter, T replacement, ReplaceOneOptions r
15351529
.withReplacement(replacement)
15361530
.withOptions(new Document()
15371531
.appendIfNotNull("upsert", replaceOneOptions.getUpsert())
1538-
.append("returnDocument", FindOneAndReplaceOptions.ReturnDocument.before.name())
1532+
.append("returnDocument", ReturnDocument.BEFORE.getKey())
15391533
);
15401534

15411535
// Execute the `findOneAndReplace`
@@ -1600,7 +1594,7 @@ private FindOneAndReplaceResult<T> executeFindOneAndReplace(Command cmd) {
16001594
* returned
16011595
*/
16021596
public Optional<T> findOneAndUpdate(Filter filter, Update update) {
1603-
return findOneAndUpdate(filter, update, FindOneAndUpdateOptions.builder().build());
1597+
return findOneAndUpdate(filter, update, new FindOneAndUpdateOptions());
16041598
}
16051599

16061600
/**
@@ -1629,7 +1623,7 @@ public Optional<T> findOneAndUpdate(Filter filter, Update update, FindOneAndUpda
16291623
.withProjection(options.getProjection())
16301624
.withOptions(new Document()
16311625
.appendIfNotNull("upsert", options.getUpsert())
1632-
.append("returnDocument", options.getReturnDocument().name())
1626+
.append("returnDocument", options.getReturnDocument())
16331627
);
16341628

16351629
ApiResponse res = runCommand(cmd);
@@ -1653,7 +1647,7 @@ public Optional<T> findOneAndUpdate(Filter filter, Update update, FindOneAndUpda
16531647
* the result of the update one operation
16541648
*/
16551649
public UpdateResult updateOne(Filter filter, Update update) {
1656-
return updateOne(filter, update, UpdateOneOptions.builder().build());
1650+
return updateOne(filter, update, new UpdateOneOptions());
16571651
}
16581652

16591653
/**
@@ -1710,7 +1704,7 @@ private static UpdateResult getUpdateResult(ApiResponse apiResponse) {
17101704
* the result of the update many operation
17111705
*/
17121706
public UpdateResult updateMany(Filter filter, Update update) {
1713-
return updateMany(filter, update, UpdateManyOptions.builder().build());
1707+
return updateMany(filter, update, new UpdateManyOptions());
17141708
}
17151709

17161710
/**

astra-db-java/src/main/java/com/datastax/astra/client/model/DeleteOneOptions.java

Lines changed: 52 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,13 @@
2020
* #L%
2121
*/
2222

23-
import com.datastax.astra.internal.utils.Assert;
24-
import lombok.Getter;
25-
26-
import java.util.List;
23+
import com.datastax.astra.internal.utils.OptionsUtils;
24+
import lombok.Data;
2725

2826
/**
2927
* Options to delete One document.
3028
*/
31-
@Getter
29+
@Data
3230
public class DeleteOneOptions {
3331

3432
/**
@@ -39,9 +37,7 @@ public class DeleteOneOptions {
3937
/**
4038
* Default constructor.
4139
*/
42-
public DeleteOneOptions(DeleteOneOptionsBuilder builder) {
43-
this.sort = builder.sort;
44-
}
40+
public DeleteOneOptions() {}
4541

4642
/**
4743
* Syntax sugar as delete option is only a sort
@@ -51,129 +47,82 @@ public DeleteOneOptions(DeleteOneOptionsBuilder builder) {
5147
* @return
5248
* current command.
5349
*/
54-
public static DeleteOneOptions sort(Sort sort) {
55-
return new DeleteOneOptionsBuilder().sort(sort).build();
50+
public DeleteOneOptions sort(Sort... sort) {
51+
setSort(OptionsUtils.sort(sort));
52+
return this;
5653
}
5754

5855
/**
59-
* Create a builder for those options.
56+
* Add a criteria with $vectorize in the sort clause
6057
*
61-
* @return
62-
* instance of the builder.
58+
* @param vectorize an expression to look for vectorization
59+
* @param sorts The sort criteria to be applied to the findOne operation.
60+
* @return current command
6361
*/
64-
public static DeleteOneOptionsBuilder builder() {
65-
return new DeleteOneOptionsBuilder();
62+
public DeleteOneOptions vectorize(String vectorize, Sort ... sorts) {
63+
setSort(Sorts.vectorize(vectorize));
64+
if (sorts != null) {
65+
getSort().putAll(OptionsUtils.sort(sorts));
66+
}
67+
return this;
6668
}
6769

6870
/**
69-
* Find is an operation with multiple options to filter, sort, project, skip, limit, and more.
70-
* This builder will help to chain options.
71+
* Add a criteria with $vector in the sort clause
72+
*
73+
* @param vector vector float
74+
* @param sorts The sort criteria to be applied to the findOne operation.
75+
* @return current command
7176
*/
72-
public static class DeleteOneOptionsBuilder {
77+
public DeleteOneOptions vector(float[] vector, Sort... sorts) {
78+
setSort(Sorts.vector(vector));
79+
if (sorts != null) {
80+
getSort().putAll(OptionsUtils.sort(sorts));
81+
}
82+
return this;
83+
}
7384

74-
/**
75-
* Order by.
76-
*/
77-
private Document sort;
85+
/**
86+
* Builder for creating {@link DeleteOneOptions} instances with a fluent API.
87+
*/
88+
public static class Builder {
7889

7990
/**
80-
* Default Builder.
91+
* Hide constructor.
8192
*/
82-
public DeleteOneOptionsBuilder() {}
93+
private Builder() {}
8394

8495
/**
85-
* Fluent api.
96+
* Initializes the building process with sorting options.
8697
*
87-
* @param pSort
88-
* list of sorts
89-
* @return
90-
* Self reference
98+
* @param sort The sort criteria to be applied to the delete operation.
99+
* @return A new {@link DeleteOneOptions} instance configured with the provided sort criteria.
91100
*/
92-
public DeleteOneOptionsBuilder sort(Sort pSort) {
93-
Assert.notNull(pSort, "sort");
94-
if (this.sort == null) {
95-
this.sort = new Document();
96-
}
97-
this.sort.put(pSort.getField(), pSort.getOrder().getCode());
98-
return this;
101+
public static DeleteOneOptions sort(Sort... sort) {
102+
return new DeleteOneOptions().sort(sort);
99103
}
100104

101105
/**
102-
* Fluent api.
106+
* Initializes the building process with vectorize options.
103107
*
104-
* @param sorts
105-
* list of sorts
106-
* @return
107-
* Self reference
108+
* @param vectorize The vectorize criteria to be applied to the findOne operation
109+
* @param sorts The sort criteria to be applied to the findOne operation.
110+
* @return A new {@link DeleteOneOptions} instance configured with the provided vectorize criteria.
108111
*/
109-
public DeleteOneOptionsBuilder sort(List<Sort> sorts) {
110-
Assert.notNull(sorts, "sort");
111-
if (this.sort == null) {
112-
sort = new Document();
113-
}
114-
for (Sort s : sorts) {
115-
this.sort.put(s.getField(), s.getOrder().getCode());
116-
}
117-
return this;
112+
public static DeleteOneOptions vectorize(String vectorize, Sort... sorts) {
113+
return new DeleteOneOptions().vectorize(vectorize, sorts);
118114
}
119115

120116
/**
121-
* Fluent api.
117+
* Initializes the building process with vector options.
122118
*
123-
* @param pSort
124-
* add a filter
125-
* @return
126-
* current command.
119+
* @param vector The vector criteria to be applied to the findOne operation
120+
* @param sorts The sort criteria to be applied to the findOne operation.
121+
* @return A new {@link DeleteOneOptions} instance configured with the provided vector criteria.
127122
*/
128-
public DeleteOneOptionsBuilder sort(Document pSort) {
129-
Assert.notNull(pSort, "sort");
130-
if (this.sort == null) {
131-
sort = new Document();
132-
}
133-
this.sort.putAll(pSort);
134-
return this;
123+
public static DeleteOneOptions vector(float[] vector, Sort... sorts) {
124+
return new DeleteOneOptions().vector(vector, sorts);
135125
}
136-
137-
/**
138-
* Builder for the find Options.
139-
*
140-
* @return
141-
* the find options object
142-
*/
143-
public DeleteOneOptions build() {
144-
return new DeleteOneOptions(this);
145-
}
146-
147126
}
148127

149-
/**
150-
* Fluent api.
151-
*
152-
* @param pSort
153-
* add a filter
154-
* @return
155-
* current command.
156-
*/
157-
public DeleteOneOptions sortingBy(Document pSort) {
158-
Assert.notNull(pSort, "sort");
159-
if (this.sort == null) {
160-
sort = new Document();
161-
}
162-
this.sort.putAll(pSort);
163-
return this;
164-
}
165-
166-
/**
167-
* Add a sort clause to the current field.
168-
*
169-
* @param fieldName
170-
* field name
171-
* @param ordering
172-
* field ordering
173-
* @return
174-
* current reference find
175-
*/
176-
public DeleteOneOptions sortingBy(String fieldName, SortOrder ordering) {
177-
return sortingBy(new Document().append(fieldName, ordering.getCode()));
178-
}
179128
}

astra-db-java/src/main/java/com/datastax/astra/client/model/DeleteResult.java

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

23-
import lombok.AllArgsConstructor;
24-
import lombok.Data;
23+
import lombok.Getter;
24+
import lombok.Setter;
2525

2626
/**
2727
* Hold the result of delete commands (deleteOne, deleteMany).
2828
*/
29-
@Data @AllArgsConstructor
29+
@Getter @Setter
3030
public class DeleteResult {
3131

3232
/**
@@ -36,7 +36,11 @@ public class DeleteResult {
3636

3737
/**
3838
* Default constructor.
39+
*
40+
* @param deletedCount
41+
* number of items deleted
3942
*/
40-
public DeleteResult() {}
41-
43+
public DeleteResult(int deletedCount) {
44+
this.deletedCount = deletedCount;
45+
}
4246
}

astra-db-java/src/main/java/com/datastax/astra/client/model/DistinctIterable.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public DistinctIterable(Collection<DOC> collection, String fieldName, Filter fil
7373
this.filter = filter;
7474
this.fieldName = fieldName;
7575
this.fieldClass = fieldClass;
76-
this.options = FindOptions.builder().build();
76+
this.options = new FindOptions();
7777
}
7878

7979
/** {@inheritDoc} */

0 commit comments

Comments
 (0)