reifydb-transaction 0.4.10

Transaction management and concurrency control for ReifyDB
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2025 ReifyDB

use std::sync::Arc;

use reifydb_core::{
	common::CommitVersion,
	encoded::key::{EncodedKey, EncodedKeyRange},
	execution::ExecutionResult,
	interface::{
		catalog::{
			authentication::{Authentication, AuthenticationId},
			config::{Config, ConfigKey},
			dictionary::Dictionary,
			flow::{Flow, FlowId},
			handler::Handler,
			id::{
				HandlerId, MigrationId, NamespaceId, ProcedureId, RingBufferId, SeriesId, SinkId,
				SourceId, TableId, TestId, ViewId,
			},
			identity::{GrantedRole, Identity, Role, RoleId},
			migration::Migration,
			namespace::Namespace,
			policy::{Policy, PolicyId},
			procedure::Procedure,
			ringbuffer::RingBuffer,
			series::Series,
			shape::ShapeId,
			sink::Sink,
			source::Source,
			sumtype::SumType,
			table::Table,
			test::Test,
			view::View,
		},
		store::{MultiVersionBatch, MultiVersionRow},
	},
	row::RowTtl,
};
use reifydb_type::{
	Result,
	params::Params,
	value::{dictionary::DictionaryId, identity::IdentityId, sumtype::SumTypeId},
};
use tracing::instrument;

use crate::{
	TransactionId,
	change::{
		TransactionalAuthenticationChanges, TransactionalChanges, TransactionalConfigChanges,
		TransactionalDictionaryChanges, TransactionalFlowChanges, TransactionalGrantedRoleChanges,
		TransactionalHandlerChanges, TransactionalIdentityChanges, TransactionalMigrationChanges,
		TransactionalNamespaceChanges, TransactionalPolicyChanges, TransactionalProcedureChanges,
		TransactionalRingBufferChanges, TransactionalRoleChanges, TransactionalRowTtlChanges,
		TransactionalSeriesChanges, TransactionalSinkChanges, TransactionalSourceChanges,
		TransactionalSumTypeChanges, TransactionalTableChanges, TransactionalTestChanges,
		TransactionalViewChanges,
	},
	multi::transaction::read::MultiReadTransaction,
	single::{SingleTransaction, read::SingleReadTransaction},
	transaction::{RqlExecutor, Transaction},
};

/// An active query transaction that holds a multi query transaction
/// and provides query-only access to single storage.
pub struct QueryTransaction {
	pub(crate) multi: MultiReadTransaction,
	pub(crate) single: SingleTransaction,

	/// The identity executing this transaction.
	pub identity: IdentityId,

	/// Optional RQL executor for running RQL within this transaction.
	pub(crate) executor: Option<Arc<dyn RqlExecutor>>,
}

impl QueryTransaction {
	/// Creates a new active query transaction
	#[instrument(name = "transaction::query::new", level = "debug", skip_all)]
	pub fn new(multi: MultiReadTransaction, single: SingleTransaction, identity: IdentityId) -> Self {
		Self {
			multi,
			single,
			identity,
			executor: None,
		}
	}

	/// Set the RQL executor for this transaction.
	pub fn set_executor(&mut self, executor: Arc<dyn RqlExecutor>) {
		self.executor = Some(executor);
	}

	/// Execute RQL within this transaction using the attached executor.
	///
	/// Panics if no `RqlExecutor` has been set on this transaction.
	pub fn rql(&mut self, rql: &str, params: Params) -> ExecutionResult {
		let executor = self.executor.clone().expect("RqlExecutor not set");
		executor.rql(&mut Transaction::Query(self), rql, params)
	}

	/// Get the transaction version
	#[inline]
	pub fn version(&self) -> CommitVersion {
		self.multi.version()
	}

	/// Get the transaction ID
	#[inline]
	pub fn id(&self) -> TransactionId {
		self.multi.tm.id()
	}

	/// Get a value by key
	#[inline]
	pub fn get(&mut self, key: &EncodedKey) -> Result<Option<MultiVersionRow>> {
		Ok(self.multi.get(key)?.map(|v| v.into_multi_version_row()))
	}

	/// Check if a key exists
	#[inline]
	pub fn contains_key(&mut self, key: &EncodedKey) -> Result<bool> {
		self.multi.contains_key(key)
	}

	/// Get a prefix batch
	#[inline]
	pub fn prefix(&mut self, prefix: &EncodedKey) -> Result<MultiVersionBatch> {
		self.multi.prefix(prefix)
	}

	/// Get a reverse prefix batch
	#[inline]
	pub fn prefix_rev(&mut self, prefix: &EncodedKey) -> Result<MultiVersionBatch> {
		self.multi.prefix_rev(prefix)
	}

	/// Read as of version exclusive
	#[inline]
	pub fn read_as_of_version_exclusive(&mut self, version: CommitVersion) -> Result<()> {
		self.multi.read_as_of_version_exclusive(version);
		Ok(())
	}

	/// Create a streaming iterator for forward range queries.
	#[inline]
	pub fn range(
		&self,
		range: EncodedKeyRange,
		batch_size: usize,
	) -> Box<dyn Iterator<Item = Result<MultiVersionRow>> + Send + '_> {
		self.multi.range(range, batch_size)
	}

	/// Create a streaming iterator for reverse range queries.
	#[inline]
	pub fn range_rev(
		&self,
		range: EncodedKeyRange,
		batch_size: usize,
	) -> Box<dyn Iterator<Item = Result<MultiVersionRow>> + Send + '_> {
		self.multi.range_rev(range, batch_size)
	}

	/// Execute a function with query access to the single transaction.
	#[instrument(name = "transaction::query::with_single_query", level = "trace", skip(self, keys, f))]
	pub fn with_single_query<'a, I, F, R>(&self, keys: I, f: F) -> Result<R>
	where
		I: IntoIterator<Item = &'a EncodedKey> + Send,
		F: FnOnce(&mut SingleReadTransaction<'_>) -> Result<R> + Send,
		R: Send,
	{
		self.single.with_query(keys, f)
	}

	/// Execute a function with access to the multi query transaction.
	/// This operates within the same transaction context.
	#[instrument(name = "transaction::query::with_multi_query", level = "trace", skip(self, f))]
	pub fn with_multi_query<F, R>(&mut self, f: F) -> Result<R>
	where
		F: FnOnce(&mut MultiReadTransaction) -> Result<R>,
	{
		f(&mut self.multi)
	}

	/// Begin a single-version query transaction for specific keys
	#[instrument(name = "transaction::query::begin_single_query", level = "trace", skip(self, keys))]
	pub fn begin_single_query<'a, I>(&self, keys: I) -> Result<SingleReadTransaction<'_>>
	where
		I: IntoIterator<Item = &'a EncodedKey>,
	{
		self.single.begin_query(keys)
	}
}

// No-op implementations of TransactionalChanges for QueryTransaction.
// Query transactions don't track changes, so all methods return None/false.

impl TransactionalDictionaryChanges for QueryTransaction {
	fn find_dictionary(&self, _id: DictionaryId) -> Option<&Dictionary> {
		None
	}

	fn find_dictionary_by_name(&self, _namespace: NamespaceId, _name: &str) -> Option<&Dictionary> {
		None
	}

	fn is_dictionary_deleted(&self, _id: DictionaryId) -> bool {
		false
	}

	fn is_dictionary_deleted_by_name(&self, _namespace: NamespaceId, _name: &str) -> bool {
		false
	}
}

impl TransactionalFlowChanges for QueryTransaction {
	fn find_flow(&self, _id: FlowId) -> Option<&Flow> {
		None
	}

	fn find_flow_by_name(&self, _namespace: NamespaceId, _name: &str) -> Option<&Flow> {
		None
	}

	fn is_flow_deleted(&self, _id: FlowId) -> bool {
		false
	}

	fn is_flow_deleted_by_name(&self, _namespace: NamespaceId, _name: &str) -> bool {
		false
	}
}

impl TransactionalNamespaceChanges for QueryTransaction {
	fn find_namespace(&self, _id: NamespaceId) -> Option<&Namespace> {
		None
	}

	fn find_namespace_by_name(&self, _name: &str) -> Option<&Namespace> {
		None
	}

	fn is_namespace_deleted(&self, _id: NamespaceId) -> bool {
		false
	}

	fn is_namespace_deleted_by_name(&self, _name: &str) -> bool {
		false
	}
}

impl TransactionalProcedureChanges for QueryTransaction {
	fn find_procedure(&self, _id: ProcedureId) -> Option<&Procedure> {
		None
	}

	fn find_procedure_by_name(&self, _namespace: NamespaceId, _name: &str) -> Option<&Procedure> {
		None
	}

	fn is_procedure_deleted(&self, _id: ProcedureId) -> bool {
		false
	}

	fn is_procedure_deleted_by_name(&self, _namespace: NamespaceId, _name: &str) -> bool {
		false
	}
}

impl TransactionalTestChanges for QueryTransaction {
	fn find_test(&self, _id: TestId) -> Option<&Test> {
		None
	}

	fn find_test_by_name(&self, _namespace: NamespaceId, _name: &str) -> Option<&Test> {
		None
	}

	fn is_test_deleted(&self, _id: TestId) -> bool {
		false
	}

	fn is_test_deleted_by_name(&self, _namespace: NamespaceId, _name: &str) -> bool {
		false
	}
}

impl TransactionalRingBufferChanges for QueryTransaction {
	fn find_ringbuffer(&self, _id: RingBufferId) -> Option<&RingBuffer> {
		None
	}

	fn find_ringbuffer_by_name(&self, _namespace: NamespaceId, _name: &str) -> Option<&RingBuffer> {
		None
	}

	fn is_ringbuffer_deleted(&self, _id: RingBufferId) -> bool {
		false
	}

	fn is_ringbuffer_deleted_by_name(&self, _namespace: NamespaceId, _name: &str) -> bool {
		false
	}
}

impl TransactionalSeriesChanges for QueryTransaction {
	fn find_series(&self, _id: SeriesId) -> Option<&Series> {
		None
	}

	fn find_series_by_name(&self, _namespace: NamespaceId, _name: &str) -> Option<&Series> {
		None
	}

	fn is_series_deleted(&self, _id: SeriesId) -> bool {
		false
	}

	fn is_series_deleted_by_name(&self, _namespace: NamespaceId, _name: &str) -> bool {
		false
	}
}

impl TransactionalTableChanges for QueryTransaction {
	fn find_table(&self, _id: TableId) -> Option<&Table> {
		None
	}

	fn find_table_by_name(&self, _namespace: NamespaceId, _name: &str) -> Option<&Table> {
		None
	}

	fn is_table_deleted(&self, _id: TableId) -> bool {
		false
	}

	fn is_table_deleted_by_name(&self, _namespace: NamespaceId, _name: &str) -> bool {
		false
	}
}

impl TransactionalViewChanges for QueryTransaction {
	fn find_view(&self, _id: ViewId) -> Option<&View> {
		None
	}

	fn find_view_by_name(&self, _namespace: NamespaceId, _name: &str) -> Option<&View> {
		None
	}

	fn is_view_deleted(&self, _id: ViewId) -> bool {
		false
	}

	fn is_view_deleted_by_name(&self, _namespace: NamespaceId, _name: &str) -> bool {
		false
	}
}

impl TransactionalSumTypeChanges for QueryTransaction {
	fn find_sumtype(&self, _id: SumTypeId) -> Option<&SumType> {
		None
	}

	fn find_sumtype_by_name(&self, _namespace: NamespaceId, _name: &str) -> Option<&SumType> {
		None
	}

	fn is_sumtype_deleted(&self, _id: SumTypeId) -> bool {
		false
	}

	fn is_sumtype_deleted_by_name(&self, _namespace: NamespaceId, _name: &str) -> bool {
		false
	}
}

impl TransactionalHandlerChanges for QueryTransaction {
	fn find_handler_by_id(&self, _id: HandlerId) -> Option<&Handler> {
		None
	}

	fn find_handler_by_name(&self, _namespace: NamespaceId, _name: &str) -> Option<&Handler> {
		None
	}

	fn is_handler_deleted_by_name(&self, _namespace: NamespaceId, _name: &str) -> bool {
		false
	}
}

impl TransactionalIdentityChanges for QueryTransaction {
	fn find_identity(&self, _id: IdentityId) -> Option<&Identity> {
		None
	}

	fn find_identity_by_name(&self, _name: &str) -> Option<&Identity> {
		None
	}

	fn is_identity_deleted(&self, _id: IdentityId) -> bool {
		false
	}

	fn is_identity_deleted_by_name(&self, _name: &str) -> bool {
		false
	}
}

impl TransactionalRoleChanges for QueryTransaction {
	fn find_role(&self, _id: RoleId) -> Option<&Role> {
		None
	}

	fn find_role_by_name(&self, _name: &str) -> Option<&Role> {
		None
	}

	fn is_role_deleted(&self, _id: RoleId) -> bool {
		false
	}

	fn is_role_deleted_by_name(&self, _name: &str) -> bool {
		false
	}
}

impl TransactionalGrantedRoleChanges for QueryTransaction {
	fn find_granted_role(&self, _identity: IdentityId, _role: RoleId) -> Option<&GrantedRole> {
		None
	}

	fn find_granted_roles_for_identity(&self, _identity: IdentityId) -> Vec<&GrantedRole> {
		Vec::new()
	}

	fn is_granted_role_deleted(&self, _identity: IdentityId, _role: RoleId) -> bool {
		false
	}
}

impl TransactionalPolicyChanges for QueryTransaction {
	fn find_policy(&self, _id: PolicyId) -> Option<&Policy> {
		None
	}

	fn find_policy_by_name(&self, _name: &str) -> Option<&Policy> {
		None
	}

	fn is_policy_deleted(&self, _id: PolicyId) -> bool {
		false
	}

	fn is_policy_deleted_by_name(&self, _name: &str) -> bool {
		false
	}
}

impl TransactionalMigrationChanges for QueryTransaction {
	fn find_migration(&self, _id: MigrationId) -> Option<&Migration> {
		None
	}

	fn find_migration_by_name(&self, _name: &str) -> Option<&Migration> {
		None
	}

	fn is_migration_deleted(&self, _id: MigrationId) -> bool {
		false
	}

	fn is_migration_deleted_by_name(&self, _name: &str) -> bool {
		false
	}
}

impl TransactionalAuthenticationChanges for QueryTransaction {
	fn find_authentication(&self, _id: AuthenticationId) -> Option<&Authentication> {
		None
	}

	fn find_authentication_by_identity_and_method(
		&self,
		_identity: IdentityId,
		_method: &str,
	) -> Option<&Authentication> {
		None
	}

	fn is_authentication_deleted(&self, _id: AuthenticationId) -> bool {
		false
	}
}

impl TransactionalSourceChanges for QueryTransaction {
	fn find_source(&self, _id: SourceId) -> Option<&Source> {
		None
	}

	fn find_source_by_name(&self, _namespace: NamespaceId, _name: &str) -> Option<&Source> {
		None
	}

	fn is_source_deleted(&self, _id: SourceId) -> bool {
		false
	}

	fn is_source_deleted_by_name(&self, _namespace: NamespaceId, _name: &str) -> bool {
		false
	}
}

impl TransactionalSinkChanges for QueryTransaction {
	fn find_sink(&self, _id: SinkId) -> Option<&Sink> {
		None
	}

	fn find_sink_by_name(&self, _namespace: NamespaceId, _name: &str) -> Option<&Sink> {
		None
	}

	fn is_sink_deleted(&self, _id: SinkId) -> bool {
		false
	}

	fn is_sink_deleted_by_name(&self, _namespace: NamespaceId, _name: &str) -> bool {
		false
	}
}

impl TransactionalConfigChanges for QueryTransaction {
	fn find_config(&self, _key: ConfigKey) -> Option<&Config> {
		None
	}
}

impl TransactionalRowTtlChanges for QueryTransaction {
	fn find_row_ttl(&self, _shape: ShapeId) -> Option<&RowTtl> {
		None
	}

	fn is_row_ttl_deleted(&self, _shape: ShapeId) -> bool {
		false
	}
}

impl TransactionalChanges for QueryTransaction {}