|
6 | 6 | // accordance with one or both of these licenses.
|
7 | 7 |
|
8 | 8 | //! Objects related to [`SqliteStore`] live here.
|
| 9 | +use std::boxed::Box; |
9 | 10 | use std::fs;
|
| 11 | +use std::future::Future; |
10 | 12 | use std::path::PathBuf;
|
| 13 | +use std::pin::Pin; |
11 | 14 | use std::sync::{Arc, Mutex};
|
12 | 15 |
|
13 | 16 | use lightning::io;
|
14 |
| -use lightning::util::persist::KVStoreSync; |
| 17 | +use lightning::util::persist::{KVStore, KVStoreSync}; |
15 | 18 | use lightning_types::string::PrintableString;
|
16 | 19 | use rusqlite::{named_params, Connection};
|
17 | 20 |
|
@@ -60,6 +63,76 @@ impl SqliteStore {
|
60 | 63 | }
|
61 | 64 | }
|
62 | 65 |
|
| 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 | + |
63 | 136 | impl KVStoreSync for SqliteStore {
|
64 | 137 | fn read(
|
65 | 138 | &self, primary_namespace: &str, secondary_namespace: &str, key: &str,
|
|
0 commit comments