From 391a724d06bc39fb0e150217a632966ec4a32b3c Mon Sep 17 00:00:00 2001 From: Buffrr Date: Wed, 25 Mar 2026 01:35:24 +0100 Subject: [PATCH] Allow changing db cache size --- client/src/config.rs | 5 +++++ client/src/store/chain.rs | 5 +++-- client/src/store/mod.rs | 9 ++++++--- client/src/store/ptrs.rs | 4 ++-- client/src/store/spaces.rs | 4 ++-- 5 files changed, 18 insertions(+), 9 deletions(-) diff --git a/client/src/config.rs b/client/src/config.rs index ef23d28..0abfb06 100644 --- a/client/src/config.rs +++ b/client/src/config.rs @@ -99,6 +99,10 @@ pub struct Args { /// keeping a buffer of blocks from tip to handle reorgs. #[arg(long, env = "SPACED_ENABLE_PRUNING", default_value = "false")] enable_pruning: bool, + + /// Cache size in bytes for the spacedb database + #[arg(long, env = "SPACED_CACHE_SIZE")] + cache_size: Option, } #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, ValueEnum, Serialize, Deserialize)] @@ -226,6 +230,7 @@ impl Args { &data_dir, args.block_index || args.block_index_full, args.index_node_hashes, + args.cache_size, )?; let anchors_path = match args.skip_anchors { diff --git a/client/src/store/chain.rs b/client/src/store/chain.rs index 5f7aabd..18d6f0b 100644 --- a/client/src/store/chain.rs +++ b/client/src/store/chain.rs @@ -145,18 +145,19 @@ impl Chain { dir: &Path, block_index: bool, index_hashes: bool, + cache_size: Option, ) -> anyhow::Result { let proto_db_path = dir.join("root.sdb"); let nums_db_path = dir.join("nums.sdb"); let initial_num_sync = !nums_db_path.exists(); - let sp_store = SpStore::open(proto_db_path, index_hashes)?; + let sp_store = SpStore::open(proto_db_path, index_hashes, cache_size)?; let sp = SpLiveStore { state: sp_store.begin(&genesis)?, store: sp_store, }; - let num_store = NumStore::open(nums_db_path, index_hashes)?; + let num_store = NumStore::open(nums_db_path, index_hashes, cache_size)?; let num = NumLiveStore { state: num_store.begin(&nums_genesis)?, store: num_store, diff --git a/client/src/store/mod.rs b/client/src/store/mod.rs index 4b83f5f..a90e596 100644 --- a/client/src/store/mod.rs +++ b/client/src/store/mod.rs @@ -7,6 +7,8 @@ use spacedb::tx::{ReadTransaction, WriteTransaction}; use spaces_protocol::bitcoin::OutPoint; use crate::store::chain::ROOT_ANCHORS_COUNT; +const DEFAULT_CACHE_SIZE: usize = 50 * 1024 * 1024; /* 50MB */ + pub mod spaces; pub mod ptrs; pub mod chain; @@ -47,10 +49,11 @@ impl spaces_protocol::hasher::KeyHasher for Sha256 { } } -fn open_db(path_buf: PathBuf, auto_hash_index: bool) -> anyhow::Result> { +fn open_db(path_buf: PathBuf, auto_hash_index: bool, cache_size: Option) -> anyhow::Result> { let config = Configuration::standard() .with_auto_hash_index(auto_hash_index) - .with_hash_index_pruning(Some(ROOT_ANCHORS_COUNT as _)); - + .with_hash_index_pruning(Some(ROOT_ANCHORS_COUNT as _)) + .with_cache_size(cache_size.unwrap_or(DEFAULT_CACHE_SIZE)); + Ok(Database::open_with_config(path_buf.to_str().unwrap(), config)?) } diff --git a/client/src/store/ptrs.rs b/client/src/store/ptrs.rs index e0fdc80..46b9091 100644 --- a/client/src/store/ptrs.rs +++ b/client/src/store/ptrs.rs @@ -55,8 +55,8 @@ pub struct Staged { } impl NumStore { - pub fn open(path: PathBuf, auto_hash_index: bool) -> Result { - let db = open_db(path, auto_hash_index)?; + pub fn open(path: PathBuf, auto_hash_index: bool, cache_size: Option) -> Result { + let db = open_db(path, auto_hash_index, cache_size)?; Ok(Self(db)) } diff --git a/client/src/store/spaces.rs b/client/src/store/spaces.rs index 9a9a366..413c9b5 100644 --- a/client/src/store/spaces.rs +++ b/client/src/store/spaces.rs @@ -58,8 +58,8 @@ pub struct Staged { } impl SpStore { - pub fn open(path: PathBuf, auto_hash_index: bool) -> Result { - let db = open_db(path, auto_hash_index)?; + pub fn open(path: PathBuf, auto_hash_index: bool, cache_size: Option) -> Result { + let db = open_db(path, auto_hash_index, cache_size)?; Ok(Self(db)) }