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 THREE INTERVAL WORDS AS A FILTER DOCUMENT** — and the assertion is the DOCUMENT, because
//! an operator count cannot tell `overlaps` from `within`.

use bson::{Document, doc};

use super::{MongoTimeRangeComparison, MongoTimeRangedComparison};
use crate::{
  At, ComplexComparison, CompositeComparison, FieldName, Mongo, MongoCarrier, RecordAddress,
  ScalarFilter, TimeRange, TimeRangeFilter, TimeRanged, TimeRangedFilter, Timestamp, TryFromFilter,
  comparison::mongo::{MongoConjunction, MongoScalarComparison},
};

/// The row every address below names. Nothing here is correlated, so no test reads it.
const ROW: RecordAddress = RecordAddress::new(FieldName::new("media"), &[FieldName::new("id")]);

fn at(second: i64) -> Timestamp {
  Timestamp::from_parts(second, 0).expect("an instant")
}

/// The window every assertion below asks about — `[10, 20]`.
fn window() -> TimeRange {
  TimeRange::new(at(10), at(20)).expect("a stretch")
}

/// One instant as the sub-document a seat is compared against — the INSTANT's own row, so no test
/// here spells `secs` or `nanos`.
fn instant(second: i64) -> bson::Bson {
  at(second).to_bson()
}

/// Lower one comparison at one field of [`ROW`] and finish the filter.
fn rendered<C: ComplexComparison<Mongo>>(comparison: &C, field: &str) -> Document {
  let frame = At::new(&ROW).frame(FieldName::new(field));
  comparison
    .apply(frame.at(), MongoConjunction::new())
    .expect("a MongoDB lowering cannot refuse")
    .into_document()
}

fn crossed(filter: TimeRangeFilter) -> MongoTimeRangeComparison {
  MongoTimeRangeComparison::try_from_filter(filter).unwrap_or_else(|infallible| match infallible {})
}

/// One range filter as the document a `span` field is matched by.
fn document(filter: TimeRangeFilter) -> Document {
  rendered(&crossed(filter), "span")
}

/// **AN EMPTY FILTER RENDERS AN EMPTY DOCUMENT.**
#[test]
fn an_empty_filter_asks_nothing() {
  let comparison = crossed(TimeRangeFilter::new());

  assert_eq!(ComplexComparison::<Mongo>::conjuncts(&comparison), 0);
  assert_eq!(document(TimeRangeFilter::new()), Document::new());
}

/// **`overlaps` IS THE CROSSED PAIR AND STRICT ON BOTH SIDES** — the `start` key against the
/// operand's END under `$lt`, and the `end` key against the operand's START under `$gt`.
///
/// The operators are where the half-openness lives on this backend: `$lte`/`$gte` would be the
/// CLOSED reading and would report two abutting stretches as meeting.
///
/// The seats are compared WHOLE, as sub-documents: BSON's own field-by-field order over the
/// instant's two keys IS the instant's order, which is the fact
/// [`MongoCarrier`](crate::MongoCarrier)'s instant row states and this row is the first to spend.
#[test]
fn overlaps_compares_each_seat_against_the_other_end() {
  assert_eq!(
    document(TimeRangeFilter::new().with_overlaps(window())),
    doc! {
      "span.start": { "$lt": instant(20) },
      "span.end": { "$gt": instant(10) },
    }
  );
  assert_eq!(
    ComplexComparison::<Mongo>::conjuncts(&crossed(TimeRangeFilter::new().with_overlaps(window()))),
    2
  );
}

/// **`contains` A SPAN COVERS, and NEITHER operator is strict** — like boundaries are contained, so
/// `[0, 10)` contains `[0, 10)`.
#[test]
fn contains_a_span_puts_the_operand_inside_the_field() {
  assert_eq!(
    document(TimeRangeFilter::new().with_contains(window())),
    doc! {
      "span.start": { "$lte": instant(10) },
      "span.end": { "$gte": instant(20) },
    }
  );
}

/// **`contains` AN INSTANT IS MEMBERSHIP, and it is the ONE MIXED ROW** — `$lte` at the start and
/// `$gt` at the end, because the open end refuses an instant sitting on it.
#[test]
fn contains_an_instant_is_half_open_membership() {
  assert_eq!(
    document(TimeRangeFilter::new().with_contains(at(15))),
    doc! {
      "span.start": { "$lte": instant(15) },
      "span.end": { "$gt": instant(15) },
    }
  );
}

/// **`within` IS `contains` REVERSED**, and here the OPERATORS are what differ.
#[test]
fn within_puts_the_field_inside_the_operand() {
  assert_eq!(
    document(TimeRangeFilter::new().with_within(window())),
    doc! {
      "span.start": { "$gte": instant(10) },
      "span.end": { "$lte": instant(20) },
    }
  );
}

/// **A DEGENERATE SPAN OPERAND IS NOT THE INSTANT SHAPE**, and the END key's operator is what says
/// so: the same `[t, t)` reaches `$gte` through the span arm where a position reaches `$gt`.
#[test]
fn a_degenerate_span_operand_is_not_the_instant_arm() {
  assert_eq!(
    document(TimeRangeFilter::new().with_contains(TimeRange::instant(at(15)))),
    doc! {
      "span.start": { "$lte": instant(15) },
      "span.end": { "$gte": instant(15) },
    }
  );
  assert_ne!(
    document(TimeRangeFilter::new().with_contains(TimeRange::instant(at(15)))),
    document(TimeRangeFilter::new().with_contains(at(15)))
  );
}

/// **THE TWO ENDS TAKE THE INSTANT'S OWN WORDS**, merged under the same two keys the interval words
/// use — one key per seat, however many operators reach it.
#[test]
fn the_component_filters_merge_under_the_same_keys() {
  assert_eq!(
    document(
      TimeRangeFilter::new()
        .with_start(ScalarFilter::new().with_gte(at(5)))
        .with_end(ScalarFilter::new().with_lt(at(30))),
    ),
    doc! {
      "span.start": { "$gte": instant(5) },
      "span.end": { "$lt": instant(30) },
    }
  );
}

/// **AN INTERVAL WORD AND A COMPONENT FILTER MERGE INTO ONE KEY**, which is what a BSON leaf does
/// where a SQL one opens a second conjunct.
#[test]
fn a_word_and_a_component_share_the_seats_key() {
  assert_eq!(
    document(
      TimeRangeFilter::new()
        .with_overlaps(window())
        .with_start(ScalarFilter::new().with_gte(at(5))),
    ),
    doc! {
      "span.start": { "$lt": instant(20), "$gte": instant(5) },
      "span.end": { "$gt": instant(10) },
    }
  );
}

/// **A REPEATED OPERATOR LANDS IN THE OVERFLOW**, which is the accumulator's own answer to a key
/// that cannot hold two of one word — and it is the same `AND` the SQL family gets from two
/// conjuncts.
///
/// **HALF-OPENNESS MOVED WHICH PAIR COLLIDES**, and that is worth recording: under the closed
/// reading `overlaps` and `contains` shared BOTH operators, and now they share exactly one. The
/// collision is `$gt` at the END key — `overlaps` writes it against the operand's start and
/// `contains` AN INSTANT writes it against the position — so asking both is the case that separates
/// a merge from a silent overwrite.
///
/// The first arrival merges under the key and the second lands in the `$and` overflow, which is the
/// same conjunction two SQL conjuncts give.
#[test]
fn two_words_wanting_one_operator_do_not_overwrite_each_other() {
  let both = document(
    TimeRangeFilter::new()
      .with_overlaps(window())
      .with_contains(at(15)),
  );

  assert_eq!(
    both,
    doc! {
      "span.start": { "$lt": instant(20), "$lte": instant(15) },
      "span.end": { "$gt": instant(10) },
      "$and": [ { "span.end": { "$gt": instant(15) } } ],
    }
  );
}

/// **THE WIDTH IS THE STORAGE MODULE'S**, read rather than restated — the same four leaves the SQL
/// twin states, which this family reaches with `.` instead of `_`.
#[test]
fn the_comparison_states_the_fields_the_type_spans() {
  const SEGMENTS: &[crate::ColumnShape] =
    <MongoTimeRangeComparison as CompositeComparison<Mongo>>::SEGMENTS;

  assert_eq!(SEGMENTS, <TimeRange as crate::ColumnKind>::SEGMENTS);
}

/// **THE TIMED PAIR: the stretch's keys SPLICED, and the payload one key below** — the same
/// flattening the columns take, in this family's dotted spelling.
#[test]
fn a_timed_payload_splices_the_stretch_and_names_the_payload() {
  type Timed = MongoTimeRangedComparison<i64, MongoScalarComparison<i64>>;

  let comparison = Timed::try_from_filter(
    TimeRangedFilter::new()
      .with_range(TimeRangeFilter::new().with_overlaps(window()))
      .with_data(ScalarFilter::<i64>::new().with_eq(7i64)),
  )
  .expect("the crossing");

  assert_eq!(
    rendered(&comparison, "heard"),
    doc! {
      "heard.start": { "$lt": instant(20) },
      "heard.end": { "$gt": instant(10) },
      "heard.data": { "$eq": 7_i64 },
    }
  );
  assert_eq!(ComplexComparison::<Mongo>::conjuncts(&comparison), 3);
  assert_eq!(
    <Timed as CompositeComparison<Mongo>>::SEGMENTS,
    <TimeRanged<i64> as crate::ColumnKind>::SEGMENTS
  );
}