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::{
CopyOptions, CopyReport, FileSinkOptions, FileSourceOptions, ImportError, ImportOptions,
ImportReport, ImportSource, NativePlatform, Probe, StoreOptions, StoreSinkOptions,
};
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>;
pub type StoreSet = store_io_engine::StoreSet<NativePlatform>;
pub type ReadStream = store_io_engine::ReadStream<NativePlatform>;
pub type StreamRange<'a> = store_io_engine::StreamRange<'a, NativePlatform>;
pub type FileSink = store_io_engine::FileSink<NativePlatform>;
pub type FileSource = store_io_engine::FileSource<NativePlatform>;
pub type StoreSink = store_io_engine::StoreSink<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)
}
}
impl From<Store> for store_io_engine::Store<NativePlatform> {
fn from(s: Store) -> Self {
s.0
}
}
#[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)
}
pub const DEFAULT_MAX_OBJECT: u64 =
store_io_engine::Directory::<NativePlatform>::DEFAULT_MAX_OBJECT;
#[must_use]
pub fn with_max_object(self, max: u64) -> Self {
Self(self.0.with_max_object(max))
}
}
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 copy_store(
from: impl AsRef<Path>,
to: impl AsRef<Path>,
opts: CopyOptions,
) -> Result<CopyReport, Error> {
store_io_engine::copy_store(
platform(opts.store.linux_tier)?,
path_of(&from),
path_of(&to),
opts,
)
}
pub fn file_sink(dir: impl AsRef<Path>, opts: FileSinkOptions) -> Result<FileSink, Error> {
store_io_engine::FileSink::create(platform(opts.store.linux_tier)?, path_of(&dir), opts)
}
pub fn file_source(dir: impl AsRef<Path>, opts: FileSourceOptions) -> Result<FileSource, Error> {
store_io_engine::FileSource::open(platform(opts.store.linux_tier)?, path_of(&dir), opts)
}
pub fn import(
source: &mut dyn ImportSource,
to: impl AsRef<Path>,
opts: ImportOptions,
) -> Result<ImportReport, Box<ImportError>> {
let p = platform(opts.store.linux_tier)
.map_err(|error| Box::new(ImportError::from_error(error)))?;
store_io_engine::import(p, path_of(&to), source, opts)
}
pub fn store_sink(to: impl AsRef<Path>, opts: StoreSinkOptions) -> Result<StoreSink, Error> {
store_io_engine::StoreSink::create(platform(opts.store.linux_tier)?, path_of(&to), opts)
}
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)
}
})?;
static PROBES: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
let n = PROBES.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
let salt = {
use std::hash::{BuildHasher, Hasher};
let mut h = std::collections::hash_map::RandomState::new().build_hasher();
h.write_u64(n);
h.finish()
};
let unique = format!(".store-io-probe-{}-{n}-{salt:016x}", std::process::id());
let name = FileName::new(&unique).map_err(|_| Error::NotWritten {
cause: NotWrittenCause::InvalidName,
ctx: ErrorContext::default(),
})?;
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 })
}