diff --git a/db/src/storage.rs b/db/src/storage.rs index 4219aef0..786106f6 100644 --- a/db/src/storage.rs +++ b/db/src/storage.rs @@ -26,7 +26,7 @@ pub const COL_BLOCK_HEADERS: u32 = 2; pub const COL_BLOCK_TRANSACTIONS: u32 = 3; pub const COL_TRANSACTIONS: u32 = 4; pub const COL_TRANSACTIONS_META: u32 = 5; -const COL_BLOCK_NUMBERS: u32 = 6; +pub const COL_BLOCK_NUMBERS: u32 = 6; const _COL_RESERVED3: u32 = 7; const _COL_RESERVED4: u32 = 8; const _COL_RESERVED5: u32 = 9; @@ -62,9 +62,18 @@ impl Storage { /// new storage at the selected path /// if no directory exists, it will be created - pub fn new>(path: P) -> Result { + pub fn with_cache>(path: P, total_cache: usize) -> Result { try!(fs::create_dir_all(path.as_ref())); - let cfg = DatabaseConfig::with_columns(Some(COL_COUNT)); + let mut cfg = DatabaseConfig::with_columns(Some(COL_COUNT)); + + cfg.set_cache(Some(COL_TRANSACTIONS), total_cache / 4); + cfg.set_cache(Some(COL_TRANSACTIONS_META), total_cache / 4); + cfg.set_cache(Some(COL_BLOCK_HEADERS), total_cache / 4); + + cfg.set_cache(Some(COL_BLOCK_HASHES), total_cache / 12); + cfg.set_cache(Some(COL_BLOCK_TRANSACTIONS), total_cache / 12); + cfg.set_cache(Some(COL_BLOCK_NUMBERS), total_cache / 12); + let db = try!(Database::open(&cfg, &*path.as_ref().to_string_lossy())); let storage = Storage { @@ -102,6 +111,10 @@ impl Storage { Ok(storage) } + pub fn new>(path: P) -> Result { + Self::with_cache(path, 256) + } + fn read_meta(&self, key: &[u8]) -> Option { self.get(COL_META, key) } diff --git a/pbtc/config.rs b/pbtc/config.rs index 10cb531a..7e20d896 100644 --- a/pbtc/config.rs +++ b/pbtc/config.rs @@ -11,8 +11,11 @@ pub struct Config { pub inbound_connections: u32, pub outbound_connections: u32, pub p2p_threads: usize, + pub db_cache: usize, } +pub const DEFAULT_DB_CACHE: usize = 512; + pub fn parse(matches: &clap::ArgMatches) -> Result { let print_to_console = matches.is_present("printtoconsole"); let magic = match (matches.is_present("testnet"), matches.is_present("regtest")) { @@ -52,6 +55,11 @@ pub fn parse(matches: &clap::ArgMatches) -> Result { None => None, }; + let db_cache = match matches.value_of("db-cache") { + Some(s) => try!(s.parse().map_err(|_| "Invalid cache size - should be number in MB".to_owned())), + None => DEFAULT_DB_CACHE, + }; + let config = Config { print_to_console: print_to_console, magic: magic, @@ -61,6 +69,7 @@ pub fn parse(matches: &clap::ArgMatches) -> Result { inbound_connections: in_connections, outbound_connections: out_connections, p2p_threads: p2p_threads, + db_cache: db_cache, }; Ok(config) diff --git a/pbtc/util.rs b/pbtc/util.rs index 79579999..f1d37e49 100644 --- a/pbtc/util.rs +++ b/pbtc/util.rs @@ -5,9 +5,9 @@ use chain::RepresentH256; use {db, APP_INFO}; use config::Config; -pub fn open_db(_cfg: &Config) -> db::SharedStore { +pub fn open_db(cfg: &Config) -> db::SharedStore { let db_path = app_dir(AppDataType::UserData, &APP_INFO, "db").expect("Failed to get app dir"); - Arc::new(db::Storage::new(db_path).expect("Failed to open database")) + Arc::new(db::Storage::with_cache(db_path, cfg.db_cache).expect("Failed to open database")) } pub fn node_table_path() -> PathBuf {