Skip to content

Commit dc8322d

Browse files
committed
I think I have the optimistic update thing down?
1 parent b858ab3 commit dc8322d

File tree

6 files changed

+137
-88
lines changed

6 files changed

+137
-88
lines changed

shared/src/sync_engine/optimistic/db/object_store.rs

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,20 @@ where
6262
if self.optimistic_changes.deletes.latest::<S>(id).is_some() {
6363
return Ok(None);
6464
}
65-
let optimistically_updated = self.optimistic_changes.updates.latest_downcasted::<S>(id);
65+
let optimistically_updated = self.optimistic_changes.updates.latest_downcasted::<S>(&id);
6666
if let Some(o) = optimistically_updated {
6767
return Ok(Some(MaybeOptimistic::new(o, true)));
6868
}
69-
let optimistically_created = self.optimistic_changes.creations.latest_downcasted::<S>(id);
69+
let optimistically_created = self
70+
.optimistic_changes
71+
.creations
72+
.latest_downcasted::<S>(id);
7073
if let Some(o) = optimistically_created {
7174
return Ok(Some(MaybeOptimistic::new(o, true)));
7275
};
7376

7477
self.inner
75-
.get(id)
78+
.get(&id)
7679
.await
7780
.map_err(|e| Error::new(e, self.location))
7881
.map(|o| o.map(|o| MaybeOptimistic::new(o, false)))
@@ -152,25 +155,28 @@ where
152155
S: Store + 'static,
153156
Mode: TxnMode<SupportsReadWrite = Present>,
154157
{
155-
pub async fn delete(&self, id: &S::Id) -> Result<(), Error> {
156-
self.inner
157-
.delete(id)
158-
.await
159-
.map_err(|e| Error::new(e, self.location))?;
160-
self.optimistic_changes.remove_successful_for_id::<S>(id);
161-
158+
pub fn add_to_reactivity_during_write(&self, id: &S::Id) {
162159
let serialized_id = SerializedId::new_from_id::<S>(id);
163160
let optimistic_id = self
164161
.optimistic_changes
165-
.get_realistic_to_optimistic_for_creations()
166-
.get(&serialized_id)
167-
.map(|i| i.clone());
162+
.get_realistic_to_optimistic_for_creations::<S>(id)
163+
.map(|i| SerializedId::new_from_id::<S>(&i));
168164

169-
let mut reactivity_trackers = self.reactivity_trackers.borrow_mut();
170-
reactivity_trackers.add_modification(S::NAME, serialized_id);
171165
if let Some(optimistic_id) = optimistic_id {
166+
let mut reactivity_trackers = self.reactivity_trackers.borrow_mut();
167+
reactivity_trackers.add_modification(S::NAME, serialized_id);
172168
reactivity_trackers.add_modification(S::NAME, optimistic_id);
173169
}
170+
}
171+
172+
pub async fn delete(&self, id: &S::Id) -> Result<(), Error> {
173+
self.inner
174+
.delete(id)
175+
.await
176+
.map_err(|e| Error::new(e, self.location))?;
177+
self.optimistic_changes.remove_successful_for_id::<S>(id);
178+
179+
self.add_to_reactivity_during_write(id);
174180

175181
Ok(())
176182
}
@@ -183,18 +189,7 @@ where
183189
self.optimistic_changes
184190
.remove_successful_for_id::<S>(item.id());
185191

186-
let serialized_id = SerializedId::new_from_row(item);
187-
let optimistic_id = self
188-
.optimistic_changes
189-
.get_realistic_to_optimistic_for_creations()
190-
.get(&serialized_id)
191-
.map(|i| i.clone());
192-
193-
let mut reactivity_trackers = self.reactivity_trackers.borrow_mut();
194-
reactivity_trackers.add_modification(S::NAME, serialized_id);
195-
if let Some(optimistic_id) = optimistic_id {
196-
reactivity_trackers.add_modification(S::NAME, optimistic_id);
197-
}
192+
self.add_to_reactivity_during_write(item.id());
198193

199194
Ok(())
200195
}

shared/src/sync_engine/optimistic/db/reactivity_trackers.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ impl SerializedId {
2424
pub fn new_from_id<S: Store>(id: &S::Id) -> Self {
2525
Self(serde_json::to_string(&id).expect("did not expect ids not to be json serializable?"))
2626
}
27+
28+
pub fn to_unserialized_id<S: Store>(&self) -> S::Id {
29+
serde_json::from_str(&self.0).expect("did not expect ids not to be json de-serializable?")
30+
}
2731
}
2832

2933
#[derive(Debug, Clone, Default)]

shared/src/sync_engine/optimistic/optimistic_change_map.rs

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,16 @@ mod status {
6868
struct OptimisticChangeMapInner<T, RealisticId> {
6969
changes:
7070
HashMap<StoreName, HashMap<SerializedId, BTreeMap<MonotonicTime, Status<T, RealisticId>>>>,
71-
realistic_to_optimistic: HashMap<RealisticId, SerializedId>,
71+
optimistic_to_realistic: HashMap<RealisticId, SerializedId>,
7272
}
7373

74-
impl<T, RealisticId> Default for OptimisticChangeMapInner<T, RealisticId> {
74+
impl<T, RealisticId: std::cmp::Eq + std::hash::Hash> Default
75+
for OptimisticChangeMapInner<T, RealisticId>
76+
{
7577
fn default() -> Self {
7678
Self {
7779
changes: Default::default(),
78-
realistic_to_optimistic: Default::default(),
80+
optimistic_to_realistic: HashMap::new(),
7981
}
8082
}
8183
}
@@ -92,7 +94,7 @@ impl<T, S> Clone for OptimisticChangeMap<T, S> {
9294
}
9395
}
9496

95-
impl<T, S> Default for OptimisticChangeMap<T, S> {
97+
impl<T, S: std::cmp::Eq + std::hash::Hash + std::fmt::Debug> Default for OptimisticChangeMap<T, S> {
9698
fn default() -> Self {
9799
Self {
98100
inner: Default::default(),
@@ -145,45 +147,44 @@ impl<T, RealisticId: Eq + std::fmt::Debug> OptimisticChangeMap<T, RealisticId> {
145147
}
146148
}
147149

148-
pub fn remove_all_realistic<S: Store>(
149-
&self,
150-
optimistic_id: &S::Id,
151-
realistic_id: &RealisticId,
152-
) {
153-
let optimistic_id = SerializedId::new_from_id::<S>(optimistic_id);
150+
pub fn remove_all_realistic<S: Store>(&self, realistic_id: &RealisticId) {
154151
let mut by_id_len = None;
155152
let mut inner = self.inner.write();
156153
if let Some(by_id) = inner.changes.get_mut(&S::NAME) {
157-
let mut by_time_len = None;
158-
if let Some(by_time) = by_id.get_mut(&optimistic_id) {
159-
let to_remove_keys = by_time
160-
.iter()
161-
.filter_map(|(time, status)| match status {
162-
Status::Realistic {
163-
realistic_id: realistic_id_candidate,
164-
..
165-
} if realistic_id_candidate == realistic_id => Some(time),
166-
_ => None,
167-
})
168-
.cloned()
169-
.collect::<Vec<_>>();
170-
for key in to_remove_keys {
171-
by_time.remove(&key);
154+
for optimistic_id in by_id.keys().cloned().collect::<Vec<_>>().into_iter() {
155+
let mut by_time_len = None;
156+
if let Some(by_time) = by_id.get_mut(&optimistic_id) {
157+
let to_remove_keys = by_time
158+
.iter()
159+
.filter_map(|(time, status)| match status {
160+
Status::Realistic {
161+
realistic_id: realistic_id_candidate,
162+
..
163+
} if realistic_id_candidate == realistic_id => Some(time),
164+
_ => None,
165+
})
166+
.cloned()
167+
.collect::<Vec<_>>();
168+
for key in to_remove_keys {
169+
by_time.remove(&key);
170+
}
171+
by_time_len = Some(by_time.len());
172172
}
173-
by_time_len = Some(by_time.len());
174-
}
175-
if by_time_len == Some(0) {
176-
by_id.remove(&optimistic_id);
173+
if by_time_len == Some(0) {
174+
by_id.remove(&optimistic_id);
175+
}
176+
by_id_len = Some(by_id.len());
177177
}
178-
by_id_len = Some(by_id.len());
179178
}
180179
if by_id_len == Some(0) {
181180
inner.changes.remove(&S::NAME);
182181
}
183182
}
184183
}
185184

186-
impl<T, RealisticId: Clone + Eq + std::hash::Hash> OptimisticChangeMap<T, RealisticId> {
185+
impl<T, RealisticId: Clone + Eq + std::hash::Hash + std::fmt::Debug>
186+
OptimisticChangeMap<T, RealisticId>
187+
{
187188
pub fn mark_realistic<S: Store>(
188189
&self,
189190
optimistic_id: &S::Id,
@@ -203,12 +204,16 @@ impl<T, RealisticId: Clone + Eq + std::hash::Hash> OptimisticChangeMap<T, Realis
203204
.mark_realistic(realistic_id.clone());
204205

205206
inner
206-
.realistic_to_optimistic
207+
.optimistic_to_realistic
207208
.insert(realistic_id, optimistic_id);
208209
}
209210

210-
pub fn get_realistic_to_optimistic(&self) -> HashMap<RealisticId, SerializedId> {
211-
self.inner.read().realistic_to_optimistic.clone()
211+
pub fn get_realistic_to_optimistic(&self, realistic_id: &RealisticId) -> Option<SerializedId> {
212+
self.inner
213+
.read()
214+
.optimistic_to_realistic
215+
.get(realistic_id)
216+
.cloned()
212217
}
213218
}
214219

shared/src/sync_engine/optimistic/optimistic_changes.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![allow(dead_code)]
22
#![allow(clippy::type_complexity)]
33

4-
use std::{any::Any, collections::HashMap, future::Future, rc::Rc};
4+
use std::{any::Any, future::Future, rc::Rc};
55

66
use any_spawner::Executor;
77
use typesafe_idb::Store;
@@ -87,15 +87,21 @@ impl OptimisticChanges {
8787
});
8888
}
8989

90+
/// This can be refactored (along with mark_realistic).
9091
pub fn remove_successful_for_id<S: Store>(&self, id: &S::Id) {
9192
tracing::info!("Called remove successful for id: {id:?}");
92-
self.deletes.remove_all_realistic::<S>(id, &());
93-
self.updates.remove_all_realistic::<S>(id, &());
93+
self.deletes.remove_all_realistic::<S>(&());
94+
self.updates.remove_all_realistic::<S>(&());
9495
self.creations
95-
.remove_all_realistic::<S>(id, &SerializedId::new_from_id::<S>(id));
96+
.remove_all_realistic::<S>(&SerializedId::new_from_id::<S>(id));
9697
}
9798

98-
pub fn get_realistic_to_optimistic_for_creations(&self) -> HashMap<SerializedId, SerializedId> {
99-
self.creations.get_realistic_to_optimistic()
99+
pub fn get_realistic_to_optimistic_for_creations<S: Store>(
100+
&self,
101+
realistic_id: &S::Id,
102+
) -> Option<S::Id> {
103+
self.creations
104+
.get_realistic_to_optimistic(&SerializedId::new_from_id::<S>(realistic_id))
105+
.map(|id| id.to_unserialized_id::<S>())
100106
}
101107
}

0 commit comments

Comments
 (0)