changeset 110:6d792bf1ac38

Merge pull request #7 from thomaskrause/feature/block-cache-size Allow to set the block cache size manually.
author Lewin Bormann <lbo@spheniscida.de>
date Tue, 16 Nov 2021 08:40:24 +0100
parents f76c3689831a (current diff) 71a541e117da (diff)
children fc26b746ea54
files
diffstat 2 files changed, 32 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/options.rs	Sun Apr 26 10:15:39 2020 +0200
+++ b/src/options.rs	Tue Nov 16 08:40:24 2021 +0100
@@ -42,6 +42,16 @@
     pub filter_policy: filter::BoxedFilterPolicy,
 }
 
+impl Options {
+    /// Configure to use a custom block cache capacity.
+    /// The capacity is given as number of items in the cache
+    /// and the minimal allowed capacity is 1.
+    pub fn with_cache_capacity(mut self, capacity: usize) -> Options {
+        self.block_cache = share(Cache::new(capacity));
+        self
+    }
+}
+
 impl Default for Options {
     fn default() -> Options {
         Options {
@@ -56,3 +66,4 @@
         }
     }
 }
+
--- a/src/table_reader.rs	Sun Apr 26 10:15:39 2020 +0200
+++ b/src/table_reader.rs	Tue Nov 16 08:40:24 2021 +0100
@@ -442,6 +442,27 @@
     }
 
     #[test]
+    fn test_table_block_tiny_cache() {
+        let (src, size) = build_table(build_data());
+        // Create a table with no block cache
+        let mut opt = Options::default().with_cache_capacity(1);
+        opt.block_size = 32;
+
+        let table = Table::new(opt.clone(), wrap_buffer(src), size).unwrap();
+        let mut iter = table.iter();
+
+        // index/metaindex blocks are not cached. That'd be a waste of memory.
+        assert_eq!(opt.block_cache.read().expect(LOCK_POISONED).count(), 0);
+
+        // We should have at most one item in the cache
+        iter.next();
+        assert_eq!(opt.block_cache.read().expect(LOCK_POISONED).count(), 1);
+        iter.next();
+        assert_eq!(opt.block_cache.read().expect(LOCK_POISONED).count(), 1);
+
+    }
+
+    #[test]
     fn test_table_iterator_fwd_bwd() {
         let (src, size) = build_table(build_data());
         let data = build_data();