lsm_tree/ingestion.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::{
6 blob_tree::ingest::BlobIngestion, tree::ingest::Ingestion, AnyTree, UserKey, UserValue,
7};
8
9/// Unified ingestion builder over `AnyTree`
10// Keep zero allocations and direct dispatch; boxing introduces heap indirection and `dyn` adds virtual dispatch.
11// Ingestion calls use `&mut self` in tight loops; the active variant is stable and branch prediction makes the match cheap.
12// Allowing this lint preserves hot-path performance at the cost of a larger enum size.
13#[expect(clippy::large_enum_variant)]
14pub enum AnyIngestion<'a> {
15 /// Ingestion for a standard LSM-tree
16 Standard(Ingestion<'a>),
17
18 /// Ingestion for a [`BlobTree`] with KV separation
19 Blob(BlobIngestion<'a>),
20}
21
22impl AnyIngestion<'_> {
23 /// Writes a key-value pair.
24 ///
25 /// # Errors
26 ///
27 /// Will return `Err` if an IO error occurs.
28 pub fn write<K: Into<UserKey>, V: Into<UserValue>>(
29 &mut self,
30 key: K,
31 value: V,
32 ) -> crate::Result<()> {
33 match self {
34 Self::Standard(i) => i.write(key.into(), value.into()),
35 Self::Blob(b) => b.write(key.into(), value.into()),
36 }
37 }
38
39 /// Writes a tombstone for a key.
40 ///
41 /// # Errors
42 ///
43 /// Will return `Err` if an IO error occurs.
44 pub fn write_tombstone<K: Into<UserKey>>(&mut self, key: K) -> crate::Result<()> {
45 match self {
46 Self::Standard(i) => i.write_tombstone(key.into()),
47 Self::Blob(b) => b.write_tombstone(key.into()),
48 }
49 }
50
51 /// Finalizes ingestion and registers created tables (and blob files if present).
52 ///
53 /// # Errors
54 ///
55 /// Will return `Err` if an IO error occurs.
56 pub fn finish(self) -> crate::Result<()> {
57 match self {
58 Self::Standard(i) => i.finish(),
59 Self::Blob(b) => b.finish(),
60 }
61 }
62}
63
64impl AnyTree {
65 /// Starts an ingestion for any tree type (standard or blob).
66 ///
67 /// # Errors
68 ///
69 /// Will return `Err` if an IO error occurs.
70 pub fn ingestion(&self) -> crate::Result<AnyIngestion<'_>> {
71 match self {
72 Self::Standard(t) => Ok(AnyIngestion::Standard(Ingestion::new(t)?)),
73 Self::Blob(b) => Ok(AnyIngestion::Blob(BlobIngestion::new(b)?)),
74 }
75 }
76}