froe 0.3.0

Reader and offline maintenance toolkit for Apache Jackrabbit Oak segment-tar (TarMK) repositories: parse archives and records, extract node data, compact, back up, and recover.
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
//! The commit path: editing the content tree and managing checkpoints.
//!
//! Every commit rewrites the *super-root* — the node the journal points
//! at, whose children are `root` (the content tree) and `checkpoints` —
//! and advances the head with a compare-and-set, exactly like Oak's
//! `SegmentNodeStore`. Untouched subtrees, property values, and child
//! maps are preserved by record identifier, so a commit writes only the
//! spine of records from the changed nodes up to the super-root.
//!
//! Checkpoints follow `LockBasedScheduler.CPCreator` faithfully: creation
//! first purges expired or corrupt checkpoints, then records `timestamp`
//! (the expiry, clamped at the 64-bit maximum), `created`, a `properties`
//! child with the caller's string metadata, and a `root` child that
//! *shares* the current content root's record — an immutable snapshot at
//! zero cost. Releasing a checkpoint is plain child removal.

use std::collections::BTreeMap;

use crate::content::provider::SegmentProvider;
use crate::content::template::ChildNodeArity;
use crate::error::{Error, Result};
use crate::segment::record::RecordIdentifier;
use crate::writer::record_writer::{
    ChildNodesToWrite, PropertyToWrite, PropertyValuesToWrite, RecordWriter, SegmentSink,
};
use crate::writer::store_writer::WritableRepository;

/// A named edit to one node's children: `Some` inserts or replaces the
/// child with the given node record, `None` removes it.
pub type ChildEdits = BTreeMap<String, Option<RecordIdentifier>>;

/// The reusable parts of an existing node.
struct NodeParts {
    primary_type: Option<String>,
    mixin_types: Vec<String>,
    properties: Vec<PropertyToWrite>,
    children: ExistingChildren,
}

/// The child structure of an existing node.
enum ExistingChildren {
    Zero,
    One {
        name: String,
        node: RecordIdentifier,
    },
    Many {
        map: RecordIdentifier,
    },
}

/// Reads the parts of an existing node needed to rewrite it while
/// preserving untouched records by identifier.
fn read_node_parts(provider: &dyn SegmentProvider, node: RecordIdentifier) -> Result<NodeParts> {
    let view = provider.segment(node.segment)?;
    let template_identifier = view.read_record_identifier(node.record_number, 0, 1)?;
    let template = provider.template(template_identifier)?;

    let children = match &template.child_arity {
        ChildNodeArity::Zero => ExistingChildren::Zero,
        ChildNodeArity::One { child_name } => ExistingChildren::One {
            name: child_name.clone(),
            node: view.read_record_identifier(node.record_number, 0, 2)?,
        },
        ChildNodeArity::Many => ExistingChildren::Many {
            map: view.read_record_identifier(node.record_number, 0, 2)?,
        },
    };

    let mut properties = Vec::with_capacity(template.properties.len());
    if !template.properties.is_empty() {
        let slot = if matches!(template.child_arity, ChildNodeArity::Zero) {
            2
        } else {
            3
        };
        let list_identifier = view.read_record_identifier(node.record_number, 0, slot)?;
        for (index, property) in template.properties.iter().enumerate() {
            let value_slot = crate::content::list::uncounted_list_entry(
                provider,
                list_identifier,
                template.properties.len() as u64,
                index as u64,
            )?;
            properties.push(PropertyToWrite {
                name: property.name.clone(),
                property_type: property.property_type,
                values: PropertyValuesToWrite::PreservedSlot {
                    value_slot,
                    is_multiple: property.is_multiple,
                },
            });
        }
    }

    Ok(NodeParts {
        primary_type: template.primary_type.clone(),
        mixin_types: template.mixin_types.clone(),
        properties,
        children,
    })
}

/// Rewrites a node with edits to its children, preserving its primary
/// type, mixin types, and every property slot. `base` of `None` starts
/// from an empty node.
#[allow(
    clippy::missing_panics_doc,
    reason = "the single-entry expect is guarded by the match arm's length"
)]
pub fn rewrite_node_with_child_edits<Sink: SegmentSink>(
    provider: &dyn SegmentProvider,
    writer: &mut RecordWriter<Sink>,
    base: Option<RecordIdentifier>,
    edits: &ChildEdits,
) -> Result<RecordIdentifier> {
    let parts = match base {
        Some(node) => read_node_parts(provider, node)?,
        None => NodeParts {
            primary_type: None,
            mixin_types: Vec::new(),
            properties: Vec::new(),
            children: ExistingChildren::Zero,
        },
    };

    // Materialize the resulting child set. Unchanged many-child maps are
    // preserved wholesale; any edit rebuilds the map from its entries
    // (which themselves preserve the child records by identifier).
    let children = if edits.is_empty() {
        match parts.children {
            ExistingChildren::Zero => ChildNodesToWrite::Zero,
            ExistingChildren::One { name, node } => ChildNodesToWrite::One { name, node },
            ExistingChildren::Many { map } => ChildNodesToWrite::ManyExistingMap(map),
        }
    } else {
        let mut entries: BTreeMap<String, RecordIdentifier> = match parts.children {
            ExistingChildren::Zero => BTreeMap::new(),
            ExistingChildren::One { name, node } => {
                let mut entries = BTreeMap::new();
                entries.insert(name, node);
                entries
            }
            ExistingChildren::Many { map } => crate::content::map::map_entries(provider, map)?
                .into_iter()
                .map(|entry| (entry.name, entry.value))
                .collect(),
        };
        for (name, edit) in edits {
            match edit {
                Some(node) => {
                    entries.insert(name.clone(), *node);
                }
                None => {
                    entries.remove(name);
                }
            }
        }
        match entries.len() {
            0 => ChildNodesToWrite::Zero,
            1 => {
                let (name, node) = entries.into_iter().next().expect("one entry");
                ChildNodesToWrite::One { name, node }
            }
            _ => ChildNodesToWrite::Many(entries.into_iter().collect()),
        }
    };

    writer.write_node(
        parts.primary_type.as_deref(),
        &parts.mixin_types,
        &children,
        &parts.properties,
    )
}

/// The checkpoint metadata AEM reads back.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CheckpointDescription {
    /// The checkpoint's name (a random UUID string).
    pub name: String,
    /// Creation time in milliseconds since the epoch, when present.
    pub created_milliseconds: Option<i64>,
    /// Expiry time in milliseconds since the epoch, when present.
    pub expires_milliseconds: Option<i64>,
}

/// Lists the checkpoints of the store's current head.
pub fn list_checkpoints(store: &WritableRepository) -> Result<Vec<CheckpointDescription>> {
    let head = store.head_node();
    let Some(checkpoints) = head.child_node("checkpoints")? else {
        return Ok(Vec::new());
    };
    let mut descriptions = Vec::new();
    for (name, checkpoint) in checkpoints.child_node_entries()? {
        descriptions.push(CheckpointDescription {
            created_milliseconds: read_long_property(&checkpoint, "created")?,
            expires_milliseconds: read_long_property(&checkpoint, "timestamp")?,
            name,
        });
    }
    Ok(descriptions)
}

fn read_long_property(
    node: &crate::content::node::NodeState<'_>,
    name: &str,
) -> Result<Option<i64>> {
    use crate::content::node::PropertyValues;
    use crate::content::property::PropertyValue;
    Ok(node
        .property(name)?
        .and_then(|property| match property.values {
            PropertyValues::Single(PropertyValue::Long(value)) => Some(value),
            _ => None,
        }))
}

/// Creates a checkpoint with the given lifetime and string metadata,
/// returning its generated name. Expired checkpoints are purged first,
/// exactly like Oak's checkpoint creator.
pub fn create_checkpoint(
    store: &WritableRepository,
    lifetime_milliseconds: i64,
    properties: &[(String, String)],
) -> Result<String> {
    if lifetime_milliseconds <= 0 {
        return Err(Error::InvalidFormat {
            details: "checkpoint lifetime must be positive".to_owned(),
        });
    }
    let now = current_time_milliseconds();
    let name = random_checkpoint_name();
    let expiry = if i64::MAX - now > lifetime_milliseconds {
        now + lifetime_milliseconds
    } else {
        i64::MAX
    };

    let head = store.head();
    let head_node = store.head_node();
    let checkpoints_container = head_node.child_node("checkpoints")?;
    let content_root = head_node
        .child_node("root")?
        .ok_or_else(|| Error::InvalidFormat {
            details: "the super-root has no \"root\" child node".to_owned(),
        })?;

    let generation = store.writing_generation()?;
    let mut writer = store.record_writer(generation);

    // Purge expired or corrupt checkpoints while assembling the edits.
    let mut edits: ChildEdits = ChildEdits::new();
    if let Some(container) = &checkpoints_container {
        for (existing_name, checkpoint) in container.child_node_entries()? {
            let expires = read_long_property(&checkpoint, "timestamp")?;
            if expires.is_none_or(|expires| now > expires) {
                edits.insert(existing_name, None);
            }
        }
    }

    // The properties child holds the caller's string metadata.
    let mut property_writes = Vec::with_capacity(properties.len());
    for (key, value) in properties {
        let value_identifier = writer.write_string(value)?;
        property_writes.push(PropertyToWrite {
            name: key.clone(),
            property_type: crate::content::property::PropertyType::String,
            values: PropertyValuesToWrite::Single(value_identifier),
        });
    }
    crate::writer::record_writer::sort_properties_for_template(&mut property_writes);
    let properties_node =
        writer.write_node(None, &[], &ChildNodesToWrite::Zero, &property_writes)?;

    // The checkpoint node: timestamp, created, properties, root snapshot.
    let timestamp_value = writer.write_string(&expiry.to_string())?;
    let created_value = writer.write_string(&now.to_string())?;
    let mut checkpoint_properties = vec![
        PropertyToWrite {
            name: "timestamp".to_owned(),
            property_type: crate::content::property::PropertyType::Long,
            values: PropertyValuesToWrite::Single(timestamp_value),
        },
        PropertyToWrite {
            name: "created".to_owned(),
            property_type: crate::content::property::PropertyType::Long,
            values: PropertyValuesToWrite::Single(created_value),
        },
    ];
    crate::writer::record_writer::sort_properties_for_template(&mut checkpoint_properties);
    let checkpoint_node = writer.write_node(
        None,
        &[],
        &ChildNodesToWrite::Many(vec![
            ("properties".to_owned(), properties_node),
            ("root".to_owned(), content_root.record_identifier()),
        ]),
        &checkpoint_properties,
    )?;
    edits.insert(name.clone(), Some(checkpoint_node));

    // Rebuild the checkpoints container and the super-root.
    let container_base = checkpoints_container.map(|container| container.record_identifier());
    let new_container = rewrite_node_with_child_edits(store, &mut writer, container_base, &edits)?;
    let mut super_root_edits = ChildEdits::new();
    super_root_edits.insert("checkpoints".to_owned(), Some(new_container));
    let new_head =
        rewrite_node_with_child_edits(store, &mut writer, Some(head), &super_root_edits)?;
    writer.finish()?;

    if !store.set_head(head, new_head) {
        return Err(Error::InvalidFormat {
            details: "the head moved while creating the checkpoint".to_owned(),
        });
    }
    store.flush()?;
    Ok(name)
}

/// Releases (removes) a checkpoint by name. Returns whether it existed.
pub fn release_checkpoint(store: &WritableRepository, name: &str) -> Result<bool> {
    let head = store.head();
    let head_node = store.head_node();
    let Some(container) = head_node.child_node("checkpoints")? else {
        return Ok(false);
    };
    if container.child_node(name)?.is_none() {
        return Ok(false);
    }

    let generation = store.writing_generation()?;
    let mut writer = store.record_writer(generation);
    let mut edits = ChildEdits::new();
    edits.insert(name.to_owned(), None);
    let new_container = rewrite_node_with_child_edits(
        store,
        &mut writer,
        Some(container.record_identifier()),
        &edits,
    )?;
    let mut super_root_edits = ChildEdits::new();
    super_root_edits.insert("checkpoints".to_owned(), Some(new_container));
    let new_head =
        rewrite_node_with_child_edits(store, &mut writer, Some(head), &super_root_edits)?;
    writer.finish()?;

    if !store.set_head(head, new_head) {
        return Err(Error::InvalidFormat {
            details: "the head moved while releasing the checkpoint".to_owned(),
        });
    }
    store.flush()?;
    Ok(true)
}

/// Removes every checkpoint. Returns how many were removed.
pub fn remove_all_checkpoints(store: &WritableRepository) -> Result<u64> {
    let names: Vec<String> = list_checkpoints(store)?
        .into_iter()
        .map(|checkpoint| checkpoint.name)
        .collect();
    let mut removed = 0u64;
    for name in names {
        if release_checkpoint(store, &name)? {
            removed += 1;
        }
    }
    Ok(removed)
}

/// Removes every checkpoint not referenced from the asynchronous indexer
/// state at `/:async`. Conservative: any string property value (or
/// member of a multi-valued string property) of that node counts as a
/// reference.
pub fn remove_unreferenced_checkpoints(store: &WritableRepository) -> Result<u64> {
    use crate::content::node::PropertyValues;
    use crate::content::property::PropertyValue;

    let mut referenced: std::collections::HashSet<String> = std::collections::HashSet::new();
    let head_node = store.head_node();
    if let Some(content_root) = head_node.child_node("root")?
        && let Some(async_state) = content_root.child_node(":async")?
    {
        for property in async_state.properties()? {
            match property.values {
                PropertyValues::Single(PropertyValue::String(value)) => {
                    referenced.insert(value);
                }
                PropertyValues::Multiple(values) => {
                    for value in values {
                        if let PropertyValue::String(value) = value {
                            referenced.insert(value);
                        }
                    }
                }
                PropertyValues::Single(_) => {}
            }
        }
    }

    let names: Vec<String> = list_checkpoints(store)?
        .into_iter()
        .map(|checkpoint| checkpoint.name)
        .filter(|name| !referenced.contains(name))
        .collect();
    let mut removed = 0u64;
    for name in names {
        if release_checkpoint(store, &name)? {
            removed += 1;
        }
    }
    Ok(removed)
}

/// Replaces the content root (`/root` of the super-root) with an already
/// written node record and advances the head. The restore operation's
/// final step.
pub fn replace_content_root(store: &WritableRepository, new_root: RecordIdentifier) -> Result<()> {
    let head = store.head();
    let generation = store.writing_generation()?;
    let mut writer = store.record_writer(generation);
    let mut edits = ChildEdits::new();
    edits.insert("root".to_owned(), Some(new_root));
    let new_head = rewrite_node_with_child_edits(store, &mut writer, Some(head), &edits)?;
    writer.finish()?;
    if !store.set_head(head, new_head) {
        return Err(Error::InvalidFormat {
            details: "the head moved while replacing the content root".to_owned(),
        });
    }
    store.flush()?;
    Ok(())
}

/// The current wall clock in milliseconds since the Unix epoch.
fn current_time_milliseconds() -> i64 {
    std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map_or(0, |duration| duration.as_millis() as i64)
}

/// A random version 4 UUID string, the checkpoint naming scheme.
fn random_checkpoint_name() -> String {
    // Reuse the segment identifier entropy stream; a checkpoint name is
    // an ordinary v4 UUID without the segment kind marker.
    let identifier = crate::writer::identifier_generator::new_data_segment_identifier();
    let most = identifier.most_significant_bits;
    // Restore a proper random variant nibble (10xx) in place of the data
    // segment marker.
    let least = (identifier.least_significant_bits & 0x3FFF_FFFF_FFFF_FFFF) | 0x8000_0000_0000_0000;
    format!(
        "{:08x}-{:04x}-{:04x}-{:04x}-{:012x}",
        most >> 32,
        (most >> 16) & 0xFFFF,
        most & 0xFFFF,
        least >> 48,
        least & 0xFFFF_FFFF_FFFF,
    )
}

#[cfg(test)]
mod tests {
    use super::{create_checkpoint, list_checkpoints, release_checkpoint, remove_all_checkpoints};
    use crate::store::Repository;
    use crate::writer::store_writer::WritableRepository;

    struct TestDirectory {
        path: std::path::PathBuf,
    }

    impl TestDirectory {
        fn new(name: &str) -> Self {
            let path =
                std::env::temp_dir().join(format!("froe-commit-{name}-{}", std::process::id()));
            let _ = std::fs::remove_dir_all(&path);
            Self { path }
        }
    }

    impl Drop for TestDirectory {
        fn drop(&mut self) {
            let _ = std::fs::remove_dir_all(&self.path);
        }
    }

    #[test]
    fn checkpoints_are_created_shared_and_released() {
        let directory = TestDirectory::new("lifecycle");
        {
            let store = WritableRepository::open(&directory.path).expect("bootstrap");
            let name = create_checkpoint(
                &store,
                1_000_000,
                &[("creator".to_owned(), "froe-test".to_owned())],
            )
            .expect("create");

            let listed = list_checkpoints(&store).expect("list");
            assert_eq!(listed.len(), 1);
            assert_eq!(listed[0].name, name);
            assert!(listed[0].created_milliseconds.is_some());
            assert!(listed[0].expires_milliseconds.is_some());
            store.close().expect("close");

            // The reader sees the checkpoint with a shared root snapshot.
            let repository = Repository::open(&directory.path).expect("reader");
            let checkpoints = repository.checkpoints().expect("checkpoints");
            assert_eq!(checkpoints.len(), 1);
            let (_, checkpoint) = &checkpoints[0];
            let snapshot = checkpoint
                .child_node("root")
                .expect("read")
                .expect("snapshot present");
            let live_root = repository.content_root().expect("content root");
            assert_eq!(
                snapshot.record_identifier(),
                live_root.record_identifier(),
                "the snapshot shares the live root's record"
            );
            let properties = checkpoint
                .child_node("properties")
                .expect("read")
                .expect("properties present");
            let creator = properties
                .property("creator")
                .expect("read")
                .expect("present");
            assert_eq!(
                creator.values,
                crate::content::node::PropertyValues::Single(
                    crate::content::property::PropertyValue::String("froe-test".to_owned())
                )
            );
        }
        {
            let store = WritableRepository::open(&directory.path).expect("reopen");
            let listed = list_checkpoints(&store).expect("list");
            assert_eq!(listed.len(), 1);
            assert!(
                release_checkpoint(&store, &listed[0].name).expect("release"),
                "the checkpoint existed"
            );
            assert!(list_checkpoints(&store).expect("list").is_empty());
            assert!(
                !release_checkpoint(&store, "missing").expect("release"),
                "absent checkpoints report false"
            );
            store.close().expect("close");
        }
    }

    #[test]
    fn expired_checkpoints_are_purged_on_create() {
        let directory = TestDirectory::new("expiry");
        let store = WritableRepository::open(&directory.path).expect("bootstrap");
        // A one-millisecond lifetime expires immediately.
        let expired = create_checkpoint(&store, 1, &[]).expect("create short lived");
        std::thread::sleep(std::time::Duration::from_millis(5));
        let fresh = create_checkpoint(&store, 1_000_000, &[]).expect("create fresh");

        let names: Vec<String> = list_checkpoints(&store)
            .expect("list")
            .into_iter()
            .map(|checkpoint| checkpoint.name)
            .collect();
        assert!(
            !names.contains(&expired),
            "the expired checkpoint is purged"
        );
        assert!(names.contains(&fresh));
        store.close().expect("close");
    }

    #[test]
    fn remove_all_reports_the_count() {
        let directory = TestDirectory::new("remove-all");
        let store = WritableRepository::open(&directory.path).expect("bootstrap");
        create_checkpoint(&store, 1_000_000, &[]).expect("first");
        create_checkpoint(&store, 1_000_000, &[]).expect("second");
        assert_eq!(remove_all_checkpoints(&store).expect("remove"), 2);
        assert!(list_checkpoints(&store).expect("list").is_empty());
        store.close().expect("close");
    }
}