changeset 271:26d6f6e1da7d

table_builder: Implement size_estimate()
author Lewin Bormann <lbo@spheniscida.de>
date Thu, 21 Sep 2017 16:04:25 +0200
parents d726aad0bd5c
children 8b70eadcd6d2
files src/table_builder.rs
diffstat 1 files changed, 20 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/src/table_builder.rs	Thu Sep 21 16:04:11 2017 +0200
+++ b/src/table_builder.rs	Thu Sep 21 16:04:25 2017 +0200
@@ -128,6 +128,20 @@
         self.num_entries
     }
 
+    pub fn size_estimate(&self) -> usize {
+        let mut size = 0;
+        if let Some(ref b) = self.data_block {
+            size += b.size_estimate();
+        }
+        if let Some(ref b) = self.index_block {
+            size += b.size_estimate();
+        }
+        if let Some(ref b) = self.filter_block {
+            size += b.size_estimate();
+        }
+        size + self.offset + FULL_FOOTER_LENGTH
+    }
+
     /// Add a key to the table. The key as to be lexically greater or equal to the last one added.
     pub fn add<'a>(&mut self, key: InternalKey<'a>, val: &[u8]) -> Result<()> {
         assert!(self.data_block.is_some());
@@ -283,8 +297,13 @@
             b.add(&data2[i].0.as_bytes(), &data2[i].1.as_bytes()).unwrap();
         }
 
+        let estimate = b.size_estimate();
+
+        assert_eq!(143, estimate);
         assert!(b.filter_block.is_some());
-        b.finish().unwrap();
+
+        let actual = b.finish().unwrap();
+        assert_eq!(233, actual);
     }
 
     #[test]