use std::path::Path;
use std::sync::Arc;
use d_engine_core::Error;
use d_engine_core::StorageError;
use rocksdb::Cache;
use rocksdb::ColumnFamilyDescriptor;
use rocksdb::DB;
use super::LOG_CF;
use super::META_CF;
use super::RocksDBStateMachine;
use super::RocksDBStorageEngine;
use super::STATE_MACHINE_CF;
use super::STATE_MACHINE_META_CF;
pub struct RocksDBUnifiedEngine;
impl RocksDBUnifiedEngine {
pub fn open<P: AsRef<Path>>(
path: P
) -> Result<(RocksDBStorageEngine, RocksDBStateMachine), Error> {
let db = Arc::new(Self::open_db(path.as_ref())?);
let storage = RocksDBStorageEngine::from_shared_db(Arc::clone(&db))?;
let sm = RocksDBStateMachine::from_shared_db(Arc::clone(&db))?;
Ok((storage, sm))
}
fn open_db(path: &Path) -> Result<DB, Error> {
let mut db_opts = super::base_db_options();
db_opts.set_max_background_jobs(2);
db_opts.set_db_write_buffer_size(0);
let block_cache = Cache::new_lru_cache(128 * 1024 * 1024);
let log_cf = ColumnFamilyDescriptor::new(LOG_CF, super::log_cf_options(&block_cache));
let meta_cf = ColumnFamilyDescriptor::new(META_CF, super::meta_cf_options(&block_cache));
let sm_cf =
ColumnFamilyDescriptor::new(STATE_MACHINE_CF, super::sm_cf_options(&block_cache));
let sm_meta_cf = ColumnFamilyDescriptor::new(
STATE_MACHINE_META_CF,
super::meta_cf_options(&block_cache),
);
DB::open_cf_descriptors(&db_opts, path, vec![log_cf, meta_cf, sm_cf, sm_meta_cf])
.map_err(|e| StorageError::DbError(e.to_string()).into())
}
}