1use crate::sqlite::StoreSql;
4use crate::store_call::StoreCall;
5use crate::{DbError, OperationId};
6use coven_foundation::id_source::{DeviceId, FileId, ObjectId};
7use serde::{de::DeserializeOwned, Serialize};
8use std::sync::Arc;
9
10#[derive(Clone, Copy, Debug, PartialEq, Eq)]
12pub enum FileWork {
13 Upload,
15 Download,
19}
20
21pub trait PendingValue: Serialize + DeserializeOwned + Send + 'static {
29 fn writes_refused(
31 slot: coven_merge::Slot,
32 cutoff: coven_merge::Slot,
33 cause: crate::WriteCutoff,
34 ) -> Self;
35 fn reload(audience: coven_merge::Audience) -> Self;
37 fn provider_access(operation: OperationId, account: String, grant: bool) -> Self;
39 type Subject: Serialize + Ord;
41 fn subject(&self) -> &Self::Subject;
43 fn operation(&self) -> Option<OperationId>;
45 fn object(&self) -> Option<(FileWork, DeviceId, ObjectId)>;
49 fn move_of(&self) -> Option<crate::MoveId>;
51 fn pin_of(&self) -> Option<FileId>;
54 fn slot(&self) -> Option<coven_merge::Slot>;
56 fn ends_when_covered(&self) -> bool;
60 const BUILD: u128;
64 fn can_retry(&self, recorded_build: u128, schema_version: u32) -> bool;
67 fn cause(&self) -> Option<&str>;
71 fn with_cause(self, cause: Option<String>) -> Self;
73}
74
75#[derive(Clone, Debug, PartialEq, Eq)]
78pub(crate) struct SubjectKey(Vec<u8>);
79
80impl SubjectKey {
81 pub(crate) fn of<S: Serialize>(subject: &S) -> Result<Self, DbError> {
82 serde_json::to_vec(subject).map(Self).map_err(DbError::StateEncoding)
83 }
84}
85
86impl sqlive::rusqlite::ToSql for SubjectKey {
87 fn to_sql(&self) -> sqlive::rusqlite::Result<sqlive::rusqlite::types::ToSqlOutput<'_>> {
88 self.0.to_sql()
89 }
90}
91
92impl sqlive::rusqlite::types::FromSql for SubjectKey {
93 fn column_result(
94 value: sqlive::rusqlite::types::ValueRef<'_>,
95 ) -> sqlive::rusqlite::types::FromSqlResult<Self> {
96 Vec::column_result(value).map(Self)
97 }
98}
99
100const SELECT: &str =
101 "SELECT subject,value,operation,slot_key,work,device,object,build,move,pin,cause,id FROM _coven_pending";
102
103struct StoredRecord {
104 position: i64,
106 subject: SubjectKey,
107 value: Vec<u8>,
108 operation: Option<i64>,
109 slot_key: Option<Vec<u8>>,
110 object: Option<(String, DeviceId, ObjectId)>,
111 build: u128,
113 moved: Option<i64>,
114 pin: Option<String>,
115 cause: Option<String>,
116}
117impl StoredRecord {
118 fn read(row: &sqlive::rusqlite::Row<'_>) -> sqlive::rusqlite::Result<Self> {
119 Ok(Self {
120 subject: row.get(0)?,
121 value: row.get(1)?,
122 operation: row.get(2)?,
123 slot_key: row.get(3)?,
124 object: match (
125 row.get::<_, Option<String>>(4)?,
126 row.get::<_, Option<DeviceId>>(5)?,
127 row.get::<_, Option<ObjectId>>(6)?,
128 ) {
129 (Some(work), Some(device), Some(object)) => Some((work, device, object)),
130 (None, None, None) => None,
131 _ => {
132 return Err(crate::write_encoding::damaged(
133 4,
134 sqlive::rusqlite::types::Type::Text,
135 ))
136 }
137 },
138 build: u128::from_be_bytes(row.get(7)?),
139 moved: row.get(8)?,
140 pin: row.get(9)?,
141 cause: row.get(10)?,
142 position: row.get(11)?,
143 })
144 }
145 fn decode<P: PendingValue>(&self) -> Result<P, DbError> {
146 let record: P = serde_json::from_slice(&self.value).map_err(DbError::StateEncoding)?;
147 if SubjectKey::of(record.subject())? != self.subject
148 || record.operation().map(|id| id.0) != self.operation
149 || record.move_of().map(|id| id.0) != self.moved
150 || record.pin_of().map(|file| file.to_string()) != self.pin
151 || stored_slot_key(&record) != self.slot_key
152 || stored_object(&record)
153 != self
154 .object
155 .as_ref()
156 .map(|(work, device, object)| (work.as_str(), *device, *object))
157 {
158 return Err(DbError::DamagedDatabase);
159 }
160 Ok(record.with_cause(self.cause.clone()))
161 }
162}
163
164pub(crate) fn read<P: PendingValue>(db: &StoreSql) -> Result<Vec<P>, DbError> {
167 Ok(positioned(db)?.into_iter().map(|(_, record)| record).collect())
168}
169
170pub(crate) fn positioned<P: PendingValue>(db: &StoreSql) -> Result<Vec<(i64, P)>, DbError> {
173 db.query(&format!("{SELECT} ORDER BY id"), [], StoredRecord::read)?
174 .iter()
175 .map(|stored| Ok((stored.position, stored.decode::<P>()?)))
176 .collect()
177}
178
179macro_rules! save_record {
183 ($db:expr, $record:expr) => {{
184 let (db, record) = ($db, $record);
185 let subject = SubjectKey::of(record.subject())?;
186 let value = serde_json::to_vec(record).map_err(DbError::StateEncoding)?;
187 let (work, device, object) = match stored_object(record) {
188 Some((work, device, object)) => (Some(work), Some(device), Some(object)),
189 None => (None, None, None),
190 };
191 db.execute(
195 "INSERT INTO _coven_pending(subject,value,operation,slot_key,work,device,object,build,move,pin,cause)
196 SELECT ?1,?2,?3,?4,?5,?6,?7,?8,?9,?10,?11
197 WHERE ?5 IS NOT 'download' OR EXISTS(SELECT 1 FROM _coven_stored_files AS s WHERE s.device=?6 AND s.object=?7)
198 ON CONFLICT(subject) DO UPDATE SET value=excluded.value,operation=excluded.operation,slot_key=excluded.slot_key,work=excluded.work,device=excluded.device,object=excluded.object,build=excluded.build,move=excluded.move,pin=excluded.pin,cause=excluded.cause
199 WHERE value!=excluded.value OR build!=excluded.build OR cause IS NOT excluded.cause",
200 sqlive::rusqlite::params![subject, value, record.operation().map(|id| id.0), stored_slot_key(record), work, device, object, P::BUILD.to_be_bytes(), record.move_of(), record.pin_of(), record.cause()],
201 )?;
202 Ok::<(), DbError>(())
203 }};
204}
205
206pub(crate) fn save<P: PendingValue>(db: &StoreSql, record: &P) -> Result<(), DbError> {
207 pending_change(db, || save_record!(db, record))
208}
209
210#[cfg(any(test, feature = "test-utils"))]
213pub fn save_outside_store<P: PendingValue>(
214 connection: &sqlive::rusqlite::Connection,
215 record: &P,
216) -> Result<(), DbError> {
217 save_record!(connection, record)
218}
219
220pub(crate) fn forget_causes(db: &StoreSql) -> Result<(), DbError> {
224 db.execute("UPDATE _coven_pending SET cause=NULL WHERE cause IS NOT NULL", [])?;
225 Ok(())
226}
227
228pub(crate) fn clear<S: Serialize>(db: &StoreSql, subject: &S) -> Result<(), DbError> {
229 clear_key(db, &SubjectKey::of(subject)?)
230}
231
232pub(crate) fn clear_key(db: &StoreSql, subject: &SubjectKey) -> Result<(), DbError> {
234 pending_change(db, || {
235 db.execute("DELETE FROM _coven_pending WHERE subject=?1", [subject])?;
236 Ok(())
237 })
238}
239
240pub(crate) fn clear_move(db: &StoreSql, id: crate::MoveId) -> Result<(), DbError> {
242 pending_change(db, || {
243 db.execute("DELETE FROM _coven_pending WHERE move=?1", [id])?;
244 Ok(())
245 })
246}
247
248pub(crate) fn clear_operation(db: &StoreSql, id: OperationId) -> Result<(), DbError> {
251 crate::retry::clear(db, crate::RetrySubject::Operation(id))?;
252 pending_change(db, || {
253 db.execute(CLEAR_OPERATION, [id.0])?;
254 Ok(())
255 })
256}
257
258const CLEAR_OPERATION: &str = "DELETE FROM _coven_pending WHERE operation=?1";
260
261#[cfg(any(test, feature = "test-utils"))]
266pub(crate) fn clear_operation_outside_store(
267 connection: &sqlive::rusqlite::Connection,
268 id: OperationId,
269) -> Result<(), DbError> {
270 connection.execute(CLEAR_OPERATION, [id.0])?;
271 Ok(())
272}
273
274pub(crate) fn operation_recorded(db: &StoreSql, id: OperationId) -> Result<bool, DbError> {
276 Ok(db.query_row(
277 "SELECT EXISTS(SELECT 1 FROM _coven_pending WHERE operation=?1)",
278 [id.0],
279 |r| r.get(0),
280 )?)
281}
282
283pub(crate) fn operation<P: PendingValue>(
284 db: &StoreSql,
285 id: OperationId,
286) -> Result<Option<P>, DbError> {
287 db.query(&format!("{SELECT} WHERE operation=?1"), [id.0], StoredRecord::read)?
288 .pop()
289 .map(|r| r.decode())
290 .transpose()
291}
292
293pub(crate) fn recorded<S: Serialize>(db: &StoreSql, subject: &S) -> Result<bool, DbError> {
295 Ok(db.query_row(
296 "SELECT EXISTS(SELECT 1 FROM _coven_pending WHERE subject=?1)",
297 [SubjectKey::of(subject)?],
298 |r| r.get(0),
299 )?)
300}
301
302fn stored<S: Serialize>(db: &StoreSql, subject: &S) -> Result<Option<StoredRecord>, DbError> {
304 Ok(db
305 .query(
306 &format!("{SELECT} WHERE subject=?1"),
307 [SubjectKey::of(subject)?],
308 StoredRecord::read,
309 )?
310 .pop())
311}
312
313pub(crate) fn find<P: PendingValue>(
315 db: &StoreSql,
316 subject: &P::Subject,
317) -> Result<Option<P>, DbError> {
318 stored(db, subject)?.map(|stored| stored.decode()).transpose()
319}
320
321pub(crate) fn retry<P: PendingValue>(
322 db: &StoreSql,
323 call: &StoreCall<'_>,
324 subject: &P::Subject,
325) -> Result<bool, DbError> {
326 match stored(db, subject)? {
327 None => Ok(true),
328 Some(stored) => Ok(stored
329 .decode::<P>()?
330 .can_retry(stored.build, crate::transaction_constants::schema_version(db, call)?)),
331 }
332}
333
334#[cfg(any(test, feature = "test-utils"))]
337pub(crate) fn set_build(db: &StoreSql, build: u128) -> Result<(), DbError> {
338 pending_change(db, || {
339 db.execute("UPDATE _coven_pending SET build=?1", [build.to_be_bytes()])?;
340 Ok(())
341 })
342}
343
344fn slot_key(slot: coven_merge::Slot, covered: bool) -> Vec<u8> {
345 let mut key = vec![u8::from(covered)];
346 key.extend(slot.get().to_be_bytes());
347 key
348}
349const COVERED_SLOT: u8 = 1;
350
351fn stored_object<P: PendingValue>(record: &P) -> Option<(&'static str, DeviceId, ObjectId)> {
353 record.object().map(|(work, device, object)| {
354 let work = match work {
355 FileWork::Upload => "upload",
356 FileWork::Download => "download",
357 };
358 (work, device, object)
359 })
360}
361
362fn stored_slot_key<P: PendingValue>(record: &P) -> Option<Vec<u8>> {
363 Some(slot_key(record.slot()?, record.ends_when_covered()))
364}
365
366pub(crate) fn reset(db: &StoreSql) -> Result<(), DbError> {
368 pending_change(db, || {
369 db.execute("DELETE FROM _coven_pending AS p WHERE NOT EXISTS(SELECT 1 FROM _coven_provider_access a WHERE a.operation=p.operation)",[])?;
370 Ok(())
371 })
372}
373
374pub(crate) fn clear_slot(db: &StoreSql, slot: coven_merge::Slot) -> Result<(), DbError> {
376 pending_change(db, || {
377 for covered in [false, true] {
378 db.execute("DELETE FROM _coven_pending WHERE slot_key=?1", [slot_key(slot, covered)])?;
379 }
380 Ok(())
381 })
382}
383
384pub(crate) fn clear_covered_slots(db: &StoreSql) -> Result<(), DbError> {
387 let waiting = db.query(
388 "SELECT slot_key FROM _coven_pending WHERE slot_key>=?1 AND slot_key<?2",
389 ([COVERED_SLOT].as_slice(), [COVERED_SLOT + 1].as_slice()),
390 |row| row.get::<_, Vec<u8>>(0),
391 )?;
392 if waiting.is_empty() {
393 return Ok(());
394 }
395 let applied = crate::sequence::applied(db)?;
396 pending_change(db, || {
397 for key in waiting {
398 let slot = u64::from_be_bytes(key[1..].try_into().expect("eight-byte slot"));
399 if slot <= applied {
400 db.execute("DELETE FROM _coven_pending WHERE slot_key=?1", [key])?;
401 }
402 }
403 Ok(())
404 })
405}
406
407pub(crate) fn pending_change<T>(
408 sql: &StoreSql,
409 run: impl FnOnce() -> Result<T, DbError>,
410) -> Result<T, DbError> {
411 run().map_err(|error| pending_failure(sql, error))
412}
413
414pub(crate) fn state_transaction<T>(
417 sql: &mut StoreSql,
418 run: impl FnOnce(&StoreSql) -> Result<T, DbError>,
419) -> Result<T, DbError> {
420 let result = sql.transaction(run);
421 result.map_err(|error| pending_failure(sql, error))
422}
423
424fn pending_failure(sql: &StoreSql, error: DbError) -> DbError {
427 if matches!(error, DbError::StatePersistence(_)) {
428 return error;
429 }
430 let error = Arc::new(error);
431 sql.fail_handle(crate::store_failure::StoreFailure::State(error.clone()));
432 DbError::StatePersistence(error)
433}
434
435#[cfg(test)]
436#[path = "pending_tests.rs"]
437pub(crate) mod tests;