use super::page_store::{MemPages, PageStore};
use super::web_time::WebClock;
use crate::Result;
use crate::error::KeyRingError;
use ::turso::core::io::FileId;
use ::turso::core::io::FileSyncType;
use ::turso::core::{
Buffer, Clock, Completion, File as TursoFileIo, IO, LimboError, MonotonicInstant, OpenFlags,
WallClockInstant, io_error,
};
use std::collections::HashMap;
use std::sync::{Arc, Mutex, MutexGuard};
type TursoResult<T> = std::result::Result<T, LimboError>;
const SNAPSHOT_MAGIC: &[u8; 10] = b"CRYPTEX-IO";
const SNAPSHOT_VERSION: u8 = 1;
pub(crate) struct WebIo {
files: Mutex<HashMap<String, Arc<WebFile>>>,
instance: String,
}
impl WebIo {
pub(crate) fn in_memory() -> Arc<Self> {
Arc::new(Self {
files: Mutex::new(HashMap::new()),
instance: new_instance(),
})
}
pub(crate) fn db_path(&self) -> Option<String> {
let files = self.files.lock().ok()?;
files.keys().find(|path| path.ends_with(".db3")).cloned()
}
pub(crate) fn snapshot(&self) -> Result<Vec<u8>> {
let files = self.files()?;
let mut entries: Vec<(&String, &Arc<WebFile>)> = files.iter().collect();
entries.sort_by(|a, b| a.0.cmp(b.0));
let mut out = Vec::new();
out.extend_from_slice(SNAPSHOT_MAGIC);
out.push(SNAPSHOT_VERSION);
out.extend_from_slice(&(entries.len() as u32).to_le_bytes());
for (path, file) in entries {
let store = file.store().map_err(|_| poisoned())?;
let size = store
.len()
.map_err(|e| KeyRingError::GeneralError { msg: e.to_string() })?;
out.extend_from_slice(&(path.len() as u32).to_le_bytes());
out.extend_from_slice(path.as_bytes());
out.extend_from_slice(&size.to_le_bytes());
out.extend_from_slice(store.as_slice());
}
Ok(out)
}
pub(crate) fn restore(bytes: &[u8]) -> Result<Arc<Self>> {
let malformed = || KeyRingError::GeneralError {
msg: "the keyring image is not a cryptex database snapshot".to_string(),
};
let mut rest = bytes;
if rest.len() < 11 || &rest[..10] != SNAPSHOT_MAGIC {
return Err(malformed());
}
rest = &rest[10..];
if rest[0] != SNAPSHOT_VERSION {
return Err(malformed());
}
rest = &rest[1..];
let (count, tail) = take::<4>(rest).ok_or_else(malformed)?;
rest = tail;
let count = u32::from_le_bytes(count);
if count > 2 {
return Err(malformed());
}
let base = unique_db_path();
let mut files = HashMap::new();
for _ in 0..count {
let (len, tail) = take::<4>(rest).ok_or_else(malformed)?;
rest = tail;
let path_len = u32::from_le_bytes(len) as usize;
if rest.len() < path_len {
return Err(malformed());
}
let path = String::from_utf8(rest[..path_len].to_vec()).map_err(|_| malformed())?;
rest = &rest[path_len..];
let (len, tail) = take::<8>(rest).ok_or_else(malformed)?;
rest = tail;
let data_len = usize::try_from(u64::from_le_bytes(len)).map_err(|_| malformed())?;
if rest.len() < data_len {
return Err(malformed());
}
let store = MemPages::from_vec(rest[..data_len].to_vec());
rest = &rest[data_len..];
let path = if path.ends_with("-wal") {
format!("{base}-wal")
} else {
base.clone()
};
files.insert(path, Arc::new(WebFile::new(store)));
}
Ok(Arc::new(Self {
files: Mutex::new(files),
instance: new_instance(),
}))
}
fn files(&self) -> Result<MutexGuard<'_, HashMap<String, Arc<WebFile>>>> {
self.files.lock().map_err(|_| poisoned())
}
}
pub(crate) fn unique_db_path() -> String {
use std::sync::atomic::{AtomicU64, Ordering};
static NEXT: AtomicU64 = AtomicU64::new(1);
format!("cryptex-{}.db3", NEXT.fetch_add(1, Ordering::Relaxed))
}
pub(super) fn new_instance() -> String {
use std::sync::atomic::{AtomicU64, Ordering};
static NEXT: AtomicU64 = AtomicU64::new(1);
format!("io-{}", NEXT.fetch_add(1, Ordering::Relaxed))
}
fn poisoned() -> KeyRingError {
KeyRingError::GeneralError {
msg: "the in-memory database lock is poisoned".to_string(),
}
}
fn take<const N: usize>(bytes: &[u8]) -> Option<([u8; N], &[u8])> {
let head = bytes.get(..N)?;
let mut out = [0u8; N];
out.copy_from_slice(head);
Some((out, &bytes[N..]))
}
impl IO for WebIo {
fn open_file(
&self,
path: &str,
flags: OpenFlags,
_direct: bool,
) -> TursoResult<Arc<dyn TursoFileIo>> {
let mut files = self
.files
.lock()
.map_err(|_| LimboError::InternalError("database lock poisoned".to_string()))?;
if let Some(file) = files.get(path) {
let existing: Arc<WebFile> = Arc::clone(file);
return Ok(existing);
}
if !flags.contains(OpenFlags::Create) {
return Err(io_error(
std::io::Error::from(std::io::ErrorKind::NotFound),
"open_file",
));
}
let file = Arc::new(WebFile::new(MemPages::new()));
files.insert(path.to_string(), Arc::clone(&file));
let opened: Arc<WebFile> = file;
Ok(opened)
}
fn remove_file(&self, path: &str) -> TursoResult<()> {
let mut files = self
.files
.lock()
.map_err(|_| LimboError::InternalError("database lock poisoned".to_string()))?;
files.remove(path);
Ok(())
}
fn file_id(&self, path: &str) -> TursoResult<FileId> {
Ok(FileId::from_path_hash(&format!(
"{}/{}",
self.instance, path
)))
}
fn is_memory(&self) -> bool {
true
}
}
impl Clock for WebIo {
fn current_time_monotonic(&self) -> MonotonicInstant {
WebClock.current_time_monotonic()
}
fn current_time_wall_clock(&self) -> WallClockInstant {
WebClock.current_time_wall_clock()
}
}
struct WebFile {
store: Mutex<MemPages>,
}
impl WebFile {
fn new(store: MemPages) -> Self {
Self {
store: Mutex::new(store),
}
}
fn store(&self) -> TursoResult<MutexGuard<'_, MemPages>> {
self.store
.lock()
.map_err(|_| LimboError::InternalError("database file lock poisoned".to_string()))
}
}
fn to_i32(n: usize) -> TursoResult<i32> {
i32::try_from(n).map_err(|_| LimboError::InternalError("I/O length overflows i32".into()))
}
impl TursoFileIo for WebFile {
fn lock_file(&self, _exclusive: bool) -> TursoResult<()> {
Ok(())
}
fn unlock_file(&self) -> TursoResult<()> {
Ok(())
}
fn pread(&self, pos: u64, c: Completion) -> TursoResult<Completion> {
let mut store = self.store()?;
let size = store.len().map_err(|e| io_error(e, "pread"))?;
let buf = c.as_read().buf().as_mut_slice();
let n = (buf.len() as u64).min(size.saturating_sub(pos)) as usize;
store
.read_exact_at(pos, &mut buf[..n])
.map_err(|e| io_error(e, "pread"))?;
c.complete(to_i32(n)?);
Ok(c)
}
fn pwrite(&self, pos: u64, buffer: Arc<Buffer>, c: Completion) -> TursoResult<Completion> {
let mut store = self.store()?;
store
.write_all_at(pos, buffer.as_slice())
.map_err(|e| io_error(e, "pwrite"))?;
c.complete(to_i32(buffer.as_slice().len())?);
Ok(c)
}
fn sync(&self, c: Completion, _sync_type: FileSyncType) -> TursoResult<Completion> {
self.store()?.sync().map_err(|e| io_error(e, "sync"))?;
c.complete(0);
Ok(c)
}
fn size(&self) -> TursoResult<u64> {
self.store()?.len().map_err(|e| io_error(e, "size"))
}
fn truncate(&self, len: u64, c: Completion) -> TursoResult<Completion> {
self.store()?
.set_len(len)
.map_err(|e| io_error(e, "truncate"))?;
c.complete(0);
Ok(c)
}
}