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