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