This change replaces the HashMap-based k-mer storage mechanism with a compact, flat array structure for `PartitionKmers`. This significantly reduces memory usage per partition and improves query execution time. It also updates the batch processing logic to handle the new data structure, introduces detailed memory footprint calculations, and updates relevant query layer APIs. Test coverage has been expanded to validate correct partitioning and k-mer ordering.
103 lines
3.8 KiB
Rust
103 lines
3.8 KiB
Rust
use super::*;
|
|
use obikidxcache::index_cache::IndexCache;
|
|
use obikindex::{IndexConfig, KmerIndex};
|
|
use std::sync::Arc;
|
|
|
|
// ── QueryStats::AddAssign ───────────────────────────────────────────────────
|
|
|
|
#[test]
|
|
fn query_stats_add_assign_sums_fields() {
|
|
let mut total = QueryStats {
|
|
n_unique_kmers: 3,
|
|
n_mphf_calls: 5,
|
|
n_hits: 2,
|
|
n_columns_scanned: 1,
|
|
n_col_get_calls: 7,
|
|
};
|
|
total += QueryStats {
|
|
n_unique_kmers: 1,
|
|
n_mphf_calls: 4,
|
|
n_hits: 1,
|
|
n_columns_scanned: 2,
|
|
n_col_get_calls: 3,
|
|
};
|
|
assert_eq!(total.n_unique_kmers, 4);
|
|
assert_eq!(total.n_mphf_calls, 9);
|
|
assert_eq!(total.n_hits, 3);
|
|
assert_eq!(total.n_columns_scanned, 3);
|
|
assert_eq!(total.n_col_get_calls, 10);
|
|
}
|
|
|
|
#[test]
|
|
fn query_stats_default_is_zero() {
|
|
let s = QueryStats::default();
|
|
assert_eq!(s.n_unique_kmers, 0);
|
|
assert_eq!(s.n_mphf_calls, 0);
|
|
assert_eq!(s.n_hits, 0);
|
|
assert_eq!(s.n_columns_scanned, 0);
|
|
assert_eq!(s.n_col_get_calls, 0);
|
|
}
|
|
|
|
// ── query_partition_with on a not-yet-indexed partition ─────────────────────
|
|
|
|
/// A `KmerPartition` created but never taken through `build_layers` has no
|
|
/// `index/` subdirectory under any partition — `IndexCache::new` still opens
|
|
/// fine on it (zero layers found), and `query_partition_with` must recognise
|
|
/// this and return default (all-zero) stats rather than panicking, exactly
|
|
/// like an empty `kmers` map.
|
|
#[test]
|
|
fn query_partition_with_missing_index_dir_returns_default_stats() {
|
|
let tmp = tempfile::tempdir().expect("tempdir");
|
|
// Same k/m pair as `batch`'s tests (11, 5) — `obikseq`'s global k/m
|
|
// params are process-wide atomics in test builds (see
|
|
// `obikseq::params`'s own doc comment), shared by every test in this
|
|
// crate's single test binary regardless of thread; a different pair
|
|
// here would race with `batch`'s tests running concurrently.
|
|
let config = IndexConfig {
|
|
kmer_size: 11,
|
|
minimizer_size: 5,
|
|
n_bits: 2,
|
|
with_counts: false,
|
|
evidence: obikindex::layer::IndexMode::Exact,
|
|
block_bits: 0,
|
|
};
|
|
let index = KmerIndex::create(tmp.path().join("idx"), config, None).expect("create index");
|
|
let cache = IndexCache::new(Arc::new(index), Some(vec![0]));
|
|
|
|
// Any well-formed canonical k-mer works here — the call must return
|
|
// before ever attempting an MPHF lookup, since the partition has zero
|
|
// layers.
|
|
let kmer = CanonicalKmer::from_raw_unchecked(0u64);
|
|
let kmers = PartitionKmers::from_occurrences(vec![(kmer, KmerDesc { seq_idx: 0, pos: 0 })]);
|
|
|
|
let stats = cache.query_partition_with(0, &kmers, 1, |_event| {
|
|
panic!("on_event must not be called: no index was built");
|
|
});
|
|
|
|
assert_eq!(stats, QueryStats::default());
|
|
}
|
|
|
|
#[test]
|
|
fn query_partition_with_empty_kmers_is_a_noop() {
|
|
let tmp = tempfile::tempdir().expect("tempdir");
|
|
// Same k/m pair as `batch`'s tests — see the comment in the previous
|
|
// test for why this must match across every test in this crate.
|
|
let config = IndexConfig {
|
|
kmer_size: 11,
|
|
minimizer_size: 5,
|
|
n_bits: 2,
|
|
with_counts: false,
|
|
evidence: obikindex::layer::IndexMode::Exact,
|
|
block_bits: 0,
|
|
};
|
|
let index = KmerIndex::create(tmp.path().join("idx"), config, None).expect("create index");
|
|
let cache = IndexCache::new(Arc::new(index), Some(vec![0]));
|
|
|
|
let kmers = PartitionKmers::empty();
|
|
let stats = cache.query_partition_with(0, &kmers, 1, |_event| {
|
|
panic!("on_event must not be called on an empty kmer map");
|
|
});
|
|
|
|
assert_eq!(stats, QueryStats::default());
|
|
}
|