pub mod cursor;
pub mod envelope;
pub(crate) mod erasure;
pub mod error;
pub(crate) mod fileio;
pub mod grammar;
pub mod journal;
pub mod layout;
mod ledger;
pub(crate) mod lock;
pub(crate) mod pages;
mod restore;
pub(crate) mod scalar;
mod search;
pub mod time;
mod turns;
mod write;
use std::fs::{self, File};
use std::io;
use std::path::{Path, PathBuf};
use self::envelope::Envelope;
use self::error::{Error, MemoryError};
use self::grammar::{parse_scope, validate_document_path};
use self::journal::Row;
use self::layout::{DOCUMENTS, REDACTIONS, VERSIONS};
pub use self::pages::Page;
pub use self::restore::Restored;
use self::time::Stamp;
pub const FORMAT: u64 = 1;
pub struct Document {
pub envelope: Envelope,
pub content: String,
}
pub struct Entry {
pub path: String,
pub version: u64,
pub created_at: Stamp,
pub updated_at: Stamp,
pub redacted: bool,
}
pub struct FileStore {
root: PathBuf,
}
impl FileStore {
pub fn open(root: &Path) -> io::Result<Self> {
let root = std::path::absolute(root)?;
fileio::private_mkdir(&root)?;
Ok(Self {
root: root.canonicalize()?,
})
}
pub fn read(&self, scope: &str, path: &str) -> Result<Option<Document>, Error> {
let _held = self.lock()?;
self.scope(scope)?;
validate_document_path(path)?;
self.present(scope, path, &layout::doc_file(&self.root, scope, path)?)
}
pub fn list_documents(&self, scope: &str, prefix: &str) -> Result<Vec<Entry>, Error> {
let _held = self.lock()?;
self.scope(scope)?;
self.listing(scope, prefix)
}
fn listing(&self, scope: &str, prefix: &str) -> Result<Vec<Entry>, Error> {
let documents = layout::scope_dir(&self.root, scope).join(DOCUMENTS);
let mut entries = Vec::new();
for path in layout::logical_paths(&documents, ".md")? {
if !path.starts_with(prefix) {
continue;
}
let Document { envelope, .. } =
self.load(scope, &path, &layout::doc_file(&self.root, scope, &path)?)?;
entries.push(Entry {
path,
version: envelope.version,
created_at: envelope.created_at,
updated_at: envelope.updated_at,
redacted: envelope.redacted,
});
}
Ok(entries)
}
pub fn versions(&self, scope: &str, path: &str, limit: usize) -> Result<Vec<Row>, Error> {
let _held = self.lock()?;
self.scope(scope)?;
validate_document_path(path)?;
let (_, mut rows) = self.rows(scope, path)?;
rows.reverse();
rows.truncate(limit);
Ok(rows)
}
pub fn scopes(&self) -> Result<Vec<String>, Error> {
let _held = self.lock()?;
let mut found = Vec::new();
walk_scopes(&self.root, &[], &mut found)?;
found.sort();
Ok(found)
}
fn lock(&self) -> io::Result<File> {
lock::acquire(&self.root.join(".store.lock"))
}
fn scope(&self, scope: &str) -> Result<(), Error> {
parse_scope(scope)?;
let directory = layout::scope_dir(&self.root, scope);
for path in [
directory.clone(),
directory.join(DOCUMENTS),
directory.join(VERSIONS),
] {
layout::contained(&self.root, path, scope)?;
}
if layout::case_collision(&self.root, scope)? {
return Err(MemoryError::conflict(scope, None, "case_collision").into());
}
Ok(())
}
fn rows(&self, scope: &str, path: &str) -> Result<(PathBuf, Vec<Row>), Error> {
let file = layout::journal_file(&self.root, scope, path)?;
let rows = journal::read_rows(&file, scope, path)?;
if rows.first().is_some_and(|row| row.path != path) {
return Err(MemoryError::conflict(scope, Some(path), "case_collision").into());
}
Ok((file, rows))
}
fn present(&self, scope: &str, path: &str, file: &Path) -> Result<Option<Document>, Error> {
if !file.is_file() {
return Ok(None);
}
self.load(scope, path, file).map(Some)
}
fn load(&self, scope: &str, path: &str, file: &Path) -> Result<Document, Error> {
let text = fs::read_to_string(file)?;
let (envelope, content) = envelope::parse(&text, scope, path)?;
Ok(Document {
envelope,
content: content.to_string(),
})
}
}
fn walk_scopes(dir: &Path, parts: &[String], found: &mut Vec<String>) -> io::Result<()> {
for entry in fs::read_dir(dir)? {
let entry = entry?;
let Ok(name) = entry.file_name().into_string() else {
continue;
};
if !entry.file_type()?.is_dir() || !name.contains("%3A") {
continue;
}
let below: Vec<String> = parts.iter().cloned().chain([name]).collect();
let decoded: Vec<String> = below.iter().map(|part| part.replace("%3A", ":")).collect();
let scope = decoded.join("/");
if parse_scope(&scope).is_err() {
continue;
}
if occupied(&entry.path())? {
found.push(scope);
}
walk_scopes(&entry.path(), &below, found)?;
}
Ok(())
}
fn occupied(scope_dir: &Path) -> io::Result<bool> {
fn holds(dir: &Path, suffix: &str) -> io::Result<bool> {
if !dir.is_dir() || fs::symlink_metadata(dir)?.file_type().is_symlink() {
return Ok(false);
}
for entry in fs::read_dir(dir)? {
let entry = entry?;
let kind = entry.file_type()?;
if kind.is_symlink() {
continue;
}
let path = entry.path();
let found = if kind.is_dir() {
holds(&path, suffix)?
} else {
path.to_string_lossy().ends_with(suffix)
};
if found {
return Ok(true);
}
}
Ok(false)
}
Ok(holds(&scope_dir.join(DOCUMENTS), ".md")?
|| holds(&scope_dir.join(VERSIONS), ".jsonl")?
|| scope_dir.join(REDACTIONS).is_file())
}
#[cfg(test)]
mod tests;