|
| 1 | +//! Objects related to [`FilesystemStoreAsync`] live here. |
| 2 | +
|
| 3 | +use std::sync::{ |
| 4 | + atomic::{AtomicU64, Ordering}, |
| 5 | + Arc, |
| 6 | +}; |
| 7 | + |
| 8 | +use crate::fs_store::FilesystemStore; |
| 9 | +use core::future::Future; |
| 10 | +use core::pin::Pin; |
| 11 | +use lightning::util::persist::{KVStore, KVStoreSync}; |
| 12 | + |
| 13 | +/// An asynchronous extension of FilesystemStore, implementing the `KVStore` trait for async operations. It is shaped as |
| 14 | +/// a wrapper around an existing [`FilesystemStore`] so that the same locks are used. This allows both the sync and |
| 15 | +/// async interface to be used simultaneously. |
| 16 | +pub struct FilesystemStoreAsync { |
| 17 | + inner: Arc<FilesystemStore>, |
| 18 | + version_counter: Arc<AtomicU64>, |
| 19 | +} |
| 20 | + |
| 21 | +impl FilesystemStoreAsync { |
| 22 | + /// Creates a new instance of [`FilesystemStoreAsync`]. |
| 23 | + pub fn new(inner: Arc<FilesystemStore>) -> Self { |
| 24 | + Self { inner, version_counter: Arc::new(AtomicU64::new(0)) } |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +impl KVStore for FilesystemStoreAsync { |
| 29 | + fn read( |
| 30 | + &self, primary_namespace: &str, secondary_namespace: &str, key: &str, |
| 31 | + ) -> Pin<Box<dyn Future<Output = Result<Vec<u8>, lightning::io::Error>> + 'static + Send>> { |
| 32 | + let primary_namespace = primary_namespace.to_string(); |
| 33 | + let secondary_namespace = secondary_namespace.to_string(); |
| 34 | + let key = key.to_string(); |
| 35 | + let this = Arc::clone(&self.inner); |
| 36 | + |
| 37 | + Box::pin(async move { |
| 38 | + tokio::task::spawn_blocking(move || { |
| 39 | + this.read(&primary_namespace, &secondary_namespace, &key) |
| 40 | + }) |
| 41 | + .await |
| 42 | + .unwrap_or_else(|e| Err(lightning::io::Error::new(lightning::io::ErrorKind::Other, e))) |
| 43 | + }) |
| 44 | + } |
| 45 | + |
| 46 | + fn write( |
| 47 | + &self, primary_namespace: &str, secondary_namespace: &str, key: &str, buf: &[u8], |
| 48 | + ) -> Pin<Box<dyn Future<Output = Result<(), lightning::io::Error>> + 'static + Send>> { |
| 49 | + let primary_namespace = primary_namespace.to_string(); |
| 50 | + let secondary_namespace = secondary_namespace.to_string(); |
| 51 | + let key = key.to_string(); |
| 52 | + let buf = buf.to_vec(); |
| 53 | + let this = Arc::clone(&self.inner); |
| 54 | + |
| 55 | + // Obtain a version number to retain the call sequence. |
| 56 | + let version = self.version_counter.fetch_add(1, Ordering::SeqCst); |
| 57 | + |
| 58 | + Box::pin(async move { |
| 59 | + tokio::task::spawn_blocking(move || { |
| 60 | + this.write_version( |
| 61 | + &primary_namespace, |
| 62 | + &secondary_namespace, |
| 63 | + &key, |
| 64 | + &buf, |
| 65 | + Some(version), |
| 66 | + ) |
| 67 | + }) |
| 68 | + .await |
| 69 | + .unwrap_or_else(|e| Err(lightning::io::Error::new(lightning::io::ErrorKind::Other, e))) |
| 70 | + }) |
| 71 | + } |
| 72 | + |
| 73 | + fn remove( |
| 74 | + &self, primary_namespace: &str, secondary_namespace: &str, key: &str, lazy: bool, |
| 75 | + ) -> Pin<Box<dyn Future<Output = Result<(), lightning::io::Error>> + 'static + Send>> { |
| 76 | + let primary_namespace = primary_namespace.to_string(); |
| 77 | + let secondary_namespace = secondary_namespace.to_string(); |
| 78 | + let key = key.to_string(); |
| 79 | + let this = Arc::clone(&self.inner); |
| 80 | + |
| 81 | + Box::pin(async move { |
| 82 | + tokio::task::spawn_blocking(move || { |
| 83 | + this.remove(&primary_namespace, &secondary_namespace, &key, lazy) |
| 84 | + }) |
| 85 | + .await |
| 86 | + .unwrap_or_else(|e| Err(lightning::io::Error::new(lightning::io::ErrorKind::Other, e))) |
| 87 | + }) |
| 88 | + } |
| 89 | + |
| 90 | + fn list( |
| 91 | + &self, primary_namespace: &str, secondary_namespace: &str, |
| 92 | + ) -> Pin<Box<dyn Future<Output = Result<Vec<String>, lightning::io::Error>> + 'static + Send>> { |
| 93 | + let primary_namespace = primary_namespace.to_string(); |
| 94 | + let secondary_namespace = secondary_namespace.to_string(); |
| 95 | + let this = Arc::clone(&self.inner); |
| 96 | + |
| 97 | + Box::pin(async move { |
| 98 | + tokio::task::spawn_blocking(move || this.list(&primary_namespace, &secondary_namespace)) |
| 99 | + .await |
| 100 | + .unwrap_or_else(|e| { |
| 101 | + Err(lightning::io::Error::new(lightning::io::ErrorKind::Other, e)) |
| 102 | + }) |
| 103 | + }) |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +mod test { |
| 108 | + use crate::{fs_store::FilesystemStore, fs_store_async::FilesystemStoreAsync}; |
| 109 | + use lightning::util::persist::KVStore; |
| 110 | + use std::sync::Arc; |
| 111 | + |
| 112 | + #[tokio::test] |
| 113 | + async fn read_write_remove_list_persist() { |
| 114 | + let mut temp_path = std::env::temp_dir(); |
| 115 | + temp_path.push("test_read_write_remove_list_persist"); |
| 116 | + let fs_store = Arc::new(FilesystemStore::new(temp_path)); |
| 117 | + let fs_store_async = FilesystemStoreAsync::new(Arc::clone(&fs_store)); |
| 118 | + |
| 119 | + let data1 = [42u8; 32]; |
| 120 | + let data2 = [43u8; 32]; |
| 121 | + |
| 122 | + let primary_namespace = "testspace"; |
| 123 | + let secondary_namespace = "testsubspace"; |
| 124 | + let key = "testkey"; |
| 125 | + |
| 126 | + // Test writing the same key twice with different data. Execute the asynchronous part out of order to ensure |
| 127 | + // that eventual consistency works. |
| 128 | + let fut1 = fs_store_async.write(primary_namespace, secondary_namespace, key, &data1); |
| 129 | + let fut2 = fs_store_async.write(primary_namespace, secondary_namespace, key, &data2); |
| 130 | + |
| 131 | + fut2.await.unwrap(); |
| 132 | + fut1.await.unwrap(); |
| 133 | + |
| 134 | + // Test list. |
| 135 | + let listed_keys = |
| 136 | + fs_store_async.list(primary_namespace, secondary_namespace).await.unwrap(); |
| 137 | + assert_eq!(listed_keys.len(), 1); |
| 138 | + assert_eq!(listed_keys[0], key); |
| 139 | + |
| 140 | + // Test read. We expect to read data2, as the write call was initiated later. |
| 141 | + let read_data = |
| 142 | + fs_store_async.read(primary_namespace, secondary_namespace, key).await.unwrap(); |
| 143 | + assert_eq!(data2, &*read_data); |
| 144 | + |
| 145 | + // Test remove. |
| 146 | + fs_store_async.remove(primary_namespace, secondary_namespace, key, false).await.unwrap(); |
| 147 | + |
| 148 | + let listed_keys = |
| 149 | + fs_store_async.list(primary_namespace, secondary_namespace).await.unwrap(); |
| 150 | + assert_eq!(listed_keys.len(), 0); |
| 151 | + } |
| 152 | +} |
0 commit comments