mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-26 21:37:56 +00:00
Storage chains: indexing, renewals and reference counting (#8265)
* Transaction indexing * Tests and fixes * Fixed a comment * Style * Build * Style * Apply suggestions from code review Co-authored-by: cheme <emericchevalier.pro@gmail.com> * Code review suggestions * Add missing impl * Apply suggestions from code review Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com> * impl JoinInput * Don't store empty slices * JoinInput operates on slices Co-authored-by: cheme <emericchevalier.pro@gmail.com> Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
This commit is contained in:
@@ -17,26 +17,41 @@
|
||||
|
||||
//! In-memory implementation of `Database`
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::collections::{HashMap, hash_map::Entry};
|
||||
use crate::{Database, Change, ColumnId, Transaction, error};
|
||||
use parking_lot::RwLock;
|
||||
|
||||
#[derive(Default)]
|
||||
/// This implements `Database` as an in-memory hash map. `commit` is not atomic.
|
||||
pub struct MemDb<H: Clone + Send + Sync + Eq + PartialEq + Default + std::hash::Hash>
|
||||
(RwLock<(HashMap<ColumnId, HashMap<Vec<u8>, Vec<u8>>>, HashMap<H, Vec<u8>>)>);
|
||||
pub struct MemDb(RwLock<HashMap<ColumnId, HashMap<Vec<u8>, (u32, Vec<u8>)>>>);
|
||||
|
||||
impl<H> Database<H> for MemDb<H>
|
||||
where H: Clone + Send + Sync + Eq + PartialEq + Default + std::hash::Hash
|
||||
impl<H> Database<H> for MemDb
|
||||
where H: Clone + AsRef<[u8]>
|
||||
{
|
||||
fn commit(&self, transaction: Transaction<H>) -> error::Result<()> {
|
||||
let mut s = self.0.write();
|
||||
for change in transaction.0.into_iter() {
|
||||
match change {
|
||||
Change::Set(col, key, value) => { s.0.entry(col).or_default().insert(key, value); },
|
||||
Change::Remove(col, key) => { s.0.entry(col).or_default().remove(&key); },
|
||||
Change::Store(hash, preimage) => { s.1.insert(hash, preimage); },
|
||||
Change::Release(hash) => { s.1.remove(&hash); },
|
||||
Change::Set(col, key, value) => { s.entry(col).or_default().insert(key, (1, value)); },
|
||||
Change::Remove(col, key) => { s.entry(col).or_default().remove(&key); },
|
||||
Change::Store(col, hash, value) => {
|
||||
s.entry(col).or_default().entry(hash.as_ref().to_vec())
|
||||
.and_modify(|(c, _)| *c += 1)
|
||||
.or_insert_with(|| (1, value));
|
||||
},
|
||||
Change::Reference(col, hash) => {
|
||||
if let Entry::Occupied(mut entry) = s.entry(col).or_default().entry(hash.as_ref().to_vec()) {
|
||||
entry.get_mut().0 += 1;
|
||||
}
|
||||
}
|
||||
Change::Release(col, hash) => {
|
||||
if let Entry::Occupied(mut entry) = s.entry(col).or_default().entry(hash.as_ref().to_vec()) {
|
||||
entry.get_mut().0 -= 1;
|
||||
if entry.get().0 == 0 {
|
||||
entry.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,18 +60,11 @@ impl<H> Database<H> for MemDb<H>
|
||||
|
||||
fn get(&self, col: ColumnId, key: &[u8]) -> Option<Vec<u8>> {
|
||||
let s = self.0.read();
|
||||
s.0.get(&col).and_then(|c| c.get(key).cloned())
|
||||
}
|
||||
|
||||
fn lookup(&self, hash: &H) -> Option<Vec<u8>> {
|
||||
let s = self.0.read();
|
||||
s.1.get(hash).cloned()
|
||||
s.get(&col).and_then(|c| c.get(key).map(|(_, v)| v.clone()))
|
||||
}
|
||||
}
|
||||
|
||||
impl<H> MemDb<H>
|
||||
where H: Clone + Send + Sync + Eq + PartialEq + Default + std::hash::Hash
|
||||
{
|
||||
impl MemDb {
|
||||
/// Create a new instance
|
||||
pub fn new() -> Self {
|
||||
MemDb::default()
|
||||
@@ -65,7 +73,7 @@ impl<H> MemDb<H>
|
||||
/// Count number of values in a column
|
||||
pub fn count(&self, col: ColumnId) -> usize {
|
||||
let s = self.0.read();
|
||||
s.0.get(&col).map(|c| c.len()).unwrap_or(0)
|
||||
s.get(&col).map(|c| c.len()).unwrap_or(0)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user