From 354dc9e703a16dfca96bfe59cba0ba83e65725cf Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Tue, 29 Oct 2024 00:08:41 +0000 Subject: [PATCH] add map accessor to Database; move cork interface Signed-off-by: Jason Volk --- src/admin/debug/commands.rs | 2 +- src/database/cork.rs | 16 +++++++++++++++- src/database/database.rs | 26 ++++++++++---------------- 3 files changed, 26 insertions(+), 18 deletions(-) diff --git a/src/admin/debug/commands.rs b/src/admin/debug/commands.rs index 2aa6078f..db102858 100644 --- a/src/admin/debug/commands.rs +++ b/src/admin/debug/commands.rs @@ -838,7 +838,7 @@ pub(super) async fn database_stats( let map_name = map.as_ref().map_or(EMPTY, String::as_str); let mut out = String::new(); - for (name, map) in self.services.db.iter_maps() { + for (name, map) in self.services.db.iter() { if !map_name.is_empty() && *map_name != *name { continue; } diff --git a/src/database/cork.rs b/src/database/cork.rs index 26c520a2..5fe5fd7a 100644 --- a/src/database/cork.rs +++ b/src/database/cork.rs @@ -1,6 +1,6 @@ use std::sync::Arc; -use crate::Engine; +use crate::{Database, Engine}; pub struct Cork { db: Arc, @@ -8,6 +8,20 @@ pub struct Cork { sync: bool, } +impl Database { + #[inline] + #[must_use] + pub fn cork(&self) -> Cork { Cork::new(&self.db, false, false) } + + #[inline] + #[must_use] + pub fn cork_and_flush(&self) -> Cork { Cork::new(&self.db, true, false) } + + #[inline] + #[must_use] + pub fn cork_and_sync(&self) -> Cork { Cork::new(&self.db, true, true) } +} + impl Cork { #[inline] pub(super) fn new(db: &Arc, flush: bool, sync: bool) -> Self { diff --git a/src/database/database.rs b/src/database/database.rs index 4c29c840..099df87d 100644 --- a/src/database/database.rs +++ b/src/database/database.rs @@ -1,9 +1,8 @@ use std::{ops::Index, sync::Arc}; -use conduit::{Result, Server}; +use conduit::{err, Result, Server}; use crate::{ - cork::Cork, maps, maps::{Maps, MapsKey, MapsVal}, Engine, Map, @@ -11,7 +10,7 @@ use crate::{ pub struct Database { pub db: Arc, - map: Maps, + maps: Maps, } impl Database { @@ -20,24 +19,19 @@ impl Database { let db = Engine::open(server)?; Ok(Arc::new(Self { db: db.clone(), - map: maps::open(&db)?, + maps: maps::open(&db)?, })) } #[inline] - #[must_use] - pub fn cork(&self) -> Cork { Cork::new(&self.db, false, false) } + pub fn get(&self, name: &str) -> Result<&Arc> { + self.maps + .get(name) + .ok_or_else(|| err!(Request(NotFound("column not found")))) + } #[inline] - #[must_use] - pub fn cork_and_flush(&self) -> Cork { Cork::new(&self.db, true, false) } - - #[inline] - #[must_use] - pub fn cork_and_sync(&self) -> Cork { Cork::new(&self.db, true, true) } - - #[inline] - pub fn iter_maps(&self) -> impl Iterator + Send + '_ { self.map.iter() } + pub fn iter(&self) -> impl Iterator + Send + '_ { self.maps.iter() } #[inline] #[must_use] @@ -52,7 +46,7 @@ impl Index<&str> for Database { type Output = Arc; fn index(&self, name: &str) -> &Self::Output { - self.map + self.maps .get(name) .expect("column in database does not exist") }