Skip to content

Commit 7616961

Browse files
authored
Private function to get Hash() for QuerySnapshot, DocumentSnapshot, and DocumentChange. (#619)
1 parent 6d3ec1d commit 7616961

18 files changed

+131
-4
lines changed

firestore/integration_test_internal/src/document_change_test.cc

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ namespace firestore {
2929

3030
using DocumentChangeTest = FirestoreIntegrationTest;
3131

32+
std::size_t DocumentChangeHash(const DocumentChange& change) {
33+
return change.Hash();
34+
}
35+
3236
#if defined(__ANDROID__)
3337

3438
TEST_F(DocumentChangeTest, Construction) {
@@ -92,12 +96,14 @@ TEST_F(DocumentChangeTest, TestDocumentChanges) {
9296

9397
#endif // defined(__ANDROID__)
9498

95-
TEST_F(DocumentChangeTest, Equality) {
99+
TEST_F(DocumentChangeTest, EqualityAndHashCode) {
96100
DocumentChange invalid_change_1 = DocumentChange();
97101
DocumentChange invalid_change_2 = DocumentChange();
98102

99103
EXPECT_TRUE(invalid_change_1 == invalid_change_2);
100104
EXPECT_FALSE(invalid_change_1 != invalid_change_2);
105+
EXPECT_EQ(DocumentChangeHash(invalid_change_1),
106+
DocumentChangeHash(invalid_change_2));
101107

102108
CollectionReference collection = Collection();
103109
Query query = collection.OrderBy("a");
@@ -123,6 +129,8 @@ TEST_F(DocumentChangeTest, Equality) {
123129
EXPECT_TRUE(change1 != invalid_change_1);
124130
EXPECT_FALSE(change1 != change1);
125131
EXPECT_FALSE(change1 == invalid_change_1);
132+
EXPECT_EQ(DocumentChangeHash(change1), DocumentChangeHash(change1));
133+
EXPECT_NE(DocumentChangeHash(change1), DocumentChangeHash(invalid_change_1));
126134

127135
WriteDocument(doc2, MapFieldValue{{"a", FieldValue::Integer(2)}});
128136
Await(listener, 2);
@@ -136,6 +144,8 @@ TEST_F(DocumentChangeTest, Equality) {
136144
EXPECT_TRUE(change2 != invalid_change_1);
137145
EXPECT_FALSE(change2 == change1);
138146
EXPECT_FALSE(change2 == invalid_change_1);
147+
EXPECT_NE(DocumentChangeHash(change2), DocumentChangeHash(change1));
148+
EXPECT_NE(DocumentChangeHash(change2), DocumentChangeHash(invalid_change_1));
139149

140150
// Make doc2 ordered before doc1.
141151
WriteDocument(doc2, MapFieldValue{{"a", FieldValue::Integer(0)}});
@@ -152,6 +162,9 @@ TEST_F(DocumentChangeTest, Equality) {
152162
EXPECT_FALSE(change3 == change1);
153163
EXPECT_FALSE(change3 == change2);
154164
EXPECT_FALSE(change3 == invalid_change_1);
165+
EXPECT_NE(DocumentChangeHash(change3), DocumentChangeHash(change1));
166+
EXPECT_NE(DocumentChangeHash(change3), DocumentChangeHash(change2));
167+
EXPECT_NE(DocumentChangeHash(change3), DocumentChangeHash(invalid_change_1));
155168
}
156169

157170
} // namespace firestore

firestore/integration_test_internal/src/document_snapshot_test.cc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ namespace firestore {
3232

3333
using DocumentSnapshotTest = FirestoreIntegrationTest;
3434

35+
std::size_t DocumentSnapshotHash(const DocumentSnapshot& snapshot) {
36+
return snapshot.Hash();
37+
}
38+
3539
#if defined(__ANDROID__)
3640

3741
TEST_F(DocumentSnapshotTest, Construction) {
@@ -68,5 +72,21 @@ TEST_F(DocumentSnapshotTest, Equality) {
6872
EXPECT_FALSE(snap3 != snap4);
6973
}
7074

75+
TEST_F(DocumentSnapshotTest, TestHashCode) {
76+
DocumentReference doc1 = Collection("col1").Document();
77+
DocumentReference doc2 = Collection("col1").Document();
78+
DocumentSnapshot doc1_snap1 = ReadDocument(doc1);
79+
DocumentSnapshot doc1_snap2 = ReadDocument(doc1);
80+
DocumentSnapshot doc2_snap1 = ReadDocument(doc2);
81+
DocumentSnapshot snap3 = DocumentSnapshot();
82+
DocumentSnapshot snap4 = DocumentSnapshot();
83+
84+
EXPECT_EQ(DocumentSnapshotHash(doc1_snap1), DocumentSnapshotHash(doc1_snap1));
85+
EXPECT_EQ(DocumentSnapshotHash(doc1_snap1), DocumentSnapshotHash(doc1_snap2));
86+
EXPECT_NE(DocumentSnapshotHash(doc1_snap1), DocumentSnapshotHash(doc2_snap1));
87+
EXPECT_NE(DocumentSnapshotHash(doc1_snap1), DocumentSnapshotHash(snap3));
88+
EXPECT_EQ(DocumentSnapshotHash(snap3), DocumentSnapshotHash(snap4));
89+
}
90+
7191
} // namespace firestore
7292
} // namespace firebase

firestore/integration_test_internal/src/query_snapshot_test.cc

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ namespace firestore {
3131

3232
using QuerySnapshotTest = FirestoreIntegrationTest;
3333

34+
std::size_t QuerySnapshotHash(const QuerySnapshot& snapshot) {
35+
return snapshot.Hash();
36+
}
37+
3438
#if defined(__ANDROID__)
3539

3640
TEST_F(QuerySnapshotTest, Construction) {
@@ -99,5 +103,41 @@ TEST_F(QuerySnapshotTest, Equality) {
99103
EXPECT_FALSE(snapshot7 != snapshot8);
100104
}
101105

106+
TEST_F(QuerySnapshotTest, TestHashCode) {
107+
CollectionReference collection =
108+
Collection({{"a", {{"k", FieldValue::String("a")}}},
109+
{"b", {{"k", FieldValue::String("b")}}},
110+
{"c", {{"k", FieldValue::String("c")}}}});
111+
QuerySnapshot snapshot1 = ReadDocuments(collection.Limit(2));
112+
QuerySnapshot snapshot2 = ReadDocuments(collection.Limit(2));
113+
QuerySnapshot snapshot3 = ReadDocuments(collection.Limit(1));
114+
QuerySnapshot snapshot4 = ReadDocuments(collection);
115+
QuerySnapshot snapshot5 =
116+
ReadDocuments(collection.OrderBy("k", Query::Direction::kAscending));
117+
QuerySnapshot snapshot6 =
118+
ReadDocuments(collection.OrderBy("k", Query::Direction::kDescending));
119+
120+
QuerySnapshot snapshot7 = QuerySnapshot();
121+
QuerySnapshot snapshot8 = QuerySnapshot();
122+
123+
EXPECT_EQ(QuerySnapshotHash(snapshot1), QuerySnapshotHash(snapshot1));
124+
EXPECT_EQ(QuerySnapshotHash(snapshot1), QuerySnapshotHash(snapshot2));
125+
EXPECT_NE(QuerySnapshotHash(snapshot1), QuerySnapshotHash(snapshot3));
126+
EXPECT_NE(QuerySnapshotHash(snapshot1), QuerySnapshotHash(snapshot4));
127+
EXPECT_NE(QuerySnapshotHash(snapshot1), QuerySnapshotHash(snapshot5));
128+
EXPECT_NE(QuerySnapshotHash(snapshot1), QuerySnapshotHash(snapshot6));
129+
EXPECT_NE(QuerySnapshotHash(snapshot3), QuerySnapshotHash(snapshot4));
130+
EXPECT_NE(QuerySnapshotHash(snapshot3), QuerySnapshotHash(snapshot5));
131+
EXPECT_NE(QuerySnapshotHash(snapshot3), QuerySnapshotHash(snapshot6));
132+
EXPECT_NE(QuerySnapshotHash(snapshot5), QuerySnapshotHash(snapshot6));
133+
EXPECT_NE(QuerySnapshotHash(snapshot1), QuerySnapshotHash(snapshot7));
134+
EXPECT_NE(QuerySnapshotHash(snapshot2), QuerySnapshotHash(snapshot7));
135+
EXPECT_NE(QuerySnapshotHash(snapshot3), QuerySnapshotHash(snapshot7));
136+
EXPECT_NE(QuerySnapshotHash(snapshot4), QuerySnapshotHash(snapshot7));
137+
EXPECT_NE(QuerySnapshotHash(snapshot5), QuerySnapshotHash(snapshot7));
138+
EXPECT_NE(QuerySnapshotHash(snapshot6), QuerySnapshotHash(snapshot7));
139+
EXPECT_EQ(QuerySnapshotHash(snapshot7), QuerySnapshotHash(snapshot8));
140+
}
141+
102142
} // namespace firestore
103143
} // namespace firebase

firestore/src/android/document_change_android.cc

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,12 @@ Method<Object> kDocument(
4141
"getDocument", "()Lcom/google/firebase/firestore/QueryDocumentSnapshot;");
4242
Method<size_t> kOldIndex("getOldIndex", "()I");
4343
Method<size_t> kNewIndex("getNewIndex", "()I");
44+
Method<int32_t> kHashCode("hashCode", "()I");
4445

4546
} // namespace
4647

4748
void DocumentChangeInternal::Initialize(jni::Loader& loader) {
48-
loader.LoadClass(kClass, kType, kDocument, kOldIndex, kNewIndex);
49+
loader.LoadClass(kClass, kType, kDocument, kOldIndex, kNewIndex, kHashCode);
4950
}
5051

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

74+
std::size_t DocumentChangeInternal::Hash() const {
75+
Env env = GetEnv();
76+
return env.Call(obj_, kHashCode);
77+
}
78+
7379
bool operator==(const DocumentChangeInternal& lhs,
7480
const DocumentChangeInternal& rhs) {
7581
return jni::EqualityCompareJni(lhs, rhs);

firestore/src/android/document_change_android.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ class DocumentChangeInternal : public Wrapper {
3636
DocumentSnapshot document() const;
3737
std::size_t old_index() const;
3838
std::size_t new_index() const;
39+
40+
std::size_t Hash() const;
3941
};
4042

4143
bool operator==(const DocumentChangeInternal& lhs,

firestore/src/android/document_snapshot_android.cc

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,13 @@ Method<Object> kGet("get",
5959
"(Lcom/google/firebase/firestore/FieldPath;"
6060
"Lcom/google/firebase/firestore/DocumentSnapshot$"
6161
"ServerTimestampBehavior;)Ljava/lang/Object;");
62+
Method<int32_t> kHashCode("hashCode", "()I");
6263

6364
} // namespace
6465

6566
void DocumentSnapshotInternal::Initialize(jni::Loader& loader) {
6667
loader.LoadClass(kClass, kGetId, kGetReference, kGetMetadata, kExists,
67-
kGetData, kContains, kGet);
68+
kGetData, kContains, kGet, kHashCode);
6869
}
6970

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

133+
std::size_t DocumentSnapshotInternal::Hash() const {
134+
Env env = GetEnv();
135+
return env.Call(obj_, kHashCode);
136+
}
137+
132138
bool operator==(const DocumentSnapshotInternal& lhs,
133139
const DocumentSnapshotInternal& rhs) {
134140
return jni::EqualityCompareJni(lhs, rhs);

firestore/src/android/document_snapshot_android.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ class DocumentSnapshotInternal : public Wrapper {
6060
FieldValue Get(const FieldPath& field,
6161
DocumentSnapshot::ServerTimestampBehavior stb) const;
6262

63+
std::size_t Hash() const;
64+
6365
private:
6466
mutable std::string cached_id_;
6567
};

firestore/src/android/query_snapshot_android.cc

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,13 @@ Method<List> kGetDocumentChanges(
4848
"(Lcom/google/firebase/firestore/MetadataChanges;)Ljava/util/List;");
4949
Method<List> kGetDocuments("getDocuments", "()Ljava/util/List;");
5050
Method<size_t> kSize("size", "()I");
51+
Method<int32_t> kHashCode("hashCode", "()I");
5152

5253
} // namespace
5354

5455
void QuerySnapshotInternal::Initialize(jni::Loader& loader) {
5556
loader.LoadClass(kClassName, kGetQuery, kGetMetadata, kGetDocumentChanges,
56-
kGetDocuments, kSize);
57+
kGetDocuments, kSize, kHashCode);
5758
}
5859

5960
Query QuerySnapshotInternal::query() const {
@@ -87,6 +88,11 @@ std::size_t QuerySnapshotInternal::size() const {
8788
return env.Call(obj_, kSize);
8889
}
8990

91+
std::size_t QuerySnapshotInternal::Hash() const {
92+
Env env = GetEnv();
93+
return env.Call(obj_, kHashCode);
94+
}
95+
9096
bool operator==(const QuerySnapshotInternal& lhs,
9197
const QuerySnapshotInternal& rhs) {
9298
return jni::EqualityCompareJni(lhs, rhs);

firestore/src/android/query_snapshot_android.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ class QuerySnapshotInternal : public Wrapper {
7777
* @return The number of documents in the QuerySnapshot.
7878
*/
7979
std::size_t size() const;
80+
81+
std::size_t Hash() const;
8082
};
8183

8284
bool operator==(const QuerySnapshotInternal& lhs,

firestore/src/common/document_change.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,11 @@ std::size_t DocumentChange::new_index() const {
119119
return internal_->new_index();
120120
}
121121

122+
size_t DocumentChange::Hash() const {
123+
if (!internal_) return {};
124+
return internal_->Hash();
125+
}
126+
122127
bool operator==(const DocumentChange& lhs, const DocumentChange& rhs) {
123128
return EqualityCompare(lhs.internal_, rhs.internal_);
124129
}

firestore/src/common/document_snapshot.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,11 @@ std::string DocumentSnapshot::ToString() const {
151151
", doc=" + ::firebase::firestore::ToString(GetData()) + ')';
152152
}
153153

154+
size_t DocumentSnapshot::Hash() const {
155+
if (!internal_) return {};
156+
return internal_->Hash();
157+
}
158+
154159
std::ostream& operator<<(std::ostream& out, const DocumentSnapshot& document) {
155160
return out << document.ToString();
156161
}

firestore/src/common/query_snapshot.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,5 +122,10 @@ bool operator==(const QuerySnapshot& lhs, const QuerySnapshot& rhs) {
122122
return EqualityCompare(lhs.internal_, rhs.internal_);
123123
}
124124

125+
size_t QuerySnapshot::Hash() const {
126+
if (!internal_) return {};
127+
return internal_->Hash();
128+
}
129+
125130
} // namespace firestore
126131
} // namespace firebase

firestore/src/include/firebase/firestore/document_change.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,10 @@ class DocumentChange {
173173
bool is_valid() const { return internal_ != nullptr; }
174174

175175
private:
176+
std::size_t Hash() const;
177+
176178
friend bool operator==(const DocumentChange& lhs, const DocumentChange& rhs);
179+
friend std::size_t DocumentChangeHash(const DocumentChange& change);
177180

178181
friend class FirestoreInternal;
179182
friend class Wrapper;

firestore/src/include/firebase/firestore/document_snapshot.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,11 @@ class DocumentSnapshot {
261261
const DocumentSnapshot& document);
262262

263263
private:
264+
std::size_t Hash() const;
265+
264266
friend bool operator==(const DocumentSnapshot& lhs,
265267
const DocumentSnapshot& rhs);
268+
friend std::size_t DocumentSnapshotHash(const DocumentSnapshot& snapshot);
266269

267270
friend class DocumentChangeInternal;
268271
friend class EventListenerInternal;

firestore/src/include/firebase/firestore/query_snapshot.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,10 @@ class QuerySnapshot {
164164
bool is_valid() const { return internal_ != nullptr; }
165165

166166
private:
167+
std::size_t Hash() const;
168+
167169
friend bool operator==(const QuerySnapshot& lhs, const QuerySnapshot& rhs);
170+
friend std::size_t QuerySnapshotHash(const QuerySnapshot& snapshot);
168171

169172
friend class EventListenerInternal;
170173
friend class FirestoreInternal;

firestore/src/main/document_change_main.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ class DocumentChangeInternal {
4141
std::size_t old_index() const;
4242
std::size_t new_index() const;
4343

44+
std::size_t Hash() const { return change_.Hash(); }
45+
4446
friend bool operator==(const DocumentChangeInternal& lhs,
4547
const DocumentChangeInternal& rhs);
4648

firestore/src/main/document_snapshot_main.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ class DocumentSnapshotInternal {
6363

6464
DocumentReference reference() const;
6565

66+
std::size_t Hash() const { return snapshot_.Hash(); }
67+
6668
friend bool operator==(const DocumentSnapshotInternal& lhs,
6769
const DocumentSnapshotInternal& rhs);
6870

firestore/src/main/query_snapshot_main.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ class QuerySnapshotInternal {
5252

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

55+
std::size_t Hash() const { return snapshot_.Hash(); }
56+
5557
friend bool operator==(const QuerySnapshotInternal& lhs,
5658
const QuerySnapshotInternal& rhs);
5759

0 commit comments

Comments
 (0)