Skip to main content

cranpose_core/slot/
table.rs

1use super::{
2    AnchorRegistry, DeferredDrop, GroupRecord, MovableIndex, NodeRecord, PayloadAnchorRegistry,
3    SlotLifecycleCoordinator, SlotWriteSessionState, debug::SlotTableDiagnostics,
4    payload_store::PayloadStore,
5};
6use crate::collections::growth_slack::GrowthSlack;
7
8mod insert_run;
9mod metadata;
10mod mutation;
11mod values;
12
13#[cfg(test)]
14pub(crate) use values::ValueSlotError;
15
16// A table's number, which no other table of its thread takes: maps key
17// hosts by it and a value slot names its table by it, so a table made where
18// a dropped one lay is not taken for it, and the maps' order does not follow
19// the heap's layout.
20struct SlotStorageIdentity(usize);
21
22impl SlotStorageIdentity {
23    fn new() -> Self {
24        crate::thread_state::cells(|cells| {
25            let id = cells.next_slot_storage_id.get();
26            cells.next_slot_storage_id.set(id + 1);
27            Self(id)
28        })
29    }
30
31    fn id(&self) -> usize {
32        self.0
33    }
34}
35
36pub(crate) struct SlotWriteSession<'a> {
37    pub(super) table: &'a mut SlotTable,
38    pub(in crate::slot) lifecycle: &'a mut SlotLifecycleCoordinator,
39    pub(in crate::slot) state: &'a mut SlotWriteSessionState,
40}
41
42/// Storage for the groups, remembered values and nodes that one composition produced.
43pub struct SlotTable {
44    storage_id: SlotStorageIdentity,
45    pub(super) groups: Vec<GroupRecord>,
46    pub(super) payloads: PayloadStore,
47    pub(super) nodes: Vec<NodeRecord>,
48    pub(super) anchors: AnchorRegistry,
49    pub(super) payload_anchors: PayloadAnchorRegistry,
50    pub(super) movables: MovableIndex,
51    pub(super) diagnostics: SlotTableDiagnostics,
52    stale_group_indexes: metadata::StaleGroupIndexes,
53    insert_run: Option<insert_run::InsertRun>,
54    next_group_generation: u32,
55}
56
57impl SlotTable {
58    /// Creates an empty table.
59    pub fn new() -> Self {
60        Self {
61            storage_id: SlotStorageIdentity::new(),
62            groups: Vec::new(),
63            payloads: PayloadStore::default(),
64            nodes: Vec::new(),
65            anchors: AnchorRegistry::new(),
66            payload_anchors: PayloadAnchorRegistry::new(),
67            movables: MovableIndex::default(),
68            diagnostics: Default::default(),
69            stale_group_indexes: metadata::StaleGroupIndexes::default(),
70            insert_run: None,
71            next_group_generation: 1,
72        }
73    }
74
75    pub(crate) fn write_session<'a>(
76        &'a mut self,
77        lifecycle: &'a mut SlotLifecycleCoordinator,
78        state: &'a mut SlotWriteSessionState,
79    ) -> SlotWriteSession<'a> {
80        SlotWriteSession {
81            table: self,
82            lifecycle,
83            state,
84        }
85    }
86
87    pub(crate) fn storage_id(&self) -> usize {
88        self.storage_id.id()
89    }
90
91    #[cfg(test)]
92    pub(in crate::slot) fn set_next_group_generation_for_test(&mut self, generation: u32) {
93        self.next_group_generation = generation;
94    }
95
96    #[cfg(test)]
97    pub(in crate::slot) fn set_next_group_anchor_for_test(&mut self, next_anchor: usize) {
98        self.anchors.set_next_anchor_for_test(next_anchor);
99    }
100
101    #[cfg(test)]
102    pub(in crate::slot) fn set_next_payload_anchor_for_test(&mut self, next_anchor: usize) {
103        self.payload_anchors.set_next_id_for_test(next_anchor);
104    }
105
106    pub(crate) fn compact_storage(&mut self) {
107        self.groups.shrink_to_fit();
108        self.payloads.compact();
109        self.nodes.shrink_to_fit();
110        self.anchors.shrink_to_fit();
111        self.payload_anchors.shrink_to_fit();
112        self.movables.shrink_to_fit();
113    }
114
115    pub(crate) fn storage_capacity(&self) -> usize {
116        self.groups.capacity() + self.payloads.capacity() + self.nodes.capacity()
117    }
118
119    pub(crate) fn trim_growth_slack(&mut self) {
120        self.groups.trim_growth_slack();
121        self.payloads.trim_growth_slack();
122        self.nodes.trim_growth_slack();
123        self.anchors.trim_growth_slack();
124        self.payload_anchors.trim_growth_slack();
125    }
126
127    pub(crate) fn take_effect_drops(&mut self) -> Vec<DeferredDrop> {
128        let mut drops = Vec::new();
129        self.payloads.for_each_mut(|payload| {
130            if let Some(fresh) = payload.payload_type.fresh {
131                let old = std::mem::replace(&mut payload.value, fresh());
132                drops.push(DeferredDrop::payload(old));
133            }
134        });
135        drops
136    }
137
138    pub(crate) fn take_all_drops(&mut self) -> Vec<DeferredDrop> {
139        let mut drops = Vec::with_capacity(self.payloads.len());
140        self.payloads
141            .drain_rev(|payload| drops.push(payload.into_deferred_drop()));
142        self.groups.clear();
143        self.stale_group_indexes = metadata::StaleGroupIndexes::default();
144        self.nodes.clear();
145        self.anchors.clear();
146        self.payload_anchors.clear();
147        self.movables.clear();
148        drops
149    }
150}
151
152impl Default for SlotTable {
153    fn default() -> Self {
154        Self::new()
155    }
156}