aboutsummaryrefslogtreecommitdiff
path: root/src/db/sled_adapter.rs
diff options
context:
space:
mode:
authorAlex Auvolat <alex@adnab.me>2022-06-03 11:44:41 +0200
committerAlex Auvolat <alex@adnab.me>2022-06-03 11:44:41 +0200
commitdf0877bbba91d210fa8a91bd095ca13e0ea2176f (patch)
treeba76018818ff1afb5e84fbda0dc91f802e806142 /src/db/sled_adapter.rs
parentcc0d984118e345c72341ff12d74b1c35deb20ae8 (diff)
downloadgarage-df0877bbba91d210fa8a91bd095ca13e0ea2176f.tar.gz
garage-df0877bbba91d210fa8a91bd095ca13e0ea2176f.zip
Conversion utility
Diffstat (limited to 'src/db/sled_adapter.rs')
-rw-r--r--src/db/sled_adapter.rs64
1 files changed, 14 insertions, 50 deletions
diff --git a/src/db/sled_adapter.rs b/src/db/sled_adapter.rs
index 7382776f..3388b0ca 100644
--- a/src/db/sled_adapter.rs
+++ b/src/db/sled_adapter.rs
@@ -10,8 +10,7 @@ use sled::transaction::{
};
use crate::{
- Db, Error, Exporter, IDb, ITx, ITxFn, IValue, Result, TxError, TxFnResult, TxResult, Value,
- ValueIter,
+ Db, Error, IDb, ITx, ITxFn, IValue, Result, TxError, TxFnResult, TxResult, Value, ValueIter,
};
pub use sled;
@@ -85,6 +84,19 @@ impl IDb for SledDb {
}
}
+ fn list_trees(&self) -> Result<Vec<String>> {
+ let mut trees = vec![];
+ for name in self.db.tree_names() {
+ let name = std::str::from_utf8(&name)
+ .map_err(|e| Error(format!("{}", e).into()))?
+ .to_string();
+ if name != "__sled__default" {
+ trees.push(name);
+ }
+ }
+ Ok(trees)
+ }
+
// ----
fn get(&self, tree: usize, key: &[u8]) -> Result<Option<Value<'_>>> {
@@ -175,54 +187,6 @@ impl IDb for SledDb {
Err(TransactionError::Storage(s)) => Err(TxError::Db(s.into())),
}
}
-
- // ----
-
- fn export(&self) -> Result<Exporter<'_>> {
- let mut trees = vec![];
- for name in self.db.tree_names() {
- let name = std::str::from_utf8(&name)
- .map_err(|e| Error(format!("{}", e).into()))?
- .to_string();
- let tree = self.open_tree(&name)?;
- let tree = self.trees.read().unwrap().0.get(tree).unwrap().clone();
- trees.push((name, tree));
- }
- let trees_exporter: Exporter<'_> = Box::new(trees.into_iter().map(|(name, tree)| {
- let iter: ValueIter<'_> = Box::new(
- tree.iter()
- .map(|v| v.map(|(x, y)| (x.into(), y.into())).map_err(Into::into)),
- );
- Ok((name, iter))
- }));
- Ok(trees_exporter)
- }
-
- fn import(&self, ex: Exporter<'_>) -> Result<()> {
- for ex_tree in ex {
- let (name, data) = ex_tree?;
-
- let tree = self.open_tree(&name)?;
- let tree = self.trees.read().unwrap().0.get(tree).unwrap().clone();
- if !tree.is_empty() {
- return Err(Error(format!("tree {} already contains data", name).into()));
- }
-
- let mut i = 0;
- for item in data {
- let (k, v) = item?;
- tree.insert(k, v)?;
- i += 1;
- if i % 1000 == 0 {
- println!("{}: imported {}", name, i);
- }
- }
- println!("{}: finished importing, {} items", name, i);
- }
- Ok(())
- }
-
- // ----
}
// ----