Files
continuwuity/src/database/mod.rs
T

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

99 lines
2.0 KiB
Rust
Raw Normal View History

#![type_length_limit = "3072"]
extern crate conduwuit_core as conduwuit;
extern crate rust_rocksdb as rocksdb;
2025-08-31 03:01:06 +01:00
use ctor::{ctor, dtor};
conduwuit::mod_ctor! {}
conduwuit::mod_dtor! {}
conduwuit::rustc_flags_capture! {}
2025-03-28 20:33:38 +00:00
#[cfg(test)]
mod benches;
2024-07-01 20:54:38 +00:00
mod cork;
2024-08-08 17:18:30 +00:00
mod de;
mod deserialized;
2024-05-28 06:59:50 +00:00
mod engine;
2024-07-02 05:56:10 +00:00
mod handle;
2024-08-08 17:18:30 +00:00
pub mod keyval;
2024-05-28 06:59:50 +00:00
mod map;
pub mod maps;
2024-11-27 06:28:32 +00:00
mod pool;
2024-08-08 17:18:30 +00:00
mod ser;
mod stream;
2025-01-08 20:43:02 +00:00
#[cfg(test)]
2024-10-06 05:15:24 +00:00
mod tests;
pub(crate) mod util;
2024-05-28 06:59:50 +00:00
mod watchers;
2021-06-08 18:10:00 +02:00
use std::{ops::Index, sync::Arc};
2024-08-08 17:18:30 +00:00
use conduwuit::{Result, Server, err};
2024-05-28 06:59:50 +00:00
2024-08-08 17:18:30 +00:00
pub use self::{
2024-10-09 19:41:52 +00:00
de::{Ignore, IgnoreAll},
2024-08-08 17:18:30 +00:00
deserialized::Deserialized,
handle::Handle,
keyval::{KeyVal, Slice, serialize_key, serialize_val},
map::{Get, Map, Qry, compact},
ser::{Cbor, Interfix, Json, SEP, Separator, serialize, serialize_to, serialize_to_vec},
2024-08-08 17:18:30 +00:00
};
pub(crate) use self::{
engine::{Engine, context::Context},
2025-01-18 12:05:07 +00:00
util::or_else,
};
use crate::maps::{Maps, MapsKey, MapsVal};
2024-03-05 19:48:54 -05:00
pub struct Database {
maps: Maps,
pub db: Arc<Engine>,
pub(crate) _ctx: Arc<Context>,
}
impl Database {
/// Load an existing database or create a new one.
pub async fn open(server: &Arc<Server>) -> Result<Arc<Self>> {
let ctx = Context::new(server)?;
let db = Engine::open(ctx.clone(), maps::MAPS).await?;
Ok(Arc::new(Self {
maps: maps::open(&db)?,
db: db.clone(),
_ctx: ctx,
}))
}
#[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()
}
#[inline]
pub fn keys(&self) -> impl Iterator<Item = &MapsKey> + Send + '_ { self.maps.keys() }
#[inline]
#[must_use]
pub fn is_read_only(&self) -> bool { self.db.is_read_only() }
#[inline]
#[must_use]
pub fn is_secondary(&self) -> bool { self.db.is_secondary() }
}
impl Index<&str> for Database {
type Output = Arc<Map>;
fn index(&self, name: &str) -> &Self::Output {
self.maps
.get(name)
.expect("column in database does not exist")
}
}