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
use std::sync::Arc;
use actix_storage::{dev::Store, Result};
use dashmap::DashMap;
#[derive(Debug, Default)]
pub struct DashMapStore {
map: DashMap<Arc<[u8]>, Arc<[u8]>>,
}
impl DashMapStore {
pub fn new() -> Self {
Self::default()
}
pub fn with_capacity(capacity: usize) -> Self {
Self {
map: DashMap::with_capacity(capacity),
}
}
pub fn from_dashmap(map: DashMap<Arc<[u8]>, Arc<[u8]>>) -> Self {
Self { map }
}
}
#[async_trait::async_trait]
impl Store for DashMapStore {
async fn set(&self, key: Arc<[u8]>, value: Arc<[u8]>) -> Result<()> {
self.map.insert(key, value);
Ok(())
}
async fn get(&self, key: Arc<[u8]>) -> Result<Option<Arc<[u8]>>> {
let val = match self.map.get(key.as_ref()) {
Some(val) => Some(val.value().clone()),
None => None,
};
Ok(val)
}
async fn delete(&self, key: Arc<[u8]>) -> Result<()> {
self.map.remove(key.as_ref());
Ok(())
}
async fn contains_key(&self, key: Arc<[u8]>) -> Result<bool> {
Ok(self.map.contains_key(key.as_ref()))
}
}
#[cfg(test)]
mod test {
use super::*;
use actix_storage::tests::*;
#[actix_rt::test]
async fn test_dashmap_basic_store() {
let store = DashMapStore::default();
test_store(store).await;
}
#[actix_rt::test]
async fn test_dashmap_basic_formats() {
impl Clone for DashMapStore {
fn clone(&self) -> Self {
Self {
map: self.map.clone(),
}
}
}
let store = DashMapStore::default();
test_all_formats(store).await;
}
}