Skip to main content

lsm_tree/
value_type.rs

1// Copyright (c) 2024-present, fjall-rs
2// This source code is licensed under both the Apache 2.0 and MIT License
3// (found in the LICENSE-* files in the repository)
4
5/// Value type (regular value or tombstone)
6#[derive(Copy, Clone, Debug, Eq, PartialEq)]
7#[cfg_attr(test, derive(strum::EnumIter))]
8pub enum ValueType {
9    /// Existing value
10    Value,
11
12    /// Deleted value
13    Tombstone,
14
15    /// "Weak" deletion (a.k.a. `SingleDelete` in `RocksDB`)
16    WeakTombstone,
17
18    /// Merge operand
19    ///
20    /// Stores a partial update that will be combined with other operands
21    /// and/or a base value via a user-provided [`crate::MergeOperator`].
22    MergeOperand = 3,
23
24    /// Value pointer
25    ///
26    /// Points to a blob in a blob file.
27    Indirection = 4,
28}
29
30impl ValueType {
31    /// Returns `true` if the type is a tombstone marker (either normal or weak).
32    #[must_use]
33    pub fn is_tombstone(self) -> bool {
34        self == Self::Tombstone || self == Self::WeakTombstone
35    }
36
37    pub(crate) fn is_indirection(self) -> bool {
38        self == Self::Indirection
39    }
40
41    /// Returns `true` if the type is a merge operand.
42    #[must_use]
43    pub fn is_merge_operand(self) -> bool {
44        self == Self::MergeOperand
45    }
46}
47
48impl TryFrom<u8> for ValueType {
49    type Error = ();
50
51    fn try_from(value: u8) -> Result<Self, Self::Error> {
52        match value {
53            0 => Ok(Self::Value),
54            1 => Ok(Self::Tombstone),
55            2 => Ok(Self::WeakTombstone),
56            3 => Ok(Self::MergeOperand),
57            4 => Ok(Self::Indirection),
58            _ => Err(()),
59        }
60    }
61}
62
63impl From<ValueType> for u8 {
64    fn from(value: ValueType) -> Self {
65        match value {
66            ValueType::Value => 0,
67            ValueType::Tombstone => 1,
68            ValueType::WeakTombstone => 2,
69            ValueType::MergeOperand => 3,
70            ValueType::Indirection => 4,
71        }
72    }
73}