Skip to main content

reifydb_core/interface/catalog/
namespace.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2025 ReifyDB
3
4use crate::interface::catalog::id::NamespaceId;
5
6impl NamespaceId {
7	/// Root sentinel — all top-level namespaces have `parent_id: NamespaceId::ROOT`.
8	/// This is not a real namespace, just the tree root.
9	pub const ROOT: NamespaceId = NamespaceId(0);
10	pub const SYSTEM: NamespaceId = NamespaceId(1);
11	pub const DEFAULT: NamespaceId = NamespaceId(2);
12	pub const SYSTEM_CONFIG: NamespaceId = NamespaceId(3);
13	pub const SYSTEM_METRICS: NamespaceId = NamespaceId(4);
14	pub const SYSTEM_METRICS_STORAGE: NamespaceId = NamespaceId(5);
15	pub const SYSTEM_METRICS_CDC: NamespaceId = NamespaceId(6);
16	pub const SYSTEM_PROCEDURES: NamespaceId = NamespaceId(7);
17	pub const SYSTEM_BINDINGS: NamespaceId = NamespaceId(8);
18}
19
20#[derive(Debug, Clone, PartialEq)]
21pub enum Namespace {
22	Local {
23		id: NamespaceId,
24		name: String,
25		local_name: String,
26		parent_id: NamespaceId,
27	},
28	Remote {
29		id: NamespaceId,
30		name: String,
31		local_name: String,
32		parent_id: NamespaceId,
33		address: String,
34		token: Option<String>,
35	},
36}
37
38impl Namespace {
39	pub fn id(&self) -> NamespaceId {
40		match self {
41			Namespace::Local {
42				id,
43				..
44			}
45			| Namespace::Remote {
46				id,
47				..
48			} => *id,
49		}
50	}
51
52	pub fn name(&self) -> &str {
53		match self {
54			Namespace::Local {
55				name,
56				..
57			}
58			| Namespace::Remote {
59				name,
60				..
61			} => name,
62		}
63	}
64
65	pub fn local_name(&self) -> &str {
66		match self {
67			Namespace::Local {
68				local_name,
69				..
70			}
71			| Namespace::Remote {
72				local_name,
73				..
74			} => local_name,
75		}
76	}
77
78	pub fn parent_id(&self) -> NamespaceId {
79		match self {
80			Namespace::Local {
81				parent_id,
82				..
83			}
84			| Namespace::Remote {
85				parent_id,
86				..
87			} => *parent_id,
88		}
89	}
90
91	pub fn address(&self) -> Option<&str> {
92		match self {
93			Namespace::Remote {
94				address,
95				..
96			} => Some(address),
97			_ => None,
98		}
99	}
100
101	pub fn token(&self) -> Option<&str> {
102		match self {
103			Namespace::Remote {
104				token,
105				..
106			} => token.as_deref(),
107			_ => None,
108		}
109	}
110
111	pub fn is_remote(&self) -> bool {
112		matches!(self, Namespace::Remote { .. })
113	}
114
115	pub fn system() -> Self {
116		Self::Local {
117			id: NamespaceId::SYSTEM,
118			name: "system".to_string(),
119			local_name: "system".to_string(),
120			parent_id: NamespaceId::ROOT,
121		}
122	}
123
124	pub fn default_namespace() -> Self {
125		Self::Local {
126			id: NamespaceId::DEFAULT,
127			name: "default".to_string(),
128			local_name: "default".to_string(),
129			parent_id: NamespaceId::ROOT,
130		}
131	}
132}