Skip to main content

reifydb_transaction/interceptor/
dictionary_row.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2025 ReifyDB
3
4use reifydb_core::interface::catalog::dictionary::Dictionary;
5use reifydb_type::{
6	Result,
7	value::{Value, dictionary::DictionaryEntryId},
8};
9
10use super::WithInterceptors;
11use crate::interceptor::chain::InterceptorChain;
12
13// PRE INSERT
14pub struct DictionaryRowPreInsertContext<'a> {
15	pub dictionary: &'a Dictionary,
16	pub value: Value,
17}
18
19impl<'a> DictionaryRowPreInsertContext<'a> {
20	pub fn new(dictionary: &'a Dictionary, value: Value) -> Self {
21		Self {
22			dictionary,
23			value,
24		}
25	}
26}
27
28pub trait DictionaryRowPreInsertInterceptor: Send + Sync {
29	fn intercept<'a>(&self, ctx: &mut DictionaryRowPreInsertContext<'a>) -> Result<()>;
30}
31
32impl InterceptorChain<dyn DictionaryRowPreInsertInterceptor + Send + Sync> {
33	pub fn execute(&self, mut ctx: DictionaryRowPreInsertContext) -> Result<Value> {
34		for interceptor in &self.interceptors {
35			interceptor.intercept(&mut ctx)?;
36		}
37		Ok(ctx.value)
38	}
39}
40
41pub struct ClosureDictionaryRowPreInsertInterceptor<F>
42where
43	F: for<'a> Fn(&mut DictionaryRowPreInsertContext<'a>) -> Result<()> + Send + Sync,
44{
45	closure: F,
46}
47
48impl<F> ClosureDictionaryRowPreInsertInterceptor<F>
49where
50	F: for<'a> Fn(&mut DictionaryRowPreInsertContext<'a>) -> Result<()> + Send + Sync,
51{
52	pub fn new(closure: F) -> Self {
53		Self {
54			closure,
55		}
56	}
57}
58
59impl<F> Clone for ClosureDictionaryRowPreInsertInterceptor<F>
60where
61	F: for<'a> Fn(&mut DictionaryRowPreInsertContext<'a>) -> Result<()> + Send + Sync + Clone,
62{
63	fn clone(&self) -> Self {
64		Self {
65			closure: self.closure.clone(),
66		}
67	}
68}
69
70impl<F> DictionaryRowPreInsertInterceptor for ClosureDictionaryRowPreInsertInterceptor<F>
71where
72	F: for<'a> Fn(&mut DictionaryRowPreInsertContext<'a>) -> Result<()> + Send + Sync,
73{
74	fn intercept<'a>(&self, ctx: &mut DictionaryRowPreInsertContext<'a>) -> Result<()> {
75		(self.closure)(ctx)
76	}
77}
78
79pub fn dictionary_row_pre_insert<F>(f: F) -> ClosureDictionaryRowPreInsertInterceptor<F>
80where
81	F: for<'a> Fn(&mut DictionaryRowPreInsertContext<'a>) -> Result<()> + Send + Sync + Clone + 'static,
82{
83	ClosureDictionaryRowPreInsertInterceptor::new(f)
84}
85
86// POST INSERT
87pub struct DictionaryRowPostInsertContext<'a> {
88	pub dictionary: &'a Dictionary,
89	pub id: DictionaryEntryId,
90	pub value: &'a Value,
91}
92
93impl<'a> DictionaryRowPostInsertContext<'a> {
94	pub fn new(dictionary: &'a Dictionary, id: DictionaryEntryId, value: &'a Value) -> Self {
95		Self {
96			dictionary,
97			id,
98			value,
99		}
100	}
101}
102
103pub trait DictionaryRowPostInsertInterceptor: Send + Sync {
104	fn intercept<'a>(&self, ctx: &mut DictionaryRowPostInsertContext<'a>) -> Result<()>;
105}
106
107impl InterceptorChain<dyn DictionaryRowPostInsertInterceptor + Send + Sync> {
108	pub fn execute(&self, mut ctx: DictionaryRowPostInsertContext) -> Result<()> {
109		for interceptor in &self.interceptors {
110			interceptor.intercept(&mut ctx)?;
111		}
112		Ok(())
113	}
114}
115
116pub struct ClosureDictionaryRowPostInsertInterceptor<F>
117where
118	F: for<'a> Fn(&mut DictionaryRowPostInsertContext<'a>) -> Result<()> + Send + Sync,
119{
120	closure: F,
121}
122
123impl<F> ClosureDictionaryRowPostInsertInterceptor<F>
124where
125	F: for<'a> Fn(&mut DictionaryRowPostInsertContext<'a>) -> Result<()> + Send + Sync,
126{
127	pub fn new(closure: F) -> Self {
128		Self {
129			closure,
130		}
131	}
132}
133
134impl<F> Clone for ClosureDictionaryRowPostInsertInterceptor<F>
135where
136	F: for<'a> Fn(&mut DictionaryRowPostInsertContext<'a>) -> Result<()> + Send + Sync + Clone,
137{
138	fn clone(&self) -> Self {
139		Self {
140			closure: self.closure.clone(),
141		}
142	}
143}
144
145impl<F> DictionaryRowPostInsertInterceptor for ClosureDictionaryRowPostInsertInterceptor<F>
146where
147	F: for<'a> Fn(&mut DictionaryRowPostInsertContext<'a>) -> Result<()> + Send + Sync,
148{
149	fn intercept<'a>(&self, ctx: &mut DictionaryRowPostInsertContext<'a>) -> Result<()> {
150		(self.closure)(ctx)
151	}
152}
153
154pub fn dictionary_row_post_insert<F>(f: F) -> ClosureDictionaryRowPostInsertInterceptor<F>
155where
156	F: for<'a> Fn(&mut DictionaryRowPostInsertContext<'a>) -> Result<()> + Send + Sync + Clone + 'static,
157{
158	ClosureDictionaryRowPostInsertInterceptor::new(f)
159}
160
161// PRE UPDATE
162pub struct DictionaryRowPreUpdateContext<'a> {
163	pub dictionary: &'a Dictionary,
164	pub id: DictionaryEntryId,
165	pub value: Value,
166}
167
168impl<'a> DictionaryRowPreUpdateContext<'a> {
169	pub fn new(dictionary: &'a Dictionary, id: DictionaryEntryId, value: Value) -> Self {
170		Self {
171			dictionary,
172			id,
173			value,
174		}
175	}
176}
177
178pub trait DictionaryRowPreUpdateInterceptor: Send + Sync {
179	fn intercept<'a>(&self, ctx: &mut DictionaryRowPreUpdateContext<'a>) -> Result<()>;
180}
181
182impl InterceptorChain<dyn DictionaryRowPreUpdateInterceptor + Send + Sync> {
183	pub fn execute(&self, mut ctx: DictionaryRowPreUpdateContext) -> Result<Value> {
184		for interceptor in &self.interceptors {
185			interceptor.intercept(&mut ctx)?;
186		}
187		Ok(ctx.value)
188	}
189}
190
191pub struct ClosureDictionaryRowPreUpdateInterceptor<F>
192where
193	F: for<'a> Fn(&mut DictionaryRowPreUpdateContext<'a>) -> Result<()> + Send + Sync,
194{
195	closure: F,
196}
197
198impl<F> ClosureDictionaryRowPreUpdateInterceptor<F>
199where
200	F: for<'a> Fn(&mut DictionaryRowPreUpdateContext<'a>) -> Result<()> + Send + Sync,
201{
202	pub fn new(closure: F) -> Self {
203		Self {
204			closure,
205		}
206	}
207}
208
209impl<F> Clone for ClosureDictionaryRowPreUpdateInterceptor<F>
210where
211	F: for<'a> Fn(&mut DictionaryRowPreUpdateContext<'a>) -> Result<()> + Send + Sync + Clone,
212{
213	fn clone(&self) -> Self {
214		Self {
215			closure: self.closure.clone(),
216		}
217	}
218}
219
220impl<F> DictionaryRowPreUpdateInterceptor for ClosureDictionaryRowPreUpdateInterceptor<F>
221where
222	F: for<'a> Fn(&mut DictionaryRowPreUpdateContext<'a>) -> Result<()> + Send + Sync,
223{
224	fn intercept<'a>(&self, ctx: &mut DictionaryRowPreUpdateContext<'a>) -> Result<()> {
225		(self.closure)(ctx)
226	}
227}
228
229pub fn dictionary_row_pre_update<F>(f: F) -> ClosureDictionaryRowPreUpdateInterceptor<F>
230where
231	F: for<'a> Fn(&mut DictionaryRowPreUpdateContext<'a>) -> Result<()> + Send + Sync + Clone + 'static,
232{
233	ClosureDictionaryRowPreUpdateInterceptor::new(f)
234}
235
236// POST UPDATE
237pub struct DictionaryRowPostUpdateContext<'a> {
238	pub dictionary: &'a Dictionary,
239	pub id: DictionaryEntryId,
240	pub post: &'a Value,
241	pub pre: &'a Value,
242}
243
244impl<'a> DictionaryRowPostUpdateContext<'a> {
245	pub fn new(dictionary: &'a Dictionary, id: DictionaryEntryId, post: &'a Value, pre: &'a Value) -> Self {
246		Self {
247			dictionary,
248			id,
249			post,
250			pre,
251		}
252	}
253}
254
255pub trait DictionaryRowPostUpdateInterceptor: Send + Sync {
256	fn intercept<'a>(&self, ctx: &mut DictionaryRowPostUpdateContext<'a>) -> Result<()>;
257}
258
259impl InterceptorChain<dyn DictionaryRowPostUpdateInterceptor + Send + Sync> {
260	pub fn execute(&self, mut ctx: DictionaryRowPostUpdateContext) -> Result<()> {
261		for interceptor in &self.interceptors {
262			interceptor.intercept(&mut ctx)?;
263		}
264		Ok(())
265	}
266}
267
268pub struct ClosureDictionaryRowPostUpdateInterceptor<F>
269where
270	F: for<'a> Fn(&mut DictionaryRowPostUpdateContext<'a>) -> Result<()> + Send + Sync,
271{
272	closure: F,
273}
274
275impl<F> ClosureDictionaryRowPostUpdateInterceptor<F>
276where
277	F: for<'a> Fn(&mut DictionaryRowPostUpdateContext<'a>) -> Result<()> + Send + Sync,
278{
279	pub fn new(closure: F) -> Self {
280		Self {
281			closure,
282		}
283	}
284}
285
286impl<F> Clone for ClosureDictionaryRowPostUpdateInterceptor<F>
287where
288	F: for<'a> Fn(&mut DictionaryRowPostUpdateContext<'a>) -> Result<()> + Send + Sync + Clone,
289{
290	fn clone(&self) -> Self {
291		Self {
292			closure: self.closure.clone(),
293		}
294	}
295}
296
297impl<F> DictionaryRowPostUpdateInterceptor for ClosureDictionaryRowPostUpdateInterceptor<F>
298where
299	F: for<'a> Fn(&mut DictionaryRowPostUpdateContext<'a>) -> Result<()> + Send + Sync,
300{
301	fn intercept<'a>(&self, ctx: &mut DictionaryRowPostUpdateContext<'a>) -> Result<()> {
302		(self.closure)(ctx)
303	}
304}
305
306pub fn dictionary_row_post_update<F>(f: F) -> ClosureDictionaryRowPostUpdateInterceptor<F>
307where
308	F: for<'a> Fn(&mut DictionaryRowPostUpdateContext<'a>) -> Result<()> + Send + Sync + Clone + 'static,
309{
310	ClosureDictionaryRowPostUpdateInterceptor::new(f)
311}
312
313// PRE DELETE
314pub struct DictionaryRowPreDeleteContext<'a> {
315	pub dictionary: &'a Dictionary,
316	pub id: DictionaryEntryId,
317}
318
319impl<'a> DictionaryRowPreDeleteContext<'a> {
320	pub fn new(dictionary: &'a Dictionary, id: DictionaryEntryId) -> Self {
321		Self {
322			dictionary,
323			id,
324		}
325	}
326}
327
328pub trait DictionaryRowPreDeleteInterceptor: Send + Sync {
329	fn intercept<'a>(&self, ctx: &mut DictionaryRowPreDeleteContext<'a>) -> Result<()>;
330}
331
332impl InterceptorChain<dyn DictionaryRowPreDeleteInterceptor + Send + Sync> {
333	pub fn execute(&self, mut ctx: DictionaryRowPreDeleteContext) -> Result<()> {
334		for interceptor in &self.interceptors {
335			interceptor.intercept(&mut ctx)?;
336		}
337		Ok(())
338	}
339}
340
341pub struct ClosureDictionaryRowPreDeleteInterceptor<F>
342where
343	F: for<'a> Fn(&mut DictionaryRowPreDeleteContext<'a>) -> Result<()> + Send + Sync,
344{
345	closure: F,
346}
347
348impl<F> ClosureDictionaryRowPreDeleteInterceptor<F>
349where
350	F: for<'a> Fn(&mut DictionaryRowPreDeleteContext<'a>) -> Result<()> + Send + Sync,
351{
352	pub fn new(closure: F) -> Self {
353		Self {
354			closure,
355		}
356	}
357}
358
359impl<F> Clone for ClosureDictionaryRowPreDeleteInterceptor<F>
360where
361	F: for<'a> Fn(&mut DictionaryRowPreDeleteContext<'a>) -> Result<()> + Send + Sync + Clone,
362{
363	fn clone(&self) -> Self {
364		Self {
365			closure: self.closure.clone(),
366		}
367	}
368}
369
370impl<F> DictionaryRowPreDeleteInterceptor for ClosureDictionaryRowPreDeleteInterceptor<F>
371where
372	F: for<'a> Fn(&mut DictionaryRowPreDeleteContext<'a>) -> Result<()> + Send + Sync,
373{
374	fn intercept<'a>(&self, ctx: &mut DictionaryRowPreDeleteContext<'a>) -> Result<()> {
375		(self.closure)(ctx)
376	}
377}
378
379pub fn dictionary_row_pre_delete<F>(f: F) -> ClosureDictionaryRowPreDeleteInterceptor<F>
380where
381	F: for<'a> Fn(&mut DictionaryRowPreDeleteContext<'a>) -> Result<()> + Send + Sync + Clone + 'static,
382{
383	ClosureDictionaryRowPreDeleteInterceptor::new(f)
384}
385
386// POST DELETE
387pub struct DictionaryRowPostDeleteContext<'a> {
388	pub dictionary: &'a Dictionary,
389	pub id: DictionaryEntryId,
390	pub value: &'a Value,
391}
392
393impl<'a> DictionaryRowPostDeleteContext<'a> {
394	pub fn new(dictionary: &'a Dictionary, id: DictionaryEntryId, value: &'a Value) -> Self {
395		Self {
396			dictionary,
397			id,
398			value,
399		}
400	}
401}
402
403pub trait DictionaryRowPostDeleteInterceptor: Send + Sync {
404	fn intercept<'a>(&self, ctx: &mut DictionaryRowPostDeleteContext<'a>) -> Result<()>;
405}
406
407impl InterceptorChain<dyn DictionaryRowPostDeleteInterceptor + Send + Sync> {
408	pub fn execute(&self, mut ctx: DictionaryRowPostDeleteContext) -> Result<()> {
409		for interceptor in &self.interceptors {
410			interceptor.intercept(&mut ctx)?;
411		}
412		Ok(())
413	}
414}
415
416pub struct ClosureDictionaryRowPostDeleteInterceptor<F>
417where
418	F: for<'a> Fn(&mut DictionaryRowPostDeleteContext<'a>) -> Result<()> + Send + Sync,
419{
420	closure: F,
421}
422
423impl<F> ClosureDictionaryRowPostDeleteInterceptor<F>
424where
425	F: for<'a> Fn(&mut DictionaryRowPostDeleteContext<'a>) -> Result<()> + Send + Sync,
426{
427	pub fn new(closure: F) -> Self {
428		Self {
429			closure,
430		}
431	}
432}
433
434impl<F> Clone for ClosureDictionaryRowPostDeleteInterceptor<F>
435where
436	F: for<'a> Fn(&mut DictionaryRowPostDeleteContext<'a>) -> Result<()> + Send + Sync + Clone,
437{
438	fn clone(&self) -> Self {
439		Self {
440			closure: self.closure.clone(),
441		}
442	}
443}
444
445impl<F> DictionaryRowPostDeleteInterceptor for ClosureDictionaryRowPostDeleteInterceptor<F>
446where
447	F: for<'a> Fn(&mut DictionaryRowPostDeleteContext<'a>) -> Result<()> + Send + Sync,
448{
449	fn intercept<'a>(&self, ctx: &mut DictionaryRowPostDeleteContext<'a>) -> Result<()> {
450		(self.closure)(ctx)
451	}
452}
453
454pub fn dictionary_row_post_delete<F>(f: F) -> ClosureDictionaryRowPostDeleteInterceptor<F>
455where
456	F: for<'a> Fn(&mut DictionaryRowPostDeleteContext<'a>) -> Result<()> + Send + Sync + Clone + 'static,
457{
458	ClosureDictionaryRowPostDeleteInterceptor::new(f)
459}
460
461/// Helper struct for executing dictionary interceptors via static methods.
462pub struct DictionaryRowInterceptor;
463
464impl DictionaryRowInterceptor {
465	pub fn pre_insert(txn: &mut impl WithInterceptors, dictionary: &Dictionary, value: Value) -> Result<Value> {
466		let ctx = DictionaryRowPreInsertContext::new(dictionary, value);
467		txn.dictionary_row_pre_insert_interceptors().execute(ctx)
468	}
469
470	pub fn post_insert(
471		txn: &mut impl WithInterceptors,
472		dictionary: &Dictionary,
473		id: DictionaryEntryId,
474		value: &Value,
475	) -> Result<()> {
476		let ctx = DictionaryRowPostInsertContext::new(dictionary, id, value);
477		txn.dictionary_row_post_insert_interceptors().execute(ctx)
478	}
479
480	pub fn pre_update(
481		txn: &mut impl WithInterceptors,
482		dictionary: &Dictionary,
483		id: DictionaryEntryId,
484		value: Value,
485	) -> Result<Value> {
486		let ctx = DictionaryRowPreUpdateContext::new(dictionary, id, value);
487		txn.dictionary_row_pre_update_interceptors().execute(ctx)
488	}
489
490	pub fn post_update(
491		txn: &mut impl WithInterceptors,
492		dictionary: &Dictionary,
493		id: DictionaryEntryId,
494		post: &Value,
495		pre: &Value,
496	) -> Result<()> {
497		let ctx = DictionaryRowPostUpdateContext::new(dictionary, id, post, pre);
498		txn.dictionary_row_post_update_interceptors().execute(ctx)
499	}
500
501	pub fn pre_delete(
502		txn: &mut impl WithInterceptors,
503		dictionary: &Dictionary,
504		id: DictionaryEntryId,
505	) -> Result<()> {
506		let ctx = DictionaryRowPreDeleteContext::new(dictionary, id);
507		txn.dictionary_row_pre_delete_interceptors().execute(ctx)
508	}
509
510	pub fn post_delete(
511		txn: &mut impl WithInterceptors,
512		dictionary: &Dictionary,
513		id: DictionaryEntryId,
514		value: &Value,
515	) -> Result<()> {
516		let ctx = DictionaryRowPostDeleteContext::new(dictionary, id, value);
517		txn.dictionary_row_post_delete_interceptors().execute(ctx)
518	}
519}