sstable/
options.rs

1use crate::block::Block;
2use crate::cache::Cache;
3use crate::cmp::{Cmp, DefaultCmp};
4use crate::filter;
5use crate::types::{share, Shared};
6
7use std::default::Default;
8use std::sync::Arc;
9
10const KB: usize = 1 << 10;
11const MB: usize = KB * KB;
12
13const BLOCK_MAX_SIZE: usize = 4 * KB;
14const BLOCK_CACHE_CAPACITY: usize = 8 * MB;
15const WRITE_BUFFER_SIZE: usize = 4 * MB;
16const DEFAULT_BITS_PER_KEY: u32 = 10; // NOTE: This may need to be optimized.
17
18#[derive(Clone, Copy, PartialEq, Debug)]
19pub enum CompressionType {
20    CompressionNone = 0,
21    CompressionSnappy = 1,
22}
23
24pub fn int_to_compressiontype(i: u32) -> Option<CompressionType> {
25    match i {
26        0 => Some(CompressionType::CompressionNone),
27        1 => Some(CompressionType::CompressionSnappy),
28        _ => None,
29    }
30}
31
32/// Options contains general parameters for reading and writing SSTables. Most of the names are
33/// self-explanatory; the defaults are defined in the `Default` implementation.
34#[derive(Clone)]
35pub struct Options {
36    pub cmp: Arc<Box<dyn Cmp>>,
37    pub write_buffer_size: usize,
38    pub block_cache: Shared<Cache<Block>>,
39    pub block_size: usize,
40    pub block_restart_interval: usize,
41    pub compression_type: CompressionType,
42    pub filter_policy: filter::BoxedFilterPolicy,
43}
44
45impl Options {
46    /// Configure to use a custom block cache capacity.
47    /// The capacity is given as number of items in the cache
48    /// and the minimal allowed capacity is 1.
49    pub fn with_cache_capacity(mut self, capacity: usize) -> Options {
50        self.block_cache = share(Cache::new(capacity));
51        self
52    }
53}
54
55impl Default for Options {
56    fn default() -> Options {
57        Options {
58            cmp: Arc::new(Box::new(DefaultCmp)),
59            write_buffer_size: WRITE_BUFFER_SIZE,
60            // 2000 elements by default
61            block_cache: share(Cache::new(BLOCK_CACHE_CAPACITY / BLOCK_MAX_SIZE)),
62            block_size: BLOCK_MAX_SIZE,
63            block_restart_interval: 16,
64            compression_type: CompressionType::CompressionNone,
65            filter_policy: Arc::new(Box::new(filter::BloomPolicy::new(DEFAULT_BITS_PER_KEY))),
66        }
67    }
68}
69