From fce1b671b0abe285147c736dea6eb1f387974762 Mon Sep 17 00:00:00 2001 From: Gwo Tzu-Hsing Date: Wed, 17 Jul 2024 13:10:34 +0800 Subject: [PATCH] chore: refactor --- src/record/mod.rs | 2 +- src/scope.rs | 13 +++++-------- src/version/edit.rs | 11 ++++------- src/version/mod.rs | 22 +++------------------- src/version/set.rs | 6 ++++-- 5 files changed, 17 insertions(+), 37 deletions(-) diff --git a/src/record/mod.rs b/src/record/mod.rs index 79fc7ee..fa805c1 100644 --- a/src/record/mod.rs +++ b/src/record/mod.rs @@ -14,7 +14,7 @@ use crate::{ serdes::{Decode, Encode}, }; -pub trait Key: 'static + Encode + Decode + Ord + Clone + Send + Sync { +pub trait Key: 'static + Encode + Decode + Ord + Clone + Send { type Ref<'r>: KeyRef<'r, Key = Self> + Copy where Self: 'r; diff --git a/src/scope.rs b/src/scope.rs index e6bf93a..081f1e5 100644 --- a/src/scope.rs +++ b/src/scope.rs @@ -7,10 +7,7 @@ use crate::{ }; #[derive(Debug, Eq, PartialEq)] -pub struct Scope -where - K: Encode + Decode + Ord + Clone, -{ +pub struct Scope { pub(crate) min: K, pub(crate) max: K, pub(crate) gen: FileId, @@ -19,7 +16,7 @@ where impl Clone for Scope where - K: Encode + Decode + Ord + Clone, + K: Clone, { fn clone(&self) -> Self { Scope { @@ -33,7 +30,7 @@ where impl Scope where - K: Encode + Decode + Ord + Clone, + K: Ord, { pub(crate) fn is_between(&self, key: &K) -> bool { self.min.le(key) && self.max.ge(key) @@ -49,7 +46,7 @@ where impl Encode for Scope where - K: Encode + Decode + Ord + Clone, + K: Encode, { type Error = ::Error; @@ -85,7 +82,7 @@ where impl Decode for Scope where - K: Encode + Decode + Ord + Clone, + K: Decode, { type Error = ::Error; diff --git a/src/version/edit.rs b/src/version/edit.rs index ef07e09..e89021b 100644 --- a/src/version/edit.rs +++ b/src/version/edit.rs @@ -10,17 +10,14 @@ use crate::{ }; #[derive(Debug, Clone, Eq, PartialEq)] -pub(crate) enum VersionEdit -where - K: Encode + Decode + Ord + Clone, -{ +pub(crate) enum VersionEdit { Add { level: u8, scope: Scope }, Remove { level: u8, gen: FileId }, } impl VersionEdit where - K: Encode + Decode + Ord + Clone, + K: Decode, { pub(crate) async fn recover(reader: &mut R) -> Vec> { let mut edits = Vec::new(); @@ -34,7 +31,7 @@ where impl Encode for VersionEdit where - K: Encode + Decode + Ord + Clone, + K: Encode, { type Error = ::Error; @@ -70,7 +67,7 @@ where impl Decode for VersionEdit where - K: Encode + Decode + Ord + Clone, + K: Decode, { type Error = ::Error; diff --git a/src/version/mod.rs b/src/version/mod.rs index 4e8f1bf..6d3dbce 100644 --- a/src/version/mod.rs +++ b/src/version/mod.rs @@ -21,14 +21,13 @@ use crate::{ DbOption, }; -pub const MAX_LEVEL: usize = 7; +const MAX_LEVEL: usize = 7; pub(crate) type VersionRef = Arc>; pub(crate) struct Version where R: Record, - E: Executor, { pub(crate) num: usize, pub(crate) level_slice: [Vec>; MAX_LEVEL], @@ -43,12 +42,10 @@ where E: Executor, { fn clone(&self) -> Self { - let mut level_slice = Version::::level_slice_new(); + let mut level_slice = [const { Vec::new() }; MAX_LEVEL]; for (level, scopes) in self.level_slice.iter().enumerate() { - for scope in scopes { - level_slice[level].push(scope.clone()); - } + level_slice[level].clone_from(scopes); } Self { @@ -117,18 +114,6 @@ where self.level_slice[level].len() } - pub(crate) fn level_slice_new() -> [Vec>; 7] { - [ - Vec::new(), - Vec::new(), - Vec::new(), - Vec::new(), - Vec::new(), - Vec::new(), - Vec::new(), - ] - } - pub(crate) async fn iters<'a>( &self, iters: &mut Vec>, @@ -163,7 +148,6 @@ where impl Drop for Version where R: Record, - E: Executor, { fn drop(&mut self) { if let Err(err) = self.clean_sender.send(CleanTag::Clean { diff --git a/src/version/set.rs b/src/version/set.rs index dda5cf3..2ef01f5 100644 --- a/src/version/set.rs +++ b/src/version/set.rs @@ -13,6 +13,8 @@ use crate::{ DbOption, }; +use super::MAX_LEVEL; + pub(crate) struct VersionSetInner where R: Record, @@ -65,7 +67,7 @@ where inner: Arc::new(RwLock::new(VersionSetInner { current: Arc::new(Version:: { num: 0, - level_slice: Version::::level_slice_new(), + level_slice: [const { Vec::new() }; MAX_LEVEL], clean_sender: clean_sender.clone(), option: option.clone(), _p: Default::default(), @@ -92,7 +94,7 @@ where ) -> Result<(), VersionError> { let mut guard = self.inner.write().await; - let mut new_version = Version::::clone(&guard.current); + let mut new_version = Version::clone(&guard.current); for version_edit in version_edits { if !is_recover {