Skip to main content

reifydb_transaction/transaction/catalog/
dictionary.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2026 ReifyDB
3
4use reifydb_core::interface::catalog::{
5	change::CatalogTrackDictionaryChangeOperations, dictionary::Dictionary, id::NamespaceId,
6};
7use reifydb_value::{Result, value::dictionary::DictionaryId};
8
9use crate::{
10	change::{
11		Change,
12		OperationType::{Create, Delete},
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_deleted(&mut self, dictionary: Dictionary) -> Result<()> {
30		let change = Change {
31			pre: Some(dictionary),
32			post: None,
33			op: Delete,
34		};
35		self.changes.add_dictionary_change(change);
36		Ok(())
37	}
38}
39
40impl TransactionalDictionaryChanges for AdminTransaction {
41	fn find_dictionary(&self, id: DictionaryId) -> Option<&Dictionary> {
42		for change in self.changes.dictionary.iter().rev() {
43			if let Some(dictionary) = &change.post {
44				if dictionary.id == id {
45					return Some(dictionary);
46				}
47			} else if let Some(dictionary) = &change.pre
48				&& dictionary.id == id && change.op == Delete
49			{
50				return None;
51			}
52		}
53		None
54	}
55
56	fn find_dictionary_by_name(&self, namespace: NamespaceId, name: &str) -> Option<&Dictionary> {
57		self.changes
58			.dictionary
59			.iter()
60			.rev()
61			.find_map(|change| change.post.as_ref().filter(|d| d.namespace == namespace && d.name == name))
62	}
63
64	fn is_dictionary_deleted(&self, id: DictionaryId) -> bool {
65		self.changes
66			.dictionary
67			.iter()
68			.rev()
69			.any(|change| change.op == Delete && change.pre.as_ref().map(|d| d.id) == Some(id))
70	}
71
72	fn is_dictionary_deleted_by_name(&self, namespace: NamespaceId, name: &str) -> bool {
73		self.changes.dictionary.iter().rev().any(|change| {
74			change.op == Delete
75				&& change
76					.pre
77					.as_ref()
78					.map(|d| d.namespace == namespace && d.name == name)
79					.unwrap_or(false)
80		})
81	}
82}