add map accessor to Database; move cork interface

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-10-29 00:08:41 +00:00 committed by strawberry
parent 567a4cb441
commit 354dc9e703
3 changed files with 26 additions and 18 deletions

View File

@ -838,7 +838,7 @@ pub(super) async fn database_stats(
let map_name = map.as_ref().map_or(EMPTY, String::as_str); let map_name = map.as_ref().map_or(EMPTY, String::as_str);
let mut out = String::new(); 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 { if !map_name.is_empty() && *map_name != *name {
continue; continue;
} }

View File

@ -1,6 +1,6 @@
use std::sync::Arc; use std::sync::Arc;
use crate::Engine; use crate::{Database, Engine};
pub struct Cork { pub struct Cork {
db: Arc<Engine>, db: Arc<Engine>,
@ -8,6 +8,20 @@ pub struct Cork {
sync: bool, 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 { impl Cork {
#[inline] #[inline]
pub(super) fn new(db: &Arc<Engine>, flush: bool, sync: bool) -> Self { pub(super) fn new(db: &Arc<Engine>, flush: bool, sync: bool) -> Self {

View File

@ -1,9 +1,8 @@
use std::{ops::Index, sync::Arc}; use std::{ops::Index, sync::Arc};
use conduit::{Result, Server}; use conduit::{err, Result, Server};
use crate::{ use crate::{
cork::Cork,
maps, maps,
maps::{Maps, MapsKey, MapsVal}, maps::{Maps, MapsKey, MapsVal},
Engine, Map, Engine, Map,
@ -11,7 +10,7 @@ use crate::{
pub struct Database { pub struct Database {
pub db: Arc<Engine>, pub db: Arc<Engine>,
map: Maps, maps: Maps,
} }
impl Database { impl Database {
@ -20,24 +19,19 @@ impl Database {
let db = Engine::open(server)?; let db = Engine::open(server)?;
Ok(Arc::new(Self { Ok(Arc::new(Self {
db: db.clone(), db: db.clone(),
map: maps::open(&db)?, maps: maps::open(&db)?,
})) }))
} }
#[inline] #[inline]
#[must_use] pub fn get(&self, name: &str) -> Result<&Arc<Map>> {
pub fn cork(&self) -> Cork { Cork::new(&self.db, false, false) } self.maps
.get(name)
.ok_or_else(|| err!(Request(NotFound("column not found"))))
}
#[inline] #[inline]
#[must_use] pub fn iter(&self) -> impl Iterator<Item = (&MapsKey, &MapsVal)> + Send + '_ { self.maps.iter() }
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<Item = (&MapsKey, &MapsVal)> + Send + '_ { self.map.iter() }
#[inline] #[inline]
#[must_use] #[must_use]
@ -52,7 +46,7 @@ impl Index<&str> for Database {
type Output = Arc<Map>; type Output = Arc<Map>;
fn index(&self, name: &str) -> &Self::Output { fn index(&self, name: &str) -> &Self::Output {
self.map self.maps
.get(name) .get(name)
.expect("column in database does not exist") .expect("column in database does not exist")
} }