value_log/
index.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::ValueHandle;
6
7/// Trait that allows reading from an external index
8///
9/// An index should point into the value log using [`ValueHandle`].
10#[allow(clippy::module_name_repetitions)]
11pub trait Reader {
12    /// Returns a value handle for a given key.
13    ///
14    /// This method is used to index back into the index to check for
15    /// stale values when scanning through the value log's segments.
16    ///
17    /// # Errors
18    ///
19    /// Will return `Err` if an IO error occurs.
20    fn get(&self, key: &[u8]) -> std::io::Result<Option<ValueHandle>>;
21}
22
23/// Trait that allows writing into an external index
24///
25/// The write process should be atomic meaning that until `finish` is called
26/// no written value handles should be handed out by the index.
27/// When `finish` fails, no value handles should be written into the index.
28pub trait Writer {
29    /// Inserts a value handle into the index write batch.
30    ///
31    /// # Errors
32    ///
33    /// Will return `Err` if an IO error occurs.
34    fn insert_indirect(
35        &mut self,
36        key: &[u8],
37        vhandle: ValueHandle,
38        size: u32,
39    ) -> std::io::Result<()>;
40
41    /// Finishes the write batch.
42    ///
43    /// # Errors
44    ///
45    /// Will return `Err` if an IO error occurs.
46    fn finish(&mut self) -> std::io::Result<()>;
47}