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    pub fn root_for_namespace(framework: FrameworkId, namespace: impl Into<String>) -> Self {
55        let namespace = namespace.into();
56        let id = root_node_id(&namespace);
57        Self::new(
58            framework,
59            namespace,
60            "root",
61            id,
62            RegistrationRule::ANY.into_owned(),
63            "<registry-root>",
64            Admission::ANY.into_owned(),
65        )
66    }
67
68    #[allow(clippy::too_many_arguments)]
69    pub(super) fn new(
70        framework: FrameworkId,
71        namespace: String,
72        path: &str,
73        id: NodeId,
74        registration_rule: OwnedRegistrationRule,
75        registration_rule_path: impl Into<String>,
76        admission: OwnedAdmission,
77    ) -> Self {
78        Self {
79            header: Arc::new(RegistryHeader {
80                namespace,
81                framework,
82                path: path.to_owned(),
83                id,
84                registration_rule,
85                registration_rule_path: registration_rule_path.into(),
86                admission,
87            }),
88            entries: Arc::new(EntryPages::default()),
89        }
90    }
91
92    /// The framework this registry was created under.
93    /// 该注册机创建时所用的框架。
94    pub fn framework(&self) -> FrameworkId {
95        self.header.framework
96    }
97}