Skip to content

Add private function that returns a query's hash code. #613

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 1 commit into from
Aug 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
14 changes: 14 additions & 0 deletions firestore/integration_test_internal/src/query_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,20 @@ std::vector<MapFieldValue> AllDocsExcept(

} // namespace

size_t QueryHash(const Query& query) { return query.Hash(); }

TEST_F(QueryTest, TestHashCode) {
CollectionReference collection =
Collection({{"a", {{"k", FieldValue::String("a")}}},
{"b", {{"k", FieldValue::String("b")}}}});
Query query1 =
collection.Limit(2).OrderBy("sort", Query::Direction::kAscending);
Query query2 =
collection.Limit(2).OrderBy("sort", Query::Direction::kDescending);
EXPECT_NE(QueryHash(query1), QueryHash(query2));
EXPECT_EQ(QueryHash(query1), QueryHash(query1));
}

TEST_F(QueryTest, TestLimitQueries) {
CollectionReference collection =
Collection({{"a", {{"k", FieldValue::String("a")}}},
Expand Down
8 changes: 7 additions & 1 deletion firestore/src/android/query_android.cc
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ Method<Object> kAddSnapshotListener(
"Lcom/google/firebase/firestore/MetadataChanges;"
"Lcom/google/firebase/firestore/EventListener;)"
"Lcom/google/firebase/firestore/ListenerRegistration;");
Method<int32_t> kHashCode("hashCode", "()I");

} // namespace

Expand All @@ -139,7 +140,7 @@ void QueryInternal::Initialize(jni::Loader& loader) {
kGreaterThan, kGreaterThanOrEqualTo, kArrayContains, kArrayContainsAny,
kIn, kNotIn, kOrderBy, kLimit, kLimitToLast, kStartAtSnapshot, kStartAt,
kStartAfterSnapshot, kStartAfter, kEndBeforeSnapshot, kEndBefore,
kEndAtSnapshot, kEndAt, kGet, kAddSnapshotListener);
kEndAtSnapshot, kEndAt, kGet, kAddSnapshotListener, kHashCode);
}

Firestore* QueryInternal::firestore() {
Expand Down Expand Up @@ -346,6 +347,11 @@ Local<Array<Object>> QueryInternal::ConvertFieldValues(
return result;
}

size_t QueryInternal::Hash() const {
Env env = GetEnv();
return env.Call(obj_, kHashCode);
}

bool operator==(const QueryInternal& lhs, const QueryInternal& rhs) {
return jni::EqualityCompareJni(lhs, rhs);
}
Expand Down
2 changes: 2 additions & 0 deletions firestore/src/android/query_android.h
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,8 @@ class QueryInternal : public Wrapper {
EventListener<QuerySnapshot>* listener,
bool passing_listener_ownership = false);

size_t Hash() const;

protected:
PromiseFactory<AsyncFn> promises_;

Expand Down
5 changes: 5 additions & 0 deletions firestore/src/common/query.cc
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,11 @@ ListenerRegistration Query::AddSnapshotListener(
firebase::Move(callback));
}

size_t Query::Hash() const {
if (!internal_) return {};
return internal_->Hash();
}

bool operator==(const Query& lhs, const Query& rhs) {
return EqualityCompare(lhs.internal_, rhs.internal_);
}
Expand Down
3 changes: 3 additions & 0 deletions firestore/src/include/firebase/firestore/query.h
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,10 @@ class Query {
bool is_valid() const { return internal_ != nullptr; }

private:
size_t Hash() const;

friend bool operator==(const Query& lhs, const Query& rhs);
friend size_t QueryHash(const Query& query);

friend class FirestoreInternal;
friend class QueryInternal;
Expand Down
2 changes: 2 additions & 0 deletions firestore/src/main/query_main.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ class QueryInternal {
return WithBound(BoundPosition::kEndAt, values);
}

size_t Hash() const { return query_.Hash(); }

friend bool operator==(const QueryInternal& lhs, const QueryInternal& rhs);

protected:
Expand Down