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        // Parity item E: thread the live LimitsConfig caps into the collection.
19        self.push_runtime_limits(&coll.inner);
20        self.metadata_colls.write().insert(name.to_string(), coll);
21
22        if let Some(ref obs) = self.observer {
23            obs.on_collection_created(name, &CollectionType::MetadataOnly);
24        }
25
26        self.schema_version
27            .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
28
29        Ok(())
30    }
31
32    /// Returns a `MetadataCollection` by name.
33    ///
34    /// Checks the typed registry first.  Falls back to opening from disk for
35    /// collections created before the typed API existed or after a restart.
36    /// The instance is cached to avoid repeated disk reads.
37    ///
38    /// Returns `None` if the collection does not exist on disk.
39    #[must_use]
40    pub fn get_metadata_collection(&self, name: &str) -> Option<MetadataCollection> {
41        if let Some(c) = self.metadata_colls.read().get(name).cloned() {
42            return Some(c);
43        }
44        self.open_metadata_collection_from_disk(name)
45    }
46
47    /// Disk fallback for `get_metadata_collection`.
48    fn open_metadata_collection_from_disk(&self, name: &str) -> Option<MetadataCollection> {
49        let cfg = self.read_collection_config(name)?;
50        if !cfg.metadata_only {
51            return None;
52        }
53        let coll = MetadataCollection::open(self.data_dir.join(name)).ok()?;
54        // Parity item E: re-push runtime limits on disk-open (not persisted).
55        self.push_runtime_limits(&coll.inner);
56        self.metadata_colls
57            .write()
58            .insert(name.to_string(), coll.clone());
59        Some(coll)
60    }
61}