1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright (c) 2024-present, fjall-rs
// This source code is licensed under both the Apache 2.0 and MIT License
// (found in the LICENSE-* files in the repository)

use crate::{blob_cache::BlobCache, compression::Compressor};
use std::sync::Arc;

/// Value log configuration
pub struct Config<C: Compressor + Clone> {
    /// Target size of vLog segments
    pub(crate) segment_size_bytes: u64,

    /// Blob cache to use
    pub(crate) blob_cache: Arc<BlobCache>,

    /// Compression to use
    pub(crate) compression: C,
}

impl<C: Compressor + Clone + Default> Default for Config<C> {
    fn default() -> Self {
        Self {
            segment_size_bytes: /* 256 MiB */ 256 * 1_024 * 1_024,
            blob_cache: Arc::new(BlobCache::with_capacity_bytes(
                /* 16 MiB */ 16 * 1_024 * 1_024,
            )),
            compression: C::default(),
        }
    }
}

impl<C: Compressor + Clone> Config<C> {
    /// Sets the compression & decompression scheme.
    #[must_use]
    pub fn compression(mut self, compressor: C) -> Self {
        self.compression = compressor;
        self
    }

    /// Sets the blob cache.
    ///
    /// You can create a global [`BlobCache`] and share it between multiple
    /// value logs to cap global cache memory usage.
    ///
    /// Defaults to a blob cache with 16 MiB of capacity *per value log*.
    #[must_use]
    pub fn blob_cache(mut self, blob_cache: Arc<BlobCache>) -> Self {
        self.blob_cache = blob_cache;
        self
    }

    /// Sets the maximum size of value log segments.
    ///
    /// This heavily influences space amplification, as
    /// space reclamation works on a per-segment basis.
    ///
    /// Like `blob_file_size` in `RocksDB`.
    ///
    /// Default = 256 MiB
    #[must_use]
    pub fn segment_size_bytes(mut self, bytes: u64) -> Self {
        self.segment_size_bytes = bytes;
        self
    }
}