mod config;
mod helpers;
mod inspection;
mod ops_batch;
mod ops_single;
use crate::data_table::DataTable;
use crate::types::{NamespaceConfig, NamespaceMetadata, NamespaceSnapshot};
use crate::{Result, RkvsError};
use std::sync::Arc;
use tokio::sync::RwLock;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BatchMode {
AllOrNothing,
BestEffort,
}
const DEFAULT_SHARD_CAPACITY: usize = 10000;
#[derive(Debug)]
pub struct Namespace {
shards: Arc<RwLock<Vec<Arc<RwLock<DataTable>>>>>,
metadata: NamespaceMetadata,
config: NamespaceConfig,
}
impl Namespace {
pub fn new(name: String, config: NamespaceConfig) -> Self {
if config.shard_count() == 0 {
config.set_shard_count(1);
}
if config.max_keys() == 0 {
config.set_max_keys(usize::MAX);
}
if config.max_value_size() == 0 {
config.set_max_value_size(usize::MAX);
}
let shards = (0..config.shard_count())
.map(|_| {
Arc::new(RwLock::new(DataTable::with_capacity(
DEFAULT_SHARD_CAPACITY,
)))
})
.collect();
Self {
shards: Arc::new(RwLock::new(shards)),
metadata: NamespaceMetadata::new(name),
config,
}
}
pub fn from_snapshot(snapshot: NamespaceSnapshot) -> Self {
let shards = snapshot
.shards
.into_iter()
.map(|table| Arc::new(RwLock::new(table)))
.collect();
Self {
shards: Arc::new(RwLock::new(shards)),
metadata: snapshot.metadata,
config: snapshot.config,
}
}
pub async fn create_snapshot(&self) -> NamespaceSnapshot {
let shards_guard = self.shards.read().await;
let mut tables = Vec::with_capacity(shards_guard.len());
for shard in shards_guard.iter() {
let data = shard.read().await;
tables.push(data.snapshot());
}
NamespaceSnapshot {
shards: tables,
metadata: self.metadata.clone(),
config: self.config.clone(),
}
}
}