Commit 38ee726a authored by siddontang's avatar siddontang Committed by GitHub

support log ttl and add test (#33)

parent 714f3272
......@@ -134,6 +134,18 @@ pub enum DBStatisticsHistogramType {
DbSeekMicros = 19,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub enum DBInfoLogLevel {
DBDebug = 0,
DBInfo = 1,
DBWarn = 2,
DBError = 3,
DBFatal = 4,
DBHeader = 5,
DBNumInfoLog = 6,
}
pub fn error_message(ptr: *mut c_char) -> String {
let c_str = unsafe { CStr::from_ptr(ptr) };
let s = format!("{}", c_str.to_string_lossy());
......@@ -223,6 +235,8 @@ extern "C" {
pub fn crocksdb_options_set_max_bytes_for_level_multiplier(options: *mut DBOptions,
mul: c_int);
pub fn crocksdb_options_set_max_log_file_size(options: *mut DBOptions, bytes: size_t);
pub fn crocksdb_options_set_log_file_time_to_roll(options: *mut DBOptions, bytes: size_t);
pub fn crocksdb_options_set_info_log_level(options: *mut DBOptions, level: DBInfoLogLevel);
pub fn crocksdb_options_set_keep_log_file_num(options: *mut DBOptions, num: size_t);
pub fn crocksdb_options_set_max_manifest_file_size(options: *mut DBOptions, bytes: u64);
pub fn crocksdb_options_set_hash_skip_list_rep(options: *mut DBOptions,
......
......@@ -28,7 +28,7 @@ mod compaction_filter;
mod slice_transform;
pub use compaction_filter::CompactionFilter;
pub use librocksdb_sys::{DBCompactionStyle, DBCompressionType, DBRecoveryMode,
pub use librocksdb_sys::{DBCompactionStyle, DBCompressionType, DBRecoveryMode, DBInfoLogLevel,
DBStatisticsTickerType, DBStatisticsHistogramType, new_bloom_filter,
self as crocksdb_ffi};
pub use merge_operator::MergeOperands;
......
......@@ -19,7 +19,7 @@ use comparator::{self, ComparatorCallback, compare_callback};
use crocksdb_ffi::{self, DBOptions, DBWriteOptions, DBBlockBasedTableOptions, DBReadOptions,
DBRestoreOptions, DBCompressionType, DBRecoveryMode, DBSnapshot, DBInstance,
DBFlushOptions, DBStatisticsTickerType, DBStatisticsHistogramType,
DBRateLimiter};
DBRateLimiter, DBInfoLogLevel};
use libc::{self, c_int, size_t, c_void};
use merge_operator::{self, MergeOperatorCallback, full_merge_callback, partial_merge_callback};
use merge_operator::MergeFn;
......@@ -677,6 +677,18 @@ impl Options {
}
}
pub fn set_log_file_time_to_roll(&mut self, ttl: u64) {
unsafe {
crocksdb_ffi::crocksdb_options_set_log_file_time_to_roll(self.inner, ttl as size_t);
}
}
pub fn set_info_log_level(&mut self, level: DBInfoLogLevel) {
unsafe {
crocksdb_ffi::crocksdb_options_set_info_log_level(self.inner, level);
}
}
pub fn set_keep_log_file_num(&mut self, num: u64) {
unsafe {
crocksdb_ffi::crocksdb_options_set_keep_log_file_num(self.inner, num as size_t);
......
......@@ -11,10 +11,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.
use rocksdb::{DB, Options, BlockBasedOptions, WriteOptions, SliceTransform};
use rocksdb::{DB, Options, BlockBasedOptions, WriteOptions, SliceTransform, Writable};
use rocksdb::crocksdb_ffi::{DBStatisticsHistogramType as HistogramType,
DBStatisticsTickerType as TickerType};
DBStatisticsTickerType as TickerType, DBInfoLogLevel as InfoLogLevel};
use std::path::Path;
use std::thread;
use std::time::Duration;
use tempdir::TempDir;
......@@ -138,14 +140,47 @@ fn test_create_info_log() {
let path = TempDir::new("_rust_rocksdb_test_create_info_log_opt").expect("");
let mut opts = Options::new();
opts.create_if_missing(true);
opts.set_info_log_level(InfoLogLevel::DBDebug);
opts.set_log_file_time_to_roll(1);
let info_dir = TempDir::new("_rust_rocksdb_test_info_log_dir").expect("");
opts.create_info_log(info_dir.path().to_str().unwrap()).unwrap();
let db = DB::open(opts, path.path().to_str().unwrap()).unwrap();
assert!(Path::new(info_dir.path().join("LOG").to_str().unwrap()).is_file());
thread::sleep(Duration::from_secs(2));
for i in 0..200 {
db.put(format!("k_{}", i).as_bytes(), b"v").unwrap();
db.flush(true).unwrap();
}
drop(db);
// The LOG must be rolled many times.
let count = info_dir.path().read_dir().unwrap().count();
assert!(count > 1);
}
#[test]
fn test_auto_roll_max_size_info_log() {
let path = TempDir::new("_rust_rocksdb_test_max_size_info_log_opt").expect("");
let mut opts = Options::new();
opts.create_if_missing(true);
opts.set_max_log_file_size(10);
let info_dir = TempDir::new("_rust_rocksdb_max_size_info_log_dir").expect("");
opts.create_info_log(info_dir.path().to_str().unwrap()).unwrap();
let db = DB::open(opts, path.path().to_str().unwrap()).unwrap();
assert!(Path::new(info_dir.path().join("LOG").to_str().unwrap()).is_file());
drop(db);
// The LOG must be rolled many times.
let count = info_dir.path().read_dir().unwrap().count();
assert!(count > 1);
}
#[test]
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment