reifydb_engine/transaction/catalog/
dictionary.rs

1// Copyright (c) reifydb.com 2025
2// This file is licensed under the AGPL-3.0-or-later, see license.md file
3
4use OperationType::{Create, Update};
5use reifydb_catalog::transaction::CatalogTrackDictionaryChangeOperations;
6use reifydb_core::interface::{
7	Change, DictionaryDef, DictionaryId, NamespaceId, OperationType, OperationType::Delete,
8	TransactionalDictionaryChanges,
9};
10use reifydb_type::IntoFragment;
11
12use crate::{StandardCommandTransaction, StandardQueryTransaction};
13
14impl CatalogTrackDictionaryChangeOperations for StandardCommandTransaction {
15	fn track_dictionary_def_created(&mut self, dictionary: DictionaryDef) -> reifydb_core::Result<()> {
16		let change = Change {
17			pre: None,
18			post: Some(dictionary),
19			op: Create,
20		};
21		self.changes.add_dictionary_def_change(change);
22		Ok(())
23	}
24
25	fn track_dictionary_def_updated(
26		&mut self,
27		pre: DictionaryDef,
28		post: DictionaryDef,
29	) -> reifydb_core::Result<()> {
30		let change = Change {
31			pre: Some(pre),
32			post: Some(post),
33			op: Update,
34		};
35		self.changes.add_dictionary_def_change(change);
36		Ok(())
37	}
38
39	fn track_dictionary_def_deleted(&mut self, dictionary: DictionaryDef) -> reifydb_core::Result<()> {
40		let change = Change {
41			pre: Some(dictionary),
42			post: None,
43			op: Delete,
44		};
45		self.changes.add_dictionary_def_change(change);
46		Ok(())
47	}
48}
49
50impl TransactionalDictionaryChanges for StandardCommandTransaction {
51	fn find_dictionary(&self, id: DictionaryId) -> Option<&DictionaryDef> {
52		// Find the last change for this dictionary ID
53		for change in self.changes.dictionary_def.iter().rev() {
54			if let Some(dictionary) = &change.post {
55				if dictionary.id == id {
56					return Some(dictionary);
57				}
58			} else if let Some(dictionary) = &change.pre {
59				if dictionary.id == id && change.op == Delete {
60					// Dictionary was deleted
61					return None;
62				}
63			}
64		}
65		None
66	}
67
68	fn find_dictionary_by_name<'a>(
69		&self,
70		namespace: NamespaceId,
71		name: impl IntoFragment<'a>,
72	) -> Option<&DictionaryDef> {
73		let name = name.into_fragment();
74		self.changes.dictionary_def.iter().rev().find_map(|change| {
75			change.post.as_ref().filter(|d| d.namespace == namespace && d.name == name.text())
76		})
77	}
78
79	fn is_dictionary_deleted(&self, id: DictionaryId) -> bool {
80		self.changes
81			.dictionary_def
82			.iter()
83			.rev()
84			.any(|change| change.op == Delete && change.pre.as_ref().map(|d| d.id) == Some(id))
85	}
86
87	fn is_dictionary_deleted_by_name<'a>(&self, namespace: NamespaceId, name: impl IntoFragment<'a>) -> bool {
88		let name = name.into_fragment();
89		self.changes.dictionary_def.iter().rev().any(|change| {
90			change.op == Delete
91				&& change
92					.pre
93					.as_ref()
94					.map(|d| d.namespace == namespace && d.name == name.text())
95					.unwrap_or(false)
96		})
97	}
98}
99
100impl TransactionalDictionaryChanges for StandardQueryTransaction {
101	fn find_dictionary(&self, _id: DictionaryId) -> Option<&DictionaryDef> {
102		None
103	}
104
105	fn find_dictionary_by_name<'a>(
106		&self,
107		_namespace: NamespaceId,
108		_name: impl IntoFragment<'a>,
109	) -> Option<&DictionaryDef> {
110		None
111	}
112
113	fn is_dictionary_deleted(&self, _id: DictionaryId) -> bool {
114		false
115	}
116
117	fn is_dictionary_deleted_by_name<'a>(&self, _namespace: NamespaceId, _name: impl IntoFragment<'a>) -> bool {
118		false
119	}
120}