feat: add convert command for in-place index evidence modification

Introduce a new CLI command that modifies existing indices to support exact, approximate, or hybrid evidence modes. This change enables the reindex module in obikrebuild, exposing an IndexReindex trait for evidence persistence and layer processing. It also refactors obikindexer to publicly expose an IndexBuilder trait for post-build evidence construction, while adding a set_evidence API in obikindex to safely update configuration fields without altering core parameters.
This commit is contained in:
Eric Coissac committed 2026-08-28 19:43:14 +02:00
1 parent bd7729b095
commit 4ea3cd32ba
10 files changed
+562 -340

No files matched your search

+5 -7
View File
@@ -6,12 +6,9 @@
//!
//! [`compact_layer`] collapses a partition's accumulated layers (the
//! stopgap `obikmerge` uses to add genomes without a full rebuild) back
//! into one, in place. `reindex` (reindexing an existing index's evidence
//! representation in place — `Exact`/`Approx`/`Hybrid`) is temporarily
//! disabled: it predates this session's refactor and still reaches
//! `obikindex` internals (`root_path`/`meta` fields, `index_dir`/
//! `layer_dir` methods) that are no longer accessible from outside that
//! crate. Fixing it is separate, future work.
//! into one, in place. [`reindex`] converts an existing index's evidence
//! representation in place (`Exact`/`Approx`/`Hybrid`), delegating the
//! actual per-layer construction to `obikindexer::IndexBuilder`.
//!
//! Removing k-mers (filtering) and repacking matrix files are *not* here:
//! the former lives in `obikfilter::Filter` (a fresh index at a new
@@ -21,6 +18,7 @@
//! cannot depend back on this crate.
mod compact_layer;
// mod reindex; // disabled — see module doc above.
mod reindex;
pub use compact_layer::IndexCompact;
pub use reindex::IndexReindex;
+62 -65
View File
@@ -1,43 +1,52 @@
use obikindex::IndexBuilder;
use obikindex::IndexMeta;
use obikindex::layer::{IndexMode, TypedLayer};
use obisys::{Reporter, Stage, progress_bar};
use std::fs;
use std::path::Path;
use std::sync::Arc;
//! Convert an existing index's evidence representation in place —
//! `Exact`/`Approx`/`Hybrid`. `unitigs.bin`/`mphf.bin` are never touched;
//! only the evidence bundle (`evidence.bin`/`unitigs.bin.idx` and/or
//! `fingerprint.bin`) is — the actual per-layer construction is
//! `obikindexer::IndexBuilder`'s job, this module only orchestrates it
//! across every partition/layer and updates `index.meta` on success.
//!
//! `KmerIndex` is foreign to this crate, so this is an extension trait,
//! same reasoning as `IndexCompact`/`IndexDump`/`IndexUnitigs`.
use obikindex::layer::{IndexMode, KmerLayer};
use obikindex::{IndexState, KmerIndex, OKIError, OKIResult};
use obikindexer::IndexBuilder;
use obisys::{PartitionRunner, Reporter, Stage, progress_bar};
use tracing::info;
use obikindex::{OKIError, OKIResult};
use obikindex::KmerIndex;
use obikindex::IndexState;
use obisys::PartitionRunner;
const EVIDENCE_FILE: &str = "evidence.bin";
const FINGERPRINT_FILE: &str = "fingerprint.bin";
/// `unitigs.bin.idx`'s filename isn't exposed by `obikindex`/`obiskio` as a
/// path accessor (it's a private implementation detail of exact-evidence
/// construction) — mirrored here as a literal, same as before this module
/// was disabled.
const UNITIG_IDX_FILE: &str = "unitigs.bin.idx";
impl KmerIndex {
/// Convert every layer's evidence bundle to `target` in-place.
pub trait IndexReindex {
/// Convert every layer's evidence bundle to `target`, in place.
///
/// - `Exact` → builds `evidence.bin` + `unitigs.bin.idx`, removes `fingerprint.bin`
/// - `Approx` → builds `fingerprint.bin`, removes `evidence.bin` + `unitigs.bin.idx`
/// - `Exact` → builds `evidence.bin` + `unitigs.bin.idx` where missing, removes `fingerprint.bin`.
/// - `Approx` → builds `fingerprint.bin` where missing or built with a different `b`, removes `evidence.bin` + `unitigs.bin.idx`.
/// - `Hybrid` → builds whichever bundle a layer is still missing, keeps both.
///
/// The MPHF (`mphf.bin`) and unitigs (`unitigs.bin`) are never touched.
/// `index.meta` is updated with the new evidence kind on success.
pub fn reindex(
&mut self,
target: IndexMode,
block_bits: u8,
rep: &mut Reporter,
) -> OKIResult<()> {
if self.state()? != IndexState::Indexed {
return Err(OKIError::NotIndexed(self.root_path.clone()));
/// Each layer's own build step is idempotent (see `IndexBuilder`), so
/// re-running this with the same `target` on an already-converted
/// index is a cheap no-op per layer, not a full rebuild.
fn reindex(&self, target: IndexMode, block_bits: u8, rep: &mut Reporter) -> OKIResult<()>;
}
impl IndexReindex for KmerIndex {
fn reindex(&self, target: IndexMode, block_bits: u8, rep: &mut Reporter) -> OKIResult<()> {
if self.meta().state().map_err(OKIError::Io)? != IndexState::Indexed {
return Err(OKIError::InvalidInput(format!(
"{}: index is not fully built",
self.dir().display()
)));
}
let n = self.n_partitions();
info!(
"reindex {} partition(s): {:?} → {:?}",
n, self.meta.config.evidence, target,
"reindex {} partition(s): {:?} → {target:?}",
n,
self.meta().config().evidence,
);
let t = Stage::start("reindex");
@@ -58,68 +67,56 @@ impl KmerIndex {
pb.finish_and_clear();
let mut config = self.meta.config.clone();
config.evidence = target;
if matches!(config.evidence, IndexMode::Exact) {
config.block_bits = block_bits;
}
let genomes = self.meta.genomes().map_err(OKIError::Io)?;
self.meta.rewrite_config(config, genomes).map_err(OKIError::Io)?;
self.meta = Arc::new(IndexMeta::open(self).map_err(OKIError::Io)?);
self.meta().set_evidence(target, block_bits).map_err(OKIError::Io)?;
rep.push(t.stop());
Ok(())
}
}
/// Process all layers of one partition's index directory.
fn reindex_partition(
index: &KmerIndex,
i: usize,
target: &IndexMode,
block_bits: u8,
) -> OKIResult<()> {
if !index.index_dir(i).exists() {
return Ok(());
}
let n_layers = index
.n_layers(i)
.map_err(|e| OKIError::InvalidInput(e.to_string()))?;
for layer_idx in 0..n_layers {
reindex_layer(&index.layer_dir(i, layer_idx), target, block_bits)?;
/// Process every layer of partition `i`.
fn reindex_partition(index: &KmerIndex, i: usize, target: &IndexMode, block_bits: u8) -> OKIResult<()> {
let partition = index.partition(i)?;
let n_layers = partition.n_layers();
for l in 0..n_layers {
let layer = partition.layer(l)?.open()?;
reindex_layer(&layer, target, block_bits)?;
remove_stale_evidence(&layer, target);
}
Ok(())
}
fn reindex_layer(layer_dir: &Path, target: &IndexMode, block_bits: u8) -> OKIResult<()> {
fn reindex_layer(layer: &KmerLayer, target: &IndexMode, block_bits: u8) -> OKIResult<()> {
match target {
IndexMode::Exact => {
TypedLayer::<()>::build_exact_evidence(layer_dir, block_bits)?;
layer.build_exact_evidence(block_bits)?;
}
IndexMode::Approx { b, z } | IndexMode::Hybrid { b, z } => {
TypedLayer::<()>::build_approx_evidence(layer_dir, *b, *z)?;
IndexMode::Approx { b, z } => {
layer.build_approx_evidence(*b, *z)?;
}
IndexMode::Hybrid { b, z } => {
layer.build_hybrid_evidence(*b, *z, block_bits)?;
}
}
remove_stale_evidence(layer_dir, target)
Ok(())
}
fn remove_stale_evidence(layer_dir: &Path, target: &IndexMode) -> OKIResult<()> {
fn remove_stale_evidence(layer: &KmerLayer, target: &IndexMode) {
match target {
IndexMode::Exact => {
remove_if_exists(&layer_dir.join(FINGERPRINT_FILE));
remove_if_exists(&layer.fingerprint_path());
}
IndexMode::Approx { .. } => {
remove_if_exists(&layer_dir.join(EVIDENCE_FILE));
remove_if_exists(&layer_dir.join(UNITIG_IDX_FILE));
remove_if_exists(&layer.evidence_path());
remove_if_exists(&layer.dir().join(UNITIG_IDX_FILE));
}
IndexMode::Hybrid { .. } => {
// both bundles kept — nothing to remove
}
}
Ok(())
}
fn remove_if_exists(path: &Path) {
if let Err(e) = fs::remove_file(path) {
fn remove_if_exists(path: &std::path::Path) {
if let Err(e) = std::fs::remove_file(path) {
if e.kind() != std::io::ErrorKind::NotFound {
eprintln!("warning: could not remove {}: {e}", path.display());
}