Files
continuwuity/src/database/database.rs
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

60 lines
1.2 KiB
Rust
Raw Normal View History

use std::{ops::Index, sync::Arc};
2024-05-28 06:59:50 +00:00
use conduit::{Result, Server};
2024-05-28 06:59:50 +00:00
use crate::{
cork::Cork,
maps,
maps::{Maps, MapsKey, MapsVal},
Engine, Map,
};
2024-05-28 06:59:50 +00:00
pub struct Database {
pub db: Arc<Engine>,
map: Maps,
2024-05-28 06:59:50 +00:00
}
impl Database {
/// Load an existing database or create a new one.
2024-07-27 07:17:07 +00:00
pub async fn open(server: &Arc<Server>) -> Result<Arc<Self>> {
2024-05-28 06:59:50 +00:00
let db = Engine::open(server)?;
2024-07-27 07:17:07 +00:00
Ok(Arc::new(Self {
2024-05-28 06:59:50 +00:00
db: db.clone(),
map: maps::open(&db)?,
2024-07-27 07:17:07 +00:00
}))
2024-05-28 06:59:50 +00:00
}
2024-07-01 20:54:38 +00:00
#[inline]
2024-07-01 20:54:38 +00:00
#[must_use]
pub fn cork(&self) -> Cork { Cork::new(&self.db, false, false) }
#[inline]
2024-07-01 20:54:38 +00:00
#[must_use]
pub fn cork_and_flush(&self) -> Cork { Cork::new(&self.db, true, false) }
#[inline]
2024-07-01 20:54:38 +00:00
#[must_use]
pub fn cork_and_sync(&self) -> Cork { Cork::new(&self.db, true, true) }
#[inline]
2024-08-08 17:18:30 +00:00
pub fn iter_maps(&self) -> impl Iterator<Item = (&MapsKey, &MapsVal)> + Send + '_ { self.map.iter() }
2024-10-01 04:20:31 +00:00
#[inline]
#[must_use]
pub fn is_read_only(&self) -> bool { self.db.secondary || self.db.read_only }
#[inline]
#[must_use]
pub fn is_secondary(&self) -> bool { self.db.secondary }
2024-05-28 06:59:50 +00:00
}
impl Index<&str> for Database {
type Output = Arc<Map>;
fn index(&self, name: &str) -> &Self::Output {
self.map
.get(name)
.expect("column in database does not exist")
}
}