lsm_tree/blob_tree/gc/
reader.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
5use crate::{blob_tree::value::MaybeInlineValue, coding::Decode, Memtable};
6use std::io::Cursor;
7use value_log::ValueHandle;
8
9#[allow(clippy::module_name_repetitions)]
10pub struct GcReader<'a> {
11    tree: &'a crate::Tree,
12    memtable: &'a Memtable,
13}
14
15impl<'a> GcReader<'a> {
16    pub fn new(tree: &'a crate::Tree, memtable: &'a Memtable) -> Self {
17        Self { tree, memtable }
18    }
19
20    fn get_internal(&self, key: &[u8]) -> crate::Result<Option<MaybeInlineValue>> {
21        let Some(item) = self
22            .tree
23            .get_internal_entry_with_memtable(self.memtable, key, None)?
24            .map(|x| x.value)
25        else {
26            return Ok(None);
27        };
28
29        let mut cursor = Cursor::new(item);
30        let item = MaybeInlineValue::decode_from(&mut cursor)?;
31
32        Ok(Some(item))
33    }
34}
35
36impl<'a> value_log::IndexReader for GcReader<'a> {
37    fn get(&self, key: &[u8]) -> std::io::Result<Option<ValueHandle>> {
38        use std::io::{Error as IoError, ErrorKind as IoErrorKind};
39        use MaybeInlineValue::{Indirect, Inline};
40
41        let Some(item) = self
42            .get_internal(key)
43            .map_err(|e| IoError::new(IoErrorKind::Other, e.to_string()))?
44        else {
45            return Ok(None);
46        };
47
48        match item {
49            Inline(_) => Ok(None),
50            Indirect { vhandle, .. } => Ok(Some(vhandle)),
51        }
52    }
53}