Skip to content

Private function to get Hash() for QuerySnapshot, DocumentSnapshot,… #619

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 26, 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
15 changes: 14 additions & 1 deletion firestore/integration_test_internal/src/document_change_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ namespace firestore {

using DocumentChangeTest = FirestoreIntegrationTest;

std::size_t DocumentChangeHash(const DocumentChange& change) {
return change.Hash();
}

#if defined(__ANDROID__)

TEST_F(DocumentChangeTest, Construction) {
Expand Down Expand Up @@ -92,12 +96,14 @@ TEST_F(DocumentChangeTest, TestDocumentChanges) {

#endif // defined(__ANDROID__)

TEST_F(DocumentChangeTest, Equality) {
TEST_F(DocumentChangeTest, EqualityAndHashCode) {
DocumentChange invalid_change_1 = DocumentChange();
DocumentChange invalid_change_2 = DocumentChange();

EXPECT_TRUE(invalid_change_1 == invalid_change_2);
EXPECT_FALSE(invalid_change_1 != invalid_change_2);
EXPECT_EQ(DocumentChangeHash(invalid_change_1),
DocumentChangeHash(invalid_change_2));

CollectionReference collection = Collection();
Query query = collection.OrderBy("a");
Expand All @@ -123,6 +129,8 @@ TEST_F(DocumentChangeTest, Equality) {
EXPECT_TRUE(change1 != invalid_change_1);
EXPECT_FALSE(change1 != change1);
EXPECT_FALSE(change1 == invalid_change_1);
EXPECT_EQ(DocumentChangeHash(change1), DocumentChangeHash(change1));
EXPECT_NE(DocumentChangeHash(change1), DocumentChangeHash(invalid_change_1));

WriteDocument(doc2, MapFieldValue{{"a", FieldValue::Integer(2)}});
Await(listener, 2);
Expand All @@ -136,6 +144,8 @@ TEST_F(DocumentChangeTest, Equality) {
EXPECT_TRUE(change2 != invalid_change_1);
EXPECT_FALSE(change2 == change1);
EXPECT_FALSE(change2 == invalid_change_1);
EXPECT_NE(DocumentChangeHash(change2), DocumentChangeHash(change1));
EXPECT_NE(DocumentChangeHash(change2), DocumentChangeHash(invalid_change_1));

// Make doc2 ordered before doc1.
WriteDocument(doc2, MapFieldValue{{"a", FieldValue::Integer(0)}});
Expand All @@ -152,6 +162,9 @@ TEST_F(DocumentChangeTest, Equality) {
EXPECT_FALSE(change3 == change1);
EXPECT_FALSE(change3 == change2);
EXPECT_FALSE(change3 == invalid_change_1);
EXPECT_NE(DocumentChangeHash(change3), DocumentChangeHash(change1));
EXPECT_NE(DocumentChangeHash(change3), DocumentChangeHash(change2));
EXPECT_NE(DocumentChangeHash(change3), DocumentChangeHash(invalid_change_1));
}

} // namespace firestore
Expand Down
20 changes: 20 additions & 0 deletions firestore/integration_test_internal/src/document_snapshot_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ namespace firestore {

using DocumentSnapshotTest = FirestoreIntegrationTest;

std::size_t DocumentSnapshotHash(const DocumentSnapshot& snapshot) {
return snapshot.Hash();
}

#if defined(__ANDROID__)

TEST_F(DocumentSnapshotTest, Construction) {
Expand Down Expand Up @@ -68,5 +72,21 @@ TEST_F(DocumentSnapshotTest, Equality) {
EXPECT_FALSE(snap3 != snap4);
}

TEST_F(DocumentSnapshotTest, TestHashCode) {
DocumentReference doc1 = Collection("col1").Document();
DocumentReference doc2 = Collection("col1").Document();
DocumentSnapshot doc1_snap1 = ReadDocument(doc1);
DocumentSnapshot doc1_snap2 = ReadDocument(doc1);
DocumentSnapshot doc2_snap1 = ReadDocument(doc2);
DocumentSnapshot snap3 = DocumentSnapshot();
DocumentSnapshot snap4 = DocumentSnapshot();

EXPECT_EQ(DocumentSnapshotHash(doc1_snap1), DocumentSnapshotHash(doc1_snap1));
EXPECT_EQ(DocumentSnapshotHash(doc1_snap1), DocumentSnapshotHash(doc1_snap2));
EXPECT_NE(DocumentSnapshotHash(doc1_snap1), DocumentSnapshotHash(doc2_snap1));
EXPECT_NE(DocumentSnapshotHash(doc1_snap1), DocumentSnapshotHash(snap3));
EXPECT_EQ(DocumentSnapshotHash(snap3), DocumentSnapshotHash(snap4));
}

} // namespace firestore
} // namespace firebase
40 changes: 40 additions & 0 deletions firestore/integration_test_internal/src/query_snapshot_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ namespace firestore {

using QuerySnapshotTest = FirestoreIntegrationTest;

std::size_t QuerySnapshotHash(const QuerySnapshot& snapshot) {
return snapshot.Hash();
}

#if defined(__ANDROID__)

TEST_F(QuerySnapshotTest, Construction) {
Expand Down Expand Up @@ -99,5 +103,41 @@ TEST_F(QuerySnapshotTest, Equality) {
EXPECT_FALSE(snapshot7 != snapshot8);
}

TEST_F(QuerySnapshotTest, TestHashCode) {
CollectionReference collection =
Collection({{"a", {{"k", FieldValue::String("a")}}},
{"b", {{"k", FieldValue::String("b")}}},
{"c", {{"k", FieldValue::String("c")}}}});
QuerySnapshot snapshot1 = ReadDocuments(collection.Limit(2));
QuerySnapshot snapshot2 = ReadDocuments(collection.Limit(2));
QuerySnapshot snapshot3 = ReadDocuments(collection.Limit(1));
QuerySnapshot snapshot4 = ReadDocuments(collection);
QuerySnapshot snapshot5 =
ReadDocuments(collection.OrderBy("k", Query::Direction::kAscending));
QuerySnapshot snapshot6 =
ReadDocuments(collection.OrderBy("k", Query::Direction::kDescending));

QuerySnapshot snapshot7 = QuerySnapshot();
QuerySnapshot snapshot8 = QuerySnapshot();

EXPECT_EQ(QuerySnapshotHash(snapshot1), QuerySnapshotHash(snapshot1));
EXPECT_EQ(QuerySnapshotHash(snapshot1), QuerySnapshotHash(snapshot2));
EXPECT_NE(QuerySnapshotHash(snapshot1), QuerySnapshotHash(snapshot3));
EXPECT_NE(QuerySnapshotHash(snapshot1), QuerySnapshotHash(snapshot4));
EXPECT_NE(QuerySnapshotHash(snapshot1), QuerySnapshotHash(snapshot5));
EXPECT_NE(QuerySnapshotHash(snapshot1), QuerySnapshotHash(snapshot6));
EXPECT_NE(QuerySnapshotHash(snapshot3), QuerySnapshotHash(snapshot4));
EXPECT_NE(QuerySnapshotHash(snapshot3), QuerySnapshotHash(snapshot5));
EXPECT_NE(QuerySnapshotHash(snapshot3), QuerySnapshotHash(snapshot6));
EXPECT_NE(QuerySnapshotHash(snapshot5), QuerySnapshotHash(snapshot6));
EXPECT_NE(QuerySnapshotHash(snapshot1), QuerySnapshotHash(snapshot7));
EXPECT_NE(QuerySnapshotHash(snapshot2), QuerySnapshotHash(snapshot7));
EXPECT_NE(QuerySnapshotHash(snapshot3), QuerySnapshotHash(snapshot7));
EXPECT_NE(QuerySnapshotHash(snapshot4), QuerySnapshotHash(snapshot7));
EXPECT_NE(QuerySnapshotHash(snapshot5), QuerySnapshotHash(snapshot7));
EXPECT_NE(QuerySnapshotHash(snapshot6), QuerySnapshotHash(snapshot7));
EXPECT_EQ(QuerySnapshotHash(snapshot7), QuerySnapshotHash(snapshot8));
}

} // namespace firestore
} // namespace firebase
8 changes: 7 additions & 1 deletion firestore/src/android/document_change_android.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,12 @@ Method<Object> kDocument(
"getDocument", "()Lcom/google/firebase/firestore/QueryDocumentSnapshot;");
Method<size_t> kOldIndex("getOldIndex", "()I");
Method<size_t> kNewIndex("getNewIndex", "()I");
Method<int32_t> kHashCode("hashCode", "()I");

} // namespace

void DocumentChangeInternal::Initialize(jni::Loader& loader) {
loader.LoadClass(kClass, kType, kDocument, kOldIndex, kNewIndex);
loader.LoadClass(kClass, kType, kDocument, kOldIndex, kNewIndex, kHashCode);
}

Type DocumentChangeInternal::type() const {
Expand All @@ -70,6 +71,11 @@ std::size_t DocumentChangeInternal::new_index() const {
return env.Call(obj_, kNewIndex);
}

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

bool operator==(const DocumentChangeInternal& lhs,
const DocumentChangeInternal& rhs) {
return jni::EqualityCompareJni(lhs, rhs);
Expand Down
2 changes: 2 additions & 0 deletions firestore/src/android/document_change_android.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class DocumentChangeInternal : public Wrapper {
DocumentSnapshot document() const;
std::size_t old_index() const;
std::size_t new_index() const;

std::size_t Hash() const;
};

bool operator==(const DocumentChangeInternal& lhs,
Expand Down
8 changes: 7 additions & 1 deletion firestore/src/android/document_snapshot_android.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,13 @@ Method<Object> kGet("get",
"(Lcom/google/firebase/firestore/FieldPath;"
"Lcom/google/firebase/firestore/DocumentSnapshot$"
"ServerTimestampBehavior;)Ljava/lang/Object;");
Method<int32_t> kHashCode("hashCode", "()I");

} // namespace

void DocumentSnapshotInternal::Initialize(jni::Loader& loader) {
loader.LoadClass(kClass, kGetId, kGetReference, kGetMetadata, kExists,
kGetData, kContains, kGet);
kGetData, kContains, kGet, kHashCode);
}

Firestore* DocumentSnapshotInternal::firestore() const {
Expand Down Expand Up @@ -129,6 +130,11 @@ FieldValue DocumentSnapshotInternal::Get(const FieldPath& field,
return FieldValueInternal::Create(env, field_value);
}

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

bool operator==(const DocumentSnapshotInternal& lhs,
const DocumentSnapshotInternal& rhs) {
return jni::EqualityCompareJni(lhs, rhs);
Expand Down
2 changes: 2 additions & 0 deletions firestore/src/android/document_snapshot_android.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ class DocumentSnapshotInternal : public Wrapper {
FieldValue Get(const FieldPath& field,
DocumentSnapshot::ServerTimestampBehavior stb) const;

std::size_t Hash() const;

private:
mutable std::string cached_id_;
};
Expand Down
8 changes: 7 additions & 1 deletion firestore/src/android/query_snapshot_android.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,13 @@ Method<List> kGetDocumentChanges(
"(Lcom/google/firebase/firestore/MetadataChanges;)Ljava/util/List;");
Method<List> kGetDocuments("getDocuments", "()Ljava/util/List;");
Method<size_t> kSize("size", "()I");
Method<int32_t> kHashCode("hashCode", "()I");

} // namespace

void QuerySnapshotInternal::Initialize(jni::Loader& loader) {
loader.LoadClass(kClassName, kGetQuery, kGetMetadata, kGetDocumentChanges,
kGetDocuments, kSize);
kGetDocuments, kSize, kHashCode);
}

Query QuerySnapshotInternal::query() const {
Expand Down Expand Up @@ -87,6 +88,11 @@ std::size_t QuerySnapshotInternal::size() const {
return env.Call(obj_, kSize);
}

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

bool operator==(const QuerySnapshotInternal& lhs,
const QuerySnapshotInternal& rhs) {
return jni::EqualityCompareJni(lhs, rhs);
Expand Down
2 changes: 2 additions & 0 deletions firestore/src/android/query_snapshot_android.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ class QuerySnapshotInternal : public Wrapper {
* @return The number of documents in the QuerySnapshot.
*/
std::size_t size() const;

std::size_t Hash() const;
};

bool operator==(const QuerySnapshotInternal& lhs,
Expand Down
5 changes: 5 additions & 0 deletions firestore/src/common/document_change.cc
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ std::size_t DocumentChange::new_index() const {
return internal_->new_index();
}

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

bool operator==(const DocumentChange& lhs, const DocumentChange& rhs) {
return EqualityCompare(lhs.internal_, rhs.internal_);
}
Expand Down
5 changes: 5 additions & 0 deletions firestore/src/common/document_snapshot.cc
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,11 @@ std::string DocumentSnapshot::ToString() const {
", doc=" + ::firebase::firestore::ToString(GetData()) + ')';
}

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

std::ostream& operator<<(std::ostream& out, const DocumentSnapshot& document) {
return out << document.ToString();
}
Expand Down
5 changes: 5 additions & 0 deletions firestore/src/common/query_snapshot.cc
Original file line number Diff line number Diff line change
Expand Up @@ -122,5 +122,10 @@ bool operator==(const QuerySnapshot& lhs, const QuerySnapshot& rhs) {
return EqualityCompare(lhs.internal_, rhs.internal_);
}

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

} // namespace firestore
} // namespace firebase
3 changes: 3 additions & 0 deletions firestore/src/include/firebase/firestore/document_change.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,10 @@ class DocumentChange {
bool is_valid() const { return internal_ != nullptr; }

private:
std::size_t Hash() const;

friend bool operator==(const DocumentChange& lhs, const DocumentChange& rhs);
friend std::size_t DocumentChangeHash(const DocumentChange& change);

friend class FirestoreInternal;
friend class Wrapper;
Expand Down
3 changes: 3 additions & 0 deletions firestore/src/include/firebase/firestore/document_snapshot.h
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,11 @@ class DocumentSnapshot {
const DocumentSnapshot& document);

private:
std::size_t Hash() const;

friend bool operator==(const DocumentSnapshot& lhs,
const DocumentSnapshot& rhs);
friend std::size_t DocumentSnapshotHash(const DocumentSnapshot& snapshot);

friend class DocumentChangeInternal;
friend class EventListenerInternal;
Expand Down
3 changes: 3 additions & 0 deletions firestore/src/include/firebase/firestore/query_snapshot.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,10 @@ class QuerySnapshot {
bool is_valid() const { return internal_ != nullptr; }

private:
std::size_t Hash() const;

friend bool operator==(const QuerySnapshot& lhs, const QuerySnapshot& rhs);
friend std::size_t QuerySnapshotHash(const QuerySnapshot& snapshot);

friend class EventListenerInternal;
friend class FirestoreInternal;
Expand Down
2 changes: 2 additions & 0 deletions firestore/src/main/document_change_main.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ class DocumentChangeInternal {
std::size_t old_index() const;
std::size_t new_index() const;

std::size_t Hash() const { return change_.Hash(); }

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

Expand Down
2 changes: 2 additions & 0 deletions firestore/src/main/document_snapshot_main.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ class DocumentSnapshotInternal {

DocumentReference reference() const;

std::size_t Hash() const { return snapshot_.Hash(); }

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

Expand Down
2 changes: 2 additions & 0 deletions firestore/src/main/query_snapshot_main.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class QuerySnapshotInternal {

std::vector<DocumentSnapshot> documents() const;

std::size_t Hash() const { return snapshot_.Hash(); }

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

Expand Down