use std::path::{Path, PathBuf};
use crate::{Error, Result};
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum StorageBackend {
Memory,
Fjall,
}
impl StorageBackend {
#[must_use]
pub const fn name(self) -> &'static str {
match self {
Self::Memory => "memory",
Self::Fjall => "fjall",
}
}
pub fn from_name(name: &str) -> Result<Self> {
match name.trim().to_ascii_lowercase().as_str() {
"memory" | "mem" => Ok(Self::Memory),
"fjall" => Ok(Self::Fjall),
"rocksdb" | "redb" => Err(Error::Unsupported(
"storage backend was replaced by fjall; use Model::open / OpenOptions::fjall"
.into(),
)),
"hashes" | "file" | "mysql" | "postgresql" | "postgres" | "sqlite" | "tstore"
| "uri" | "virtuoso" => Err(Error::Unsupported(format!(
"legacy Redland storage backend '{name}' is unsupported; export to N-Quads and use memory or fjall (see docs/design/0.4-legacy-storage.md)"
))),
other => Err(Error::Unsupported(format!(
"storage backend '{other}' is not recognized"
))),
}
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct OpenOptions {
path: PathBuf,
read_only: bool,
create: bool,
}
impl OpenOptions {
#[must_use]
pub fn fjall(path: impl AsRef<Path>) -> Self {
Self {
path: path.as_ref().to_owned(),
read_only: false,
create: true,
}
}
#[must_use]
pub fn path(&self) -> &Path {
&self.path
}
#[must_use]
pub fn read_only(mut self, read_only: bool) -> Self {
self.read_only = read_only;
self
}
#[must_use]
pub fn create(mut self, create: bool) -> Self {
self.create = create;
self
}
#[must_use]
pub fn is_read_only(&self) -> bool {
self.read_only
}
#[must_use]
pub fn can_create(&self) -> bool {
self.create
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct StorageCapabilities {
pub backend: StorageBackend,
pub durable: bool,
pub transactions: bool,
pub sync: bool,
pub clear: bool,
pub read_only: bool,
pub bulk_load: bool,
}
impl StorageCapabilities {
#[must_use]
pub const fn memory() -> Self {
Self {
backend: StorageBackend::Memory,
durable: false,
transactions: true,
sync: true,
clear: true,
read_only: false,
bulk_load: true,
}
}
#[must_use]
pub const fn fjall(read_only: bool) -> Self {
Self {
backend: StorageBackend::Fjall,
durable: true,
transactions: !read_only,
sync: true,
clear: !read_only,
read_only,
bulk_load: !read_only,
}
}
}