bitcoinleveldb_cache/cache.rs
1/*!
2 | A Cache is an interface that maps keys to
3 | values. It has internal synchronization and
4 | may be safely accessed concurrently from
5 | multiple threads. It may automatically evict
6 | entries to make room for new entries. Values
7 | have a specified charge against the cache
8 | capacity. For example, a cache where the
9 | values are variable length strings, may use the
10 | length of the string as the charge for the
11 | string.
12 |
13 | A builtin cache implementation with
14 | a least-recently-used eviction policy is
15 | provided. Clients may use their own
16 | implementations if they want something more
17 | sophisticated (like scan-resistance, a custom
18 | eviction policy, variable cache sizing, etc.)
19 */
20
21crate::ix!();
22
23pub trait CacheInterface:
24CacheInsert
25+ CacheLookup
26+ CacheRelease
27+ CacheValue
28+ CacheErase
29+ CacheNewId
30+ CachePrune
31+ CacheTotalCharge {}
32
33pub trait CacheInsert {
34
35 /**
36 | Insert a mapping from key->value into the
37 | cache and assign it the specified charge
38 | against the total cache capacity.
39 |
40 | Returns a handle that corresponds to the
41 | mapping. The caller must call
42 | this->Release(handle) when the returned
43 | mapping is no longer needed.
44 |
45 | When the inserted entry is no longer needed,
46 | the key and value will be passed to
47 | "deleter".
48 */
49 fn insert(&mut self,
50 key_: &Slice,
51 value: *mut c_void,
52 charge: usize,
53 deleter: fn(key_: &Slice, value: *mut c_void) -> c_void) -> *mut CacheHandle;
54}
55
56pub trait CacheLookup {
57
58 /**
59 | If the cache has no mapping for "key",
60 | returns nullptr.
61 |
62 | Else return a handle that corresponds to the
63 | mapping. The caller must call
64 | this->Release(handle) when the returned
65 | mapping is no longer needed.
66 */
67 fn lookup(&mut self, key_: &Slice) -> *mut CacheHandle;
68}
69
70pub trait CacheRelease {
71
72 /**
73 | Release a mapping returned by a previous
74 | Lookup().
75 |
76 | REQUIRES: handle must not have been
77 | released yet.
78 |
79 | REQUIRES: handle must have been returned
80 | by a method on *this.
81 |
82 */
83 fn release(&mut self, handle: *mut CacheHandle);
84}
85
86pub trait CacheValue {
87
88 /**
89 | Return the value encapsulated in a handle
90 | returned by a successful Lookup().
91 |
92 | REQUIRES: handle must not have been released
93 | yet.
94 |
95 | REQUIRES: handle must have been returned by
96 | a method on *this.
97 */
98 fn value(&mut self, handle: *mut CacheHandle);
99}
100
101pub trait CacheErase {
102
103 /**
104 | If the cache contains entry for key, erase
105 | it. Note that the underlying entry will be
106 | kept around until all existing handles to it
107 | have been released.
108 */
109 fn erase(&mut self, key_: &Slice);
110}
111
112pub trait CacheNewId {
113
114 /**
115 | Return a new numeric id. May be used by
116 | multiple clients who are sharing the same
117 | cache to partition the key space. Typically
118 | the client will allocate a new id at startup
119 | and prepend the id to its cache keys.
120 */
121 fn new_id(&mut self) -> u64;
122}
123
124pub trait CachePrune {
125
126 /**
127 | Remove all cache entries that are not
128 | actively in use. Memory-constrained
129 | applications may wish to call this method to
130 | reduce memory usage.
131 |
132 | Default implementation of Prune() does
133 | nothing. Subclasses are strongly encouraged
134 | to override the default implementation.
135 | A future release of leveldb may change
136 | Prune() to a pure abstract method.
137 */
138 fn prune(&mut self);
139}
140
141pub trait CacheTotalCharge {
142
143 /**
144 | Return an estimate of the combined charges
145 | of all elements stored in the cache.
146 |
147 */
148 fn total_charge(&self) -> usize;
149}
150
151//-------------------------------------------[.cpp/bitcoin/src/leveldb/include/leveldb/cache.h]
152
153/**
154 | Create a new cache with a fixed size capacity.
155 |
156 | This implementation of Cache uses a
157 | least-recently-used eviction policy.
158 |
159 */
160pub fn new_lru_cache(capacity: usize) -> *mut Cache {
161
162 todo!();
163 /*
164
165 */
166}
167
168/**
169 | Opaque handle to an entry stored in the
170 | cache.
171 |
172 */
173pub struct CacheHandle {}
174
175#[derive(Default)]
176pub struct CacheRep {/*TODO: where is this struct in the c++?*/}
177
178#[derive(Default)]
179pub struct Cache {
180 rep: Rc<RefCell<CacheRep>>,
181}
182
183impl Drop for Cache {
184
185 /**
186 | Destroys all existing entries by calling
187 | the "deleter" function that was passed
188 | to the constructor.
189 |
190 */
191 fn drop(&mut self) {
192 todo!();
193 /*
194
195 */
196 }
197}
198
199impl Cache {
200
201 pub fn lru_remove(&mut self, e: *mut CacheHandle) {
202
203 todo!();
204 /*
205
206 */
207 }
208
209 pub fn lru_append(&mut self, e: *mut CacheHandle) {
210
211 todo!();
212 /*
213
214 */
215 }
216
217 pub fn unref(&mut self, e: *mut CacheHandle) {
218
219 todo!();
220 /*
221
222 */
223 }
224}