Skip to main content

reifydb_transaction/transaction/catalog/
row_ttl.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2025 ReifyDB
3
4use reifydb_core::{
5	interface::catalog::{change::CatalogTrackRowTtlChangeOperations, shape::ShapeId},
6	row::RowTtl,
7};
8use reifydb_type::Result;
9
10use crate::{
11	change::{
12		Change,
13		OperationType::{Create, Delete, Update},
14		TransactionalRowTtlChanges,
15	},
16	transaction::admin::AdminTransaction,
17};
18
19impl CatalogTrackRowTtlChangeOperations for AdminTransaction {
20	fn track_row_ttl_created(&mut self, shape: ShapeId, ttl: RowTtl) -> Result<()> {
21		let change = Change {
22			pre: None,
23			post: Some((shape, ttl)),
24			op: Create,
25		};
26		self.changes.add_row_ttl_change(change);
27		Ok(())
28	}
29
30	fn track_row_ttl_updated(&mut self, shape: ShapeId, pre: RowTtl, post: RowTtl) -> Result<()> {
31		let change = Change {
32			pre: Some((shape, pre)),
33			post: Some((shape, post)),
34			op: Update,
35		};
36		self.changes.add_row_ttl_change(change);
37		Ok(())
38	}
39
40	fn track_row_ttl_deleted(&mut self, shape: ShapeId, ttl: RowTtl) -> Result<()> {
41		let change = Change {
42			pre: Some((shape, ttl)),
43			post: None,
44			op: Delete,
45		};
46		self.changes.add_row_ttl_change(change);
47		Ok(())
48	}
49}
50
51impl TransactionalRowTtlChanges for AdminTransaction {
52	fn find_row_ttl(&self, shape: ShapeId) -> Option<&RowTtl> {
53		for change in self.changes.row_ttl.iter().rev() {
54			if let Some((s, ttl)) = &change.post {
55				if *s == shape {
56					return Some(ttl);
57				}
58			} else if let Some((s, _)) = &change.pre
59				&& *s == shape && change.op == Delete
60			{
61				return None;
62			}
63		}
64		None
65	}
66
67	fn is_row_ttl_deleted(&self, shape: ShapeId) -> bool {
68		self.changes.row_ttl.iter().rev().any(|change| {
69			change.op == Delete && change.pre.as_ref().map(|(s, _)| *s == shape).unwrap_or(false)
70		})
71	}
72}