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	},
29}
30
31impl Namespace {
32	pub fn id(&self) -> NamespaceId {
33		match self {
34			Namespace::Local {
35				id,
36				..
37			}
38			| Namespace::Remote {
39				id,
40				..
41			} => *id,
42		}
43	}
44
45	pub fn name(&self) -> &str {
46		match self {
47			Namespace::Local {
48				name,
49				..
50			}
51			| Namespace::Remote {
52				name,
53				..
54			} => name,
55		}
56	}
57
58	pub fn local_name(&self) -> &str {
59		match self {
60			Namespace::Local {
61				local_name,
62				..
63			}
64			| Namespace::Remote {
65				local_name,
66				..
67			} => local_name,
68		}
69	}
70
71	pub fn parent_id(&self) -> NamespaceId {
72		match self {
73			Namespace::Local {
74				parent_id,
75				..
76			}
77			| Namespace::Remote {
78				parent_id,
79				..
80			} => *parent_id,
81		}
82	}
83
84	pub fn address(&self) -> Option<&str> {
85		match self {
86			Namespace::Remote {
87				address,
88				..
89			} => Some(address),
90			_ => None,
91		}
92	}
93
94	pub fn is_remote(&self) -> bool {
95		matches!(self, Namespace::Remote { .. })
96	}
97
98	pub fn system() -> Self {
99		Self::Local {
100			id: NamespaceId::SYSTEM,
101			name: "system".to_string(),
102			local_name: "system".to_string(),
103			parent_id: NamespaceId::ROOT,
104		}
105	}
106
107	pub fn default_namespace() -> Self {
108		Self::Local {
109			id: NamespaceId::DEFAULT,
110			name: "default".to_string(),
111			local_name: "default".to_string(),
112			parent_id: NamespaceId::ROOT,
113		}
114	}
115}