reifydb_core/state/
store.rs1use reifydb_codec::{
5 key::encoded::{EncodedKey, EncodedKeyRange},
6 row::operator::EncodedOperatorRow,
7};
8use reifydb_value::{
9 Result,
10 value::{datetime::DateTime, row_number::RowNumber},
11};
12
13use crate::key::operator_state::{GroupId, GroupStateKey};
14
15#[derive(Debug, Clone, Copy, PartialEq, Eq)]
16#[repr(u8)]
17pub enum TimerKind {
18 Seal = 0,
19 Grace = 1,
20 RowTtl = 2,
21 Maintenance = 3,
22}
23
24impl TimerKind {
25 pub fn is_unique(&self) -> bool {
26 matches!(self, Self::Maintenance)
27 }
28
29 pub fn from_u8(value: u8) -> Option<Self> {
30 match value {
31 0 => Some(Self::Seal),
32 1 => Some(Self::Grace),
33 2 => Some(Self::RowTtl),
34 3 => Some(Self::Maintenance),
35 _ => None,
36 }
37 }
38}
39
40pub trait StateStore {
41 fn state_get(&mut self, key: &GroupStateKey) -> Result<Option<EncodedOperatorRow>>;
42
43 fn state_get_many_visit(
44 &mut self,
45 keys: &[GroupStateKey],
46 visit: &mut dyn FnMut(GroupStateKey, EncodedOperatorRow) -> Result<()>,
47 ) -> Result<()>;
48
49 fn state_set(&mut self, key: &GroupStateKey, payload: EncodedOperatorRow) -> Result<()>;
50
51 fn state_remove(&mut self, key: &GroupStateKey) -> Result<()>;
52
53 fn state_range_visit(
55 &mut self,
56 range: EncodedKeyRange,
57 limit: Option<usize>,
58 visit: &mut dyn FnMut(GroupStateKey, EncodedOperatorRow) -> Result<()>,
59 ) -> Result<()>;
60
61 fn state_last(&mut self, range: EncodedKeyRange) -> Result<Option<(GroupStateKey, EncodedOperatorRow)>> {
62 let mut last = None;
63 self.state_range_visit(range, None, &mut |key, payload| {
64 last = Some((key, payload));
65 Ok(())
66 })?;
67 Ok(last)
68 }
69
70 fn intern_groups(&mut self, groups: &[EncodedKey]) -> Result<Vec<(GroupId, bool)>>;
71
72 fn lookup_groups(&mut self, groups: &[EncodedKey]) -> Result<Vec<Option<GroupId>>>;
73
74 fn get_or_create_row_numbers(&mut self, group: GroupId, keys: &[EncodedKey]) -> Result<Vec<(RowNumber, bool)>>;
75
76 fn get_or_create_row_numbers_for_pairs(
77 &mut self,
78 pairs: &[(GroupId, EncodedKey)],
79 ) -> Result<Vec<(RowNumber, bool)>>;
80
81 fn remove_row_number(&mut self, group: GroupId, key: &EncodedKey) -> Result<()>;
82
83 fn written_at(&self) -> DateTime;
84}
85
86pub trait TimerStore {
87 fn arm_timer(&mut self, due: DateTime, kind: TimerKind, key: &EncodedKey) -> Result<()>;
88
89 fn disarm_timer(&mut self, due: DateTime, kind: TimerKind, key: &EncodedKey) -> Result<()>;
90
91 fn flow_watermark(&mut self) -> Result<Option<DateTime>>;
92}