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        // Bound before the `if let` — see `get_vector_collection`: the disk
42        // fallback takes `metadata_colls` for WRITE.
43        let cached = self.metadata_colls.read().get(name).cloned();
44        if let Some(c) = cached {
45            return Some(c);
46        }
47        self.open_metadata_collection_from_disk(name)
48    }
49
50    /// Disk fallback for `get_metadata_collection`.
51    fn open_metadata_collection_from_disk(&self, name: &str) -> Option<MetadataCollection> {
52        let cfg = self.read_collection_config(name)?;
53        if !cfg.metadata_only {
54            return None;
55        }
56        let coll = MetadataCollection::open(self.data_dir.join(name)).ok()?;
57        // Parity item E: re-push runtime limits on disk-open (not persisted).
58        self.push_runtime_limits(&coll.inner);
59        self.metadata_colls
60            .write()
61            .insert(name.to_string(), coll.clone());
62        Some(coll)
63    }
64}