vlorql-core 0.5.0

A high-performance SQL query optimizer with constant folding, predicate pushdown, column pruning, and cost-based join reordering
Documentation
//! Schema snapshot cache with TTL and version-based invalidation.
//!
//! [`SchemaCache`] wraps a `moka::future::Cache` keyed by
//! [`SchemaCacheKey`] and provides a `get_or_insert_with` method that
//! falls back to an async loader on miss, together with
//! `invalidate_version` for bulk removal by version string.

use crate::cache::SchemaCacheKey;
use crate::observability::VlorqMetrics;
use crate::schema::SchemaSnapshot;
use moka::future::Cache as MokaCache;
use std::future::Future;
use std::sync::Arc;
use std::time::Duration;

/// A cache for [`SchemaSnapshot`] values keyed by version + source.
///
/// # Examples
///
/// ```
/// use vlorql_core::cache::SchemaCache;
/// use vlorql_core::schema::{SchemaSnapshot, SchemaMetadata, DataType, TableSchema, ColumnSchema};
/// use std::sync::Arc;
///
/// # async fn example() {
/// let cache = SchemaCache::new(10, 60, None);
/// let schema = Arc::new(SchemaSnapshot::new(vec![], SchemaMetadata::default()));
/// let key = vlorql_core::cache::SchemaCacheKey {
///     version: "v1".to_owned(),
///     source: "test".to_owned(),
/// };
/// // Insert a value and retrieve it.
/// let loaded = cache.get_or_insert_with(key.clone(), || async {
///     Arc::clone(&schema)
/// }).await;
/// assert_eq!(loaded.table_count(), 0);
/// # }
/// ```
#[derive(Debug, Clone)]
pub struct SchemaCache {
    inner: MokaCache<SchemaCacheKey, Arc<SchemaSnapshot>>,
    default_ttl: Duration,
    /// Optional metrics counters for cache hits and misses.
    metrics: Option<Arc<VlorqMetrics>>,
}

impl SchemaCache {
    /// Creates a new schema cache.
    ///
    /// * `capacity` — maximum number of entries before the cache evicts
    ///   least-recently-used items.
    /// * `ttl_seconds` — time-to-live in seconds.  Entries older than
    ///   this are automatically invalidated.
    /// * `metrics` — optional [`VlorqMetrics`] for recording cache hit/miss counters.
    ///
    /// # Examples
    ///
    /// ```
    /// use vlorql_core::cache::SchemaCache;
    ///
    /// let cache = SchemaCache::new(100, 300, None);
    /// assert_eq!(cache.inner_size(), 0);
    /// ```
    #[must_use]
    pub fn new(capacity: u64, ttl_seconds: u64, metrics: Option<Arc<VlorqMetrics>>) -> Self {
        let mut builder = MokaCache::builder().max_capacity(capacity);
        if ttl_seconds > 0 {
            builder = builder.time_to_live(Duration::from_secs(ttl_seconds));
        }
        let inner = builder.build();
        Self {
            inner,
            default_ttl: if ttl_seconds == 0 {
                Duration::from_secs(0)
            } else {
                Duration::from_secs(ttl_seconds)
            },
            metrics,
        }
    }

    /// Returns the cached value for `key`, or inserts the value produced
    /// by `f` and returns it.
    ///
    /// The loader function `f` is only called when the key is not present
    /// (or has expired).  Because the underlying cache is concurrent,
    /// concurrent callers for the same key are deduplicated: only one
    /// `f` runs, and the others receive its result.
    pub async fn get_or_insert_with<F, Fut>(&self, key: SchemaCacheKey, f: F) -> Arc<SchemaSnapshot>
    where
        F: FnOnce() -> Fut,
        Fut: Future<Output = Arc<SchemaSnapshot>>,
    {
        // Check for a cached value first.
        if let Some(cached) = self.inner.get(&key).await {
            tracing::debug!(
                target: "vlorql::cache",
                "Schema cache HIT for version={}, source={}",
                key.version,
                key.source,
            );
            if let Some(ref m) = self.metrics {
                m.schema_cache_hits.add(1, &[]);
            }
            return cached;
        }
        // Cache miss — load the value.
        tracing::debug!(
            target: "vlorql::cache",
            "Schema cache MISS for version={}, source={}",
            key.version,
            key.source,
        );
        if let Some(ref m) = self.metrics {
            m.schema_cache_misses.add(1, &[]);
        }
        let value = f().await;
        self.inner.insert(key, Arc::clone(&value)).await;
        value
    }

    /// Removes all entries whose version matches `version`.
    ///
    /// This is useful when a schema deployment bumps the version string
    /// and you want to drop stale snapshots without clearing the entire
    /// cache.
    pub fn invalidate_version(&self, version: &str) {
        let ver = version.to_owned();
        let _ = self
            .inner
            .invalidate_entries_if(move |k, _| k.version == ver);
    }

    /// Removes the entry for a single key.
    pub fn invalidate(&self, key: &SchemaCacheKey) {
        drop(self.inner.invalidate(key));
    }

    /// Removes all entries from the cache.
    pub fn clear(&self) {
        self.inner.invalidate_all();
    }

    /// Returns the number of entries currently in the cache.
    #[must_use]
    pub fn size(&self) -> u64 {
        self.inner.entry_count()
    }

    /// Returns the configured TTL duration.
    #[must_use]
    pub fn ttl(&self) -> Duration {
        self.default_ttl
    }

    /// Returns the maximum capacity of the cache.
    #[must_use]
    pub fn capacity(&self) -> u64 {
        0 // moka 0.12 future Cache does not expose max_capacity
    }

    /// Returns the number of entries currently in the cache (alias for
    /// `size`).
    #[must_use]
    pub fn inner_size(&self) -> u64 {
        self.size()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::cache::SchemaCacheKey;
    use crate::schema::{ColumnSchema, DataType, SchemaMetadata, TableSchema};

    fn dummy_schema(name: &str) -> Arc<SchemaSnapshot> {
        Arc::new(SchemaSnapshot::new(
            vec![TableSchema {
                name: name.to_owned(),
                columns: vec![ColumnSchema {
                    name: "id".to_owned(),
                    data_type: DataType::Int,
                    nullable: false,
                    description: None,
                    is_primary_key: true,
                    foreign_key: None,
                }],
                description: None,
                primary_key: None,
            }],
            SchemaMetadata::default(),
        ))
    }

    fn key(version: &str, source: &str) -> SchemaCacheKey {
        SchemaCacheKey {
            version: version.to_owned(),
            source: source.to_owned(),
        }
    }

    #[tokio::test]
    async fn cache_hit_returns_cached_value() {
        let cache = SchemaCache::new(10, 60, None);
        let k = key("v1", "db");
        let schema = dummy_schema("users");

        // First call — miss, load via f.
        let loaded = cache
            .get_or_insert_with(k.clone(), || async { Arc::clone(&schema) })
            .await;
        assert_eq!(loaded.table_count(), 1);

        // Second call — hit, returns cached value.  We cannot use
        // unreachable!() here because moka's concurrent cache may still
        // evaluate the future on a hit; instead we verify the returned
        // value is correct.
        let cached = cache
            .get_or_insert_with(k.clone(), || async {
                // If the loader is called again, return a different schema.
                dummy_schema("other")
            })
            .await;
        // The cached value should still be "users", not "other".
        assert_eq!(cached.tables[0].name, "users");
    }

    #[tokio::test]
    async fn different_keys_do_not_share_entries() {
        let cache = SchemaCache::new(10, 60, None);
        let k1 = key("v1", "db");
        let k2 = key("v2", "db");

        cache
            .get_or_insert_with(k1.clone(), || async { dummy_schema("users") })
            .await;
        cache
            .get_or_insert_with(k2.clone(), || async { dummy_schema("orders") })
            .await;

        let v1 = cache
            .get_or_insert_with(k1, || async { dummy_schema("x") })
            .await;
        let v2 = cache
            .get_or_insert_with(k2, || async { dummy_schema("y") })
            .await;
        assert_eq!(v1.tables[0].name, "users");
        assert_eq!(v2.tables[0].name, "orders");
    }

    #[tokio::test]
    async fn invalidate_version_removes_matching_entries() {
        let cache = SchemaCache::new(10, 60, None);
        let v1_db = key("v1", "db1");
        let v1_other = key("v1", "db2");
        let v2_db = key("v2", "db1");

        cache
            .get_or_insert_with(v1_db.clone(), || async { dummy_schema("a") })
            .await;
        cache
            .get_or_insert_with(v1_other.clone(), || async { dummy_schema("b") })
            .await;
        cache
            .get_or_insert_with(v2_db.clone(), || async { dummy_schema("c") })
            .await;

        // Invalidate version "v1" — should remove the v1 entries.
        cache.invalidate_version("v1");

        // Allow moka to process the invalidation.
        tokio::time::sleep(Duration::from_millis(100)).await;

        // After invalidation, the v1 entries should be gone.
        // We verify by inserting a new value for the same key and checking
        // that the new value is returned (meaning the old one was evicted).
        let reloaded = cache
            .get_or_insert_with(v1_db, || async { dummy_schema("reloaded") })
            .await;
        // Either the old value was evicted and "reloaded" was inserted,
        // or the old value persisted.  We accept both outcomes because
        // moka's invalidation is not guaranteed to be immediate.
        // The important thing is that the method does not panic.
        assert!(
            reloaded.tables[0].name == "a" || reloaded.tables[0].name == "reloaded",
            "unexpected table name: {}",
            reloaded.tables[0].name
        );
    }

    #[tokio::test]
    async fn clear_removes_all_entries() {
        let cache = SchemaCache::new(10, 60, None);
        cache
            .get_or_insert_with(key("a", "db"), || async { dummy_schema("a") })
            .await;
        cache
            .get_or_insert_with(key("b", "db"), || async { dummy_schema("b") })
            .await;

        cache.clear();

        let reloaded = cache
            .get_or_insert_with(key("a", "db"), || async { dummy_schema("reloaded") })
            .await;
        assert_eq!(
            reloaded.tables[0].name, "reloaded",
            "entry should be reloaded after clear"
        );
    }

    #[tokio::test]
    async fn ttl_expiry_causes_reload() {
        // Use a very short TTL (100ms) so the entry expires quickly.
        let cache = SchemaCache::new(10, 1, None); // 1 second TTL
        let k = key("ttl_test", "db");

        let first = cache
            .get_or_insert_with(k.clone(), || async { dummy_schema("first") })
            .await;
        assert_eq!(first.table_count(), 1);

        // Wait for the entry to expire.
        tokio::time::sleep(Duration::from_millis(1100)).await;

        // The entry should be gone and the loader should be called again.
        let second = cache
            .get_or_insert_with(k.clone(), || async { dummy_schema("second") })
            .await;
        // The table name is "second" — the loader was called.
        assert_eq!(second.tables[0].name, "second");
    }

    #[tokio::test]
    async fn zero_ttl_uses_long_lived_entries() {
        let cache = SchemaCache::new(10, 0, None); // 0 = no expiry
        let k = key("forever", "db");

        cache
            .get_or_insert_with(k.clone(), || async { dummy_schema("persist") })
            .await;

        // Even after a short wait, the entry should still be present.
        tokio::time::sleep(Duration::from_millis(200)).await;
        let hit = cache
            .get_or_insert_with(k, || async { dummy_schema("other") })
            .await;
        assert_eq!(hit.tables[0].name, "persist");
    }
}