Skip to main content

lsm_tree/
value_type.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2024-present, fjall-rs
3// Copyright (c) 2026-present, Dmitry Prudnikov
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    /// A row written as cells, some of which may keep their bytes in a blob
30    /// file (see [`crate::blob_tree::field_row`]). Reads as a put, like a
31    /// value, with each referenced cell resolved to its object.
32    CellRow = 5,
33}
34
35impl ValueType {
36    /// Returns `true` if the type is a tombstone marker (either normal or weak).
37    #[must_use]
38    pub fn is_tombstone(self) -> bool {
39        self == Self::Tombstone || self == Self::WeakTombstone
40    }
41
42    /// Whether the entry is a put in any of its physical forms: a value, a
43    /// value kept in a blob file, or a row written as cells.
44    pub(crate) fn is_put(self) -> bool {
45        matches!(self, Self::Value | Self::Indirection | Self::CellRow)
46    }
47
48    pub(crate) fn is_indirection(self) -> bool {
49        self == Self::Indirection
50    }
51
52    /// Whether the entry is a row written as cells.
53    pub(crate) fn is_cell_row(self) -> bool {
54        self == Self::CellRow
55    }
56
57    /// Returns `true` if the type is a merge operand.
58    #[must_use]
59    pub fn is_merge_operand(self) -> bool {
60        self == Self::MergeOperand
61    }
62}
63
64impl TryFrom<u8> for ValueType {
65    type Error = ();
66
67    fn try_from(value: u8) -> Result<Self, Self::Error> {
68        match value {
69            0 => Ok(Self::Value),
70            1 => Ok(Self::Tombstone),
71            2 => Ok(Self::WeakTombstone),
72            3 => Ok(Self::MergeOperand),
73            4 => Ok(Self::Indirection),
74            5 => Ok(Self::CellRow),
75            _ => Err(()),
76        }
77    }
78}
79
80impl From<ValueType> for u8 {
81    fn from(value: ValueType) -> Self {
82        match value {
83            ValueType::Value => 0,
84            ValueType::Tombstone => 1,
85            ValueType::WeakTombstone => 2,
86            ValueType::MergeOperand => 3,
87            ValueType::Indirection => 4,
88            ValueType::CellRow => 5,
89        }
90    }
91}