froe 0.12.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
//! The tail: what an apply proves about a subtree before it publishes it,
//! and the test-only seam that proves the proving is live.
//!
//! Split out of `apply.rs` because it is a second subject — the operation
//! decides what to write, this decides whether to keep it — and because the
//! two together exceed the thousand-line gate.

use std::collections::BTreeMap;
use std::path::Path;

use crate::error::{Error, Result};
use crate::progress::{ProgressObserver, count};
use crate::segment::record::RecordIdentifier;
#[cfg(test)]
use crate::writer::record_writer::{
    ChildNodesToWrite, PropertyToWrite, PropertyValuesToWrite, RecordWriter, SegmentSink,
};
use crate::writer::store_writer::WritableRepository;

use super::{RebuiltDefinition, Verification, index_error_to_store_error};

/// Every new subtree, verified through the open session before anything is
/// published.
///
/// Two halves, and neither is `check`'s: the node tree, which proves every
/// record the subtree names is readable, and then an entry-side pass that
/// proves the entries agree with the content they name. The covered-node
/// half — a walk of every node the definition covers — is deliberately not
/// run here. It is what the rebuild just did, at the same cost, and running
/// it again would double a reindex's walk to re-derive the input it was
/// given.
pub(super) fn verify_before_publication(
    store: &WritableRepository,
    rebuilt: &[RebuiltDefinition],
    observer: &mut dyn ProgressObserver,
) -> Result<()> {
    let mut verified = 0usize;
    for definition in rebuilt {
        crate::tooling::check::verify_node_tree(store, definition.rebuilt_record)?;
        verify_one_definition(store, definition)?;
        verified += 1;
        observer.step_advanced(count(verified));
    }
    Ok(())
}

/// The entry-side half for one rebuilt definition.
fn verify_one_definition(store: &WritableRepository, definition: &RebuiltDefinition) -> Result<()> {
    match &definition.verification {
        Verification::NodeTreeOnly => Ok(()),
        Verification::Entries {
            state_root,
            entries,
        } => verify_entries(store, definition, *state_root, *entries),
        Verification::Counter {
            state_root,
            credited_by_path,
        } => verify_counter(store, definition, *state_root, credited_by_path),
        Verification::LuceneSegment { documents, files } => {
            verify_lucene_segment(store, definition, *documents, files)
        }
    }
}

/// The Lucene arm: the `:data` the run wrote is read back **through the
/// open session, before publication**, with plan 0008's own readers.
///
/// Two things are asserted, and they are the two a wrong write would break:
/// every file the copy named reads back with the bytes the segment
/// directory holds, and the commit the directory carries names the document
/// count the writer reported. A file that does not read back refuses here,
/// which is the precondition plan 0008's case states for its own `:data`
/// write and this plan's mutation table repeats.
fn verify_lucene_segment(
    store: &WritableRepository,
    definition: &RebuiltDefinition,
    documents: u64,
    files: &[String],
) -> Result<()> {
    use std::io::Read as _;

    let node = crate::content::node::NodeState::new(store, definition.rebuilt_record);
    let model = crate::index::definition::IndexDefinition::read(&node, &definition.path)
        .map_err(index_error_to_store_error)?;
    let directory = crate::index::lucene::OakDirectory::open(
        store,
        &node,
        &model,
        crate::index::lucene::INDEX_DATA_CHILD_NAME,
    )
    .map_err(index_error_to_store_error)?
    .ok_or_else(|| Error::InvalidFormat {
        details: format!(
            "the rebuilt definition {} has no {} child",
            definition.path,
            crate::index::lucene::INDEX_DATA_CHILD_NAME
        ),
    })?;
    let present: std::collections::BTreeSet<&str> =
        directory.file_names().iter().map(String::as_str).collect();
    for name in files {
        if !present.contains(name.as_str()) {
            return Err(Error::InvalidFormat {
                details: format!(
                    "the rebuilt {} is missing the file {name} the copy wrote",
                    definition.path
                ),
            });
        }
        // Reading the file back is what proves the records hold it: a
        // truncated or unreadable blob fails here, before the head moves.
        let file = directory.file(name).map_err(index_error_to_store_error)?;
        let mut stored = Vec::new();
        file.reader().read_to_end(&mut stored)?;
        if stored.is_empty() {
            return Err(Error::InvalidFormat {
                details: format!(
                    "the rebuilt {}'s {name} reads back empty, which no segment file is",
                    definition.path
                ),
            });
        }
    }
    // The structural check is plan 0008's own, over the records this run
    // just wrote: the commit parses, every file it names is present,
    // nothing is unreferenced, and the live document count is the one the
    // writer reported.
    let report = crate::index::lucene::check::check_structure(&directory)
        .map_err(index_error_to_store_error)?;
    if !report.is_coherent() {
        return Err(Error::InvalidFormat {
            details: format!(
                "the rebuilt {} does not read back as a coherent index: {} missing, {} \
                 unreferenced, {} unreadable",
                definition.path,
                report.missing_files.len(),
                report.unreferenced_files.len(),
                report.unreadable_files.len()
            ),
        });
    }
    let counted = u64::try_from(report.live_document_count).unwrap_or(0);
    if counted != documents {
        return Err(Error::InvalidFormat {
            details: format!(
                "the rebuilt {} holds {counted} live documents where the writer wrote \
                 {documents}",
                definition.path
            ),
        });
    }
    Ok(())
}

/// A test-only way to damage a subtree between the builders and the tail.
///
/// The tail refuses when what was written disagrees with the content it
/// indexes or with the build's own record of what it counted. A suite that
/// only ever runs healthy reindexes cannot tell that apart from a tail that
/// returns `Ok(())` unconditionally — both are green — so this seam rewrites
/// one subtree after its builder returns and before the tail runs, and the
/// regressions below assert the refusal fires, names what it found, and
/// moves no head.
#[cfg(test)]
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub(crate) enum SubtreePerturbation {
    /// Splice `count` forged key nodes into the mirror, each carrying
    /// `match` under a path that is not in the content.
    ///
    /// One is a stale entry the pass reports. Two exceed the budget, which
    /// is the collected count plus one and accepts at its limit — so two is
    /// the smallest number that proves the budget is the collected count
    /// rather than something looser.
    SpliceForgedEntries(u32),
    /// Add one to the counter mirror's root `:cnt`.
    AlterOneCount,
}

#[cfg(test)]
std::thread_local! {
    static PERTURBATION: std::cell::RefCell<Option<SubtreePerturbation>> =
        const { std::cell::RefCell::new(None) };
}

/// Makes the next reindex on this thread damage what it built.
#[cfg(test)]
pub(crate) fn perturb_subtree(perturbation: Option<SubtreePerturbation>) {
    PERTURBATION.with(|cell| *cell.borrow_mut() = perturbation);
}

/// Damages every rebuilt definition, in a writer finished before the tail.
#[cfg(test)]
pub(super) fn perturb_all(
    store: &WritableRepository,
    rebuilt: &mut [RebuiltDefinition],
) -> Result<()> {
    if PERTURBATION.with(|cell| cell.borrow().is_none()) {
        return Ok(());
    }
    let mut writer = store.record_writer(store.writing_generation()?);
    for built in rebuilt {
        perturb(store, &mut writer, built)?;
    }
    writer.finish()?;
    Ok(())
}

/// Does nothing outside a test build.
#[cfg(not(test))]
#[inline]
#[expect(
    clippy::unnecessary_wraps,
    reason = "the sibling this stands in for can fail, and the caller is the same either way"
)]
pub(super) fn perturb_all(
    store: &WritableRepository,
    rebuilt: &mut [RebuiltDefinition],
) -> Result<()> {
    let _ = (store, rebuilt);
    Ok(())
}

/// Rewrites one rebuilt definition's `:index`, in place of the record the
/// builder produced.
#[cfg(test)]
fn perturb<Sink: SegmentSink>(
    store: &WritableRepository,
    writer: &mut RecordWriter<Sink>,
    built: &mut RebuiltDefinition,
) -> Result<()> {
    let Some(perturbation) = PERTURBATION.with(|cell| *cell.borrow()) else {
        return Ok(());
    };
    let definition_node = crate::content::node::NodeState::new(store, built.rebuilt_record);
    let Some(index) = definition_node.child_node(crate::index::INDEX_CONTENT_NODE_NAME)? else {
        return Ok(());
    };

    let new_index = match perturbation {
        SubtreePerturbation::SpliceForgedEntries(count) => {
            let mut edits = crate::writer::commit::ChildEdits::new();
            for serial in 0..count {
                let flag = writer.write_string("true")?;
                let leaf = writer.write_node(
                    None,
                    &[],
                    &ChildNodesToWrite::Zero,
                    &[PropertyToWrite {
                        name: "match".to_owned(),
                        property_type: crate::PropertyType::Boolean,
                        values: PropertyValuesToWrite::Single(flag),
                    }],
                )?;
                let key = writer.write_node(
                    None,
                    &[],
                    &ChildNodesToWrite::One {
                        name: "nowhere".to_owned(),
                        node: leaf,
                    },
                    &[],
                )?;
                edits.insert(format!("froe-forged-{serial}"), Some(key));
            }
            crate::writer::commit::rewrite_node_with_child_edits(
                store,
                writer,
                Some(index.record_identifier()),
                &edits,
            )?
        }
        SubtreePerturbation::AlterOneCount => {
            let own = counter_value(&index, "/", &built.path)?;
            let value = writer.write_string(&(own + 1).to_string())?;
            crate::writer::commit::rewrite_node_with_edits(
                store,
                writer,
                Some(index.record_identifier()),
                &crate::writer::commit::NodeEdits {
                    property_replacements: vec![PropertyToWrite {
                        name: ":cnt".to_owned(),
                        property_type: crate::PropertyType::Long,
                        values: PropertyValuesToWrite::Single(value),
                    }],
                    property_removals: Vec::new(),
                    child_edits: crate::writer::commit::ChildEdits::new(),
                },
            )?
        }
    };

    let mut edits = crate::writer::commit::ChildEdits::new();
    edits.insert(
        crate::index::INDEX_CONTENT_NODE_NAME.to_owned(),
        Some(new_index),
    );
    built.rebuilt_record = crate::writer::commit::rewrite_node_with_child_edits(
        store,
        writer,
        Some(built.rebuilt_record),
        &edits,
    )?;
    Ok(())
}

/// Plan 0006's entry half over the subtree just written.
///
/// The budget is the collected entry count plus one: one more than the walk
/// produced, so a correct index is never cut short and an index that somehow
/// holds more entries than were collected exhausts the budget rather than
/// passing quietly.
fn verify_entries(
    store: &WritableRepository,
    definition: &RebuiltDefinition,
    state_root: RecordIdentifier,
    entries: u64,
) -> Result<()> {
    let definition_node = crate::content::node::NodeState::new(store, definition.rebuilt_record);
    let state_root = crate::content::node::NodeState::new(store, state_root);
    let model = crate::index::IndexDefinition::read(&definition_node, &definition.path)
        .map_err(index_error_to_store_error)?;
    let report = crate::index::property::consistency::check_entries(
        &definition_node,
        &model,
        &state_root,
        crate::index::property::consistency::EntryCheckBudget::of_entries(
            entries.saturating_add(1),
        ),
    )
    .map_err(index_error_to_store_error)?;
    // Every fault is definite here. `check`'s "missing" category, which is
    // the one an operator cannot attribute, is the covered-node half's, and
    // that half is not run.
    if report.has_definite_faults() {
        return Err(Error::InvalidFormat {
            details: format!(
                "the index just rebuilt at {} does not agree with the content it indexes: \
                 {} stale, {} mismatched and {} duplicate entries",
                definition.path,
                report.stale_entries.len(),
                report.mismatched_entries.len(),
                report.duplicate_entries.len(),
            ),
        });
    }
    Ok(())
}

/// The counter's arm, which no general checker can run.
///
/// Three claims. Every node of the mirror resolves in the state root, so the
/// counter describes content that exists. Every `:cnt` is exactly what task
/// 0705's `credited_by_path` recorded for that path, and the mirror holds a
/// node for every credited path and no others — the map is the independent
/// record of what the walk counted, and no walk of the written tree can
/// recover it. And every `:cnt` is at least the sum of its children's, which
/// is the one claim about the counting itself rather than its transcription:
/// a credit reaches a node and all of its ancestors, so a parent's total can
/// never fall below what its children hold.
fn verify_counter(
    store: &WritableRepository,
    definition: &RebuiltDefinition,
    state_root: RecordIdentifier,
    credited_by_path: &BTreeMap<String, i64>,
) -> Result<()> {
    let definition_node = crate::content::node::NodeState::new(store, definition.rebuilt_record);
    let Some(index) = definition_node.child_node(crate::index::INDEX_CONTENT_NODE_NAME)? else {
        // No `:index` is a real outcome, not an empty tree: nothing hit.
        return Ok(());
    };
    let state_root = crate::content::node::NodeState::new(store, state_root);
    let mut seen = 0usize;
    verify_counter_node(
        &index,
        &state_root,
        "",
        credited_by_path,
        &definition.path,
        &mut seen,
    )?;
    if seen != credited_by_path.len() {
        return Err(Error::InvalidFormat {
            details: format!(
                "the counter just rebuilt at {} holds {seen} counted nodes where the \
                 build credited {}",
                definition.path,
                credited_by_path.len(),
            ),
        });
    }
    Ok(())
}

/// One counter node and its descendants, counting the nodes it visits.
fn verify_counter_node(
    node: &crate::content::node::NodeState<'_>,
    state_root: &crate::content::node::NodeState<'_>,
    path: &str,
    credited_by_path: &BTreeMap<String, i64>,
    definition_path: &str,
    seen: &mut usize,
) -> Result<()> {
    let absolute = if path.is_empty() { "/" } else { path };
    *seen += 1;
    if resolve(state_root, path)?.is_none() {
        return Err(Error::InvalidFormat {
            details: format!(
                "the counter just rebuilt at {definition_path} counts {absolute}, \
                 which is not a node of the state it counted"
            ),
        });
    }

    let own = counter_value(node, absolute, definition_path)?;
    let Some(credited) = credited_by_path.get(absolute).copied() else {
        return Err(Error::InvalidFormat {
            details: format!(
                "the counter just rebuilt at {definition_path} counts {absolute}, \
                 which the build credited nothing"
            ),
        });
    };
    if own != credited {
        return Err(Error::InvalidFormat {
            details: format!(
                "the counter just rebuilt at {definition_path} holds {own} at \
                 {absolute} where the build credited it {credited}"
            ),
        });
    }

    let mut children_total = 0i64;
    for (name, child) in node.child_node_entries()? {
        let child_path = format!("{path}/{name}");
        children_total += counter_value(&child, &child_path, definition_path)?;
        verify_counter_node(
            &child,
            state_root,
            &child_path,
            credited_by_path,
            definition_path,
            seen,
        )?;
    }
    if own < children_total {
        return Err(Error::InvalidFormat {
            details: format!(
                "the counter just rebuilt at {definition_path} holds {own} at \
                 {absolute} where its children hold {children_total}, which a credit \
                 that reaches every ancestor cannot produce"
            ),
        });
    }
    Ok(())
}

/// One node's `:cnt`, which every node of a counter mirror carries.
fn counter_value(
    node: &crate::content::node::NodeState<'_>,
    path: &str,
    definition_path: &str,
) -> Result<i64> {
    let property = node.property(":cnt")?;
    let Some(value) = property
        .as_ref()
        .and_then(|property| crate::index::converting_long(Some(property)))
    else {
        return Err(Error::InvalidFormat {
            details: format!(
                "the counter just rebuilt at {definition_path} has no readable :cnt at \
                 {path}, which every node of a counter mirror carries"
            ),
        });
    };
    Ok(value)
}

/// A path resolved against a state root, where the empty path is the root.
fn resolve<'store>(
    state_root: &crate::content::node::NodeState<'store>,
    path: &str,
) -> Result<Option<crate::content::node::NodeState<'store>>> {
    let mut node = *state_root;
    for element in path.split('/').filter(|element| !element.is_empty()) {
        let Some(child) = node.child_node(element)? else {
            return Ok(None);
        };
        node = child;
    }
    Ok(Some(node))
}

/// The same verification after a fresh reopen, plus the head identity, the
/// spine that reaches each rebuilt definition, and the single journal line.
///
/// The reopen is what proves the run is durable rather than merely
/// consistent in the session that wrote it: every record is read again
/// through a repository opened from the directory, with none of the writing
/// session's caches.
pub(super) fn verify_after_reopen(
    directory: &Path,
    published: RecordIdentifier,
    rebuilt: &[RebuiltDefinition],
    journal_lines_before: usize,
) -> Result<()> {
    let journal_lines_after = crate::journal::read_journal(&directory.join("journal.log"))?.len();
    if journal_lines_after != journal_lines_before + 1 {
        return Err(Error::InvalidFormat {
            details: format!(
                "the reindex left {journal_lines_after} journal lines where it found \
                 {journal_lines_before}, and a run appends exactly one"
            ),
        });
    }

    let repository = crate::store::Repository::open(directory)?;
    if repository.head_record_identifier() != published {
        return Err(Error::InvalidFormat {
            details: format!(
                "the reopened store's head is {} rather than the {published} just \
                 published",
                repository.head_record_identifier()
            ),
        });
    }
    for definition in rebuilt {
        crate::tooling::check::verify_node_tree(&repository, definition.rebuilt_record)?;
        // The spine: the head has to *reach* the rebuilt definition, not
        // merely hold a readable copy of it somewhere in the store.
        let reached = repository
            .node_at_path(&definition.path)?
            .map(|node| node.record_identifier());
        if reached != Some(definition.rebuilt_record) {
            return Err(Error::InvalidFormat {
                details: match reached {
                    Some(record) => format!(
                        "the published head reaches {record} at {}, not the {} the run \
                         built",
                        definition.path, definition.rebuilt_record
                    ),
                    None => format!(
                        "the published head does not reach {} at all",
                        definition.path
                    ),
                },
            });
        }
    }
    Ok(())
}