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};
10
11use crate::{StandardCommandTransaction, StandardQueryTransaction};
12
13impl CatalogTrackDictionaryChangeOperations for StandardCommandTransaction {
14	fn track_dictionary_def_created(&mut self, dictionary: DictionaryDef) -> reifydb_core::Result<()> {
15		let change = Change {
16			pre: None,
17			post: Some(dictionary),
18			op: Create,
19		};
20		self.changes.add_dictionary_def_change(change);
21		Ok(())
22	}
23
24	fn track_dictionary_def_updated(
25		&mut self,
26		pre: DictionaryDef,
27		post: DictionaryDef,
28	) -> reifydb_core::Result<()> {
29		let change = Change {
30			pre: Some(pre),
31			post: Some(post),
32			op: Update,
33		};
34		self.changes.add_dictionary_def_change(change);
35		Ok(())
36	}
37
38	fn track_dictionary_def_deleted(&mut self, dictionary: DictionaryDef) -> reifydb_core::Result<()> {
39		let change = Change {
40			pre: Some(dictionary),
41			post: None,
42			op: Delete,
43		};
44		self.changes.add_dictionary_def_change(change);
45		Ok(())
46	}
47}
48
49impl TransactionalDictionaryChanges for StandardCommandTransaction {
50	fn find_dictionary(&self, id: DictionaryId) -> Option<&DictionaryDef> {
51		// Find the last change for this dictionary ID
52		for change in self.changes.dictionary_def.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				if dictionary.id == id && change.op == Delete {
59					// Dictionary was deleted
60					return None;
61				}
62			}
63		}
64		None
65	}
66
67	fn find_dictionary_by_name(&self, namespace: NamespaceId, name: &str) -> Option<&DictionaryDef> {
68		self.changes
69			.dictionary_def
70			.iter()
71			.rev()
72			.find_map(|change| change.post.as_ref().filter(|d| d.namespace == namespace && d.name == name))
73	}
74
75	fn is_dictionary_deleted(&self, id: DictionaryId) -> bool {
76		self.changes
77			.dictionary_def
78			.iter()
79			.rev()
80			.any(|change| change.op == Delete && change.pre.as_ref().map(|d| d.id) == Some(id))
81	}
82
83	fn is_dictionary_deleted_by_name(&self, namespace: NamespaceId, name: &str) -> bool {
84		self.changes.dictionary_def.iter().rev().any(|change| {
85			change.op == Delete
86				&& change
87					.pre
88					.as_ref()
89					.map(|d| d.namespace == namespace && d.name == name)
90					.unwrap_or(false)
91		})
92	}
93}
94
95impl TransactionalDictionaryChanges for StandardQueryTransaction {
96	fn find_dictionary(&self, _id: DictionaryId) -> Option<&DictionaryDef> {
97		None
98	}
99
100	fn find_dictionary_by_name(&self, _namespace: NamespaceId, _name: &str) -> Option<&DictionaryDef> {
101		None
102	}
103
104	fn is_dictionary_deleted(&self, _id: DictionaryId) -> bool {
105		false
106	}
107
108	fn is_dictionary_deleted_by_name(&self, _namespace: NamespaceId, _name: &str) -> bool {
109		false
110	}
111}