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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version = "0.1.0"
authors = ["William Speirs <bill.speirs@gmail.com>"]

[dependencies]
rand = "*"
rand = "0.5.0"
bincode = "0.6.0"
rustc-serialize = "0.3.19"
itertools = "0.5.5"
26 changes: 21 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl <K: KeyType, V: ValueType> BTree<K, V> {
let mut wal_file = try!(RecordFile::<K,V>::new(&wal_file_path, key_size, value_size));

// if we have a WAL file, replay it into the mem_tree
if try!(wal_file.is_new()) {
if !try!(wal_file.is_new()) {
for kv in &mut wal_file {
mem_tree.insert(kv.key, kv.value);
}
Expand Down Expand Up @@ -204,10 +204,6 @@ mod tests {
// setup tree
let mut btree = BTree::<String, String>::new(&file_path, 15, 15).unwrap();

// expected return set
let mut expected: BTreeSet<String> = BTreeSet::new();
expected.insert("World".to_string());

btree.insert("Hello".to_owned(), "World".to_owned());

// get the set at the hello key
Expand All @@ -218,6 +214,26 @@ mod tests {
remove_files(file_path); // remove files assuming it all went well
}

#[test]
fn get_from_disk() {
let file_path = gen_temp_name();

// setup tree
let mut btree = BTree::<String, String>::new(&file_path, 15, 15).unwrap();
btree.insert("Hello".to_owned(), "World".to_owned());

// drop the btree and create a new one to ensure the `get` below happens from disk rather than cache
drop(btree);
let btree = BTree::<String, String>::new(&file_path, 15, 15).unwrap();

// get the set at the hello key
let set_at_hello: Vec<String> = btree.get(&"Hello".to_string()).expect("should read data").cloned().collect();

assert_eq!(set_at_hello, ["World".to_string()]);

remove_files(file_path); // remove files assuming it all went well
}

#[test]
fn insert_multiple() {
let file_path = gen_temp_name();
Expand Down
36 changes: 36 additions & 0 deletions src/wal_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,40 @@ mod tests {

fs::remove_file(&file_path);
}

#[test]
fn load_from_disk() {
let temp_path = gen_temp_name();
let file_path = temp_path.to_owned() + ".wal";

// create a new blank file
let mut wal_file = RecordFile::new(&file_path, 20, 20).unwrap();
assert!(wal_file.is_new().unwrap());

let kv1 = KeyValuePair{key: "hello".to_owned(), value: "world".to_owned()};
let kv2 = KeyValuePair{key: "foo".to_owned(), value: "bar".to_owned()};

wal_file.insert_record(&kv1).unwrap();
wal_file.insert_record(&kv2).unwrap();

// drop the above wal_file and create a new one
drop(wal_file);
let mut wal_file : RecordFile<String, String> = RecordFile::new(&file_path, 20, 20).unwrap();

assert!(wal_file.count().unwrap() == 2);

let mut wal_it = wal_file.into_iter();

let it_kv1 = wal_it.next().unwrap();

assert!(kv1.key == it_kv1.key);
assert!(kv1.value == it_kv1.value);

let it_kv2 = wal_it.next().unwrap();

assert!(kv2.key == it_kv2.key);
assert!(kv2.value == it_kv2.value);

fs::remove_file(&file_path);
}
}