@@ -20,6 +20,7 @@ use std::collections::HashSet;
2020use batch:: Batch ;
2121use journal:: block_manager:: BlockManager ;
2222use journal:: commit_store:: CommitStore ;
23+ use std:: sync:: { Arc , RwLock } ;
2324use transaction:: Transaction ;
2425
2526#[ derive( Debug , PartialEq ) ]
@@ -124,21 +125,26 @@ pub fn validate_transaction_dependencies(
124125 Ok ( ( ) )
125126}
126127
128+ type TransactionCommitCacheLRU = uluru:: LRUCache < String , 300 > ;
129+
127130pub struct TransactionCommitCache {
128- committed : HashSet < String > ,
131+ committed : Arc < RwLock < TransactionCommitCacheLRU > > ,
129132 commit_store : CommitStore ,
130133}
131134
132135impl TransactionCommitCache {
133136 pub fn new ( commit_store : CommitStore ) -> Self {
134137 TransactionCommitCache {
135- committed : HashSet :: new ( ) ,
138+ committed : Arc :: new ( RwLock :: new ( TransactionCommitCacheLRU :: default ( ) ) ) ,
136139 commit_store,
137140 }
138141 }
139142
140143 pub fn add ( & mut self , transaction_id : String ) {
141- self . committed . insert ( transaction_id) ;
144+ self . committed
145+ . write ( )
146+ . expect ( "Failed to acquire write lock on commit cache" )
147+ . insert ( transaction_id) ;
142148 }
143149
144150 pub fn add_batch ( & mut self , batch : & Batch ) {
@@ -148,8 +154,8 @@ impl TransactionCommitCache {
148154 . for_each ( |txn| self . add ( txn. header_signature . clone ( ) ) ) ;
149155 }
150156
151- pub fn remove ( & mut self , transaction_id : & str ) {
152- self . committed . remove ( transaction_id ) ;
157+ fn remove ( & mut self , transaction_id : & str ) {
158+ ( )
153159 }
154160
155161 pub fn remove_batch ( & mut self , batch : & Batch ) {
@@ -161,7 +167,11 @@ impl TransactionCommitCache {
161167
162168 pub fn contains ( & self , transaction_id : & str ) -> bool {
163169 // Shouldn't expect here
164- self . committed . contains ( transaction_id)
170+ self . committed
171+ . write ( )
172+ . expect ( "Failed to acquire write lock on commit cache" )
173+ . find ( |x| x == transaction_id)
174+ . is_some ( )
165175 || self
166176 . commit_store
167177 . contains_transaction ( transaction_id)
@@ -281,7 +291,7 @@ mod test {
281291 let block_manager = setup_state ( ) ;
282292
283293 let transactions: Vec < Transaction > = [ "B6b0t0" , "B6b0t1" , "B6b0t2" ]
284- . into_iter ( )
294+ . iter ( )
285295 . map ( |t_id| create_transaction ( ( * t_id) . into ( ) , vec ! [ "B2b0t0" . into( ) ] ) )
286296 . collect ( ) ;
287297
@@ -300,7 +310,7 @@ mod test {
300310 let block_manager = setup_state ( ) ;
301311
302312 let transactions: Vec < Transaction > = [ "B6b0t0" , "B6b0t1" , "B6b0t2" ]
303- . into_iter ( )
313+ . iter ( )
304314 . map ( |t_id| create_transaction ( ( * t_id) . into ( ) , vec ! [ "B1b0t0" . into( ) ] ) )
305315 . collect ( ) ;
306316
@@ -319,7 +329,7 @@ mod test {
319329 let block_manager = setup_state ( ) ;
320330
321331 let transactions: Vec < Transaction > = [ "B3-1b0t0" , "B3-1b0t1" , "B3-1b0t2" ]
322- . into_iter ( )
332+ . iter ( )
323333 . map ( |t_id| create_transaction ( ( * t_id) . into ( ) , vec ! [ "B1b0t0" . into( ) ] ) )
324334 . collect ( ) ;
325335
@@ -338,7 +348,7 @@ mod test {
338348 let block_manager = setup_state ( ) ;
339349
340350 let transactions: Vec < Transaction > = [ "B6b0t0" , "B6b0t1" , "B6b0t2" ]
341- . into_iter ( )
351+ . iter ( )
342352 . map ( |t_id| create_transaction ( ( * t_id) . into ( ) , vec ! [ "B4-3b0t0" . into( ) ] ) )
343353 . collect ( ) ;
344354
0 commit comments