From b9426ea412700fdd06d06428d4c202790e3adecd Mon Sep 17 00:00:00 2001 From: kennytm Date: Fri, 3 Sep 2021 19:04:55 +0800 Subject: [PATCH] *: refactor: remove the tikv_util::map macro (#10886) * *: refactor: remove the tikv_util::map macro Signed-off-by: kennytm * raftstore: fix clippy Signed-off-by: kennytm --- components/raftstore/src/store/snap.rs | 9 ++- .../tidb_query_datatype/src/codec/table.rs | 35 +++++----- components/tikv_util/src/macros.rs | 64 ------------------- src/storage/txn/flow_controller.rs | 10 +-- tests/integrations/config/mod.rs | 5 +- 5 files changed, 30 insertions(+), 93 deletions(-) diff --git a/components/raftstore/src/store/snap.rs b/components/raftstore/src/store/snap.rs index 6019b77fdb3..ecc7c2ea475 100644 --- a/components/raftstore/src/store/snap.rs +++ b/components/raftstore/src/store/snap.rs @@ -34,7 +34,7 @@ use file_system::{ use keys::{enc_end_key, enc_start_key}; use tikv_util::time::{duration_to_sec, Instant, Limiter}; use tikv_util::HandyRwLock; -use tikv_util::{box_err, box_try, debug, error, info, map, warn}; +use tikv_util::{box_err, box_try, debug, error, info, warn}; use crate::coprocessor::CoprocessorHost; use crate::store::metrics::{ @@ -1515,7 +1515,7 @@ impl SnapManagerBuilder { SnapManager { core: SnapManagerCore { base: path.into(), - registry: Arc::new(RwLock::new(map![])), + registry: Default::default(), limiter, temp_sst_id: Arc::new(AtomicU64::new(0)), encryption_key_manager: self.key_manager, @@ -1532,7 +1532,7 @@ pub mod tests { use std::io::{self, Read, Seek, SeekFrom, Write}; use std::path::{Path, PathBuf}; use std::sync::atomic::{AtomicU64, AtomicUsize}; - use std::sync::{Arc, RwLock}; + use std::sync::Arc; use encryption::{EncryptionConfig, FileConfig, MasterKeyConfig}; use encryption_export::data_key_manager_from_config; @@ -1553,7 +1553,6 @@ pub mod tests { use protobuf::Message; use tempfile::{Builder, TempDir}; - use tikv_util::map; use tikv_util::time::Limiter; use super::{ @@ -1699,7 +1698,7 @@ pub mod tests { fn create_manager_core(path: &str) -> SnapManagerCore { SnapManagerCore { base: path.to_owned(), - registry: Arc::new(RwLock::new(map![])), + registry: Default::default(), limiter: Limiter::new(f64::INFINITY), temp_sst_id: Arc::new(AtomicU64::new(0)), encryption_key_manager: None, diff --git a/components/tidb_query_datatype/src/codec/table.rs b/components/tidb_query_datatype/src/codec/table.rs index adf2f60b6af..4dc076b853e 100644 --- a/components/tidb_query_datatype/src/codec/table.rs +++ b/components/tidb_query_datatype/src/codec/table.rs @@ -536,13 +536,12 @@ pub fn generate_index_data_for_test( #[cfg(test)] mod tests { - use std::i64; + use std::{i64, iter::FromIterator}; use tipb::ColumnInfo; use crate::codec::datum::{self, Datum}; use collections::{HashMap, HashSet}; - use tikv_util::map; use super::*; @@ -623,21 +622,23 @@ mod tests { .set_tp(FieldTypeTp::Duration) .set_decimal(2); - let mut cols = map![ - 1 => FieldTypeTp::LongLong.into(), - 2 => FieldTypeTp::VarChar.into(), - 3 => FieldTypeTp::NewDecimal.into(), - 5 => FieldTypeTp::JSON.into(), - 6 => duration_col - ]; - - let mut row = map![ - 1 => Datum::I64(100), - 2 => Datum::Bytes(b"abc".to_vec()), - 3 => Datum::Dec(10.into()), - 5 => Datum::Json(r#"{"name": "John"}"#.parse().unwrap()), - 6 => Datum::Dur(Duration::parse(&mut EvalContext::default(),"23:23:23.666",2 ).unwrap()) - ]; + let mut cols = HashMap::from_iter([ + (1, FieldTypeTp::LongLong.into()), + (2, FieldTypeTp::VarChar.into()), + (3, FieldTypeTp::NewDecimal.into()), + (5, FieldTypeTp::JSON.into()), + (6, duration_col), + ]); + + let duration_row = Duration::parse(&mut EvalContext::default(), "23:23:23.666", 2).unwrap(); + + let mut row = HashMap::from_iter([ + (1, Datum::I64(100)), + (2, Datum::Bytes(b"abc".to_vec())), + (3, Datum::Dec(10.into())), + (5, Datum::Json(r#"{"name": "John"}"#.parse().unwrap())), + (6, Datum::Dur(duration_row)), + ]); let mut ctx = EvalContext::default(); let col_ids: Vec<_> = row.iter().map(|(&id, _)| id).collect(); diff --git a/components/tikv_util/src/macros.rs b/components/tikv_util/src/macros.rs index ba0101f2546..ff32d255276 100644 --- a/components/tikv_util/src/macros.rs +++ b/components/tikv_util/src/macros.rs @@ -2,70 +2,6 @@ //! The macros crate contains all useful needed macros. -/// Gets the count of macro's arguments. -/// -/// # Examples -/// -/// ``` -/// # #[macro_use] extern crate tikv_util; -/// # fn main() { -/// assert_eq!(count_args!(), 0); -/// assert_eq!(count_args!(1), 1); -/// assert_eq!(count_args!(1, 2), 2); -/// assert_eq!(count_args!(1, 2, 3), 3); -/// # } -/// ``` -#[macro_export] -macro_rules! count_args { - () => { 0 }; - ($head:expr $(, $tail:expr)*) => { 1 + $crate::count_args!($($tail),*) }; -} - -/// Initializes a `HashMap` with specified key-value pairs. -/// -/// # Examples -/// -/// ``` -/// # #[macro_use] extern crate tikv_util; -/// # fn main() { -/// // empty map -/// let m: tikv_util::collections::HashMap = map!(); -/// assert!(m.is_empty()); -/// -/// // one initial kv pairs. -/// let m = map!("key" => "value"); -/// assert_eq!(m.len(), 1); -/// assert_eq!(m["key"], "value"); -/// -/// // initialize with multiple kv pairs. -/// let m = map!("key1" => "value1", "key2" => "value2"); -/// assert_eq!(m.len(), 2); -/// assert_eq!(m["key1"], "value1"); -/// assert_eq!(m["key2"], "value2"); -/// # } -/// ``` -#[macro_export] -macro_rules! map { - () => { - { - collections::HashMap::default() - } - }; - ( $( $k:expr => $v:expr ),+ ) => { - { - let mut temp_map = - collections::HashMap::with_capacity_and_hasher( - $crate::count_args!($(($k, $v)),+), - Default::default() - ); - $( - temp_map.insert($k, $v); - )+ - temp_map - } - }; -} - /// A shortcut to box an error. #[macro_export] macro_rules! box_err { diff --git a/src/storage/txn/flow_controller.rs b/src/storage/txn/flow_controller.rs index 3aa6e22acbb..2b519bd69c0 100644 --- a/src/storage/txn/flow_controller.rs +++ b/src/storage/txn/flow_controller.rs @@ -459,11 +459,11 @@ impl FlowChecker { discard_ratio: Arc, limiter: Arc, ) -> Self { - let mut cf_checkers = map![]; - - for cf in engine.cf_names() { - cf_checkers.insert(cf.to_owned(), CFFlowChecker::default()); - } + let cf_checkers = engine + .cf_names() + .into_iter() + .map(|cf| (cf.to_owned(), CFFlowChecker::default())) + .collect(); Self { soft_pending_compaction_bytes_limit: config.soft_pending_compaction_bytes_limit.0, diff --git a/tests/integrations/config/mod.rs b/tests/integrations/config/mod.rs index 69e186970b3..8483e33c6a9 100644 --- a/tests/integrations/config/mod.rs +++ b/tests/integrations/config/mod.rs @@ -2,12 +2,13 @@ use std::fs::File; use std::io::Read; +use std::iter::FromIterator; use std::path::PathBuf; use slog::Level; use batch_system::Config as BatchSystemConfig; -use collections::HashSet; +use collections::{HashMap, HashSet}; use encryption::{EncryptionConfig, FileConfig, MasterKeyConfig}; use engine_rocks::config::{BlobRunMode, CompressionType, LogLevel}; use engine_rocks::raw::{ @@ -68,7 +69,7 @@ fn test_serde_custom_tikv_config() { value.server = ServerConfig { cluster_id: 0, // KEEP IT ZERO, it is skipped by serde. addr: "example.com:443".to_owned(), - labels: map! { "a".to_owned() => "b".to_owned() }, + labels: HashMap::from_iter([("a".to_owned(), "b".to_owned())]), advertise_addr: "example.com:443".to_owned(), status_addr: "example.com:443".to_owned(), advertise_status_addr: "example.com:443".to_owned(),