1//! The section thread: the only owner of the parsed section, which is the cached base
2//! image with each sealed batch replayed and the open batch's edits applied. Edits apply
3//! here and are written in one SQLite transaction per burst; the sync thread asks it to seal
4//! and, when the remote changed, to rebase the queue. In the browser, which has one thread,
5//! the section is served as each request is sent, and a rebuild runs as it is asked for.
6
7use crate::{Moving, Result, base, lock, merge, queue, worker::Signal};
8use onestore::{
9 Arena, ExGuid, Section, Transaction,
10 op::{Edit, OpError},
11 page::Page,
12 protected::Key,
13};
14use rusqlite::{Connection, OptionalExtension, TransactionBehavior, params};
15use std::{
16 collections::{BTreeMap, BTreeSet, VecDeque},
17 io,
18 path::{Path, PathBuf},
19 sync::{
20 Arc, Mutex, Weak,
21 atomic::{AtomicUsize, Ordering},
22 mpsc,
23 },
24};
25
26pub(crate) type Reply<T> = Box<dyn FnOnce(Result<T>) + Send>;
27pub(crate) type Notice = Box<dyn Fn(&str) + Send>;
28type Changed = Box<dyn Fn(Vec<ExGuid>) + Send>;
29
30pub(crate) enum Request {
31 Apply(Accepted),
32 Page {
33 space: ExGuid,
34 reply: Reply<Page>,
35 },
36 Pages {
37 reply: Reply<Vec<(ExGuid, String, u32)>>,
38 },
39 Identities {
40 reply: Reply<Vec<(ExGuid, Option<[u8; 16]>)>>,
41 },
42 Conflicts {
43 reply: Reply<Vec<(ExGuid, Vec<onestore::ConflictPage>)>>,
44 },
45 Versions {
46 reply: Reply<Vec<(ExGuid, Vec<onestore::PageVersion>)>>,
47 },
48 Version {
49 space: ExGuid,
50 version: ExGuid,
51 reply: Reply<Page>,
52 },
53 Moving {
54 space: ExGuid,
55 reply: Reply<Moving>,
56 },
57 /// Answers once the edits before it are written, optionally awaiting host durability.
58 Flush {
59 reply: Reply<()>,
60 durable: bool,
61 },
62 /// Seals the open batch, when no sealed batch waits for publication, recording the
63 /// attempt to publish it.
64 Seal {
65 reply: Reply<Option<Sealed>>,
66 },
67 /// Replays the queue on `image` (the stored remote image, or the base, when `None`);
68 /// answers with the pages the remote changed.
69 Rebase {
70 image: Option<Vec<u8>>,
71 reply: Reply<Vec<ExGuid>>,
72 },
73 /// Rereads the queue after the sync thread replaced it.
74 Reopen {
75 reply: Reply<()>,
76 },
77 /// From the thread that rebuilt the section: hand it the requests.
78 #[cfg_attr(target_arch = "wasm32", allow(dead_code))]
79 Handover(mpsc::SyncSender<Takeover>),
80 /// From the thread that rebuilt nothing: carry on with the section as it is.
81 #[cfg_attr(target_arch = "wasm32", allow(dead_code))]
82 Resume,
83}
84
85/// The request channel and the requests held while the section was rebuilt.
86type Takeover = (mpsc::Receiver<Request>, VecDeque<Request>);
87
88/// A sealed batch: the transaction publishing it, or none when its edits changed nothing.
89pub(crate) struct Sealed {
90 pub batch: i64,
91 pub transaction: Option<Transaction>,
92}
93
94/// The cache's connection, or what a failed commit leaves: the cache closed until it reopens
95/// and reads cold whether the commit whose last edit is `last` landed, `ids` naming its edits
96/// still waiting and `queued` the edits queued before it; or lost where it never will reopen.
97pub(crate) enum Cache {
98 Open(Connection),
99 Closed {
100 last: u64,
101 ids: Vec<u64>,
102 queued: u64,
103 owner: Owner,
104 },
105 Lost(String),
106}
107
108/// What keeps everyone else out of a closed cache: the warm connection, unused, until
109/// `crate::Owned` can be taken in its stead.
110pub(crate) enum Owner {
111 Warm(#[allow(dead_code)] Connection),
112 Owned(#[allow(dead_code)] crate::Owned),
113}
114
115/// Whether a failed commit keeps the cache closed.
116fn closed(cache: &Mutex<Cache>) -> bool {
117 cache
118 .lock()
119 .is_ok_and(|cache| matches!(*cache, Cache::Closed { .. }))
120}
121
122impl Request {
123 fn fail(self, kind: io::ErrorKind, message: &str) {
124 let error = || io::Error::new(kind, message.to_owned()).into();
125 match self {
126 Self::Apply(Accepted { reply, .. }) => reply(Err(error())),
127 Self::Page { reply, .. } => reply(Err(error())),
128 Self::Pages { reply } => reply(Err(error())),
129 Self::Identities { reply } => reply(Err(error())),
130 Self::Conflicts { reply } => reply(Err(error())),
131 Self::Versions { reply } => reply(Err(error())),
132 Self::Version { reply, .. } => reply(Err(error())),
133 Self::Moving { reply, .. } => reply(Err(error())),
134 Self::Flush { reply, .. } => reply(Err(error())),
135 Self::Seal { reply } => reply(Err(error())),
136 Self::Rebase { reply, .. } => reply(Err(error())),
137 Self::Reopen { reply } => reply(Err(error())),
138 Self::Handover(_) | Self::Resume => {}
139 }
140 }
141}
142
143/// The section thread as the replica sees it. Rereading the section after the queue was
144/// replaced (a rebase, a released attempt) happens on a new thread while the current one
145/// keeps answering page reads from the section as it was; the new thread then takes the
146/// requests over, so reads never wait for a rebuild.
147pub(crate) struct Thread {
148 pub(crate) cache: Mutex<Cache>,
149 /// Where the cache reopens after a failed commit; none in the browser, where reopening
150 /// through its file system is unverified.
151 path: Option<PathBuf>,
152 /// The sync worker, which a durable burst of edits wakes.
153 pub(crate) worker: Mutex<Weak<Signal>>,
154 /// Taken when the replica drops, which ends every section thread.
155 sender: Mutex<Option<mpsc::Sender<Request>>>,
156 #[cfg(target_arch = "wasm32")]
157 durable: Mutex<Option<mpsc::Sender<Receipt>>>,
158 #[cfg(target_arch = "wasm32")]
159 durable_wake: crate::task::Sender<()>,
160 #[cfg(not(target_arch = "wasm32"))]
161 threads: Mutex<Vec<std::thread::JoinHandle<()>>>,
162 /// A password-protected section's key, under which its queue is sealed too.
163 pub(crate) key: Option<Key>,
164 /// The edits the cache last refused for now, as `crate::unwritten` counts them.
165 pub(crate) waiting: AtomicUsize,
166 /// Told the pages a reread after a refusal may change, which their views reload.
167 pub(crate) reread: Mutex<Option<Changed>>,
168 /// Last, so the cache closes before background synchronization takes it over.
169 pub(crate) released: crate::Released,
170}
171
172impl Drop for Thread {
173 fn drop(&mut self) {
174 self.wait(0);
175 }
176}
177
178impl Thread {
179 fn wait(&self, edits: usize) {
180 let was = self.waiting.swap(edits, Ordering::Relaxed);
181 crate::UNWRITTEN.fetch_add(edits, Ordering::Relaxed);
182 crate::UNWRITTEN.fetch_sub(was, Ordering::Relaxed);
183 }
184
185 pub(crate) fn send(&self, request: Request) -> Result<()> {
186 self.sender
187 .lock()
188 .ok()
189 .and_then(|sender| sender.as_ref()?.send(request).ok())
190 .ok_or_else(|| io::Error::other("The section thread stopped"))?;
191 #[cfg(target_arch = "wasm32")]
192 serving::serve(self);
193 Ok(())
194 }
195
196 /// Ends request serving; pending browser receipts retain the cache until they settle.
197 pub(crate) fn stop(&self) {
198 if let Ok(mut sender) = self.sender.lock() {
199 sender.take();
200 }
201 #[cfg(target_arch = "wasm32")]
202 {
203 serving::serve(self);
204 if let Ok(mut durable) = self.durable.lock() {
205 durable.take();
206 }
207 let _ = self.durable_wake.try_send(());
208 }
209 #[cfg(not(target_arch = "wasm32"))]
210 while let Some(thread) = self
211 .threads
212 .lock()
213 .ok()
214 .and_then(|mut threads| threads.pop())
215 {
216 let _ = thread.join();
217 }
218 }
219
220 #[cfg(not(target_arch = "wasm32"))]
221 fn start<F: Future<Output = ()>>(
222 self: &Arc<Self>,
223 run: impl FnOnce(Arc<Self>) -> F + Send + 'static,
224 ) -> Result<()> {
225 let shared = Arc::clone(self);
226 let thread = std::thread::Builder::new()
227 .name("onestore-section".into())
228 .spawn(move || crate::task::complete(run(shared)))?;
229 self.threads
230 .lock()
231 .map_err(|_| io::Error::other("The section thread panicked"))?
232 .push(thread);
233 Ok(())
234 }
235
236 #[cfg(target_arch = "wasm32")]
237 fn start<F: Future<Output = ()> + 'static>(
238 self: &Arc<Self>,
239 run: impl FnOnce(Arc<Self>) -> F,
240 ) -> Result<()> {
241 serving::start(self, Box::pin(run(Arc::clone(self))));
242 Ok(())
243 }
244
245 #[cfg(target_arch = "wasm32")]
246 fn receipt(self: &Arc<Self>, replies: Vec<Reply<()>>, edited: bool) {
247 let receipt = Receipt {
248 shared: Arc::clone(self),
249 replies,
250 edited,
251 };
252 let sent = match self.durable.lock() {
253 Ok(sender) => match sender.as_ref() {
254 Some(sender) => sender.send(receipt).map_err(|error| error.0),
255 None => Err(receipt),
256 },
257 Err(_) => Err(receipt),
258 };
259 if let Err(receipt) = sent {
260 for reply in receipt.replies {
261 reply(Err(io::Error::other("The section thread stopped").into()));
262 }
263 }
264 let _ = self.durable_wake.try_send(());
265 }
266}
267
268#[cfg(target_arch = "wasm32")]
269struct Receipt {
270 shared: Arc<Thread>,
271 replies: Vec<Reply<()>>,
272 edited: bool,
273}
274
275#[cfg(target_arch = "wasm32")]
276async fn receipts(requests: mpsc::Receiver<Receipt>, wake: crate::task::Receiver<()>) {
277 loop {
278 let Receipt {
279 shared,
280 replies,
281 edited,
282 } = match requests.try_recv() {
283 Ok(receipt) => receipt,
284 Err(mpsc::TryRecvError::Empty) => {
285 crate::task::wait(&wake, None).await;
286 continue;
287 }
288 Err(mpsc::TryRecvError::Disconnected) => return,
289 };
290 let result = loop {
291 match crate::fs::durable().await {
292 Err(error) if error.kind() == io::ErrorKind::WouldBlock => {
293 crate::task::wait(&wake, Some(std::time::Duration::from_secs(1))).await;
294 }
295 result => break result,
296 }
297 };
298 match result {
299 Ok(()) => {
300 for reply in replies {
301 reply(Ok(()));
302 }
303 if edited {
304 crate::edited(&shared.worker);
305 }
306 }
307 Err(error) => {
308 let message = error.to_string();
309 for reply in replies {
310 reply(Err(io::Error::new(error.kind(), message.clone()).into()));
311 }
312 }
313 }
314 }
315}
316
317/// The browser's section threads: each replica's `run`, polled whenever a request is sent
318/// until it waits for the next.
319#[cfg(target_arch = "wasm32")]
320mod serving {
321 use super::Thread;
322 use std::{cell::RefCell, collections::BTreeMap, future::Future, pin::Pin, task};
323
324 type Serving = Pin<Box<dyn Future<Output = ()>>>;
325
326 thread_local! {
327 /// By the address of each replica's `Thread`, which its `run` keeps alive.
328 static SERVING: RefCell<BTreeMap<usize, Serving>> = const { RefCell::new(BTreeMap::new()) };
329 }
330
331 pub(super) fn start(thread: &Thread, run: Serving) {
332 SERVING.with_borrow_mut(|serving| serving.insert(std::ptr::from_ref(thread).addr(), run));
333 serve(thread);
334 }
335
336 /// Serves what `thread` was sent. A request sent while it is being served, as from a
337 /// reply, waits for the serving already under way, which takes it next.
338 pub(super) fn serve(thread: &Thread) {
339 let id = std::ptr::from_ref(thread).addr();
340 let Some(mut run) = SERVING.with_borrow_mut(|serving| serving.remove(&id)) else {
341 return;
342 };
343 let mut context = task::Context::from_waker(task::Waker::noop());
344 if run.as_mut().poll(&mut context).is_pending() {
345 SERVING.with_borrow_mut(|serving| serving.insert(id, run));
346 }
347 }
348}
349
350/// How often the cache is tried again while edits wait for it or it is closed.
351const RETRY: std::time::Duration = std::time::Duration::from_secs(1);
352
353/// The next request, or a timeout once `retry` passes without one.
354#[cfg(not(target_arch = "wasm32"))]
355async fn receive(
356 requests: &mpsc::Receiver<Request>,
357 retry: Option<std::time::Duration>,
358) -> std::result::Result<Request, mpsc::RecvTimeoutError> {
359 match retry {
360 Some(wait) => requests.recv_timeout(wait),
361 None => requests
362 .recv()
363 .map_err(|_| mpsc::RecvTimeoutError::Disconnected),
364 }
365}
366
367/// The next request, waiting for `serving::serve` where none is queued; requests alone
368/// retry unwritten edits.
369#[cfg(target_arch = "wasm32")]
370async fn receive(
371 requests: &mpsc::Receiver<Request>,
372 _retry: Option<std::time::Duration>,
373) -> std::result::Result<Request, mpsc::RecvTimeoutError> {
374 std::future::poll_fn(|_| match requests.try_recv() {
375 Ok(request) => std::task::Poll::Ready(Ok(request)),
376 Err(mpsc::TryRecvError::Disconnected) => {
377 std::task::Poll::Ready(Err(mpsc::RecvTimeoutError::Disconnected))
378 }
379 Err(mpsc::TryRecvError::Empty) => std::task::Poll::Pending,
380 })
381 .await
382}
383
384/// Starts the section thread once the queue opens.
385pub(crate) fn spawn(connection: Connection, key: Option<Key>) -> Result<(Arc<Thread>, ExGuid)> {
386 let (sender, requests) = mpsc::channel();
387 #[cfg(target_arch = "wasm32")]
388 let (durable, written) = mpsc::channel();
389 #[cfg(target_arch = "wasm32")]
390 let (durable_wake, woken) = crate::task::channel();
391 let shared = Arc::new(Thread {
392 path: (connection.path())
393 .filter(|_| cfg!(not(target_arch = "wasm32")))
394 .map(PathBuf::from),
395 cache: Mutex::new(Cache::Open(connection)),
396 worker: Mutex::new(Weak::new()),
397 sender: Mutex::new(Some(sender)),
398 #[cfg(target_arch = "wasm32")]
399 durable: Mutex::new(Some(durable)),
400 #[cfg(target_arch = "wasm32")]
401 durable_wake,
402 #[cfg(not(target_arch = "wasm32"))]
403 threads: Mutex::new(Vec::new()),
404 key,
405 waiting: AtomicUsize::new(0),
406 reread: Mutex::new(None),
407 released: crate::Released::default(),
408 });
409 #[cfg(target_arch = "wasm32")]
410 crate::task::spawn("onestore-durable", move || receipts(written, woken))?;
411 let (ready, opened) = mpsc::sync_channel(1);
412 shared.start(move |shared| run(shared, requests, VecDeque::new(), Some(ready)))?;
413 match opened.recv() {
414 Ok(Ok(root)) => Ok((shared, root)),
415 Ok(Err(error)) => {
416 shared.stop();
417 Err(error)
418 }
419 Err(_) => {
420 shared.stop();
421 Err(io::Error::other("The section thread panicked").into())
422 }
423 }
424}
425
426enum Next {
427 Stop,
428 Reopen,
429 Handover(mpsc::SyncSender<Takeover>, VecDeque<Request>),
430}
431
432async fn run(
433 shared: Arc<Thread>,
434 requests: mpsc::Receiver<Request>,
435 mut backlog: VecDeque<Request>,
436 mut ready: Option<mpsc::SyncSender<Result<ExGuid>>>,
437) {
438 loop {
439 let arena = Arena::default();
440 let working = match Working::open(&arena, &shared.cache, shared.key.as_ref()) {
441 Ok(working) => working,
442 Err(error) => {
443 if let Some(ready) = ready.take() {
444 let _ = ready.send(Err(error));
445 } else {
446 fail(&shared, error, backlog, requests);
447 }
448 return;
449 }
450 };
451 if let Some(ready) = ready.take() {
452 let _ = ready.send(Ok(working.section.root()));
453 }
454 match working.serve(&shared, &requests, &mut backlog).await {
455 Next::Stop => return,
456 Next::Reopen => {}
457 Next::Handover(to, held) => {
458 let _ = to.send((requests, held));
459 return;
460 }
461 }
462 }
463}
464
465/// Answers every request with `error` until the replica drops.
466fn fail(
467 shared: &Thread,
468 error: crate::Error,
469 backlog: VecDeque<Request>,
470 requests: mpsc::Receiver<Request>,
471) {
472 // The edits that waited are in the backlog, refused with the rest.
473 shared.wait(0);
474 let message = error.to_string();
475 for request in backlog.into_iter().chain(requests.iter()) {
476 request.fail(io::ErrorKind::Other, &message);
477 }
478}
479
480/// What a rebuilding thread does before it rereads the section.
481#[cfg_attr(target_arch = "wasm32", allow(dead_code))]
482enum Job {
483 Rebase {
484 image: Option<Vec<u8>>,
485 reply: Reply<Vec<ExGuid>>,
486 },
487 Reopen {
488 reply: Reply<()>,
489 },
490}
491
492/// Runs `job`, rereads the section and takes the requests over from the thread that
493/// started it; a failed rebase changed nothing that thread serves.
494#[cfg(not(target_arch = "wasm32"))]
495fn build(shared: Arc<Thread>, job: Job, signal: mpsc::Sender<Request>) {
496 let answer: Box<dyn FnOnce() + Send> = match job {
497 Job::Reopen { reply } => Box::new(move || reply(Ok(()))),
498 Job::Rebase { image, reply } => match rebase(&shared.cache, image, shared.key.as_ref()) {
499 Ok(changed) => Box::new(move || reply(Ok(changed))),
500 Err(error) => {
501 reply(Err(error));
502 let _ = signal.send(Request::Resume);
503 return;
504 }
505 },
506 };
507 let arena = Arena::default();
508 let opened = Working::open(&arena, &shared.cache, shared.key.as_ref());
509 let (to, from) = mpsc::sync_channel(1);
510 if signal.send(Request::Handover(to)).is_err() {
511 return;
512 }
513 drop(signal);
514 answer();
515 let Ok((requests, mut backlog)) = from.recv() else {
516 return;
517 };
518 let working = match opened {
519 Ok(working) => working,
520 Err(error) => return fail(&shared, error, backlog, requests),
521 };
522 match crate::task::complete(working.serve(&shared, &requests, &mut backlog)) {
523 Next::Stop => {}
524 Next::Reopen => crate::task::complete(run(shared, requests, backlog, None)),
525 Next::Handover(to, held) => {
526 let _ = to.send((requests, held));
527 }
528 }
529}
530
531/// `build` where there is one thread: runs `job` before the section is reread, which the
532/// serving thread does once true is returned; a failed rebase changed nothing it serves.
533#[cfg(target_arch = "wasm32")]
534fn inline(shared: &Thread, job: Job) -> bool {
535 match job {
536 Job::Reopen { reply } => reply(Ok(())),
537 Job::Rebase { image, reply } => match rebase(&shared.cache, image, shared.key.as_ref()) {
538 Ok(changed) => reply(Ok(changed)),
539 Err(error) => {
540 reply(Err(error));
541 return false;
542 }
543 },
544 }
545 true
546}
547
548/// Writes again each second the edits a dropped replica left waiting, until none do and the
549/// cache is open, or the cache is gone; the cache stays owned and `Released` unfired meanwhile.
550/// With `reread`, the closed commit is settled first and the edits applied again to the queue
551/// as it then stands, refusing those that no longer apply, as the section thread would have.
552#[cfg(not(target_arch = "wasm32"))]
553fn orphan(shared: &Arc<Thread>, mut open: Option<i64>, mut unwritten: Vec<Accepted>, reread: bool) {
554 let shared = Arc::clone(shared);
555 let spawned = std::thread::Builder::new()
556 .name("onestore-unwritten".into())
557 .spawn(move || {
558 // A cache deleted, as with its notebook, takes no edits.
559 let kept = || shared.path.as_ref().is_some_and(|path| path.exists());
560 let second = || std::thread::sleep(std::time::Duration::from_secs(1));
561 while closed(&shared.cache) && kept() {
562 second();
563 let _ = write(&shared, &mut open, &mut unwritten, !reread);
564 }
565 // An edit that applies part way is refused, and the others apply again to a fresh
566 // reading of the queue.
567 if reread {
568 'reread: loop {
569 let arena = Arena::default();
570 let mut working =
571 match Working::open(&arena, &shared.cache, shared.key.as_ref()) {
572 Ok(working) => working,
573 Err(error) => {
574 for edit in unwritten.drain(..) {
575 (edit.reply)(Err(io::Error::other(error.to_string()).into()));
576 }
577 break;
578 }
579 };
580 let mut edits = std::mem::take(&mut unwritten).into_iter();
581 while let Some(edit) = edits.next() {
582 if working.accept(edit) {
583 unwritten = std::mem::take(&mut working.unwritten);
584 unwritten.extend(edits);
585 continue 'reread;
586 }
587 }
588 open = working.open;
589 unwritten = std::mem::take(&mut working.unwritten);
590 break;
591 }
592 }
593 while !unwritten.is_empty() && kept() {
594 second();
595 let _ = write(&shared, &mut open, &mut unwritten, true);
596 }
597 });
598 if let Err(error) = spawned {
599 eprintln!("Edits waiting for the cache are lost: {error}");
600 }
601}
602
603/// An edit to apply, with who waits for it.
604pub(crate) struct Accepted {
605 pub author: String,
606 pub edit: Edit,
607 pub reply: Reply<u64>,
608 /// Set where nobody blocks on the reply: a failed write keeps the edit and reports here.
609 pub waiting: Option<Notice>,
610 /// A page as a move copied it, which the edit applies only while it still holds.
611 pub moving: Option<Box<Moving>>,
612}
613
614struct Working<'a> {
615 section: Section<'a>,
616 /// The batch collecting edits; sealed batches before it are replayed.
617 open: Option<i64>,
618 /// Spaces the open batch's edits change.
619 touched: BTreeSet<ExGuid>,
620 /// Applied to the section and not yet in the queue, so the section is the queue replayed
621 /// with these after it; written in order before anything seals, rebases or rereads.
622 unwritten: Vec<Accepted>,
623}
624
625impl<'a> Working<'a> {
626 fn open(arena: &'a Arena, connection: &Mutex<Cache>, key: Option<&Key>) -> Result<Self> {
627 let (section, open, touched) = replay(arena, &*lock(connection)?, key)?;
628 Ok(Self {
629 section,
630 open,
631 touched,
632 unwritten: Vec::new(),
633 })
634 }
635
636 async fn serve(
637 mut self,
638 shared: &Arc<Thread>,
639 requests: &mpsc::Receiver<Request>,
640 backlog: &mut VecDeque<Request>,
641 ) -> Next {
642 let connection = &shared.cache;
643 // While another thread rebuilds the section, reads answer from this one and every
644 // other request waits for the rebuilt section.
645 let mut held: Option<VecDeque<Request>> = None;
646 // While a reread waits for the closed cache, reads answer from this section and edits
647 // wait for the reread, as one this section refused may underlie them.
648 let mut stale: Option<VecDeque<Request>> = None;
649 let mut attempted = web_time::Instant::now();
650 loop {
651 let mut burst = std::mem::take(backlog);
652 // Only a burst that took an edit, or a retry falling due, writes at its end: reads
653 // never.
654 let mut write = false;
655 let retry = !self.unwritten.is_empty() || closed(connection);
656 if burst.is_empty() {
657 let wait = retry.then(|| RETRY.saturating_sub(attempted.elapsed()));
658 match receive(requests, wait).await {
659 Ok(request) => burst.push_back(request),
660 Err(mpsc::RecvTimeoutError::Timeout) => {}
661 Err(mpsc::RecvTimeoutError::Disconnected) => {
662 let stale_section = stale.is_some();
663 // A refusal now leaves the section holding an edit the queue lacks too.
664 #[cfg_attr(target_arch = "wasm32", allow(unused_variables))]
665 let reread = crate::working::write(
666 shared,
667 &mut self.open,
668 &mut self.unwritten,
669 !stale_section,
670 )
671 .is_err()
672 || stale_section;
673 for request in stale.into_iter().flatten() {
674 match request {
675 Request::Apply(accepted) => self.unwritten.push(accepted),
676 request => {
677 request.fail(io::ErrorKind::Other, "The section thread stopped")
678 }
679 }
680 }
681 #[cfg(not(target_arch = "wasm32"))]
682 if !self.unwritten.is_empty() || closed(connection) {
683 orphan(
684 shared,
685 self.open,
686 std::mem::take(&mut self.unwritten),
687 reread,
688 );
689 }
690 return Next::Stop;
691 }
692 }
693 }
694 burst.extend(requests.try_iter());
695 // Requests that write the queue beyond the burst's edits run after its reads.
696 let mut later = VecDeque::new();
697 let mut reopen = false;
698 while !reopen && let Some(request) = burst.pop_front().or_else(|| later.pop_front()) {
699 let Some(request) = self.answer(request) else {
700 continue;
701 };
702 if let Some(waiting) = &mut held {
703 match request {
704 Request::Handover(to) => {
705 let mut waiting = held.take().unwrap_or_default();
706 waiting.extend(burst.drain(..).chain(later.drain(..)));
707 return Next::Handover(to, waiting);
708 }
709 Request::Resume => {
710 let waiting = held.take().unwrap_or_default();
711 burst = waiting.into_iter().chain(burst.drain(..)).collect();
712 }
713 other => waiting.push_back(other),
714 }
715 continue;
716 }
717 if let Some(waiting) = &mut stale {
718 match request {
719 Request::Apply(
720 accepted @ Accepted {
721 waiting: Some(_), ..
722 },
723 ) => {
724 if let Some(notice) = &accepted.waiting {
725 notice("The cache reopens once it can");
726 }
727 waiting.push_back(Request::Apply(accepted));
728 }
729 request @ Request::Reopen { .. } => waiting.push_back(request),
730 request => {
731 request.fail(io::ErrorKind::WouldBlock, "The cache reopens once it can")
732 }
733 }
734 continue;
735 }
736 if matches!(request, Request::Flush { .. } | Request::Seal { .. })
737 && !burst.is_empty()
738 {
739 later.push_back(request);
740 continue;
741 }
742 if matches!(
743 request,
744 Request::Flush { .. } | Request::Seal { .. } | Request::Rebase { .. }
745 ) {
746 let failed = self.flush(shared).is_err();
747 if failed || !self.unwritten.is_empty() || closed(connection) {
748 // The worker retries later; the edits wait here meanwhile.
749 request.fail(io::ErrorKind::WouldBlock, "Edits wait to be written");
750 reopen = failed;
751 continue;
752 }
753 }
754 reopen = match request {
755 Request::Apply(accepted) => {
756 // While the cache is closed, an edit nobody waits for is refused before
757 // it applies, instead of showing until a reread can drop it.
758 if accepted.waiting.is_none()
759 && let Err(error) = lock(connection).map(drop)
760 {
761 (accepted.reply)(Err(error));
762 false
763 } else {
764 write = true;
765 self.accept(accepted)
766 }
767 }
768 Request::Flush { reply, durable } => {
769 #[cfg(target_arch = "wasm32")]
770 if durable {
771 shared.receipt(vec![reply], false);
772 } else {
773 reply(Ok(()));
774 }
775 #[cfg(not(target_arch = "wasm32"))]
776 {
777 let _ = durable;
778 reply(Ok(()));
779 }
780 false
781 }
782 Request::Seal { reply } => {
783 let sealed = self.seal(connection);
784 let failed = sealed.is_err();
785 reply(sealed);
786 failed
787 }
788 Request::Rebase { image, reply } => {
789 #[cfg(target_arch = "wasm32")]
790 let reopen = inline(shared, Job::Rebase { image, reply });
791 #[cfg(not(target_arch = "wasm32"))]
792 let reopen = {
793 held = self
794 .rebuild(shared, Job::Rebase { image, reply })
795 .then(VecDeque::new);
796 false
797 };
798 reopen
799 }
800 Request::Reopen { reply } => {
801 let _ = self.flush(shared);
802 if closed(connection) {
803 stale
804 .get_or_insert_default()
805 .push_back(Request::Reopen { reply });
806 continue;
807 }
808 #[cfg(target_arch = "wasm32")]
809 let reopen = inline(shared, Job::Reopen { reply });
810 #[cfg(not(target_arch = "wasm32"))]
811 let reopen = {
812 held = self
813 .rebuild(shared, Job::Reopen { reply })
814 .then(|| self.unwritten.drain(..).map(Request::Apply).collect());
815 false
816 };
817 reopen
818 }
819 Request::Page { .. }
820 | Request::Pages { .. }
821 | Request::Identities { .. }
822 | Request::Conflicts { .. }
823 | Request::Versions { .. }
824 | Request::Version { .. }
825 | Request::Moving { .. }
826 | Request::Handover(_)
827 | Request::Resume => false,
828 };
829 }
830 // Retries fall due once a second however busy the section is; a closed cache is
831 // tried then alone, not with each edit.
832 if retry {
833 let due = attempted.elapsed() >= RETRY;
834 write = if closed(connection) {
835 due
836 } else {
837 write || due
838 };
839 }
840 if write {
841 attempted = web_time::Instant::now();
842 }
843 if let Some(waiting) = &stale {
844 // Only whether the closed commit landed is settled before the reread.
845 if write {
846 let _ =
847 crate::working::write(shared, &mut self.open, &mut self.unwritten, false);
848 }
849 if closed(connection) {
850 let held = waiting
851 .iter()
852 .filter(|request| matches!(request, Request::Apply(_)))
853 .count();
854 shared.wait(self.unwritten.len() + held);
855 continue;
856 }
857 } else if reopen || write && self.flush(shared).is_err() {
858 if closed(connection) {
859 stale = Some(VecDeque::new());
860 backlog.extend(burst.drain(..).chain(later.drain(..)));
861 continue;
862 }
863 } else {
864 continue;
865 }
866 backlog.extend(
867 self.unwritten
868 .drain(..)
869 .map(Request::Apply)
870 .chain(stale.into_iter().flatten())
871 .chain(burst.drain(..))
872 .chain(later.drain(..)),
873 );
874 // What the host asks next answers from the reread section.
875 if let (Ok(pages), Ok(reread)) = (self.section.pages(), shared.reread.lock())
876 && let Some(reread) = reread.as_ref()
877 {
878 reread(pages.into_iter().map(|(space, ..)| space).collect());
879 }
880 return Next::Reopen;
881 }
882 }
883
884 /// Answers `request` where it reads the section; any other comes back.
885 fn answer(&mut self, request: Request) -> Option<Request> {
886 match request {
887 Request::Page { space, reply } => reply(self.section.page(space).map_err(Into::into)),
888 Request::Pages { reply } => reply(self.section.pages().map_err(Into::into)),
889 Request::Identities { reply } => reply(self.section.identities().map_err(Into::into)),
890 Request::Conflicts { reply } => reply(self.section.conflicts().map_err(Into::into)),
891 Request::Versions { reply } => reply(self.section.versions().map_err(Into::into)),
892 Request::Version {
893 space,
894 version,
895 reply,
896 } => reply(self.section.version(space, version).map_err(Into::into)),
897 Request::Moving { space, reply } => reply(self.moving(space)),
898 request => return Some(request),
899 }
900 None
901 }
902
903 /// Starts a thread that runs `job` and rereads the section; false when the replica is
904 /// stopping, which answers the job's request.
905 #[cfg(not(target_arch = "wasm32"))]
906 fn rebuild(&self, shared: &Arc<Thread>, job: Job) -> bool {
907 let signal = shared
908 .sender
909 .lock()
910 .ok()
911 .and_then(|sender| sender.as_ref().cloned());
912 let Some(signal) = signal else {
913 match job {
914 Job::Rebase { reply, .. } => {
915 reply(Err(io::Error::other("The section thread stopped").into()))
916 }
917 Job::Reopen { reply } => {
918 reply(Err(io::Error::other("The section thread stopped").into()))
919 }
920 }
921 return false;
922 };
923 shared
924 .start(move |shared| async move { build(shared, job, signal) })
925 .is_ok()
926 }
927
928 /// Page `space` as a move copies it, with the conflict pages and versions it leaves.
929 fn moving(&mut self, space: ExGuid) -> Result<Moving> {
930 fn of<T>(space: ExGuid, listed: Vec<(ExGuid, Vec<T>)>) -> Vec<T> {
931 (listed.into_iter())
932 .find_map(|(page, of)| (page == space).then_some(of))
933 .unwrap_or_default()
934 }
935 Ok(Moving {
936 space,
937 page: self.section.page(space)?,
938 conflicts: of(space, self.section.conflicts()?),
939 versions: of(space, self.section.versions()?),
940 })
941 }
942
943 /// Applies an edit, keeping it to be written with the burst. A refused edit is answered
944 /// here; true when it failed part way and the section must be reread.
945 fn accept(&mut self, accepted: Accepted) -> bool {
946 if let Some(moving) = &accepted.moving
947 && !self.moving(moving.space).is_ok_and(|now| now == **moving)
948 {
949 let changed = OpError::StructureChanged("The page changed while it moved.");
950 (accepted.reply)(Err(changed.into()));
951 return false;
952 }
953 match self.section.apply(&accepted.author, &accepted.edit) {
954 Ok(()) => {
955 self.touched
956 .extend(queue::spaces(&accepted.edit, self.section.root()));
957 self.unwritten.push(accepted);
958 false
959 }
960 Err(error) => {
961 let broken = matches!(error, OpError::Failed(_));
962 (accepted.reply)(Err(error.into()));
963 broken
964 }
965 }
966 }
967
968 fn flush(&mut self, shared: &Arc<Thread>) -> Result<()> {
969 write(shared, &mut self.open, &mut self.unwritten, true)
970 }
971
972 /// Seals the open batch unless a sealed batch still waits for publication.
973 fn seal(&mut self, connection: &Mutex<Cache>) -> Result<Option<Sealed>> {
974 let Some(batch) = self.open else {
975 return Ok(None);
976 };
977 {
978 let connection = lock(connection)?;
979 let waiting: bool = connection.query_row(
980 "SELECT EXISTS(SELECT 1 FROM batches WHERE sealed IS NOT NULL)",
981 [],
982 |row| row.get(0),
983 )?;
984 if waiting {
985 return Ok(None);
986 }
987 }
988 let transaction = self.section.seal()?;
989 // A batch whose edits change nothing is recorded against the section's root.
990 let root = self.section.root();
991 let revisions: BTreeMap<ExGuid, ExGuid> = self
992 .section
993 .newest()
994 .filter(|(space, _)| {
995 self.touched.contains(space) || (self.touched.is_empty() && *space == root)
996 })
997 .collect();
998 // The sync thread publishes what it seals at once: the attempt is recorded with it.
999 lock(connection)?.execute(
1000 "UPDATE batches SET sealed=?1, revisions=?2, attempted=?3 WHERE id=?4",
1001 params![
1002 serde_json::to_string(&transaction).map_err(io::Error::other)?,
1003 serde_json::to_string(&revisions).map_err(io::Error::other)?,
1004 transaction.is_some(),
1005 batch
1006 ],
1007 )?;
1008 self.open = None;
1009 self.touched.clear();
1010 Ok(Some(Sealed { batch, transaction }))
1011 }
1012}
1013
1014/// Replays every queued edit on `image`, which becomes the base; each page whose local
1015/// version the remote could not take gains a conflict page holding it, queued as a new edit.
1016/// Returns the pages the remote changed.
1017fn rebase(
1018 connection: &Mutex<Cache>,
1019 image: Option<Vec<u8>>,
1020 key: Option<&Key>,
1021) -> Result<Vec<ExGuid>> {
1022 let (base_image, image, edits) = {
1023 let connection = lock(connection)?;
1024 if connection.query_row(
1025 "SELECT EXISTS(SELECT 1 FROM batches WHERE attempted=1)",
1026 [],
1027 |row| row.get::<_, bool>(0),
1028 )? {
1029 return Err(io::Error::new(
1030 io::ErrorKind::InvalidInput,
1031 "An uncertain attempt is never rebased",
1032 )
1033 .into());
1034 }
1035 let base_image = base::base(&connection)?;
1036 let image = match image {
1037 Some(image) => image,
1038 None => {
1039 base::read(&connection, base::Image::Remote)?.unwrap_or_else(|| base_image.clone())
1040 }
1041 };
1042 (base_image, image, queue::load(&connection, key, None)?)
1043 };
1044 let old_arena = Arena::default();
1045 let mut old = open(&old_arena, base_image, key)?;
1046 let before: BTreeMap<ExGuid, ExGuid> = old.revisions().collect();
1047 let remote_arena = Arena::default();
1048 let remote = open(&remote_arena, image.clone(), key)?;
1049 if old.root() != remote.root() {
1050 return Err(io::Error::new(
1051 io::ErrorKind::InvalidInput,
1052 "Remote snapshot belongs to another document",
1053 )
1054 .into());
1055 }
1056 let after: BTreeMap<ExGuid, ExGuid> = remote.revisions().collect();
1057 // The pages as the queue leaves them, read once a page conflicts: O(section).
1058 let local_arena = Arena::default();
1059 let mut local = None;
1060 // Pages the remote already holds as the local edits leave them: their ops are done.
1061 let mut converged = BTreeSet::new();
1062 let (rewritten, added) = loop {
1063 let arena = Arena::default();
1064 let mut new = open(&arena, image.clone(), key)?;
1065 let merged = merge::rebase(&mut old, &mut new, &edits, &converged)?;
1066 if !merged.conflicts.is_empty() && local.is_none() {
1067 local = Some(replay(&local_arena, &*lock(connection)?, key)?.0);
1068 }
1069 let pages: BTreeMap<ExGuid, Page> = merged
1070 .conflicts
1071 .keys()
1072 .filter_map(|space| Some((*space, local.as_ref()?.page(*space).ok()?)))
1073 .collect();
1074 let settled: Vec<ExGuid> = pages
1075 .iter()
1076 .filter(|(space, page)| remote.page(**space).is_ok_and(|remote| remote == **page))
1077 .map(|(space, _)| *space)
1078 .collect();
1079 if !settled.is_empty() {
1080 converged.extend(settled);
1081 continue;
1082 }
1083 let mut added = Vec::new();
1084 for (space, (author, objects)) in &merged.conflicts {
1085 // A page the queue went on to delete keeps no version of its own.
1086 let (Some(page), Some(local)) = (pages.get(space), local.as_mut()) else {
1087 continue;
1088 };
1089 let edit = merge::conflict_page(
1090 &mut new,
1091 local,
1092 &merged.moved,
1093 *space,
1094 page,
1095 author,
1096 objects,
1097 crate::now(),
1098 None,
1099 )?;
1100 added.push((author.clone(), edit));
1101 }
1102 break (merged.rewritten, added);
1103 };
1104 let mut connection = lock(connection)?;
1105 let transaction = connection.transaction_with_behavior(TransactionBehavior::Immediate)?;
1106 base::write(&transaction, base::Image::Base, &image)?;
1107 base::clear(&transaction, base::Image::Remote)?;
1108 transaction.execute("INSERT INTO batches DEFAULT VALUES", [])?;
1109 let batch = transaction.last_insert_rowid();
1110 transaction.execute("UPDATE edits SET batch=?1", [batch])?;
1111 transaction.execute("DELETE FROM batches WHERE id<>?1", [batch])?;
1112 for (id, edit) in rewritten {
1113 queue::rewrite(&transaction, key, id, &edit)?;
1114 }
1115 for (author, edit) in &added {
1116 queue::insert(&transaction, key, None, batch, author, edit)?;
1117 }
1118 if transaction.query_row("SELECT count(*) FROM edits", [], |row| row.get::<_, i64>(0))? == 0 {
1119 transaction.execute("DELETE FROM batches", [])?;
1120 }
1121 queue::collect(&transaction)?;
1122 transaction.commit()?;
1123 let mut changed: BTreeSet<ExGuid> = before
1124 .iter()
1125 .filter(|(space, rid)| after.get(space) != Some(rid))
1126 .map(|(space, _)| *space)
1127 .chain(
1128 after
1129 .keys()
1130 .filter(|space| !before.contains_key(space))
1131 .copied(),
1132 )
1133 .collect();
1134 changed.extend(converged);
1135 for (space, rid) in &before {
1136 if after.get(space) == Some(rid)
1137 && let (Ok(before), Ok(after)) = (old.page(*space), remote.page(*space))
1138 && before != after
1139 {
1140 changed.insert(*space);
1141 }
1142 }
1143 Ok(changed.into_iter().collect())
1144}
1145
1146/// Batches in order with their sealed transactions: `None` while open, `Some(None)` when
1147/// sealing stored nothing.
1148fn batches(connection: &Connection) -> Result<Vec<(i64, Option<Option<Transaction>>)>> {
1149 let mut query = connection.prepare("SELECT id, sealed FROM batches ORDER BY id")?;
1150 let mut rows = query.query([])?;
1151 let mut batches = Vec::new();
1152 while let Some(row) = rows.next()? {
1153 let sealed: Option<String> = row.get(1)?;
1154 batches.push((
1155 row.get(0)?,
1156 sealed
1157 .map(|sealed| serde_json::from_str(&sealed))
1158 .transpose()
1159 .map_err(|error| io::Error::new(io::ErrorKind::InvalidData, error))?,
1160 ));
1161 }
1162 Ok(batches)
1163}
1164
1165/// The cached base with each sealed batch replayed and the open batch's edits applied,
1166/// with the open batch and the spaces its edits change.
1167fn replay<'a>(
1168 arena: &'a Arena,
1169 connection: &Connection,
1170 key: Option<&Key>,
1171) -> Result<(Section<'a>, Option<i64>, BTreeSet<ExGuid>)> {
1172 let image = base::base(connection)?;
1173 let mut section = open(arena, image, key)?;
1174 let mut open = None;
1175 let mut touched = BTreeSet::new();
1176 for (batch, sealed) in batches(connection)? {
1177 if open.is_some() {
1178 return Err(io::Error::new(
1179 io::ErrorKind::InvalidData,
1180 "A cached batch follows the open batch",
1181 )
1182 .into());
1183 }
1184 match sealed {
1185 Some(Some(transaction)) => section.replay(&transaction)?,
1186 Some(None) => {}
1187 None => {
1188 for queued in queue::load(connection, key, Some(batch))? {
1189 section
1190 .apply(&queued.author, &queued.edit)
1191 .map_err(|error| {
1192 io::Error::new(
1193 io::ErrorKind::InvalidData,
1194 format!("Queued edit {} no longer applies: {error}", queued.id),
1195 )
1196 })?;
1197 touched.extend(queue::spaces(&queued.edit, section.root()));
1198 }
1199 open = Some(batch);
1200 }
1201 }
1202 }
1203 Ok((section, open, touched))
1204}
1205
1206/// The image the queue leaves, its unsealed edits sealed as one more revision; that
1207/// revision's identities are fresh on every call. O(section), for tests and recovery.
1208pub(crate) fn image(connection: &Connection, key: Option<&Key>) -> Result<Vec<u8>> {
1209 let arena = Arena::default();
1210 let (mut section, ..) = replay(&arena, connection, key)?;
1211 section.seal()?;
1212 Ok(section.image())
1213}
1214
1215/// A section image, a password-protected one under `key`.
1216pub(crate) fn open<'a>(arena: &'a Arena, image: Vec<u8>, key: Option<&Key>) -> Result<Section<'a>> {
1217 Ok(match key {
1218 Some(key) => Section::unlock(arena, image, key)?,
1219 None => Section::open(arena, image)?,
1220 })
1221}
1222
1223/// The sealed batch waiting for publication, if any.
1224pub(crate) fn sealed(connection: &Connection) -> Result<Option<Sealed>> {
1225 let row: Option<(i64, String)> = connection
1226 .query_row(
1227 "SELECT id, sealed FROM batches WHERE sealed IS NOT NULL ORDER BY id LIMIT 1",
1228 [],
1229 |row| Ok((row.get(0)?, row.get(1)?)),
1230 )
1231 .optional()?;
1232 row.map(|(batch, sealed)| {
1233 Ok(Sealed {
1234 batch,
1235 transaction: serde_json::from_str(&sealed)
1236 .map_err(|error| io::Error::new(io::ErrorKind::InvalidData, error))?,
1237 })
1238 })
1239 .transpose()
1240}
1241
1242/// Whether a write that failed may succeed when tried again, once the disk has room or a lock
1243/// is let go; any other error would fail the same way forever.
1244fn transient(error: &crate::Error) -> bool {
1245 use rusqlite::ErrorCode::*;
1246 matches!(error, crate::Error::Database(rusqlite::Error::SqliteFailure(error, _))
1247 if matches!(error.code, SystemIoFailure | DiskFull | DatabaseBusy | DatabaseLocked | CannotOpen))
1248}
1249
1250/// Writes `unwritten` in one transaction after the open batch `open`, then answers their
1251/// senders. A write that may succeed later keeps the edits with a notice; one that never will
1252/// refuses the edit it failed on, and an edit nobody waits for is refused at once. A failed
1253/// commit leaves the cache closed, still owned, until it reopens to decide whether the commit
1254/// landed; a lost cache refuses every edit. An error means the section holds an edit the
1255/// queue never will, or the cache is lost, and must be reread.
1256fn write(
1257 shared: &Arc<Thread>,
1258 open: &mut Option<i64>,
1259 unwritten: &mut Vec<Accepted>,
1260 commit: bool,
1261) -> Result<()> {
1262 let mut cache = (shared.cache.lock()).map_err(|_| io::Error::other("Cache owner panicked"))?;
1263 if unwritten.is_empty() && !matches!(*cache, Cache::Closed { .. }) {
1264 return Ok(());
1265 }
1266 let path = shared.path.as_deref();
1267 // The ids the cache holds for the first of the edits.
1268 let mut ids = Vec::new();
1269 // The error, and the edit it refuses when it would fail again.
1270 let failed = (|| {
1271 ids = reopen(&mut cache, path, open).map_err(|error| (error, None))?;
1272 if !commit || ids.len() == unwritten.len() {
1273 return Ok(());
1274 }
1275 let Cache::Open(connection) = &mut *cache else {
1276 unreachable!("A cache that does not reopen fails");
1277 };
1278 let mut inserted = Vec::new();
1279 let mut queued = 0;
1280 let committed = connection
1281 .transaction_with_behavior(TransactionBehavior::Immediate)
1282 .map_err(crate::Error::from)
1283 .and_then(|transaction| {
1284 queued = transaction
1285 .query_row("SELECT count(*) FROM edits", [], |row| row.get::<_, i64>(0))?;
1286 let batch = match *open {
1287 Some(batch) => batch,
1288 None => {
1289 transaction.execute("INSERT INTO batches DEFAULT VALUES", [])?;
1290 transaction.last_insert_rowid()
1291 }
1292 };
1293 for edit in &unwritten[ids.len()..] {
1294 inserted.push(queue::insert(
1295 &transaction,
1296 shared.key.as_ref(),
1297 None,
1298 batch,
1299 &edit.author,
1300 &edit.edit,
1301 )?);
1302 }
1303 Ok((batch, transaction.commit().err()))
1304 });
1305 match committed {
1306 Ok((batch, None)) => *open = Some(batch),
1307 Ok((_, Some(error))) => {
1308 // The warm connection may not know of frames its WAL holds, and must never
1309 // write again; it keeps the cache owned until the owner lock can.
1310 if let Cache::Open(warm) =
1311 std::mem::replace(&mut *cache, Cache::Lost(String::new()))
1312 {
1313 *cache = Cache::Closed {
1314 last: inserted[inserted.len() - 1],
1315 ids: inserted,
1316 queued: queued.unsigned_abs(),
1317 owner: Owner::Warm(warm),
1318 };
1319 }
1320 inserted = reopen(&mut cache, path, open).map_err(|error| (error, None))?;
1321 if inserted.is_empty() {
1322 return Err((error.into(), None));
1323 }
1324 }
1325 Err(error) => {
1326 let refused = (!transient(&error)).then_some(inserted.len());
1327 return Err((error, refused));
1328 }
1329 }
1330 ids.extend(inserted);
1331 Ok(())
1332 })();
1333 let written: Vec<_> = unwritten.drain(..ids.len()).zip(ids).collect();
1334 let refused: Vec<_> = match &failed {
1335 Ok(()) => Vec::new(),
1336 Err((_, failing)) => {
1337 let lost = matches!(*cache, Cache::Lost(_));
1338 let (mut at, mut kept) = (0, 0);
1339 (unwritten.extract_if(.., |edit| {
1340 at += 1;
1341 let refuse = lost || edit.waiting.is_none() || *failing == Some(at - 1);
1342 if !refuse {
1343 kept += 1;
1344 } else if let Cache::Closed { ids, .. } = &mut *cache
1345 && kept < ids.len()
1346 {
1347 // Whether its commit landed, it is answered now; a reread shows which.
1348 ids.remove(kept);
1349 }
1350 refuse
1351 }))
1352 .collect()
1353 }
1354 };
1355 drop(cache);
1356 // Counted before anyone hears, as a host hearing may ask.
1357 shared.wait(unwritten.len());
1358 if !written.is_empty() {
1359 #[cfg(target_arch = "wasm32")]
1360 shared.receipt(
1361 written
1362 .into_iter()
1363 .map(|(edit, id)| {
1364 let reply: Reply<()> =
1365 Box::new(move |result| (edit.reply)(result.map(|()| id)));
1366 reply
1367 })
1368 .collect(),
1369 true,
1370 );
1371 #[cfg(not(target_arch = "wasm32"))]
1372 {
1373 for (edit, id) in written {
1374 (edit.reply)(Ok(id));
1375 }
1376 crate::edited(&shared.worker);
1377 }
1378 }
1379 let Err((error, _)) = failed else {
1380 return Ok(());
1381 };
1382 let message = error.to_string();
1383 let kept = refused.is_empty();
1384 for edit in refused {
1385 (edit.reply)(Err(io::Error::other(message.clone()).into()));
1386 }
1387 if let Some(notice) = unwritten.iter().find_map(|edit| edit.waiting.as_ref()) {
1388 notice(&message);
1389 }
1390 if kept { Ok(()) } else { Err(error) }
1391}
1392
1393/// Reopens the cache a failed commit closed and reads cold whether the commit landed, as one
1394/// lands whole: the ids of its edits still waiting when it did, its batch becoming `open`;
1395/// none where it did not or the cache was open. The closed cache stays owned meanwhile, and
1396/// is lost once the file is gone or no longer a database.
1397fn reopen(cache: &mut Cache, path: Option<&Path>, open: &mut Option<i64>) -> Result<Vec<u64>> {
1398 let (last, ids, owner) = match cache {
1399 Cache::Open(_) => return Ok(Vec::new()),
1400 Cache::Closed {
1401 last, ids, owner, ..
1402 } => (*last, ids, owner),
1403 Cache::Lost(error) => return Err(io::Error::other(error.clone()).into()),
1404 };
1405 // Reopening through the browser's file system is unverified: it stays closed.
1406 let Some(path) = path else {
1407 let error = "The cache could not confirm a write";
1408 *cache = Cache::Lost(error.to_owned());
1409 return Err(io::Error::other(error).into());
1410 };
1411 let reopened = (|| -> Result<(Connection, Option<i64>)> {
1412 if let Owner::Warm(_) = owner {
1413 *owner = Owner::Owned(crate::Owned::take(path)?);
1414 }
1415 let cold = crate::connect(path)?;
1416 crate::write_ahead(&cold)?;
1417 if cold.is_readonly(rusqlite::MAIN_DB)? {
1418 return Err(io::Error::from(io::ErrorKind::ReadOnlyFilesystem).into());
1419 }
1420 let batch = cold
1421 .query_row(
1422 "SELECT batch FROM edits WHERE id=?1",
1423 [crate::signed(last)?],
1424 |row| row.get(0),
1425 )
1426 .optional()?;
1427 Ok((cold, batch))
1428 })();
1429 match reopened {
1430 Ok((cold, batch)) => {
1431 let landed = match batch {
1432 Some(batch) => {
1433 *open = Some(batch);
1434 std::mem::take(ids)
1435 }
1436 None => Vec::new(),
1437 };
1438 *cache = Cache::Open(cold);
1439 Ok(landed)
1440 }
1441 Err(error) => {
1442 use rusqlite::ErrorCode::*;
1443 let lost = !path.exists()
1444 || matches!(&error, crate::Error::Database(rusqlite::Error::SqliteFailure(error, _))
1445 if matches!(error.code, NotADatabase | DatabaseCorrupt));
1446 if lost {
1447 *cache = Cache::Lost(error.to_string());
1448 }
1449 Err(error)
1450 }
1451 }
1452}