doublets 0.8.0

Doublets (links) data structure implementation.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
//! Transactions with reversible writes and optional durable recovery.
//!
//! The store's callbacks run *after* mutation. A journal of callbacks alone
//! therefore cannot recover a crash between mutation and callback. This layer
//! persists a complete before snapshot at begin and an after snapshot at commit,
//! as well as every callback transition. Recovery restores the last committed
//! snapshot, or the before snapshot of an interrupted transaction.
//!
//! Snapshots use O(number of links) space per begin/commit and grow the journal;
//! restore uses O(number of links + highest address) store operations, plus sorting
//! and backend index costs. There is no isolation from readers
//! outside the exclusively owned store, no nesting, and no asynchronous commit.
//! Recovery assumes the underlying store remains structurally readable; repairing
//! torn tree-index writes requires support from the storage backend itself.
//!
//! Put validation/cascade policies around the transaction *handle*, with a raw
//! store underneath this layer. This records all policy writes and lets recovery
//! restore arbitrary addresses without validation rejecting intermediate states.

mod log;
mod types;

pub use log::{FileTransitionLog, MemoryTransitionLog, TransitionLog};
pub use types::{JournalEntry, Transition, TransitionKind};

use std::{
    collections::{BTreeMap, BTreeSet},
    io,
    ops::{Deref, DerefMut},
};

use data::{Flow, LinkReference, LinksConstants};

use super::macros::forward;
use crate::{Doublets, Error, Fuse, Link, Links, ReadHandler, WriteHandler};

struct Pending<T: LinkReference> {
    id: u64,
    before: Vec<Link<T>>,
    start: usize,
    failed: bool,
}

/// Adds transactions to any raw [`Doublets`] store.
///
/// Writes outside an explicit transaction are committed individually. Use a
/// handle to group compound helpers such as `create_point` or `create_link`.
/// A successful file-backed commit is recoverable even if the store has not
/// flushed, because the journal contains its complete committed state.
///
/// ```
/// use doublets::{decorators::{DecoratorsExt, MemoryTransitionLog}, mem, unit, Doublets};
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let mut store = unit::Store::<usize, _>::new(mem::Global::new())?
///     .with_transactions(MemoryTransitionLog::default())?;
/// let mut tx = store.begin_transaction()?;
/// let point = tx.create_point()?;
/// tx.commit()?;
/// assert!(store.exist(point));
/// # Ok(())
/// # }
/// ```
pub struct TransactionsDecorator<T: LinkReference, L: Doublets<T>, J: TransitionLog<T>> {
    links: L,
    journal: J,
    transitions: Vec<Transition<T>>,
    pending: Option<Pending<T>>,
    next_id: u64,
    next_sequence: u64,
    poisoned: bool,
    tracing: bool,
    rollback_error: Option<String>,
}

impl<T: LinkReference, L: Doublets<T>, J: TransitionLog<T>> TransactionsDecorator<T, L, J> {
    /// Wraps the store and recovers its journal before returning.
    ///
    /// An empty journal adopts the current graph. A nonempty journal belongs to
    /// its original store and overrides the graph with its last recovery state.
    pub fn new(links: L, journal: J) -> Result<Self, Error<T>> {
        let mut store = Self {
            links,
            journal,
            transitions: Vec::new(),
            pending: None,
            next_id: 1,
            next_sequence: 1,
            poisoned: false,
            tracing: false,
            rollback_error: None,
        };
        store.recover()?;
        Ok(store)
    }

    /// Starts a transaction, persisting its before snapshot first.
    ///
    /// Nested transactions are rejected. The handle exclusively borrows this
    /// decorator; dropping it without completion rolls back, including on unwind.
    pub fn begin_transaction(&mut self) -> Result<Transaction<'_, T, L, J>, Error<T>> {
        self.begin()?;
        Ok(Transaction { store: self })
    }

    /// Returns the wrapped store for read-only inspection.
    #[must_use]
    pub const fn inner(&self) -> &L {
        &self.links
    }

    /// Returns the journal for read-only inspection.
    #[must_use]
    pub const fn journal(&self) -> &J {
        &self.journal
    }

    /// All recorded transitions, including writes subsequently rolled back.
    #[must_use]
    pub fn transitions(&self) -> &[Transition<T>] {
        &self.transitions
    }

    /// Whether an I/O or restoration failure has blocked further writes.
    #[must_use]
    pub const fn is_poisoned(&self) -> bool {
        self.poisoned
    }

    /// Enables diagnostic transaction and transition messages on stderr.
    /// Disabled by default.
    pub fn set_tracing(&mut self, enabled: bool) {
        self.tracing = enabled;
    }

    /// The last rollback failure, including one encountered by a handle's drop.
    ///
    /// Drop cannot return an error. A failed rollback poisons the decorator and
    /// leaves the begin record available for recovery when the store is reopened.
    #[must_use]
    pub fn rollback_error(&self) -> Option<&str> {
        self.rollback_error.as_deref()
    }

    /// Unwraps the decorator after rolling back any forgotten transaction.
    pub fn into_parts(mut self) -> Result<(L, J), Error<T>> {
        self.rollback()?;
        Ok((self.links, self.journal))
    }

    fn begin(&mut self) -> Result<(), Error<T>> {
        if self.poisoned {
            return Err(failure(
                "transaction journal is poisoned; reopen for recovery",
            ));
        }
        if self.pending.is_some() {
            return Err(failure("nested transactions are not supported"));
        }
        let id = self.next_id;
        self.next_id = id
            .checked_add(1)
            .ok_or_else(|| failure("transaction id exhausted"))?;
        let before = snapshot(&self.links);
        self.append(&JournalEntry::Begin {
            transaction_id: id,
            snapshot: before.clone(),
        })?;
        self.pending = Some(Pending {
            id,
            before,
            start: self.transitions.len(),
            failed: false,
        });
        if self.tracing {
            eprintln!("[doublets transactions] begin {id}");
        }
        Ok(())
    }

    fn append(&mut self, entry: &JournalEntry<T>) -> Result<(), Error<T>> {
        if let Err(error) = self.journal.append(entry) {
            self.poisoned = true;
            return Err(error.into());
        }
        Ok(())
    }

    fn commit(&mut self) -> Result<(), Error<T>> {
        let Some(pending) = &self.pending else {
            return Ok(());
        };
        if pending.failed || self.poisoned {
            return Err(failure("failed transaction cannot be committed"));
        }
        let id = pending.id;
        let entry = JournalEntry::Commit {
            transaction_id: id,
            snapshot: snapshot(&self.links),
        };
        self.append(&entry)?;
        if self.tracing {
            eprintln!("[doublets transactions] commit {id}");
        }
        self.pending = None;
        Ok(())
    }

    fn rollback(&mut self) -> Result<(), Error<T>> {
        let Some(pending) = &self.pending else {
            return Ok(());
        };
        let id = pending.id;
        let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
            restore(&mut self.links, &pending.before)
        }))
        .unwrap_or_else(|_| {
            Err(failure(
                "wrapped store panicked while restoring transaction",
            ))
        });
        if let Err(error) = result {
            self.poisoned = true;
            self.rollback_error = Some(error.to_string());
            return Err(error);
        }
        self.pending = None;
        if self.poisoned {
            return Ok(());
        }
        if self.tracing {
            eprintln!("[doublets transactions] rollback {id}");
        }
        if let Err(error) = self.append(&JournalEntry::Rollback { transaction_id: id }) {
            self.rollback_error = Some(error.to_string());
            return Err(error);
        }
        Ok(())
    }

    fn recover(&mut self) -> Result<(), Error<T>> {
        let entries = self.journal.read_entries()?;
        let mut pending = None;
        let mut desired = None;
        for entry in entries {
            match entry {
                JournalEntry::Begin {
                    transaction_id,
                    snapshot,
                } => {
                    if pending.is_some() || transaction_id < self.next_id {
                        return Err(failure("invalid transaction begin order"));
                    }
                    validate(&self.links, &snapshot)?;
                    self.next_id = transaction_id
                        .checked_add(1)
                        .ok_or_else(|| failure("transaction id exhausted"))?;
                    pending = Some(transaction_id);
                    desired = Some(snapshot);
                }
                JournalEntry::Transition(transition) => {
                    transition.validate(&self.links)?;
                    if pending != Some(transition.transaction_id)
                        || transition.sequence < self.next_sequence
                    {
                        return Err(failure("invalid transition order"));
                    }
                    self.next_sequence = transition
                        .sequence
                        .checked_add(1)
                        .ok_or_else(|| failure("transition sequence exhausted"))?;
                    self.transitions.push(transition);
                }
                JournalEntry::Commit {
                    transaction_id,
                    snapshot,
                } => {
                    if pending != Some(transaction_id) {
                        return Err(failure("commit without matching begin"));
                    }
                    validate(&self.links, &snapshot)?;
                    desired = Some(snapshot);
                    pending = None;
                }
                JournalEntry::Rollback { transaction_id } => {
                    if pending != Some(transaction_id) {
                        return Err(failure("rollback without matching begin"));
                    }
                    pending = None;
                }
            }
        }
        if let Some(desired) = desired {
            restore(&mut self.links, &desired)?;
        }
        if let Some(transaction_id) = pending {
            self.append(&JournalEntry::Rollback { transaction_id })?;
        }
        Ok(())
    }

    fn write<F>(&mut self, handler: WriteHandler<'_, T>, operation: F) -> Result<Flow, Error<T>>
    where
        F: FnOnce(&mut L, WriteHandler<'_, T>) -> Result<Flow, Error<T>>,
    {
        if self.poisoned {
            return Err(failure(
                "transaction journal is poisoned; reopen for recovery",
            ));
        }
        let automatic = self.pending.is_none();
        if automatic {
            self.begin()?;
        }
        let pending = self
            .pending
            .as_mut()
            .ok_or_else(|| failure("no active transaction"))?;
        if pending.failed {
            return Err(failure("transaction has a failed write; roll it back"));
        }
        let id = pending.id;
        let mut handler = Fuse::new(handler);
        let mut log_error = None;
        let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
            operation(&mut self.links, &mut |before, after| {
                if log_error.is_some() {
                    return Flow::Break;
                }
                if before != after {
                    let Some(next) = self.next_sequence.checked_add(1) else {
                        log_error = Some(io::Error::other("transition sequence exhausted"));
                        return Flow::Break;
                    };
                    let transition =
                        Transition::new(id, self.next_sequence, before.clone(), after.clone());
                    if self.tracing {
                        eprintln!("[doublets transactions] {transition:?}");
                    }
                    // Keep the record even if append fails: the mutation already happened.
                    self.transitions.push(transition.clone());
                    self.next_sequence = next;
                    if let Err(error) = self.journal.append(&JournalEntry::Transition(transition)) {
                        log_error = Some(error);
                        return Flow::Break;
                    }
                }
                handler.call(before, after)
            })
        }));
        let result = match result {
            Ok(result) => result,
            Err(panic) => {
                pending.failed = true;
                if automatic {
                    let _ = self.rollback();
                }
                std::panic::resume_unwind(panic);
            }
        };
        let result = if let Some(error) = log_error {
            self.poisoned = true;
            Err(error.into())
        } else {
            result
        };
        if result.is_err() {
            pending.failed = true;
        }
        if automatic {
            match result {
                Ok(flow) => {
                    if let Err(error) = self.commit() {
                        self.rollback()?;
                        return Err(error);
                    }
                    Ok(flow)
                }
                Err(error) => {
                    self.rollback()?;
                    Err(error)
                }
            }
        } else {
            result
        }
    }
}

impl<T: LinkReference, L: Doublets<T>, J: TransitionLog<T>> Links<T>
    for TransactionsDecorator<T, L, J>
{
    forward!(count_links, each_links);

    fn create_links(
        &mut self,
        query: &[T],
        handler: WriteHandler<'_, T>,
    ) -> Result<Flow, Error<T>> {
        self.write(handler, |links, handler| links.create_links(query, handler))
    }
    fn update_links(
        &mut self,
        query: &[T],
        change: &[T],
        handler: WriteHandler<'_, T>,
    ) -> Result<Flow, Error<T>> {
        self.write(handler, |links, handler| {
            links.update_links(query, change, handler)
        })
    }
    fn delete_links(
        &mut self,
        query: &[T],
        handler: WriteHandler<'_, T>,
    ) -> Result<Flow, Error<T>> {
        self.write(handler, |links, handler| links.delete_links(query, handler))
    }
}

impl<T: LinkReference, L: Doublets<T>, J: TransitionLog<T>> Doublets<T>
    for TransactionsDecorator<T, L, J>
{
    fn get_link(&self, index: T) -> Option<Link<T>> {
        self.links.get_link(index)
    }
}

/// An exclusively borrowed transaction; unfinished handles roll back on drop.
///
/// `commit` and `rollback` consume the handle. If commit returns an I/O error,
/// its outcome is indeterminate until journal recovery: an append may have reached
/// disk despite reporting failure. Further writes are blocked on that decorator.
/// Call explicit rollback to receive restoration errors; drop records them in
/// [`TransactionsDecorator::rollback_error`] and never panics intentionally.
#[must_use = "commit or rollback the transaction; dropping it rolls back"]
pub struct Transaction<'a, T: LinkReference, L: Doublets<T>, J: TransitionLog<T>> {
    store: &'a mut TransactionsDecorator<T, L, J>,
}

impl<T: LinkReference, L: Doublets<T>, J: TransitionLog<T>> Transaction<'_, T, L, J> {
    /// Commits the transaction after persisting the complete resulting graph.
    pub fn commit(self) -> Result<(), Error<T>> {
        self.store.commit()
    }
    /// Restores the original graph and records the rollback.
    pub fn rollback(self) -> Result<(), Error<T>> {
        self.store.rollback()
    }
    /// Transitions recorded by this transaction so far.
    #[must_use]
    pub fn transitions(&self) -> &[Transition<T>] {
        let start = self
            .store
            .pending
            .as_ref()
            .map_or(self.store.transitions.len(), |p| p.start);
        &self.store.transitions[start..]
    }
}

impl<T: LinkReference, L: Doublets<T>, J: TransitionLog<T>> Drop for Transaction<'_, T, L, J> {
    fn drop(&mut self) {
        let _ = self.store.rollback();
    }
}

impl<T: LinkReference, L: Doublets<T>, J: TransitionLog<T>> Deref for Transaction<'_, T, L, J> {
    type Target = TransactionsDecorator<T, L, J>;
    fn deref(&self) -> &Self::Target {
        self.store
    }
}
impl<T: LinkReference, L: Doublets<T>, J: TransitionLog<T>> DerefMut for Transaction<'_, T, L, J> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.store
    }
}

// Implement Links on the handle itself so it composes with DecoratorsExt.
impl<T: LinkReference, L: Doublets<T>, J: TransitionLog<T>> Links<T> for Transaction<'_, T, L, J> {
    fn constants(&self) -> &LinksConstants<T> {
        self.store.constants()
    }
    fn count_links(&self, query: &[T]) -> T {
        self.store.count_links(query)
    }
    fn each_links(&self, query: &[T], handler: ReadHandler<'_, T>) -> Flow {
        self.store.each_links(query, handler)
    }
    fn create_links(
        &mut self,
        query: &[T],
        handler: WriteHandler<'_, T>,
    ) -> Result<Flow, Error<T>> {
        self.store.create_links(query, handler)
    }
    fn update_links(
        &mut self,
        query: &[T],
        change: &[T],
        handler: WriteHandler<'_, T>,
    ) -> Result<Flow, Error<T>> {
        self.store.update_links(query, change, handler)
    }
    fn delete_links(
        &mut self,
        query: &[T],
        handler: WriteHandler<'_, T>,
    ) -> Result<Flow, Error<T>> {
        self.store.delete_links(query, handler)
    }
}
impl<T: LinkReference, L: Doublets<T>, J: TransitionLog<T>> Doublets<T>
    for Transaction<'_, T, L, J>
{
    fn get_link(&self, index: T) -> Option<Link<T>> {
        self.store.get_link(index)
    }
}

fn failure<T: LinkReference>(message: &str) -> Error<T> {
    io::Error::other(message).into()
}

fn snapshot<T: LinkReference, L: Doublets<T>>(links: &L) -> Vec<Link<T>> {
    let mut snapshot = Vec::new();
    links.each_links(&[], &mut |link| {
        snapshot.push(link);
        Flow::Continue
    });
    snapshot.sort_by_key(|link| link.index);
    snapshot
}

fn validate<T: LinkReference, L: Doublets<T>>(
    links: &L,
    desired: &[Link<T>],
) -> Result<(), Error<T>> {
    let mut indexes = BTreeSet::new();
    for link in desired {
        if !links.constants().is_internal(link.index) || !indexes.insert(link.index) {
            return Err(failure(
                "snapshot contains an invalid or duplicate link address",
            ));
        }
    }
    Ok(())
}

fn restore<T: LinkReference, L: Doublets<T>>(
    links: &mut L,
    desired: &[Link<T>],
) -> Result<(), Error<T>> {
    validate(links, desired)?;
    let current = snapshot(links);
    let desired: BTreeMap<_, _> = desired.iter().map(|link| (link.index, link)).collect();
    if current.iter().eq(desired.values().copied()) {
        return Ok(());
    }
    // Clear indexed contents first: applying snapshots one link at a time can
    // transiently duplicate pairs (e.g. swapping two doublets) and damage trees.
    let null = T::from_byte(0);
    for link in &current {
        if link.source != null || link.target != null {
            links.update(link.index, null, null)?;
        }
    }
    let mut allocated: BTreeSet<_> = current.iter().map(|link| link.index).collect();
    let mut missing: BTreeSet<_> = desired
        .keys()
        .copied()
        .filter(|index| !allocated.contains(index))
        .collect();
    let limit = current
        .iter()
        .map(|link| link.index)
        .chain(desired.keys().copied())
        .max()
        .unwrap_or(null);
    while !missing.is_empty() {
        let index = links.create()?;
        if !allocated.insert(index) {
            return Err(failure("store did not allocate a fresh link address"));
        }
        if index > limit {
            return Err(failure("store cannot restore requested link addresses"));
        }
        missing.remove(&index);
    }
    for index in allocated.into_iter().rev() {
        if !desired.contains_key(&index) {
            links.delete(index)?;
        }
    }
    for link in desired.values() {
        if link.source != null || link.target != null {
            links.update(link.index, link.source, link.target)?;
        }
    }
    Ok(())
}