1
1
use std:: collections:: { btree_map:: Entry , BTreeMap , HashMap } ;
2
2
use std:: os:: raw:: c_char;
3
+ use std:: path:: Path ;
3
4
use std:: sync:: { Arc , RwLock } ;
4
- use std:: thread;
5
+ use std:: { fs , thread} ;
5
6
6
7
use ffi_support:: { rust_string_to_c, FfiStr } ;
7
8
use once_cell:: sync:: Lazy ;
8
9
9
10
use crate :: common:: error:: prelude:: * ;
10
11
use crate :: common:: handle:: ResourceHandle ;
11
12
use crate :: config:: PoolConfig ;
13
+ use crate :: pool:: cache:: storage:: { new_fs_ordered_store, OrderedHashMap } ;
14
+ use crate :: pool:: cache:: strategy:: CacheStrategyTTL ;
12
15
use crate :: pool:: cache:: Cache ;
13
16
use crate :: pool:: {
14
17
InMemoryCache , PoolBuilder , PoolRunner , PoolTransactions , PoolTransactionsCache , RequestMethod ,
@@ -31,6 +34,50 @@ pub struct PoolInstance {
31
34
pub node_weights : Option < NodeWeights > ,
32
35
}
33
36
37
+ #[ derive( Clone ) ]
38
+ pub struct LegerCacheConfig {
39
+ pub cache_size : usize ,
40
+ pub cache_ttl : u128 ,
41
+ pub path : Option < String > ,
42
+ }
43
+
44
+ impl LegerCacheConfig {
45
+ pub fn new ( cache_size : usize , cache_ttl : u128 , path : Option < String > ) -> Self {
46
+ Self {
47
+ cache_size,
48
+ cache_ttl,
49
+ path,
50
+ }
51
+ }
52
+ pub fn create_cache (
53
+ & self ,
54
+ id : Option < String > ,
55
+ ) -> VdrResult < Cache < String , ( String , RequestResultMeta ) > > {
56
+ if let Some ( path) = & self . path {
57
+ let full_path = Path :: new ( path) . join ( id. unwrap_or ( "default" . to_string ( ) ) ) ;
58
+ let full_path_string = full_path
59
+ . into_os_string ( )
60
+ . into_string ( )
61
+ . unwrap_or ( path. to_string ( ) ) ;
62
+ fs:: create_dir_all ( full_path_string. clone ( ) ) ?;
63
+ let store = OrderedHashMap :: new ( new_fs_ordered_store ( full_path_string) ?) ;
64
+ Ok ( Cache :: new ( CacheStrategyTTL :: new (
65
+ self . cache_size ,
66
+ self . cache_ttl ,
67
+ Some ( store) ,
68
+ None ,
69
+ ) ) )
70
+ } else {
71
+ Ok ( Cache :: new ( CacheStrategyTTL :: new (
72
+ self . cache_size ,
73
+ self . cache_ttl ,
74
+ None ,
75
+ None ,
76
+ ) ) )
77
+ }
78
+ }
79
+ }
80
+
34
81
pub type NodeWeights = HashMap < String , f32 > ;
35
82
36
83
pub static POOL_CONFIG : Lazy < RwLock < PoolConfig > > = Lazy :: new ( || RwLock :: new ( PoolConfig :: default ( ) ) ) ;
@@ -41,7 +88,7 @@ pub static POOLS: Lazy<RwLock<BTreeMap<PoolHandle, PoolInstance>>> =
41
88
pub static POOL_CACHE : Lazy < RwLock < Option < Arc < dyn PoolTransactionsCache > > > > =
42
89
Lazy :: new ( || RwLock :: new ( Some ( Arc :: new ( InMemoryCache :: new ( ) ) ) ) ) ;
43
90
44
- pub static LEDGER_TXN_CACHE : Lazy < RwLock < Option < Cache < String , ( String , RequestResultMeta ) > > > > =
91
+ pub static LEDGER_TXN_CACHE_CONFIG : Lazy < RwLock < Option < LegerCacheConfig > > > =
45
92
Lazy :: new ( || RwLock :: new ( None ) ) ;
46
93
47
94
#[ derive( Serialize , Deserialize , Debug , Clone ) ]
@@ -72,6 +119,12 @@ pub extern "C" fn indy_vdr_pool_create(params: FfiStr, handle_p: *mut PoolHandle
72
119
"Invalid pool create parameters: must provide transactions or transactions_path"
73
120
) ) ;
74
121
} ;
122
+ let txn_cache_config = read_lock!( LEDGER_TXN_CACHE_CONFIG ) ?. clone( ) ;
123
+ let txn_cache = if let Some ( config) = txn_cache_config {
124
+ config. create_cache( txns. root_hash_base58( ) . ok( ) ) . ok( )
125
+ } else {
126
+ None
127
+ } ;
75
128
let mut cached = false ;
76
129
if let Some ( cache) = read_lock!( POOL_CACHE ) ?. as_ref( ) {
77
130
if let Some ( newer_txns) = cache. resolve_latest( & txns) ? {
@@ -80,7 +133,6 @@ pub extern "C" fn indy_vdr_pool_create(params: FfiStr, handle_p: *mut PoolHandle
80
133
}
81
134
}
82
135
let config = read_lock!( POOL_CONFIG ) ?. clone( ) ;
83
- let txn_cache = read_lock!( LEDGER_TXN_CACHE ) ?. clone( ) ;
84
136
let runner = PoolBuilder :: new( config, txns. clone( ) ) . node_weights( params. node_weights. clone( ) ) . refreshed( cached) . into_runner( txn_cache) ?;
85
137
let handle = PoolHandle :: next( ) ;
86
138
let mut pools = write_lock!( POOLS ) ?;
@@ -107,7 +159,12 @@ fn handle_pool_refresh(
107
159
cache. update( & init_txns, latest_txns) ?;
108
160
}
109
161
if let Some ( new_txns) = new_txns {
110
- let txn_cache = read_lock!( LEDGER_TXN_CACHE ) ?. clone( ) ;
162
+ let txn_cache_config = read_lock!( LEDGER_TXN_CACHE_CONFIG ) ?. clone( ) ;
163
+ let txn_cache = if let Some ( config) = txn_cache_config {
164
+ config. create_cache( init_txns. root_hash_base58( ) . ok( ) ) . ok( )
165
+ } else {
166
+ None
167
+ } ;
111
168
let runner = PoolBuilder :: new( config, new_txns) . node_weights( node_weights) . refreshed( true ) . into_runner( txn_cache) ?;
112
169
let mut pools = write_lock!( POOLS ) ?;
113
170
if let Entry :: Occupied ( mut entry) = pools. entry( pool_handle) {
0 commit comments