lsm_tree/config.rs
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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
// 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::{
descriptor_table::FileDescriptorTable,
path::absolute_path,
segment::meta::{CompressionType, TableType},
BlobTree, BlockCache, Tree,
};
use std::{
path::{Path, PathBuf},
sync::Arc,
};
use value_log::BlobCache;
/// LSM-tree type
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum TreeType {
/// Standard LSM-tree, see [`Tree`]
Standard,
/// Key-value separated LSM-tree, see [`BlobTree`]
Blob,
}
impl From<TreeType> for u8 {
fn from(val: TreeType) -> Self {
match val {
TreeType::Standard => 0,
TreeType::Blob => 1,
}
}
}
impl TryFrom<u8> for TreeType {
type Error = ();
fn try_from(value: u8) -> Result<Self, Self::Error> {
match value {
0 => Ok(Self::Standard),
1 => Ok(Self::Blob),
_ => Err(()),
}
}
}
const DEFAULT_FILE_FOLDER: &str = ".lsm.data";
#[derive(Clone)]
/// Tree configuration builder
pub struct Config {
/// Folder path
#[doc(hidden)]
pub path: PathBuf,
/// Tree type (unused)
#[allow(unused)]
pub tree_type: TreeType,
/// What type of compression is used
pub compression: CompressionType,
/// What type of compression is used for blobs
pub blob_compression: CompressionType,
/// Table type (unused)
#[allow(unused)]
pub(crate) table_type: TableType,
/// Block size of data blocks
pub data_block_size: u32,
/// Block size of index blocks
pub index_block_size: u32,
/// Amount of levels of the LSM tree (depth of tree)
pub level_count: u8,
/// Bits per key for levels that are not L0, L1, L2
// NOTE: bloom_bits_per_key is not conditionally compiled,
// because that would change the file format
#[doc(hidden)]
pub bloom_bits_per_key: i8,
/// Block cache to use
#[doc(hidden)]
pub block_cache: Arc<BlockCache>,
/// Blob cache to use
#[doc(hidden)]
pub blob_cache: Arc<BlobCache>,
/// Blob file (value log segment) target size in bytes
#[doc(hidden)]
pub blob_file_target_size: u64,
/// Key-value separation threshold in bytes
#[doc(hidden)]
pub blob_file_separation_threshold: u32,
/// Descriptor table to use
#[doc(hidden)]
pub descriptor_table: Arc<FileDescriptorTable>,
}
impl Default for Config {
fn default() -> Self {
Self {
path: absolute_path(DEFAULT_FILE_FOLDER),
descriptor_table: Arc::new(FileDescriptorTable::new(128, 2)),
block_cache: Arc::new(BlockCache::with_capacity_bytes(/* 16 MiB */ 16 * 1_024 * 1_024)),
data_block_size: /* 4 KiB */ 4_096,
index_block_size: /* 4 KiB */ 4_096,
level_count: 7,
tree_type: TreeType::Standard,
table_type: TableType::Block,
compression: CompressionType::None,
blob_compression: CompressionType::None,
bloom_bits_per_key: 10,
blob_cache: Arc::new(BlobCache::with_capacity_bytes(/* 16 MiB */ 16 * 1_024 * 1_024)),
blob_file_target_size: /* 64 MiB */ 64 * 1_024 * 1_024,
blob_file_separation_threshold: /* 4 KiB */ 4 * 1_024,
}
}
}
impl Config {
/// Initializes a new config
pub fn new<P: AsRef<Path>>(path: P) -> Self {
Self {
path: absolute_path(path),
..Default::default()
}
}
/// Sets the bits per key to use for bloom filters
/// in levels that are not L0 or L1.
///
/// Use -1 to disable bloom filters even in L0, L1, L2.
///
/// Defaults to 10 bits.
///
/// # Panics
///
/// Panics if `n` is less than -1.
#[must_use]
#[cfg(feature = "bloom")]
pub fn bloom_bits_per_key(mut self, bits: i8) -> Self {
assert!(bits >= -1, "invalid bits_per_key value");
self.bloom_bits_per_key = bits;
self
}
/// Sets the compression method.
///
/// Using some compression is recommended.
///
/// Default = None
#[must_use]
pub fn compression(mut self, compression: CompressionType) -> Self {
self.compression = compression;
self
}
/// Sets the compression method.
///
/// Using some compression is recommended.
///
/// Default = None
#[must_use]
pub fn blob_compression(mut self, compression: CompressionType) -> Self {
self.blob_compression = compression;
self
}
/// Sets the amount of levels of the LSM tree (depth of tree).
///
/// Defaults to 7, like `LevelDB` and `RocksDB`.
///
/// Cannot be changed once set.
///
/// # Panics
///
/// Panics if `n` is 0.
#[must_use]
pub fn level_count(mut self, n: u8) -> Self {
assert!(n > 0);
self.level_count = n;
self
}
/// Sets the data block size.
///
/// Defaults to 4 KiB (4096 bytes).
///
/// For point read heavy workloads (get) a sensible default is
/// somewhere between 4 - 8 KiB, depending on the average value size.
///
/// For scan heavy workloads (range, prefix), use 16 - 64 KiB
/// which also increases compression efficiency.
///
/// # Panics
///
/// Panics if the block size is smaller than 1 KiB or larger than 512 KiB.
#[must_use]
pub fn data_block_size(mut self, block_size: u32) -> Self {
assert!(block_size >= 1_024);
assert!(block_size <= 512 * 1_024);
self.data_block_size = block_size;
self
}
/// Sets the index block size.
///
/// Defaults to 4 KiB (4096 bytes).
///
/// For point read heavy workloads (get) a sensible default is
/// somewhere between 4 - 8 KiB, depending on the average value size.
///
/// For scan heavy workloads (range, prefix), use 16 - 64 KiB
/// which also increases compression efficiency.
///
/// # Panics
///
/// Panics if the block size is smaller than 1 KiB or larger than 512 KiB.
#[must_use]
pub fn index_block_size(mut self, block_size: u32) -> Self {
assert!(block_size >= 1_024);
assert!(block_size <= 512 * 1_024);
self.index_block_size = block_size;
self
}
/// Sets the block cache.
///
/// You can create a global [`BlockCache`] and share it between multiple
/// trees to cap global cache memory usage.
///
/// Defaults to a block cache with 8 MiB of capacity *per tree*.
#[must_use]
pub fn block_cache(mut self, block_cache: Arc<BlockCache>) -> Self {
self.block_cache = block_cache;
self
}
/// Sets the block cache.
///
/// You can create a global [`BlobCache`] and share it between multiple
/// trees and their value logs to cap global cache memory usage.
///
/// Defaults to a block cache with 8 MiB of capacity *per tree*.
///
/// This option has no effect when not used for opening a blob tree.
#[must_use]
pub fn blob_cache(mut self, blob_cache: Arc<BlobCache>) -> Self {
self.blob_cache = blob_cache;
self
}
/// Sets the target size of blob files.
///
/// Smaller blob files allow more granular garbage collection
/// which allows lower space amp for lower write I/O cost.
///
/// Larger blob files decrease the number of files on disk and maintenance
/// overhead.
///
/// Defaults to 64 MiB.
///
/// This option has no effect when not used for opening a blob tree.
#[must_use]
pub fn blob_file_target_size(mut self, bytes: u64) -> Self {
self.blob_file_target_size = bytes;
self
}
/// Sets the key-value separation threshold in bytes.
///
/// Smaller value will reduce compaction overhead and thus write amplification,
/// at the cost of lower read performance.
///
/// Defaults to 4KiB.
///
/// This option has no effect when not used for opening a blob tree.
#[must_use]
pub fn blob_file_separation_threshold(mut self, bytes: u32) -> Self {
self.blob_file_separation_threshold = bytes;
self
}
#[must_use]
#[doc(hidden)]
pub fn descriptor_table(mut self, descriptor_table: Arc<FileDescriptorTable>) -> Self {
self.descriptor_table = descriptor_table;
self
}
/// Opens a tree using the config.
///
/// # Errors
///
/// Will return `Err` if an IO error occurs.
pub fn open(self) -> crate::Result<Tree> {
Tree::open(self)
}
/// Opens a blob tree using the config.
///
/// # Errors
///
/// Will return `Err` if an IO error occurs.
pub fn open_as_blob_tree(mut self) -> crate::Result<BlobTree> {
self.tree_type = TreeType::Blob;
BlobTree::open(self)
}
}