Skip to content

Commit 7bfde98

Browse files
committed
clarify successful->realistic
1 parent 123ed3a commit 7bfde98

File tree

5 files changed

+107
-82
lines changed

5 files changed

+107
-82
lines changed

shared/src/sync_engine/new_defn.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,28 @@ impl<BackendApi: BackendApiTrait, Transport: TransportTrait, GithubApi>
7676
db,
7777
Rc::new(move |reactivity_trackers| {
7878
let orig_trackers = db_subscriptions2.get();
79-
orig_trackers
79+
let matching_trackers = orig_trackers
8080
.iter()
8181
.filter(|sub| {
82+
tracing::trace!(
83+
"checking if {:?} is affected by {:?}",
84+
sub.original_reactivity_trackers,
85+
reactivity_trackers
86+
);
8287
sub.original_reactivity_trackers
8388
.is_affected_by_writes_in(reactivity_trackers)
8489
})
85-
.for_each(|sub| (sub.func)());
90+
.collect::<Vec<_>>();
91+
92+
tracing::trace!(
93+
"matching_trackers: {:?}",
94+
matching_trackers
95+
.iter()
96+
.map(|sub| sub.original_reactivity_trackers.clone())
97+
.collect::<Vec<_>>()
98+
);
99+
100+
matching_trackers.into_iter().for_each(|sub| (sub.func)());
86101
}),
87102
)
88103
.await?;

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,13 +154,17 @@ where
154154
.delete(id)
155155
.await
156156
.map_err(|e| Error::new(e, self.location))?;
157-
self.optimistic_changes.remove_obsoletes_for_id::<S>(id);
157+
self.optimistic_changes.remove_successful_for_id::<S>(id);
158158

159159
if let Some(commit_listener) = self.commit_listener.as_ref() {
160160
let reactivity_trackers = ReactivityTrackers {
161161
stores_modified: hashmap![S::NAME => hashset![SerializedId::new_from_id::<S>(id)]],
162162
..Default::default()
163163
};
164+
tracing::trace!(
165+
"In ObjectStoreWithOptimisticChanges::delete calling commit listener with {:?}",
166+
reactivity_trackers
167+
);
164168
commit_listener(&reactivity_trackers);
165169
}
166170

@@ -173,13 +177,17 @@ where
173177
.await
174178
.map_err(|e| Error::new(e, self.location))?;
175179
self.optimistic_changes
176-
.remove_obsoletes_for_id::<S>(item.id());
180+
.remove_successful_for_id::<S>(item.id());
177181

178182
if let Some(commit_listener) = self.commit_listener.as_ref() {
179183
let reactivity_trackers = ReactivityTrackers {
180184
stores_modified: hashmap![S::NAME => hashset![SerializedId::new_from_row(item)]],
181185
..Default::default()
182186
};
187+
tracing::trace!(
188+
"In ObjectStoreWithOptimisticChanges::put calling commit listener with {:?}",
189+
reactivity_trackers
190+
);
183191
commit_listener(&reactivity_trackers);
184192
}
185193

@@ -193,6 +201,10 @@ where
193201
self.optimistic_changes.update(row, update_fut);
194202

195203
if let Some(commit_listener) = self.commit_listener.as_ref() {
204+
tracing::trace!(
205+
"In ObjectStoreWithOptimisticChanges::update calling commit listener with {:?}",
206+
reactivity_trackers
207+
);
196208
commit_listener(&reactivity_trackers);
197209
}
198210
}
@@ -205,6 +217,10 @@ where
205217
self.optimistic_changes.create(row, create_fut);
206218

207219
if let Some(commit_listener) = self.commit_listener.as_ref() {
220+
tracing::trace!(
221+
"In ObjectStoreWithOptimisticChanges::create calling commit listener with {:?}",
222+
reactivity_trackers
223+
);
208224
commit_listener(&reactivity_trackers);
209225
}
210226
}

shared/src/sync_engine/optimistic/optimistic_change_map.rs

Lines changed: 62 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -13,60 +13,63 @@ use super::{db::SerializedId, monotonic_time::MonotonicTime};
1313

1414
mod status {
1515
/// The value inside is never `None`. But having an option is the only way I know of enabling
16-
/// the `mark_successful()` function.
16+
/// the `mark_realistic()` function.
1717
#[derive(Clone)]
18-
pub enum Status<T, SuccessMarker> {
19-
Pending(Option<T>),
20-
Successful { t: T, marker: SuccessMarker },
18+
pub enum Status<T, RealisticId> {
19+
Optimistic(Option<T>),
20+
Realistic { t: T, realistic_id: RealisticId },
2121
}
2222

23-
impl<T, SuccessMarker> Status<T, SuccessMarker> {
24-
pub fn new(t: T) -> Status<T, SuccessMarker> {
25-
Self::Pending(Some(t))
23+
impl<T, RealisticId> Status<T, RealisticId> {
24+
pub fn new(t: T) -> Status<T, RealisticId> {
25+
Self::Optimistic(Some(t))
2626
}
27-
pub fn mark_successful(&mut self, marker: SuccessMarker) {
28-
if let Status::Pending(t) = self {
27+
pub fn mark_realistic(&mut self, marker: RealisticId) {
28+
if let Status::Optimistic(t) = self {
2929
let t = std::mem::take(t).unwrap();
30-
*self = Status::Successful { marker, t }
30+
*self = Status::Realistic {
31+
realistic_id: marker,
32+
t,
33+
}
3134
} else {
32-
panic!("Tried to mark_successful something that was already marked as such!")
35+
panic!("Tried to mark_realistic something that was already marked as such!")
3336
}
3437
}
3538

36-
pub fn as_successful(&self) -> Option<&T> {
39+
pub fn as_realistic(&self) -> Option<&T> {
3740
match self {
38-
Status::Successful { t, .. } => Some(t),
41+
Status::Realistic { t, .. } => Some(t),
3942
_ => None,
4043
}
4144
}
4245

4346
pub fn read(&self) -> &T {
4447
match self {
45-
Status::Pending(p) => p.as_ref().unwrap(),
46-
Status::Successful { t, .. } => t,
48+
Status::Optimistic(p) => p.as_ref().unwrap(),
49+
Status::Realistic { t, .. } => t,
4750
}
4851
}
4952

5053
pub fn get(self) -> T {
5154
match self {
52-
Status::Pending(p) => p.unwrap(),
53-
Status::Successful { t, .. } => t,
55+
Status::Optimistic(p) => p.unwrap(),
56+
Status::Realistic { t, .. } => t,
5457
}
5558
}
5659
}
5760

5861
impl<T> From<T> for Status<T, ()> {
5962
fn from(value: T) -> Self {
60-
Self::Pending(Some(value))
63+
Self::Optimistic(Some(value))
6164
}
6265
}
6366
}
6467

65-
type OptimisticChangeMapInner<T, SuccessMarker> =
66-
HashMap<StoreName, HashMap<SerializedId, BTreeMap<MonotonicTime, Status<T, SuccessMarker>>>>;
68+
type OptimisticChangeMapInner<T, RealisticId> =
69+
HashMap<StoreName, HashMap<SerializedId, BTreeMap<MonotonicTime, Status<T, RealisticId>>>>;
6770

68-
pub struct OptimisticChangeMap<T, SuccessMarker = ()> {
69-
inner: Arc<RwLock<OptimisticChangeMapInner<T, SuccessMarker>>>,
71+
pub struct OptimisticChangeMap<T, RealisticId = ()> {
72+
inner: Arc<RwLock<OptimisticChangeMapInner<T, RealisticId>>>,
7073
}
7174

7275
impl<T, S> Clone for OptimisticChangeMap<T, S> {
@@ -85,31 +88,31 @@ impl<T, S> Default for OptimisticChangeMap<T, S> {
8588
}
8689
}
8790

88-
impl<T, SuccessMarker: Eq + std::fmt::Debug> OptimisticChangeMap<T, SuccessMarker> {
89-
pub fn insert<S: Store>(&self, id: &S::Id, v: T) -> MonotonicTime {
90-
let id = SerializedId::new_from_id::<S>(id);
91+
impl<T, RealisticId: Eq + std::fmt::Debug> OptimisticChangeMap<T, RealisticId> {
92+
pub fn insert<S: Store>(&self, optimistic_id: &S::Id, v: T) -> MonotonicTime {
93+
let optimistic_id = SerializedId::new_from_id::<S>(optimistic_id);
9194
let time = MonotonicTime::new();
9295
self.inner
9396
.write()
9497
.entry(S::NAME)
9598
.or_default()
96-
.entry(id)
99+
.entry(optimistic_id)
97100
.or_default()
98101
.insert(time, Status::new(v));
99102
time
100103
}
101104

102105
/// Will panic if the thing is not pending, or if it doesn't exist.
103-
pub fn remove_pending<S: Store>(&self, id: &S::Id, time: &MonotonicTime) {
104-
let id = SerializedId::new_from_id::<S>(id);
106+
pub fn remove_pending<S: Store>(&self, optimistic_id: &S::Id, time: &MonotonicTime) {
107+
let optimistic_id = SerializedId::new_from_id::<S>(optimistic_id);
105108
let mut by_id_len = None;
106109
let mut inner = self.inner.write();
107110
if let Some(by_id) = inner.get_mut(&S::NAME) {
108111
let mut by_time_len = None;
109-
if let Some(by_time) = by_id.get_mut(&id) {
112+
if let Some(by_time) = by_id.get_mut(&optimistic_id) {
110113
match by_time.entry(*time) {
111114
std::collections::btree_map::Entry::Occupied(occupied_entry) => {
112-
if let Status::Pending(_) = occupied_entry.get() {
115+
if let Status::Optimistic(_) = occupied_entry.get() {
113116
occupied_entry.remove();
114117
} else {
115118
panic!("Is not pending.");
@@ -120,7 +123,7 @@ impl<T, SuccessMarker: Eq + std::fmt::Debug> OptimisticChangeMap<T, SuccessMarke
120123
by_time_len = Some(by_time.len());
121124
}
122125
if by_time_len == Some(0) {
123-
by_id.remove(&id);
126+
by_id.remove(&optimistic_id);
124127
}
125128
by_id_len = Some(by_id.len());
126129
}
@@ -129,17 +132,24 @@ impl<T, SuccessMarker: Eq + std::fmt::Debug> OptimisticChangeMap<T, SuccessMarke
129132
}
130133
}
131134

132-
pub fn remove_all_successful<S: Store>(&self, id: &S::Id, success_marker: &SuccessMarker) {
133-
let id = SerializedId::new_from_id::<S>(id);
135+
pub fn remove_all_realistic<S: Store>(
136+
&self,
137+
optimistic_id: &S::Id,
138+
realistic_id: &RealisticId,
139+
) {
140+
let optimistic_id = SerializedId::new_from_id::<S>(optimistic_id);
134141
let mut by_id_len = None;
135142
let mut inner = self.inner.write();
136143
if let Some(by_id) = inner.get_mut(&S::NAME) {
137144
let mut by_time_len = None;
138-
if let Some(by_time) = by_id.get_mut(&id) {
145+
if let Some(by_time) = by_id.get_mut(&optimistic_id) {
139146
let to_remove_keys = by_time
140147
.iter()
141148
.filter_map(|(time, status)| match status {
142-
Status::Successful { marker, .. } if marker == success_marker => Some(time),
149+
Status::Realistic {
150+
realistic_id: realistic_id_candidate,
151+
..
152+
} if realistic_id_candidate == realistic_id => Some(time),
143153
_ => None,
144154
})
145155
.cloned()
@@ -150,7 +160,7 @@ impl<T, SuccessMarker: Eq + std::fmt::Debug> OptimisticChangeMap<T, SuccessMarke
150160
by_time_len = Some(by_time.len());
151161
}
152162
if by_time_len == Some(0) {
153-
by_id.remove(&id);
163+
by_id.remove(&optimistic_id);
154164
}
155165
by_id_len = Some(by_id.len());
156166
}
@@ -159,49 +169,48 @@ impl<T, SuccessMarker: Eq + std::fmt::Debug> OptimisticChangeMap<T, SuccessMarke
159169
}
160170
}
161171

162-
pub fn mark_successful<S: Store>(
172+
pub fn mark_realistic<S: Store>(
163173
&self,
164-
id: &S::Id,
174+
optimistic_id: &S::Id,
165175
time: &MonotonicTime,
166-
marker: SuccessMarker,
176+
realistic_id: RealisticId,
167177
) {
168-
tracing::info!("Marking succesful id={id:?}, and marker={marker:?}");
169-
let id = SerializedId::new_from_id::<S>(id);
178+
let optimistic_id = SerializedId::new_from_id::<S>(optimistic_id);
170179
self.inner
171180
.write()
172181
.get_mut(&S::NAME)
173182
.unwrap()
174-
.get_mut(&id)
175-
.expect("id to mark successful not found")
183+
.get_mut(&optimistic_id)
184+
.expect("id to mark realistic not found")
176185
.get_mut(time)
177-
.expect("could not find the monotonic time to mark succesful")
178-
.mark_successful(marker);
186+
.expect("could not find the monotonic time to mark realistic")
187+
.mark_realistic(realistic_id);
179188
}
180189
}
181190

182-
impl<T: Clone, SuccessMarker: Clone> OptimisticChangeMap<T, SuccessMarker> {
183-
pub fn latest<S: Store>(&self, id: &S::Id) -> Option<Status<T, SuccessMarker>> {
184-
let id = SerializedId::new_from_id::<S>(id);
191+
impl<T: Clone, RealisticId: Clone> OptimisticChangeMap<T, RealisticId> {
192+
pub fn latest<S: Store>(&self, optimistic_id: &S::Id) -> Option<Status<T, RealisticId>> {
193+
let optimistic_id = SerializedId::new_from_id::<S>(optimistic_id);
185194
Some(
186195
self.inner
187196
.read()
188197
.get(&S::NAME)?
189-
.get(&id)?
198+
.get(&optimistic_id)?
190199
.last_key_value()?
191200
.1
192201
.clone(),
193202
)
194203
}
195204
}
196205

197-
impl<SuccessMarker> OptimisticChangeMap<Rc<dyn Any>, SuccessMarker> {
198-
pub fn latest_downcasted<S: Store + 'static>(&self, id: &S::Id) -> Option<S> {
199-
let id = SerializedId::new_from_id::<S>(id);
206+
impl<RealisticId> OptimisticChangeMap<Rc<dyn Any>, RealisticId> {
207+
pub fn latest_downcasted<S: Store + 'static>(&self, optimistic_id: &S::Id) -> Option<S> {
208+
let optimistic_id = SerializedId::new_from_id::<S>(optimistic_id);
200209
Some(
201210
self.inner
202211
.read()
203212
.get(&S::NAME)?
204-
.get(&id)?
213+
.get(&optimistic_id)?
205214
.last_key_value()?
206215
.1
207216
.read()

0 commit comments

Comments
 (0)