Skip to main content

reifydb_transaction/interceptor/
series_row.rs

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