refactor: Shift KmerIndex ownership to Arc for thread-safe sharing

Replaces lifetime-bound references with runtime reference counting across multiple crates. This enables safe concurrent access across parallel workers without explicit cloning or manual lifetime management. Introduces the `query` and `utils` CLI commands in obikmer2, along with supporting modules for batch processing, sparse indexing, sliding-window findere logic, and output formatting. Updates dependency manifests and aligns test suites with the new ownership model.
This commit is contained in:
Eric Coissac committed 2026-08-28 19:32:45 +02:00
1 parent 10a0909687
commit bd7729b095
31 files changed
+1689 -178

No files matched your search

+19 -13
View File
@@ -1,18 +1,23 @@
use std::collections::HashMap;
use std::sync::Arc;
use obikindex::{KmerIndex, layer::KmerLayer};
use obikseq::CanonicalKmer;
use crate::meta_cache::MetaCache;
pub struct IndexCache<'a> {
/// Ties this cache's lifetime to the `KmerIndex` it was opened from —
/// borrow-checker-enforced, not just documentation: an `IndexCache`
/// cannot outlive its index, so it can never be read against an index
/// that has since been mutated or dropped. `index()` is a genuine
/// accessor, but the field's real job is this lifetime bound, kept even
/// on paths that never call `index()`.
raw_index: &'a KmerIndex,
pub struct IndexCache {
/// Owns a strong reference to the `KmerIndex` it was opened from — the
/// cache cannot outlive its index (the `Arc` keeps it alive at least as
/// long as the cache itself, enforced at runtime by refcounting rather
/// than by a borrow-checked lifetime). `Arc`, not `&'a KmerIndex`,
/// because `IndexCache` must itself be `'static`-capable to be shared
/// across `obipipeline`'s `thread::spawn`-based workers (see
/// `obikquery::query_layer`) — a plain borrow can never satisfy that,
/// regardless of how it's threaded through. `index()` is a genuine
/// accessor, but the field's real job is this ownership guarantee, kept
/// even on paths that never call `index()`.
raw_index: Arc<KmerIndex>,
meta: MetaCache,
/// Keyed by each partition's own index number, not renumbered —
@@ -24,7 +29,7 @@ pub struct IndexCache<'a> {
layer_cache: HashMap<usize, Vec<KmerLayer>>,
}
impl<'a> IndexCache<'a> {
impl IndexCache {
/// Opens every layer of the given `partitions` (every partition of the
/// index, if `None`) once, up front, and keeps them alive for the
/// cache's lifetime — each partition's own layer count is read
@@ -41,7 +46,7 @@ impl<'a> IndexCache<'a> {
/// cache can only be built on partitions that actually exist and are
/// complete, so any failure here is a caller error, not a case to
/// design around with a `Result`.
pub fn new(index: &'a KmerIndex, partitions: Option<Vec<usize>>) -> Self {
pub fn new(index: Arc<KmerIndex>, partitions: Option<Vec<usize>>) -> Self {
let selected = partitions.unwrap_or_else(|| (0..index.n_partitions()).collect());
let mut layer_cache = HashMap::with_capacity(selected.len());
@@ -62,17 +67,18 @@ impl<'a> IndexCache<'a> {
layer_cache.insert(p, layers);
}
let meta = MetaCache::from_meta(&index.meta());
IndexCache {
raw_index: index,
meta: MetaCache::from_meta(&index.meta()),
meta,
layer_cache,
}
}
/// The index this cache was opened from.
#[inline]
pub fn index(&self) -> &'a KmerIndex {
self.raw_index
pub fn index(&self) -> &KmerIndex {
&self.raw_index
}
/// The index's metadata, snapshotted once at construction — see