Skip to content

Commit cf04e67

Browse files
committed
Fix bug where SaveOverlay() would not correctly update a previous entry for the given document
1 parent 8afdb23 commit cf04e67

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

Firestore/core/src/local/leveldb_document_overlay_cache.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,12 +168,24 @@ Overlay LevelDbDocumentOverlayCache::ParseOverlay(
168168
void LevelDbDocumentOverlayCache::SaveOverlay(int largest_batch_id,
169169
const DocumentKey& key,
170170
const Mutation& mutation) {
171+
DeleteOverlay(key);
171172
const std::string leveldb_key =
172173
LevelDbDocumentOverlayKey::Key(user_id_, key, largest_batch_id);
173174
auto serialized_mutation = serializer_->EncodeMutation(mutation);
174175
db_->current_transaction()->Put(leveldb_key, serialized_mutation);
175176
}
176177

178+
void LevelDbDocumentOverlayCache::DeleteOverlay(const model::DocumentKey& key) {
179+
const std::string leveldb_key_prefix =
180+
LevelDbDocumentOverlayKey::KeyPrefix(user_id_, key);
181+
auto it = db_->current_transaction()->NewIterator();
182+
for (it->Seek(leveldb_key_prefix);
183+
it->Valid() && absl::StartsWith(it->key(), leveldb_key_prefix);
184+
it->Next()) {
185+
db_->current_transaction()->Delete(it->key());
186+
}
187+
}
188+
177189
void LevelDbDocumentOverlayCache::ForEachOverlay(
178190
std::function<void(absl::string_view encoded_key,
179191
const LevelDbDocumentOverlayKey& decoded_key,

Firestore/core/src/local/leveldb_document_overlay_cache.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ class LevelDbDocumentOverlayCache final : public DocumentOverlayCache {
7575
const model::DocumentKey& key,
7676
const model::Mutation& mutation);
7777

78+
void DeleteOverlay(const model::DocumentKey& key);
79+
7880
void ForEachOverlay(
7981
std::function<void(absl::string_view encoded_key,
8082
const LevelDbDocumentOverlayKey& decoded_key,

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,28 @@ TEST_P(DocumentOverlayCacheTest,
266266
});
267267
}
268268

269+
TEST_P(DocumentOverlayCacheTest, UpdateDocumentOverlay) {
270+
this->persistence_->Run("Test", [&] {
271+
Mutation mutation1 = PatchMutation("coll/doc", Map("foo", "bar1"));
272+
Mutation mutation2 = PatchMutation("coll/doc", Map("foo", "bar2"));
273+
this->SaveOverlaysWithMutations(1, {mutation1});
274+
this->SaveOverlaysWithMutations(2, {mutation2});
275+
276+
// Verify that `GetOverlay()` returns the updated mutation.
277+
auto overlay_opt =
278+
this->cache_->GetOverlay(DocumentKey::FromPathString("coll/doc"));
279+
EXPECT_TRUE(overlay_opt);
280+
if (overlay_opt) {
281+
EXPECT_EQ(mutation2, overlay_opt.value().mutation());
282+
}
283+
284+
// Verify that `RemoveOverlaysForBatchId()` removes the overlay completely.
285+
this->cache_->RemoveOverlaysForBatchId(2);
286+
EXPECT_FALSE(
287+
this->cache_->GetOverlay(DocumentKey::FromPathString("coll/doc")));
288+
});
289+
}
290+
269291
} // namespace
270292
} // namespace local
271293
} // namespace firestore

0 commit comments

Comments
 (0)