Skip to main content

nichlink/registry_core/tree/
registry.rs

1// The one registry type, including atomic recursive registration and queries.
2// 唯一的 Registry 类型,包含原子递归注册和查询。
3
4use std::sync::Arc;
5
6use super::entry_pages::EntryPages;
7use super::header::RegistryHeader;
8pub use super::index::{RegistryIndex, RegistryStorageStats};
9use crate::registry_core::declaration::FrameworkId;
10use crate::registry_core::declaration::{
11    Admission, OwnedAdmission, OwnedRegistrationRule, RegistrationRule,
12};
13use crate::registry_core::identity::{NodeId, ROOT_NODE_ID, root_node_id};
14
15/// Parent and child levels use this exact type.
16/// 父级和子级使用完全相同的类型。
17#[derive(Clone, Debug)]
18pub struct Registry {
19    pub(super) header: Arc<RegistryHeader>,
20    // The map is immutable while a snapshot is shared. Transactions copy only
21    // the map page they mutate, not the whole registry tree.
22    // 快照共享时 map 不可变;事务只复制实际修改的 map 页,不复制整棵树。
23    pub(super) entries: Arc<EntryPages>,
24}
25
26impl Default for Registry {
27    fn default() -> Self {
28        Self::root()
29    }
30}
31
32impl Registry {
33    /// Create the one top-level registry.
34    /// 创建唯一的顶层注册机。
35    pub fn root() -> Self {
36        Self::new(
37            FrameworkId::new("nichlink.default"),
38            "nichlink.default".to_owned(),
39            "root",
40            ROOT_NODE_ID,
41            RegistrationRule::ANY.into_owned(),
42            "<registry-root>",
43            Admission::ANY.into_owned(),
44        )
45    }
46
47    /// Create a root for one framework namespace.
48    /// 为一个框架命名空间创建根注册机。
49    pub fn root_for(framework: FrameworkId) -> Self {
50        Self::root_for_namespace(framework, framework.0)
51    }
52
53    /// Create an isolated root for one package or application namespace.
54    /// 为一个包或应用命名空间创建隔离的根。
55    pub fn root_for_namespace(framework: FrameworkId, namespace: impl Into<String>) -> Self {
56        let namespace = namespace.into();
57        let id = root_node_id(&namespace);
58        Self::new(
59            framework,
60            namespace,
61            "root",
62            id,
63            RegistrationRule::ANY.into_owned(),
64            "<registry-root>",
65            Admission::ANY.into_owned(),
66        )
67    }
68
69    #[allow(clippy::too_many_arguments)]
70    pub(super) fn new(
71        framework: FrameworkId,
72        namespace: String,
73        path: &str,
74        id: NodeId,
75        registration_rule: OwnedRegistrationRule,
76        registration_rule_path: impl Into<String>,
77        admission: OwnedAdmission,
78    ) -> Self {
79        Self {
80            header: Arc::new(RegistryHeader {
81                namespace,
82                framework,
83                path: path.to_owned(),
84                id,
85                registration_rule,
86                registration_rule_path: registration_rule_path.into(),
87                admission,
88            }),
89            entries: Arc::new(EntryPages::default()),
90        }
91    }
92
93    /// The framework this registry was created under.
94    /// 该注册机创建时所用的框架。
95    pub fn framework(&self) -> FrameworkId {
96        self.header.framework
97    }
98}