Skip to main content

velesdb_core/database/
metadata_ops.rs

1//! Metadata-only collection creation and retrieval operations.
2
3use crate::collection::MetadataCollection;
4use crate::{CollectionType, Result};
5
6use super::Database;
7
8impl Database {
9    /// Creates a new metadata-only collection.
10    ///
11    /// # Errors
12    ///
13    /// Returns an error if a collection with the same name already exists.
14    pub fn create_metadata_collection(&self, name: &str) -> Result<()> {
15        self.ensure_collection_name_available(name)?;
16        let path = self.data_dir.join(name);
17        let coll = MetadataCollection::create(path, name)?;
18        self.metadata_colls.write().insert(name.to_string(), coll);
19
20        if let Some(ref obs) = self.observer {
21            obs.on_collection_created(name, &CollectionType::MetadataOnly);
22        }
23
24        self.schema_version
25            .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
26
27        Ok(())
28    }
29
30    /// Returns a `MetadataCollection` by name.
31    ///
32    /// Checks the typed registry first.  Falls back to opening from disk for
33    /// collections created before the typed API existed or after a restart.
34    /// The instance is cached to avoid repeated disk reads.
35    ///
36    /// Returns `None` if the collection does not exist on disk.
37    #[must_use]
38    pub fn get_metadata_collection(&self, name: &str) -> Option<MetadataCollection> {
39        if let Some(c) = self.metadata_colls.read().get(name).cloned() {
40            return Some(c);
41        }
42        self.open_metadata_collection_from_disk(name)
43    }
44
45    /// Disk fallback for `get_metadata_collection`.
46    fn open_metadata_collection_from_disk(&self, name: &str) -> Option<MetadataCollection> {
47        let cfg = self.read_collection_config(name)?;
48        if !cfg.metadata_only {
49            return None;
50        }
51        let coll = MetadataCollection::open(self.data_dir.join(name)).ok()?;
52        self.metadata_colls
53            .write()
54            .insert(name.to_string(), coll.clone());
55        Some(coll)
56    }
57}