use std::path::PathBuf;
use typed_builder::TypedBuilder;
#[derive(Debug, Clone, TypedBuilder)]
#[builder(doc)]
pub struct FileConfig {
pub path: PathBuf,
#[builder(default = 256)]
pub cache_size_mb: usize,
#[builder(default = true)]
pub create_if_missing: bool,
#[builder(default = false)]
pub truncate: bool,
#[builder(default = false)]
pub read_only: bool,
#[builder(default = true)]
pub use_fsync: bool,
}
impl FileConfig {
pub fn new<P: Into<PathBuf>>(path: P) -> Self {
Self {
path: path.into(),
cache_size_mb: 256,
create_if_missing: true,
truncate: false,
read_only: false,
use_fsync: true,
}
}
pub fn temp() -> Self {
let temp_path = std::env::temp_dir().join(format!("netabase_{}", uuid::Uuid::new_v4()));
Self::new(temp_path)
}
}
#[derive(Debug, Clone, TypedBuilder)]
#[builder(doc)]
pub struct MemoryConfig {
#[builder(default = 1000)]
pub initial_capacity: usize,
#[builder(default = None)]
pub max_entries: Option<usize>,
}
impl Default for MemoryConfig {
fn default() -> Self {
Self {
initial_capacity: 1000,
max_entries: None,
}
}
}
#[derive(Debug, Clone, TypedBuilder)]
#[builder(doc)]
pub struct IndexedDBConfig {
pub database_name: String,
#[builder(default = 1)]
pub version: u32,
}
impl IndexedDBConfig {
pub fn new<S: Into<String>>(database_name: S) -> Self {
Self {
database_name: database_name.into(),
version: 1,
}
}
}
#[derive(Debug, Clone, TypedBuilder)]
#[builder(doc)]
pub struct RedbZeroCopyConfig {
#[builder(default = FileConfig::new("redb_zc.db"))]
pub file_config: FileConfig,
#[builder(default = false)]
pub auto_repair: bool,
#[builder(default = 4096)]
pub page_size: usize,
}
impl RedbZeroCopyConfig {
pub fn new<P: Into<PathBuf>>(path: P) -> Self {
Self {
file_config: FileConfig::new(path),
auto_repair: false,
page_size: 4096,
}
}
}
mod uuid {
use std::time::{SystemTime, UNIX_EPOCH};
pub struct Uuid(u128);
impl Uuid {
pub fn new_v4() -> Self {
let nanos = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_nanos();
Self(nanos)
}
}
impl std::fmt::Display for Uuid {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:032x}", self.0)
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_file_config_builder() {
let config = FileConfig::builder()
.path(PathBuf::from("/tmp/test.db"))
.cache_size_mb(512)
.create_if_missing(false)
.build();
assert_eq!(config.path, PathBuf::from("/tmp/test.db"));
assert_eq!(config.cache_size_mb, 512);
assert!(!config.create_if_missing);
}
#[test]
fn test_file_config_defaults() {
let config = FileConfig::new("/tmp/default.db");
assert_eq!(config.cache_size_mb, 256);
assert!(config.create_if_missing);
assert!(!config.truncate);
}
#[test]
fn test_memory_config_default() {
let config = MemoryConfig::default();
assert_eq!(config.initial_capacity, 1000);
assert_eq!(config.max_entries, None);
}
}