Files
continuwuity/src/database/database.rs
T

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

57 lines
1.1 KiB
Rust
Raw Normal View History

use std::{ops::Index, sync::Arc};
2024-05-28 06:59:50 +00:00
use conduit::{err, Result, Server};
2024-05-28 06:59:50 +00:00
use crate::{
maps,
maps::{Maps, MapsKey, MapsVal},
Engine, Map,
};
2024-05-28 06:59:50 +00:00
pub struct Database {
pub db: Arc<Engine>,
maps: 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(),
maps: 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]
pub fn get(&self, name: &str) -> Result<&Arc<Map>> {
self.maps
.get(name)
.ok_or_else(|| err!(Request(NotFound("column not found"))))
}
#[inline]
pub fn iter(&self) -> impl Iterator<Item = (&MapsKey, &MapsVal)> + Send + '_ { self.maps.iter() }
2024-10-01 04:20:31 +00:00
2024-10-29 03:10:18 +00:00
#[inline]
pub fn keys(&self) -> impl Iterator<Item = &MapsKey> + Send + '_ { self.maps.keys() }
2024-10-01 04:20:31 +00:00
#[inline]
#[must_use]
2024-11-14 22:43:18 +00:00
pub fn is_read_only(&self) -> bool { self.db.is_read_only() }
2024-10-01 04:20:31 +00:00
#[inline]
#[must_use]
2024-11-14 22:43:18 +00:00
pub fn is_secondary(&self) -> bool { self.db.is_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.maps
2024-05-28 06:59:50 +00:00
.get(name)
.expect("column in database does not exist")
}
}