moltendb-core 0.10.1

MoltenDB core engine — in-memory DashMap storage, WAL persistence, query evaluation. No HTTP, no auth.
Documentation
#![allow(dead_code)]
use serde_json::{Value, json};
use crate::validation;
use crate::{engine, analytics};

/// Handle an analytics query request.
///
/// Parses the payload as an AnalyticsQuery and executes it against the database.
/// Returns the result value and execution metadata (time, rows scanned).
///
/// Note: this function exists but is not currently wired to an HTTP route.
/// It is available for future use or direct calls from other handlers.
pub fn process_analytics(db: &engine::Db, payload: &Value, max_body_size: usize, max_keys_per_request: usize) -> Value {
    // Validate the request structure first.
    if let Err(e) = validation::validate_request(payload, max_body_size, max_keys_per_request) {
        return json!({ "error": e.to_string() });
    }

    // Deserialize the payload into a strongly-typed AnalyticsQuery struct.
    let query: analytics::AnalyticsQuery = match serde_json::from_value(payload.clone()) {
        Ok(q)  => q,
        Err(e) => return json!({ "error": format!("Invalid analytics query: {}", e) }),
    };

    // Execute the analytics query (COUNT, SUM, AVG, etc.) against the database.
    let result = match analytics::execute_query(db, &query) {
        Ok(res) => res,
        Err(e)  => return json!({ "error": format!("Analytics execution failed: {}", e) }),
    };

    // Return the result along with execution metadata for performance monitoring.
    json!({
        "result": result.result,
        "metadata": {
            "execution_time_ms": result.metadata.execution_time_ms,
            "rows_scanned": result.metadata.rows_scanned
        }
    })
}