From 12d79986b2269b43a6cb808077a3aac6880a275d Mon Sep 17 00:00:00 2001 From: Alexander Regueiro Date: Sat, 26 Nov 2016 23:55:46 +0000 Subject: [PATCH] Changed rocksdb module name prefix to db. Also rustfmt on source. --- src/backup.rs | 61 ++++++++++++++++++----- src/comparator.rs | 4 +- src/{rocksdb.rs => db.rs} | 16 +++--- src/{rocksdb_options.rs => db_options.rs} | 16 +++--- src/lib.rs | 14 +++--- src/merge_operator.rs | 4 +- 6 files changed, 75 insertions(+), 40 deletions(-) rename src/{rocksdb.rs => db.rs} (99%) rename src/{rocksdb_options.rs => db_options.rs} (99%) diff --git a/src/backup.rs b/src/backup.rs index 13572a4..a49f848 100644 --- a/src/backup.rs +++ b/src/backup.rs @@ -13,14 +13,14 @@ // limitations under the License. // -use std::ffi::CString; -use std::path::{Path}; - -use libc::{uint32_t}; use {DB, Error}; use ffi; +use libc::{c_int, uint32_t}; +use std::ffi::CString; +use std::path::Path; + pub struct BackupEngine { inner: *mut ffi::rocksdb_backup_engine_t, } @@ -29,9 +29,15 @@ pub struct BackupEngineOptions { inner: *mut ffi::rocksdb_options_t, } +pub struct RestoreOptions { + inner: *mut ffi::rocksdb_restore_options_t, +} + impl BackupEngine { /// Open a backup engine with the specified options. - pub fn open>(opts: &BackupEngineOptions, path: P) -> Result { + pub fn open>(opts: &BackupEngineOptions, + path: P) + -> Result { let path = path.as_ref(); let cpath = match CString::new(path.to_string_lossy().as_bytes()) { Ok(c) => c, @@ -43,17 +49,13 @@ impl BackupEngine { }; let be: *mut ffi::rocksdb_backup_engine_t; - unsafe { - be = ffi_try!(ffi::rocksdb_backup_engine_open(opts.inner, cpath.as_ptr())) - } + unsafe { be = ffi_try!(ffi::rocksdb_backup_engine_open(opts.inner, cpath.as_ptr())) } if be.is_null() { return Err(Error::new("Could not initialize backup engine.".to_owned())); } - Ok(BackupEngine { - inner: be, - }) + Ok(BackupEngine { inner: be }) } fn create_new_backup(&mut self, db: &DB) -> Result<(), Error> { @@ -65,24 +67,49 @@ impl BackupEngine { fn purge_old_backups(&mut self, num_backups_to_keep: usize) -> Result<(), Error> { unsafe { - ffi_try!(ffi::rocksdb_backup_engine_purge_old_backups(self.inner, num_backups_to_keep as uint32_t)); + ffi_try!(ffi::rocksdb_backup_engine_purge_old_backups(self.inner, + num_backups_to_keep as uint32_t)); Ok(()) } } } +impl BackupEngineOptions { + // +} + +impl RestoreOptions { + pub fn set_keep_log_files(&mut self, keep_log_files: bool) { + unsafe { + ffi::rocksdb_restore_options_set_keep_log_files(self.inner, keep_log_files as c_int); + } + } +} + impl Default for BackupEngineOptions { fn default() -> BackupEngineOptions { unsafe { let opts = ffi::rocksdb_options_create(); if opts.is_null() { - panic!("Could not create backup options".to_owned()); + panic!("Could not create RocksDB backup options".to_owned()); } BackupEngineOptions { inner: opts } } } } +impl Default for RestoreOptions { + fn default() -> RestoreOptions { + unsafe { + let opts = ffi::rocksdb_restore_options_create(); + if opts.is_null() { + panic!("Could not create RocksDB restore options".to_owned()); + } + RestoreOptions { inner: opts } + } + } +} + impl Drop for BackupEngine { fn drop(&mut self) { unsafe { @@ -98,3 +125,11 @@ impl Drop for BackupEngineOptions { } } } + +impl Drop for RestoreOptions { + fn drop(&mut self) { + unsafe { + ffi::rocksdb_restore_options_destroy(self.inner); + } + } +} diff --git a/src/comparator.rs b/src/comparator.rs index ae998d9..d75bb7b 100644 --- a/src/comparator.rs +++ b/src/comparator.rs @@ -13,13 +13,13 @@ // limitations under the License. // + +use libc::{c_char, c_int, c_void, size_t}; use std::cmp::Ordering; use std::ffi::CString; use std::mem; use std::slice; -use libc::{c_char, c_int, c_void, size_t}; - pub type CompareFn = fn(&[u8], &[u8]) -> Ordering; pub struct ComparatorCallback { diff --git a/src/rocksdb.rs b/src/db.rs similarity index 99% rename from src/rocksdb.rs rename to src/db.rs index d529868..7dcfb0c 100644 --- a/src/rocksdb.rs +++ b/src/db.rs @@ -13,29 +13,25 @@ // limitations under the License. // + +use {DB, Error, Options, WriteOptions}; +use ffi; + +use libc::{self, c_char, c_int, c_uchar, c_void, size_t}; use std::collections::BTreeMap; use std::ffi::CString; use std::fmt; use std::fs; use std::ops::Deref; -use std::path::{Path}; +use std::path::Path; use std::ptr; use std::slice; use std::str; -use libc::{self, c_char, c_int, c_uchar, c_void, size_t}; - -use {DB, Error, Options, WriteOptions}; -use ffi; - pub fn new_bloom_filter(bits: c_int) -> *mut ffi::rocksdb_filterpolicy_t { unsafe { ffi::rocksdb_filterpolicy_create_bloom(bits) } } -pub fn new_cache(capacity: size_t) -> *mut ffi::rocksdb_cache_t { - unsafe { ffi::rocksdb_cache_create_lru(capacity) } -} - unsafe impl Send for DB {} unsafe impl Sync for DB {} diff --git a/src/rocksdb_options.rs b/src/db_options.rs similarity index 99% rename from src/rocksdb_options.rs rename to src/db_options.rs index f134e4e..6ab3c96 100644 --- a/src/rocksdb_options.rs +++ b/src/db_options.rs @@ -13,17 +13,21 @@ // limitations under the License. // -use std::ffi::{CStr, CString}; -use std::mem; -use libc::{self, c_int, c_uchar, c_uint, c_void, size_t, uint64_t}; - -use {BlockBasedOptions, Options, WriteOptions}; +use {BlockBasedOptions, DBCompactionStyle, DBCompressionType, DBRecoveryMode, Options, + WriteOptions}; use comparator::{self, ComparatorCallback, CompareFn}; use ffi; + +use libc::{self, c_int, c_uchar, c_uint, c_void, size_t, uint64_t}; use merge_operator::{self, MergeFn, MergeOperatorCallback, full_merge_callback, partial_merge_callback}; -use rocksdb::{DBCompactionStyle, DBCompressionType, DBRecoveryMode, new_cache}; +use std::ffi::{CStr, CString}; +use std::mem; + +pub fn new_cache(capacity: size_t) -> *mut ffi::rocksdb_cache_t { + unsafe { ffi::rocksdb_cache_create_lru(capacity) } +} impl Drop for Options { fn drop(&mut self) { diff --git a/src/lib.rs b/src/lib.rs index 9596ff2..5a54a83 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -40,17 +40,17 @@ mod ffi_util; pub mod backup; mod comparator; pub mod merge_operator; -mod rocksdb; -mod rocksdb_options; +mod db; +mod db_options; +pub use db::{DBCompactionStyle, DBCompressionType, DBIterator, DBRecoveryMode, DBVector, + Direction, IteratorMode, Snapshot, WriteBatch, new_bloom_filter}; + +pub use merge_operator::MergeOperands; use std::collections::BTreeMap; use std::error; use std::fmt; -use std::path::{PathBuf}; - -pub use merge_operator::MergeOperands; -pub use rocksdb::{DBCompactionStyle, DBCompressionType, DBIterator, DBRecoveryMode, DBVector, - Direction, IteratorMode, Snapshot, WriteBatch, new_bloom_filter}; +use std::path::PathBuf; /// A RocksDB database. pub struct DB { diff --git a/src/merge_operator.rs b/src/merge_operator.rs index 8980ee0..2f9df3f 100644 --- a/src/merge_operator.rs +++ b/src/merge_operator.rs @@ -53,13 +53,13 @@ //! } //! ``` + +use libc::{self, c_char, c_int, c_void, size_t}; use std::ffi::CString; use std::mem; use std::ptr; use std::slice; -use libc::{self, c_char, c_int, c_void, size_t}; - pub type MergeFn = fn(&[u8], Option<&[u8]>, &mut MergeOperands) -> Vec; pub struct MergeOperatorCallback {