Mercurial > lbo > hg > leveldb-rs
changeset 39:e38f5617df6f
Move BlockHandle into its own module
author | Lewin Bormann <lbo@spheniscida.de> |
---|---|
date | Sun, 12 Jun 2016 20:05:40 +0200 |
parents | 6ccabb8a96f4 |
children | 30947e6a9cee 05a99b5b895c |
files | src/blockhandle.rs src/lib.rs src/types.rs |
diffstat | 3 files changed, 70 insertions(+), 66 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/blockhandle.rs Sun Jun 12 20:05:40 2016 +0200 @@ -0,0 +1,69 @@ +use integer_encoding::VarInt; + +/// Contains an offset and a length (or size); can be efficiently encoded in to varints. This is +/// used typically as file-internal pointer in table (SSTable) files. For example, the index block +/// in an SSTable is a block of (key = largest key in block) -> (value = encoded blockhandle of +/// block). +pub struct BlockHandle { + offset: usize, + size: usize, +} + +impl BlockHandle { + /// Decodes a block handle from `from` and returns a block handle + /// together with how many bytes were read from the slice. + pub fn decode(from: &[u8]) -> (BlockHandle, usize) { + let (off, offsize) = usize::decode_var(from); + let (sz, szsize) = usize::decode_var(&from[offsize..]); + + (BlockHandle { + offset: off, + size: sz, + }, + offsize + szsize) + } + + pub fn new(offset: usize, size: usize) -> BlockHandle { + BlockHandle { + offset: offset, + size: size, + } + } + + pub fn offset(&self) -> usize { + self.offset + } + + pub fn size(&self) -> usize { + self.size + } + + /// Returns how many bytes were written, or 0 if the write failed because `dst` is too small. + pub fn encode_to(&self, dst: &mut [u8]) -> usize { + if dst.len() < self.offset.required_space() + self.size.required_space() { + 0 + } else { + let off = self.offset.encode_var(dst); + off + self.size.encode_var(&mut dst[off..]) + } + + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_blockhandle() { + let bh = BlockHandle::new(890, 777); + let mut dst = [0 as u8; 128]; + let enc_sz = bh.encode_to(&mut dst[..]); + + let (bh2, dec_sz) = BlockHandle::decode(&dst); + + assert_eq!(enc_sz, dec_sz); + assert_eq!(bh.size(), bh2.size()); + assert_eq!(bh.offset(), bh2.offset()); + } +}
--- a/src/lib.rs Sun Jun 12 20:02:12 2016 +0200 +++ b/src/lib.rs Sun Jun 12 20:05:40 2016 +0200 @@ -6,6 +6,7 @@ extern crate integer_encoding; mod block; +mod blockhandle; mod log; mod memtable; mod skipmap;
--- a/src/types.rs Sun Jun 12 20:02:12 2016 +0200 +++ b/src/types.rs Sun Jun 12 20:05:40 2016 +0200 @@ -1,8 +1,6 @@ use std::default::Default; use std::cmp::Ordering; -use integer_encoding::VarInt; - pub enum ValueType { TypeDeletion = 0, TypeValue = 1, @@ -97,67 +95,3 @@ } } } - -pub struct BlockHandle { - offset: usize, - size: usize, -} - -impl BlockHandle { - /// Decodes a block handle from `from` and returns a block handle - /// together with how many bytes were read from the slice. - pub fn decode(from: &[u8]) -> (BlockHandle, usize) { - let (off, offsize) = usize::decode_var(from); - let (sz, szsize) = usize::decode_var(&from[offsize..]); - - (BlockHandle { - offset: off, - size: sz, - }, - offsize + szsize) - } - - pub fn new(offset: usize, size: usize) -> BlockHandle { - BlockHandle { - offset: offset, - size: size, - } - } - - pub fn offset(&self) -> usize { - self.offset - } - - pub fn size(&self) -> usize { - self.size - } - - /// Returns how many bytes were written, or 0 if the write failed because `dst` is too small. - pub fn encode_to(&self, dst: &mut [u8]) -> usize { - if dst.len() < self.offset.required_space() + self.size.required_space() { - 0 - } else { - let off = self.offset.encode_var(dst); - off + self.size.encode_var(&mut dst[off..]) - } - - } -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn test_blockhandle() { - let bh = BlockHandle::new(890, 777); - let mut dst = [0 as u8; 128]; - let enc_sz = bh.encode_to(&mut dst[..]); - - let (bh2, dec_sz) = BlockHandle::decode(&dst); - - assert_eq!(enc_sz, dec_sz); - assert_eq!(bh.size(), bh2.size()); - assert_eq!(bh.offset(), bh2.offset()); - } -}