Skip to content

Public count #10254

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 28, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Firestore/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# Unreleased
- [feature] Added `Query.count()`, which fetches the number of documents in the
result set without actually downloading the documents (#10246).
- [fixed] Fixed compiler warning about `@param comparator` (#10226).

# 9.6.0
Expand Down
6 changes: 3 additions & 3 deletions Firestore/Example/Tests/Integration/API/FIRCountTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
#import <XCTest/XCTest.h>

#import "Firestore/Example/Tests/Util/FSTIntegrationTestCase.h"
#import "Firestore/Source/API/FIRAggregateQuery+Internal.h"
#import "Firestore/Source/API/FIRAggregateQuerySnapshot+Internal.h"
#import "Firestore/Source/API/FIRQuery+Internal.h"
#import "Firestore/Source/Public/FirebaseFirestore/FIRAggregateQuery.h"
#import "Firestore/Source/Public/FirebaseFirestore/FIRAggregateQuerySnapshot.h"
#import "Firestore/Source/Public/FirebaseFirestore/FIRAggregateSource.h"

@interface FIRCountTests : FSTIntegrationTestCase
@end
Expand Down
27 changes: 2 additions & 25 deletions Firestore/Source/API/FIRAggregateQuery+Internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,12 @@
* limitations under the License.
*/

// TODO(b/246760853): Move FIRAggregateQuery to public headers to release it.

#import "FIRAggregateSource+Internal.h"
#import "FIRAggregateQuery.h"
#import "FIRQuery.h"

@class FIRAggregateQuerySnapshot;

/**
* An `AggregateQuery` computes some aggregation statistics from the result set of a base
* `Query`.
*/
NS_SWIFT_NAME(AggregateQuery)
@interface FIRAggregateQuery : NSObject
@interface FIRAggregateQuery (/* init */)

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

/** The base `Query` for this aggregate query. */
@property(nonatomic, readonly) FIRQuery *_Nonnull query;

/**
* Executes the aggregate query and reads back the results as a `FIRAggregateQuerySnapshot`.
*
* @param source indicates where the results should be fetched from.
* @param completion a block to execute once the results have been successfully read.
* snapshot will be `nil` only if error is `non-nil`.
*/
- (void)aggregationWithSource:(FIRAggregateSource)source
completion:(void (^_Nonnull)(FIRAggregateQuerySnapshot *_Nullable snapshot,
NSError *_Nullable error))completion
NS_SWIFT_NAME(aggregation(source:completion:));
@end
19 changes: 2 additions & 17 deletions Firestore/Source/API/FIRAggregateQuerySnapshot+Internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,14 @@
* limitations under the License.
*/

// TODO(b/246760853): Move FIRAggregateQuerySnapshot to public headers to release it.

#import "FIRAggregateQuery+Internal.h"
#import "FIRAggregateQuerySnapshot.h"

@class FIRAggregateQuery;

/**
* An `AggregateQuerySnapshot` contains results of a `AggregateQuery`.
*/
NS_SWIFT_NAME(AggregateQuerySnapshot)
@interface FIRAggregateQuerySnapshot : NSObject
@interface FIRAggregateQuerySnapshot (/* init */)

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

/** The original `AggregateQuery` this snapshot is a result of. */
@property(nonatomic, readonly) FIRAggregateQuery* _Nonnull query;

/**
* The result of a document count aggregation. Null if no count aggregation is
* available in the result.
*/
@property(nonatomic, readonly) NSNumber* _Nullable count;

@end
7 changes: 0 additions & 7 deletions Firestore/Source/API/FIRQuery+Internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* limitations under the License.
*/

#import "FIRAggregateQuery+Internal.h"
#import "FIRQuery.h"

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

// TODO(b/246760853): This property will become public API.
/**
* An `AggregateQuery` counting the number of documents matching this query.
*/
@property(nonatomic, readonly) FIRAggregateQuery *count;

@end

NS_ASSUME_NONNULL_END
1 change: 1 addition & 0 deletions Firestore/Source/API/FIRQuery.mm
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <utility>
#include <vector>

#import "FIRAggregateQuery+Internal.h"
#import "FIRDocumentReference.h"
#import "FIRFirestoreErrors.h"
#import "Firestore/Source/API/FIRDocumentReference+Internal.h"
Expand Down
49 changes: 49 additions & 0 deletions Firestore/Source/Public/FirebaseFirestore/FIRAggregateQuery.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#import <Foundation/Foundation.h>

#import "FIRAggregateSource.h"

NS_ASSUME_NONNULL_BEGIN

@class FIRQuery;
@class FIRAggregateQuerySnapshot;

/**
* An `AggregateQuery` computes some aggregation statistics from the result set of a base
* `Query`.
*/
NS_SWIFT_NAME(AggregateQuery)
@interface FIRAggregateQuery : NSObject

/** The base `Query` for this aggregate query. */
@property(nonatomic, readonly) FIRQuery *query;

/**
* Executes the aggregate query and reads back the results as a `FIRAggregateQuerySnapshot`.
*
* @param source indicates where the results should be fetched from.
* @param completion a block to execute once the results have been successfully read.
* snapshot will be `nil` only if error is `non-nil`.
*/
- (void)aggregationWithSource:(FIRAggregateSource)source
completion:(void (^)(FIRAggregateQuerySnapshot *_Nullable snapshot,
NSError *_Nullable error))completion
NS_SWIFT_NAME(getAggregation(source:completion:));
@end

NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@class FIRAggregateQuery;

/**
* An `AggregateQuerySnapshot` contains results of a `AggregateQuery`.
*/
NS_SWIFT_NAME(AggregateQuerySnapshot)
@interface FIRAggregateQuerySnapshot : NSObject

/** The original `AggregateQuery` this snapshot is a result of. */
@property(nonatomic, readonly) FIRAggregateQuery* query;

/**
* The result of a document count aggregation. Null if no count aggregation is
* available in the result.
*/
@property(nonatomic, readonly) NSNumber* count;

@end

NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
* limitations under the License.
*/

// TODO(b/246760853): Move FIRAggregateSource to public headers to release it.

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

/** Configures the behavior of `AggregateQuery.aggregateWithSource(source:completion:)`. */
typedef NS_ENUM(NSUInteger, FIRAggregateSource) {
/**
Expand All @@ -28,3 +28,5 @@ typedef NS_ENUM(NSUInteger, FIRAggregateSource) {
*/
FIRAggregateSourceServer,
} NS_SWIFT_NAME(AggregateSource);

NS_ASSUME_NONNULL_END
8 changes: 8 additions & 0 deletions Firestore/Source/Public/FirebaseFirestore/FIRQuery.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#import "FIRFirestoreSource.h"
#import "FIRListenerRegistration.h"

@class FIRAggregateQuery;
@class FIRFieldPath;
@class FIRFirestore;
@class FIRQuerySnapshot;
Expand Down Expand Up @@ -542,6 +543,13 @@ NS_SWIFT_NAME(Query)
*/
- (FIRQuery *)queryEndingAtValues:(NSArray *)fieldValues NS_SWIFT_NAME(end(at:));

#pragma mark - Aggregation

/**
* An `AggregateQuery` counting the number of documents matching this query.
*/
@property(nonatomic, readonly) FIRAggregateQuery *count;

@end

NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,12 @@ let emptyBundle = """

XCTAssertNil(value, "value should be nil on success")
}

func testCount() async throws {
let collection = collectionRef()
try await collection.addDocument(data: [:])
let snapshot = try await collection.count.getAggregation(source: .server)
XCTAssertEqual(snapshot.count, 1)
}
}
#endif