@@ -154,6 +154,34 @@ pub trait CachingDb {
154154
155155 /// Deconstruct into the cache
156156 fn into_cache ( self ) -> Cache ;
157+
158+ /// Extend the cache with the given cache by copying data.
159+ ///
160+ /// The behavior is as follows:
161+ /// - Accounts are overridden with outer accounts
162+ /// - Contracts are overridden with outer contracts
163+ /// - Logs are appended
164+ /// - Block hashes are overridden with outer block hashes
165+ fn extend_ref ( & mut self , cache : & Cache ) {
166+ self . cache_mut ( ) . accounts . extend ( cache. accounts . iter ( ) . map ( |( k, v) | ( * k, v. clone ( ) ) ) ) ;
167+ self . cache_mut ( ) . contracts . extend ( cache. contracts . iter ( ) . map ( |( k, v) | ( * k, v. clone ( ) ) ) ) ;
168+ self . cache_mut ( ) . logs . extend ( cache. logs . iter ( ) . cloned ( ) ) ;
169+ self . cache_mut ( ) . block_hashes . extend ( cache. block_hashes . iter ( ) . map ( |( k, v) | ( * k, * v) ) ) ;
170+ }
171+
172+ /// Extend the cache with the given cache by moving data.
173+ ///
174+ /// The behavior is as follows:
175+ /// - Accounts are overridden with outer accounts
176+ /// - Contracts are overridden with outer contracts
177+ /// - Logs are appended
178+ /// - Block hashes are overridden with outer block hashes
179+ fn extend ( & mut self , cache : Cache ) {
180+ self . cache_mut ( ) . accounts . extend ( cache. accounts ) ;
181+ self . cache_mut ( ) . contracts . extend ( cache. contracts ) ;
182+ self . cache_mut ( ) . logs . extend ( cache. logs ) ;
183+ self . cache_mut ( ) . block_hashes . extend ( cache. block_hashes ) ;
184+ }
157185}
158186
159187impl < Db > CachingDb for CacheDB < Db > {
@@ -184,6 +212,44 @@ pub trait TryCachingDb {
184212
185213 /// Attempt to deconstruct into the cache
186214 fn try_into_cache ( self ) -> Result < Cache , Self :: Error > ;
215+
216+ /// Attempt to fold a cache into the database.
217+ ///
218+ /// The behavior is as follows:
219+ /// - Accounts are overridden with outer accounts
220+ /// - Contracts are overridden with outer contracts
221+ /// - Logs are appended
222+ /// - Block hashes are overridden with outer block hashes
223+ fn try_extend_ref ( & mut self , cache : & Cache ) -> Result < ( ) , Self :: Error >
224+ where
225+ Self : Sized ,
226+ {
227+ let inner_cache = self . try_cache_mut ( ) ?;
228+ inner_cache. accounts . extend ( cache. accounts . iter ( ) . map ( |( k, v) | ( * k, v. clone ( ) ) ) ) ;
229+ inner_cache. contracts . extend ( cache. contracts . iter ( ) . map ( |( k, v) | ( * k, v. clone ( ) ) ) ) ;
230+ inner_cache. logs . extend ( cache. logs . iter ( ) . cloned ( ) ) ;
231+ inner_cache. block_hashes . extend ( cache. block_hashes . iter ( ) . map ( |( k, v) | ( * k, * v) ) ) ;
232+ Ok ( ( ) )
233+ }
234+
235+ /// Attempt to extend the cache with the given cache by moving data.
236+ ///
237+ /// The behavior is as follows:
238+ /// - Accounts are overridden with outer accounts
239+ /// - Contracts are overridden with outer contracts
240+ /// - Logs are appended
241+ /// - Block hashes are overridden with outer block hashes
242+ fn try_extend ( & mut self , cache : Cache ) -> Result < ( ) , Self :: Error >
243+ where
244+ Self : Sized ,
245+ {
246+ let inner_cache = self . try_cache_mut ( ) ?;
247+ inner_cache. accounts . extend ( cache. accounts ) ;
248+ inner_cache. contracts . extend ( cache. contracts ) ;
249+ inner_cache. logs . extend ( cache. logs ) ;
250+ inner_cache. block_hashes . extend ( cache. block_hashes ) ;
251+ Ok ( ( ) )
252+ }
187253}
188254
189255impl < Db > TryCachingDb for Arc < Db >
0 commit comments