Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
XiangpengHao committed Nov 21, 2024
1 parent ae058af commit 33d7ae4
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 35 deletions.
7 changes: 0 additions & 7 deletions src/base_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,13 +247,6 @@ impl BaseNode {
Self::read_lock_inner(node)
}

pub(crate) fn read_lock_deprecated<'a, const MAX_LEVEL: usize>(
node: NodePtr,
current_level: usize,
) -> Result<ReadGuard<'a>, ArtError> {
Self::read_lock_inner(node.as_ptr_safe::<MAX_LEVEL>(current_level))
}

pub(crate) fn read_lock_root<'a>(node: NonNull<Node256>) -> Result<ReadGuard<'a>, ArtError> {
Self::read_lock_inner(unsafe {
std::mem::transmute::<NonNull<Node256>, NonNull<BaseNode>>(node)
Expand Down
8 changes: 0 additions & 8 deletions src/node_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,6 @@ impl NodePtr {
unsafe { self.payload }
}

pub(crate) fn as_ptr_safe<const MAX_LEVEL: usize>(
&self,
current_level: usize,
) -> NonNull<BaseNode> {
debug_assert!(current_level < MAX_LEVEL);
unsafe { self.sub_node }
}

pub(crate) fn downcast<const K_LEN: usize>(&self, current_level: usize) -> PtrType {
if current_level == (K_LEN - 1) {
PtrType::Payload(unsafe { self.as_payload_unchecked() })
Expand Down
24 changes: 15 additions & 9 deletions src/range_scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ impl<'a, const K_LEN: usize> RangeScan<'a, K_LEN> {
if k == start_level {
self.find_start(sub_node, &node, key_tracker.clone())?;
} else if k > start_level && k < end_level {
let cur_key = KeyTracker::append_prefix(n, &key_tracker);
let cur_key = KeyTracker::append_prefix(n, &key_tracker)?;
self.copy_node_recursive(n, &cur_key)?;
} else if k == end_level {
self.find_end(sub_node, &node, key_tracker.clone())?;
Expand All @@ -120,11 +120,17 @@ impl<'a, const K_LEN: usize> RangeScan<'a, K_LEN> {
}
key_tracker.push(start_level);

let next_node =
BaseNode::read_lock_deprecated::<K_LEN>(next_node_tmp, level)?;
parent_node = Some(node);
node = next_node;
continue;
match next_node_tmp.downcast_key_tracker::<K_LEN>(&key_tracker) {
PtrType::Payload(_payload) => {
unreachable!()
}
PtrType::SubNode(sub_node) => {
let next_node = BaseNode::read_lock(sub_node)?;
parent_node = Some(node);
node = next_node;
continue;
}
}
}
return Ok(self.result_found);
}
Expand Down Expand Up @@ -176,7 +182,7 @@ impl<'a, const K_LEN: usize> RangeScan<'a, K_LEN> {
if k == end_level {
self.find_end(sub_node, &node, key_tracker.clone())?;
} else if k < end_level {
let cur_key = KeyTracker::append_prefix(n, &key_tracker);
let cur_key = KeyTracker::append_prefix(n, &key_tracker)?;
self.copy_node_recursive(n, &cur_key)?;
}
}
Expand Down Expand Up @@ -234,7 +240,7 @@ impl<'a, const K_LEN: usize> RangeScan<'a, K_LEN> {
if k == start_level {
self.find_start(sub_node, &node, key_tracker.clone())?;
} else if k > start_level {
let cur_key = KeyTracker::append_prefix(n, &key_tracker);
let cur_key = KeyTracker::append_prefix(n, &key_tracker)?;
self.copy_node_recursive(n, &cur_key)?;
}
}
Expand Down Expand Up @@ -284,7 +290,7 @@ impl<'a, const K_LEN: usize> RangeScan<'a, K_LEN> {

key_tracker.push(k);

let cur_key = KeyTracker::append_prefix(c, &key_tracker);
let cur_key = KeyTracker::append_prefix(c, &key_tracker)?;
self.copy_node_recursive(c, &cur_key)?;

if self.to_continue {
Expand Down
24 changes: 13 additions & 11 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::base_node::BaseNode;
use crate::node_ptr::NodePtr;
use crate::error::ArtError;
use crate::node_ptr::{NodePtr, PtrType};
use core::cell::Cell;
use core::fmt;

Expand Down Expand Up @@ -131,17 +132,18 @@ impl<const K_LEN: usize> KeyTracker<K_LEN> {
pub(crate) fn append_prefix(
node: NodePtr,
key_tracker: &KeyTracker<K_LEN>,
) -> KeyTracker<K_LEN> {
let mut cur_key = key_tracker.clone();
if key_tracker.len() == K_LEN {
cur_key
} else {
let node_ref = BaseNode::read_lock_deprecated::<K_LEN>(node, 0).unwrap();
let n_prefix = node_ref.as_ref().prefix().iter().skip(key_tracker.len());
for i in n_prefix {
cur_key.push(*i);
) -> Result<KeyTracker<K_LEN>, ArtError> {
match node.downcast_key_tracker::<K_LEN>(key_tracker) {
PtrType::Payload(_payload) => Ok(key_tracker.clone()),
PtrType::SubNode(sub_node) => {
let node_ref = BaseNode::read_lock(sub_node)?;
let n_prefix = node_ref.as_ref().prefix().iter().skip(key_tracker.len());
let mut cur_key = key_tracker.clone();
for i in n_prefix {
cur_key.push(*i);
}
Ok(cur_key)
}
cur_key
}
}

Expand Down

0 comments on commit 33d7ae4

Please sign in to comment.