diff --git a/client/db/src/parity_db.rs b/client/db/src/parity_db.rs index 7085aa3bf8ccd..313069706f33f 100644 --- a/client/db/src/parity_db.rs +++ b/client/db/src/parity_db.rs @@ -18,7 +18,7 @@ /// A `Database` adapter for parity-db. use sp_database::{Database, Change, ColumnId, Transaction, error::DatabaseError}; -use crate::utils::NUM_COLUMNS; +use crate::utils::{DatabaseType, NUM_COLUMNS}; use crate::columns; struct DbAdapter(parity_db::Db); @@ -32,13 +32,17 @@ fn handle_err(result: parity_db::Result) -> T { } } -/// Wrap RocksDb database into a trait object that implements `sp_database::Database` -pub fn open(path: &std::path::Path) -> parity_db::Result>> { +/// Wrap parity-db database into a trait object that implements `sp_database::Database` +pub fn open(path: &std::path::Path, db_type: DatabaseType) + -> parity_db::Result>> +{ let mut config = parity_db::Options::with_columns(path, NUM_COLUMNS as u8); - let mut state_col = &mut config.columns[columns::STATE as usize]; - state_col.ref_counted = true; - state_col.preimage = true; - state_col.uniform = true; + if db_type == DatabaseType::Full { + let mut state_col = &mut config.columns[columns::STATE as usize]; + state_col.ref_counted = true; + state_col.preimage = true; + state_col.uniform = true; + } let db = parity_db::Db::open(&config)?; Ok(std::sync::Arc::new(DbAdapter(db))) } diff --git a/client/db/src/utils.rs b/client/db/src/utils.rs index 3ad6c421135d0..e999469c18ff0 100644 --- a/client/db/src/utils.rs +++ b/client/db/src/utils.rs @@ -227,31 +227,46 @@ pub fn open_database( // and now open database assuming that it has the latest version let mut db_config = kvdb_rocksdb::DatabaseConfig::with_columns(NUM_COLUMNS); - let state_col_budget = (*cache_size as f64 * 0.9) as usize; - let other_col_budget = (cache_size - state_col_budget) / (NUM_COLUMNS as usize - 1); - let mut memory_budget = std::collections::HashMap::new(); let path = path.to_str() .ok_or_else(|| sp_blockchain::Error::Backend("Invalid database path".into()))?; - for i in 0..NUM_COLUMNS { - if i == crate::columns::STATE { - memory_budget.insert(i, state_col_budget); - } else { - memory_budget.insert(i, other_col_budget); + let mut memory_budget = std::collections::HashMap::new(); + match db_type { + DatabaseType::Full => { + let state_col_budget = (*cache_size as f64 * 0.9) as usize; + let other_col_budget = (cache_size - state_col_budget) / (NUM_COLUMNS as usize - 1); + + for i in 0..NUM_COLUMNS { + if i == crate::columns::STATE { + memory_budget.insert(i, state_col_budget); + } else { + memory_budget.insert(i, other_col_budget); + } + } + log::trace!( + target: "db", + "Open RocksDB database at {}, state column budget: {} MiB, others({}) column cache: {} MiB", + path, + state_col_budget, + NUM_COLUMNS, + other_col_budget, + ); + }, + DatabaseType::Light => { + let col_budget = cache_size / (NUM_COLUMNS as usize); + for i in 0..NUM_COLUMNS { + memory_budget.insert(i, col_budget); + } + log::trace!( + target: "db", + "Open RocksDB light database at {}, column cache: {} MiB", + path, + col_budget, + ); } } - db_config.memory_budget = memory_budget; - log::trace!( - target: "db", - "Open RocksDB database at {}, state column budget: {} MiB, others({}) column cache: {} MiB", - path, - state_col_budget, - NUM_COLUMNS, - other_col_budget, - ); - let db = kvdb_rocksdb::Database::open(&db_config, &path) .map_err(|err| sp_blockchain::Error::Backend(format!("{}", err)))?; sp_database::as_database(db) @@ -262,7 +277,7 @@ pub fn open_database( }, #[cfg(feature = "with-parity-db")] DatabaseSettingsSrc::ParityDb { path } => { - crate::parity_db::open(&path) + crate::parity_db::open(&path, db_type) .map_err(|e| sp_blockchain::Error::Backend(format!("{:?}", e)))? }, #[cfg(not(feature = "with-parity-db"))]