Skip to content

Commit 4576bad

Browse files
authored
Fix bugs in LevelDbDocumentOverlayCache where it failed to check for equality of the DocumentKey (#9381)
1 parent a1dde8e commit 4576bad

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

Firestore/core/src/local/leveldb_document_overlay_cache.cc

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ absl::optional<Overlay> LevelDbDocumentOverlayCache::GetOverlay(
6666

6767
LevelDbDocumentOverlayKey decoded_key;
6868
HARD_ASSERT(decoded_key.Decode(it->key()));
69+
if (decoded_key.document_key() != key) {
70+
return absl::nullopt;
71+
}
72+
6973
return ParseOverlay(decoded_key, it->value());
7074
}
7175

@@ -193,7 +197,11 @@ void LevelDbDocumentOverlayCache::DeleteOverlay(const model::DocumentKey& key) {
193197
for (it->Seek(leveldb_key_prefix);
194198
it->Valid() && absl::StartsWith(it->key(), leveldb_key_prefix);
195199
it->Next()) {
196-
db_->current_transaction()->Delete(it->key());
200+
LevelDbDocumentOverlayKey decoded_key;
201+
HARD_ASSERT(decoded_key.Decode(it->key()));
202+
if (decoded_key.document_key() == key) {
203+
db_->current_transaction()->Delete(it->key());
204+
}
197205
}
198206
}
199207

Firestore/core/test/unit/local/document_overlay_cache_test.cc

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,25 @@ TEST_P(DocumentOverlayCacheTest, CanReadSavedOverlays) {
152152
});
153153
}
154154

155+
TEST_P(DocumentOverlayCacheTest, GetOverlayExactlyMatchesTheGivenDocumentKey) {
156+
this->persistence_->Run("Test", [&] {
157+
this->SaveOverlaysWithSetMutations(1, {"coll/doc1/sub/doc2"});
158+
159+
EXPECT_FALSE(
160+
this->cache_->GetOverlay(DocumentKey::FromPathString("coll/d")));
161+
EXPECT_FALSE(
162+
this->cache_->GetOverlay(DocumentKey::FromPathString("coll/doc1")));
163+
EXPECT_FALSE(
164+
this->cache_->GetOverlay(DocumentKey::FromPathString("coll/doc1ZZ")));
165+
166+
const DocumentKey document_key =
167+
DocumentKey::FromPathString("coll/doc1/sub/doc2");
168+
auto overlay_opt = this->cache_->GetOverlay(document_key);
169+
ASSERT_TRUE(overlay_opt);
170+
EXPECT_EQ(overlay_opt->key(), document_key);
171+
});
172+
}
173+
155174
TEST_P(DocumentOverlayCacheTest, SavingOverlayOverwrites) {
156175
this->persistence_->Run("Test", [&] {
157176
Mutation m1 = PatchMutation("coll/doc1", Map("foo", "bar"));
@@ -288,6 +307,34 @@ TEST_P(DocumentOverlayCacheTest, UpdateDocumentOverlay) {
288307
});
289308
}
290309

310+
TEST_P(DocumentOverlayCacheTest, SaveDoesntAffectSubCollections) {
311+
this->persistence_->Run("Test", [&] {
312+
Mutation mutation1 =
313+
PatchMutation("coll/doc/subcoll/subdoc", Map("foo", "bar1"));
314+
Mutation mutation2 = PatchMutation("coll/doc", Map("foo", "bar2"));
315+
this->SaveOverlaysWithMutations(1, {mutation1});
316+
this->SaveOverlaysWithMutations(2, {mutation2});
317+
318+
// Verify that `GetOverlay()` returns the correct mutations.
319+
{
320+
auto overlay_opt = this->cache_->GetOverlay(
321+
DocumentKey::FromPathString("coll/doc/subcoll/subdoc"));
322+
EXPECT_TRUE(overlay_opt);
323+
if (overlay_opt) {
324+
EXPECT_EQ(overlay_opt.value().mutation(), mutation1);
325+
}
326+
}
327+
{
328+
auto overlay_opt =
329+
this->cache_->GetOverlay(DocumentKey::FromPathString("coll/doc"));
330+
EXPECT_TRUE(overlay_opt);
331+
if (overlay_opt) {
332+
EXPECT_EQ(overlay_opt.value().mutation(), mutation2);
333+
}
334+
}
335+
});
336+
}
337+
291338
} // namespace
292339
} // namespace local
293340
} // namespace firestore

0 commit comments

Comments
 (0)