Skip to content

Commit

Permalink
chore: refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
ethe committed Jul 17, 2024
1 parent a211a1e commit fce1b67
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 37 deletions.
2 changes: 1 addition & 1 deletion src/record/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
13 changes: 5 additions & 8 deletions src/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ use crate::{
};

#[derive(Debug, Eq, PartialEq)]
pub struct Scope<K>
where
K: Encode + Decode + Ord + Clone,
{
pub struct Scope<K> {
pub(crate) min: K,
pub(crate) max: K,
pub(crate) gen: FileId,
Expand All @@ -19,7 +16,7 @@ where

impl<K> Clone for Scope<K>
where
K: Encode + Decode + Ord + Clone,
K: Clone,
{
fn clone(&self) -> Self {
Scope {
Expand All @@ -33,7 +30,7 @@ where

impl<K> Scope<K>
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)
Expand All @@ -49,7 +46,7 @@ where

impl<K> Encode for Scope<K>
where
K: Encode + Decode + Ord + Clone,
K: Encode,
{
type Error = <K as Encode>::Error;

Expand Down Expand Up @@ -85,7 +82,7 @@ where

impl<K> Decode for Scope<K>
where
K: Encode + Decode + Ord + Clone,
K: Decode,
{
type Error = <K as Decode>::Error;

Expand Down
11 changes: 4 additions & 7 deletions src/version/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,14 @@ use crate::{
};

#[derive(Debug, Clone, Eq, PartialEq)]
pub(crate) enum VersionEdit<K>
where
K: Encode + Decode + Ord + Clone,
{
pub(crate) enum VersionEdit<K> {
Add { level: u8, scope: Scope<K> },
Remove { level: u8, gen: FileId },
}

impl<K> VersionEdit<K>
where
K: Encode + Decode + Ord + Clone,
K: Decode,
{
pub(crate) async fn recover<R: AsyncRead + Unpin>(reader: &mut R) -> Vec<VersionEdit<K>> {
let mut edits = Vec::new();
Expand All @@ -34,7 +31,7 @@ where

impl<K> Encode for VersionEdit<K>
where
K: Encode + Decode + Ord + Clone,
K: Encode,
{
type Error = <K as Encode>::Error;

Expand Down Expand Up @@ -70,7 +67,7 @@ where

impl<K> Decode for VersionEdit<K>
where
K: Encode + Decode + Ord + Clone,
K: Decode,
{
type Error = <K as Decode>::Error;

Expand Down
22 changes: 3 additions & 19 deletions src/version/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@ use crate::{
DbOption,
};

pub const MAX_LEVEL: usize = 7;
const MAX_LEVEL: usize = 7;

pub(crate) type VersionRef<R, E> = Arc<Version<R, E>>;

pub(crate) struct Version<R, E>
where
R: Record,
E: Executor,
{
pub(crate) num: usize,
pub(crate) level_slice: [Vec<Scope<R::Key>>; MAX_LEVEL],
Expand All @@ -43,12 +42,10 @@ where
E: Executor,
{
fn clone(&self) -> Self {
let mut level_slice = Version::<R, E>::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 {
Expand Down Expand Up @@ -117,18 +114,6 @@ where
self.level_slice[level].len()
}

pub(crate) fn level_slice_new() -> [Vec<Scope<R::Key>>; 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<ScanStream<'a, R>>,
Expand Down Expand Up @@ -163,7 +148,6 @@ where
impl<R, E> Drop for Version<R, E>
where
R: Record,
E: Executor,
{
fn drop(&mut self) {
if let Err(err) = self.clean_sender.send(CleanTag::Clean {
Expand Down
6 changes: 4 additions & 2 deletions src/version/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ use crate::{
DbOption,
};

use super::MAX_LEVEL;

pub(crate) struct VersionSetInner<R, E>
where
R: Record,
Expand Down Expand Up @@ -65,7 +67,7 @@ where
inner: Arc::new(RwLock::new(VersionSetInner {
current: Arc::new(Version::<R, E> {
num: 0,
level_slice: Version::<R, E>::level_slice_new(),
level_slice: [const { Vec::new() }; MAX_LEVEL],
clean_sender: clean_sender.clone(),
option: option.clone(),
_p: Default::default(),
Expand All @@ -92,7 +94,7 @@ where
) -> Result<(), VersionError<R>> {
let mut guard = self.inner.write().await;

let mut new_version = Version::<R, E>::clone(&guard.current);
let mut new_version = Version::clone(&guard.current);

for version_edit in version_edits {
if !is_recover {
Expand Down

0 comments on commit fce1b67

Please sign in to comment.