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
//! What layer two's shipped machine owes: it CREATES the corpora a blueprint names, at the posture
//! the declaration stated, and creating one twice is not a second act.
//!
//! The mount is hand-written here — a `#[ingraph::resource(tantivy)]` declaration is what emits one,
//! and this crate cannot expand its own macros — so what is under test is the visitor and the door
//! it calls, with the declaration's half spelled out.
use crate::Resources;

use std::path::PathBuf;

use smol_bytes::Utf8Bytes;

use super::{TantivyFulltext, TantivyFulltextError, TantivySlotMaker, TantivyStore};
use crate::{
  AsyncResource, Describe, FulltextMount, FulltextStore, Habitat, Shared, SlotColumn, SlotName,
  SlotSchema, TypeVisitor,
};

/// THE MOUNT — what the kind word emits: a store opened once, and the mode-blind seam over it.
struct Ft {
  store: TantivyStore,
}

impl AsyncResource for Ft {
  type Options = PathBuf;
  type Error = TantivyFulltextError;

  async fn build(_ctx: &Resources, options: Self::Options) -> Result<Self, Self::Error> {
    <TantivyStore as FulltextStore>::open(options).map(|store| Self { store })
  }
}

impl Habitat for Ft {
  type Blueprint = &'static [SlotSchema];
}

impl FulltextMount for Ft {
  type Handle = TantivyFulltext;

  fn corpus(&self, slot: impl Into<Utf8Bytes>) -> Self::Handle {
    <TantivyStore as FulltextStore>::fulltext(&self.store, slot)
  }

  fn connection(&self) -> &TantivyStore {
    &self.store
  }
}

/// THE DECLARATION — two elections pointing at that mount, one of them keeping its text.
struct Media;

impl Describe<Ft> for Media {
  const BLUEPRINT: &'static [SlotSchema] = &[
    SlotSchema::new(
      SlotName::new("media__title__lexical"),
      &[SlotColumn::text("title")],
    ),
    SlotSchema::new(
      SlotName::new("media__body__lexical"),
      &[SlotColumn::text("body")],
    )
    .keeping(),
  ];
}

/// A second declaration in the same mount, so the walk's ORDER is observable.
struct Caption;

impl Describe<Ft> for Caption {
  const BLUEPRINT: &'static [SlotSchema] = &[SlotSchema::new(
    SlotName::new("caption__text__lexical"),
    &[SlotColumn::text("text")],
  )];
}

async fn mounted(root: PathBuf) -> Shared<Ft> {
  Shared::<Ft>::build(&Resources::new(), root)
    .await
    .expect("the mount opens")
}

/// **THE WALK CREATES.** Every corpus the blueprints name exists on disk when the walk returns, and
/// nothing else does — which is the whole of what layer two is for.
#[tokio::test]
async fn the_walk_creates_every_corpus_its_blueprints_name() {
  let home = tempfile::tempdir().expect("a temporary directory");
  let ft = mounted(home.path().to_path_buf()).await;

  let mut maker = TantivySlotMaker::new(ft);
  maker.next_type::<Media>().expect("the corpora are made");
  maker.next_type::<Caption>().expect("the corpus is made");

  assert_eq!(
    maker.made(),
    [
      "media__title__lexical",
      "media__body__lexical",
      "caption__text__lexical"
    ],
    "the walk creates in blueprint order, declaration by declaration",
  );
  for slot in maker.made() {
    assert!(
      home.path().join(slot.as_str()).is_dir(),
      "`{slot}` has no directory, so nothing was created",
    );
  }
}

/// **THE POSTURE IS THE DECLARATION'S.** A corpus whose blueprint kept its text is created keeping
/// it, which is what makes the walk's creation and a later write agree about the on-disk schema —
/// `Index::open_or_create` refuses a directory whose schema differs, so a disagreement is a launch
/// that runs and a write that cannot.
#[tokio::test]
async fn a_kept_corpus_is_created_keeping_its_text() {
  let home = tempfile::tempdir().expect("a temporary directory");
  let ft = mounted(home.path().to_path_buf()).await;
  let store: &TantivyStore = FulltextMount::connection(ft.get());

  let mut maker = TantivySlotMaker::new(ft.clone());
  maker.next_type::<Media>().expect("the corpora are made");

  // The WRITE side asks for the same posture through its own door. A slot created at the other one
  // is tantivy's own loud refusal here, so this call succeeding IS the agreement.
  store
    .writer_with(
      "media__body__lexical",
      TantivyStore::MIN_WRITER_HEAP_BYTES,
      true,
    )
    .expect("the kept corpus reopens at the posture the walk created it under");
  store
    .writer_with(
      "media__title__lexical",
      TantivyStore::MIN_WRITER_HEAP_BYTES,
      false,
    )
    .expect("the indexed corpus reopens at its own posture");
}

/// **CREATING IS IDEMPOTENT.** A second launch over a live index root makes nothing new and refuses
/// nothing — `Index::open_or_create` opens what is there — which is what lets the walk run on every
/// boot rather than only the first.
#[tokio::test]
async fn a_second_walk_over_a_live_root_refuses_nothing() {
  let home = tempfile::tempdir().expect("a temporary directory");

  {
    let ft = mounted(home.path().to_path_buf()).await;
    let mut first = TantivySlotMaker::new(ft);
    first.next_type::<Media>().expect("the corpora are made");
  }

  let ft = mounted(home.path().to_path_buf()).await;
  let mut second = TantivySlotMaker::new(ft);
  second
    .next_type::<Media>()
    .expect("a second boot over a live root opens what is already there");
  assert_eq!(second.made().len(), 2);
}

/// **THE KNOB TABLE IS KEYED BY CORPUS**, and its value type is the engine's own
/// [`Options`](crate::FulltextEngine::Options) seat — the projection that makes the seat a position
/// rather than a reservation. The seat is `()` at this backend, so what is asserted is the SHAPE:
/// a value written under a corpus name reaches `create_slot` for that corpus and no other.
#[tokio::test]
async fn the_knob_table_is_the_options_seats_projection() {
  let home = tempfile::tempdir().expect("a temporary directory");
  let ft = mounted(home.path().to_path_buf()).await;

  let knobs: <TantivyFulltext as crate::FulltextEngine>::Options =
    crate::store::tantivy::LaneAnalyzers::new();
  let mut maker = TantivySlotMaker::new(ft).with_knobs("media__title__lexical", knobs);
  maker.next_type::<Media>().expect("the corpora are made");

  assert_eq!(maker.made().len(), 2, "both corpora are still made");
}