pagedb 0.1.0-beta.4

Encrypted, portable, embedded page store with B+ tree and segment-file surfaces.
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
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
//! Stable identity-keyed migration of immutable segment files during rekey.

use crate::Result;
use crate::btree::BTree;
use crate::catalog::codec::{
    Catalog, CatalogRowKind, RekeyIntent, RekeySegmentProgress, RekeySegmentProgressState,
    SegmentMeta,
};
use crate::crypto::DerivedKey;
use crate::errors::PagedbError;
use crate::segment::reader::SegmentReader;
use crate::segment::writer::SegmentWriter;
use crate::txn::write::SegmentSideEffect;
use crate::vfs::Vfs;

#[cfg(test)]
use super::super::core::RekeyTestFault;
use super::super::core::{Db, WriterState};
use super::main::RekeyCatalogCommit;

/// Catalog rows read per batch while rekey walks segment and progress rows. A
/// segment row is a fixed-width authenticated value plus a name capped at
/// `MAX_SEGMENT_NAME_LEN`, so one batch is a few hundred KiB resident however
/// many segments the catalog holds.
const REKEY_ROW_BATCH: usize = 256;

struct SegmentEntry {
    key: Vec<u8>,
    meta: SegmentMeta,
}

impl<V: Vfs + Clone> Db<V> {
    /// Returns whether any catalog-linked segment still requires migration.
    /// A progress row whose source is no longer catalog-linked is only stale
    /// bookkeeping: catalog replacement has already published the successor,
    /// while pending tombstone ownership retains cleanup of the old file.
    ///
    /// The walk stops at the first row that still needs migrating, so the
    /// resident cost is one batch of rows.
    pub(super) async fn rekey_segments_pending(
        &self,
        state: &WriterState,
        intent: &RekeyIntent,
    ) -> Result<bool> {
        let mut cursor: Vec<u8> = vec![CatalogRowKind::Segment as u8];
        loop {
            let batch = self.segment_batch_from(state, &cursor).await?;
            let Some(last) = batch.last() else {
                return Ok(false);
            };
            cursor.clear();
            cursor.extend_from_slice(&last.key);
            // The exact successor of the last key in the key ordering: resume
            // strictly past the row just examined.
            cursor.push(0);
            let exhausted = batch.len() < REKEY_ROW_BATCH;

            if batch
                .iter()
                .any(|entry| Self::segment_needs_rekey(&entry.meta, intent))
            {
                return Ok(true);
            }
            if exhausted {
                return Ok(false);
            }
        }
    }

    /// Migrate every catalog-linked segment to the target epoch, then drain any
    /// progress row whose source the catalog no longer names.
    ///
    /// Both passes stream the catalog in fixed-size batches, and each segment is
    /// migrated from the row that carried it. Resolving a source by identity
    /// instead — the earlier shape — cost a full catalog scan per segment, so a
    /// rekey was quadratic in segment count; here each pass reads the catalog
    /// once and holds one batch resident.
    ///
    /// Migration replaces a row's value under its own key and otherwise touches
    /// only the disjoint progress-row prefix, so key order is stable across the
    /// mutations and the cursor stays valid. The tree is reopened per batch
    /// because every catalog swap commits a new root.
    pub(super) async fn migrate_rekey_segments(
        &self,
        state: &mut WriterState,
        intent: &RekeyIntent,
        target_hk: &DerivedKey,
    ) -> Result<()> {
        let mut cursor: Vec<u8> = vec![CatalogRowKind::Segment as u8];
        loop {
            let batch = self.segment_batch_from(state, &cursor).await?;
            let Some(last) = batch.last() else {
                break;
            };
            cursor.clear();
            cursor.extend_from_slice(&last.key);
            cursor.push(0);
            let exhausted = batch.len() < REKEY_ROW_BATCH;

            for entry in batch {
                self.migrate_rekey_segment_entry(state, intent, target_hk, entry)
                    .await?;
            }
            if exhausted {
                break;
            }
        }

        self.drain_orphaned_rekey_progress(state, intent, target_hk)
            .await
    }

    /// Resolve every progress row the segment pass left behind.
    ///
    /// That pass clears the progress row of each segment it migrates, so what
    /// remains names a source the catalog no longer carries — its successor was
    /// already published by a durable swap before the interruption. The rows are
    /// streamed and deleted as they are resolved, so the resident cost is one
    /// batch.
    async fn drain_orphaned_rekey_progress(
        &self,
        state: &mut WriterState,
        intent: &RekeyIntent,
        target_hk: &DerivedKey,
    ) -> Result<()> {
        let prefix = [CatalogRowKind::RekeySegmentProgress as u8];
        let mut cursor: Vec<u8> = prefix.to_vec();
        loop {
            if state.catalog_root_page_id == 0 {
                return Ok(());
            }
            let tree = self.rekey_catalog_tree(state);
            let batch = tree
                .collect_prefix_batch_from(&prefix, &cursor, REKEY_ROW_BATCH)
                .await?;
            let Some((last_key, _)) = batch.last() else {
                return Ok(());
            };
            cursor.clear();
            cursor.extend_from_slice(last_key);
            cursor.push(0);
            let exhausted = batch.len() < REKEY_ROW_BATCH;

            for (key, value) in &batch {
                if key.len() != 17 {
                    return Err(PagedbError::rekey_state_invalid(
                        "rekey.segment_progress.key",
                    ));
                }
                let mut source_id = [0u8; 16];
                source_id.copy_from_slice(&key[1..17]);
                let progress = Catalog::decode_rekey_segment_progress(value)?;
                // A source that is somehow still catalog-linked is migrated
                // through the ordinary path; only a genuinely absent one is
                // finished as an orphan.
                match self.segment_entry_by_id(state, source_id).await? {
                    Some(entry) => {
                        self.migrate_rekey_segment_entry(state, intent, target_hk, entry)
                            .await?;
                    }
                    None => {
                        self.finish_orphaned_rekey_progress(
                            state, intent, target_hk, source_id, progress,
                        )
                        .await?;
                    }
                }
            }
            if exhausted {
                return Ok(());
            }
        }
    }

    fn segment_needs_rekey(meta: &SegmentMeta, intent: &RekeyIntent) -> bool {
        // Rekey has no target-cipher API. Segments using a different persisted
        // cipher are not silently folded into this transition; their routing is
        // a separate compatibility concern.
        meta.cipher_id == intent.source_cipher_id && meta.mk_epoch != intent.target_mk_epoch
    }

    /// Migrate one catalog segment row to the target epoch.
    ///
    /// The row is handed in by the streaming pass that read it, so this never
    /// searches the catalog for its source; the only catalog read it makes is a
    /// point lookup of the row's own progress entry. A progress row already
    /// present means a previous attempt sealed the replacement and was
    /// interrupted before the swap — resume from it rather than sealing a second
    /// copy.
    async fn migrate_rekey_segment_entry(
        &self,
        state: &mut WriterState,
        intent: &RekeyIntent,
        target_hk: &DerivedKey,
        source: SegmentEntry,
    ) -> Result<()> {
        let source_id = source.meta.segment_id;
        let progress = if let Some(progress) = self.rekey_progress_row(state, source_id).await? {
            progress
        } else {
            if !Self::segment_needs_rekey(&source.meta, intent) {
                return Ok(());
            }
            self.seal_rekey_replacement(state, intent, target_hk, &source)
                .await?
        };

        let replacement = SegmentReader::open_rekey_replacement(
            self.pager.clone(),
            &source.meta,
            progress.replacement_segment_id,
            intent.target_mk_epoch,
            intent.target_cipher_id,
            self.mmap_bytes_in_use.clone(),
            u64::try_from(self.options.mmap_view_scratch_bytes).unwrap_or(u64::MAX),
        )
        .await?;
        let replacement_meta = replacement.meta().clone();
        self.replace_rekey_segment(
            state,
            intent,
            target_hk,
            &source,
            source_id,
            &replacement_meta,
        )
        .await?;
        #[cfg(test)]
        self.interrupt_rekey_if_requested(RekeyTestFault::CatalogSwapEffects)?;
        // The catalog swap is the migration commit point. A reader may
        // defer the old-file tombstone, but that queue owns eventual GC and
        // must not keep this durable transition open.
        self.delete_rekey_progress(state, source_id, intent.target_mk_epoch, target_hk)
            .await
    }

    /// Copy one source segment into a fresh target-epoch file and record the
    /// durable progress row naming it. Returns that row.
    async fn seal_rekey_replacement(
        &self,
        state: &mut WriterState,
        intent: &RekeyIntent,
        target_hk: &DerivedKey,
        source: &SegmentEntry,
    ) -> Result<RekeySegmentProgress> {
        let replacement_id = crate::crypto::random::segment_id()?;
        let limit = u64::try_from(self.options.mmap_view_scratch_bytes).unwrap_or(u64::MAX);
        let reader = SegmentReader::open_internal(
            self.pager.clone(),
            source.meta.clone(),
            self.mmap_bytes_in_use.clone(),
            limit,
        )
        .await?;
        let footer = reader.authenticated_footer();
        let mut writer = SegmentWriter::create_rekey_internal(
            self.pager.clone(),
            &source.meta,
            replacement_id,
            footer.fields.index_start_page,
            footer.fields.index_page_count,
        )
        .await?;
        writer.set_manifest(&footer.manifest)?;
        // Pages stream one at a time through the pager's own budget; the copy
        // never holds a whole segment resident.
        for page_id in 1..source.meta.page_count.saturating_sub(1) {
            let (kind, body) = reader.read_authenticated_page(page_id).await?;
            let copied_page_id = writer.append_rekey_page(kind, &body).await?;
            if copied_page_id != page_id {
                return Err(PagedbError::rekey_state_invalid("rekey.page_id_ordering"));
            }
        }
        let replacement = writer.seal().await?;
        #[cfg(test)]
        self.interrupt_rekey_if_requested(RekeyTestFault::SegmentSeal)?;
        drop(reader);
        if replacement.page_count != source.meta.page_count
            || replacement.format_version != source.meta.format_version
            || replacement.segment_kind != source.meta.segment_kind
            || replacement.evictable != source.meta.evictable
        {
            return Err(PagedbError::rekey_state_invalid(
                "rekey.replacement_metadata",
            ));
        }
        let progress = RekeySegmentProgress {
            replacement_segment_id: replacement_id,
            state: RekeySegmentProgressState::Sealed,
        };
        self.write_rekey_progress(
            state,
            source.meta.segment_id,
            progress,
            intent.target_mk_epoch,
            target_hk,
        )
        .await?;
        Ok(progress)
    }

    async fn finish_orphaned_rekey_progress(
        &self,
        state: &mut WriterState,
        intent: &RekeyIntent,
        target_hk: &DerivedKey,
        source_id: [u8; 16],
        progress: RekeySegmentProgress,
    ) -> Result<()> {
        let replacement = self
            .segment_entry_by_id(state, progress.replacement_segment_id)
            .await?
            .ok_or(PagedbError::RekeyReplacementMissing {
                replacement_segment_id: progress.replacement_segment_id,
            })?;
        if replacement.meta.mk_epoch != intent.target_mk_epoch
            || replacement.meta.cipher_id != intent.target_cipher_id
        {
            return Err(PagedbError::RekeyReplacementMissing {
                replacement_segment_id: progress.replacement_segment_id,
            });
        }
        // The catalog swap is authoritative, but a crash can leave the
        // replacement in staging before its post-header promotion ran. Finish
        // that publication idempotently; old-source cleanup remains elsewhere.
        let effects = [SegmentSideEffect::Promote {
            segment_id: progress.replacement_segment_id,
        }];
        self.reconcile_segment_effects(&effects, state.latest_commit_id)
            .await?;
        let limit = u64::try_from(self.options.mmap_view_scratch_bytes).unwrap_or(u64::MAX);
        SegmentReader::open_internal(
            self.pager.clone(),
            replacement.meta.clone(),
            self.mmap_bytes_in_use.clone(),
            limit,
        )
        .await
        .map_err(|_| PagedbError::RekeyReplacementMissing {
            replacement_segment_id: progress.replacement_segment_id,
        })?;
        // The source disappeared from the catalog in a prior durable swap.
        // Replaying its tombstone here would turn a reader-deferred cleanup
        // into false corruption after restart; catalog reconciliation and the
        // pending tombstone queue own that unreferenced file instead.
        self.delete_rekey_progress(state, source_id, intent.target_mk_epoch, target_hk)
            .await
    }

    async fn replace_rekey_segment(
        &self,
        state: &mut WriterState,
        intent: &RekeyIntent,
        target_hk: &DerivedKey,
        source: &SegmentEntry,
        source_id: [u8; 16],
        replacement: &SegmentMeta,
    ) -> Result<()> {
        let mut tree = BTree::open(
            self.pager.clone(),
            self.realm_id,
            state.catalog_root_page_id,
            state.next_page_id,
            self.page_size,
        );
        tree.put(&source.key, &Catalog::encode_segment_meta(replacement))
            .await?;
        tree.flush().await?;
        let freed_pages = tree.drain_freed();
        let effects = [
            SegmentSideEffect::Promote {
                segment_id: replacement.segment_id,
            },
            SegmentSideEffect::Tombstone {
                segment_id: source_id,
                tombstone_commit_id: None,
            },
        ];
        self.commit_rekey_catalog_root(
            state,
            RekeyCatalogCommit {
                catalog_root_page_id: tree.root_page_id(),
                next_page_id: tree.next_page_id(),
                freed_pages: &freed_pages,
                effects: &effects,
            },
            intent.target_mk_epoch,
            target_hk,
        )
        .await
        .map(|_| ())
    }

    async fn write_rekey_progress(
        &self,
        state: &mut WriterState,
        source_id: [u8; 16],
        progress: RekeySegmentProgress,
        header_epoch: u64,
        header_hk: &DerivedKey,
    ) -> Result<()> {
        let mut tree = self.rekey_catalog_tree(state);
        tree.put(
            &Catalog::rekey_segment_progress_key(source_id),
            &Catalog::encode_rekey_segment_progress(progress),
        )
        .await?;
        tree.flush().await?;
        let freed_pages = tree.drain_freed();
        self.commit_rekey_catalog_root(
            state,
            RekeyCatalogCommit {
                catalog_root_page_id: tree.root_page_id(),
                next_page_id: tree.next_page_id(),
                freed_pages: &freed_pages,
                effects: &[],
            },
            header_epoch,
            header_hk,
        )
        .await?;
        #[cfg(test)]
        self.interrupt_rekey_if_requested(RekeyTestFault::ProgressRowCommit)?;
        Ok(())
    }

    async fn delete_rekey_progress(
        &self,
        state: &mut WriterState,
        source_id: [u8; 16],
        header_epoch: u64,
        header_hk: &DerivedKey,
    ) -> Result<()> {
        let mut tree = self.rekey_catalog_tree(state);
        let _ = tree
            .delete(&Catalog::rekey_segment_progress_key(source_id))
            .await?;
        tree.flush().await?;
        let freed_pages = tree.drain_freed();
        self.commit_rekey_catalog_root(
            state,
            RekeyCatalogCommit {
                catalog_root_page_id: tree.root_page_id(),
                next_page_id: tree.next_page_id(),
                freed_pages: &freed_pages,
                effects: &[],
            },
            header_epoch,
            header_hk,
        )
        .await?;
        #[cfg(test)]
        self.interrupt_rekey_if_requested(RekeyTestFault::ProgressDeletion)?;
        Ok(())
    }

    fn rekey_catalog_tree(&self, state: &WriterState) -> BTree<V> {
        BTree::open(
            self.pager.clone(),
            self.realm_id,
            state.catalog_root_page_id,
            state.next_page_id,
            self.page_size,
        )
    }

    /// The durable progress row for one source segment, by point lookup.
    ///
    /// The row is keyed by source identity, so this is an O(height) descent —
    /// nothing about it scales with how many segments the catalog holds.
    async fn rekey_progress_row(
        &self,
        state: &WriterState,
        source_id: [u8; 16],
    ) -> Result<Option<RekeySegmentProgress>> {
        if state.catalog_root_page_id == 0 {
            return Ok(None);
        }
        let tree = self.rekey_catalog_tree(state);
        let key = Catalog::rekey_segment_progress_key(source_id);
        let Some(bytes) = tree.get(&key).await? else {
            return Ok(None);
        };
        Ok(Some(Catalog::decode_rekey_segment_progress(&bytes)?))
    }

    /// One bounded batch of catalog segment rows at or after `cursor`.
    async fn segment_batch_from(
        &self,
        state: &WriterState,
        cursor: &[u8],
    ) -> Result<Vec<SegmentEntry>> {
        if state.catalog_root_page_id == 0 {
            return Ok(Vec::new());
        }
        let tree = self.rekey_catalog_tree(state);
        let prefix = [CatalogRowKind::Segment as u8];
        tree.collect_prefix_batch_from(&prefix, cursor, REKEY_ROW_BATCH)
            .await?
            .into_iter()
            .map(|(key, value)| {
                Ok(SegmentEntry {
                    key,
                    meta: Catalog::decode_segment_meta(&value)?,
                })
            })
            .collect()
    }

    /// Resolve a catalog segment row by segment identity.
    ///
    /// The catalog is keyed by `(realm, name)`, so identity has no index to
    /// descend and the rows must be walked. Walking them in bounded batches with
    /// an early exit keeps the resident cost at one batch. Only the
    /// orphaned-progress path needs this, and progress rows are written and
    /// cleared one segment at a time, so the number of such lookups is the
    /// number of rows a single interrupted migration left behind — not a
    /// function of catalog size.
    async fn segment_entry_by_id(
        &self,
        state: &WriterState,
        segment_id: [u8; 16],
    ) -> Result<Option<SegmentEntry>> {
        let mut cursor: Vec<u8> = vec![CatalogRowKind::Segment as u8];
        loop {
            let batch = self.segment_batch_from(state, &cursor).await?;
            let Some(last) = batch.last() else {
                return Ok(None);
            };
            cursor.clear();
            cursor.extend_from_slice(&last.key);
            cursor.push(0);
            let exhausted = batch.len() < REKEY_ROW_BATCH;

            if let Some(entry) = batch
                .into_iter()
                .find(|entry| entry.meta.segment_id == segment_id)
            {
                return Ok(Some(entry));
            }
            if exhausted {
                return Ok(None);
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::pager::format::segment_footer::FORMAT_VERSION;
    use crate::segment::types::SegmentPageKind;
    use crate::vfs::memory::MemVfs;
    use crate::{RealmId, SegmentKind};

    use super::*;

    const PAGE: usize = 4096;
    const REALM: RealmId = RealmId::new([0x61; 16]);
    const KEK: [u8; 32] = [0x62; 32];

    #[tokio::test(flavor = "current_thread")]
    async fn rekey_preserves_manifest_footer_version_and_extent_index() {
        let db = Db::open_internal(MemVfs::new(), KEK, PAGE, REALM)
            .await
            .unwrap();

        // A segment with no extents carries an empty index block; the
        // replacement must keep that shape rather than synthesise one.
        let mut plain_writer = db
            .create_segment(REALM, SegmentKind::Unspecified)
            .await
            .unwrap();
        plain_writer
            .append_page(SegmentPageKind::Data, b"plain-data")
            .await
            .unwrap();
        plain_writer.set_manifest(b"plain-manifest").unwrap();
        let plain_meta = plain_writer.seal().await.unwrap();
        let mut txn = db.begin_write().await.unwrap();
        txn.link_segment("plain", &plain_meta).await.unwrap();
        txn.commit().await.unwrap();

        let mut indexed_writer = db
            .create_segment(REALM, SegmentKind::Unspecified)
            .await
            .unwrap();
        let extent = indexed_writer
            .append_extent(&[b"extent-a", b"extent-b"])
            .await
            .unwrap();
        indexed_writer.set_manifest(b"indexed-manifest").unwrap();
        let indexed_meta = indexed_writer.seal().await.unwrap();
        let mut txn = db.begin_write().await.unwrap();
        txn.link_segment("indexed", &indexed_meta).await.unwrap();
        txn.commit().await.unwrap();

        db.rekey_db(KEK, 1).await.unwrap();

        let plain = db.open_segment(REALM, "plain").await.unwrap();
        assert_eq!(plain.meta().format_version, FORMAT_VERSION);
        assert_eq!(plain.index_page_count(), 0);
        assert_eq!(
            plain.authenticated_footer().manifest.as_slice(),
            b"plain-manifest"
        );
        let indexed = db.open_segment(REALM, "indexed").await.unwrap();
        assert_eq!(indexed.meta().format_version, FORMAT_VERSION);
        assert_eq!(
            indexed.authenticated_footer().manifest.as_slice(),
            b"indexed-manifest"
        );
        let pages = indexed.find_extent(extent.start_page_id).await.unwrap();
        assert!(pages[0].starts_with(b"extent-a"));
        assert!(pages[1].starts_with(b"extent-b"));
    }
}