icydb_core/db/registry/
handle.rs1use crate::db::{
2 data::DataStore,
3 index::{IndexState, IndexStore},
4 schema::SchemaStore,
5};
6use std::{cell::RefCell, thread::LocalKey};
7
8#[derive(Clone, Copy, Debug)]
18pub struct StoreHandle {
19 data: &'static LocalKey<RefCell<DataStore>>,
20 index: &'static LocalKey<RefCell<IndexStore>>,
21 schema: &'static LocalKey<RefCell<SchemaStore>>,
22 allocations: StoreAllocationIdentities,
23}
24
25#[derive(Clone, Copy, Debug, Eq, PartialEq)]
32pub struct StoreAllocationIdentity {
33 memory_id: u8,
34 stable_key: &'static str,
35}
36
37impl StoreAllocationIdentity {
38 #[must_use]
40 pub const fn new(memory_id: u8, stable_key: &'static str) -> Self {
41 Self {
42 memory_id,
43 stable_key,
44 }
45 }
46
47 #[must_use]
49 pub const fn memory_id(self) -> u8 {
50 self.memory_id
51 }
52
53 #[must_use]
55 pub const fn stable_key(self) -> &'static str {
56 self.stable_key
57 }
58}
59
60#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
68pub struct StoreAllocationIdentities {
69 data: Option<StoreAllocationIdentity>,
70 index: Option<StoreAllocationIdentity>,
71 schema: Option<StoreAllocationIdentity>,
72}
73
74impl StoreAllocationIdentities {
75 #[must_use]
77 pub const fn absent() -> Self {
78 Self {
79 data: None,
80 index: None,
81 schema: None,
82 }
83 }
84
85 #[must_use]
87 pub const fn new(
88 data: StoreAllocationIdentity,
89 index: StoreAllocationIdentity,
90 schema: StoreAllocationIdentity,
91 ) -> Self {
92 Self {
93 data: Some(data),
94 index: Some(index),
95 schema: Some(schema),
96 }
97 }
98
99 #[must_use]
101 pub const fn data(self) -> Option<StoreAllocationIdentity> {
102 self.data
103 }
104
105 #[must_use]
107 pub const fn index(self) -> Option<StoreAllocationIdentity> {
108 self.index
109 }
110
111 #[must_use]
113 pub const fn schema(self) -> Option<StoreAllocationIdentity> {
114 self.schema
115 }
116}
117
118impl StoreHandle {
119 #[must_use]
121 pub const fn new(
122 data: &'static LocalKey<RefCell<DataStore>>,
123 index: &'static LocalKey<RefCell<IndexStore>>,
124 schema: &'static LocalKey<RefCell<SchemaStore>>,
125 ) -> Self {
126 Self::new_with_allocations(data, index, schema, StoreAllocationIdentities::absent())
127 }
128
129 #[must_use]
131 pub const fn new_with_allocations(
132 data: &'static LocalKey<RefCell<DataStore>>,
133 index: &'static LocalKey<RefCell<IndexStore>>,
134 schema: &'static LocalKey<RefCell<SchemaStore>>,
135 allocations: StoreAllocationIdentities,
136 ) -> Self {
137 Self {
138 data,
139 index,
140 schema,
141 allocations,
142 }
143 }
144
145 pub fn with_data<R>(&self, f: impl FnOnce(&DataStore) -> R) -> R {
147 #[cfg(feature = "diagnostics")]
148 {
149 crate::db::physical_access::measure_physical_access_operation(|| {
150 self.data.with_borrow(f)
151 })
152 }
153
154 #[cfg(not(feature = "diagnostics"))]
155 {
156 self.data.with_borrow(f)
157 }
158 }
159
160 pub fn with_data_mut<R>(&self, f: impl FnOnce(&mut DataStore) -> R) -> R {
162 self.data.with_borrow_mut(f)
163 }
164
165 pub fn with_index<R>(&self, f: impl FnOnce(&IndexStore) -> R) -> R {
167 #[cfg(feature = "diagnostics")]
168 {
169 crate::db::physical_access::measure_physical_access_operation(|| {
170 self.index.with_borrow(f)
171 })
172 }
173
174 #[cfg(not(feature = "diagnostics"))]
175 {
176 self.index.with_borrow(f)
177 }
178 }
179
180 pub fn with_index_mut<R>(&self, f: impl FnOnce(&mut IndexStore) -> R) -> R {
182 self.index.with_borrow_mut(f)
183 }
184
185 pub fn with_schema<R>(&self, f: impl FnOnce(&SchemaStore) -> R) -> R {
187 self.schema.with_borrow(f)
188 }
189
190 pub fn with_schema_mut<R>(&self, f: impl FnOnce(&mut SchemaStore) -> R) -> R {
192 self.schema.with_borrow_mut(f)
193 }
194
195 #[must_use]
197 pub(in crate::db) fn index_state(&self) -> IndexState {
198 self.with_index(IndexStore::state)
199 }
200
201 pub(in crate::db) fn mark_index_building(&self) {
203 self.with_index_mut(IndexStore::mark_building);
204 }
205
206 pub(in crate::db) fn mark_index_ready(&self) {
208 self.with_index_mut(IndexStore::mark_ready);
209 }
210
211 #[must_use]
213 pub const fn data_store(&self) -> &'static LocalKey<RefCell<DataStore>> {
214 self.data
215 }
216
217 #[must_use]
219 pub const fn index_store(&self) -> &'static LocalKey<RefCell<IndexStore>> {
220 self.index
221 }
222
223 #[must_use]
225 pub const fn schema_store(&self) -> &'static LocalKey<RefCell<SchemaStore>> {
226 self.schema
227 }
228
229 #[must_use]
232 pub const fn data_allocation(&self) -> Option<StoreAllocationIdentity> {
233 self.allocations.data()
234 }
235
236 #[must_use]
239 pub const fn index_allocation(&self) -> Option<StoreAllocationIdentity> {
240 self.allocations.index()
241 }
242
243 #[must_use]
246 pub const fn schema_allocation(&self) -> Option<StoreAllocationIdentity> {
247 self.allocations.schema()
248 }
249}