Skip to content

Commit

Permalink
feat: add cow btree
Browse files Browse the repository at this point in the history
  • Loading branch information
arriqaaq committed Dec 20, 2024
1 parent fe3ae95 commit 67735ba
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 28 deletions.
20 changes: 6 additions & 14 deletions src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,21 +154,13 @@ impl<K: KeyTrait + Clone, V: Clone> TwigNode<K, V> {

#[inline]
pub(crate) fn get_leaf_by_ts(&self, ts: u64) -> Option<&Arc<LeafValue<V>>> {
// self.values
// .iter()
// .filter(|value| value.ts <= ts)
// .max_by_key(|value| value.ts)

self.values
.iter()
.filter(|value| {
let should_include = value.ts <= ts;
if should_include {
println!("Version: {:?}, Key: {:?}, TS: {}", value.version, self.key, value.ts);
}
should_include
})
.max_by_key(|value| value.ts)
.iter()
.filter(|value| value.ts <= ts)
.max_by(|a, b| {
a.ts.cmp(&b.ts)
.then_with(|| Arc::as_ptr(a).cmp(&Arc::as_ptr(b)))
})
}

#[inline]
Expand Down
62 changes: 48 additions & 14 deletions src/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,14 @@ impl<V: Clone> BTree<V> {
{
i -= 1;
}
node.entries.insert(i, leaf_value);
// Check for duplicate version and replace if the new one has the same or later timestamp
if i > 0 && node.entries[i - 1].version == leaf_value.version {
if node.entries[i - 1].ts <= leaf_value.ts {
node.entries[i - 1] = leaf_value;
}
} else {
node.entries.insert(i, leaf_value);
}
} else {
while i > 0
&& !self.should_go_right(&node.entries[i - 1], leaf_value.version, leaf_value.ts)
Expand All @@ -115,6 +122,37 @@ impl<V: Clone> BTree<V> {
}
}

// fn insert_non_full(&self, node: &mut BNode<V>, leaf_value: Arc<LeafValue<V>>) {
// let mut i = node.entries.len();

// if node.is_leaf {
// while i > 0
// && !self.should_go_right(&node.entries[i - 1], leaf_value.version, leaf_value.ts)
// {
// i -= 1;
// }
// node.entries.insert(i, leaf_value);
// } else {
// while i > 0
// && !self.should_go_right(&node.entries[i - 1], leaf_value.version, leaf_value.ts)
// {
// i -= 1;
// }

// let child = Arc::clone(&node.children[i]);
// if child.is_full() {
// self.split_child(node, i);
// if self.should_go_right(&node.entries[i], leaf_value.version, leaf_value.ts) {
// i += 1;
// }
// }

// let mut child = (*node.children[i]).clone();
// self.insert_non_full(&mut child, leaf_value);
// node.children[i] = Arc::new(child);
// }
// }

fn should_go_right(&self, entry: &LeafValue<V>, version: u64, ts: u64) -> bool {
if version != entry.version {
version > entry.version
Expand Down Expand Up @@ -460,20 +498,16 @@ mod tests {
tree.insert(10, 1, ts);
tree.insert(20, 1, ts);

let val = tree
.iter()
.filter(|value| {
let should_include = value.ts <= ts;
if should_include {
println!("Version: {:?}, TS: {}", value.version, value.ts);
}
should_include
})
.max_by_key(|value| value.ts).unwrap();
println!("Max value: {:?}", val.value);

let max_val = tree
.iter()
.filter(|value| value.ts <= ts)
.max_by(|a, b| {
a.ts.cmp(&b.ts)
.then_with(|| Arc::as_ptr(a).cmp(&Arc::as_ptr(b)))
})
.unwrap();

assert_eq!(max_val.value, 20);
assert_eq!(tree.search(1, ts).map(|leaf| &leaf.value), Some(&20));
}

}

0 comments on commit 67735ba

Please sign in to comment.