forked from kaspanet/rusty-kaspa
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Store each relation child in its own key (kaspanet#325)
* Store each relation child in its own key * Database version upgrade logic * Improve staging relations perf * Implement CachedDbSetAccess * Use BlockHasher for children store * Pass children readlock * clippy fix * Use default Debug impl for ReadLock * Address review comments * Remove relations rwlock * Get rid of relations service * Use RefCell instead of Mutex in MemoryRelationsStore and StagingRelationsStore * fix clippy warnings * fix simpa for low delay values * Improve delete_children n StagingRelationsStore * Suggestion for removing the need for `RefCell` (#4) * prep for ref-cell removal * remove ref-cell from StagingRelationsStore * remove ref-cell from MemoryRelationsStore * add comment * update comment * flatten staging (semantic change only) * unify deletions * use correct prefix * bug fix: add to child deletions even if not removed from insertions * remove unused API method * fix user msg * add simpa as test * Revert "Get rid of relations service" This reverts commit e8f61b1. * Revert "Remove relations rwlock" This reverts commit 0a4c5dd. * Remove redundant ChildKey * set access: - cache the new item only if the set entry already exists in the cache - fix bug in delete_bucket where set was emptied in cache but the entry was not removed * bug fix: make sure to propagate key not found err if staging has no data for this hash * clean * Remove redundant comment --------- Co-authored-by: Michael Sutton <[email protected]>
- Loading branch information
1 parent
0ef5d5b
commit b7ababa
Showing
20 changed files
with
619 additions
and
175 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
use kaspa_consensus_core::BlockHashSet; | ||
use kaspa_consensus_core::BlockHasher; | ||
use kaspa_consensus_core::BlockLevel; | ||
use kaspa_database::prelude::BatchDbWriter; | ||
use kaspa_database::prelude::CachedDbSetAccess; | ||
use kaspa_database::prelude::DbWriter; | ||
use kaspa_database::prelude::ReadLock; | ||
use kaspa_database::prelude::StoreError; | ||
use kaspa_database::prelude::StoreResult; | ||
use kaspa_database::prelude::DB; | ||
use kaspa_database::registry::DatabaseStorePrefixes; | ||
use kaspa_hashes::Hash; | ||
use rocksdb::WriteBatch; | ||
use std::sync::Arc; | ||
|
||
pub trait ChildrenStoreReader { | ||
fn get(&self, hash: Hash) -> StoreResult<ReadLock<BlockHashSet>>; | ||
} | ||
|
||
pub trait ChildrenStore { | ||
fn insert_child(&mut self, writer: impl DbWriter, parent: Hash, child: Hash) -> Result<(), StoreError>; | ||
fn delete_child(&mut self, writer: impl DbWriter, parent: Hash, child: Hash) -> Result<(), StoreError>; | ||
} | ||
|
||
/// A DB + cache implementation of `DbChildrenStore` trait, with concurrency support. | ||
#[derive(Clone)] | ||
pub struct DbChildrenStore { | ||
db: Arc<DB>, | ||
access: CachedDbSetAccess<Hash, Hash, BlockHasher, BlockHasher>, | ||
} | ||
|
||
impl DbChildrenStore { | ||
pub fn new(db: Arc<DB>, level: BlockLevel, cache_size: u64) -> Self { | ||
let lvl_bytes = level.to_le_bytes(); | ||
Self { | ||
db: Arc::clone(&db), | ||
access: CachedDbSetAccess::new( | ||
db, | ||
cache_size, | ||
DatabaseStorePrefixes::RelationsChildren.into_iter().chain(lvl_bytes).collect(), | ||
), | ||
} | ||
} | ||
|
||
pub fn with_prefix(db: Arc<DB>, prefix: &[u8], cache_size: u64) -> Self { | ||
let db_prefix = prefix.iter().copied().chain(DatabaseStorePrefixes::RelationsChildren).collect(); | ||
Self { db: Arc::clone(&db), access: CachedDbSetAccess::new(db, cache_size, db_prefix) } | ||
} | ||
|
||
pub fn insert_batch(&self, batch: &mut WriteBatch, parent: Hash, child: Hash) -> Result<(), StoreError> { | ||
self.access.write(BatchDbWriter::new(batch), parent, child)?; | ||
Ok(()) | ||
} | ||
|
||
pub(crate) fn delete_children(&self, mut writer: impl DbWriter, parent: Hash) -> Result<(), StoreError> { | ||
self.access.delete_bucket(&mut writer, parent) | ||
} | ||
|
||
pub(crate) fn prefix(&self) -> &[u8] { | ||
self.access.prefix() | ||
} | ||
} | ||
|
||
impl ChildrenStoreReader for DbChildrenStore { | ||
fn get(&self, parent: Hash) -> StoreResult<ReadLock<BlockHashSet>> { | ||
self.access.read(parent) | ||
} | ||
} | ||
|
||
impl ChildrenStore for DbChildrenStore { | ||
fn insert_child(&mut self, writer: impl DbWriter, parent: Hash, child: Hash) -> Result<(), StoreError> { | ||
self.access.write(writer, parent, child)?; | ||
Ok(()) | ||
} | ||
|
||
fn delete_child(&mut self, writer: impl DbWriter, parent: Hash, child: Hash) -> Result<(), StoreError> { | ||
self.access.delete(writer, parent, child) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.