Skip to content

Commit

Permalink
[storage] Merge Storage Instance (#1970)
Browse files Browse the repository at this point in the history
* [storage] Merge Storage Instance

* rename rocks_store_dir to store_dir
  • Loading branch information
jolestar authored Jun 23, 2024
1 parent 4a433ed commit 62fbbfc
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 63 deletions.
46 changes: 13 additions & 33 deletions crates/rooch-config/src/store_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ use std::str::FromStr;
use std::sync::Arc;

pub const DEFAULT_DB_DIR: &str = "roochdb";
pub const DEFAULT_DB_ROOCH_SUBDIR: &str = "rooch_store";
pub const DEFAULT_DB_MOVEOS_SUBDIR: &str = "moveos_store";
pub const DEFAULT_DB_STORE_SUBDIR: &str = "store";
pub const DEFAULT_DB_INDEXER_SUBDIR: &str = "indexer";

// for Rooch DB instance, doesn't need too much row cache:
Expand Down Expand Up @@ -94,14 +93,10 @@ pub struct StoreConfig {
impl StoreConfig {
pub(crate) fn init(&mut self, base: Arc<BaseConfig>) -> Result<()> {
self.base = Some(base);
let rooch_store_dir = self.get_rooch_store_dir();
let moveos_store_dir = self.get_moveos_store_dir();
let indexer_store_dir = self.get_indexer_store_dir();
if !rooch_store_dir.exists() {
std::fs::create_dir_all(rooch_store_dir.clone())?;
}
if !moveos_store_dir.exists() {
std::fs::create_dir_all(moveos_store_dir.clone())?;
let store_dir = self.get_store_dir();
let indexer_store_dir = self.get_indexer_dir();
if !store_dir.exists() {
std::fs::create_dir_all(store_dir.clone())?;
}
if !indexer_store_dir.exists() {
std::fs::create_dir_all(indexer_store_dir.clone())?;
Expand All @@ -121,26 +116,18 @@ impl StoreConfig {
self.data_dir().join(DEFAULT_DB_DIR)
}

pub fn get_moveos_store_dir(&self) -> PathBuf {
self.get_rooch_db_dir().join(DEFAULT_DB_MOVEOS_SUBDIR)
}

pub fn get_rooch_store_dir(&self) -> PathBuf {
self.get_rooch_db_dir().join(DEFAULT_DB_ROOCH_SUBDIR)
pub fn get_store_dir(&self) -> PathBuf {
self.get_rooch_db_dir().join(DEFAULT_DB_STORE_SUBDIR)
}

pub fn get_indexer_store_dir(&self) -> PathBuf {
pub fn get_indexer_dir(&self) -> PathBuf {
self.get_rooch_db_dir().join(DEFAULT_DB_INDEXER_SUBDIR)
}

pub fn rocksdb_config(&self, is_moveos_db: bool) -> RocksdbConfig {
pub fn rocksdb_config(&self) -> RocksdbConfig {
let default = RocksdbConfig::default();
let mut block_cache_size = default.block_cache_size;
let mut row_cache_size = default.row_cache_size;
if !is_moveos_db {
block_cache_size = DEFAULT_ROCKSDB_BLOCK_CACHE_SIZE;
row_cache_size = DEFAULT_ROCKSDB_ROW_CACHE_SIZE;
}
let block_cache_size = default.block_cache_size;
let row_cache_size = default.row_cache_size;

RocksdbConfig {
max_open_files: self.max_open_files.unwrap_or(default.max_open_files),
Expand All @@ -163,18 +150,11 @@ impl StoreConfig {
}
}

pub fn get_mock_moveos_store_dir(data_dir: &DataDirPath) -> PathBuf {
data_dir
.path()
.join(DEFAULT_DB_DIR)
.join(DEFAULT_DB_MOVEOS_SUBDIR)
}

pub fn get_mock_rooch_store_dir(data_dir: &DataDirPath) -> PathBuf {
pub fn get_mock_store_dir(data_dir: &DataDirPath) -> PathBuf {
data_dir
.path()
.join(DEFAULT_DB_DIR)
.join(DEFAULT_DB_ROOCH_SUBDIR)
.join(DEFAULT_DB_STORE_SUBDIR)
}
}

Expand Down
55 changes: 30 additions & 25 deletions crates/rooch-db/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) RoochNetwork
// SPDX-License-Identifier: Apache-2.0

use std::collections::HashSet;

use anyhow::Result;
use moveos_store::MoveOSStore;
use moveos_types::moveos_std::object::RootObjectEntity;
Expand All @@ -19,31 +21,34 @@ pub struct RoochDB {

impl RoochDB {
pub fn init(config: &StoreConfig) -> Result<Self> {
let (rooch_store_dir, moveos_store_dir, indexer_store_dir) = (
config.get_rooch_store_dir(),
config.get_moveos_store_dir(),
config.get_indexer_store_dir(),
);

//TODO should we merge the moveos_store with rooch_store use one StoreInstance.
let moveos_store =
MoveOSStore::new_with_instance(StoreInstance::new_db_instance(RocksDB::new(
moveos_store_dir,
moveos_store::StoreMeta::get_column_family_names().to_vec(),
config.rocksdb_config(true),
None,
)?))?;

let rooch_store =
RoochStore::new_with_instance(StoreInstance::new_db_instance(RocksDB::new(
rooch_store_dir,
rooch_store::StoreMeta::get_column_family_names().to_vec(),
config.rocksdb_config(false),
None,
)?))?;

let indexer_store = IndexerStore::new(indexer_store_dir.clone())?;
let indexer_reader = IndexerReader::new(indexer_store_dir)?;
let (store_dir, indexer_dir) = (config.get_store_dir(), config.get_indexer_dir());

let mut column_families = moveos_store::StoreMeta::get_column_family_names().to_vec();
column_families.append(&mut rooch_store::StoreMeta::get_column_family_names().to_vec());
//ensure no duplicate column families
{
let mut set = HashSet::new();
column_families.iter().for_each(|cf| {
if !set.insert(cf) {
panic!("Duplicate column family: {}", cf);
}
});
}

let instance = StoreInstance::new_db_instance(RocksDB::new(
store_dir,
column_families,
config.rocksdb_config(),
//TODO collect metrics
None,
)?);

let moveos_store = MoveOSStore::new_with_instance(instance.clone())?;

let rooch_store = RoochStore::new_with_instance(instance)?;

let indexer_store = IndexerStore::new(indexer_dir.clone())?;
let indexer_reader = IndexerReader::new(indexer_dir)?;

Ok(Self {
moveos_store,
Expand Down
2 changes: 1 addition & 1 deletion crates/rooch/src/commands/indexer/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fn init_indexer(

let store_config = opt.store_config();

let indexer_db_path = store_config.get_indexer_store_dir();
let indexer_db_path = store_config.get_indexer_dir();
let indexer_store = IndexerStore::new(indexer_db_path.clone())?;
let indexer_reader = IndexerReader::new(indexer_db_path)?;

Expand Down
4 changes: 2 additions & 2 deletions moveos/moveos-store/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub mod transaction_store;

// pub const DEFAULT_PREFIX_NAME: ColumnFamilyName = "default";
pub const STATE_NODE_PREFIX_NAME: ColumnFamilyName = "state_node";
pub const TRANSACTION_PREFIX_NAME: ColumnFamilyName = "transaction";
pub const TRANSACTION_EXECUTION_INFO_PREFIX_NAME: ColumnFamilyName = "transaction_execution_info";
pub const EVENT_PREFIX_NAME: ColumnFamilyName = "event";
pub const EVENT_HANDLE_PREFIX_NAME: ColumnFamilyName = "event_handle";
pub const CONFIG_STARTUP_INFO_PREFIX_NAME: ColumnFamilyName = "config_startup_info";
Expand All @@ -50,7 +50,7 @@ pub const CONFIG_GENESIS_PREFIX_NAME: ColumnFamilyName = "config_genesis";
static VEC_PREFIX_NAME: Lazy<Vec<ColumnFamilyName>> = Lazy::new(|| {
vec![
STATE_NODE_PREFIX_NAME,
TRANSACTION_PREFIX_NAME,
TRANSACTION_EXECUTION_INFO_PREFIX_NAME,
EVENT_PREFIX_NAME,
EVENT_HANDLE_PREFIX_NAME,
CONFIG_STARTUP_INFO_PREFIX_NAME,
Expand Down
4 changes: 2 additions & 2 deletions moveos/moveos-store/src/transaction_store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ use moveos_types::h256::H256;
use moveos_types::transaction::TransactionExecutionInfo;
use raw_store::CodecKVStore;

use crate::TRANSACTION_PREFIX_NAME;
use crate::TRANSACTION_EXECUTION_INFO_PREFIX_NAME;
use raw_store::derive_store;

derive_store!(
TransactionDBStore,
H256,
TransactionExecutionInfo,
TRANSACTION_PREFIX_NAME
TRANSACTION_EXECUTION_INFO_PREFIX_NAME
);

pub trait TransactionStore {
Expand Down

0 comments on commit 62fbbfc

Please sign in to comment.