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 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;
}

View File

@ -1,6 +1,6 @@
use std::sync::Arc;
use crate::Engine;
use crate::{Database, Engine};
pub struct Cork {
db: Arc<Engine>,
@ -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<Engine>, flush: bool, sync: bool) -> Self {

View File

@ -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<Engine>,
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<Map>> {
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<Item = (&MapsKey, &MapsVal)> + Send + '_ { self.map.iter() }
pub fn iter(&self) -> impl Iterator<Item = (&MapsKey, &MapsVal)> + Send + '_ { self.maps.iter() }
#[inline]
#[must_use]
@ -52,7 +46,7 @@ impl Index<&str> for Database {
type Output = Arc<Map>;
fn index(&self, name: &str) -> &Self::Output {
self.map
self.maps
.get(name)
.expect("column in database does not exist")
}