Skip to content

Wave 4: AST query.rs code quality split and DRY helpers (A1/CX2-CX4) #287

Description

@dean0x

Deferred code quality and refactoring work from PR #284 (Wave 3f, #197). These are cosmetic but improve maintainability for Wave 4 feature work.

Refactoring Items

A1/CX2: Split query.rs into focused modules

File:Line: crates/rskim-search/src/query.rs (482 lines, 4 responsibilities)

Current file bundles: parsing, query engine dispatch, scoring, and adapter/trait logic. Split into:

  • query/parse.rsparse_*_query() functions
  • query/engine.rsrun_ngram_set() and dispatch logic (~73 lines)
  • query/scoring.rsscore_postings(), IDF cache, scoring helpers
  • query/adapter.rsAstPostingSource and trait implementations

This aligns with single-responsibility principle and reduces merge contention during #198/#283/#200 work.

CX3: Extract dedup+assert generic helper

File:Line: crates/rskim-search/src/query.rs:155,162

Duplicated block for bigram and trigram deduplication:

{
    set.sort_unstable_by_key(|p| p.bigram_key());
    set.dedup_by_key(|p| p.bigram_key());
    debug_assert!(!set.is_empty(), "bigram postings should never be empty");
}

Extract a generic dedup_by_ngram_key<F>(set: &mut Vec<T>, key_fn: F) -> Result<()> helper with the assert built-in.

CX4: Extract idf_for helper

File:Line: crates/rskim-search/src/query.rs:422

Flatten score_postings() nesting (current depth 4 → target depth 2) by extracting:

fn idf_for(cache: &mut IdfCache, lang: LangId, postings: &[AstPosting]) -> Result<f64>

This makes the scoring loop more readable without affecting logic.

Sequencing

Bundle with: #198 (multi-layer intersection), #283 (unigram posting index), #200 (composite ranking)
Rationale: Doing this refactor alongside Wave 4 feature work limits merge contention. Standalone refactor carries risk of conflicts.
North Star: #174 (AST Structural Query Engine)

Closes #284 (code quality items A1/CX2-CX4).

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    foundationFoundation infrastructuresearchCode search featuretech-debtTechnical debt backlog item

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions