Repository navigation
Expand file tree
/
Copy pathstorage.rs
More file actions
131 lines (118 loc) · 2.91 KB
/
Copy pathstorage.rs
File metadata and controls
131 lines (118 loc) · 2.91 KB
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
impl Default for MemStore {
fn default() -> Self {
Self::new()
}
}
///
/// Supports in-memory, RocksDB, and Sled backends. Used for S3/data paths.
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
#[cfg(feature = "rocksdb")]
use rocksdb::{Options, DB};
#[cfg(feature = "sled")]
use sled;
pub trait KVStore: Send + Sync {
fn get(&self, key: &str) -> Option<Vec<u8>>;
fn put(&self, key: &str, value: Vec<u8>);
fn delete(&self, key: &str);
}
pub struct MemStore {
map: Mutex<HashMap<String, Vec<u8>>>,
}
impl MemStore {
pub fn new() -> Self {
Self {
map: Mutex::new(HashMap::new()),
}
}
}
impl KVStore for MemStore {
fn get(&self, key: &str) -> Option<Vec<u8>> {
self.map.lock().unwrap().get(key).cloned()
}
fn put(&self, key: &str, value: Vec<u8>) {
self.map.lock().unwrap().insert(key.to_string(), value);
}
fn delete(&self, key: &str) {
self.map.lock().unwrap().remove(key);
}
}
#[cfg(feature = "rocksdb")]
pub struct RocksStore {
db: DB,
}
#[cfg(feature = "rocksdb")]
impl RocksStore {
pub fn new(path: &str) -> Self {
let mut opts = Options::default();
opts.create_if_missing(true);
let db = DB::open(&opts, path).unwrap();
Self { db }
}
}
#[cfg(feature = "rocksdb")]
impl KVStore for RocksStore {
fn get(&self, key: &str) -> Option<Vec<u8>> {
self.db.get(key).unwrap()
}
fn put(&self, key: &str, value: Vec<u8>) {
self.db.put(key, value).unwrap();
}
fn delete(&self, key: &str) {
self.db.delete(key).unwrap();
}
}
#[cfg(feature = "sled")]
pub struct SledStore {
db: sled::Db,
}
#[cfg(feature = "sled")]
impl SledStore {
pub fn new(path: &str) -> Self {
let db = sled::open(path).unwrap();
Self { db }
}
}
#[cfg(feature = "sled")]
impl KVStore for SledStore {
fn get(&self, key: &str) -> Option<Vec<u8>> {
self.db.get(key).unwrap().map(|ivec| ivec.to_vec())
}
fn put(&self, key: &str, value: Vec<u8>) {
self.db.insert(key, value).unwrap();
}
fn delete(&self, key: &str) {
self.db.remove(key).unwrap();
}
}
pub struct Storage {
backend: Arc<dyn KVStore>,
}
impl Storage {
pub fn new_memory() -> Self {
Self {
backend: Arc::new(MemStore::new()),
}
}
#[cfg(feature = "rocksdb")]
pub fn new_rocks(path: &str) -> Self {
Self {
backend: Arc::new(RocksStore::new(path)),
}
}
#[cfg(feature = "sled")]
pub fn new_sled(path: &str) -> Self {
Self {
backend: Arc::new(SledStore::new(path)),
}
}
pub fn get(&self, key: &str) -> Option<Vec<u8>> {
self.backend.get(key)
}
pub fn put(&self, key: &str, value: Vec<u8>) {
self.backend.put(key, value)
}
pub fn delete(&self, key: &str) {
self.backend.delete(key)
}
}