changeset 51:b851387cdf9c

Tighten Table::get() API And reimplement size_estimate() methods for test
author Lewin Bormann <lbo@spheniscida.de>
date Wed, 14 Mar 2018 20:38:44 +0100
parents 2247f47ea794
children b557e42af582
files src/filter_block.rs src/table_builder.rs src/table_reader.rs
diffstat 3 files changed, 21 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/src/filter_block.rs	Mon Mar 12 19:30:57 2018 +0100
+++ b/src/filter_block.rs	Wed Mar 14 20:38:44 2018 +0100
@@ -46,6 +46,10 @@
         }
     }
 
+    pub fn size_estimate(&self) -> usize {
+        self.filters.len() + 4 * self.filter_offsets.len() + 4 + 1
+    }
+
     pub fn filter_name(&self) -> &'static str {
         self.policy.name()
     }
--- a/src/table_builder.rs	Mon Mar 12 19:30:57 2018 +0100
+++ b/src/table_builder.rs	Wed Mar 14 20:38:44 2018 +0100
@@ -122,6 +122,17 @@
         self.num_entries
     }
 
+    #[allow(unused)]
+    fn size_estimate(&self) -> usize {
+        let mut size = 0;
+        size += self.data_block.as_ref().map(|b| b.size_estimate()).unwrap_or(0);
+        size += self.index_block.as_ref().map(|b| b.size_estimate()).unwrap_or(0);
+        size += self.filter_block.as_ref().map(|b| b.size_estimate()).unwrap_or(0);
+        size += self.offset;
+        size += FULL_FOOTER_LENGTH;
+        size
+    }
+
     /// Add a key to the table. The key must be lexically greater or equal to the one that was
     /// previously added.
     pub fn add(&mut self, key: &[u8], val: &[u8]) -> Result<()> {
--- a/src/table_reader.rs	Mon Mar 12 19:30:57 2018 +0100
+++ b/src/table_reader.rs	Wed Mar 14 20:38:44 2018 +0100
@@ -161,18 +161,10 @@
         iter
     }
 
-    /// Retrieve next-biggest entry for key from table. This function uses the attached filters, so
+    /// Retrieve an entry for a key from the table. This function uses the attached filters, so
     /// is better suited if you frequently look for non-existing values (as it will detect the
     /// non-existence of an entry in a block without having to load the block).
-    ///
-    /// The caller must check if the returned key, which is the raw key (not e.g. the user portion
-    /// of an InternalKey) is acceptable (e.g. correct value type or sequence number), as it may
-    /// not be an exact match for key.
-    ///
-    /// This is done this way because some key types, like internal keys, will not result in an
-    /// exact match; it depends on other comparators than the one that the table reader knows
-    /// whether a match is acceptable.
-    pub fn get(&self, key: &[u8]) -> Result<Option<(Vec<u8>, Vec<u8>)>> {
+    pub fn get(&self, key: &[u8]) -> Result<Option<Vec<u8>>> {
         let mut index_iter = self.indexblock.iter();
         index_iter.seek(key);
 
@@ -203,8 +195,8 @@
         // Go to entry and check if it's the wanted entry.
         iter.seek(key);
         if let Some((k, v)) = current_key_val(&iter) {
-            if self.opt.cmp.cmp(&k, key) >= Ordering::Equal {
-                return Ok(Some((k, v)));
+            if self.opt.cmp.cmp(&k, key) == Ordering::Equal {
+                return Ok(Some(v));
             }
         }
         Ok(None)
@@ -628,7 +620,7 @@
         // Test that all of the table's entries are reachable via get()
         for (k, v) in SSIteratorIter::wrap(&mut _iter) {
             let r = table2.get(&k);
-            assert_eq!(Ok(Some((k, v))), r);
+            assert_eq!(Ok(Some(v)), r);
         }
 
         assert_eq!(table.opt.block_cache.borrow().count(), 3);
@@ -639,6 +631,7 @@
         assert!(table.get(b"aa").unwrap().is_none());
         assert!(table.get(b"abcd").unwrap().is_none());
         assert!(table.get(b"abb").unwrap().is_none());
+        assert!(table.get(b"xyy").unwrap().is_none());
         assert!(table.get(b"zzy").unwrap().is_none());
         assert!(table.get(b"zz1").unwrap().is_none());
         assert!(table.get("zz{".as_bytes()).unwrap().is_none());