Skip to content

Commit f3f1d02

Browse files
wu-huia-maurice
authored andcommitted
Port LimitToLast to C++ SDK.
PiperOrigin-RevId: 298915552
1 parent 12f9d30 commit f3f1d02

File tree

6 files changed

+55
-7
lines changed

6 files changed

+55
-7
lines changed

firestore/src/android/query_android.cc

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ Query QueryInternal::OrderBy(const FieldPath& field,
4646

4747
Query QueryInternal::Limit(int32_t limit) {
4848
JNIEnv* env = firestore_->app()->GetJNIEnv();
49-
// Although the backend supports signed int32, Android client SDK use long
50-
// as parameter type. So we up-cast it to jlong instead of jint here.
49+
// Although the backend supports signed int32, Android client SDK uses long
50+
// as parameter type. So we cast it to jlong instead of jint here.
5151
jobject query = env->CallObjectMethod(obj_, query::GetMethodId(query::kLimit),
5252
static_cast<jlong>(limit));
5353
CheckAndClearJniExceptions(env);
@@ -58,6 +58,20 @@ Query QueryInternal::Limit(int32_t limit) {
5858
return Query{internal};
5959
}
6060

61+
Query QueryInternal::LimitToLast(int32_t limit) {
62+
JNIEnv* env = firestore_->app()->GetJNIEnv();
63+
// Although the backend supports signed int32, Android client SDK uses long
64+
// as parameter type. So we cast it to jlong instead of jint here.
65+
jobject query = env->CallObjectMethod(
66+
obj_, query::GetMethodId(query::kLimitToLast), static_cast<jlong>(limit));
67+
CheckAndClearJniExceptions(env);
68+
QueryInternal* internal = new QueryInternal{firestore_, query};
69+
env->DeleteLocalRef(query);
70+
71+
CheckAndClearJniExceptions(env);
72+
return Query{internal};
73+
}
74+
6175
Future<QuerySnapshot> QueryInternal::Get(Source source) {
6276
JNIEnv* env = firestore_->app()->GetJNIEnv();
6377
jobject task =

firestore/src/android/query_android.h

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ enum class QueryFn {
6161
"Lcom/google/firebase/firestore/Query$Direction;)" \
6262
"Lcom/google/firebase/firestore/Query;"), \
6363
X(Limit, "limit", "(J)Lcom/google/firebase/firestore/Query;"), \
64+
X(LimitToLast, "limitToLast", \
65+
"(J)Lcom/google/firebase/firestore/Query;"), \
6466
X(StartAtSnapshot, "startAt", \
6567
"(Lcom/google/firebase/firestore/DocumentSnapshot;)" \
6668
"Lcom/google/firebase/firestore/Query;"), \
@@ -235,15 +237,26 @@ class QueryInternal : public WrapperFuture<QueryFn, QueryFn::kCount> {
235237
Query OrderBy(const FieldPath& field, Query::Direction direction);
236238

237239
/**
238-
* @brief Creates and returns a new Query that's additionally limited to only
239-
* return up to the specified number of documents.
240+
* @brief Creates and returns a new Query that only returns the first matching
241+
* documents up to the specified number.
240242
*
241243
* @param[in] limit A non-negative integer to specify the maximum number of
242244
* items to return.
243245
*
244246
* @return The created Query.
245247
*/
246-
Query Limit(int32_t limit);
248+
virtual Query Limit(int32_t limit);
249+
250+
/**
251+
* @brief Creates and returns a new Query that only returns the last matching
252+
* documents up to the specified number.
253+
*
254+
* @param[in] limit A non-negative integer to specify the maximum number of
255+
* items to return.
256+
*
257+
* @return The created Query.
258+
*/
259+
virtual Query LimitToLast(int32_t limit);
247260

248261
/**
249262
* @brief Creates and returns a new Query that starts at the provided document

firestore/src/common/query.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,11 @@ Query Query::Limit(int32_t limit) {
192192
return internal_->Limit(limit);
193193
}
194194

195+
Query Query::LimitToLast(int32_t limit) {
196+
if (!internal_) return {};
197+
return internal_->LimitToLast(limit);
198+
}
199+
195200
Query Query::StartAt(const DocumentSnapshot& snapshot) {
196201
if (!internal_) return {};
197202
return internal_->StartAt(snapshot);

firestore/src/include/firebase/firestore/query.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -393,8 +393,8 @@ class Query {
393393
Direction direction = Direction::kAscending);
394394

395395
/**
396-
* @brief Creates and returns a new Query that's additionally limited to only
397-
* return up to the specified number of documents.
396+
* @brief Creates and returns a new Query that only returns the first matching
397+
* documents up to the specified number.
398398
*
399399
* @param[in] limit A non-negative integer to specify the maximum number of
400400
* items to return.
@@ -403,6 +403,17 @@ class Query {
403403
*/
404404
virtual Query Limit(int32_t limit);
405405

406+
/**
407+
* @brief Creates and returns a new Query that only returns the last matching
408+
* documents up to the specified number.
409+
*
410+
* @param[in] limit A non-negative integer to specify the maximum number of
411+
* items to return.
412+
*
413+
* @return The created Query.
414+
*/
415+
virtual Query LimitToLast(int32_t limit);
416+
406417
/**
407418
* @brief Creates and returns a new Query that starts at the provided document
408419
* (inclusive). The starting position is relative to the order of the query.

firestore/src/ios/query_ios.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ Query QueryInternal::Limit(int32_t limit) {
5353
return MakePublic(query_.LimitToFirst(limit));
5454
}
5555

56+
Query QueryInternal::LimitToLast(int32_t limit) {
57+
return MakePublic(query_.LimitToLast(limit));
58+
}
59+
5660
Future<QuerySnapshot> QueryInternal::Get(Source source) {
5761
auto promise = promise_factory_.CreatePromise<QuerySnapshot>(AsyncApis::kGet);
5862
auto listener = ListenerWithPromise<api::QuerySnapshot>(promise);

firestore/src/ios/query_ios.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class QueryInternal {
3030

3131
Query OrderBy(const FieldPath& field, Query::Direction direction);
3232
Query Limit(int32_t limit);
33+
Query LimitToLast(int32_t limit);
3334

3435
virtual Future<QuerySnapshot> Get(Source source);
3536
virtual Future<QuerySnapshot> GetLastResult();

0 commit comments

Comments
 (0)