Skip to content

Commit

Permalink
fix compile errors
Browse files Browse the repository at this point in the history
  • Loading branch information
simonjiao committed Sep 6, 2023
1 parent 20a7e21 commit 86eb873
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 59 deletions.
2 changes: 1 addition & 1 deletion account/src/account_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use starcoin_account_api::{AccountPrivateKey, AccountPublicKey, Setting};
use starcoin_config::{temp_dir, RocksdbConfig};
use starcoin_crypto::ValidCryptoMaterial;
use starcoin_decrypt::{decrypt, encrypt};
use starcoin_schemadb::{SchemaBatch, DB};
use starcoin_schemadb::{db::DBStorage as DB, SchemaBatch};
use starcoin_types::{account_address::AccountAddress, account_config::token_code::TokenCode};
use std::{convert::TryFrom, path::Path, sync::Arc};

Expand Down
9 changes: 7 additions & 2 deletions cmd/db-exporter/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ pub fn export<W: std::io::Write>(
schema: DbSchema,
) -> anyhow::Result<()> {
let db_storage = DBStorage::open_with_cfs(
"starcoindb",
db,
StorageVersion::current_version()
.get_column_family_names()
Expand All @@ -88,7 +89,7 @@ pub fn export<W: std::io::Write>(
Default::default(),
None,
)?;
let mut iter = db_storage.iter::<Vec<u8>, Vec<u8>>(schema.to_string().as_str())?;
let mut iter = db_storage.iter_raw::<Vec<u8>, Vec<u8>>(schema.to_string().as_str())?;
iter.seek_to_first();
let key_codec = schema.get_key_codec();
let value_codec = schema.get_value_codec();
Expand Down Expand Up @@ -502,6 +503,7 @@ async fn main() -> anyhow::Result<()> {
}
Cmd::Checkkey(option) => {
let db = DBStorage::open_with_cfs(
"starcoindb",
option.db_path.display().to_string().as_str(),
StorageVersion::current_version()
.get_column_family_names()
Expand All @@ -511,7 +513,7 @@ async fn main() -> anyhow::Result<()> {
None,
)?;

let result = db.get(option.cf_name.as_str(), option.block_hash.to_vec())?;
let result = db.get_raw(option.cf_name.as_str(), option.block_hash.to_vec())?;
if result.is_some() {
println!("{} block_hash {} exist", option.cf_name, option.block_hash);
} else {
Expand Down Expand Up @@ -620,6 +622,7 @@ pub fn export_block_range(
) -> anyhow::Result<()> {
let net = ChainNetwork::new_builtin(network);
let db_storage = DBStorage::open_with_cfs(
"starcoindb",
from_dir.join("starcoindb/db/starcoindb"),
StorageVersion::current_version()
.get_column_family_names()
Expand Down Expand Up @@ -1277,6 +1280,7 @@ pub fn export_snapshot(
let start_time = SystemTime::now();
let net = ChainNetwork::new_builtin(network);
let db_storage = DBStorage::open_with_cfs(
"starcoindb",
from_dir.join("starcoindb/db/starcoindb"),
StorageVersion::current_version()
.get_column_family_names()
Expand Down Expand Up @@ -1825,6 +1829,7 @@ pub fn export_resource(
fields: &[String],
) -> anyhow::Result<()> {
let db_storage = DBStorage::open_with_cfs(
"starcoindb",
db,
StorageVersion::current_version()
.get_column_family_names()
Expand Down
1 change: 1 addition & 0 deletions cmd/resource-exporter/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub fn export(
fields: &[String],
) -> anyhow::Result<()> {
let db_storage = DBStorage::open_with_cfs(
"starcoindb",
db,
StorageVersion::current_version()
.get_column_family_names()
Expand Down
8 changes: 4 additions & 4 deletions storage/src/cache_storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ impl<K: Hash + Eq + Default, V: Default> Default for GCacheStorage<K, V> {
}

impl InnerStore for CacheStorage {
fn get(&self, prefix_name: &str, key: Vec<u8>) -> Result<Option<Vec<u8>>> {
fn get_raw(&self, prefix_name: &str, key: Vec<u8>) -> Result<Option<Vec<u8>>> {
let composed_key = compose_key(Some(prefix_name), key);
record_metrics("cache", prefix_name, "get", self.metrics.as_ref())
.call(|| Ok(self.get_inner(&composed_key)))
}

fn put(&self, prefix_name: &str, key: Vec<u8>, value: Vec<u8>) -> Result<()> {
fn put_raw(&self, prefix_name: &str, key: Vec<u8>, value: Vec<u8>) -> Result<()> {
// remove record_metrics for performance
// record_metrics add in write_batch to reduce Instant::now system call
let composed_key = compose_key(Some(prefix_name), key);
Expand All @@ -67,7 +67,7 @@ impl InnerStore for CacheStorage {
record_metrics("cache", prefix_name, "contains_key", self.metrics.as_ref())
.call(|| Ok(self.contains_key_inner(&composed_key)))
}
fn remove(&self, prefix_name: &str, key: Vec<u8>) -> Result<()> {
fn remove_raw(&self, prefix_name: &str, key: Vec<u8>) -> Result<()> {
// remove record_metrics for performance
// record_metrics add in write_batch to reduce Instant::now system call
let composed_key = compose_key(Some(prefix_name), key);
Expand Down Expand Up @@ -107,7 +107,7 @@ impl InnerStore for CacheStorage {
}

fn put_sync(&self, prefix_name: &str, key: Vec<u8>, value: Vec<u8>) -> Result<()> {
self.put(prefix_name, key, value)
self.put_raw(prefix_name, key, value)
}

fn write_batch_sync(&self, prefix_name: &str, batch: WriteBatch) -> Result<()> {
Expand Down
19 changes: 11 additions & 8 deletions storage/src/db_storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ pub trait ClassicIter {
direction: ScanDirection,
) -> Result<SchemaIterator<K, V>>;

fn iter<K: KeyCodec, V: ValueCodec>(&self, prefix_name: &str) -> Result<SchemaIterator<K, V>>;
fn rev_iter<K: KeyCodec, V: ValueCodec>(
fn iter_raw<K: KeyCodec, V: ValueCodec>(
&self,
prefix_name: &str,
) -> Result<SchemaIterator<K, V>>;
fn rev_iter_raw<K: KeyCodec, V: ValueCodec>(
&self,
prefix_name: &str,
) -> Result<SchemaIterator<K, V>>;
Expand All @@ -45,7 +48,7 @@ impl ClassicIter for DBStorage {
))
}
/// Returns a forward [`SchemaIterator`] on a certain schema.
fn iter<K, V>(&self, prefix_name: &str) -> Result<SchemaIterator<K, V>>
fn iter_raw<K, V>(&self, prefix_name: &str) -> Result<SchemaIterator<K, V>>
where
K: KeyCodec,
V: ValueCodec,
Expand All @@ -54,7 +57,7 @@ impl ClassicIter for DBStorage {
}

/// Returns a backward [`SchemaIterator`] on a certain schema.
fn rev_iter<K, V>(&self, prefix_name: &str) -> Result<SchemaIterator<K, V>>
fn rev_iter_raw<K, V>(&self, prefix_name: &str) -> Result<SchemaIterator<K, V>>
where
K: KeyCodec,
V: ValueCodec,
Expand All @@ -64,15 +67,15 @@ impl ClassicIter for DBStorage {
}

impl InnerStore for DBStorage {
fn get(&self, prefix_name: &str, key: Vec<u8>) -> Result<Option<Vec<u8>>> {
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(|| {
let cf_handle = self.get_cf_handle(prefix_name)?;
let result = self.db.get_cf(cf_handle, key.as_slice())?;
Ok(result)
})
}

fn put(&self, prefix_name: &str, key: Vec<u8>, value: Vec<u8>) -> Result<()> {
fn put_raw(&self, prefix_name: &str, key: Vec<u8>, value: Vec<u8>) -> Result<()> {
if let Some(metrics) = self.metrics.as_ref() {
metrics
.storage_item_bytes
Expand All @@ -90,13 +93,13 @@ impl InnerStore for DBStorage {

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(prefix_name, key)
.get_raw(prefix_name, key)
{
Ok(Some(_)) => Ok(true),
_ => Ok(false),
})
}
fn remove(&self, prefix_name: &str, key: Vec<u8>) -> Result<()> {
fn remove_raw(&self, prefix_name: &str, key: Vec<u8>) -> Result<()> {
record_metrics("db", prefix_name, "remove", self.metrics.as_ref()).call(|| {
let cf_handle = self.get_cf_handle(prefix_name)?;
self.db.delete_cf(cf_handle, &key)?;
Expand Down
48 changes: 24 additions & 24 deletions storage/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ pub trait KVStore: Send + Sync {
}

pub trait InnerStore: Send + Sync {
fn get(&self, prefix_name: &str, key: Vec<u8>) -> Result<Option<Vec<u8>>>;
fn put(&self, prefix_name: &str, key: Vec<u8>, value: Vec<u8>) -> Result<()>;
fn get_raw(&self, prefix_name: &str, key: Vec<u8>) -> Result<Option<Vec<u8>>>;
fn put_raw(&self, prefix_name: &str, key: Vec<u8>, value: Vec<u8>) -> Result<()>;
fn contains_key(&self, prefix_name: &str, key: Vec<u8>) -> Result<bool>;
fn remove(&self, prefix_name: &str, key: Vec<u8>) -> Result<()>;
fn remove_raw(&self, prefix_name: &str, key: Vec<u8>) -> Result<()>;
fn write_batch(&self, prefix_name: &str, batch: WriteBatch) -> Result<()>;
fn get_len(&self) -> Result<u64>;
fn keys(&self) -> Result<Vec<Vec<u8>>>;
Expand Down Expand Up @@ -120,17 +120,17 @@ impl StorageInstance {
}

impl InnerStore for StorageInstance {
fn get(&self, prefix_name: &str, key: Vec<u8>) -> Result<Option<Vec<u8>>> {
fn get_raw(&self, prefix_name: &str, key: Vec<u8>) -> Result<Option<Vec<u8>>> {
match self {
StorageInstance::CACHE { cache } => cache.get(prefix_name, key),
StorageInstance::DB { db } => db.get(prefix_name, key),
StorageInstance::CACHE { cache } => cache.get_raw(prefix_name, key),
StorageInstance::DB { db } => db.get_raw(prefix_name, key),
StorageInstance::CacheAndDb { cache, db } => {
// first get from cache
// if from cache get non-existent, query from db
if let Ok(Some(value)) = cache.get(prefix_name, key.clone()) {
if let Ok(Some(value)) = cache.get_raw(prefix_name, key.clone()) {
Ok(Some(value))
} else {
match db.get(prefix_name, key)? {
match db.get_raw(prefix_name, key)? {
Some(value) => {
// cache.put_obj(prefix_name, key, CacheObject::Value(value.clone()))?;
Ok(Some(value))
Expand All @@ -146,13 +146,13 @@ impl InnerStore for StorageInstance {
}
}

fn put(&self, prefix_name: &str, key: Vec<u8>, value: Vec<u8>) -> Result<()> {
fn put_raw(&self, prefix_name: &str, key: Vec<u8>, value: Vec<u8>) -> Result<()> {
match self {
StorageInstance::CACHE { cache } => cache.put(prefix_name, key, value),
StorageInstance::DB { db } => db.put(prefix_name, key, value),
StorageInstance::CACHE { cache } => cache.put_raw(prefix_name, key, value),
StorageInstance::DB { db } => db.put_raw(prefix_name, key, value),
StorageInstance::CacheAndDb { cache, db } => db
.put(prefix_name, key.clone(), value.clone())
.and_then(|_| cache.put(prefix_name, key, value)),
.put_raw(prefix_name, key.clone(), value.clone())
.and_then(|_| cache.put_raw(prefix_name, key, value)),
}
}

Expand All @@ -169,13 +169,13 @@ impl InnerStore for StorageInstance {
}
}

fn remove(&self, prefix_name: &str, key: Vec<u8>) -> Result<()> {
fn remove_raw(&self, prefix_name: &str, key: Vec<u8>) -> Result<()> {
match self {
StorageInstance::CACHE { cache } => cache.remove(prefix_name, key),
StorageInstance::DB { db } => db.remove(prefix_name, key),
StorageInstance::CACHE { cache } => cache.remove_raw(prefix_name, key),
StorageInstance::DB { db } => db.remove_raw(prefix_name, key),
StorageInstance::CacheAndDb { cache, db } => {
match db.remove(prefix_name, key.clone()) {
Ok(_) => cache.remove(prefix_name, key),
match db.remove_raw(prefix_name, key.clone()) {
Ok(_) => cache.remove_raw(prefix_name, key),
_ => bail!("db storage remove error."),
}
}
Expand Down Expand Up @@ -212,11 +212,11 @@ impl InnerStore for StorageInstance {

fn put_sync(&self, prefix_name: &str, key: Vec<u8>, value: Vec<u8>) -> Result<()> {
match self {
StorageInstance::CACHE { cache } => cache.put(prefix_name, key, value),
StorageInstance::CACHE { cache } => cache.put_raw(prefix_name, key, value),
StorageInstance::DB { db } => db.put_sync(prefix_name, key, value),
StorageInstance::CacheAndDb { cache, db } => db
.put_sync(prefix_name, key.clone(), value.clone())
.and_then(|_| cache.put(prefix_name, key, value)),
.and_then(|_| cache.put_raw(prefix_name, key, value)),
}
}

Expand Down Expand Up @@ -314,23 +314,23 @@ where
CF: ColumnFamily,
{
fn get(&self, key: &[u8]) -> Result<Option<Vec<u8>>> {
self.instance.get(self.prefix_name, key.to_vec())
self.instance.get_raw(self.prefix_name, key.to_vec())
}

fn multiple_get(&self, keys: Vec<Vec<u8>>) -> Result<Vec<Option<Vec<u8>>>> {
self.instance.multi_get(self.prefix_name, keys)
}

fn put(&self, key: Vec<u8>, value: Vec<u8>) -> Result<()> {
self.instance.put(self.prefix_name, key, value)
self.instance.put_raw(self.prefix_name, key, value)
}

fn contains_key(&self, key: Vec<u8>) -> Result<bool> {
self.instance.contains_key(self.prefix_name, key)
}

fn remove(&self, key: Vec<u8>) -> Result<()> {
self.instance.remove(self.prefix_name, key)
self.instance.remove_raw(self.prefix_name, key)
}

fn write_batch(&self, batch: WriteBatch) -> Result<()> {
Expand Down Expand Up @@ -621,6 +621,6 @@ where
.storage()
.db()
.ok_or_else(|| format_err!("Only support scan on db storage instance"))?;
db.iter::<K, V>(self.get_store().prefix_name)
db.iter_raw::<K, V>(self.get_store().prefix_name)
}
}
10 changes: 5 additions & 5 deletions storage/src/tests/test_batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ fn test_db_batch() {
assert_eq!(
RichTransactionInfo::decode_value(
&db_storage
.get(DEFAULT_PREFIX_NAME, id.to_vec())
.get_raw(DEFAULT_PREFIX_NAME, id.to_vec())
.unwrap()
.unwrap()
)
Expand All @@ -66,7 +66,7 @@ fn test_db_batch() {
assert_eq!(
RichTransactionInfo::decode_value(
&db_storage
.get(DEFAULT_PREFIX_NAME, id2.to_vec())
.get_raw(DEFAULT_PREFIX_NAME, id2.to_vec())
.unwrap()
.unwrap()
)
Expand Down Expand Up @@ -115,7 +115,7 @@ fn test_cache_batch() {
assert_eq!(
RichTransactionInfo::decode_value(
&cache_storage
.get(DEFAULT_PREFIX_NAME, id.to_vec())
.get_raw(DEFAULT_PREFIX_NAME, id.to_vec())
.unwrap()
.unwrap()
)
Expand All @@ -125,7 +125,7 @@ fn test_cache_batch() {
assert_eq!(
RichTransactionInfo::decode_value(
&cache_storage
.get(DEFAULT_PREFIX_NAME, id2.to_vec())
.get_raw(DEFAULT_PREFIX_NAME, id2.to_vec())
.unwrap()
.unwrap()
)
Expand All @@ -145,7 +145,7 @@ fn test_batch_comm() {
write_batch.delete(key.to_vec()).unwrap();
let result = db.write_batch(DEFAULT_PREFIX_NAME, write_batch.clone());
assert!(result.is_ok());
let result = db.get(DEFAULT_PREFIX_NAME, key.to_vec()).unwrap();
let result = db.get_raw(DEFAULT_PREFIX_NAME, key.to_vec()).unwrap();
assert_eq!(result, None);
let mut key_vec = vec![];
write_batch.clone().clear().unwrap();
Expand Down
Loading

0 comments on commit 86eb873

Please sign in to comment.