Skip to content

Commit 28941f9

Browse files
authored
Revert "Revert "Public count (#10246)" (#10252)"
This reverts commit b695d99.
1 parent 65f6cfa commit 28941f9

File tree

11 files changed

+118
-54
lines changed

11 files changed

+118
-54
lines changed

Firestore/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# Unreleased
2+
- [feature] Added `Query.count()`, which fetches the number of documents in the
3+
result set without actually downloading the documents (#10246).
24
- [fixed] Fixed compiler warning about `@param comparator` (#10226).
35

46
# 9.6.0

Firestore/Example/Tests/Integration/API/FIRCountTests.mm

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
#import <XCTest/XCTest.h>
2020

2121
#import "Firestore/Example/Tests/Util/FSTIntegrationTestCase.h"
22-
#import "Firestore/Source/API/FIRAggregateQuery+Internal.h"
23-
#import "Firestore/Source/API/FIRAggregateQuerySnapshot+Internal.h"
24-
#import "Firestore/Source/API/FIRQuery+Internal.h"
22+
#import "Firestore/Source/Public/FirebaseFirestore/FIRAggregateQuery.h"
23+
#import "Firestore/Source/Public/FirebaseFirestore/FIRAggregateQuerySnapshot.h"
24+
#import "Firestore/Source/Public/FirebaseFirestore/FIRAggregateSource.h"
2525

2626
@interface FIRCountTests : FSTIntegrationTestCase
2727
@end

Firestore/Source/API/FIRAggregateQuery+Internal.h

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,35 +14,12 @@
1414
* limitations under the License.
1515
*/
1616

17-
// TODO(b/246760853): Move FIRAggregateQuery to public headers to release it.
18-
19-
#import "FIRAggregateSource+Internal.h"
17+
#import "FIRAggregateQuery.h"
2018
#import "FIRQuery.h"
2119

22-
@class FIRAggregateQuerySnapshot;
23-
24-
/**
25-
* An `AggregateQuery` computes some aggregation statistics from the result set of a base
26-
* `Query`.
27-
*/
28-
NS_SWIFT_NAME(AggregateQuery)
29-
@interface FIRAggregateQuery : NSObject
20+
@interface FIRAggregateQuery (/* init */)
3021

3122
- (instancetype _Nonnull)init NS_UNAVAILABLE;
3223
- (instancetype _Nonnull)initWithQuery:(FIRQuery *_Nonnull)query NS_DESIGNATED_INITIALIZER;
3324

34-
/** The base `Query` for this aggregate query. */
35-
@property(nonatomic, readonly) FIRQuery *_Nonnull query;
36-
37-
/**
38-
* Executes the aggregate query and reads back the results as a `FIRAggregateQuerySnapshot`.
39-
*
40-
* @param source indicates where the results should be fetched from.
41-
* @param completion a block to execute once the results have been successfully read.
42-
* snapshot will be `nil` only if error is `non-nil`.
43-
*/
44-
- (void)aggregationWithSource:(FIRAggregateSource)source
45-
completion:(void (^_Nonnull)(FIRAggregateQuerySnapshot *_Nullable snapshot,
46-
NSError *_Nullable error))completion
47-
NS_SWIFT_NAME(aggregation(source:completion:));
4825
@end

Firestore/Source/API/FIRAggregateQuerySnapshot+Internal.h

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,14 @@
1414
* limitations under the License.
1515
*/
1616

17-
// TODO(b/246760853): Move FIRAggregateQuerySnapshot to public headers to release it.
18-
19-
#import "FIRAggregateQuery+Internal.h"
17+
#import "FIRAggregateQuerySnapshot.h"
2018

2119
@class FIRAggregateQuery;
2220

23-
/**
24-
* An `AggregateQuerySnapshot` contains results of a `AggregateQuery`.
25-
*/
26-
NS_SWIFT_NAME(AggregateQuerySnapshot)
27-
@interface FIRAggregateQuerySnapshot : NSObject
21+
@interface FIRAggregateQuerySnapshot (/* init */)
2822

2923
- (instancetype _Nonnull)init NS_UNAVAILABLE;
3024
- (instancetype _Nonnull)initWithCount:(int64_t)result
3125
Query:(FIRAggregateQuery* _Nonnull)query NS_DESIGNATED_INITIALIZER;
3226

33-
/** The original `AggregateQuery` this snapshot is a result of. */
34-
@property(nonatomic, readonly) FIRAggregateQuery* _Nonnull query;
35-
36-
/**
37-
* The result of a document count aggregation. Null if no count aggregation is
38-
* available in the result.
39-
*/
40-
@property(nonatomic, readonly) NSNumber* _Nullable count;
41-
4227
@end

Firestore/Source/API/FIRQuery+Internal.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
* limitations under the License.
1515
*/
1616

17-
#import "FIRAggregateQuery+Internal.h"
1817
#import "FIRQuery.h"
1918

2019
#include <memory>
@@ -48,12 +47,6 @@ NS_ASSUME_NONNULL_BEGIN
4847
// TODO(orquery): This method will become public API. Change visibility and add documentation.
4948
- (FIRQuery *)queryWhereFilter:(FIRFilter *)filter;
5049

51-
// TODO(b/246760853): This property will become public API.
52-
/**
53-
* An `AggregateQuery` counting the number of documents matching this query.
54-
*/
55-
@property(nonatomic, readonly) FIRAggregateQuery *count;
56-
5750
@end
5851

5952
NS_ASSUME_NONNULL_END

Firestore/Source/API/FIRQuery.mm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <utility>
2121
#include <vector>
2222

23+
#import "FIRAggregateQuery+Internal.h"
2324
#import "FIRDocumentReference.h"
2425
#import "FIRFirestoreErrors.h"
2526
#import "Firestore/Source/API/FIRDocumentReference+Internal.h"
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#import <Foundation/Foundation.h>
18+
19+
#import "FIRAggregateSource.h"
20+
21+
NS_ASSUME_NONNULL_BEGIN
22+
23+
@class FIRQuery;
24+
@class FIRAggregateQuerySnapshot;
25+
26+
/**
27+
* An `AggregateQuery` computes some aggregation statistics from the result set of a base
28+
* `Query`.
29+
*/
30+
NS_SWIFT_NAME(AggregateQuery)
31+
@interface FIRAggregateQuery : NSObject
32+
33+
/** The base `Query` for this aggregate query. */
34+
@property(nonatomic, readonly) FIRQuery *query;
35+
36+
/**
37+
* Executes the aggregate query and reads back the results as a `FIRAggregateQuerySnapshot`.
38+
*
39+
* @param source indicates where the results should be fetched from.
40+
* @param completion a block to execute once the results have been successfully read.
41+
* snapshot will be `nil` only if error is `non-nil`.
42+
*/
43+
- (void)aggregationWithSource:(FIRAggregateSource)source
44+
completion:(void (^)(FIRAggregateQuerySnapshot *_Nullable snapshot,
45+
NSError *_Nullable error))completion
46+
NS_SWIFT_NAME(getAggregation(source:completion:));
47+
@end
48+
49+
NS_ASSUME_NONNULL_END
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#import <Foundation/Foundation.h>
18+
19+
NS_ASSUME_NONNULL_BEGIN
20+
21+
@class FIRAggregateQuery;
22+
23+
/**
24+
* An `AggregateQuerySnapshot` contains results of a `AggregateQuery`.
25+
*/
26+
NS_SWIFT_NAME(AggregateQuerySnapshot)
27+
@interface FIRAggregateQuerySnapshot : NSObject
28+
29+
/** The original `AggregateQuery` this snapshot is a result of. */
30+
@property(nonatomic, readonly) FIRAggregateQuery* query;
31+
32+
/**
33+
* The result of a document count aggregation. Null if no count aggregation is
34+
* available in the result.
35+
*/
36+
@property(nonatomic, readonly) NSNumber* count;
37+
38+
@end
39+
40+
NS_ASSUME_NONNULL_END

Firestore/Source/API/FIRAggregateSource+Internal.h renamed to Firestore/Source/Public/FirebaseFirestore/FIRAggregateSource.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
* limitations under the License.
1515
*/
1616

17-
// TODO(b/246760853): Move FIRAggregateSource to public headers to release it.
18-
1917
#import <Foundation/Foundation.h>
2018

19+
NS_ASSUME_NONNULL_BEGIN
20+
2121
/** Configures the behavior of `AggregateQuery.aggregateWithSource(source:completion:)`. */
2222
typedef NS_ENUM(NSUInteger, FIRAggregateSource) {
2323
/**
@@ -28,3 +28,5 @@ typedef NS_ENUM(NSUInteger, FIRAggregateSource) {
2828
*/
2929
FIRAggregateSourceServer,
3030
} NS_SWIFT_NAME(AggregateSource);
31+
32+
NS_ASSUME_NONNULL_END

Firestore/Source/Public/FirebaseFirestore/FIRQuery.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#import "FIRFirestoreSource.h"
2020
#import "FIRListenerRegistration.h"
2121

22+
@class FIRAggregateQuery;
2223
@class FIRFieldPath;
2324
@class FIRFirestore;
2425
@class FIRQuerySnapshot;
@@ -542,6 +543,13 @@ NS_SWIFT_NAME(Query)
542543
*/
543544
- (FIRQuery *)queryEndingAtValues:(NSArray *)fieldValues NS_SWIFT_NAME(end(at:));
544545

546+
#pragma mark - Aggregation
547+
548+
/**
549+
* An `AggregateQuery` counting the number of documents matching this query.
550+
*/
551+
@property(nonatomic, readonly) FIRAggregateQuery *count;
552+
545553
@end
546554

547555
NS_ASSUME_NONNULL_END

Firestore/Swift/Tests/Integration/AsyncAwaitIntegrationTests.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,5 +73,12 @@ let emptyBundle = """
7373

7474
XCTAssertNil(value, "value should be nil on success")
7575
}
76+
77+
func testCount() async throws {
78+
let collection = collectionRef()
79+
try await collection.addDocument(data: [:])
80+
let snapshot = try await collection.count.getAggregation(source: .server)
81+
XCTAssertEqual(snapshot.count, 1)
82+
}
7683
}
7784
#endif

0 commit comments

Comments
 (0)