Skip to content

Commit

Permalink
remove: keys_at_ts (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
arriqaaq authored Jan 9, 2025
1 parent 6243aa8 commit 85d01c4
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 146 deletions.
134 changes: 1 addition & 133 deletions src/art.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::ops::RangeBounds;
use std::sync::Arc;

use crate::iter::IterItem;
use crate::iter::{query_keys_at_node, scan_node, Iter, Range};
use crate::iter::{scan_node, Iter, Range};
use crate::node::{FlatNode, LeafValue, Node256, Node48, NodeTrait, TwigNode};
use crate::{KeyTrait, TrieError};

Expand Down Expand Up @@ -1719,13 +1719,6 @@ impl<P: KeyTrait, V: Clone> Tree<P, V> {
scan_node(self.root.as_ref(), range, QueryType::LatestByTs(ts))
}

pub fn keys_at_ts<'a, R>(&'a self, range: R, ts: u64) -> impl Iterator<Item = &'a [u8]> + 'a
where
R: RangeBounds<P> + 'a,
{
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
Expand Down Expand Up @@ -3451,131 +3444,6 @@ mod tests {
assert_eq!(values[0].1, &42);
}

#[test]
fn keys_at_empty_range() {
let tree: Tree<VariableSizeKey, i32> = Tree::new();
let keys: Vec<_> = tree.keys_at_ts(RangeFull {}, 0).collect();
assert!(keys.is_empty());
}

#[test]
fn keys_at_range_includes_some_keys() {
let mut tree: Tree<VariableSizeKey, i32> = Tree::new();
let keys_to_insert = ["key_1", "key_2", "key_3"];
for key in keys_to_insert.iter() {
tree.insert(&VariableSizeKey::from_str(key).unwrap(), 1, 0, 0)
.unwrap();
}
let range = VariableSizeKey::from_slice("key_1".as_bytes())
..=VariableSizeKey::from_slice("key_2".as_bytes());
let keys: Vec<_> = tree.keys_at_ts(range, 0).collect();
assert_eq!(keys.len(), 2);
}

#[test]
fn keys_at_range_includes_all_keys() {
let mut tree: Tree<VariableSizeKey, i32> = Tree::new();
let keys_to_insert = ["key_1", "key_2", "key_3"];
for key in keys_to_insert.iter() {
tree.insert(&VariableSizeKey::from_str(key).unwrap(), 1, 0, 0)
.unwrap();
}
let keys: Vec<_> = tree.keys_at_ts(RangeFull {}, 0).collect();
assert_eq!(keys.len(), keys_to_insert.len());
}

#[test]
fn keys_at_range_includes_no_keys() {
let mut tree: Tree<VariableSizeKey, i32> = Tree::new();
let keys_to_insert = ["key_1", "key_2", "key_3"];
for key in keys_to_insert.iter() {
tree.insert(&VariableSizeKey::from_str(key).unwrap(), 1, 0, 0)
.unwrap();
}
let range = VariableSizeKey::from("key_4".as_bytes().to_vec())
..VariableSizeKey::from("key_5".as_bytes().to_vec());
let keys: Vec<_> = tree.keys_at_ts(range, 0).collect();
assert!(keys.is_empty());
}

#[test]
fn keys_at_different_timestamps() {
let mut tree: Tree<VariableSizeKey, i32> = Tree::new();
let keys_to_insert = ["key_1", "key_2", "key_3"];
for (i, key) in keys_to_insert.iter().enumerate() {
tree.insert(
&VariableSizeKey::from_str(key).unwrap(),
i as i32,
0,
i as u64,
)
.unwrap();
}
for (i, _) in keys_to_insert.iter().enumerate() {
let keys: Vec<_> = tree.keys_at_ts(RangeFull {}, i as u64).collect();
assert_eq!(keys.len(), i + 1);
}
}

#[test]
fn keys_at_large_number_of_inserts() {
let mut tree: Tree<VariableSizeKey, u64> = Tree::new();
let num_keys = 10000u64; // Large number of keys
let mut expected_keys = Vec::new();
for i in 0..num_keys {
let key = format!("key_{}", i);
expected_keys.push(VariableSizeKey::from_str(&key).unwrap());
tree.insert(&VariableSizeKey::from_str(&key).unwrap(), i, 0, i)
.unwrap();
}

let keys: Vec<_> = tree.keys_at_ts(RangeFull {}, num_keys).collect();
assert_eq!(keys.len(), num_keys as usize); // Expect all keys to be visible

// Sort the expected keys lexicographically
expected_keys.sort();

// Verify each key is proper
for (expected_key, key) in expected_keys.iter().zip(keys.iter()) {
assert_eq!(*key, expected_key.to_slice());
}
}

#[test]
fn keys_at_various_timestamps() {
let mut tree: Tree<VariableSizeKey, i32> = Tree::new();
let keys_to_insert = ["key_1", "key_2", "key_3"];
for (i, key) in keys_to_insert.iter().enumerate() {
let timestamp = ((i + 1) * 2) as u64;
tree.insert(
&VariableSizeKey::from_str(key).unwrap(),
i as i32,
0,
timestamp,
)
.unwrap();
}

let keys_before: Vec<_> = tree.keys_at_ts(RangeFull {}, 0).collect();
assert!(keys_before.is_empty());

let keys_mid: Vec<_> = tree.keys_at_ts(RangeFull {}, 4).collect();
assert_eq!(keys_mid.len(), 2); // Expect first two keys to be visible

let keys_after: Vec<_> = tree.keys_at_ts(RangeFull {}, 7).collect();
assert_eq!(keys_after.len(), keys_to_insert.len()); // Expect all keys to be visible
}

#[test]
fn keys_at_with_single_item_in_snapshot() {
let mut tree: Tree<VariableSizeKey, i32> = Tree::new();
tree.insert(&VariableSizeKey::from_str("key_1").unwrap(), 42, 0, 0)
.unwrap();

let keys: Vec<_> = tree.keys_at_ts(RangeFull {}, 0).collect();
assert_eq!(keys.len(), 1);
}

#[test]
fn snapshot_isolation() {
let mut tree: Tree<VariableSizeKey, i32> = Tree::<VariableSizeKey, i32>::new();
Expand Down
13 changes: 0 additions & 13 deletions src/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -536,19 +536,6 @@ where
QueryIterator::new(node, range, query_type)
}

pub(crate) fn query_keys_at_node<'a, K, V, R>(
node: Option<&'a Arc<Node<K, V>>>,
range: R,
query_type: QueryType,
) -> impl Iterator<Item = &'a [u8]> + 'a
where
K: KeyTrait + 'a,
V: Clone,
R: RangeBounds<K> + 'a,
{
QueryIterator::new(node, range, query_type).map(|(k, _, _, _)| k)
}

pub(crate) struct QueryIterator<'a, K: KeyTrait, V: Clone, R: RangeBounds<K>> {
forward: ForwardIterState<'a, K, V>,
prefix: Vec<u8>,
Expand Down

0 comments on commit 85d01c4

Please sign in to comment.