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    /// Value handle
19    ///
20    /// Points to a blob in a blob file.
21    Indirection,
22}
23
24impl ValueType {
25    /// Returns `true` if the type is a tombstone marker (either normal or weak).
26    #[must_use]
27    pub fn is_tombstone(self) -> bool {
28        self == Self::Tombstone || self == Self::WeakTombstone
29    }
30
31    pub(crate) fn is_indirection(self) -> bool {
32        self == Self::Indirection
33    }
34}
35
36impl TryFrom<u8> for ValueType {
37    type Error = ();
38
39    fn try_from(value: u8) -> Result<Self, Self::Error> {
40        match value {
41            0 => Ok(Self::Value),
42            0x0000_0001 => Ok(Self::Tombstone),
43            0x0000_0011 => Ok(Self::WeakTombstone),
44            0b0000_0100 => Ok(Self::Indirection),
45            _ => Err(()),
46        }
47    }
48}
49
50impl From<ValueType> for u8 {
51    fn from(value: ValueType) -> Self {
52        match value {
53            ValueType::Value => 0,
54            ValueType::Tombstone => 0x0000_0001,
55            ValueType::WeakTombstone => 0x0000_0011,
56            ValueType::Indirection => 0b0000_0100,
57        }
58    }
59}