Skip to content

Commit

Permalink
Public count (#10246)
Browse files Browse the repository at this point in the history
* Public Count

* Swift Test Commit

* Swift Format

* No extra whitespace

* Hopefully formatted.

* Change log and feedback.
  • Loading branch information
wu-hui authored Sep 23, 2022
1 parent 747eac2 commit 8aae6be
Show file tree
Hide file tree
Showing 11 changed files with 118 additions and 54 deletions.
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

0 comments on commit 8aae6be

Please sign in to comment.
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy