diff --git a/src/lib.rs b/src/lib.rs index 79d0792..24f8761 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -46,10 +46,10 @@ impl BTree { let wal_file_path = tree_file_path.to_owned() + ".wal"; // construct our WAL file - let mut wal_file = try!(RecordFile::::new(&wal_file_path, key_size, value_size)); + let mut wal_file = RecordFile::::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); } @@ -71,14 +71,14 @@ impl BTree { 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( () ); @@ -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"); } diff --git a/src/multi_map.rs b/src/multi_map.rs index 8306cb1..ec8e521 100644 --- a/src/multi_map.rs +++ b/src/multi_map.rs @@ -25,17 +25,20 @@ impl <'a, K: KeyType, V: ValueType> MultiMap { } 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::::new(); set.insert(value); + self.count += 1; + self.multi_map.insert(key, set); return self.count; @@ -66,7 +69,7 @@ impl <'a, K: KeyType, V: ValueType> MultiMap { 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() { @@ -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); @@ -166,7 +169,7 @@ mod tests { #[test] fn test_get() { let mut mmap = MultiMap::::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); @@ -204,6 +207,12 @@ mod tests { assert!(it.next() == None); } -} - - + #[test] + fn test_size() { + let mut mmap = MultiMap::::new(); + mmap.insert(1, "abc".into()); + mmap.insert(1, "abc".into()); + mmap.delete(1, "abc".into()); + assert_eq!( mmap.size(),0) + } +} \ No newline at end of file