reifydb-catalog 0.4.13

Database catalog and metadata management 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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2025 ReifyDB

use std::collections::HashSet;

use reifydb_core::interface::catalog::{
	change::{
		CatalogTrackGrantedRoleChangeOperations, CatalogTrackIdentityChangeOperations,
		CatalogTrackRoleChangeOperations,
	},
	identity::{GrantedRole, Identity, Role, RoleId},
};
use reifydb_runtime::context::{clock::Clock, rng::Rng};
use reifydb_transaction::{
	change::{TransactionalGrantedRoleChanges, TransactionalIdentityChanges, TransactionalRoleChanges},
	transaction::{Transaction, admin::AdminTransaction},
};
use reifydb_type::{fragment::Fragment, value::identity::IdentityId};
use tracing::{instrument, warn};

use crate::{
	CatalogStore, Result,
	catalog::Catalog,
	error::{CatalogError, CatalogObjectKind},
};

impl Catalog {
	#[instrument(name = "catalog::identity::find_by_name", level = "trace", skip(self, txn))]
	pub fn find_identity_by_name(&self, txn: &mut Transaction<'_>, name: &str) -> Result<Option<Identity>> {
		match txn.reborrow() {
			Transaction::Admin(admin) => {
				// 1. Check transactional changes first
				if let Some(ident) = TransactionalIdentityChanges::find_identity_by_name(admin, name) {
					return Ok(Some(ident.clone()));
				}

				// 2. Check if deleted
				if TransactionalIdentityChanges::is_identity_deleted_by_name(admin, name) {
					return Ok(None);
				}

				// 3. Check MaterializedCatalog
				if let Some(ident) = self.materialized.find_identity_by_name_at(name, admin.version()) {
					return Ok(Some(ident));
				}

				// 4. Fall back to storage
				if let Some(ident) =
					CatalogStore::find_identity_by_name(&mut Transaction::Admin(&mut *admin), name)?
				{
					warn!("Identity '{}' found in storage but not in MaterializedCatalog", name);
					return Ok(Some(ident));
				}

				Ok(None)
			}
			Transaction::Command(cmd) => {
				if let Some(ident) = self.materialized.find_identity_by_name_at(name, cmd.version()) {
					return Ok(Some(ident));
				}

				if let Some(ident) =
					CatalogStore::find_identity_by_name(&mut Transaction::Command(&mut *cmd), name)?
				{
					warn!("Identity '{}' found in storage but not in MaterializedCatalog", name);
					return Ok(Some(ident));
				}

				Ok(None)
			}
			Transaction::Query(qry) => {
				if let Some(ident) = self.materialized.find_identity_by_name_at(name, qry.version()) {
					return Ok(Some(ident));
				}

				if let Some(ident) =
					CatalogStore::find_identity_by_name(&mut Transaction::Query(&mut *qry), name)?
				{
					warn!("Identity '{}' found in storage but not in MaterializedCatalog", name);
					return Ok(Some(ident));
				}

				Ok(None)
			}
			Transaction::Test(t) => {
				// 1. Check transactional changes first
				if let Some(ident) = TransactionalIdentityChanges::find_identity_by_name(t.inner, name)
				{
					return Ok(Some(ident.clone()));
				}

				// 2. Check if deleted
				if TransactionalIdentityChanges::is_identity_deleted_by_name(t.inner, name) {
					return Ok(None);
				}

				// 3. Check MaterializedCatalog
				if let Some(ident) = self.materialized.find_identity_by_name_at(name, t.inner.version())
				{
					return Ok(Some(ident));
				}

				// 4. Fall back to storage
				if let Some(ident) = CatalogStore::find_identity_by_name(
					&mut Transaction::Admin(&mut *t.inner),
					name,
				)? {
					warn!("Identity '{}' found in storage but not in MaterializedCatalog", name);
					return Ok(Some(ident));
				}

				Ok(None)
			}
			Transaction::Replica(rep) => {
				if let Some(ident) = self.materialized.find_identity_by_name_at(name, rep.version()) {
					return Ok(Some(ident));
				}

				if let Some(ident) =
					CatalogStore::find_identity_by_name(&mut Transaction::Replica(&mut *rep), name)?
				{
					warn!("Identity '{}' found in storage but not in MaterializedCatalog", name);
					return Ok(Some(ident));
				}

				Ok(None)
			}
		}
	}

	#[instrument(name = "catalog::identity::find", level = "trace", skip(self, txn))]
	pub fn find_identity(&self, txn: &mut Transaction<'_>, identity: IdentityId) -> Result<Option<Identity>> {
		match txn.reborrow() {
			Transaction::Admin(admin) => {
				// 1. Check transactional changes first
				if let Some(ident) = TransactionalIdentityChanges::find_identity(admin, identity) {
					return Ok(Some(ident.clone()));
				}

				// 2. Check if deleted
				if TransactionalIdentityChanges::is_identity_deleted(admin, identity) {
					return Ok(None);
				}

				// 3. Check MaterializedCatalog
				if let Some(ident) = self.materialized.find_identity_at(identity, admin.version()) {
					return Ok(Some(ident));
				}

				// 4. Fall back to storage
				if let Some(ident) =
					CatalogStore::find_identity(&mut Transaction::Admin(&mut *admin), identity)?
				{
					warn!(
						"Identity '{}' found in storage but not in MaterializedCatalog",
						identity
					);
					return Ok(Some(ident));
				}

				Ok(None)
			}
			Transaction::Command(cmd) => {
				if let Some(ident) = self.materialized.find_identity_at(identity, cmd.version()) {
					return Ok(Some(ident));
				}

				if let Some(ident) =
					CatalogStore::find_identity(&mut Transaction::Command(&mut *cmd), identity)?
				{
					warn!(
						"Identity '{}' found in storage but not in MaterializedCatalog",
						identity
					);
					return Ok(Some(ident));
				}

				Ok(None)
			}
			Transaction::Query(qry) => {
				if let Some(ident) = self.materialized.find_identity_at(identity, qry.version()) {
					return Ok(Some(ident));
				}

				if let Some(ident) =
					CatalogStore::find_identity(&mut Transaction::Query(&mut *qry), identity)?
				{
					warn!(
						"Identity '{}' found in storage but not in MaterializedCatalog",
						identity
					);
					return Ok(Some(ident));
				}

				Ok(None)
			}
			Transaction::Test(t) => {
				// 1. Check transactional changes first
				if let Some(ident) = TransactionalIdentityChanges::find_identity(t.inner, identity) {
					return Ok(Some(ident.clone()));
				}

				// 2. Check if deleted
				if TransactionalIdentityChanges::is_identity_deleted(t.inner, identity) {
					return Ok(None);
				}

				// 3. Check MaterializedCatalog
				if let Some(ident) = self.materialized.find_identity_at(identity, t.inner.version()) {
					return Ok(Some(ident));
				}

				// 4. Fall back to storage
				if let Some(ident) =
					CatalogStore::find_identity(&mut Transaction::Admin(&mut *t.inner), identity)?
				{
					warn!(
						"Identity '{}' found in storage but not in MaterializedCatalog",
						identity
					);
					return Ok(Some(ident));
				}

				Ok(None)
			}
			Transaction::Replica(rep) => {
				if let Some(ident) = self.materialized.find_identity_at(identity, rep.version()) {
					return Ok(Some(ident));
				}

				if let Some(ident) =
					CatalogStore::find_identity(&mut Transaction::Replica(&mut *rep), identity)?
				{
					warn!(
						"Identity '{}' found in storage but not in MaterializedCatalog",
						identity
					);
					return Ok(Some(ident));
				}

				Ok(None)
			}
		}
	}

	#[instrument(name = "catalog::identity::create", level = "debug", skip(self, txn, clock, rng))]
	pub fn create_identity(
		&self,
		txn: &mut AdminTransaction,
		name: &str,
		clock: &Clock,
		rng: &Rng,
	) -> Result<Identity> {
		let ident = CatalogStore::create_identity(txn, name, clock, rng)?;
		txn.track_identity_created(ident.clone())?;
		Ok(ident)
	}

	#[instrument(name = "catalog::identity::drop", level = "debug", skip(self, txn))]
	pub fn drop_identity(&self, txn: &mut AdminTransaction, identity: IdentityId) -> Result<()> {
		// Get the identity def before dropping for change tracking
		if let Some(ident) = CatalogStore::find_identity(&mut Transaction::Admin(&mut *txn), identity)? {
			CatalogStore::drop_identity(txn, identity)?;
			txn.track_identity_deleted(ident)?;
		} else {
			CatalogStore::drop_identity(txn, identity)?;
		}
		Ok(())
	}

	#[instrument(name = "catalog::identity::list_all", level = "debug", skip(self, txn))]
	pub fn list_identities_all(&self, txn: &mut Transaction<'_>) -> Result<Vec<Identity>> {
		match txn.reborrow() {
			Transaction::Command(cmd) => Ok(self.materialized.list_all_identities_at(cmd.version())),
			Transaction::Admin(admin) => {
				let mut idents = self.materialized.list_all_identities_at(admin.version());
				for change in &admin.changes.identity {
					if let Some(ident) = &change.post
						&& !idents.iter().any(|existing| existing.id == ident.id)
					{
						idents.push(ident.clone());
					}
				}
				idents.retain(|i| !admin.is_identity_deleted(i.id));
				Ok(idents)
			}
			Transaction::Query(qry) => Ok(self.materialized.list_all_identities_at(qry.version())),
			Transaction::Test(t) => {
				let mut idents = self.materialized.list_all_identities_at(t.inner.version());
				for change in &t.inner.changes.identity {
					if let Some(ident) = &change.post
						&& !idents.iter().any(|existing| existing.id == ident.id)
					{
						idents.push(ident.clone());
					}
				}
				idents.retain(|i| !t.inner.is_identity_deleted(i.id));
				Ok(idents)
			}
			Transaction::Replica(rep) => Ok(self.materialized.list_all_identities_at(rep.version())),
		}
	}

	#[instrument(name = "catalog::role::find_by_name", level = "trace", skip(self, txn))]
	pub fn find_role_by_name(&self, txn: &mut Transaction<'_>, name: &str) -> Result<Option<Role>> {
		match txn.reborrow() {
			Transaction::Admin(admin) => {
				if let Some(role) = TransactionalRoleChanges::find_role_by_name(admin, name) {
					return Ok(Some(role.clone()));
				}

				if TransactionalRoleChanges::is_role_deleted_by_name(admin, name) {
					return Ok(None);
				}

				if let Some(role) = self.materialized.find_role_by_name_at(name, admin.version()) {
					return Ok(Some(role));
				}

				if let Some(role) =
					CatalogStore::find_role_by_name(&mut Transaction::Admin(&mut *admin), name)?
				{
					warn!("Role '{}' found in storage but not in MaterializedCatalog", name);
					return Ok(Some(role));
				}

				Ok(None)
			}
			Transaction::Command(cmd) => {
				if let Some(role) = self.materialized.find_role_by_name_at(name, cmd.version()) {
					return Ok(Some(role));
				}

				if let Some(role) =
					CatalogStore::find_role_by_name(&mut Transaction::Command(&mut *cmd), name)?
				{
					warn!("Role '{}' found in storage but not in MaterializedCatalog", name);
					return Ok(Some(role));
				}

				Ok(None)
			}
			Transaction::Query(qry) => {
				if let Some(role) = self.materialized.find_role_by_name_at(name, qry.version()) {
					return Ok(Some(role));
				}

				if let Some(role) =
					CatalogStore::find_role_by_name(&mut Transaction::Query(&mut *qry), name)?
				{
					warn!("Role '{}' found in storage but not in MaterializedCatalog", name);
					return Ok(Some(role));
				}

				Ok(None)
			}
			Transaction::Test(t) => {
				if let Some(role) = TransactionalRoleChanges::find_role_by_name(t.inner, name) {
					return Ok(Some(role.clone()));
				}

				if TransactionalRoleChanges::is_role_deleted_by_name(t.inner, name) {
					return Ok(None);
				}

				if let Some(role) = self.materialized.find_role_by_name_at(name, t.inner.version()) {
					return Ok(Some(role));
				}

				if let Some(role) =
					CatalogStore::find_role_by_name(&mut Transaction::Admin(&mut *t.inner), name)?
				{
					warn!("Role '{}' found in storage but not in MaterializedCatalog", name);
					return Ok(Some(role));
				}

				Ok(None)
			}
			Transaction::Replica(rep) => {
				if let Some(role) = self.materialized.find_role_by_name_at(name, rep.version()) {
					return Ok(Some(role));
				}

				if let Some(role) =
					CatalogStore::find_role_by_name(&mut Transaction::Replica(&mut *rep), name)?
				{
					warn!("Role '{}' found in storage but not in MaterializedCatalog", name);
					return Ok(Some(role));
				}

				Ok(None)
			}
		}
	}

	#[instrument(name = "catalog::role::find", level = "trace", skip(self, txn))]
	pub fn find_role(&self, txn: &mut Transaction<'_>, role_id: RoleId) -> Result<Option<Role>> {
		match txn.reborrow() {
			Transaction::Admin(admin) => {
				// 1. Check transactional changes first
				if let Some(role) = TransactionalRoleChanges::find_role(admin, role_id) {
					return Ok(Some(role.clone()));
				}

				// 2. Check if deleted
				if TransactionalRoleChanges::is_role_deleted(admin, role_id) {
					return Ok(None);
				}

				// 3. Check MaterializedCatalog
				if let Some(role) = self.materialized.find_role_at(role_id, admin.version()) {
					return Ok(Some(role));
				}

				// 4. Fall back to storage
				if let Some(role) =
					CatalogStore::find_role(&mut Transaction::Admin(&mut *admin), role_id)?
				{
					warn!("Role '{}' found in storage but not in MaterializedCatalog", role_id);
					return Ok(Some(role));
				}

				Ok(None)
			}
			Transaction::Command(cmd) => {
				if let Some(role) = self.materialized.find_role_at(role_id, cmd.version()) {
					return Ok(Some(role));
				}

				if let Some(role) =
					CatalogStore::find_role(&mut Transaction::Command(&mut *cmd), role_id)?
				{
					warn!("Role '{}' found in storage but not in MaterializedCatalog", role_id);
					return Ok(Some(role));
				}

				Ok(None)
			}
			Transaction::Query(qry) => {
				if let Some(role) = self.materialized.find_role_at(role_id, qry.version()) {
					return Ok(Some(role));
				}

				if let Some(role) =
					CatalogStore::find_role(&mut Transaction::Query(&mut *qry), role_id)?
				{
					warn!("Role '{}' found in storage but not in MaterializedCatalog", role_id);
					return Ok(Some(role));
				}

				Ok(None)
			}
			Transaction::Test(t) => {
				// 1. Check transactional changes first
				if let Some(role) = TransactionalRoleChanges::find_role(t.inner, role_id) {
					return Ok(Some(role.clone()));
				}

				// 2. Check if deleted
				if TransactionalRoleChanges::is_role_deleted(t.inner, role_id) {
					return Ok(None);
				}

				// 3. Check MaterializedCatalog
				if let Some(role) = self.materialized.find_role_at(role_id, t.inner.version()) {
					return Ok(Some(role));
				}

				// 4. Fall back to storage
				if let Some(role) =
					CatalogStore::find_role(&mut Transaction::Admin(&mut *t.inner), role_id)?
				{
					warn!("Role '{}' found in storage but not in MaterializedCatalog", role_id);
					return Ok(Some(role));
				}

				Ok(None)
			}
			Transaction::Replica(rep) => {
				if let Some(role) = self.materialized.find_role_at(role_id, rep.version()) {
					return Ok(Some(role));
				}

				if let Some(role) =
					CatalogStore::find_role(&mut Transaction::Replica(&mut *rep), role_id)?
				{
					warn!("Role '{}' found in storage but not in MaterializedCatalog", role_id);
					return Ok(Some(role));
				}

				Ok(None)
			}
		}
	}

	#[instrument(name = "catalog::role::create", level = "debug", skip(self, txn))]
	pub fn create_role(&self, txn: &mut AdminTransaction, name: &str) -> Result<Role> {
		let role = CatalogStore::create_role(txn, name)?;
		txn.track_role_created(role.clone())?;
		Ok(role)
	}

	#[instrument(name = "catalog::role::drop", level = "debug", skip(self, txn))]
	pub fn drop_role(&self, txn: &mut AdminTransaction, role_id: RoleId) -> Result<()> {
		if let Some(role) = CatalogStore::find_role(&mut Transaction::Admin(&mut *txn), role_id)? {
			CatalogStore::drop_role(txn, role_id)?;
			txn.track_role_deleted(role)?;
		} else {
			CatalogStore::drop_role(txn, role_id)?;
		}
		Ok(())
	}

	#[instrument(name = "catalog::identity::grant_role", level = "debug", skip(self, txn))]
	pub fn grant_role(
		&self,
		txn: &mut AdminTransaction,
		identity: IdentityId,
		role_id: RoleId,
	) -> Result<GrantedRole> {
		let ir = CatalogStore::grant_role(txn, identity, role_id)?;
		txn.track_granted_role_created(ir.clone())?;
		Ok(ir)
	}

	#[instrument(name = "catalog::identity::revoke_role", level = "debug", skip(self, txn))]
	pub fn revoke_role(&self, txn: &mut AdminTransaction, identity: IdentityId, role_id: RoleId) -> Result<()> {
		let ir = GrantedRole {
			identity,
			role_id,
		};
		CatalogStore::revoke_role(txn, identity, role_id)?;
		txn.track_granted_role_deleted(ir)?;
		Ok(())
	}

	#[instrument(name = "catalog::identity::find_role_names_for_identity", level = "trace", skip(self, txn))]
	pub fn find_role_names_for_identity(
		&self,
		txn: &mut Transaction<'_>,
		identity: IdentityId,
	) -> Result<Vec<String>> {
		match txn.reborrow() {
			Transaction::Admin(admin) => {
				let version = admin.version();
				let mut names = Vec::new();
				let mut seen_roles = HashSet::new();

				// 1. Check transactional granted-role changes first
				for ir in TransactionalGrantedRoleChanges::find_granted_roles_for_identity(
					admin, identity,
				) {
					if !TransactionalRoleChanges::is_role_deleted(admin, ir.role_id) {
						if let Some(role) =
							TransactionalRoleChanges::find_role(admin, ir.role_id)
						{
							seen_roles.insert(ir.role_id);
							names.push(role.name.clone());
						} else if let Some(role) =
							self.materialized.find_role_at(ir.role_id, version)
						{
							seen_roles.insert(ir.role_id);
							names.push(role.name);
						}
					}
				}

				// 2. Check materialized granted-roles
				for ir in self.materialized.find_granted_roles_at(identity, version) {
					if !seen_roles.contains(&ir.role_id)
						&& !TransactionalGrantedRoleChanges::is_granted_role_deleted(
							admin, identity, ir.role_id,
						) && let Some(role) = self.materialized.find_role_at(ir.role_id, version)
					{
						names.push(role.name);
					}
				}

				Ok(names)
			}
			Transaction::Test(t) => {
				let version = t.inner.version();
				let mut names = Vec::new();
				let mut seen_roles = HashSet::new();

				// 1. Check transactional granted-role changes first
				for ir in TransactionalGrantedRoleChanges::find_granted_roles_for_identity(
					t.inner, identity,
				) {
					if !TransactionalRoleChanges::is_role_deleted(t.inner, ir.role_id) {
						if let Some(role) =
							TransactionalRoleChanges::find_role(t.inner, ir.role_id)
						{
							seen_roles.insert(ir.role_id);
							names.push(role.name.clone());
						} else if let Some(role) =
							self.materialized.find_role_at(ir.role_id, version)
						{
							seen_roles.insert(ir.role_id);
							names.push(role.name);
						}
					}
				}

				// 2. Check materialized granted-roles
				for ir in self.materialized.find_granted_roles_at(identity, version) {
					if !seen_roles.contains(&ir.role_id)
						&& !TransactionalGrantedRoleChanges::is_granted_role_deleted(
							t.inner, identity, ir.role_id,
						) && let Some(role) = self.materialized.find_role_at(ir.role_id, version)
					{
						names.push(role.name);
					}
				}

				Ok(names)
			}
			_ => {
				let version = match txn.reborrow() {
					Transaction::Command(cmd) => cmd.version(),
					Transaction::Query(qry) => qry.version(),
					Transaction::Replica(rep) => rep.version(),
					_ => unreachable!(),
				};

				let granted_roles = self.materialized.find_granted_roles_at(identity, version);
				let mut names = Vec::with_capacity(granted_roles.len());
				for ir in granted_roles {
					if let Some(role) = self.materialized.find_role_at(ir.role_id, version) {
						names.push(role.name);
					}
				}
				Ok(names)
			}
		}
	}

	pub fn get_identity_by_name(&self, txn: &mut Transaction<'_>, name: &str) -> Result<Identity> {
		self.find_identity_by_name(txn, name)?.ok_or_else(|| {
			CatalogError::NotFound {
				kind: CatalogObjectKind::Identity,
				namespace: "system".to_string(),
				name: name.to_string(),
				fragment: Fragment::None,
			}
			.into()
		})
	}

	pub fn get_role_by_name(&self, txn: &mut Transaction<'_>, name: &str) -> Result<Role> {
		self.find_role_by_name(txn, name)?.ok_or_else(|| {
			CatalogError::NotFound {
				kind: CatalogObjectKind::Role,
				namespace: "system".to_string(),
				name: name.to_string(),
				fragment: Fragment::None,
			}
			.into()
		})
	}
}