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 map at the readings whose levels are narrower than their value — the two shipped position
//! citizens, each answering through the vocabulary it publishes beside its own sequence.

use core::num::NonZeroI32;

use crate::{
  ColumnKind, MediaTimeRange, MediaTimeRangeLeaf, MediaTimestamp, MediaTimestampLeaf,
  ToOrderLeaves, marker::CompositeMarker,
};

/// The instant the citizen's own storage note is written about: `900` ticks at `1/1000`, which is
/// `900_000_000` nanoseconds.
fn instant() -> MediaTimestamp {
  MediaTimestamp::new(mediatime::Timestamp::new(
    900,
    mediatime::Timebase::new(1, NonZeroI32::new(1000).expect("1000 is not zero")),
  ))
}

/// The `at`-th leaf of that instant, through the projection an emitted cursor is minted with.
fn leaf(at: usize) -> Option<MediaTimestampLeaf> {
  <CompositeMarker<MediaTimestamp> as ToOrderLeaves>::leaf(&instant(), at)
}

#[test]
fn a_citizen_answers_through_its_own_published_vocabulary() {
  // The SEGMENT numbering, not the level one: the permutation is spent before the vocabulary is
  // asked, so one numbering serves the tree, the citizen and the renderer.
  assert_eq!(leaf(0), Some(MediaTimestampLeaf::Pts(900)));
  assert_eq!(leaf(1), Some(MediaTimestampLeaf::TimebaseNum(1)));
  assert_eq!(leaf(2), Some(MediaTimestampLeaf::TimebaseDen(1000)));
  assert_eq!(leaf(3), Some(MediaTimestampLeaf::Nanos(900_000_000)));
}

#[test]
fn a_leaf_past_the_type_is_absent_rather_than_a_panic() {
  // The standing `ToCrossedOrderBy::level` takes one seam up: a renderer walking a term's own width
  // cannot reach it, and a sequence longer than its segments should surface as a missing boundary
  // rather than as a crash inside a statement.
  assert_eq!(leaf(4), None);
}

#[test]
fn the_derived_leaf_is_recomputed_and_absent_past_the_ceiling() {
  // The write binds `NULL` past the ±292-year ceiling and the boundary is the same answer, which is
  // what keeps a cursor the value the statement sorted by rather than a second reading of it.
  let far = MediaTimestamp::new(mediatime::Timestamp::new(
    i64::MAX,
    mediatime::Timebase::new(1, NonZeroI32::new(1).expect("1 is not zero")),
  ));
  let leaf = |at| <CompositeMarker<MediaTimestamp> as ToOrderLeaves>::leaf(&far, at);

  assert_eq!(
    leaf(3),
    None,
    "an instant with no representable nanosecond position has no boundary at its derived leaf"
  );
  assert_eq!(
    leaf(0),
    Some(MediaTimestampLeaf::Pts(i64::MAX)),
    "and its VALUE leaves are exact, which is the whole posture the split exists for"
  );
}

#[test]
fn the_vocabulary_is_as_wide_as_the_sequence_the_citizen_declares() {
  // The pairing `ColumnKind::ORDER`'s note states: a type that declares a sequence owes a leaf at
  // every level of it, because each level's slot is one leaf's value and a level that could not be
  // read would be a page with no boundary to resume at.
  for level in <MediaTimestamp as ColumnKind>::ORDER {
    assert!(
      leaf(level.leaf()).is_some(),
      "every declared level of a citizen has a leaf to compare"
    );
  }
}

/// The timebase halves a boundary carries are the REDUCED pair, which is what the columns hold.
///
/// A cursor carrying the DECLARED pair would compare against a value no column has — the miss the
/// storage note records for the write, read at one leaf.
#[test]
fn the_timebase_leaves_are_the_reduced_pair_the_columns_hold() {
  let declared = MediaTimestamp::new(mediatime::Timestamp::new(
    900,
    mediatime::Timebase::new(2, NonZeroI32::new(2000).expect("2000 is not zero")),
  ));
  let leaf = |at| <CompositeMarker<MediaTimestamp> as ToOrderLeaves>::leaf(&declared, at);

  assert_eq!(leaf(1), Some(MediaTimestampLeaf::TimebaseNum(1)));
  assert_eq!(leaf(2), Some(MediaTimestampLeaf::TimebaseDen(1000)));
}

/// The range the second citizen's storage note is written about: `[90000, 180000)` at `1/90000`,
/// which is one second to two.
fn span() -> MediaTimeRange {
  MediaTimeRange::new(mediatime::TimeRange::new(
    90_000,
    180_000,
    mediatime::Timebase::new(1, NonZeroI32::new(90_000).expect("90000 is not zero")),
  ))
}

/// The `at`-th leaf of that range, through the projection an emitted cursor is minted with.
fn span_leaf(at: usize) -> Option<MediaTimeRangeLeaf> {
  <CompositeMarker<MediaTimeRange> as ToOrderLeaves>::leaf(&span(), at)
}

/// **THE SECOND CITIZEN REACHES THE SAME SEAM**, which is the claim a per-citizen row could not make
/// on its own: this module holds no citizen's answer, so a type that joins the family is reached by
/// the ONE uniform spelling an emission writes per column.
#[test]
fn the_range_citizen_answers_through_its_own_published_vocabulary() {
  assert_eq!(span_leaf(0), Some(MediaTimeRangeLeaf::StartPts(90_000)));
  assert_eq!(span_leaf(1), Some(MediaTimeRangeLeaf::EndPts(180_000)));
  assert_eq!(span_leaf(2), Some(MediaTimeRangeLeaf::TimebaseNum(1)));
  assert_eq!(span_leaf(3), Some(MediaTimeRangeLeaf::TimebaseDen(90_000)));
  assert_eq!(
    span_leaf(4),
    Some(MediaTimeRangeLeaf::StartNanos(1_000_000_000))
  );
  assert_eq!(
    span_leaf(5),
    Some(MediaTimeRangeLeaf::EndNanos(2_000_000_000))
  );
  assert_eq!(span_leaf(6), None);
}

/// **And its sequence is as wide as its vocabulary**, the pairing read at the second citizen.
#[test]
fn the_range_vocabulary_is_as_wide_as_the_sequence_it_declares() {
  for level in <MediaTimeRange as ColumnKind>::ORDER {
    assert!(
      span_leaf(level.leaf()).is_some(),
      "every declared level of a citizen has a leaf to compare"
    );
  }
}