Skip to content

Commit bd2d1de

Browse files
authored
Fix marking nodes as dirty when inserting into an existing hashmap entry (#4959)
* Fix marking nodes as dirty when inserting into an existing hashmap entry * remove logs
1 parent 317abed commit bd2d1de

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

packages/stores/src/impls/btreemap.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,15 @@ impl<Lens: Readable<Target = BTreeMap<K, V>> + 'static, K: 'static, V: 'static>
131131
K: Ord,
132132
Lens: Writable,
133133
{
134-
self.selector().mark_dirty_shallow();
134+
// TODO: This method was released in 0.7 without the hash bound so we don't have a way
135+
// to mark only the existing value as dirty. Instead we need to check if the value already exists
136+
// in the map and mark the whole map as dirty if it does.
137+
// In the 0.8 release, we should change this method to only mark the existing value as dirty.
138+
if self.peek().contains_key(&key) {
139+
self.selector().mark_dirty();
140+
} else {
141+
self.selector().mark_dirty_shallow();
142+
}
135143
self.selector().write_untracked().insert(key, value);
136144
}
137145

packages/stores/src/impls/hashmap.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,13 @@ impl<Lens: Readable<Target = HashMap<K, V, St>> + 'static, K: 'static, V: 'stati
139139
St: BuildHasher,
140140
Lens: Writable,
141141
{
142+
// Mark the store itself as dirty since the keys may have changed
142143
self.selector().mark_dirty_shallow();
144+
// Mark the existing value as dirty if it exists
145+
self.selector()
146+
.as_ref()
147+
.hash_child_unmapped(key.borrow())
148+
.mark_dirty();
143149
self.selector().write_untracked().insert(key, value);
144150
}
145151

packages/stores/src/scope.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,15 @@ impl<Lens> SelectorScope<Lens> {
185185
{
186186
self.write.write_unchecked()
187187
}
188+
189+
/// Borrow the writer
190+
pub(crate) fn as_ref(&self) -> SelectorScope<&Lens> {
191+
SelectorScope {
192+
path: self.path,
193+
store: self.store,
194+
write: &self.write,
195+
}
196+
}
188197
}
189198

190199
impl<Lens: Readable> Readable for SelectorScope<Lens> {

0 commit comments

Comments
 (0)