Skip to content

Commit 5f59132

Browse files
committed
Implement KVStore for SqliteStore
1 parent 00baaa2 commit 5f59132

File tree

1 file changed

+74
-1
lines changed

1 file changed

+74
-1
lines changed

src/io/sqlite_store/mod.rs

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@
66
// accordance with one or both of these licenses.
77

88
//! Objects related to [`SqliteStore`] live here.
9+
use std::boxed::Box;
910
use std::fs;
11+
use std::future::Future;
1012
use std::path::PathBuf;
13+
use std::pin::Pin;
1114
use std::sync::{Arc, Mutex};
1215

1316
use lightning::io;
14-
use lightning::util::persist::KVStoreSync;
17+
use lightning::util::persist::{KVStore, KVStoreSync};
1518
use lightning_types::string::PrintableString;
1619
use rusqlite::{named_params, Connection};
1720

@@ -60,6 +63,76 @@ impl SqliteStore {
6063
}
6164
}
6265

66+
impl KVStore for SqliteStore {
67+
fn read(
68+
&self, primary_namespace: &str, secondary_namespace: &str, key: &str,
69+
) -> Pin<Box<dyn Future<Output = Result<Vec<u8>, io::Error>> + Send>> {
70+
let primary_namespace = primary_namespace.to_string();
71+
let secondary_namespace = secondary_namespace.to_string();
72+
let key = key.to_string();
73+
let inner = Arc::clone(&self.inner);
74+
let fut = tokio::task::spawn_blocking(move || {
75+
inner.read_internal(&primary_namespace, &secondary_namespace, &key)
76+
});
77+
Box::pin(async move {
78+
fut.await.unwrap_or_else(|e| {
79+
let msg = format!("Failed to IO operation due join error: {}", e);
80+
Err(io::Error::new(io::ErrorKind::Other, msg))
81+
})
82+
})
83+
}
84+
fn write(
85+
&self, primary_namespace: &str, secondary_namespace: &str, key: &str, buf: Vec<u8>,
86+
) -> Pin<Box<dyn Future<Output = Result<(), io::Error>> + Send>> {
87+
let primary_namespace = primary_namespace.to_string();
88+
let secondary_namespace = secondary_namespace.to_string();
89+
let key = key.to_string();
90+
let inner = Arc::clone(&self.inner);
91+
let fut = tokio::task::spawn_blocking(move || {
92+
inner.write_internal(&primary_namespace, &secondary_namespace, &key, buf)
93+
});
94+
Box::pin(async move {
95+
fut.await.unwrap_or_else(|e| {
96+
let msg = format!("Failed to IO operation due join error: {}", e);
97+
Err(io::Error::new(io::ErrorKind::Other, msg))
98+
})
99+
})
100+
}
101+
fn remove(
102+
&self, primary_namespace: &str, secondary_namespace: &str, key: &str, lazy: bool,
103+
) -> Pin<Box<dyn Future<Output = Result<(), io::Error>> + Send>> {
104+
let primary_namespace = primary_namespace.to_string();
105+
let secondary_namespace = secondary_namespace.to_string();
106+
let key = key.to_string();
107+
let inner = Arc::clone(&self.inner);
108+
let fut = tokio::task::spawn_blocking(move || {
109+
inner.remove_internal(&primary_namespace, &secondary_namespace, &key, lazy)
110+
});
111+
Box::pin(async move {
112+
fut.await.unwrap_or_else(|e| {
113+
let msg = format!("Failed to IO operation due join error: {}", e);
114+
Err(io::Error::new(io::ErrorKind::Other, msg))
115+
})
116+
})
117+
}
118+
fn list(
119+
&self, primary_namespace: &str, secondary_namespace: &str,
120+
) -> Pin<Box<dyn Future<Output = Result<Vec<String>, io::Error>> + Send>> {
121+
let primary_namespace = primary_namespace.to_string();
122+
let secondary_namespace = secondary_namespace.to_string();
123+
let inner = Arc::clone(&self.inner);
124+
let fut = tokio::task::spawn_blocking(move || {
125+
inner.list_internal(&primary_namespace, &secondary_namespace)
126+
});
127+
Box::pin(async move {
128+
fut.await.unwrap_or_else(|e| {
129+
let msg = format!("Failed to IO operation due join error: {}", e);
130+
Err(io::Error::new(io::ErrorKind::Other, msg))
131+
})
132+
})
133+
}
134+
}
135+
63136
impl KVStoreSync for SqliteStore {
64137
fn read(
65138
&self, primary_namespace: &str, secondary_namespace: &str, key: &str,

0 commit comments

Comments
 (0)