mirror of
https://github.com/ShadowJonathan/conduit_toolbox.git
synced 2025-07-27 13:40:52 +03:00
refactor
This commit is contained in:
parent
03fde79046
commit
8eabefe55b
9 changed files with 159 additions and 82 deletions
43
tools/iface/src/db.rs
Normal file
43
tools/iface/src/db.rs
Normal file
|
@ -0,0 +1,43 @@
|
|||
pub mod sled;
|
||||
pub mod sqlite;
|
||||
|
||||
use itertools::Itertools;
|
||||
|
||||
pub type KVIter<'a> = Box<dyn Iterator<Item = (Vec<u8>, Vec<u8>)> + 'a>;
|
||||
|
||||
pub type TreeKVIter<'a> = Box<dyn Iterator<Item = (Vec<u8>, KVIter<'a>)> + 'a>;
|
||||
|
||||
pub trait Database {
|
||||
fn iter<'a>(&'a self) -> TreeKVIter<'a>;
|
||||
|
||||
fn segment<'a>(&'a mut self, name: Vec<u8>) -> Option<Box<dyn Segment + 'a>>; // change return type to Result
|
||||
}
|
||||
|
||||
pub trait Segment {
|
||||
fn batch_insert<'a>(
|
||||
&'a mut self,
|
||||
batch: Box<dyn Iterator<Item = (Vec<u8>, Vec<u8>)> + 'a>,
|
||||
) -> anyhow::Result<()>;
|
||||
}
|
||||
|
||||
pub fn copy_database(
|
||||
src: &impl Database,
|
||||
dst: &mut impl Database,
|
||||
chunk_size: usize,
|
||||
) -> anyhow::Result<()> {
|
||||
for (tree, i) in src.iter() {
|
||||
dbg!(&tree);
|
||||
|
||||
let mut t = dst.segment(tree).unwrap(); // todo remove unwrap
|
||||
|
||||
let mut x: usize = 0;
|
||||
|
||||
for chunk in &i.chunks(chunk_size) {
|
||||
dbg!(&x);
|
||||
t.batch_insert(Box::new(chunk))?;
|
||||
x += chunk_size;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue