feat: Allow dumping the database, useful for full snapshot

This commit is contained in:
Clément Renault 2018-12-10 11:09:09 +01:00
parent 0f8ae5a8bc
commit 916b46c839
No known key found for this signature in database
GPG Key ID: 0151CDAB43460DAE

View File

@ -1,9 +1,10 @@
use std::error::Error; use std::error::Error;
use std::path::Path;
use std::ops::Deref; use std::ops::Deref;
use std::{fmt, marker}; use std::{fmt, marker};
use rocksdb::rocksdb::{DB, DBVector, Snapshot, SeekKey}; use rocksdb::rocksdb_options::{ReadOptions, EnvOptions, ColumnFamilyOptions};
use rocksdb::rocksdb_options::ReadOptions; use rocksdb::rocksdb::{DB, DBVector, Snapshot, SeekKey, SstFileWriter};
use serde::de::DeserializeOwned; use serde::de::DeserializeOwned;
use crate::database::{DocumentKey, DocumentKeyAttr}; use crate::database::{DocumentKey, DocumentKeyAttr};
@ -52,6 +53,25 @@ where D: Deref<Target=DB>
Ok(self.snapshot.get(key)?) Ok(self.snapshot.get(key)?)
} }
pub fn dump_all<P: AsRef<Path>>(&self, path: P) -> Result<(), Box<Error>> {
let path = path.as_ref().to_string_lossy();
let env_options = EnvOptions::new();
let column_family_options = ColumnFamilyOptions::new();
let mut file_writer = SstFileWriter::new(env_options, column_family_options);
file_writer.open(&path)?;
let mut iter = self.snapshot.iter();
iter.seek(SeekKey::Start);
for (key, value) in &mut iter {
file_writer.put(&key, &value)?;
}
file_writer.finish()?;
Ok(())
}
pub fn query_builder(&self) -> Result<QueryBuilder<D, Box<dyn Criterion<D>>>, Box<Error>> { pub fn query_builder(&self) -> Result<QueryBuilder<D, Box<dyn Criterion<D>>>, Box<Error>> {
QueryBuilder::new(self) QueryBuilder::new(self)
} }