use std::collections::{HashMap, VecDeque};
use std::ops::Range;
use std::sync::Arc;
use crate::{ColumnValue, DbError, Model, ModelContainer, SqlType, Value};
#[derive(Clone, Default)]
pub struct Blob {
pending: Option<Arc<Vec<u8>>>,
}
impl Blob {
pub fn new(bytes: impl Into<Vec<u8>>) -> Blob {
Blob {
pending: Some(Arc::new(bytes.into())),
}
}
pub const fn stored() -> Blob {
Blob { pending: None }
}
pub fn is_pending(&self) -> bool {
self.pending.is_some()
}
pub fn pending(&self) -> Option<&Arc<Vec<u8>>> {
self.pending.as_ref()
}
pub fn take_pending(&mut self) -> Option<Arc<Vec<u8>>> {
self.pending.take()
}
}
impl PartialEq for Blob {
fn eq(&self, other: &Self) -> bool {
match (&self.pending, &other.pending) {
(None, None) => true,
(Some(a), Some(b)) => a == b,
_ => false,
}
}
}
impl std::fmt::Debug for Blob {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match &self.pending {
Some(b) => write!(f, "Blob(pending {} bytes)", b.len()),
None => f.write_str("Blob(stored)"),
}
}
}
impl ColumnValue for Blob {
const SQL_TYPE: SqlType = SqlType::Blob;
fn to_sqlite_value(&self) -> Value {
match &self.pending {
Some(b) => Value::Blob(b.as_ref().clone()),
None => Value::Null,
}
}
fn from_sqlite_value(_: Value) -> Result<Self, DbError> {
Ok(Blob::stored())
}
}
pub trait BlobField {
fn bytes(&self) -> Result<Arc<Vec<u8>>, DbError>;
fn len(&self) -> Result<usize, DbError>;
fn is_empty(&self) -> Result<bool, DbError> {
Ok(self.len()? == 0)
}
fn read_range(&self, range: Range<usize>) -> Result<Vec<u8>, DbError>;
fn write_bytes(&self, bytes: impl Into<Vec<u8>>);
fn reader(&self) -> BlobReader;
}
impl<S, M> BlobField for day_model::Field<S, M, Blob>
where
S: day_model::Source<M> + Copy + 'static,
M: Model + 'static,
{
fn bytes(&self) -> Result<Arc<Vec<u8>>, DbError> {
if let Some(pending) = day_reactive::Binding::read(self).pending {
return Ok(pending);
}
let (container, store, key) = locate(*self)?;
container.blob_bytes(store, key, column_of::<M>(self.label()))
}
fn len(&self) -> Result<usize, DbError> {
if let Some(pending) = day_reactive::Binding::read(self).pending {
return Ok(pending.len());
}
let (container, store, key) = locate(*self)?;
container.blob_len(store, key, column_of::<M>(self.label()))
}
fn read_range(&self, range: Range<usize>) -> Result<Vec<u8>, DbError> {
if let Some(pending) = day_reactive::Binding::read(self).pending {
return Ok(slice(&pending, range));
}
let (container, store, key) = locate(*self)?;
container.blob_range(store, key, column_of::<M>(self.label()), range)
}
fn write_bytes(&self, bytes: impl Into<Vec<u8>>) {
if let Ok((container, store, key)) = locate(*self) {
container.blob_forget_field(store, key, column_of::<M>(self.label()));
}
day_reactive::Binding::write(self, Blob::new(bytes));
}
fn reader(&self) -> BlobReader {
let field = *self;
BlobReader {
read_range: Box::new(move |r| field.read_range(r)),
len: Box::new(move || field.len()),
pos: 0,
end: None,
}
}
}
type RangeReadFn = Box<dyn Fn(Range<usize>) -> Result<Vec<u8>, DbError>>;
type LenFn = Box<dyn Fn() -> Result<usize, DbError>>;
pub struct BlobReader {
read_range: RangeReadFn,
len: LenFn,
pos: usize,
end: Option<usize>,
}
impl BlobReader {
pub const CHUNK: usize = 256 * 1024;
}
impl std::io::Read for BlobReader {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
let end = if let Some(e) = self.end {
e
} else {
let e = (self.len)().map_err(std::io::Error::other)?;
self.end = Some(e);
e
};
if self.pos >= end || buf.is_empty() {
return Ok(0);
}
let want = buf.len().min(Self::CHUNK).min(end - self.pos);
let chunk = (self.read_range)(self.pos..self.pos + want).map_err(std::io::Error::other)?;
let n = chunk.len().min(buf.len());
buf[..n].copy_from_slice(&chunk[..n]);
self.pos += n;
Ok(n)
}
}
fn slice(bytes: &[u8], range: Range<usize>) -> Vec<u8> {
let start = range.start.min(bytes.len());
let end = range.end.clamp(start, bytes.len());
bytes[start..end].to_vec()
}
fn column_of<M: Model>(field: &'static str) -> &'static str {
M::COLUMNS
.iter()
.find(|c| c.field == field)
.map_or(field, |c| c.name)
}
fn locate<S, M>(field: day_model::Field<S, M, Blob>) -> Result<(ModelContainer, u64, u64), DbError>
where
S: day_model::Source<M> + Copy + 'static,
M: 'static,
{
let mut parts = Vec::new();
day_model::Source::components(field, &mut parts);
let (Some(&store), Some(&key)) = (parts.first(), parts.get(1)) else {
return Err(DbError::driver(
"a Blob field can only be read through a row of a container's store",
));
};
crate::relations::container_for_store(store)
.map(|c| (c, store, key))
.ok_or_else(|| {
DbError::driver("a Blob field was read on a store no open ModelContainer holds")
})
}
#[derive(Default)]
pub(crate) struct BlobMemo {
entries: HashMap<(u64, u64, &'static str), Arc<Vec<u8>>>,
order: VecDeque<(u64, u64, &'static str)>,
bytes: usize,
}
impl BlobMemo {
pub(crate) fn get(&mut self, k: (u64, u64, &'static str)) -> Option<Arc<Vec<u8>>> {
let hit = self.entries.get(&k).cloned()?;
if let Some(i) = self.order.iter().position(|e| *e == k) {
self.order.remove(i);
self.order.push_back(k);
}
Some(hit)
}
pub(crate) fn insert(&mut self, k: (u64, u64, &'static str), v: Arc<Vec<u8>>, limit: usize) {
self.forget_one(k);
if v.len() > limit {
return;
}
self.bytes += v.len();
self.entries.insert(k, v);
self.order.push_back(k);
while self.bytes > limit {
let Some(old) = self.order.pop_front() else {
break;
};
if let Some(v) = self.entries.remove(&old) {
self.bytes -= v.len();
}
}
}
pub(crate) fn forget_one(&mut self, k: (u64, u64, &'static str)) {
if let Some(v) = self.entries.remove(&k) {
self.bytes -= v.len();
if let Some(i) = self.order.iter().position(|e| *e == k) {
self.order.remove(i);
}
}
}
pub(crate) fn forget_row(&mut self, store: u64, key: u64) {
let keys: Vec<_> = self
.entries
.keys()
.filter(|(s, k, _)| *s == store && *k == key)
.copied()
.collect();
for k in keys {
self.forget_one(k);
}
}
pub(crate) fn forget_store(&mut self, store: u64) {
let keys: Vec<_> = self
.entries
.keys()
.filter(|(s, _, _)| *s == store)
.copied()
.collect();
for k in keys {
self.forget_one(k);
}
}
pub(crate) fn clear(&mut self) {
self.entries.clear();
self.order.clear();
self.bytes = 0;
}
pub(crate) fn bytes(&self) -> usize {
self.bytes
}
pub(crate) fn len(&self) -> usize {
self.entries.len()
}
}