Skip to content

Commit

Permalink
chore: add method to find max version in trie
Browse files Browse the repository at this point in the history
  • Loading branch information
arriqaaq committed Oct 28, 2024
1 parent 03f45a7 commit ebb722e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 19 deletions.
45 changes: 40 additions & 5 deletions src/art.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1323,15 +1323,17 @@ impl<P: KeyTrait, V: Clone> Tree<P, V> {
Node::get_recurse(root, key, QueryType::LatestByVersion(commit_version))
}

/// Retrieves the latest version of the Trie.
/// Retrieves the latest version inserted in the Trie.
///
/// This function returns the version of the latest version of the Trie. If the Trie is empty,
/// it returns `0`.
/// This function returns the latest version inserted in the Trie. This is used internally
/// for surrealkv for determining the transaction id. The version only moves forward in time
/// and does not get updated upon any removals. This behavior ensures that deletions do not
/// roll back version to read at an older snapshot, which is crucial for internal use in
/// SurrealKV for snapshot reads.
///
/// # Returns
///
/// Returns the version of the latest version of the Trie, or `0` if the Trie is empty.
///
/// Returns the version of the latest version of the Trie, or `0` if the Trie is empty.
pub fn version(&self) -> u64 {
self.version
}
Expand Down Expand Up @@ -1509,6 +1511,17 @@ impl<P: KeyTrait, V: Clone> Tree<P, V> {
{
query_keys_at_node(self.root.as_ref(), range, QueryType::LatestByTs(ts))
}

/// Retrieves the maximum version inside the Trie.
///
/// This function returns the maximum version found inside the Trie by traversing
/// all the nodes. This version could be lesser than the current incremental version.
pub fn max_version_in_trie(&self) -> u64 {
self.iter()
.map(|(_, _, version, _)| *version)
.max()
.unwrap_or(0)
}
}

/*
Expand Down Expand Up @@ -2040,6 +2053,28 @@ mod tests {
assert_eq!(tree.version(), 15);
}

#[test]
fn timed_deletion_check_root_ts() {
let mut tree: Tree<VariableSizeKey, i32> = Tree::<VariableSizeKey, i32>::new();
let key1 = VariableSizeKey::from_str("key_1").unwrap();
let key2 = VariableSizeKey::from_str("key_2").unwrap();

// Initial insertions
assert!(tree.insert(&key1, 1, 0, 0).is_ok());
assert!(tree.insert(&key2, 1, 0, 0).is_ok());
assert_eq!(tree.version(), 2);

// Deletions
assert!(tree.remove(&key1));
assert!(tree.remove(&key2));

// tree version should still be 2
assert_eq!(tree.version(), 2);

// max version in the tree should be 0 post deletions
assert_eq!(tree.max_version_in_trie(), 0);
}

fn from_be_bytes_key(k: &[u8]) -> u64 {
let padded_k = if k.len() < 8 {
let mut new_k = vec![0; 8];
Expand Down
14 changes: 0 additions & 14 deletions src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -626,20 +626,6 @@ mod tests {
use rand::prelude::SliceRandom;
use std::sync::Arc;

// macro_rules! impl_timestamp {
// ($($t:ty),*) => {
// $(
// impl Version for $t {
// fn version(&self) -> u64 {
// *self as u64
// }
// }
// )*
// };
// }

// impl_timestamp!(usize, u8, u16, u32, u64);

fn node_test<N: NodeTrait<usize>>(mut node: N, size: usize) {
for i in 0..size {
node.add_child(i as u8, i);
Expand Down

0 comments on commit ebb722e

Please sign in to comment.