|
| 1 | +use crate::{Storage, Store, SPHERE_DB_STORE_NAMES}; |
| 2 | +use anyhow::Result; |
| 3 | +use async_trait::async_trait; |
| 4 | +use noosphere_common::ConditionalSync; |
| 5 | +use std::pin::Pin; |
| 6 | +use tokio_stream::{Stream, StreamExt}; |
| 7 | + |
| 8 | +#[cfg(target_arch = "wasm32")] |
| 9 | +pub type IterableStream<'a> = Pin<Box<dyn Stream<Item = Result<(Vec<u8>, Option<Vec<u8>>)>> + 'a>>; |
| 10 | +#[cfg(not(target_arch = "wasm32"))] |
| 11 | +pub type IterableStream<'a> = |
| 12 | + Pin<Box<dyn Stream<Item = Result<(Vec<u8>, Option<Vec<u8>>)>> + Send + 'a>>; |
| 13 | + |
| 14 | +#[cfg_attr(not(target_arch = "wasm32"), async_trait)] |
| 15 | +#[cfg_attr(target_arch = "wasm32", async_trait(?Send))] |
| 16 | +pub trait Iterable: Store { |
| 17 | + fn get_all_entries<'a>(&'a self) -> IterableStream<'a>; |
| 18 | +} |
| 19 | + |
| 20 | +/// A blanket implementation for [Storage]s that can be |
| 21 | +/// imported via [Importable::import]. |
| 22 | +#[cfg_attr(not(target_arch = "wasm32"), async_trait)] |
| 23 | +#[cfg_attr(target_arch = "wasm32", async_trait(?Send))] |
| 24 | +pub trait Exportable |
| 25 | +where |
| 26 | + Self: Storage, |
| 27 | + <Self as Storage>::KeyValueStore: Iterable, |
| 28 | +{ |
| 29 | + async fn get_all_store_names(&self) -> Result<Vec<String>> { |
| 30 | + let mut names = vec![]; |
| 31 | + names.extend(SPHERE_DB_STORE_NAMES.iter().map(|name| String::from(*name))); |
| 32 | + Ok(names) |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +impl<S> Exportable for S |
| 37 | +where |
| 38 | + S: Storage, |
| 39 | + S::KeyValueStore: Iterable, |
| 40 | +{ |
| 41 | +} |
| 42 | + |
| 43 | +/// A blanket implementation for all [Storage]s to import |
| 44 | +/// an [Exportable] storage. |
| 45 | +#[cfg_attr(not(target_arch = "wasm32"), async_trait)] |
| 46 | +#[cfg_attr(target_arch = "wasm32", async_trait(?Send))] |
| 47 | +pub trait Importable<'a, E> |
| 48 | +where |
| 49 | + Self: Storage, |
| 50 | + Self::KeyValueStore: Store, |
| 51 | + E: Exportable + ConditionalSync + 'a, |
| 52 | + <E as Storage>::KeyValueStore: Iterable, |
| 53 | +{ |
| 54 | + async fn import(&'a mut self, exportable: E) -> Result<()> { |
| 55 | + for store_name in exportable.get_all_store_names().await? { |
| 56 | + let mut store = self.get_key_value_store(&store_name).await?; |
| 57 | + let export_store = exportable.get_key_value_store(&store_name).await?; |
| 58 | + { |
| 59 | + let mut stream = export_store.get_all_entries(); |
| 60 | + while let Some((key, value)) = stream.try_next().await? { |
| 61 | + if let Some(value) = value { |
| 62 | + Store::write(&mut store, key.as_ref(), value.as_ref()).await?; |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + drop(export_store) |
| 67 | + } |
| 68 | + Ok(()) |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +#[cfg_attr(not(target_arch = "wasm32"), async_trait)] |
| 73 | +#[cfg_attr(target_arch = "wasm32", async_trait(?Send))] |
| 74 | +impl<'a, T, E> Importable<'a, E> for T |
| 75 | +where |
| 76 | + T: Storage, |
| 77 | + T::KeyValueStore: Store, |
| 78 | + E: Exportable + ConditionalSync + 'a, |
| 79 | + <E as Storage>::KeyValueStore: Iterable, |
| 80 | +{ |
| 81 | +} |
| 82 | + |
| 83 | +#[cfg(test)] |
| 84 | +mod test { |
| 85 | + use super::*; |
| 86 | + use std::path::PathBuf; |
| 87 | + |
| 88 | + #[cfg(target_arch = "wasm32")] |
| 89 | + use wasm_bindgen_test::{wasm_bindgen_test, wasm_bindgen_test_configure}; |
| 90 | + #[cfg(target_arch = "wasm32")] |
| 91 | + wasm_bindgen_test_configure!(run_in_browser); |
| 92 | + |
| 93 | + struct TempPath { |
| 94 | + #[cfg(target_arch = "wasm32")] |
| 95 | + pub path: String, |
| 96 | + #[cfg(not(target_arch = "wasm32"))] |
| 97 | + pub path: PathBuf, |
| 98 | + #[cfg(not(target_arch = "wasm32"))] |
| 99 | + #[allow(unused)] |
| 100 | + temp_dir: tempfile::TempDir, |
| 101 | + } |
| 102 | + |
| 103 | + impl TempPath { |
| 104 | + pub fn new() -> Result<Self> { |
| 105 | + #[cfg(target_arch = "wasm32")] |
| 106 | + let path: String = witty_phrase_generator::WPGen::new() |
| 107 | + .with_words(3) |
| 108 | + .unwrap() |
| 109 | + .into_iter() |
| 110 | + .map(|word| String::from(word)) |
| 111 | + .collect(); |
| 112 | + |
| 113 | + #[cfg(not(target_arch = "wasm32"))] |
| 114 | + let temp_dir = tempfile::TempDir::new()?; |
| 115 | + #[cfg(not(target_arch = "wasm32"))] |
| 116 | + let path = temp_dir.path().to_owned(); |
| 117 | + |
| 118 | + #[cfg(target_arch = "wasm32")] |
| 119 | + let obj = Self { path }; |
| 120 | + #[cfg(not(target_arch = "wasm32"))] |
| 121 | + let obj = Self { temp_dir, path }; |
| 122 | + |
| 123 | + Ok(obj) |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + /// wasm32: MemoryStorage -> IndexedDbStorage |
| 128 | + /// native: SledStorage -> MemoryStorage |
| 129 | + /// native+rocks: SledStorage -> RocksDbStorage |
| 130 | + #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] |
| 131 | + #[cfg_attr(not(target_arch = "wasm32"), tokio::test)] |
| 132 | + pub async fn it_can_migrate_to_new_storage_backend() -> Result<()> { |
| 133 | + noosphere_core::tracing::initialize_tracing(None); |
| 134 | + |
| 135 | + #[allow(unused)] |
| 136 | + let from_temp_path = TempPath::new()?; |
| 137 | + #[allow(unused)] |
| 138 | + let to_temp_path = TempPath::new()?; |
| 139 | + |
| 140 | + #[cfg(target_arch = "wasm32")] |
| 141 | + let from_storage = crate::MemoryStorage::default(); |
| 142 | + #[cfg(not(target_arch = "wasm32"))] |
| 143 | + let from_storage = crate::SledStorage::new(from_temp_path.path.clone())?; |
| 144 | + |
| 145 | + #[cfg(target_arch = "wasm32")] |
| 146 | + let mut to_storage = crate::IndexedDbStorage::new(&to_temp_path.path).await?; |
| 147 | + #[cfg(all(feature = "rocksdb", not(target_arch = "wasm32")))] |
| 148 | + let mut to_storage = crate::RocksDbStorage::new(to_temp_path.path.clone())?; |
| 149 | + #[cfg(all(not(feature = "rocksdb"), not(target_arch = "wasm32")))] |
| 150 | + let mut to_storage = crate::MemoryStorage::default(); |
| 151 | + |
| 152 | + { |
| 153 | + let mut store = from_storage.get_key_value_store("links").await?; |
| 154 | + for n in 0..10 { |
| 155 | + let bytes = vec![n; 10]; |
| 156 | + store.write(&[n], bytes.as_ref()).await?; |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + to_storage.import(from_storage).await?; |
| 161 | + |
| 162 | + { |
| 163 | + let store = to_storage.get_key_value_store("links").await?; |
| 164 | + for n in 0..10 { |
| 165 | + let expected_bytes = vec![n; 10]; |
| 166 | + |
| 167 | + if let Some(bytes) = store.read(&[n]).await? { |
| 168 | + assert_eq!(bytes, expected_bytes); |
| 169 | + } else { |
| 170 | + panic!("Expected key `{n}` to exist in new db"); |
| 171 | + } |
| 172 | + } |
| 173 | + } |
| 174 | + Ok(()) |
| 175 | + } |
| 176 | +} |
0 commit comments