use std::ops::Deref;
use std::path::Path;
use store_io_core::decide::{Trust, decide};
use store_io_core::error::{Error, ErrorContext, Named, NotWrittenCause, Op, OsError};
use store_io_platform::{FileName, LinuxTier, Platform};
use crate::{NativePlatform, Probe, StoreOptions};
fn platform(tier: LinuxTier) -> Result<NativePlatform, Error> {
#[cfg(target_os = "linux")]
{
NativePlatform::with_tier(tier).map_err(|raw| io(Op::Queue, raw))
}
#[cfg(not(target_os = "linux"))]
{
let _ = tier;
Ok(NativePlatform::new())
}
}
fn path_of<P: AsRef<Path> + ?Sized>(p: &P) -> &Path {
p.as_ref()
}
pub type AppendRegion = store_io_engine::AppendRegion<NativePlatform>;
pub type PageRegion = store_io_engine::PageRegion<NativePlatform>;
pub type Slot = store_io_engine::Slot<NativePlatform>;
pub type AppendBatch = store_io_engine::AppendBatch<NativePlatform>;
pub type PageBatch = store_io_engine::PageBatch<NativePlatform>;
pub type Reservation = store_io_engine::Reservation<NativePlatform>;
pub type Submitter = store_io_engine::Submitter<NativePlatform>;
#[derive(Clone, Debug)]
pub struct Store(store_io_engine::Store<NativePlatform>);
impl Store {
pub fn create(path: impl AsRef<Path>, opts: StoreOptions) -> Result<Self, Error> {
store_io_engine::Store::create(platform(opts.linux_tier)?, path_of(&path), opts).map(Self)
}
pub fn open(path: impl AsRef<Path>) -> Result<Self, Error> {
Self::open_with(path, StoreOptions::default())
}
pub fn open_with(path: impl AsRef<Path>, opts: StoreOptions) -> Result<Self, Error> {
store_io_engine::Store::open(platform(opts.linux_tier)?, path_of(&path), opts).map(Self)
}
pub fn open_readonly(path: impl AsRef<Path>) -> Result<Self, Error> {
Self::open_readonly_with(path, StoreOptions::default())
}
pub fn open_readonly_with(path: impl AsRef<Path>, opts: StoreOptions) -> Result<Self, Error> {
store_io_engine::Store::open_readonly(platform(opts.linux_tier)?, path_of(&path), opts)
.map(Self)
}
#[must_use]
pub fn engine(&self) -> &store_io_engine::Store<NativePlatform> {
&self.0
}
}
impl Deref for Store {
type Target = store_io_engine::Store<NativePlatform>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl From<store_io_engine::Store<NativePlatform>> for Store {
fn from(s: store_io_engine::Store<NativePlatform>) -> Self {
Self(s)
}
}
#[derive(Debug)]
pub struct Directory(store_io_engine::Directory<NativePlatform>);
impl Directory {
pub fn open(path: impl AsRef<Path>, create: bool) -> Result<Self, Error> {
Self::open_with(path, create, Trust::default())
}
pub fn open_with(path: impl AsRef<Path>, create: bool, trust: Trust) -> Result<Self, Error> {
store_io_engine::Directory::open(platform(LinuxTier::Auto)?, path_of(&path), create, trust)
.map(Self)
}
}
impl Deref for Directory {
type Target = store_io_engine::Directory<NativePlatform>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
fn io(op: Op, raw: OsError) -> Error {
Error::Io {
op,
raw,
ctx: ErrorContext::default(),
}
}
pub fn probe(dir: impl AsRef<Path>) -> Result<Probe, Error> {
let p = NativePlatform::new();
let d = p.open_dir(path_of(&dir), false).map_err(|raw| {
if store_io_core::errno::is_not_found(raw) {
Error::NotFound { what: Named::Store }
} else {
io(Op::Open, raw)
}
})?;
let name = FileName::new(&format!(".store-io-probe-{}", std::process::id())).map_err(|_| {
Error::NotWritten {
cause: NotWrittenCause::InvalidName,
ctx: ErrorContext::default(),
}
})?;
let _leftover = p.unlink(&d, &name);
let file = p
.create_file(&d, &name)
.map_err(|raw| io(Op::Create, raw))?;
let evidence = p
.allocate(&file, 4096)
.and_then(|()| p.probe(&d, &file))
.map_err(|raw| io(Op::Probe, raw));
drop(file);
let _removed = p.unlink(&d, &name);
let evidence = evidence?;
let decision = decide(&evidence, &Trust::default());
Ok(Probe { evidence, decision })
}