Skip to content

Commit

Permalink
Merge pull request #27 from cowlicks/add-debug-feature
Browse files Browse the repository at this point in the history
Add debug feature and move `.print` & `.height` methods into it.
  • Loading branch information
cowlicks authored Mar 31, 2024
2 parents e91369f + 743a46c commit 0e1118d
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Removed

- `Hyperbee::print` and `Hyperbee::height` are removed from the default API. They can still be accessed by enabling the `debug` feature.
- `CoreMem` trait and all usage of it.


Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ required-features = ["ffi"]
default = ["ffi"]
clib = ["tokio/rt-multi-thread", "dep:libc"]
ffi = ["clib", "dep:uniffi"]
debug = []

[dependencies]
async-trait = "0.1.77"
Expand Down
2 changes: 2 additions & 0 deletions src/hb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,13 @@ impl Hyperbee {
self.tree.read().await.create_header(metadata).await
}

#[cfg(feature = "debug")]
/// The number of levels in the tree
pub async fn height(&self) -> Result<usize, HyperbeeError> {
self.tree.read().await.height().await
}

#[cfg(feature = "debug")]
/// Returs a string representing the structure of the tree showing the keys in each node
pub async fn print(&self) -> Result<String, HyperbeeError> {
self.tree.read().await.print().await
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ impl Node {
self.n_children().await == 0
}

#[cfg(feature = "debug")]
/// The number of children between this node and a leaf + 1
pub async fn height(&self) -> Result<usize, HyperbeeError> {
if self.is_leaf().await {
Expand Down
3 changes: 1 addition & 2 deletions src/put.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ mod test {
Ok(())
}

#[cfg(feature = "debug")]
#[tokio::test]
async fn print_put() -> Result<(), Box<dyn std::error::Error>> {
let hb = Tree::from_ram().await?;
Expand Down Expand Up @@ -317,7 +318,6 @@ mod test {
let val = Some(key.clone());
hb.put(&key, val.as_deref()).await?;
hb = check_tree(hb).await?;
let _ = hb.print().await?;

for j in 0..(i + 1) {
let js = j.to_string();
Expand Down Expand Up @@ -352,7 +352,6 @@ mod test {
}

hb = check_tree(hb).await?;
let _ = hb.print().await?;
}
Ok(())
}
Expand Down
2 changes: 2 additions & 0 deletions src/traverse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,8 +447,10 @@ impl<'a> Stream for Traverse<'a> {
}
}

#[cfg(feature = "debug")]
static LEADER: &str = "\t";

#[cfg(feature = "debug")]
/// Print the keys of the provided node and it's descendents as a tree
pub(crate) async fn print(node: SharedNode) -> Result<String, HyperbeeError> {
let starting_height = node.read().await.height().await?;
Expand Down
7 changes: 5 additions & 2 deletions src/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
error::HyperbeeError,
messages::{header::Metadata, Header},
nearest_node,
traverse::{self, KeyDataResult, Traverse, TraverseConfig},
traverse::{KeyDataResult, Traverse, TraverseConfig},
Node, Shared, PROTOCOL,
};
use std::{
Expand Down Expand Up @@ -57,6 +57,7 @@ impl Tree {
Ok(Some(root))
}

#[cfg(feature = "debug")]
pub async fn height(&self) -> Result<usize, HyperbeeError> {
let Some(root) = self.get_root(false).await? else {
// When there is no root, return zero.
Expand Down Expand Up @@ -119,13 +120,14 @@ impl Tree {
self.blocks.read().await.append(&buf).await
}

#[cfg(feature = "debug")]
/// Returs a string representing the structure of the tree showing the keys in each node
pub async fn print(&self) -> Result<String, HyperbeeError> {
let root = self
.get_root(false)
.await?
.ok_or(HyperbeeError::NoRootError)?;
let out = traverse::print(root).await?;
let out = crate::traverse::print(root).await?;
Ok(out)
}

Expand Down Expand Up @@ -183,6 +185,7 @@ impl Clone for Tree {
}
}

#[cfg(feature = "debug")]
#[cfg(test)]
mod test {
use super::*;
Expand Down
10 changes: 8 additions & 2 deletions tests/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ use hyperbee::{traverse::TraverseConfig, Hyperbee};

use crate::common::js::require_js_data;
static BASIC_TEST_DATA_STORAGE: &str = "basic";
static MORE_HEIGHT_TEST_DATA_STORAGE: &str = "more_height";
static SMALL_TEST_DATA_STORAGE: &str = "alphabet";

#[tokio::test]
async fn basic_integration() -> Result<()> {
Expand Down Expand Up @@ -65,6 +63,10 @@ async fn stream() -> Result<()> {
Ok(())
}

#[cfg(feature = "debug")]
static MORE_HEIGHT_TEST_DATA_STORAGE: &str = "more_height";

#[cfg(feature = "debug")]
#[tokio::test]
async fn height() -> Result<()> {
require_js_data()?;
Expand All @@ -75,6 +77,10 @@ async fn height() -> Result<()> {
Ok(())
}

#[cfg(feature = "debug")]
static SMALL_TEST_DATA_STORAGE: &str = "alphabet";

#[cfg(feature = "debug")]
#[tokio::test]
async fn print() -> Result<()> {
require_js_data()?;
Expand Down
15 changes: 11 additions & 4 deletions tests/disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,12 @@ async fn order_via_depth_first_search() -> Result<()> {
last_i
);
let (hb, jsdir, rsdir) = put_rs_and_js_range!(keys.clone().into_iter(), extra_js);
let jshb = Hyperbee::from_storage_dir(&jsdir).await?;
hb.put(&last, Some(&last)).await?;
assert_eq!(hb.print().await?, jshb.print().await?);
#[cfg(feature = "debug")]
{
let jshb = Hyperbee::from_storage_dir(&jsdir).await?;
assert_eq!(hb.print().await?, jshb.print().await?);
}

diff_dirs(&jsdir, &rsdir)?;
Ok(())
Expand Down Expand Up @@ -222,8 +225,12 @@ async fn big_random_test() -> Result<()> {
hb.del(&key).await?;
}

let jshb = Hyperbee::from_storage_dir(&jsdir).await?;
assert_eq!(hb.print().await?, jshb.print().await?);
#[cfg(feature = "debug")]
{
let jshb = Hyperbee::from_storage_dir(&jsdir).await?;
assert_eq!(hb.print().await?, jshb.print().await?);
}

diff_dirs(&jsdir, &rsdir)?;
Ok(())
}

0 comments on commit 0e1118d

Please sign in to comment.