Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore/add twig node types #61

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions src/art.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,20 @@ impl<P: KeyTrait, V: Clone> Node<P, V> {
/// Returns a new `Node` instance with a Twig node containing the provided key, value, and version.
///
#[inline]
pub(crate) fn new_twig(prefix: P, key: P, value: V, version: u64, ts: u64) -> Node<P, V> {
pub(crate) fn new_twig(
prefix: P,
key: P,
value: V,
version: u64,
ts: u64,
is_single: bool,
) -> Node<P, V> {
// Create a new TwigNode instance using the provided prefix and key.
let mut twig = TwigNode::new(prefix, key);
let mut twig = if is_single {
TwigNode::new_single(prefix, key)
} else {
TwigNode::new(prefix, key)
};

// Insert the provided value into the TwigNode along with the version.
twig.insert_mut(value, version, ts);
Expand Down Expand Up @@ -714,7 +725,7 @@ impl<P: KeyTrait, V: Clone> Node<P, V> {
if is_prefix_match && twig.prefix.len() == key_prefix.len() {
let new_twig = if replace {
// Create a replacement Twig node with the new value only.
let mut new_twig = TwigNode::new(twig.prefix.clone(), twig.key.clone());
let mut new_twig = TwigNode::new_single(twig.prefix.clone(), twig.key.clone());
new_twig.insert_mut(value, commit_version, ts);
new_twig
} else {
Expand All @@ -740,6 +751,7 @@ impl<P: KeyTrait, V: Clone> Node<P, V> {
value,
commit_version,
ts,
replace,
);
n4 = n4.add_child(k1, old_node).add_child(k2, new_twig);

Expand Down Expand Up @@ -770,6 +782,7 @@ impl<P: KeyTrait, V: Clone> Node<P, V> {
value,
commit_version,
ts,
replace,
);
let new_node = cur_node.add_child(k, new_twig);

Expand Down Expand Up @@ -825,6 +838,7 @@ impl<P: KeyTrait, V: Clone> Node<P, V> {
value,
commit_version,
ts,
replace,
);
cur_node.add_child_mut(k1, old_node);
cur_node.add_child_mut(k2, new_twig);
Expand Down Expand Up @@ -856,6 +870,7 @@ impl<P: KeyTrait, V: Clone> Node<P, V> {
value,
commit_version,
ts,
replace,
);
cur_node.add_child_mut(k, new_twig);
}
Expand Down Expand Up @@ -1054,6 +1069,7 @@ impl<P: KeyTrait, V: Clone> Tree<P, V> {
value,
commit_version,
ts,
replace,
))
}
Some(root) => {
Expand Down Expand Up @@ -1103,6 +1119,7 @@ impl<P: KeyTrait, V: Clone> Tree<P, V> {
value,
commit_version,
ts,
replace,
)));
}
self.size += 1;
Expand Down
185 changes: 147 additions & 38 deletions src/node.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::slice::Iter;
use std::sync::Arc;

use crate::{art::QueryType, KeyTrait};
Expand Down Expand Up @@ -26,10 +27,86 @@ pub(crate) trait Version {
pub(crate) struct TwigNode<K: KeyTrait, V: Clone> {
pub(crate) prefix: K,
pub(crate) key: K,
pub(crate) values: Vec<Arc<LeafValue<V>>>,
pub(crate) values: Values<V>,
pub(crate) version: u64, // Version for the twig node
}

#[derive(Clone)]
pub(crate) enum Values<V: Clone> {
Single(Option<Arc<LeafValue<V>>>),
Multiple(Vec<Arc<LeafValue<V>>>),
}

pub(crate) struct ValuesIter<'a, V: Clone> {
single: Option<&'a Arc<LeafValue<V>>>,
multiple: Option<Iter<'a, Arc<LeafValue<V>>>>,
}

impl<V: Clone> Values<V> {
pub(crate) fn iter(&self) -> ValuesIter<V> {
match self {
Values::Single(Some(value)) => ValuesIter {
single: Some(value),
multiple: None,
},
Values::Single(None) => ValuesIter {
single: None,
multiple: None,
},
Values::Multiple(values) => ValuesIter {
single: None,
multiple: Some(values.iter()),
},
}
}

#[allow(unused)]
pub(crate) fn len(&self) -> usize {
match self {
Values::Single(Some(_)) => 1,
Values::Single(None) => 0,
Values::Multiple(values) => values.len(),
}
}

pub(crate) fn clear(&mut self) {
match self {
Values::Single(_) => {
*self = Values::Single(None);
}
Values::Multiple(values) => {
values.clear();
}
}
}
}

impl<'a, V: Clone> Iterator for ValuesIter<'a, V> {
type Item = &'a Arc<LeafValue<V>>;

fn next(&mut self) -> Option<Self::Item> {
if let Some(single) = self.single.take() {
return Some(single);
}
if let Some(multiple) = &mut self.multiple {
return multiple.next();
}
None
}
}

impl<'a, V: Clone> DoubleEndedIterator for ValuesIter<'a, V> {
fn next_back(&mut self) -> Option<Self::Item> {
if let Some(multiple) = &mut self.multiple {
return multiple.next_back();
}
if let Some(single) = self.single.take() {
return Some(single);
}
None
}
}

// Timestamp-Version Ordering Constraint Explanation:
// Given two internal keys associated with the same user key, represented as:
// (key, version1, ts1) and (key, version2, ts2),
Expand All @@ -56,51 +133,72 @@ impl<K: KeyTrait, V: Clone> TwigNode<K, V> {
TwigNode {
prefix,
key,
values: Vec::new(),
values: Values::Multiple(Vec::new()),
version: 0,
}
}

pub(crate) fn new_single(prefix: K, key: K) -> Self {
TwigNode {
prefix,
key,
values: Values::Single(None),
version: 0,
}
}

pub(crate) fn version(&self) -> u64 {
self.values
.iter()
.map(|value| value.version)
.max()
.unwrap_or(self.version)
match &self.values {
Values::Single(Some(value)) => value.version,
Values::Single(None) => self.version,
Values::Multiple(values) => values
.iter()
.map(|value| value.version)
.max()
.unwrap_or(self.version),
}
}

fn insert_common(values: &mut Vec<Arc<LeafValue<V>>>, value: V, version: u64, ts: u64) {
fn insert_common(values: &mut Values<V>, value: V, version: u64, ts: u64) {
let new_leaf_value = LeafValue::new(value, version, ts);

// Check if a LeafValue with the same version exists and update or insert accordingly
match values.binary_search_by(|v| v.version.cmp(&new_leaf_value.version)) {
Ok(index) => {
// If an entry with the same version and timestamp exists, just put the same value
if values[index].ts == ts {
values[index] = Arc::new(new_leaf_value);
} else {
// If an entry with the same version and different timestamp exists, add a new entry
// Determine the direction to scan based on the comparison of timestamps
let mut insert_position = index;
if values[index].ts < ts {
// Scan forward to find the first entry with a timestamp greater than the new entry's timestamp
insert_position +=
values[index..].iter().take_while(|v| v.ts <= ts).count();
} else {
// Scan backward to find the insertion point before the first entry with a timestamp less than the new entry's timestamp
insert_position -= values[..index]
.iter()
.rev()
.take_while(|v| v.ts >= ts)
.count();
match values {
Values::Single(existing_value) => {
// Replace the existing single value
*existing_value = Some(new_leaf_value.into());
}
Values::Multiple(values) => {
// Check if a LeafValue with the same version exists and update or insert accordingly
match values.binary_search_by(|v| v.version.cmp(&new_leaf_value.version)) {
Ok(index) => {
// If an entry with the same version and timestamp exists, just put the same value
if values[index].ts == ts {
values[index] = Arc::new(new_leaf_value);
} else {
// If an entry with the same version and different timestamp exists, add a new entry
// Determine the direction to scan based on the comparison of timestamps
let mut insert_position = index;
if values[index].ts < ts {
// Scan forward to find the first entry with a timestamp greater than the new entry's timestamp
insert_position +=
values[index..].iter().take_while(|v| v.ts <= ts).count();
} else {
// Scan backward to find the insertion point before the first entry with a timestamp less than the new entry's timestamp
insert_position -= values[..index]
.iter()
.rev()
.take_while(|v| v.ts >= ts)
.count();
}
values.insert(insert_position, Arc::new(new_leaf_value));
}
}
Err(index) => {
// If no entry with the same version exists, insert the new value at the correct position
values.insert(index, Arc::new(new_leaf_value));
}
values.insert(insert_position, Arc::new(new_leaf_value));
}
}
Err(index) => {
// If no entry with the same version exists, insert the new value at the correct position
values.insert(index, Arc::new(new_leaf_value));
}
}
}

Expand Down Expand Up @@ -1164,8 +1262,13 @@ mod tests {
let new_node = node.insert(42, 123, 0);
assert_eq!(node.values.len(), 0);
assert_eq!(new_node.values.len(), 1);
assert_eq!(new_node.values[0].value, 42);
assert_eq!(new_node.values[0].version, 123);
let mut iter = new_node.values.iter();
if let Some(first_value) = iter.next() {
assert_eq!(first_value.value, 42);
assert_eq!(first_value.version, 123);
} else {
panic!("Expected at least one value in the node");
}
}

#[test]
Expand All @@ -1176,8 +1279,14 @@ mod tests {

node.insert_mut(42, 123, 0);
assert_eq!(node.values.len(), 1);
assert_eq!(node.values[0].value, 42);
assert_eq!(node.values[0].version, 123);

let mut iter = node.values.iter();
if let Some(first_value) = iter.next() {
assert_eq!(first_value.value, 42);
assert_eq!(first_value.version, 123);
} else {
panic!("Expected at least one value in the node");
}
}

#[test]
Expand Down
1 change: 1 addition & 0 deletions src/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ impl<P: KeyTrait, V: Clone> Snapshot<P, V> {
value,
self.version,
ts,
false,
)))
}
};
Expand Down
Loading