@@ -168,10 +168,10 @@ impl AccountsDbIndex {
168168 pubkey : & Pubkey ,
169169 owner : & Pubkey ,
170170 allocation : Allocation ,
171+ txn : & mut RwTransaction ,
171172 ) -> AccountsDbResult < Option < ExistingAllocation > > {
172173 let Allocation { offset, blocks, .. } = allocation;
173174
174- let mut txn = self . env . begin_rw_txn ( ) ?;
175175 let mut dealloc = None ;
176176
177177 // merge offset and block count into one single u64 and cast it to [u8; 8]
@@ -181,23 +181,21 @@ impl AccountsDbIndex {
181181
182182 // optimisitically try to insert account to index, assuming that it doesn't exist
183183 let inserted =
184- self . accounts
185- . put_if_not_exists ( & mut txn, pubkey, index_value) ?;
184+ self . accounts . put_if_not_exists ( txn, pubkey, index_value) ?;
186185 // if the account does exist, then it already occupies space in main storage
187186 if !inserted {
188187 // in which case we just move the account to a new allocation
189188 // adjusting all of the offsets and cleaning up the older ones
190189 let previous =
191- self . reallocate_account ( pubkey, & mut txn, & index_value) ?;
190+ self . reallocate_account ( pubkey, txn, & index_value) ?;
192191 dealloc. replace ( previous) ;
193192 } ;
194193
195194 // track the account via programs' index as well
196- self . programs . put ( & mut txn, owner, offset_and_pubkey) ?;
195+ self . programs . put ( txn, owner, offset_and_pubkey) ?;
197196 // track the reverse relation between account and its owner
198- self . owners . put ( & mut txn, pubkey, owner) ?;
197+ self . owners . put ( txn, pubkey, owner) ?;
199198
200- txn. commit ( ) ?;
201199 Ok ( dealloc)
202200 }
203201
@@ -267,9 +265,9 @@ impl AccountsDbIndex {
267265 & self ,
268266 pubkey : & Pubkey ,
269267 owner : & Pubkey ,
268+ txn : & mut RwTransaction ,
270269 ) -> AccountsDbResult < ( ) > {
271- let txn = self . env . begin_ro_txn ( ) ?;
272- let old_owner = match self . owners . get ( & txn, pubkey) ? {
270+ let old_owner = match self . owners . get ( txn, pubkey) ? {
273271 // if current owner matches with that stored in index, then we are all set
274272 Some ( val) if owner. as_ref ( ) == val => {
275273 return Ok ( ( ) ) ;
@@ -278,23 +276,21 @@ impl AccountsDbIndex {
278276 // if they don't match, then we have to remove old entries and create new ones
279277 Some ( val) => Pubkey :: try_from ( val) . ok ( ) ,
280278 } ;
281- let allocation = self . get_allocation ( & txn, pubkey) ?;
282- let mut txn = self . env . begin_rw_txn ( ) ?;
279+ let allocation = self . get_allocation ( txn, pubkey) ?;
283280 // cleanup `programs` and `owners` index
284281 self . remove_programs_index_entry (
285282 pubkey,
286283 old_owner,
287- & mut txn,
284+ txn,
288285 allocation. offset ,
289286 ) ?;
290287 // track new owner of the account via programs' index
291288 let offset_and_pubkey =
292289 bytes ! ( #pack, allocation. offset, Offset , * pubkey, Pubkey ) ;
293- self . programs . put ( & mut txn, owner, offset_and_pubkey) ?;
290+ self . programs . put ( txn, owner, offset_and_pubkey) ?;
294291 // track the reverse relation between account and its owner
295- self . owners . put ( & mut txn, pubkey, owner) ?;
296-
297- txn. commit ( ) . map_err ( Into :: into)
292+ self . owners . put ( txn, pubkey, owner) ?;
293+ Ok ( ( ) )
298294 }
299295
300296 fn remove_programs_index_entry (
@@ -379,9 +375,9 @@ impl AccountsDbIndex {
379375 pub ( crate ) fn try_recycle_allocation (
380376 & self ,
381377 space : Blocks ,
378+ txn : & mut RwTransaction ,
382379 ) -> AccountsDbResult < ExistingAllocation > {
383- let mut txn = self . env . begin_rw_txn ( ) ?;
384- let mut cursor = self . deallocations . cursor_rw ( & mut txn) ?;
380+ let mut cursor = self . deallocations . cursor_rw ( txn) ?;
385381 // this is a neat lmdb trick where we can search for entry with matching
386382 // or greater key since we are interested in any allocation of at least
387383 // `blocks` size or greater, this works perfectly well for this case
@@ -402,9 +398,6 @@ impl AccountsDbIndex {
402398 cursor. put ( & remainder. to_le_bytes ( ) , & index_value, WEMPTY ) ?;
403399 }
404400
405- drop ( cursor) ;
406- txn. commit ( ) ?;
407-
408401 Ok ( ExistingAllocation { offset, blocks } )
409402 }
410403
@@ -438,6 +431,11 @@ impl AccountsDbIndex {
438431 } ;
439432 self . deallocations . entries ( & txn)
440433 }
434+
435+ /// Initiate RW Transaction
436+ pub ( crate ) fn rwtxn ( & self ) -> lmdb:: Result < RwTransaction < ' _ > > {
437+ self . env . begin_rw_txn ( )
438+ }
441439}
442440
443441pub ( crate ) mod iterator;
0 commit comments