1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
use crate::client::stream::{ClientKVPayload, ClientStreamReq};
use crate::helpers::deserialize;
use crate::network::api::ApiStreamResponsePayload;
use crate::network::serialize_network;
use crate::store::state_machine::memory::kv_handler::CacheRequestHandler;
use crate::store::state_machine::memory::state_machine::{CacheRequest, CacheResponse};
use crate::{CacheVariants, Client, Error};
use chrono::Utc;
use serde::{Deserialize, Serialize};
use std::borrow::Cow;
use std::collections::BTreeMap;
use tokio::sync::oneshot;
impl Client {
/// Clears a single cache.
pub async fn clear_cache<C>(&self, cache: C) -> Result<(), Error>
where
C: CacheVariants,
{
self.cache_req_retry(
CacheRequest::Clear {
cache_idx: cache.hiqlite_cache_index(),
},
false,
)
.await?;
Ok(())
}
/// Clears all counters for the given Cache
#[cfg(feature = "counters")]
pub async fn clear_counters<C>(&self, cache: C) -> Result<(), Error>
where
C: CacheVariants,
{
self.cache_req_retry(
CacheRequest::ClearCounters {
cache_idx: cache.hiqlite_cache_index(),
},
false,
)
.await?;
Ok(())
}
/// Clears all available caches.
pub async fn clear_cache_all(&self) -> Result<(), Error> {
self.cache_req_retry(CacheRequest::ClearAll, false).await?;
Ok(())
}
/// GET a value from the cache.
///
/// ```rust, notest
/// let key = "my key 1";
/// let value = Value {
/// id: "some id".to_string(),
/// num: 1337,
/// description: Some("My Example Description".to_string()),
/// };
///
/// // Insert a value that will expire 1 second later. Each value has its own custom expiry.
/// client.put(Cache::One, key, &value, Some(1)).await?;
///
/// let v: Value = client.get(Cache::One, key).await?.unwrap();
// assert_eq!(v, value);
/// ```
pub async fn get<C, K, V>(&self, cache: C, key: K) -> Result<Option<V>, Error>
where
C: CacheVariants,
K: Into<String>,
V: for<'a> Deserialize<'a>,
{
match self.get_bytes(cache, key).await {
Ok(value) => {
if let Some(v) = value {
Ok(Some(deserialize(&v)?))
} else {
Ok(None)
}
}
Err(err) => Err(err),
}
}
/// GET a raw bytes value from the cache.
///
/// Works in the same way as `.get()` without any value mapping.
pub async fn get_bytes<C, K>(&self, cache: C, key: K) -> Result<Option<Vec<u8>>, Error>
where
C: CacheVariants,
K: Into<String>,
{
if let Some(state) = &self.inner.state {
let (ack, rx) = oneshot::channel();
state
.raft_cache
.tx_caches
.get(cache.hiqlite_cache_index())
.unwrap()
.send(CacheRequestHandler::Get((key.into(), ack)))
.expect("kv handler to always be running");
let value = rx
.await
.expect("to always get an answer from the kv handler");
Ok(value)
} else {
let res = self
.cache_req_retry(
CacheRequest::Get {
cache_idx: cache.hiqlite_cache_index(),
key: key.into(),
},
true,
)
.await?;
match res {
CacheResponse::Value(opt) => Ok(opt),
_ => unreachable!(),
}
}
}
/// GET a full snapshot of the current cache.
///
/// This function does not for remote caches, only if this node is actually a Raft member.
///
/// CAUTION: Entry expiry does not work on a snapshot. This is frozen in time and not live data!
pub async fn get_snapshot<C, V>(&self, cache: C) -> Result<BTreeMap<String, V>, Error>
where
C: CacheVariants,
V: for<'a> Deserialize<'a>,
{
if let Some(state) = &self.inner.state {
let (ack, rx) = oneshot::channel();
state
.raft_cache
.tx_caches
.get(cache.hiqlite_cache_index())
.unwrap()
.send(CacheRequestHandler::SnapshotBuildCacheOnly(ack))
.expect("kv handler to always be running");
let snapshot = rx
.await
.expect("to always get an answer from the kv handler");
let mut res = BTreeMap::new();
for (k, v) in snapshot {
res.insert(k, deserialize(&v)?);
}
Ok(res)
} else {
Err(Error::Error(
"This function does only work for Raft members and not on remote nodes.".into(),
))
}
}
/// `Put` a value into the cache.
/// The optional `ttl` is the lifetime of the value in seconds from *now* on.
///
/// ```rust, notest
/// let key = "my key 1";
/// let value = Value {
/// id: "some id".to_string(),
/// num: 1337,
/// description: Some("My Example Description".to_string()),
/// };
///
/// // Insert a value that will expire 1 second later. Each value has its own custom expiry.
/// client.put(Cache::One, key, &value, Some(1)).await?;
///
/// let v: Value = client.get(Cache::One, key).await?.unwrap();
/// assert_eq!(v, value);
/// ```
pub async fn put<C, K, V>(
&self,
cache: C,
key: K,
value: &V,
ttl: Option<i64>,
) -> Result<(), Error>
where
C: CacheVariants,
K: Into<Cow<'static, str>>,
V: Serialize,
{
self.put_bytes(cache, key, serialize_network(value), ttl)
.await?;
Ok(())
}
/// PUT a raw bytes value into the cache
pub async fn put_bytes<C, K>(
&self,
cache: C,
key: K,
value: Vec<u8>,
ttl: Option<i64>,
) -> Result<(), Error>
where
C: CacheVariants,
K: Into<Cow<'static, str>>,
{
self.cache_req_retry(
CacheRequest::Put {
cache_idx: cache.hiqlite_cache_index(),
key: key.into(),
value,
expires: ttl.map(|seconds| Utc::now().timestamp().saturating_add(seconds)),
},
false,
)
.await?;
Ok(())
}
/// `Delete` a value from the cache.
pub async fn delete<C, K>(&self, cache: C, key: K) -> Result<(), Error>
where
C: CacheVariants,
K: Into<Cow<'static, str>>,
{
self.cache_req_retry(
CacheRequest::Delete {
cache_idx: cache.hiqlite_cache_index(),
key: key.into(),
},
false,
)
.await?;
Ok(())
}
/// Get the current counter value for the Cache + Key
#[cfg(feature = "counters")]
pub async fn counter_get<C, K>(&self, cache: C, key: K) -> Result<Option<i64>, Error>
where
C: CacheVariants,
K: Into<Cow<'static, str>>,
{
if let Some(state) = &self.inner.state {
let (ack, rx) = oneshot::channel();
state
.raft_cache
.tx_caches
.get(cache.hiqlite_cache_index())
.unwrap()
.send(CacheRequestHandler::CounterGet((
key.into().to_string(),
ack,
)))
.expect("kv handler to always be running");
let value = rx
.await
.expect("to always get an answer from the kv handler");
Ok(value)
} else {
let res = self
.cache_req_retry(
CacheRequest::CounterGet {
cache_idx: cache.hiqlite_cache_index(),
key: key.into(),
},
true,
)
.await?;
match res {
CacheResponse::CounterValue(opt) => Ok(opt),
_ => unreachable!(),
}
}
}
/// Sets the counter to a fixed value, no matter what the current one is.
#[cfg(feature = "counters")]
pub async fn counter_set<C, K>(&self, cache: C, key: K, value: i64) -> Result<(), Error>
where
C: CacheVariants,
K: Into<Cow<'static, str>>,
{
self.cache_req_retry(
CacheRequest::CounterSet {
cache_idx: cache.hiqlite_cache_index(),
key: key.into(),
value,
},
false,
)
.await?;
Ok(())
}
/// Adds the given value to the Cache + Key and returns the new value.
#[cfg(feature = "counters")]
pub async fn counter_add<C, K>(&self, cache: C, key: K, value: i64) -> Result<i64, Error>
where
C: CacheVariants,
K: Into<Cow<'static, str>>,
{
let resp = self
.cache_req_retry(
CacheRequest::CounterAdd {
cache_idx: cache.hiqlite_cache_index(),
key: key.into(),
value,
},
false,
)
.await?;
match resp {
CacheResponse::CounterValue(v) => {
Ok(v.expect("to always get a CounterValue after CounterAdd"))
}
_ => unreachable!(),
}
}
/// Deletes the counter for the given Cache + Key and frees up the memory, while setting it to
/// `0` would keep it in memory.
#[cfg(feature = "counters")]
pub async fn counter_del<C, K>(&self, cache: C, key: K) -> Result<(), Error>
where
C: CacheVariants,
K: Into<Cow<'static, str>>,
{
self.cache_req_retry(
CacheRequest::CounterDel {
cache_idx: cache.hiqlite_cache_index(),
key: key.into(),
},
false,
)
.await?;
Ok(())
}
pub(crate) async fn cache_req_retry(
&self,
cache_req: CacheRequest,
is_remote_get: bool,
) -> Result<CacheResponse, Error> {
match self.cache_req(cache_req.clone(), is_remote_get).await {
Ok(resp) => Ok(resp),
Err(err) => {
if self
.was_leader_update_error(
&err,
&self.inner.leader_cache,
&self.inner.tx_client_cache,
)
.await
{
self.cache_req(cache_req, is_remote_get).await
} else {
Err(err)
}
}
}
}
async fn cache_req(
&self,
cache_req: CacheRequest,
is_remote_get: bool,
) -> Result<CacheResponse, Error> {
if let Some(state) = self.is_leader_cache_with_state().await {
let res = state.raft_cache.raft.client_write(cache_req).await?;
Ok(res.data)
} else {
let (ack, rx) = oneshot::channel();
let payload = if is_remote_get {
ClientStreamReq::KVGet(ClientKVPayload {
request_id: self.new_request_id(),
cache_req,
ack,
})
} else {
ClientStreamReq::KV(ClientKVPayload {
request_id: self.new_request_id(),
cache_req,
ack,
})
};
self.inner
.tx_client_cache
.send_async(payload)
.await
.map_err(|err| Error::Error(err.to_string().into()))?;
let res = rx
.await
.expect("To always receive an answer from Client Stream Manager")?;
match res {
ApiStreamResponsePayload::KV(res) => res,
#[cfg(any(feature = "sqlite", feature = "dlock", feature = "listen_notify_local"))]
_ => unreachable!(),
}
}
}
}