use rusqlite::{OptionalExtension, Params, params};
use super::engine::{Blobs, rows_of};
use super::filling::{END_CLAIM, END_CLAIMS_UNDER, TAKE_CLAIM};
use super::info::{FileInfo, matches, meta_of};
use super::paths::Folder;
use crate::sqlite::{Tx, sql_error};
use crate::{Error, ErrorKind, Result};
macro_rules! hidden {
() => {
"exists (select 1 from _tinystore_blobs_cleared as c where c.set_id = f.set_id and f.revision <= c.revision
and substr(f.path, 1, length(c.prefix)) = c.prefix)"
};
}
pub(crate) use hidden;
pub(crate) struct Content {
pub(crate) id: i64,
pub(crate) size: i64,
pub(crate) sha256: Vec<u8>,
pub(crate) place: Place,
}
pub(crate) enum Place {
Inline(Vec<u8>),
File,
Shared,
}
pub(crate) struct Version {
pub(crate) content: Content,
pub(crate) content_type: String,
pub(crate) meta: Option<String>,
pub(crate) modified: i64,
pub(crate) expires: Option<i64>,
}
impl Version {
pub(crate) fn info(&self, folder: &Folder, path: &str) -> Result<FileInfo> {
Ok(FileInfo {
path: folder.relative(path).to_owned(),
size: u64::try_from(self.content.size).unwrap_or(0),
etag: super::info::etag_of(&self.content.sha256),
content_type: self.content_type.clone(),
last_modified: Blobs::time(self.modified),
expires: self.expires.map(Blobs::time),
meta: meta_of(self.meta.as_deref())?,
})
}
}
pub(crate) struct Current {
pub(crate) content: i64,
pub(crate) size: i64,
pub(crate) sha256: Vec<u8>,
pub(crate) content_type: String,
pub(crate) expires: Option<i64>,
pub(crate) meta: Option<String>,
hidden: bool,
}
impl Current {
pub(crate) fn live(&self, now: i64) -> bool {
!self.hidden && self.expires.is_none_or(|expires| expires > now)
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub(crate) enum WhenThere {
#[default]
Replace,
Skip,
Conflict,
}
#[derive(Clone, Debug, Default)]
pub(crate) struct Condition {
pub(crate) if_match: Option<String>,
pub(crate) when_there: WhenThere,
pub(crate) fill: Option<i64>,
}
pub(super) fn holds_etag(etag: &str, existing: Option<&Current>, now: i64) -> Result<()> {
match existing.filter(|current| current.live(now)) {
Some(current) if matches(etag, ¤t.sha256) => Ok(()),
_ => Err(Error::new(ErrorKind::Conflict, "the path holds no file with the ETag given")),
}
}
impl Condition {
fn holds(&self, existing: Option<&Current>, now: i64) -> Result<bool> {
if let Some(etag) = &self.if_match {
return holds_etag(etag, existing, now).map(|()| true);
}
let live = existing.filter(|current| current.live(now));
match (live, self.when_there) {
(None, _) | (Some(_), WhenThere::Replace) => Ok(true),
(Some(_), WhenThere::Skip) => Ok(false),
(Some(_), WhenThere::Conflict) => Err(Error::new(ErrorKind::Conflict, "the path holds a file")),
}
}
}
const SELECT_CURRENT: &str = concat!(
"select f.content, f.size, f.sha256, f.type, f.expires, f.meta, ",
hidden!(),
" from _tinystore_blobs_files as f where f.set_id = ?1 and f.path = ?2"
);
const INSERT_CONTENT: &str =
"insert into _tinystore_blobs_contents (id, names, size, sha256, inline) values (?1, 1, ?2, ?3, ?4)";
const INSERT_BODY: &str = "insert into _tinystore_blobs_bodies (id, bytes) values (?1, ?2)";
const NAME_CONTENT: &str = "update _tinystore_blobs_contents set names = names + 1 where id = ?1 returning names";
const UPSERT_FILE: &str =
"insert into _tinystore_blobs_files (set_id, path, revision, content, size, sha256, type, modified, expires, meta)
values (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10)
on conflict (set_id, path) do update set revision = excluded.revision, content = excluded.content,
size = excluded.size, sha256 = excluded.sha256, type = excluded.type, modified = excluded.modified,
expires = excluded.expires, meta = excluded.meta, used = null, rank = null";
const DELETE_FILE: &str = "delete from _tinystore_blobs_files where set_id = ?1 and path = ?2 returning content";
const UNNAME_CONTENT: &str =
"update _tinystore_blobs_contents set names = names - ?2 where id = ?1 returning names, inline";
const DROP_CONTENT: &str = "delete from _tinystore_blobs_contents where id = ?1";
const DROP_BODY: &str = "delete from _tinystore_blobs_bodies where id = ?1";
pub(crate) struct Change<'t, 'c> {
pub(super) tx: &'t Tx<'c>,
pub(super) blobs: &'t Blobs,
pub(crate) now: i64,
pub(crate) freed: Vec<i64>,
}
impl<'t, 'c> Change<'t, 'c> {
pub(crate) fn new(tx: &'t Tx<'c>, blobs: &'t Blobs) -> Self {
Change { tx, blobs, now: blobs.now(), freed: Vec::new() }
}
pub(crate) fn current(&self, set: i64, path: &str) -> Result<Option<Current>> {
self.tx
.prepare_cached(SELECT_CURRENT)
.and_then(|mut statement| {
statement
.query_row(params![set, path], |row| {
Ok(Current {
content: row.get(0)?,
size: row.get(1)?,
sha256: row.get(2)?,
content_type: row.get(3)?,
expires: row.get(4)?,
meta: row.get(5)?,
hidden: row.get(6)?,
})
})
.optional()
})
.map_err(|error| sql_error("blobs: a file's row", error))
}
pub(crate) fn write(&mut self, set: i64, path: &str, version: &Version, condition: &Condition) -> Result<bool> {
if let Some(owner) = condition.fill
&& !self.fence(set, path, owner)?
{
return Ok(false);
}
let existing = self.current(set, path)?;
if !condition.holds(existing.as_ref(), self.now)? {
return Ok(false);
}
self.end_claim(set, path)?;
let revision = self.blobs.next_revision(self.tx)?;
self.name(&version.content)?;
let content = &version.content;
self.execute(
UPSERT_FILE,
params![
set,
path,
revision,
content.id,
content.size,
content.sha256,
version.content_type,
version.modified,
version.expires,
version.meta
],
)?;
if let Some(existing) = existing {
self.release(existing.content, 1)?;
}
Ok(true)
}
fn name(&mut self, content: &Content) -> Result<()> {
match &content.place {
Place::Shared => {
let names: Option<i64> = self
.tx
.prepare_cached(NAME_CONTENT)
.and_then(|mut statement| statement.query_row([content.id], |row| row.get(0)).optional())
.map_err(|error| sql_error("blobs: name a content", error))?;
names
.map(drop)
.ok_or_else(|| Error::corrupt(format!("content {}, which a file names, is missing", content.id)))
}
Place::Inline(body) => {
self.execute(INSERT_CONTENT, params![content.id, content.size, content.sha256, true])?;
self.execute(INSERT_BODY, params![content.id, body])
}
Place::File => self.execute(INSERT_CONTENT, params![content.id, content.size, content.sha256, false]),
}
}
pub(crate) fn remove(&mut self, set: i64, path: &str) -> Result<bool> {
self.end_claim(set, path)?;
let content: Option<i64> = self
.tx
.prepare_cached(DELETE_FILE)
.and_then(|mut statement| statement.query_row(params![set, path], |row| row.get(0)).optional())
.map_err(|error| sql_error("blobs: delete a file's row", error))?;
match content {
Some(id) => self.release(id, 1).map(|()| true),
None => Ok(false),
}
}
pub(crate) fn release(&mut self, id: i64, count: i64) -> Result<()> {
let left: Option<(i64, bool)> = self
.tx
.prepare_cached(UNNAME_CONTENT)
.and_then(|mut statement| {
statement.query_row(params![id, count], |row| Ok((row.get(0)?, row.get(1)?))).optional()
})
.map_err(|error| sql_error("blobs: unname a content", error))?;
match left {
None => Err(Error::corrupt(format!("content {id}, which a file named, is missing"))),
Some((names, _)) if names < 0 => Err(Error::corrupt(format!("content {id} lost more names than it had"))),
Some((names, _)) if names > 0 => Ok(()),
Some((_, false)) => {
self.freed.push(id);
Ok(())
}
Some((_, true)) => {
self.execute(DROP_BODY, [id])?;
self.execute(DROP_CONTENT, [id])
}
}
}
pub(crate) fn release_all(&mut self, mut contents: Vec<i64>) -> Result<()> {
contents.sort_unstable();
for run in contents.chunk_by(|a, b| a == b) {
self.release(run[0], i64::try_from(run.len()).unwrap_or(i64::MAX))?;
}
Ok(())
}
pub(crate) fn deleted(&self, sql: &str, parameters: impl Params) -> Result<Vec<i64>> {
rows_of(self.tx, (sql, "blobs: delete files"), parameters, |row| row.get(0))
}
pub(crate) fn execute(&self, sql: &str, parameters: impl Params) -> Result<()> {
self.tx
.prepare_cached(sql)
.and_then(|mut statement| statement.execute(parameters))
.map(drop)
.map_err(|error| sql_error("blobs: a write", error))
}
pub(crate) fn next_revision(&self) -> Result<i64> {
self.blobs.next_revision(self.tx)
}
fn fence(&self, set: i64, path: &str, owner: i64) -> Result<bool> {
let taken = self
.tx
.prepare_cached(TAKE_CLAIM)
.and_then(|mut take| take.execute(params![set, path, owner, self.now]))
.map_err(|error| sql_error("blobs: a run's claim", error))?;
Ok(taken == 1)
}
fn end_claim(&self, set: i64, path: &str) -> Result<()> {
let ended = rows_of(self.tx, (END_CLAIM, "blobs: end a claim"), params![set, path], |row| row.get(0))?;
self.end_runs(ended);
Ok(())
}
pub(crate) fn end_claims_under(&self, set: i64, from: &str, to: &str) -> Result<()> {
let query = (END_CLAIMS_UNDER, "blobs: end the claims under a folder");
let ended = rows_of(self.tx, query, params![set, from, to], |row| row.get(0))?;
self.end_runs(ended);
Ok(())
}
fn end_runs(&self, owners: Vec<i64>) {
for owner in owners {
self.blobs.fills.end(owner, None);
}
}
}