Skip to main content

reifydb_transaction/transaction/catalog/
dictionary.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2025 ReifyDB
3
4use reifydb_core::interface::catalog::{
5	change::CatalogTrackDictionaryChangeOperations, dictionary::Dictionary, id::NamespaceId,
6};
7use reifydb_type::{Result, value::dictionary::DictionaryId};
8
9use crate::{
10	change::{
11		Change,
12		OperationType::{Create, Delete, Update},
13		TransactionalDictionaryChanges,
14	},
15	transaction::admin::AdminTransaction,
16};
17
18impl CatalogTrackDictionaryChangeOperations for AdminTransaction {
19	fn track_dictionary_created(&mut self, dictionary: Dictionary) -> Result<()> {
20		let change = Change {
21			pre: None,
22			post: Some(dictionary),
23			op: Create,
24		};
25		self.changes.add_dictionary_change(change);
26		Ok(())
27	}
28
29	fn track_dictionary_updated(&mut self, pre: Dictionary, post: Dictionary) -> Result<()> {
30		let change = Change {
31			pre: Some(pre),
32			post: Some(post),
33			op: Update,
34		};
35		self.changes.add_dictionary_change(change);
36		Ok(())
37	}
38
39	fn track_dictionary_deleted(&mut self, dictionary: Dictionary) -> Result<()> {
40		let change = Change {
41			pre: Some(dictionary),
42			post: None,
43			op: Delete,
44		};
45		self.changes.add_dictionary_change(change);
46		Ok(())
47	}
48}
49
50impl TransactionalDictionaryChanges for AdminTransaction {
51	fn find_dictionary(&self, id: DictionaryId) -> Option<&Dictionary> {
52		for change in self.changes.dictionary.iter().rev() {
53			if let Some(dictionary) = &change.post {
54				if dictionary.id == id {
55					return Some(dictionary);
56				}
57			} else if let Some(dictionary) = &change.pre
58				&& dictionary.id == id && change.op == Delete
59			{
60				return None;
61			}
62		}
63		None
64	}
65
66	fn find_dictionary_by_name(&self, namespace: NamespaceId, name: &str) -> Option<&Dictionary> {
67		self.changes
68			.dictionary
69			.iter()
70			.rev()
71			.find_map(|change| change.post.as_ref().filter(|d| d.namespace == namespace && d.name == name))
72	}
73
74	fn is_dictionary_deleted(&self, id: DictionaryId) -> bool {
75		self.changes
76			.dictionary
77			.iter()
78			.rev()
79			.any(|change| change.op == Delete && change.pre.as_ref().map(|d| d.id) == Some(id))
80	}
81
82	fn is_dictionary_deleted_by_name(&self, namespace: NamespaceId, name: &str) -> bool {
83		self.changes.dictionary.iter().rev().any(|change| {
84			change.op == Delete
85				&& change
86					.pre
87					.as_ref()
88					.map(|d| d.namespace == namespace && d.name == name)
89					.unwrap_or(false)
90		})
91	}
92}