canic_core/storage/stable/registry/
app.rs1use crate::{
2 cdk::structures::{DefaultMemoryImpl, memory::VirtualMemory},
3 storage::{prelude::*, stable::memory::topology::APP_REGISTRY_ID},
4};
5use ic_memory::stable_structures::btreemap::BTreeMap as StableBtreeMap;
6use std::cell::RefCell;
7
8eager_static! {
15 static APP_REGISTRY: RefCell<StableBtreeMap<Principal, Principal, VirtualMemory<DefaultMemoryImpl>>> =
16 RefCell::new(StableBtreeMap::init(crate::ic_memory_key!("canic.core.app_registry.v1", AppRegistry, APP_REGISTRY_ID)));
17}
18
19#[derive(Clone, Debug)]
24pub struct AppRegistryRecord {
25 pub entries: Vec<(Principal, Principal)>,
26}
27
28pub struct AppRegistry;
40
41impl AppRegistry {
42 pub(crate) fn upsert(subnet_pid: Principal, root_pid: Principal) {
44 APP_REGISTRY.with_borrow_mut(|map| {
45 map.insert(subnet_pid, root_pid);
46 });
47 }
48
49 #[must_use]
50 pub(crate) fn export() -> AppRegistryRecord {
51 AppRegistryRecord {
52 entries: APP_REGISTRY
53 .with_borrow(|map| map.iter().map(|e| (*e.key(), e.value())).collect()),
54 }
55 }
56}