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}
18
19#[derive(Debug, Clone, PartialEq)]
20pub enum Namespace {
21	Local {
22		id: NamespaceId,
23		name: String,
24		local_name: String,
25		parent_id: NamespaceId,
26	},
27	Remote {
28		id: NamespaceId,
29		name: String,
30		local_name: String,
31		parent_id: NamespaceId,
32		address: String,
33		token: Option<String>,
34	},
35}
36
37impl Namespace {
38	pub fn id(&self) -> NamespaceId {
39		match self {
40			Namespace::Local {
41				id,
42				..
43			}
44			| Namespace::Remote {
45				id,
46				..
47			} => *id,
48		}
49	}
50
51	pub fn name(&self) -> &str {
52		match self {
53			Namespace::Local {
54				name,
55				..
56			}
57			| Namespace::Remote {
58				name,
59				..
60			} => name,
61		}
62	}
63
64	pub fn local_name(&self) -> &str {
65		match self {
66			Namespace::Local {
67				local_name,
68				..
69			}
70			| Namespace::Remote {
71				local_name,
72				..
73			} => local_name,
74		}
75	}
76
77	pub fn parent_id(&self) -> NamespaceId {
78		match self {
79			Namespace::Local {
80				parent_id,
81				..
82			}
83			| Namespace::Remote {
84				parent_id,
85				..
86			} => *parent_id,
87		}
88	}
89
90	pub fn address(&self) -> Option<&str> {
91		match self {
92			Namespace::Remote {
93				address,
94				..
95			} => Some(address),
96			_ => None,
97		}
98	}
99
100	pub fn token(&self) -> Option<&str> {
101		match self {
102			Namespace::Remote {
103				token,
104				..
105			} => token.as_deref(),
106			_ => None,
107		}
108	}
109
110	pub fn is_remote(&self) -> bool {
111		matches!(self, Namespace::Remote { .. })
112	}
113
114	pub fn system() -> Self {
115		Self::Local {
116			id: NamespaceId::SYSTEM,
117			name: "system".to_string(),
118			local_name: "system".to_string(),
119			parent_id: NamespaceId::ROOT,
120		}
121	}
122
123	pub fn default_namespace() -> Self {
124		Self::Local {
125			id: NamespaceId::DEFAULT,
126			name: "default".to_string(),
127			local_name: "default".to_string(),
128			parent_id: NamespaceId::ROOT,
129		}
130	}
131}