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