@@ -13,60 +13,63 @@ use super::{db::SerializedId, monotonic_time::MonotonicTime};
1313
1414mod 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
7275impl < 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