Skip to content
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
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@ path = "src/lib.rs"
byteorder = "1.3.4"
uuid = { version = "0.8", features = ["serde", "v4"] }
memmap = "0.7.0"

[dev-dependencies]
tempfile = { version = "3" }
47 changes: 21 additions & 26 deletions src/btree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,18 +415,16 @@ impl BTree {

#[cfg(test)]
mod tests {
use crate::btree::{BTree, BTreeBuilder};
use crate::error::Error;
use tempfile::TempDir;

#[test]
fn search_works() -> Result<(), Error> {
use crate::btree::BTreeBuilder;
use crate::node_type::KeyValuePair;
use std::path::Path;

let mut btree = BTreeBuilder::new()
.path(Path::new("/tmp/db"))
.b_parameter(2)
.build()?;
let dir = tempfile::tempdir()?; // Deleted on drop.
let mut btree = create_temp_btree(dir)?;
btree.insert(KeyValuePair::new("a".to_string(), "shalom".to_string()))?;
btree.insert(KeyValuePair::new("b".to_string(), "hello".to_string()))?;
btree.insert(KeyValuePair::new("c".to_string(), "marhaba".to_string()))?;
Expand All @@ -444,14 +442,10 @@ mod tests {

#[test]
fn insert_works() -> Result<(), Error> {
use crate::btree::BTreeBuilder;
use crate::node_type::KeyValuePair;
use std::path::Path;

let mut btree = BTreeBuilder::new()
.path(Path::new("/tmp/db"))
.b_parameter(2)
.build()?;
let dir = tempfile::tempdir()?; // Deleted on drop.
let mut btree = create_temp_btree(dir)?;
btree.insert(KeyValuePair::new("a".to_string(), "shalom".to_string()))?;
btree.insert(KeyValuePair::new("b".to_string(), "hello".to_string()))?;
btree.insert(KeyValuePair::new("c".to_string(), "marhaba".to_string()))?;
Expand Down Expand Up @@ -502,15 +496,11 @@ mod tests {

#[test]
fn delete_works() -> Result<(), Error> {
use crate::btree::BTreeBuilder;
use crate::error::Error;
use crate::node_type::{Key, KeyValuePair};
use std::path::Path;

let mut btree = BTreeBuilder::new()
.path(Path::new("/tmp/db"))
.b_parameter(2)
.build()?;
let dir = tempfile::tempdir()?; // Deleted on drop.
let mut btree = create_temp_btree(dir)?;
btree.insert(KeyValuePair::new("d".to_string(), "olah".to_string()))?;
btree.insert(KeyValuePair::new("e".to_string(), "salam".to_string()))?;
btree.insert(KeyValuePair::new("f".to_string(), "hallo".to_string()))?;
Expand Down Expand Up @@ -547,14 +537,10 @@ mod tests {

#[test]
fn delete_with_empty_sub_tree() -> Result<(), Error> {
use crate::btree::BTreeBuilder;
use crate::node_type::{Key, KeyValuePair};
use std::path::Path;

let mut btree = BTreeBuilder::new()
.path(Path::new("/tmp/db"))
.b_parameter(2)
.build()?;
let dir = tempfile::tempdir()?; // Deleted on drop.
let mut btree = create_temp_btree(dir)?;
btree.insert(KeyValuePair::new("a".to_string(), "shalom".to_string()))?;
btree.insert(KeyValuePair::new("b".to_string(), "hello".to_string()))?;
btree.insert(KeyValuePair::new("c".to_string(), "marhaba".to_string()))?;
Expand All @@ -576,11 +562,11 @@ mod tests {
btree.delete(Key("a".to_string()))?;
res = btree.search("a".to_string());
assert!(matches!(res, Err(Error::KeyNotFound)));

btree.delete(Key("b".to_string()))?;
res = btree.search("b".to_string());
assert!(matches!(res, Err(Error::KeyNotFound)));

btree.delete(Key("c".to_string()))?;
res = btree.search("c".to_string());
assert!(matches!(res, Err(Error::KeyNotFound)));
Expand All @@ -594,4 +580,13 @@ mod tests {
assert!(matches!(res, Err(Error::KeyNotFound)));
Ok(())
}

fn create_temp_btree(dir: TempDir) -> Result<BTree, Error> {
let db_path = dir.path().join("db");
let btree = BTreeBuilder::new()
.path(Box::leak(db_path.into_boxed_path()))
.b_parameter(2)
.build()?;
Ok(btree)
}
}