Skip to content

Commit

Permalink
minor updates
Browse files Browse the repository at this point in the history
1.use get_pinned_cf instead of get_cf
2.reference db in AccountStore
3.make some members of DBStorage private
4.make StorageInstance generic
  • Loading branch information
simonjiao committed Sep 9, 2023
1 parent 86eb873 commit 5a20423
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 99 deletions.
21 changes: 14 additions & 7 deletions account/src/account_schemadb/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub(crate) use global_setting::*;
pub(crate) use private_key::*;
pub(crate) use public_key::*;
pub(crate) use setting::*;
use starcoin_schemadb::SchemaBatch;
use starcoin_schemadb::{db::DBStorage as DB, SchemaBatch};

#[derive(Debug, Ord, PartialOrd, Eq, PartialEq, Hash, Clone, Copy)]
pub(crate) struct AccountAddressWrapper(AccountAddress);
Expand Down Expand Up @@ -43,27 +43,34 @@ impl TryFrom<&[u8]> for AccountAddressWrapper {
#[derive(Clone)]
pub(super) struct AccountStore<S: Schema> {
cache: Arc<GCacheStorage<S::Key, S::Value>>,
db: Arc<DB>,
}

impl<S: Schema> AccountStore<S> {
pub fn new() -> Self {
pub fn new(db: &Arc<DB>) -> Self {
Self {
cache: Arc::new(GCacheStorage::<S::Key, S::Value>::new(None)),
db: Arc::clone(db),
}
}

pub fn get(&self, key: &S::Key) -> Result<Option<S::Value>> {
Ok(self.cache.get_inner(key))
self.cache
.get_inner(key)
.map(|val| Ok(Some(val)))
.unwrap_or_else(|| self.db.get::<S>(key))
}

pub fn put(&self, key: S::Key, value: S::Value) -> Result<()> {
self.cache.put_inner(key, value);
Ok(())
self.db.put::<S>(&key, &value).map(|_| {
self.cache.put_inner(key, value);
})
}

pub fn remove(&self, key: &S::Key) -> Result<()> {
self.cache.remove_inner(key);
Ok(())
self.db.remove::<S>(key).map(|_| {
self.cache.remove_inner(key);
})
}

pub fn put_batch(&self, key: S::Key, value: S::Value, batch: &SchemaBatch) -> Result<()> {
Expand Down
66 changes: 20 additions & 46 deletions account/src/account_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
use crate::account_schemadb::{
AcceptedToken, AcceptedTokens, AccountAddressWrapper, AccountSetting, AccountStore,
EncryptedPrivateKey, GlobalSetting, GlobalSettingKey, GlobalValue, PrivateKey, PublicKey,
PublicKeyWrapper, SettingWrapper, ACCEPTED_TOKEN_PREFIX_NAME,
ENCRYPTED_PRIVATE_KEY_PREFIX_NAME, GLOBAL_PREFIX_NAME, PUBLIC_KEY_PREFIX_NAME,
SETTING_PREFIX_NAME,
SettingWrapper, ACCEPTED_TOKEN_PREFIX_NAME, ENCRYPTED_PRIVATE_KEY_PREFIX_NAME,
GLOBAL_PREFIX_NAME, PUBLIC_KEY_PREFIX_NAME, SETTING_PREFIX_NAME,
};
use anyhow::{Error, Result};
use starcoin_account_api::{AccountPrivateKey, AccountPublicKey, Setting};
Expand Down Expand Up @@ -49,11 +48,11 @@ impl AccountStorage {
pub fn new(db: Arc<DB>) -> Self {
Self {
db: Arc::clone(&db),
setting_store: AccountStore::<AccountSetting>::new(),
private_key_store: AccountStore::<PrivateKey>::new(),
public_key_store: AccountStore::<PublicKey>::new(),
accepted_token_store: AccountStore::<AcceptedToken>::new(),
global_value_store: AccountStore::<GlobalSetting>::new(),
setting_store: AccountStore::<AccountSetting>::new(&db),
private_key_store: AccountStore::<PrivateKey>::new(&db),
public_key_store: AccountStore::<PublicKey>::new(&db),
accepted_token_store: AccountStore::<AcceptedToken>::new(&db),
global_value_store: AccountStore::<GlobalSetting>::new(&db),
}
}

Expand Down Expand Up @@ -113,19 +112,19 @@ impl AccountStorage {
.get(global_setting_key)?
.map(|v| Ok(Some(v)))
.unwrap_or_else(|| {
self.db
.get::<GlobalSetting>(&GlobalSettingKey::AllAddresses)
if global_setting_key != &GlobalSettingKey::AllAddresses {
self.global_value_store.get(&GlobalSettingKey::AllAddresses)
} else {
Ok(None)
}
})
}

fn put_addresses(&self, key: GlobalSettingKey, value: GlobalValue) -> Result<()> {
self.db
.put::<GlobalSetting>(&key, &value)
.and_then(|_| self.global_value_store.put(key, value))
self.global_value_store.put(key, value)
}

fn remove_address(&self, key: &GlobalSettingKey) -> Result<()> {
self.db.remove::<GlobalSetting>(key)?;
self.global_value_store.remove(key)
}

Expand Down Expand Up @@ -159,19 +158,11 @@ impl AccountStorage {
}

fn get_public_key(&self, address: &AccountAddressWrapper) -> Result<Option<AccountPublicKey>> {
self.public_key_store
.get(address)?
.map(|v| Ok(Some(v)))
.unwrap_or_else(|| self.db.get::<PublicKey>(address))
.map(|v| v.map(Into::into))
Ok(self.public_key_store.get(address)?.map(Into::into))
}

fn put_public_key(&self, key: AccountAddress, value: AccountPublicKey) -> Result<()> {
let key: AccountAddressWrapper = key.into();
let value: PublicKeyWrapper = value.into();
self.db
.put::<PublicKey>(&key, &value)
.and_then(|_| self.public_key_store.put(key, value))
self.public_key_store.put(key.into(), value.into())
}

pub fn public_key(&self, address: AccountAddress) -> Result<Option<AccountPublicKey>> {
Expand All @@ -182,10 +173,7 @@ impl AccountStorage {
&self,
address: &AccountAddressWrapper,
) -> Result<Option<EncryptedPrivateKey>> {
self.private_key_store
.get(address)?
.map(|v| Ok(Some(v)))
.unwrap_or_else(|| self.db.get::<PrivateKey>(address))
self.private_key_store.get(address)
}

//fn put_private_key(&self, key: AccountAddress, value: EncryptedPrivateKey) -> Result<()> {
Expand Down Expand Up @@ -239,9 +227,7 @@ impl AccountStorage {
fn put_setting(&self, address: AccountAddress, setting: Setting) -> Result<()> {
let key: AccountAddressWrapper = address.into();
let value: SettingWrapper = setting.into();
self.db
.put::<AccountSetting>(&key, &value)
.and_then(|_| self.setting_store.put(key, value))
self.setting_store.put(key, value)
}

pub fn update_setting(&self, address: AccountAddress, setting: Setting) -> Result<()> {
Expand All @@ -250,13 +236,7 @@ impl AccountStorage {

pub fn load_setting(&self, address: AccountAddress) -> Result<Setting> {
let key: AccountAddressWrapper = address.into();
Ok(self
.setting_store
.get(&key)?
.map(|setting| Ok(Some(setting)))
.unwrap_or_else(|| self.db.get::<AccountSetting>(&key))?
.unwrap_or_default()
.0)
Ok(self.setting_store.get(&key)?.unwrap_or_default().0)
}

pub fn destroy_account(&self, address: AccountAddress) -> Result<()> {
Expand Down Expand Up @@ -298,18 +278,12 @@ impl AccountStorage {

pub fn get_accepted_tokens(&self, address: AccountAddress) -> Result<Vec<TokenCode>> {
let key: AccountAddressWrapper = address.into();
let ts = self
.accepted_token_store
.get(&key)?
.map(|v| Ok(Some(v)))
.unwrap_or_else(|| self.db.get::<AcceptedToken>(&key))?;
let ts = self.accepted_token_store.get(&key)?;
Ok(ts.map(|t| t.0).unwrap_or_default())
}

fn put_accepted_tokens(&self, key: AccountAddressWrapper, value: AcceptedTokens) -> Result<()> {
self.db
.put::<AcceptedToken>(&key, &value)
.and_then(|_| self.accepted_token_store.put(key, value))
self.accepted_token_store.put(key, value)
}

pub fn add_accepted_token(
Expand Down
17 changes: 12 additions & 5 deletions storage/schemadb/src/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,19 @@ const RES_FDS: u64 = 4096;
#[allow(clippy::upper_case_acronyms)]
pub struct DBStorage {
name: String, // for logging
// Todo, make me private to other crates
pub db: DB,
db: DB,
cfs: Vec<ColumnFamilyName>,
// Todo, make me private to other crates
pub metrics: Option<StorageMetrics>,
metrics: Option<StorageMetrics>,
}
impl DBStorage {
pub fn db(&self) -> &DB {
&self.db
}

pub fn metrics(&self) -> Option<&StorageMetrics> {
self.metrics.as_ref()
}

pub fn new<P: AsRef<Path> + Clone>(
db_root_path: P,
rocksdb_config: RocksdbConfig,
Expand Down Expand Up @@ -312,6 +318,7 @@ impl DBStorage {
}
}

// The new Apis
impl DBStorage {
pub fn open(
name: &str,
Expand Down Expand Up @@ -383,7 +390,7 @@ impl DBStorage {
let raw_key = <S::Key as KeyCodec<S>>::encode_key(key)?;
let cf_handle = self.get_cf_handle(S::COLUMN_FAMILY)?;
self.db
.get_cf(cf_handle, raw_key)
.get_pinned_cf(cf_handle, raw_key)
.map_err(Into::into)
.and_then(|raw_value| {
raw_value
Expand Down
40 changes: 20 additions & 20 deletions storage/src/db_storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl ClassicIter for DBStorage {
{
let cf_handle = self.get_cf_handle(prefix_name)?;
Ok(SchemaIterator::new(
self.db
self.db()
.raw_iterator_cf_opt(cf_handle, ReadOptions::default()),
direction,
))
Expand All @@ -68,48 +68,48 @@ impl ClassicIter for DBStorage {

impl InnerStore for DBStorage {
fn get_raw(&self, prefix_name: &str, key: Vec<u8>) -> Result<Option<Vec<u8>>> {
record_metrics("db", prefix_name, "get", self.metrics.as_ref()).call(|| {
record_metrics("db", prefix_name, "get", self.metrics()).call(|| {
let cf_handle = self.get_cf_handle(prefix_name)?;
let result = self.db.get_cf(cf_handle, key.as_slice())?;
let result = self.db().get_cf(cf_handle, key.as_slice())?;
Ok(result)
})
}

fn put_raw(&self, prefix_name: &str, key: Vec<u8>, value: Vec<u8>) -> Result<()> {
if let Some(metrics) = self.metrics.as_ref() {
if let Some(metrics) = self.metrics() {
metrics
.storage_item_bytes
.with_label_values(&[prefix_name])
.observe((key.len() + value.len()) as f64);
}

record_metrics("db", prefix_name, "put", self.metrics.as_ref()).call(|| {
record_metrics("db", prefix_name, "put", self.metrics()).call(|| {
let cf_handle = self.get_cf_handle(prefix_name)?;
self.db
self.db()
.put_cf_opt(cf_handle, &key, &value, &Self::default_write_options())?;
Ok(())
})
}

fn contains_key(&self, prefix_name: &str, key: Vec<u8>) -> Result<bool> {
record_metrics("db", prefix_name, "contains_key", self.metrics.as_ref()).call(|| match self
.get_raw(prefix_name, key)
{
Ok(Some(_)) => Ok(true),
_ => Ok(false),
record_metrics("db", prefix_name, "contains_key", self.metrics()).call(|| {
match self.get_raw(prefix_name, key) {
Ok(Some(_)) => Ok(true),
_ => Ok(false),
}
})
}
fn remove_raw(&self, prefix_name: &str, key: Vec<u8>) -> Result<()> {
record_metrics("db", prefix_name, "remove", self.metrics.as_ref()).call(|| {
record_metrics("db", prefix_name, "remove", self.metrics()).call(|| {
let cf_handle = self.get_cf_handle(prefix_name)?;
self.db.delete_cf(cf_handle, &key)?;
self.db().delete_cf(cf_handle, &key)?;
Ok(())
})
}

/// Writes a group of records wrapped in a WriteBatch.
fn write_batch(&self, prefix_name: &str, batch: WriteBatch) -> Result<()> {
record_metrics("db", prefix_name, "write_batch", self.metrics.as_ref()).call(|| {
record_metrics("db", prefix_name, "write_batch", self.metrics()).call(|| {
self.write_batch_inner(
prefix_name,
batch.rows.as_slice(),
Expand All @@ -127,28 +127,28 @@ impl InnerStore for DBStorage {
}

fn put_sync(&self, prefix_name: &str, key: Vec<u8>, value: Vec<u8>) -> Result<()> {
if let Some(metrics) = self.metrics.as_ref() {
if let Some(metrics) = self.metrics() {
metrics
.storage_item_bytes
.with_label_values(&[prefix_name])
.observe((key.len() + value.len()) as f64);
}

record_metrics("db", prefix_name, "put_sync", self.metrics.as_ref()).call(|| {
record_metrics("db", prefix_name, "put_sync", self.metrics()).call(|| {
let cf_handle = self.get_cf_handle(prefix_name)?;
self.db
self.db()
.put_cf_opt(cf_handle, &key, &value, &Self::sync_write_options())?;
Ok(())
})
}

fn write_batch_sync(&self, prefix_name: &str, batch: WriteBatch) -> Result<()> {
record_metrics("db", prefix_name, "write_batch_sync", self.metrics.as_ref())
record_metrics("db", prefix_name, "write_batch_sync", self.metrics())
.call(|| self.write_batch_inner(prefix_name, batch.rows.as_slice(), true))
}

fn multi_get(&self, prefix_name: &str, keys: Vec<Vec<u8>>) -> Result<Vec<Option<Vec<u8>>>> {
record_metrics("db", prefix_name, "multi_get", self.metrics.as_ref()).call(|| {
record_metrics("db", prefix_name, "multi_get", self.metrics()).call(|| {
let cf_handle = self.get_cf_handle(prefix_name)?;
let cf_handles = iter::repeat(&cf_handle)
.take(keys.len())
Expand All @@ -159,7 +159,7 @@ impl InnerStore for DBStorage {
.map(|(key, handle)| (handle, key.as_slice()))
.collect::<Vec<_>>();

let result = self.db.multi_get_cf(keys_multi);
let result = self.db().multi_get_cf(keys_multi);
let mut res = vec![];
for item in result {
let item = item?;
Expand Down
Loading

0 comments on commit 5a20423

Please sign in to comment.