canic/model/memory/
scaling.rs1use crate::{
2 cdk::structures::{BTreeMap, DefaultMemoryImpl, memory::VirtualMemory},
3 eager_static, ic_memory, impl_storable_bounded,
4 model::memory::id::scaling::SCALING_REGISTRY_ID,
5 types::CanisterType,
6};
7use candid::{CandidType, Principal};
8use serde::{Deserialize, Serialize};
9use std::cell::RefCell;
10
11eager_static! {
16 static SCALING_REGISTRY: RefCell<
17 BTreeMap<Principal, WorkerEntry, VirtualMemory<DefaultMemoryImpl>>
18 > = RefCell::new(
19 BTreeMap::init(ic_memory!(ScalingRegistry, SCALING_REGISTRY_ID)),
20 );
21}
22
23#[derive(CandidType, Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
28pub struct WorkerEntry {
29 pub pool: String, pub canister_type: CanisterType, pub created_at_secs: u64, }
33
34impl WorkerEntry {
35 pub const STORABLE_MAX_SIZE: u32 = 128;
36}
37
38impl_storable_bounded!(WorkerEntry, WorkerEntry::STORABLE_MAX_SIZE, false);
39
40pub type ScalingRegistryView = Vec<(Principal, WorkerEntry)>;
45
46#[derive(Clone, Copy, Debug, Default)]
52pub struct ScalingRegistry;
53
54impl ScalingRegistry {
55 pub fn insert(pid: Principal, entry: WorkerEntry) {
57 SCALING_REGISTRY.with_borrow_mut(|map| {
58 map.insert(pid, entry);
59 });
60 }
61
62 #[must_use]
64 pub fn remove(pid: &Principal) -> Option<WorkerEntry> {
65 SCALING_REGISTRY.with_borrow_mut(|map| map.remove(pid))
66 }
67
68 #[must_use]
70 pub fn find_by_pid(pid: &Principal) -> Option<WorkerEntry> {
71 SCALING_REGISTRY.with_borrow(|map| map.get(pid))
72 }
73
74 #[must_use]
76 pub fn find_by_pool(pool: &str) -> Vec<(Principal, WorkerEntry)> {
77 SCALING_REGISTRY.with_borrow(|map| {
78 map.iter()
79 .filter(|e| e.value().pool == pool)
80 .map(|e| (*e.key(), e.value()))
81 .collect()
82 })
83 }
84
85 #[must_use]
87 pub fn export() -> Vec<(Principal, WorkerEntry)> {
88 SCALING_REGISTRY.with_borrow(|map| map.iter().map(|e| (*e.key(), e.value())).collect())
89 }
90
91 pub fn clear() {
93 SCALING_REGISTRY.with_borrow_mut(BTreeMap::clear);
94 }
95}