1use std::collections::{HashMap, VecDeque};
26use std::ops::Range;
27use std::sync::Arc;
28
29use crate::{ColumnValue, DbError, Model, ModelContainer, SqlType, Value};
30
31#[derive(Clone, Default)]
35pub struct Blob {
36 pending: Option<Arc<Vec<u8>>>,
37}
38
39impl Blob {
40 pub fn new(bytes: impl Into<Vec<u8>>) -> Blob {
42 Blob {
43 pending: Some(Arc::new(bytes.into())),
44 }
45 }
46
47 pub const fn stored() -> Blob {
49 Blob { pending: None }
50 }
51
52 pub fn is_pending(&self) -> bool {
54 self.pending.is_some()
55 }
56
57 pub fn pending(&self) -> Option<&Arc<Vec<u8>>> {
59 self.pending.as_ref()
60 }
61
62 pub fn take_pending(&mut self) -> Option<Arc<Vec<u8>>> {
65 self.pending.take()
66 }
67}
68
69impl PartialEq for Blob {
70 fn eq(&self, other: &Self) -> bool {
73 match (&self.pending, &other.pending) {
74 (None, None) => true,
75 (Some(a), Some(b)) => a == b,
76 _ => false,
77 }
78 }
79}
80
81impl std::fmt::Debug for Blob {
82 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
83 match &self.pending {
84 Some(b) => write!(f, "Blob(pending {} bytes)", b.len()),
85 None => f.write_str("Blob(stored)"),
86 }
87 }
88}
89
90impl ColumnValue for Blob {
91 const SQL_TYPE: SqlType = SqlType::Blob;
92 fn to_sqlite_value(&self) -> Value {
95 match &self.pending {
96 Some(b) => Value::Blob(b.as_ref().clone()),
97 None => Value::Null,
98 }
99 }
100 fn from_sqlite_value(_: Value) -> Result<Self, DbError> {
102 Ok(Blob::stored())
103 }
104}
105
106pub trait BlobField {
116 fn bytes(&self) -> Result<Arc<Vec<u8>>, DbError>;
118 fn len(&self) -> Result<usize, DbError>;
120 fn is_empty(&self) -> Result<bool, DbError> {
122 Ok(self.len()? == 0)
123 }
124 fn read_range(&self, range: Range<usize>) -> Result<Vec<u8>, DbError>;
127 fn write_bytes(&self, bytes: impl Into<Vec<u8>>);
129 fn reader(&self) -> BlobReader;
131}
132
133impl<S, M> BlobField for day_model::Field<S, M, Blob>
134where
135 S: day_model::Source<M> + Copy + 'static,
136 M: Model + 'static,
137{
138 fn bytes(&self) -> Result<Arc<Vec<u8>>, DbError> {
139 if let Some(pending) = day_reactive::Binding::read(self).pending {
140 return Ok(pending);
141 }
142 let (container, store, key) = locate(*self)?;
143 container.blob_bytes(store, key, column_of::<M>(self.label()))
144 }
145
146 fn len(&self) -> Result<usize, DbError> {
147 if let Some(pending) = day_reactive::Binding::read(self).pending {
148 return Ok(pending.len());
149 }
150 let (container, store, key) = locate(*self)?;
151 container.blob_len(store, key, column_of::<M>(self.label()))
152 }
153
154 fn read_range(&self, range: Range<usize>) -> Result<Vec<u8>, DbError> {
155 if let Some(pending) = day_reactive::Binding::read(self).pending {
156 return Ok(slice(&pending, range));
157 }
158 let (container, store, key) = locate(*self)?;
159 container.blob_range(store, key, column_of::<M>(self.label()), range)
160 }
161
162 fn write_bytes(&self, bytes: impl Into<Vec<u8>>) {
163 if let Ok((container, store, key)) = locate(*self) {
165 container.blob_forget_field(store, key, column_of::<M>(self.label()));
166 }
167 day_reactive::Binding::write(self, Blob::new(bytes));
168 }
169
170 fn reader(&self) -> BlobReader {
171 let field = *self;
172 BlobReader {
173 read_range: Box::new(move |r| field.read_range(r)),
174 len: Box::new(move || field.len()),
175 pos: 0,
176 end: None,
177 }
178 }
179}
180
181type RangeReadFn = Box<dyn Fn(Range<usize>) -> Result<Vec<u8>, DbError>>;
182type LenFn = Box<dyn Fn() -> Result<usize, DbError>>;
183
184pub struct BlobReader {
186 read_range: RangeReadFn,
187 len: LenFn,
188 pos: usize,
189 end: Option<usize>,
190}
191
192impl BlobReader {
193 pub const CHUNK: usize = 256 * 1024;
195}
196
197impl std::io::Read for BlobReader {
198 fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
199 let end = match self.end {
200 Some(e) => e,
201 None => {
202 let e = (self.len)().map_err(std::io::Error::other)?;
203 self.end = Some(e);
204 e
205 }
206 };
207 if self.pos >= end || buf.is_empty() {
208 return Ok(0);
209 }
210 let want = buf.len().min(Self::CHUNK).min(end - self.pos);
211 let chunk = (self.read_range)(self.pos..self.pos + want).map_err(std::io::Error::other)?;
212 let n = chunk.len().min(buf.len());
213 buf[..n].copy_from_slice(&chunk[..n]);
214 self.pos += n;
215 Ok(n)
216 }
217}
218
219fn slice(bytes: &[u8], range: Range<usize>) -> Vec<u8> {
220 let start = range.start.min(bytes.len());
221 let end = range.end.clamp(start, bytes.len());
222 bytes[start..end].to_vec()
223}
224
225fn column_of<M: Model>(field: &'static str) -> &'static str {
227 M::COLUMNS
228 .iter()
229 .find(|c| c.field == field)
230 .map(|c| c.name)
231 .unwrap_or(field)
232}
233
234fn locate<S, M>(field: day_model::Field<S, M, Blob>) -> Result<(ModelContainer, u64, u64), DbError>
236where
237 S: day_model::Source<M> + Copy + 'static,
238 M: 'static,
239{
240 let mut parts = Vec::new();
241 day_model::Source::components(field, &mut parts);
242 let (Some(&store), Some(&key)) = (parts.first(), parts.get(1)) else {
243 return Err(DbError::driver(
244 "a Blob field can only be read through a row of a container's store",
245 ));
246 };
247 crate::relations::container_for_store(store)
248 .map(|c| (c, store, key))
249 .ok_or_else(|| {
250 DbError::driver("a Blob field was read on a store no open ModelContainer holds")
251 })
252}
253
254#[derive(Default)]
261pub(crate) struct BlobMemo {
262 entries: HashMap<(u64, u64, &'static str), Arc<Vec<u8>>>,
263 order: VecDeque<(u64, u64, &'static str)>,
264 bytes: usize,
265}
266
267impl BlobMemo {
268 pub(crate) fn get(&mut self, k: (u64, u64, &'static str)) -> Option<Arc<Vec<u8>>> {
269 let hit = self.entries.get(&k).cloned()?;
270 if let Some(i) = self.order.iter().position(|e| *e == k) {
272 self.order.remove(i);
273 self.order.push_back(k);
274 }
275 Some(hit)
276 }
277
278 pub(crate) fn insert(&mut self, k: (u64, u64, &'static str), v: Arc<Vec<u8>>, limit: usize) {
279 self.forget_one(k);
280 if v.len() > limit {
281 return;
282 }
283 self.bytes += v.len();
284 self.entries.insert(k, v);
285 self.order.push_back(k);
286 while self.bytes > limit {
287 let Some(old) = self.order.pop_front() else {
288 break;
289 };
290 if let Some(v) = self.entries.remove(&old) {
291 self.bytes -= v.len();
292 }
293 }
294 }
295
296 pub(crate) fn forget_one(&mut self, k: (u64, u64, &'static str)) {
297 if let Some(v) = self.entries.remove(&k) {
298 self.bytes -= v.len();
299 if let Some(i) = self.order.iter().position(|e| *e == k) {
300 self.order.remove(i);
301 }
302 }
303 }
304
305 pub(crate) fn forget_row(&mut self, store: u64, key: u64) {
307 let keys: Vec<_> = self
308 .entries
309 .keys()
310 .filter(|(s, k, _)| *s == store && *k == key)
311 .copied()
312 .collect();
313 for k in keys {
314 self.forget_one(k);
315 }
316 }
317
318 pub(crate) fn forget_store(&mut self, store: u64) {
320 let keys: Vec<_> = self
321 .entries
322 .keys()
323 .filter(|(s, _, _)| *s == store)
324 .copied()
325 .collect();
326 for k in keys {
327 self.forget_one(k);
328 }
329 }
330
331 pub(crate) fn clear(&mut self) {
332 self.entries.clear();
333 self.order.clear();
334 self.bytes = 0;
335 }
336
337 pub(crate) fn bytes(&self) -> usize {
338 self.bytes
339 }
340
341 pub(crate) fn len(&self) -> usize {
342 self.entries.len()
343 }
344}