Skip to content
Open
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
Prev Previous commit
Next Next commit
feat: Add sum method to AggregateQuerySnapshot
  • Loading branch information
KKimj committed Apr 1, 2025
commit f89d482a92227efd66a889fffc0e158176bf63f2
3 changes: 2 additions & 1 deletion firestore/src/android/aggregate_query_android.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ class AggregateQueryInternal : public Wrapper {
// Firestore Future manager.
enum class AsyncFn {
kGet = 0,
kCount, // Must be the last enum value.
kCount,
kSum, // Must be the last enum value.
};

static void Initialize(jni::Loader& loader);
Expand Down
1 change: 1 addition & 0 deletions firestore/src/android/aggregate_query_snapshot_android.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class AggregateQuerySnapshotInternal : public Wrapper {

AggregateQuery query() const;
int64_t count() const;
double sum(const AggregateField& aggregate_field) const;

std::size_t Hash() const;

Expand Down
5 changes: 5 additions & 0 deletions firestore/src/common/aggregate_query_snapshot.cc
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ int64_t AggregateQuerySnapshot::count() const {
return internal_->count();
}

double AggregateQuerySnapshot::sum(const AggregateField& aggregate_field) const {
if (!internal_) return 0;
return internal_->sum(aggregate_field);
}

bool operator==(const AggregateQuerySnapshot& lhs,
const AggregateQuerySnapshot& rhs) {
return EqualityCompare(lhs.internal_, rhs.internal_);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,14 @@ class AggregateQuerySnapshot {
*/
virtual int64_t count() const;

/**
* @brief Returns the sum of the specified field across all documents in the
* result set of the underlying query.
*
* @return The sum of the specified field across all documents.
*/
virtual double sum(const AggregateField& aggregate_field) const;

/**
* @brief Returns true if this `AggregateQuerySnapshot` is valid, false if it
* is not valid. An invalid `AggregateQuerySnapshot` could be the result of:
Expand Down
1 change: 1 addition & 0 deletions firestore/src/main/aggregate_query_main.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class AggregateQueryInternal {
enum class AsyncApis {
kGet,
kCount,
kSum,
};

friend class AggregateQuerySnapshotTest;
Expand Down
16 changes: 14 additions & 2 deletions firestore/src/main/aggregate_query_snapshot_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,15 @@ namespace firebase {
namespace firestore {

AggregateQuerySnapshotInternal::AggregateQuerySnapshotInternal(
api::AggregateQuery&& aggregate_query, int64_t count_result)
api::AggregateQuery&& aggregate_query, int64_t count_result,
// TODO: Update with sum
// double sum_result,
)
: aggregate_query_(std::move(aggregate_query)),
count_result_(count_result) {}
count_result_(count_result)
// TODO: Update with sum
// sum_result_(sum_result),
{}

FirestoreInternal* AggregateQuerySnapshotInternal::firestore_internal() {
return GetFirestoreInternal(&aggregate_query_.query());
Expand All @@ -43,10 +49,16 @@ AggregateQuery AggregateQuerySnapshotInternal::query() const {

int64_t AggregateQuerySnapshotInternal::count() const { return count_result_; }

double AggregateQuerySnapshotInternal::sum(const AggregateField& aggregate_field) const {
// TODO: implement
}

bool operator==(const AggregateQuerySnapshotInternal& lhs,
const AggregateQuerySnapshotInternal& rhs) {
return lhs.aggregate_query_ == rhs.aggregate_query_ &&
lhs.count_result_ == rhs.count_result_;
// TODO: Update with sum
// && lhs.sum_result == rhs.sum_result_;
}

} // namespace firestore
Expand Down
6 changes: 6 additions & 0 deletions firestore/src/main/aggregate_query_snapshot_main.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,20 @@ class AggregateQuerySnapshotInternal {
public:
explicit AggregateQuerySnapshotInternal(api::AggregateQuery&& aggregate_query,
int64_t count);
// TODO: Update with sum
// double sum);

FirestoreInternal* firestore_internal();
const FirestoreInternal* firestore_internal() const;

AggregateQuery query() const;
int64_t count() const;
double sum(const AggregateField& aggregate_field) const;

std::size_t Hash() const {
return util::Hash(aggregate_query_.query().Hash(), count_result_);
// TODO: Update with sum
// , sum_result_);
}

friend bool operator==(const AggregateQuerySnapshotInternal& lhs,
Expand All @@ -52,6 +57,7 @@ class AggregateQuerySnapshotInternal {
private:
api::AggregateQuery aggregate_query_;
int64_t count_result_ = 0;
double sum_result_ = 0.0;
};

inline bool operator!=(const AggregateQuerySnapshotInternal& lhs,
Expand Down