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#[allow(clippy::module_name_repetitions)]
8#[cfg_attr(test, derive(strum::EnumIter))]
9pub enum ValueType {
10    /// Existing value
11    Value,
12
13    /// Deleted value
14    Tombstone,
15
16    /// "Weak" deletion (a.k.a. `SingleDelete` in `RocksDB`)
17    WeakTombstone,
18
19    /// Value handle
20    ///
21    /// Points to a blob in a blob file.
22    Indirection,
23}
24
25impl ValueType {
26    /// Returns `true` if the type is a tombstone marker (either normal or weak).
27    #[must_use]
28    pub fn is_tombstone(self) -> bool {
29        self == Self::Tombstone || self == Self::WeakTombstone
30    }
31
32    pub(crate) fn is_indirection(self) -> bool {
33        self == Self::Indirection
34    }
35}
36
37impl TryFrom<u8> for ValueType {
38    type Error = ();
39
40    fn try_from(value: u8) -> Result<Self, Self::Error> {
41        match value {
42            0 => Ok(Self::Value),
43            0x0000_0001 => Ok(Self::Tombstone),
44            0x0000_0011 => Ok(Self::WeakTombstone),
45            0b0000_0100 => Ok(Self::Indirection),
46            _ => Err(()),
47        }
48    }
49}
50
51impl From<ValueType> for u8 {
52    fn from(value: ValueType) -> Self {
53        match value {
54            ValueType::Value => 0,
55            ValueType::Tombstone => 0x0000_0001,
56            ValueType::WeakTombstone => 0x0000_0011,
57            ValueType::Indirection => 0b0000_0100,
58        }
59    }
60}