pub mod copy;
pub mod ddl;
pub mod dml;
pub mod plan_cache;
pub mod query;
pub mod standalone_call;
pub mod substitute;
pub mod transaction;
pub mod utils;
use crate::database::Database;
use crate::prepared_statement::PreparedStatement;
use plan_cache::{CachedPlan, PlanCache};
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
const PLAN_CACHE_CAPACITY: usize = 100;
pub(crate) struct TxnResources {
pub local_storage: akar_storage::LocalStorage,
pub local_wal: akar_storage::LocalWAL,
pub shadow_file: akar_storage::ShadowFile,
}
pub struct Connection {
pub(crate) database: Arc<Database>,
pub(crate) statement_cache: Mutex<HashMap<String, PreparedStatement>>,
pub(crate) plan_cache: Mutex<PlanCache<CachedPlan>>,
pub(crate) txn_resources: Mutex<HashMap<u64, TxnResources>>,
}
impl Connection {
pub fn new(database: &Arc<Database>) -> Self {
Self {
database: database.clone(),
statement_cache: Mutex::new(HashMap::new()),
plan_cache: Mutex::new(PlanCache::new(PLAN_CACHE_CAPACITY)),
txn_resources: Mutex::new(HashMap::new()),
}
}
pub fn clear_cache(&self) {
if let Ok(mut cache) = self.statement_cache.lock() {
cache.clear();
}
if let Ok(mut cache) = self.plan_cache.lock() {
cache.clear();
}
}
pub fn cache_size(&self) -> usize {
self.statement_cache.lock().map(|c| c.len()).unwrap_or(0)
}
pub fn plan_cache_size(&self) -> usize {
self.plan_cache.lock().map(|c| c.len()).unwrap_or(0)
}
}