From 916b46c8394c18c55f0b3995b581a429bcd59b4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Renault?= Date: Mon, 10 Dec 2018 11:09:09 +0100 Subject: [PATCH] feat: Allow dumping the database, useful for full snapshot --- src/database/database_view.rs | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/database/database_view.rs b/src/database/database_view.rs index 671d18433..3fc79091f 100644 --- a/src/database/database_view.rs +++ b/src/database/database_view.rs @@ -1,9 +1,10 @@ use std::error::Error; +use std::path::Path; use std::ops::Deref; use std::{fmt, marker}; -use rocksdb::rocksdb::{DB, DBVector, Snapshot, SeekKey}; -use rocksdb::rocksdb_options::ReadOptions; +use rocksdb::rocksdb_options::{ReadOptions, EnvOptions, ColumnFamilyOptions}; +use rocksdb::rocksdb::{DB, DBVector, Snapshot, SeekKey, SstFileWriter}; use serde::de::DeserializeOwned; use crate::database::{DocumentKey, DocumentKeyAttr}; @@ -52,6 +53,25 @@ where D: Deref Ok(self.snapshot.get(key)?) } + pub fn dump_all>(&self, path: P) -> Result<(), Box> { + 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>>, Box> { QueryBuilder::new(self) }