changeset 121:04b1d08a5876

Start integration of table reader with BlockCache
author Lewin Bormann <lbo@spheniscida.de>
date Mon, 26 Dec 2016 11:21:25 +0000
parents 28bf33d01950
children 14dbd87dd144
files src/cache.rs src/filter.rs src/memtable.rs src/table_reader.rs
diffstat 4 files changed, 27 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/src/cache.rs	Mon Dec 26 11:09:07 2016 +0000
+++ b/src/cache.rs	Mon Dec 26 11:21:25 2016 +0000
@@ -1,6 +1,8 @@
 use std::collections::HashMap;
 use std::mem::{swap, replace};
 
+use integer_encoding::FixedIntWriter;
+
 // No clone, no copy! That asserts that an LRUHandle exists only once.
 type LRUHandle<T> = *mut LRUNode<T>;
 
@@ -150,8 +152,18 @@
 }
 
 pub type CacheKey = Vec<u8>;
+pub struct CacheID(u64);
 type CacheEntry<T> = (T, LRUHandle<CacheKey>);
 
+impl CacheID {
+    // Serialize a Cache ID to a byte string.
+    pub fn serialize(&self) -> Vec<u8> {
+        let mut v = vec![0; 8];
+        let _ = v.write_fixedint(self.0);
+        v
+    }
+}
+
 /// Implementation of `ShardedLRUCache`.
 /// Based on a HashMap; the elements are linked in order to support the LRU ordering.
 pub struct Cache<T> {
@@ -161,6 +173,7 @@
     list: LRUList<CacheKey>,
     map: HashMap<CacheKey, CacheEntry<T>>,
     cap: usize,
+    id: u64,
 }
 
 impl<T> Cache<T> {
@@ -170,9 +183,15 @@
             list: LRUList::new(),
             map: HashMap::with_capacity(1024),
             cap: capacity,
+            id: 0,
         }
     }
 
+    pub fn new_cache_id(&mut self) -> CacheID {
+        self.id += 1;
+        return CacheID(self.id);
+    }
+
     /// How many the cache currently contains
     pub fn count(&self) -> usize {
         return self.list.count();
--- a/src/filter.rs	Mon Dec 26 11:09:07 2016 +0000
+++ b/src/filter.rs	Mon Dec 26 11:21:25 2016 +0000
@@ -126,7 +126,7 @@
     }
     fn key_may_match(&self, key: &[u8], filter: &[u8]) -> bool {
         if filter.len() == 0 {
-            return true
+            return true;
         }
 
         let bits = (filter.len() - 1) as u32 * 8;
--- a/src/memtable.rs	Mon Dec 26 11:09:07 2016 +0000
+++ b/src/memtable.rs	Mon Dec 26 11:21:25 2016 +0000
@@ -1,6 +1,7 @@
 use std::cmp::Ordering;
 
-use key_types::{LookupKey, UserKey, InternalKey, MemtableKey, parse_memtable_key, build_memtable_key};
+use key_types::{LookupKey, UserKey, InternalKey, MemtableKey, parse_memtable_key,
+                build_memtable_key};
 use types::{ValueType, SequenceNumber, Status, LdbIterator, cmp};
 use skipmap::{SkipMap, SkipMapIter};
 
--- a/src/table_reader.rs	Mon Dec 26 11:09:07 2016 +0000
+++ b/src/table_reader.rs	Mon Dec 26 11:21:25 2016 +0000
@@ -1,5 +1,6 @@
 use block::{Block, BlockIter};
 use blockhandle::BlockHandle;
+use cache::CacheID;
 use filter::{InternalFilterPolicy, FilterPolicy};
 use filter_block::FilterBlockReader;
 use options::{self, CompressionType, Options};
@@ -72,6 +73,7 @@
 pub struct Table<R: Read + Seek, FP: FilterPolicy> {
     file: R,
     file_size: usize,
+    cache_id: CacheID,
 
     opt: Options,
     cmp: Box<CmpFn>,
@@ -113,9 +115,12 @@
 
         metaindexiter.reset();
 
+        let cache_id = opt.block_cache.lock().unwrap().new_cache_id();
+
         Ok(Table {
             file: file,
             file_size: size,
+            cache_id: cache_id,
             opt: opt,
             cmp: Box::new(cmp),
             footer: footer,