Skip to content

Commit c5dc3b7

Browse files
committed
Slice and estimatedCount
1 parent 1ad37a2 commit c5dc3b7

File tree

15 files changed

+190
-25
lines changed

15 files changed

+190
-25
lines changed

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1310,6 +1310,32 @@ public Page<T> findPage(Filter filter, FindOptions options) {
13101310
.collect(Collectors.toList()));
13111311
}
13121312

1313+
/**
1314+
* Executes a paginated 'find' query on the collection using the specified filter and find options asynchronously.
1315+
* <p>
1316+
* This method constructs and executes a command to fetch a specific page of documents from the collection that match
1317+
* the provided filter criteria. It allows for detailed control over the query through {@code FindOptions}, such as sorting,
1318+
* projection, pagination, and more. The result is wrapped in a {@link Page} object, which includes the documents found,
1319+
* the page size, and the state for fetching subsequent pages.
1320+
* </p>
1321+
* <p>
1322+
* Pagination is facilitated by the {@code skip}, {@code limit}, and {@code pageState} parameters within {@code FindOptions},
1323+
* enabling efficient data retrieval in scenarios where the total dataset is too large to be fetched in a single request.
1324+
* Optionally, similarity scoring can be included if {@code includeSimilarity} is set, which is useful for vector-based search queries.
1325+
* </p>
1326+
* <p>
1327+
* The method processes the command's response, mapping each document to the specified document class and collecting them into a list.
1328+
* This list, along with the maximum page size and the next page state, is used to construct the {@link Page} object returned by the method.
1329+
* </p>
1330+
*
1331+
* @param filter The filter criteria used to select documents from the collection.
1332+
* @param options The {@link FindOptions} providing additional query parameters, such as sorting and pagination.
1333+
* @return A {@link Page} object containing the documents that match the query, along with pagination information.
1334+
*/
1335+
public CompletableFuture<Page<T>> findPageASync(Filter filter, FindOptions options) {
1336+
return CompletableFuture.supplyAsync(() -> findPage(filter, options));
1337+
}
1338+
13131339
// --------------------------
13141340
// --- Distinct ----
13151341
// --------------------------
@@ -1380,6 +1406,25 @@ public int countDocuments(int upperBound) throws TooManyDocumentsToCountExceptio
13801406
return countDocuments(null, upperBound);
13811407
}
13821408

1409+
/**
1410+
* Executes the "estimatedDocumentCount" command to estimate the number of documents
1411+
* in a collection.
1412+
* <p>
1413+
* This method sends a command to estimate the document count and parses the count
1414+
* from the command's response. It handles the execution of the command and the extraction
1415+
* of the document count from the response.
1416+
* </p>
1417+
* @return the estimated number of documents in the collection.
1418+
* @throws IllegalStateException if the command response does not contain the expected result count.
1419+
*/
1420+
public long estimatedDocumentCount() {
1421+
Command command = new Command("estimatedDocumentCount");
1422+
// Run command
1423+
ApiResponse response = runCommand(command);
1424+
// Build Result
1425+
return response.getStatus().getInteger(RESULT_COUNT);
1426+
}
1427+
13831428
/**
13841429
* Counts the number of documents in the collection with a filter.
13851430
*

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public Command withReplacement(Object replacement) {
111111
* @return
112112
* self-reference
113113
*/
114-
public Command withProjection(Map<String, Integer> projection) {
114+
public Command withProjection(Map<String, Object> projection) {
115115
payload.appendIfNotNull("projection", projection);
116116
return this;
117117
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ public enum DataAPIKeywords {
7979
*/
8080
EXISTS("$exists"),
8181

82+
/**
83+
* SIMILARITY.
84+
*/
85+
SLICE("$slice"),
86+
8287
/**
8388
* SIMILARITY.
8489
*/

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class FindOneAndDeleteOptions {
4141
/**
4242
* Select.
4343
*/
44-
private Map<String, Integer> projection;
44+
private Map<String, Object> projection;
4545

4646
/**
4747
* Default constructor.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public class FindOneAndReplaceOptions {
4242
/**
4343
* Options to project (select) the result.
4444
*/
45-
private Map<String, Integer> projection;
45+
private Map<String, Object> projection;
4646

4747
/**
4848
* Flag to enforce the replacement

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public class FindOneAndUpdateOptions {
4343
/**
4444
* Select.
4545
*/
46-
private Map<String, Integer> projection;
46+
private Map<String, Object> projection;
4747

4848
/**
4949
* Upsert flag.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class FindOneOptions {
4141
/**
4242
* Select.
4343
*/
44-
private Map<String, Integer> projection;
44+
private Map<String, Object> projection;
4545

4646
/**
4747
* Options.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class FindOptions {
4141
/**
4242
* Projection for return document (select)
4343
*/
44-
private Map<String, Integer> projection;
44+
private Map<String, Object> projection;
4545

4646
/**
4747
* Skip a few result in the beginning

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

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,42 @@ public class Projection {
3333
private final String field;
3434

3535
/** Is the field present in the result. */
36-
private final boolean present;
36+
private final Boolean present;
37+
38+
/** Mutually exclusive with 'present', provide a slice */
39+
private final Integer sliceStart;
40+
41+
/** Mutually exclusive with 'present', provide a slice end */
42+
private final Integer sliceEnd;
3743

3844
/**
3945
* Default constructor.
46+
*
4047
* @param field
4148
* field value
4249
* @param present
4350
* tell if field is present
4451
*/
4552
public Projection(String field, boolean present) {
46-
this.field = field;
47-
this.present = present;
53+
this(field, present, null, null);
54+
}
55+
56+
/**
57+
* Default constructor.
58+
*
59+
* @param field
60+
* field value
61+
* @param present
62+
* tell if field is present
63+
* @param sliceStart
64+
* start of slice (mutually exclusive with 'present')
65+
* @param sliceEnd
66+
* end of slice (mutually exclusive with 'present'), optional
67+
*/
68+
public Projection(String field, Boolean present, Integer sliceStart, Integer sliceEnd) {
69+
this.field = field;
70+
this.present = present;
71+
this.sliceStart = sliceStart;
72+
this.sliceEnd = sliceEnd;
4873
}
4974
}

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

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,41 @@ private Projections() {}
4242
*/
4343
public static Projection[] include(String... field) {
4444
return Arrays.stream(field)
45-
.map(f -> new Projection(f, true))
45+
.map(f -> new Projection(f, true, null, null))
4646
.toArray(Projection[]::new);
4747
}
4848

49+
/**
50+
* Specifies the number of elements in an array to return in the query result.
51+
* <pre>
52+
* {@code
53+
* // Return the first two elements
54+
* { $slice: 2 }
55+
*
56+
* // Return the last two elements
57+
* { $slice: -2 }
58+
*
59+
* // Skip 4 elements (from 0th index), return the next 2
60+
* { $slice: [4, 2] }
61+
*
62+
* // Skip backward 4 elements, return next 2 elements (forward)
63+
* { $slice: [-4, 2] }
64+
* }
65+
* </pre>
66+
*
67+
* @param field
68+
* field name for slice
69+
* @param start
70+
* start index of slice
71+
* @param end
72+
* end index of slice
73+
* @return
74+
* a projection for the slide
75+
*/
76+
public static Projection slice(String field, Integer start, Integer end) {
77+
return new Projection(field, null, start, end);
78+
}
79+
4980
/**
5081
* Exclude a field in the result.
5182
*
@@ -56,7 +87,7 @@ public static Projection[] include(String... field) {
5687
*/
5788
public static Projection[] exclude(String... field) {
5889
return Arrays.stream(field)
59-
.map(f -> new Projection(f, false))
90+
.map(f -> new Projection(f, false, null, null))
6091
.toArray(Projection[]::new);
6192
}
6293
}

0 commit comments

Comments
 (0)