45
45
import com .datastax .astra .client .model .InsertManyResult ;
46
46
import com .datastax .astra .client .model .InsertOneResult ;
47
47
import com .datastax .astra .client .model .Page ;
48
- import com .datastax .astra .client .model .Projection ;
49
48
import com .datastax .astra .client .model .ReplaceOneOptions ;
50
- import com .datastax .astra .client .model .SortOrder ;
51
49
import com .datastax .astra .client .model .Update ;
52
50
import com .datastax .astra .client .model .UpdateOneOptions ;
53
51
import com .datastax .astra .client .model .UpdateResult ;
58
56
import lombok .Getter ;
59
57
import lombok .extern .slf4j .Slf4j ;
60
58
61
- import javax .print .Doc ;
62
59
import java .util .ArrayList ;
63
60
import java .util .List ;
64
- import java .util .Map ;
65
61
import java .util .Optional ;
66
62
import java .util .concurrent .Callable ;
67
63
import java .util .concurrent .CompletableFuture ;
@@ -851,10 +847,10 @@ public Optional<DOC> findOne(Filter filter) {
851
847
* // Assuming a Document in the collection with an id field
852
848
* Document doc = new Document().id(1).append("name", "John Doe");
853
849
* // To find the document with the id 1
854
- * FindOneOptions options = FindOneOptions.builder()
855
- * .withIncludeSimilarity() // return similarity in vector search
856
- * .withProjection(Map.of("name ", 1), // return a subset of fields
857
- * .build()) ;
850
+ * FindOneOptions options2 = FindOneOptions.builder()
851
+ * .withIncludeSimilarity() // return similarity in vector search
852
+ * .projections("_id ", "name") // return a subset of fields
853
+ * .build();
858
854
* Optional<Document> foundDoc = collection.findOne(Filters.eq("_id", 1), );
859
855
* foundDoc.ifPresent(System.out::println);
860
856
* }
@@ -902,6 +898,46 @@ public Optional<DOC> findOne(Filter filter, FindOneOptions options) {
902
898
public CompletableFuture <Optional <DOC >> findOneASync (Filter filter ) {
903
899
return CompletableFuture .supplyAsync (() -> findOne (filter ));
904
900
}
901
+
902
+
903
+ /**
904
+ * Asynchronously attempts to find a single document within the collection that matches the given filter criteria,
905
+ * utilizing the specified {@link FindOneOptions} for the query. This method offers a non-blocking approach to
906
+ * querying the database, making it well-suited for applications requiring efficient I/O operations without
907
+ * compromising the responsiveness of the application.
908
+ *
909
+ * <p>By executing the search operation in an asynchronous manner, this method allows other tasks to proceed
910
+ * concurrently, effectively utilizing system resources and improving application throughput. The query leverages
911
+ * a {@link Filter} instance to define the search criteria, and {@link FindOneOptions} to specify query
912
+ * customizations, such as projection or sort parameters.</p>
913
+ *
914
+ * <p>In cases where no document matches the filter, the method returns a {@link CompletableFuture} completed with
915
+ * an empty {@link java.util.Optional}, thus avoiding exceptions for non-existent documents. This behavior ensures
916
+ * a more graceful handling of such scenarios, allowing for cleaner and more robust client code by leveraging
917
+ * the {@link java.util.Optional} pattern within asynchronous workflows.</p>
918
+ *
919
+ * @param filter The {@link Filter} instance encapsulating the criteria used to identify the desired document.
920
+ * It defines the conditions that a document must meet to be considered a match.
921
+ * @param options The {@link FindOneOptions} providing additional query configurations such as projection
922
+ * and sort criteria to tailor the search operation.
923
+ * @return A {@link CompletableFuture<Optional<DOC>>} that, upon completion, contains an {@link Optional<DOC>}
924
+ * with the found document if one exists matching the filter criteria. If no matching document is found,
925
+ * a completed future with an empty {@link Optional} is returned, facilitating safe asynchronous retrieval.
926
+ *
927
+ * <p>Example usage:</p>
928
+ * <pre>
929
+ * {@code
930
+ * Filter filter = Filters.eq("id", 1);
931
+ * FindOneOptions options = FindOneOptions.builder().projection("name");
932
+ * CompletableFuture<Optional<Document>> futureDoc = collection.findOneASync(filter, options);
933
+ * futureDoc.thenAccept(doc -> doc.ifPresent(System.out::println));
934
+ * }
935
+ * </pre>
936
+ */
937
+ public CompletableFuture <Optional <DOC >> findOneASync (Filter filter , FindOneOptions options ) {
938
+ return CompletableFuture .supplyAsync (() -> findOne (filter , options ));
939
+ }
940
+
905
941
/**
906
942
* Retrieves a document by its identifier from the collection.
907
943
* <p>
@@ -918,6 +954,40 @@ public Optional<DOC> findById(Object id) {
918
954
return findOne (Filters .eq (id ));
919
955
}
920
956
957
+ /**
958
+ * Asynchronously retrieves a document from the collection by its unique identifier. This method wraps
959
+ * the synchronous {@code findById} operation in a {@link CompletableFuture}, enabling non-blocking
960
+ * database queries that improve application responsiveness and efficiency. The method is ideal for
961
+ * applications that require the retrieval of specific documents without impacting the performance
962
+ * of the user interface or other concurrent operations.
963
+ *
964
+ * <p>The unique identifier used to locate the document is typically the primary key or a unique field
965
+ * within the collection. This method abstracts the complexity of asynchronous programming, providing
966
+ * a straightforward way to execute and handle database queries within a non-blocking model.</p>
967
+ *
968
+ * <p>If the document with the specified identifier exists, it is wrapped in an {@link Optional} and
969
+ * returned within the completed future. Otherwise, the future is completed with an empty {@link Optional},
970
+ * neatly handling cases where no matching document is found without throwing exceptions.</p>
971
+ *
972
+ * @param id The unique identifier of the document to retrieve. This can be of any type that the database
973
+ * recognizes as a valid identifier format (e.g., String, Integer).
974
+ * @return A {@link CompletableFuture<Optional<DOC>>} that, upon completion, contains an {@link Optional<DOC>}
975
+ * with the document if found. If no document matches the specified identifier, a completed future
976
+ * with an empty {@link Optional} is returned.
977
+ *
978
+ * <p>Example usage:</p>
979
+ * <pre>
980
+ * {@code
981
+ * Object documentId = "uniqueDocumentId123";
982
+ * CompletableFuture<Optional<Document>> futureDocument = collection.findByIdASync(documentId);
983
+ * futureDocument.thenAccept(document -> document.ifPresent(System.out::println));
984
+ * }
985
+ * </pre>
986
+ */
987
+ public CompletableFuture <Optional <DOC >> findByIdASync (Object id ) {
988
+ return CompletableFuture .supplyAsync (() -> findById (id ));
989
+ }
990
+
921
991
/**
922
992
* Finds all documents in the collection.
923
993
*
@@ -942,7 +1012,7 @@ public FindIterable<DOC> find(Filter filter, FindOptions options) {
942
1012
* @return A {@link FindIterable} for iterating over all documents in the collection.
943
1013
*/
944
1014
public FindIterable <DOC > find () {
945
- return find (null , new FindOptions ());
1015
+ return find (null , FindOptions . builder (). build ());
946
1016
}
947
1017
948
1018
/**
@@ -956,7 +1026,7 @@ public FindIterable<DOC> find() {
956
1026
* @return A {@link FindIterable} for iterating over the documents that match the filter.
957
1027
*/
958
1028
public FindIterable <DOC > find (Filter filter ) {
959
- return find (filter , new FindOptions ());
1029
+ return find (filter , FindOptions . builder (). build ());
960
1030
}
961
1031
962
1032
/**
@@ -975,7 +1045,7 @@ public FindIterable<DOC> find(Filter filter) {
975
1045
*/
976
1046
public FindIterable <DOC > find (Filter filter , float [] vector , int limit ) {
977
1047
return find (filter , FindOptions .builder ()
978
- .withVector (vector )
1048
+ .vector (vector )
979
1049
.limit (limit )
980
1050
.build ());
981
1051
}
@@ -994,7 +1064,7 @@ public FindIterable<DOC> find(Filter filter, float[] vector, int limit) {
994
1064
*/
995
1065
public FindIterable <DOC > find (Filter filter , String vectorize , int limit ) {
996
1066
return find (filter , FindOptions .builder ()
997
- .withVectorize (vectorize )
1067
+ .vectorize (vectorize )
998
1068
.limit (limit )
999
1069
.build ());
1000
1070
}
0 commit comments