ingraph 0.1.0

A declaration-driven indexing framework: records and nodes are written as Rust types, and the storage rows, the dataflow between them and the GraphQL query surface are emitted from the declaration.
Documentation
//! THE FAN AT THIS BACKEND — the field a lane is named after, the options it is built with, and the
//! equality that makes an unfanned corpus the corpus this store has always written.
//!
//! Beside [`stored_tests`](super::stored_tests) rather than inside it, on this file's own precedent:
//! one file per thing being proved. What a `lanes(…)` word MEANS in a declaration is
//! `ingraph-derive`'s; what an expansion writes is `ingraph-codegen`'s; that a declared roster
//! reaches a real index and answers is `tests/fulltext_lanes.rs`. Here there is one claim that
//! nothing else can make: **the schema an unfanned corpus gets is byte-for-byte the one this
//! backend built before the word existed.**

use tantivy::schema::{STORED, TEXT};

use super::{
  DEFAULT_ANALYZER, LaneAnalyzers, RESERVED_LANE_KEYS, SEARCHABLE_FIELD, lane_field, lane_options,
  open_unfanned,
};
use crate::LaneIdent;

/// **THE DEFAULT LANE IS TANTIVY'S OWN `TEXT`** — the equality the whole byte-identity claim rests
/// on.
///
/// `TEXT` is a constant this backend can no longer write, because the tokenizer name has to vary. So
/// the construction is spelled out by hand, and this is the pin that the hand-spelled one IS the
/// constant: fieldnorms on, positions recorded, not stored, not fast, tokenized by `default`.
///
/// Without it, a lane's options could drift a field away from the shape every existing corpus on
/// disk was written with — and the symptom would be `Index::open_or_create` refusing a directory
/// that was perfectly good.
#[test]
fn the_default_lane_is_tantivys_own_text() {
  assert_eq!(
    lane_options(DEFAULT_ANALYZER, false),
    TEXT,
    "an unfanned corpus's field is `TEXT`",
  );
  assert_eq!(
    lane_options(DEFAULT_ANALYZER, true),
    TEXT | STORED,
    "and `TEXT | STORED` where the declaration asked",
  );
}

/// **A NAMED ANALYZER IS THE ONLY THING THAT MOVES** — the same options, a different tokenizer.
#[test]
fn a_named_analyzer_changes_the_tokenizer_and_nothing_else() {
  let stemmed = lane_options("en_stem", false);
  assert_ne!(stemmed, TEXT, "the tokenizer name is part of the options");
  let indexing = stemmed
    .get_indexing_options()
    .expect("a lane field is indexed");
  assert_eq!(indexing.tokenizer(), "en_stem");
  assert!(indexing.fieldnorms(), "fieldnorms stay on");
  assert_eq!(
    indexing.index_option(),
    TEXT
      .get_indexing_options()
      .expect("`TEXT` is indexed")
      .index_option(),
    "positions are still recorded, so a phrase query still works",
  );
}

/// **THE UNDETERMINED LANE KEEPS `body`**, and every other lane is its own key.
///
/// This is the mapping the write path, the read path and the schema builder all spend, so it is
/// asserted once, here, rather than three times by accident.
#[test]
fn the_undetermined_lane_is_the_field_this_backend_always_had() {
  assert_eq!(lane_field(LaneIdent::UND), SEARCHABLE_FIELD);
  assert_eq!(lane_field(LaneIdent::new("zh")), "zh");
  assert_eq!(lane_field(LaneIdent::new("rest")), "rest");
}

/// **AN UNFANNED SLOT'S SCHEMA IS THE FOUR FIELDS IT ALWAYS WAS**, in the order it always had them.
#[test]
fn an_unfanned_slot_holds_exactly_the_four_fields() {
  let dir = tempfile::tempdir().expect("a temporary directory");
  let opened = open_unfanned(&dir.path().join("corpus"), false).expect("the slot opens");

  let held: Vec<String> = opened
    .index
    .schema()
    .fields()
    .map(|(_, entry)| entry.name().to_owned())
    .collect();

  assert_eq!(held, ["source", "identity", "key", "body"]);
  assert_eq!(
    opened.fields.searchable(),
    vec![opened.fields.body()],
    "one searchable field, and it is the body",
  );
}

/// **THE RESERVED KEYS ARE THIS SCHEMA'S OWN WORDS**, and `body` is among them because it is the
/// undetermined lane's field.
#[test]
fn the_reserved_keys_are_the_schemas_own_field_names() {
  assert_eq!(
    RESERVED_LANE_KEYS,
    ["source", "identity", "key", SEARCHABLE_FIELD]
  );
}

/// **THE TABLE ANSWERS WHAT IT WAS TOLD AND NOTHING ELSE** — and a lane named twice keeps the last
/// word, which is what a builder means.
#[test]
fn the_analyzer_table_answers_by_lane() {
  let knobs = LaneAnalyzers::new()
    .with("zh", "raw")
    .with("en", "en_stem")
    .with("zh", "whitespace");

  assert_eq!(knobs.analyzer("en"), Some("en_stem"));
  assert_eq!(
    knobs.analyzer("zh"),
    Some("whitespace"),
    "the last word for a lane is the one that counts",
  );
  assert_eq!(
    knobs.analyzer("rest"),
    None,
    "a lane the table says nothing about rides tantivy's own default",
  );
  assert_eq!(
    knobs.iter().collect::<Vec<_>>(),
    [("zh", "whitespace"), ("en", "en_stem")],
    "in the order the lanes were first named",
  );
}