icydb_core/db/registry/
registry.rs1use crate::{
2 db::{
3 data::DataStore,
4 index::IndexStore,
5 registry::{StoreAllocationIdentities, StoreHandle, StoreRegistryError},
6 schema::SchemaStore,
7 },
8 error::InternalError,
9};
10use std::{cell::RefCell, thread::LocalKey};
11
12#[derive(Default)]
22pub struct StoreRegistry {
23 stores: Vec<(&'static str, StoreHandle)>,
24}
25
26impl StoreRegistry {
27 #[must_use]
29 pub fn new() -> Self {
30 Self::default()
31 }
32
33 pub fn iter(&self) -> impl Iterator<Item = (&'static str, StoreHandle)> {
39 self.stores.iter().copied()
40 }
41
42 pub fn register_store(
50 &mut self,
51 name: &'static str,
52 data: &'static LocalKey<RefCell<DataStore>>,
53 index: &'static LocalKey<RefCell<IndexStore>>,
54 schema: &'static LocalKey<RefCell<SchemaStore>>,
55 allocations: StoreAllocationIdentities,
56 ) -> Result<(), InternalError> {
57 if self
58 .stores
59 .iter()
60 .any(|(existing_name, _)| *existing_name == name)
61 {
62 return Err(StoreRegistryError::StoreAlreadyRegistered(name.to_string()).into());
63 }
64
65 if let Some(existing_name) =
68 self.stores
69 .iter()
70 .find_map(|(existing_name, existing_handle)| {
71 (std::ptr::eq(existing_handle.data_store(), data)
72 && std::ptr::eq(existing_handle.index_store(), index)
73 && std::ptr::eq(existing_handle.schema_store(), schema))
74 .then_some(*existing_name)
75 })
76 {
77 return Err(StoreRegistryError::StoreHandleTripletAlreadyRegistered {
78 name: name.to_string(),
79 existing_name: existing_name.to_string(),
80 }
81 .into());
82 }
83
84 self.stores
85 .push((name, StoreHandle::new(data, index, schema, allocations)));
86
87 Ok(())
88 }
89
90 pub fn try_get_store(&self, path: &str) -> Result<StoreHandle, InternalError> {
92 self.stores
93 .iter()
94 .find_map(|(existing_path, handle)| (*existing_path == path).then_some(*handle))
95 .ok_or_else(|| StoreRegistryError::StoreNotFound(path.to_string()).into())
96 }
97}