Skip to main content

cranpose_core/slot/
table.rs

1use super::debug::SlotTableDiagnostics;
2use super::{
3    AnchorRegistry, DeferredDrop, GroupRecord, NodeRecord, PayloadAnchorRegistry, PayloadRecord,
4    ScopeIndex, SlotLifecycleCoordinator, SlotWriteSessionState,
5};
6use std::sync::atomic::{AtomicUsize, Ordering};
7
8mod metadata;
9mod mutation;
10mod values;
11
12static NEXT_SLOT_STORAGE_ID: AtomicUsize = AtomicUsize::new(1);
13
14pub(crate) struct SlotWriteSession<'a> {
15    pub(super) table: &'a mut SlotTable,
16    pub(in crate::slot) lifecycle: &'a mut SlotLifecycleCoordinator,
17    pub(in crate::slot) state: &'a mut SlotWriteSessionState,
18}
19
20pub struct SlotTable {
21    storage_id: usize,
22    pub(super) groups: Vec<GroupRecord>,
23    pub(super) payloads: Vec<PayloadRecord>,
24    pub(super) nodes: Vec<NodeRecord>,
25    pub(super) anchors: AnchorRegistry,
26    pub(super) payload_anchors: PayloadAnchorRegistry,
27    pub(super) scope_index: ScopeIndex,
28    pub(super) diagnostics: SlotTableDiagnostics,
29    next_group_generation: u32,
30}
31
32impl SlotTable {
33    pub fn new() -> Self {
34        Self {
35            storage_id: NEXT_SLOT_STORAGE_ID.fetch_add(1, Ordering::Relaxed),
36            groups: Vec::new(),
37            payloads: Vec::new(),
38            nodes: Vec::new(),
39            anchors: AnchorRegistry::new(),
40            payload_anchors: PayloadAnchorRegistry::new(),
41            scope_index: ScopeIndex::new(),
42            diagnostics: SlotTableDiagnostics::default(),
43            next_group_generation: 1,
44        }
45    }
46
47    pub(crate) fn write_session<'a>(
48        &'a mut self,
49        lifecycle: &'a mut SlotLifecycleCoordinator,
50        state: &'a mut SlotWriteSessionState,
51    ) -> SlotWriteSession<'a> {
52        SlotWriteSession {
53            table: self,
54            lifecycle,
55            state,
56        }
57    }
58
59    pub(crate) fn storage_id(&self) -> usize {
60        self.storage_id
61    }
62
63    pub(crate) fn compact_storage(&mut self) {
64        self.groups.shrink_to_fit();
65        self.payloads.shrink_to_fit();
66        self.nodes.shrink_to_fit();
67        self.anchors.shrink_to_fit();
68        self.payload_anchors.shrink_to_fit();
69        self.scope_index.shrink_to_fit();
70    }
71
72    pub(crate) fn take_all_drops(&mut self) -> Vec<DeferredDrop> {
73        let payload_count = self.payloads.len();
74        let mut drops = Vec::with_capacity(payload_count);
75        for payload in self.payloads.drain(..).rev() {
76            drops.push(payload.into_deferred_drop());
77        }
78        self.groups.clear();
79        self.nodes.clear();
80        self.anchors.clear();
81        self.payload_anchors.clear();
82        self.scope_index.clear();
83        drops
84    }
85}
86
87impl Default for SlotTable {
88    fn default() -> Self {
89        Self::new()
90    }
91}