Skip to main content

reifydb_store_multi/tier/
mod.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2025 ReifyDB
3
4use std::{collections::HashMap, ops::Bound};
5
6use reifydb_core::{common::CommitVersion, encoded::key::EncodedKey, interface::store::EntryKind};
7use reifydb_type::{Result, util::cowvec::CowVec};
8
9pub type TierBatch = HashMap<EntryKind, Vec<(EncodedKey, Option<CowVec<u8>>)>>;
10
11#[derive(Debug, Clone)]
12pub struct RawEntry {
13	pub key: EncodedKey,
14	pub version: CommitVersion,
15	pub value: Option<CowVec<u8>>,
16}
17
18#[derive(Debug, Clone)]
19pub struct RangeBatch {
20	pub entries: Vec<RawEntry>,
21
22	pub has_more: bool,
23}
24
25impl RangeBatch {
26	pub fn empty() -> Self {
27		Self {
28			entries: Vec::new(),
29			has_more: false,
30		}
31	}
32
33	pub fn is_empty(&self) -> bool {
34		self.entries.is_empty()
35	}
36}
37
38#[derive(Debug, Clone)]
39pub struct RangeCursor {
40	pub last_key: Option<EncodedKey>,
41
42	pub exhausted: bool,
43}
44
45#[derive(Debug, Clone, Default)]
46pub struct HistoricalCursor {
47	pub last_key: Option<EncodedKey>,
48	pub last_version: Option<CommitVersion>,
49	pub exhausted: bool,
50}
51
52impl HistoricalCursor {
53	pub fn new() -> Self {
54		Self::default()
55	}
56
57	pub fn is_exhausted(&self) -> bool {
58		self.exhausted
59	}
60}
61
62impl RangeCursor {
63	pub fn new() -> Self {
64		Self {
65			last_key: None,
66			exhausted: false,
67		}
68	}
69
70	pub fn is_exhausted(&self) -> bool {
71		self.exhausted
72	}
73}
74
75impl Default for RangeCursor {
76	fn default() -> Self {
77		Self::new()
78	}
79}
80
81pub trait TierStorage: Send + Sync + Clone + 'static {
82	fn get(&self, table: EntryKind, key: &[u8], version: CommitVersion) -> Result<Option<CowVec<u8>>>;
83
84	fn contains(&self, table: EntryKind, key: &[u8], version: CommitVersion) -> Result<bool> {
85		Ok(self.get(table, key, version)?.is_some())
86	}
87
88	fn set(&self, version: CommitVersion, batches: TierBatch) -> Result<()>;
89
90	fn range_next(
91		&self,
92		table: EntryKind,
93		cursor: &mut RangeCursor,
94		start: Bound<&[u8]>,
95		end: Bound<&[u8]>,
96		version: CommitVersion,
97		batch_size: usize,
98	) -> Result<RangeBatch>;
99
100	fn range_rev_next(
101		&self,
102		table: EntryKind,
103		cursor: &mut RangeCursor,
104		start: Bound<&[u8]>,
105		end: Bound<&[u8]>,
106		version: CommitVersion,
107		batch_size: usize,
108	) -> Result<RangeBatch>;
109
110	fn ensure_table(&self, table: EntryKind) -> Result<()>;
111
112	fn clear_table(&self, table: EntryKind) -> Result<()>;
113
114	fn drop(&self, batches: HashMap<EntryKind, Vec<(EncodedKey, CommitVersion)>>) -> Result<()>;
115
116	fn get_all_versions(&self, table: EntryKind, key: &[u8]) -> Result<Vec<(CommitVersion, Option<CowVec<u8>>)>>;
117
118	fn scan_historical_below(
119		&self,
120		table: EntryKind,
121		cutoff: CommitVersion,
122		cursor: &mut HistoricalCursor,
123		batch_size: usize,
124	) -> Result<Vec<(EncodedKey, CommitVersion)>>;
125}
126
127pub trait TierBackend: TierStorage {}