From 6e9964ac9716a249714e2a946d499cbc0a77355c Mon Sep 17 00:00:00 2001 From: Yingwen Date: Mon, 20 Feb 2023 14:35:30 +0800 Subject: [PATCH] refactor(storage): Simplify debug output of some structs (#1028) * refactor: Simplify debug output of RegionImpl * feat: Simplify memtable debug output --- src/storage/src/memtable/btree.rs | 16 +++++++++++++++- src/storage/src/region.rs | 19 +++++++++++++++++-- src/storage/src/schema/region.rs | 11 ++++++++++- 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/src/storage/src/memtable/btree.rs b/src/storage/src/memtable/btree.rs index 153aaf77c9..fa1ba729de 100644 --- a/src/storage/src/memtable/btree.rs +++ b/src/storage/src/memtable/btree.rs @@ -14,6 +14,7 @@ use std::cmp::Ordering; use std::collections::{btree_map, BTreeMap}; +use std::fmt; use std::ops::Bound; use std::sync::atomic::{AtomicUsize, Ordering as AtomicOrdering}; use std::sync::{Arc, RwLock}; @@ -37,7 +38,6 @@ type RwLockMap = RwLock>; /// A simple memtable implementation based on std's [`BTreeMap`]. /// /// Mainly for test purpose, don't use in production. -#[derive(Debug)] pub struct BTreeMemtable { id: MemtableId, schema: RegionSchemaRef, @@ -56,6 +56,20 @@ impl BTreeMemtable { } } +impl fmt::Debug for BTreeMemtable { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let len = self.map.read().unwrap().len(); + + f.debug_struct("BTreeMemtable") + .field("id", &self.id) + // Only show StoreSchema + .field("schema", &self.schema) + .field("rows", &len) + .field("estimated_bytes", &self.estimated_bytes) + .finish() + } +} + impl Memtable for BTreeMemtable { fn id(&self) -> MemtableId { self.id diff --git a/src/storage/src/region.rs b/src/storage/src/region.rs index fb136a48a3..77d0531d04 100644 --- a/src/storage/src/region.rs +++ b/src/storage/src/region.rs @@ -15,7 +15,9 @@ #[cfg(test)] mod tests; mod writer; + use std::collections::BTreeMap; +use std::fmt; use std::sync::Arc; use async_trait::async_trait; @@ -50,7 +52,6 @@ use crate::wal::Wal; use crate::write_batch::WriteBatch; /// [Region] implementation. -#[derive(Debug)] pub struct RegionImpl { inner: Arc>, } @@ -63,6 +64,21 @@ impl Clone for RegionImpl { } } +impl fmt::Debug for RegionImpl { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.debug_struct("RegionImpl") + .field("id", &self.inner.shared.id) + .field("name", &self.inner.shared.name) + .field("wal", &self.inner.wal) + .field("flush_strategy", &self.inner.flush_strategy) + .field("flush_scheduler", &self.inner.flush_scheduler) + .field("compaction_scheduler", &self.inner.compaction_scheduler) + .field("sst_layer", &self.inner.sst_layer) + .field("manifest", &self.inner.manifest) + .finish() + } +} + #[async_trait] impl Region for RegionImpl { type Error = Error; @@ -454,7 +470,6 @@ impl SharedData { pub type SharedDataRef = Arc; -#[derive(Debug)] struct RegionInner { shared: SharedDataRef, writer: RegionWriterRef, diff --git a/src/storage/src/schema/region.rs b/src/storage/src/schema/region.rs index 0b3625ff0a..071797e6b1 100644 --- a/src/storage/src/schema/region.rs +++ b/src/storage/src/schema/region.rs @@ -12,6 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +use std::fmt; use std::sync::Arc; use common_error::prelude::*; @@ -32,7 +33,7 @@ use crate::schema::{StoreSchema, StoreSchemaRef}; /// /// The user schema is the schema that only contains columns that user could visit, /// as well as what the schema user created. -#[derive(Debug, PartialEq, Eq)] +#[derive(PartialEq, Eq)] pub struct RegionSchema { /// Schema that only contains columns that user defined, excluding internal columns /// that are reserved and used by the storage engine. @@ -48,6 +49,14 @@ pub struct RegionSchema { columns: ColumnsMetadataRef, } +impl fmt::Debug for RegionSchema { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.debug_struct("RegionSchema") + .field("columns", &self.columns) + .finish() + } +} + impl RegionSchema { pub fn new(columns: ColumnsMetadataRef, version: u32) -> Result { let user_schema = Arc::new(build_user_schema(&columns, version)?);