changeset 114:8944889f75bd

A bit of cleanup for table reader/filter blocks
author Lewin Bormann <lbo@spheniscida.de>
date Sat, 24 Dec 2016 18:09:27 +0000
parents 0d848c6665f5
children 48c612aff71c
files src/filter_block.rs src/table_builder.rs
diffstat 2 files changed, 18 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/src/filter_block.rs	Sat Dec 24 15:56:40 2016 +0000
+++ b/src/filter_block.rs	Sat Dec 24 18:09:27 2016 +0000
@@ -131,7 +131,6 @@
     }
 
     /// Returns the offset of the offset with index i.
-    #[inline]
     fn offset_of(&self, i: u32) -> usize {
         let offset_offset = self.offsets_offset + 4 * i as usize;
         u32::decode_fixed(&self.block[offset_offset..offset_offset + 4]) as usize
@@ -190,7 +189,7 @@
     fn test_filter_block_builder() {
         let result = produce_filter_block();
         // 2 blocks of 4 filters of 4 bytes plus 1B for `k`; plus three filter offsets (because of
-        //   the block offset of 5000); plus footer
+        //   the block offsets of 0 and 5000); plus footer
         assert_eq!(result.len(), 2 * (get_keys().len() * 4 + 1) + (3 * 4) + 5);
         assert_eq!(result,
                    vec![234, 195, 25, 155, 61, 141, 173, 140, 221, 28, 222, 92, 220, 112, 234,
@@ -204,13 +203,14 @@
         let reader = FilterBlockReader::new_owned(BloomPolicy::new(32), result);
 
         assert_eq!(reader.offset_of(get_filter_index(5121, FILTER_BASE_LOG2)),
-                   17); // third block in third filter\
+                   17); // third block in third filter
 
         let unknown_keys = vec!["xsb".as_bytes(), "9sad".as_bytes(), "assssaaaass".as_bytes()];
 
-        for block_offset in vec![0, 5000, 5, 5500].into_iter() {
+        for block_offset in vec![0, 1024, 5000, 6025].into_iter() {
             for key in get_keys().iter() {
-                assert!(reader.key_may_match(block_offset, key));
+                assert!(reader.key_may_match(block_offset, key),
+                        format!("{} {:?} ", block_offset, key));
             }
             for key in unknown_keys.iter() {
                 assert!(!reader.key_may_match(block_offset, key));
--- a/src/table_builder.rs	Sat Dec 24 15:56:40 2016 +0000
+++ b/src/table_builder.rs	Sat Dec 24 18:09:27 2016 +0000
@@ -136,6 +136,10 @@
     }
 }
 
+/// TableBuilder is used for building a new SSTable. It groups entries into blocks,
+/// calculating checksums and bloom filters.
+/// It's recommended that you use InternalFilterPolicy as FilterPol, as that policy extracts the
+/// underlying user keys from the InternalKeys used as keys in the table.
 impl<'a, C: Comparator, Dst: Write, FilterPol: FilterPolicy> TableBuilder<'a, C, Dst, FilterPol> {
     pub fn new(opt: Options,
                cmp: C,
@@ -180,6 +184,7 @@
 
     /// Writes an index entry for the current data_block where `next_key` is the first key of the
     /// next block.
+    /// Calls write_block() for writing the block to disk.
     fn write_data_block<'b>(&mut self, next_key: InternalKey<'b>) {
         assert!(self.data_block.is_some());
 
@@ -196,32 +201,35 @@
         self.data_block = Some(BlockBuilder::new(self.o, self.cmp));
 
         let ctype = self.o.compression_type;
-        self.write_block(contents, ctype);
 
+        // Use offset of block that the keys are in.
         if let Some(ref mut fblock) = self.filter_block {
             fblock.start_block(self.offset);
         }
+
+        self.write_block(contents, ctype);
     }
 
+    /// Calculates the checksum, writes the block to disk and updates the offset.
     fn write_block(&mut self, block: BlockContents, t: CompressionType) -> BlockHandle {
         // compression is still unimplemented
         assert_eq!(t, CompressionType::CompressionNone);
 
-        let mut buf = [0 as u8; 4];
+        let mut buf = [0 as u8; TABLE_BLOCK_CKSUM_LEN];
         let mut digest = crc32::Digest::new(crc32::CASTAGNOLI);
 
         digest.write(&block);
-        digest.write(&[self.o.compression_type as u8; 1]);
+        digest.write(&[self.o.compression_type as u8; TABLE_BLOCK_COMPRESS_LEN]);
         digest.sum32().encode_fixed(&mut buf);
 
         // TODO: Handle errors here.
         let _ = self.dst.write(&block);
-        let _ = self.dst.write(&[t as u8; 1]);
+        let _ = self.dst.write(&[t as u8; TABLE_BLOCK_COMPRESS_LEN]);
         let _ = self.dst.write(&buf);
 
         let handle = BlockHandle::new(self.offset, block.len());
 
-        self.offset += block.len() + 1 + buf.len();
+        self.offset += block.len() + TABLE_BLOCK_COMPRESS_LEN + TABLE_BLOCK_CKSUM_LEN;
 
         handle
     }