reifydb_core/interceptor/
interceptors.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 std::{marker::PhantomData, rc::Rc};
5
6use crate::{
7	interceptor::{
8		Chain, InterceptorChain, NamespaceDefPostCreateInterceptor, NamespaceDefPostUpdateInterceptor,
9		NamespaceDefPreDeleteInterceptor, NamespaceDefPreUpdateInterceptor, PostCommitInterceptor,
10		PreCommitInterceptor, RingBufferPostDeleteInterceptor, RingBufferPostInsertInterceptor,
11		RingBufferPostUpdateInterceptor, RingBufferPreDeleteInterceptor, RingBufferPreInsertInterceptor,
12		RingBufferPreUpdateInterceptor, TableDefPostCreateInterceptor, TableDefPostUpdateInterceptor,
13		TableDefPreDeleteInterceptor, TableDefPreUpdateInterceptor, TablePostDeleteInterceptor,
14		TablePostInsertInterceptor, TablePostUpdateInterceptor, TablePreDeleteInterceptor,
15		TablePreInsertInterceptor, TablePreUpdateInterceptor, ViewDefPostCreateInterceptor,
16		ViewDefPostUpdateInterceptor, ViewDefPreDeleteInterceptor, ViewDefPreUpdateInterceptor,
17	},
18	interface::CommandTransaction,
19};
20
21/// Container for all interceptor chains
22pub struct Interceptors<CT: CommandTransaction> {
23	// Table data interceptors
24	pub table_pre_insert: Chain<CT, dyn TablePreInsertInterceptor<CT>>,
25	pub table_post_insert: Chain<CT, dyn TablePostInsertInterceptor<CT>>,
26	pub table_pre_update: Chain<CT, dyn TablePreUpdateInterceptor<CT>>,
27	pub table_post_update: Chain<CT, dyn TablePostUpdateInterceptor<CT>>,
28	pub table_pre_delete: Chain<CT, dyn TablePreDeleteInterceptor<CT>>,
29	pub table_post_delete: Chain<CT, dyn TablePostDeleteInterceptor<CT>>,
30	// Ring buffer data interceptors
31	pub ring_buffer_pre_insert: Chain<CT, dyn RingBufferPreInsertInterceptor<CT>>,
32	pub ring_buffer_post_insert: Chain<CT, dyn RingBufferPostInsertInterceptor<CT>>,
33	pub ring_buffer_pre_update: Chain<CT, dyn RingBufferPreUpdateInterceptor<CT>>,
34	pub ring_buffer_post_update: Chain<CT, dyn RingBufferPostUpdateInterceptor<CT>>,
35	pub ring_buffer_pre_delete: Chain<CT, dyn RingBufferPreDeleteInterceptor<CT>>,
36	pub ring_buffer_post_delete: Chain<CT, dyn RingBufferPostDeleteInterceptor<CT>>,
37	// Transaction interceptors
38	pub pre_commit: Chain<CT, dyn PreCommitInterceptor<CT>>,
39	pub post_commit: Chain<CT, dyn PostCommitInterceptor<CT>>,
40	// Namespace definition interceptors
41	pub namespace_def_post_create: Chain<CT, dyn NamespaceDefPostCreateInterceptor<CT>>,
42	pub namespace_def_pre_update: Chain<CT, dyn NamespaceDefPreUpdateInterceptor<CT>>,
43	pub namespace_def_post_update: Chain<CT, dyn NamespaceDefPostUpdateInterceptor<CT>>,
44	pub namespace_def_pre_delete: Chain<CT, dyn NamespaceDefPreDeleteInterceptor<CT>>,
45	// Table definition interceptors
46	pub table_def_post_create: Chain<CT, dyn TableDefPostCreateInterceptor<CT>>,
47	pub table_def_pre_update: Chain<CT, dyn TableDefPreUpdateInterceptor<CT>>,
48	pub table_def_post_update: Chain<CT, dyn TableDefPostUpdateInterceptor<CT>>,
49	pub table_def_pre_delete: Chain<CT, dyn TableDefPreDeleteInterceptor<CT>>,
50	// View definition interceptors
51	pub view_def_post_create: Chain<CT, dyn ViewDefPostCreateInterceptor<CT>>,
52	pub view_def_pre_update: Chain<CT, dyn ViewDefPreUpdateInterceptor<CT>>,
53	pub view_def_post_update: Chain<CT, dyn ViewDefPostUpdateInterceptor<CT>>,
54	pub view_def_pre_delete: Chain<CT, dyn ViewDefPreDeleteInterceptor<CT>>,
55	// Marker to prevent Send and Sync
56	_not_send_sync: PhantomData<*const ()>,
57}
58
59impl<CT: CommandTransaction> Default for Interceptors<CT> {
60	fn default() -> Self {
61		Self::new()
62	}
63}
64
65impl<CT: CommandTransaction> Interceptors<CT> {
66	pub fn new() -> Self {
67		Self {
68			table_pre_insert: InterceptorChain::new(),
69			table_post_insert: InterceptorChain::new(),
70			table_pre_update: InterceptorChain::new(),
71			table_post_update: InterceptorChain::new(),
72			table_pre_delete: InterceptorChain::new(),
73			table_post_delete: InterceptorChain::new(),
74			ring_buffer_pre_insert: InterceptorChain::new(),
75			ring_buffer_post_insert: InterceptorChain::new(),
76			ring_buffer_pre_update: InterceptorChain::new(),
77			ring_buffer_post_update: InterceptorChain::new(),
78			ring_buffer_pre_delete: InterceptorChain::new(),
79			ring_buffer_post_delete: InterceptorChain::new(),
80			pre_commit: InterceptorChain::new(),
81			post_commit: InterceptorChain::new(),
82			namespace_def_post_create: InterceptorChain::new(),
83			namespace_def_pre_update: InterceptorChain::new(),
84			namespace_def_post_update: InterceptorChain::new(),
85			namespace_def_pre_delete: InterceptorChain::new(),
86			table_def_post_create: InterceptorChain::new(),
87			table_def_pre_update: InterceptorChain::new(),
88			table_def_post_update: InterceptorChain::new(),
89			table_def_pre_delete: InterceptorChain::new(),
90			view_def_post_create: InterceptorChain::new(),
91			view_def_pre_update: InterceptorChain::new(),
92			view_def_post_update: InterceptorChain::new(),
93			view_def_pre_delete: InterceptorChain::new(),
94			_not_send_sync: PhantomData,
95		}
96	}
97}
98
99impl<CT: CommandTransaction> Clone for Interceptors<CT> {
100	fn clone(&self) -> Self {
101		Self {
102			table_pre_insert: self.table_pre_insert.clone(),
103			table_post_insert: self.table_post_insert.clone(),
104			table_pre_update: self.table_pre_update.clone(),
105			table_post_update: self.table_post_update.clone(),
106			table_pre_delete: self.table_pre_delete.clone(),
107			table_post_delete: self.table_post_delete.clone(),
108			ring_buffer_pre_insert: self.ring_buffer_pre_insert.clone(),
109			ring_buffer_post_insert: self.ring_buffer_post_insert.clone(),
110			ring_buffer_pre_update: self.ring_buffer_pre_update.clone(),
111			ring_buffer_post_update: self.ring_buffer_post_update.clone(),
112			ring_buffer_pre_delete: self.ring_buffer_pre_delete.clone(),
113			ring_buffer_post_delete: self.ring_buffer_post_delete.clone(),
114			pre_commit: self.pre_commit.clone(),
115			post_commit: self.post_commit.clone(),
116			namespace_def_post_create: self.namespace_def_post_create.clone(),
117			namespace_def_pre_update: self.namespace_def_pre_update.clone(),
118			namespace_def_post_update: self.namespace_def_post_update.clone(),
119			namespace_def_pre_delete: self.namespace_def_pre_delete.clone(),
120			table_def_post_create: self.table_def_post_create.clone(),
121			table_def_pre_update: self.table_def_pre_update.clone(),
122			table_def_post_update: self.table_def_post_update.clone(),
123			table_def_pre_delete: self.table_def_pre_delete.clone(),
124			view_def_post_create: self.view_def_post_create.clone(),
125			view_def_pre_update: self.view_def_pre_update.clone(),
126			view_def_post_update: self.view_def_post_update.clone(),
127			view_def_pre_delete: self.view_def_pre_delete.clone(),
128			_not_send_sync: PhantomData,
129		}
130	}
131}
132
133impl<CT: CommandTransaction> Interceptors<CT> {
134	/// Register any interceptor - it will be added to all appropriate
135	/// chains based on which traits it implements
136	pub fn register<I>(&mut self, interceptor: I)
137	where
138		I: super::RegisterInterceptor<CT> + 'static,
139	{
140		Rc::new(interceptor).register(self);
141	}
142}