From 86eb8737d09a336b627d0220cb1b22d70c4223ab Mon Sep 17 00:00:00 2001 From: simonjiao Date: Wed, 6 Sep 2023 21:24:32 +0800 Subject: [PATCH] fix compile errors --- account/src/account_storage.rs | 2 +- cmd/db-exporter/src/main.rs | 9 ++++-- cmd/resource-exporter/src/main.rs | 1 + storage/src/cache_storage/mod.rs | 8 +++--- storage/src/db_storage/mod.rs | 19 ++++++------ storage/src/storage.rs | 48 +++++++++++++++---------------- storage/src/tests/test_batch.rs | 10 +++---- storage/src/tests/test_storage.rs | 30 +++++++++---------- 8 files changed, 68 insertions(+), 59 deletions(-) diff --git a/account/src/account_storage.rs b/account/src/account_storage.rs index 4eef67e7fe..0db69b1e57 100644 --- a/account/src/account_storage.rs +++ b/account/src/account_storage.rs @@ -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}; diff --git a/cmd/db-exporter/src/main.rs b/cmd/db-exporter/src/main.rs index 91488db3da..05d3288d24 100644 --- a/cmd/db-exporter/src/main.rs +++ b/cmd/db-exporter/src/main.rs @@ -80,6 +80,7 @@ pub fn export( schema: DbSchema, ) -> anyhow::Result<()> { let db_storage = DBStorage::open_with_cfs( + "starcoindb", db, StorageVersion::current_version() .get_column_family_names() @@ -88,7 +89,7 @@ pub fn export( Default::default(), None, )?; - let mut iter = db_storage.iter::, Vec>(schema.to_string().as_str())?; + let mut iter = db_storage.iter_raw::, Vec>(schema.to_string().as_str())?; iter.seek_to_first(); let key_codec = schema.get_key_codec(); let value_codec = schema.get_value_codec(); @@ -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() @@ -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 { @@ -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() @@ -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() @@ -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() diff --git a/cmd/resource-exporter/src/main.rs b/cmd/resource-exporter/src/main.rs index c9a08012ce..7036f50885 100644 --- a/cmd/resource-exporter/src/main.rs +++ b/cmd/resource-exporter/src/main.rs @@ -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() diff --git a/storage/src/cache_storage/mod.rs b/storage/src/cache_storage/mod.rs index a9ad413a93..dd4d361794 100644 --- a/storage/src/cache_storage/mod.rs +++ b/storage/src/cache_storage/mod.rs @@ -45,13 +45,13 @@ impl Default for GCacheStorage { } impl InnerStore for CacheStorage { - fn get(&self, prefix_name: &str, key: Vec) -> Result>> { + fn get_raw(&self, prefix_name: &str, key: Vec) -> Result>> { 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, value: Vec) -> Result<()> { + fn put_raw(&self, prefix_name: &str, key: Vec, value: Vec) -> 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); @@ -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) -> Result<()> { + fn remove_raw(&self, prefix_name: &str, key: Vec) -> 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); @@ -107,7 +107,7 @@ impl InnerStore for CacheStorage { } fn put_sync(&self, prefix_name: &str, key: Vec, value: Vec) -> 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<()> { diff --git a/storage/src/db_storage/mod.rs b/storage/src/db_storage/mod.rs index 38b8654574..79a0fcc5dc 100644 --- a/storage/src/db_storage/mod.rs +++ b/storage/src/db_storage/mod.rs @@ -20,8 +20,11 @@ pub trait ClassicIter { direction: ScanDirection, ) -> Result>; - fn iter(&self, prefix_name: &str) -> Result>; - fn rev_iter( + fn iter_raw( + &self, + prefix_name: &str, + ) -> Result>; + fn rev_iter_raw( &self, prefix_name: &str, ) -> Result>; @@ -45,7 +48,7 @@ impl ClassicIter for DBStorage { )) } /// Returns a forward [`SchemaIterator`] on a certain schema. - fn iter(&self, prefix_name: &str) -> Result> + fn iter_raw(&self, prefix_name: &str) -> Result> where K: KeyCodec, V: ValueCodec, @@ -54,7 +57,7 @@ impl ClassicIter for DBStorage { } /// Returns a backward [`SchemaIterator`] on a certain schema. - fn rev_iter(&self, prefix_name: &str) -> Result> + fn rev_iter_raw(&self, prefix_name: &str) -> Result> where K: KeyCodec, V: ValueCodec, @@ -64,7 +67,7 @@ impl ClassicIter for DBStorage { } impl InnerStore for DBStorage { - fn get(&self, prefix_name: &str, key: Vec) -> Result>> { + fn get_raw(&self, prefix_name: &str, key: Vec) -> Result>> { 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())?; @@ -72,7 +75,7 @@ impl InnerStore for DBStorage { }) } - fn put(&self, prefix_name: &str, key: Vec, value: Vec) -> Result<()> { + fn put_raw(&self, prefix_name: &str, key: Vec, value: Vec) -> Result<()> { if let Some(metrics) = self.metrics.as_ref() { metrics .storage_item_bytes @@ -90,13 +93,13 @@ impl InnerStore for DBStorage { fn contains_key(&self, prefix_name: &str, key: Vec) -> Result { 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) -> Result<()> { + fn remove_raw(&self, prefix_name: &str, key: Vec) -> 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)?; diff --git a/storage/src/storage.rs b/storage/src/storage.rs index 24ca5b4bfb..9b929faad8 100644 --- a/storage/src/storage.rs +++ b/storage/src/storage.rs @@ -33,10 +33,10 @@ pub trait KVStore: Send + Sync { } pub trait InnerStore: Send + Sync { - fn get(&self, prefix_name: &str, key: Vec) -> Result>>; - fn put(&self, prefix_name: &str, key: Vec, value: Vec) -> Result<()>; + fn get_raw(&self, prefix_name: &str, key: Vec) -> Result>>; + fn put_raw(&self, prefix_name: &str, key: Vec, value: Vec) -> Result<()>; fn contains_key(&self, prefix_name: &str, key: Vec) -> Result; - fn remove(&self, prefix_name: &str, key: Vec) -> Result<()>; + fn remove_raw(&self, prefix_name: &str, key: Vec) -> Result<()>; fn write_batch(&self, prefix_name: &str, batch: WriteBatch) -> Result<()>; fn get_len(&self) -> Result; fn keys(&self) -> Result>>; @@ -120,17 +120,17 @@ impl StorageInstance { } impl InnerStore for StorageInstance { - fn get(&self, prefix_name: &str, key: Vec) -> Result>> { + fn get_raw(&self, prefix_name: &str, key: Vec) -> Result>> { 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)) @@ -146,13 +146,13 @@ impl InnerStore for StorageInstance { } } - fn put(&self, prefix_name: &str, key: Vec, value: Vec) -> Result<()> { + fn put_raw(&self, prefix_name: &str, key: Vec, value: Vec) -> 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)), } } @@ -169,13 +169,13 @@ impl InnerStore for StorageInstance { } } - fn remove(&self, prefix_name: &str, key: Vec) -> Result<()> { + fn remove_raw(&self, prefix_name: &str, key: Vec) -> 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."), } } @@ -212,11 +212,11 @@ impl InnerStore for StorageInstance { fn put_sync(&self, prefix_name: &str, key: Vec, value: Vec) -> 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)), } } @@ -314,7 +314,7 @@ where CF: ColumnFamily, { fn get(&self, key: &[u8]) -> Result>> { - 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>) -> Result>>> { @@ -322,7 +322,7 @@ where } fn put(&self, key: Vec, value: Vec) -> Result<()> { - self.instance.put(self.prefix_name, key, value) + self.instance.put_raw(self.prefix_name, key, value) } fn contains_key(&self, key: Vec) -> Result { @@ -330,7 +330,7 @@ where } fn remove(&self, key: Vec) -> Result<()> { - self.instance.remove(self.prefix_name, key) + self.instance.remove_raw(self.prefix_name, key) } fn write_batch(&self, batch: WriteBatch) -> Result<()> { @@ -621,6 +621,6 @@ where .storage() .db() .ok_or_else(|| format_err!("Only support scan on db storage instance"))?; - db.iter::(self.get_store().prefix_name) + db.iter_raw::(self.get_store().prefix_name) } } diff --git a/storage/src/tests/test_batch.rs b/storage/src/tests/test_batch.rs index caeaaf5acf..5b4ebcfa5b 100644 --- a/storage/src/tests/test_batch.rs +++ b/storage/src/tests/test_batch.rs @@ -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() ) @@ -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() ) @@ -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() ) @@ -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() ) @@ -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(); diff --git a/storage/src/tests/test_storage.rs b/storage/src/tests/test_storage.rs index d633a8feaf..b1a40ca63b 100644 --- a/storage/src/tests/test_storage.rs +++ b/storage/src/tests/test_storage.rs @@ -36,17 +36,17 @@ fn test_reopen() { let value = HashValue::zero(); { let db = DBStorage::new(tmpdir.path(), RocksdbConfig::default(), None).unwrap(); - db.put(DEFAULT_PREFIX_NAME, key.to_vec(), value.to_vec()) + db.put_raw(DEFAULT_PREFIX_NAME, key.to_vec(), value.to_vec()) .unwrap(); assert_eq!( - db.get(DEFAULT_PREFIX_NAME, key.to_vec()).unwrap(), + db.get_raw(DEFAULT_PREFIX_NAME, key.to_vec()).unwrap(), Some(value.to_vec()) ); } { let db = DBStorage::new(tmpdir.path(), RocksdbConfig::default(), None).unwrap(); assert_eq!( - db.get(DEFAULT_PREFIX_NAME, key.to_vec()).unwrap(), + db.get_raw(DEFAULT_PREFIX_NAME, key.to_vec()).unwrap(), Some(value.to_vec()) ); } @@ -58,7 +58,7 @@ fn test_open_read_only() { let db = DBStorage::new(tmpdir.path(), RocksdbConfig::default(), None).unwrap(); let key = HashValue::random(); let value = HashValue::zero(); - let result = db.put(DEFAULT_PREFIX_NAME, key.to_vec(), value.to_vec()); + let result = db.put_raw(DEFAULT_PREFIX_NAME, key.to_vec(), value.to_vec()); assert!(result.is_ok()); let path = tmpdir.as_ref().join("starcoindb"); let db = DBStorage::open_with_cfs( @@ -72,9 +72,9 @@ fn test_open_read_only() { None, ) .unwrap(); - let result = db.put(DEFAULT_PREFIX_NAME, key.to_vec(), value.to_vec()); + let result = db.put_raw(DEFAULT_PREFIX_NAME, key.to_vec(), value.to_vec()); assert!(result.is_err()); - 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, Some(value.to_vec())); } @@ -177,14 +177,14 @@ fn test_two_level_storage() { assert_eq!(transaction_info1, transaction_info2.unwrap()); //verfiy cache storage let value3 = cache_storage - .get(TRANSACTION_INFO_PREFIX_NAME_V2, id.to_vec()) + .get_raw(TRANSACTION_INFO_PREFIX_NAME_V2, id.to_vec()) .unwrap() .unwrap(); let transaction_info3 = RichTransactionInfo::decode_value(&value3).unwrap(); assert_eq!(transaction_info3, transaction_info1); // // verify db storage let value4 = db_storage - .get(TRANSACTION_INFO_PREFIX_NAME_V2, id.to_vec()) + .get_raw(TRANSACTION_INFO_PREFIX_NAME_V2, id.to_vec()) .unwrap() .unwrap(); let transaction_info4 = RichTransactionInfo::decode_value(&value4).unwrap(); @@ -195,11 +195,11 @@ fn test_two_level_storage() { assert_eq!(transaction_info5, None); // verify cache storage is null let value6 = cache_storage - .get(TRANSACTION_INFO_PREFIX_NAME_V2, id.to_vec()) + .get_raw(TRANSACTION_INFO_PREFIX_NAME_V2, id.to_vec()) .unwrap(); assert!(value6.is_none()); let value7 = db_storage - .get(TRANSACTION_INFO_PREFIX_NAME_V2, id.to_vec()) + .get_raw(TRANSACTION_INFO_PREFIX_NAME_V2, id.to_vec()) .unwrap(); assert_eq!(value7, None); } @@ -246,7 +246,7 @@ fn test_two_level_storage_read_through() -> Result<()> { let transaction_info_data = storage_instance .cache() .unwrap() - .get(TRANSACTION_INFO_PREFIX_NAME, id.to_vec())?; + .get_raw(TRANSACTION_INFO_PREFIX_NAME, id.to_vec())?; assert!(transaction_info_data.is_none()); //let transaction_info3 = @@ -268,14 +268,14 @@ fn test_missing_key_handle() -> Result<()> { let key = HashValue::random(); let result = storage.get_transaction_info(key)?; assert!(result.is_none()); - let value2 = cache_storage.get(TRANSACTION_INFO_PREFIX_NAME, key.clone().to_vec())?; + let value2 = cache_storage.get_raw(TRANSACTION_INFO_PREFIX_NAME, key.clone().to_vec())?; assert!(value2.is_none()); - let value3 = db_storage.get(TRANSACTION_INFO_PREFIX_NAME, key.clone().to_vec())?; + let value3 = db_storage.get_raw(TRANSACTION_INFO_PREFIX_NAME, key.clone().to_vec())?; assert!(value3.is_none()); // test remove - let result2 = instance.remove(TRANSACTION_INFO_PREFIX_NAME, key.clone().to_vec()); + let result2 = instance.remove_raw(TRANSACTION_INFO_PREFIX_NAME, key.clone().to_vec()); assert!(result2.is_ok()); - let value4 = cache_storage.get(TRANSACTION_INFO_PREFIX_NAME, key.clone().to_vec())?; + let value4 = cache_storage.get_raw(TRANSACTION_INFO_PREFIX_NAME, key.clone().to_vec())?; assert!(value4.is_none()); let contains = instance.contains_key(TRANSACTION_INFO_PREFIX_NAME, key.clone().to_vec())?; assert!(!contains);