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
11 changes: 6 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ impl <K: KeyType, V: ValueType> BTree<K, V> {
let wal_file_path = tree_file_path.to_owned() + ".wal";

// construct our WAL file
let mut wal_file = try!(RecordFile::<K,V>::new(&wal_file_path, key_size, value_size));
let mut wal_file = 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 wal_file.is_new()? {
for kv in &mut wal_file {
mem_tree.insert(kv.key, kv.value);
}
Expand All @@ -71,14 +71,14 @@ impl <K: KeyType, V: ValueType> BTree<K, V> {
let record = KeyValuePair{key: key, value: value};

// should wrap this in a read-write lock
try!(self.wal_file.insert_record(&record));
self.wal_file.insert_record(&record)?;

let KeyValuePair{key, value} = record;

let size = self.mem_tree.insert(key, value);

if size > MAX_MEMORY_ITEMS {
try!(self.compact());
self.compact()?;
}

return Ok( () );
Expand Down Expand Up @@ -117,9 +117,10 @@ mod tests {
use ::BTree;
use rand::{thread_rng, Rng};
use std::collections::BTreeSet;
use rand::distributions::Alphanumeric;

pub fn gen_temp_name() -> String {
let file_name: String = thread_rng().gen_ascii_chars().take(10).collect();
let file_name: String = thread_rng().sample_iter(&Alphanumeric).take(10).collect();

return String::from("/tmp/") + &file_name + &String::from(".btr");
}
Expand Down
29 changes: 19 additions & 10 deletions src/multi_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,20 @@ impl <'a, K: KeyType, V: ValueType> MultiMap<K,V> {
}

pub fn insert(&mut self, key: K, value: V) -> usize {
self.count += 1;

if let Some(set) = self.multi_map.get_mut(&key) {
set.insert(value);
if set.insert(value) {
self.count +=1;
}
return self.count;
}

let mut set = BTreeSet::<V>::new();

set.insert(value);

self.count += 1;

self.multi_map.insert(key, set);

return self.count;
Expand Down Expand Up @@ -66,7 +69,7 @@ impl <'a, K: KeyType, V: ValueType> MultiMap<K,V> {
if let Occupied(mut entry) = self.multi_map.entry(key) {

if entry.get_mut().remove(&value) {
self.count -= 1;
self.count -= 1;
}

if entry.get().is_empty() {
Expand Down Expand Up @@ -153,11 +156,11 @@ mod tests {
let e1 = it.next().unwrap();
assert!(12 == e1.key);
assert!(String::from("abc") == e1.value);

let e2 = it.next().unwrap();
assert!(23 == e2.key);
assert!(String::from("abc") == e2.value);

let e3 = it.next().unwrap();
assert!(23 == e3.key);
assert!(String::from("def") == e3.value);
Expand All @@ -166,7 +169,7 @@ mod tests {
#[test]
fn test_get() {
let mut mmap = MultiMap::<i32,String>::new();

assert!(mmap.insert(12, String::from("abc")) == 1);
assert!(mmap.insert(23, String::from("abc")) == 2);
assert!(mmap.insert(23, String::from("def")) == 3);
Expand Down Expand Up @@ -204,6 +207,12 @@ mod tests {

assert!(it.next() == None);
}
}


#[test]
fn test_size() {
let mut mmap = MultiMap::<i32, String>::new();
mmap.insert(1, "abc".into());
mmap.insert(1, "abc".into());
mmap.delete(1, "abc".into());
assert_eq!( mmap.size(),0)
}
}