@@ -65,8 +65,20 @@ mod status {
6565 }
6666}
6767
68- type OptimisticChangeMapInner < T , RealisticId > =
69- HashMap < StoreName , HashMap < SerializedId , BTreeMap < MonotonicTime , Status < T , RealisticId > > > > ;
68+ struct OptimisticChangeMapInner < T , RealisticId > {
69+ changes :
70+ HashMap < StoreName , HashMap < SerializedId , BTreeMap < MonotonicTime , Status < T , RealisticId > > > > ,
71+ realistic_to_optimistic : HashMap < RealisticId , SerializedId > ,
72+ }
73+
74+ impl < T , RealisticId > Default for OptimisticChangeMapInner < T , RealisticId > {
75+ fn default ( ) -> Self {
76+ Self {
77+ changes : Default :: default ( ) ,
78+ realistic_to_optimistic : Default :: default ( ) ,
79+ }
80+ }
81+ }
7082
7183pub struct OptimisticChangeMap < T , RealisticId = ( ) > {
7284 inner : Arc < RwLock < OptimisticChangeMapInner < T , RealisticId > > > ,
@@ -94,6 +106,7 @@ impl<T, RealisticId: Eq + std::fmt::Debug> OptimisticChangeMap<T, RealisticId> {
94106 let time = MonotonicTime :: new ( ) ;
95107 self . inner
96108 . write ( )
109+ . changes
97110 . entry ( S :: NAME )
98111 . or_default ( )
99112 . entry ( optimistic_id)
@@ -107,7 +120,7 @@ impl<T, RealisticId: Eq + std::fmt::Debug> OptimisticChangeMap<T, RealisticId> {
107120 let optimistic_id = SerializedId :: new_from_id :: < S > ( optimistic_id) ;
108121 let mut by_id_len = None ;
109122 let mut inner = self . inner . write ( ) ;
110- if let Some ( by_id) = inner. get_mut ( & S :: NAME ) {
123+ if let Some ( by_id) = inner. changes . get_mut ( & S :: NAME ) {
111124 let mut by_time_len = None ;
112125 if let Some ( by_time) = by_id. get_mut ( & optimistic_id) {
113126 match by_time. entry ( * time) {
@@ -128,7 +141,7 @@ impl<T, RealisticId: Eq + std::fmt::Debug> OptimisticChangeMap<T, RealisticId> {
128141 by_id_len = Some ( by_id. len ( ) ) ;
129142 }
130143 if by_id_len == Some ( 0 ) {
131- inner. remove ( & S :: NAME ) ;
144+ inner. changes . remove ( & S :: NAME ) ;
132145 }
133146 }
134147
@@ -140,7 +153,7 @@ impl<T, RealisticId: Eq + std::fmt::Debug> OptimisticChangeMap<T, RealisticId> {
140153 let optimistic_id = SerializedId :: new_from_id :: < S > ( optimistic_id) ;
141154 let mut by_id_len = None ;
142155 let mut inner = self . inner . write ( ) ;
143- if let Some ( by_id) = inner. get_mut ( & S :: NAME ) {
156+ if let Some ( by_id) = inner. changes . get_mut ( & S :: NAME ) {
144157 let mut by_time_len = None ;
145158 if let Some ( by_time) = by_id. get_mut ( & optimistic_id) {
146159 let to_remove_keys = by_time
@@ -165,26 +178,37 @@ impl<T, RealisticId: Eq + std::fmt::Debug> OptimisticChangeMap<T, RealisticId> {
165178 by_id_len = Some ( by_id. len ( ) ) ;
166179 }
167180 if by_id_len == Some ( 0 ) {
168- inner. remove ( & S :: NAME ) ;
181+ inner. changes . remove ( & S :: NAME ) ;
169182 }
170183 }
184+ }
171185
186+ impl < T , RealisticId : Clone + Eq + std:: hash:: Hash > OptimisticChangeMap < T , RealisticId > {
172187 pub fn mark_realistic < S : Store > (
173188 & self ,
174189 optimistic_id : & S :: Id ,
175190 time : & MonotonicTime ,
176191 realistic_id : RealisticId ,
177192 ) {
178193 let optimistic_id = SerializedId :: new_from_id :: < S > ( optimistic_id) ;
179- self . inner
180- . write ( )
194+ let mut inner = self . inner . write ( ) ;
195+ inner
196+ . changes
181197 . get_mut ( & S :: NAME )
182198 . unwrap ( )
183199 . get_mut ( & optimistic_id)
184200 . expect ( "id to mark realistic not found" )
185201 . get_mut ( time)
186202 . expect ( "could not find the monotonic time to mark realistic" )
187- . mark_realistic ( realistic_id) ;
203+ . mark_realistic ( realistic_id. clone ( ) ) ;
204+
205+ inner
206+ . realistic_to_optimistic
207+ . insert ( realistic_id, optimistic_id) ;
208+ }
209+
210+ pub fn get_realistic_to_optimistic ( & self ) -> HashMap < RealisticId , SerializedId > {
211+ self . inner . read ( ) . realistic_to_optimistic . clone ( )
188212 }
189213}
190214
@@ -194,6 +218,7 @@ impl<T: Clone, RealisticId: Clone> OptimisticChangeMap<T, RealisticId> {
194218 Some (
195219 self . inner
196220 . read ( )
221+ . changes
197222 . get ( & S :: NAME ) ?
198223 . get ( & optimistic_id) ?
199224 . last_key_value ( ) ?
@@ -209,6 +234,7 @@ impl<RealisticId> OptimisticChangeMap<Rc<dyn Any>, RealisticId> {
209234 Some (
210235 self . inner
211236 . read ( )
237+ . changes
212238 . get ( & S :: NAME ) ?
213239 . get ( & optimistic_id) ?
214240 . last_key_value ( ) ?
@@ -223,6 +249,7 @@ impl<RealisticId> OptimisticChangeMap<Rc<dyn Any>, RealisticId> {
223249 pub fn all_the_latest_downcasted < S : Store + ' static > ( & self ) -> Vec < S > {
224250 self . inner
225251 . read ( )
252+ . changes
226253 . get ( & S :: NAME )
227254 . map ( |s| s. values ( ) )
228255 . into_iter ( )
0 commit comments