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

Return iter in get_all_versions #81

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
8 changes: 0 additions & 8 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,3 @@ jobs:

- name: Format
run: cargo fmt --all -- --check

semver:
name: semver
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Check semver
uses: obi1kenobi/cargo-semver-checks-action@v2
3 changes: 3 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ jobs:
chmod +x taplo
sudo mv taplo /usr/bin/taplo

- name: Check semver
uses: obi1kenobi/cargo-semver-checks-action@v2

- name: Configure git
run: |
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
Expand Down
97 changes: 47 additions & 50 deletions src/art.rs
Original file line number Diff line number Diff line change
Expand Up @@ -703,20 +703,6 @@ impl<P: KeyTrait, V: Clone> Node<P, V> {
twig.get_leaf_by_query(query_type)
}

#[inline]
pub(crate) fn get_all_versions(&self) -> Option<Vec<(V, u64, u64)>> {
// Unwrap the NodeType::Twig to access the TwigNode instance.
let NodeType::Twig(twig) = &self.node_type else {
return None;
};

// Get the value from the TwigNode instance by the specified version.
let val = twig.get_all_versions();

// Return the retrieved key, value, and version as a tuple.
Some(val)
}

#[allow(unused)]
pub(crate) fn node_type_name(&self) -> String {
match &self.node_type {
Expand Down Expand Up @@ -1108,11 +1094,20 @@ impl<P: KeyTrait, V: Clone> Node<P, V> {
Some((val.value.clone(), val.version, val.ts))
}

pub(crate) fn get_version_history(
cur_node: &Node<P, V>,
pub(crate) fn get_version_history<'a>(
cur_node: &'a Node<P, V>,
key: &P,
) -> Option<Vec<(V, u64, u64)>> {
Self::navigate_to_node(cur_node, key).and_then(|cur_node| cur_node.get_all_versions())
) -> impl Iterator<Item = IterItem<'a, V>> {
Self::navigate_to_node(cur_node, key)
.and_then(|node| {
if let NodeType::Twig(twig) = &node.node_type {
Some(twig.get_all_versions())
} else {
None
}
})
.into_iter()
.flatten()
}

/// Returns an iterator that iterates over child nodes of the current node.
Expand Down Expand Up @@ -1656,22 +1651,14 @@ impl<P: KeyTrait, V: Clone> Tree<P, V> {
///
/// This function searches for all versions of the specified key in the Trie and returns
/// a vector of tuples containing the value, version number, and timestamp for each version.
///
/// # Arguments
///
/// * `key`: A reference to the key to be searched.
///
/// # Returns
///
/// Returns an `Option` containing a vector of tuples `(V, u64, u64)` if the key is found:
/// - `V`: The value associated with the key.
/// - `u64`: The version number of the value.
/// - `u64`: The timestamp of the value.
///
/// Returns `None` if the key is not found.
pub fn get_version_history(&self, key: &P) -> Option<Vec<(V, u64, u64)>> {
let root = self.root.as_ref()?;
Node::get_version_history(root, key)
pub fn get_version_history<'a>(
&'a self,
key: &'a P,
) -> impl Iterator<Item = IterItem<'a, V>> + 'a {
self.root
.as_ref()
.into_iter()
.flat_map(move |root| Node::get_version_history(root, key))
}

/// Retrieves the value associated with the given key based on the specified query type.
Expand Down Expand Up @@ -3032,16 +3019,16 @@ mod tests {
tree.insert(&key1, value1_1, 0, ts1_1).unwrap();
tree.insert(&key1, value1_2, 0, ts1_2).unwrap();

let history1 = tree.get_version_history(&key1).unwrap();
let history1: Vec<_> = tree.get_version_history(&key1).collect();
assert_eq!(history1.len(), 2);

let (retrieved_value1_1, v1_1, t1_1) = history1[0];
assert_eq!(retrieved_value1_1, value1_1);
let (_, retrieved_value1_1, v1_1, t1_1) = history1[0];
assert_eq!(retrieved_value1_1, &value1_1);
assert_eq!(v1_1, 1);
assert_eq!(t1_1, ts1_1);

let (retrieved_value1_2, v1_2, t1_2) = history1[1];
assert_eq!(retrieved_value1_2, value1_2);
let (_, retrieved_value1_2, v1_2, t1_2) = history1[1];
assert_eq!(retrieved_value1_2, &value1_2);
assert_eq!(v1_2, 2);
assert_eq!(t1_2, ts1_2);

Expand All @@ -3051,17 +3038,18 @@ mod tests {
let ts2 = 300;
tree.insert(&key2, value2, 0, ts2).unwrap();

let history2 = tree.get_version_history(&key2).unwrap();
let history2: Vec<_> = tree.get_version_history(&key2).collect();
assert_eq!(history2.len(), 1);

let (retrieved_value2, v2, t2) = history2[0];
assert_eq!(retrieved_value2, value2);
let (_, retrieved_value2, v2, t2) = history2[0];
assert_eq!(retrieved_value2, &value2);
assert_eq!(v2, 3);
assert_eq!(t2, ts2);

// Scenario 3: Ensure no history for a non-existent key
// Scenario 3: Ensure empty iterator for a non-existent key
let key3 = VariableSizeKey::from_str("non_existent_key").unwrap();
assert!(tree.get_version_history(&key3).is_none());
let history3: Vec<_> = tree.get_version_history(&key3).collect();
assert!(history3.is_empty());
}

#[test]
Expand Down Expand Up @@ -3294,9 +3282,12 @@ mod tests {
tree.insert_or_replace(&key, 1, 10, 100).unwrap();
tree.insert_or_replace(&key, 2, 20, 200).unwrap();

let history = tree.get_version_history(&key).unwrap();
let history: Vec<_> = tree.get_version_history(&key).collect();
assert_eq!(history.len(), 1);
assert_eq!(history[0], (2, 20, 200));
let (_, value, version, ts) = &history[0];
assert_eq!(**value, 2);
assert_eq!(*version, 20);
assert_eq!(*ts, 200);
}

#[test]
Expand All @@ -3308,18 +3299,24 @@ mod tests {
tree.insert_or_replace_unchecked(&key, 1, 10, 100).unwrap();
tree.insert_or_replace_unchecked(&key, 2, 20, 200).unwrap();

let history = tree.get_version_history(&key).unwrap();
let history: Vec<_> = tree.get_version_history(&key).collect();
assert_eq!(history.len(), 1);
assert_eq!(history[0], (2, 20, 200));
let (_, value, version, ts) = &history[0];
assert_eq!(**value, 2);
assert_eq!(*version, 20);
assert_eq!(*ts, 200);

// Scenario 2: the new value has the smaller version and hence
// is older than the one already in the tree. Discard the new
// value.
tree.insert_or_replace_unchecked(&key, 1, 1, 1).unwrap();

let history = tree.get_version_history(&key).unwrap();
let history: Vec<_> = tree.get_version_history(&key).collect();
assert_eq!(history.len(), 1);
assert_eq!(history[0], (2, 20, 200));
let (_, value, version, ts) = &history[0];
assert_eq!(**value, 2);
assert_eq!(*version, 20);
assert_eq!(*ts, 200);
}

#[test]
Expand Down
20 changes: 13 additions & 7 deletions src/node.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::slice::from_ref;
use std::sync::Arc;

use crate::{art::QueryType, KeyTrait};
use crate::{art::QueryType, iter::IterItem, KeyTrait};

/*
Immutable nodes
Expand Down Expand Up @@ -196,11 +196,10 @@ impl<K: KeyTrait + Clone, V: Clone> TwigNode<K, V> {
}

#[inline]
pub(crate) fn get_all_versions(&self) -> Vec<(V, u64, u64)> {
pub(crate) fn get_all_versions(&self) -> impl Iterator<Item = IterItem<'_, V>> {
self.values
.iter()
.map(|value| (value.value.clone(), value.version, value.ts))
.collect()
.map(move |value| (self.key.as_slice(), &value.value, value.version, value.ts))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently returning the key to keep the interface uniform by returning the same IterItem everywhere. Do you think it is required here @gsserge ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think key is required here. However, if this operation will participate in merging then we might keep the key to keep the iterators uniform - it's just a slice ref, not a copy. Otherwise let's drop the key.

}

#[inline]
Expand Down Expand Up @@ -1049,10 +1048,17 @@ mod tests {
node.insert_mut(50, 200, 10); // value: 50, version: 200, timestamp: 10
node.insert_mut(51, 201, 20); // value: 51, version: 201, timestamp: 20

let versions = node.get_all_versions();
let versions: Vec<_> = node.get_all_versions().collect();
assert_eq!(versions.len(), 2);
assert_eq!(versions[0], (50, 200, 10));
assert_eq!(versions[1], (51, 201, 20));
let (_, value, version, ts) = &versions[0];
assert_eq!(**value, 50);
assert_eq!(*version, 200);
assert_eq!(*ts, 10);

let (_, value, version, ts) = &versions[1];
assert_eq!(**value, 51);
assert_eq!(*version, 201);
assert_eq!(*ts, 20);
}

#[test]
Expand Down
Loading