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
//! **A collection of COMPOSITE elements on MongoDB is a native SUBDOCUMENT ARRAY, and this backend
//! needed nothing to get one.**
//!
//! ```text
//! SQL       take_stamps: (id, stamps_ordinal, stamps_secs, stamps_nanos)   a second table
//! MongoDB   { stamps: [ { secs: 10, nanos: 250 }, … ] }                    the document itself
//! ```
//!
//! # There is no strategy here, and a composite element does not create one
//!
//! The SQL family splits four ways over two questions this backend never asks — *can this dialect
//! keep a collection in a column* and *can this element type live in an array* — and a composite
//! element is the case where both answers finally diverge on the SQL side: an array of two columns is
//! not a thing any dialect has, so every one of them takes the edge table. MongoDB's answer does not
//! move, because the operand and the stored value are the same kind of value whatever that value is:
//! [`MongoCarrier`](crate::MongoCarrier) for the framework's own instant already returns a
//! `Bson::Document`, so an ARRAY of them is what `membership` builds with no widening at all.
//!
//! That is the whole finding, and it is worth a file rather than a sentence: the two kinds here take
//! `V: MongoCarrier` and nothing about a WIDTH, so what the SQL family needed a second comparison
//! type, a second scope and a bind ceiling for is on this backend a row that was already written.
//!
//! # The SEGMENT NAMES are the same ones the SQL columns are joined from
//!
//! `secs` and `nanos` are read off the instant's own module in both families — the SQL side joins
//! them under the field and this side nests them under it — so a rename moves `stamps_secs`,
//! `{ stamps: { secs } }` and the element scope's `value_secs` together. The sub-document is where
//! the `flatten` refusal's asymmetry shows: a family that FLATTENS a composite joins the names, and a
//! family that NESTS one does not.
//!
//! # An element whose OWN PART is composite needs nothing here either, and the census says why
//!
//! The SQL side widened for one: its edge table writes a column per LEAF, so a part that lends no
//! name splices its leaves into the element's own namespace and every party there had to descend.
//! This backend reads no width at all — the two kinds take `V: MongoCarrier` and nothing else — so a
//! nested element is whatever its own carrier row says one document is, and `DetectedLanguage`'s row
//! already writes the identity's four keys beside the confidence at ONE level, for exactly the reason
//! the SQL side joins those four without a prefix.
//!
//! What is missing for a `Vec<DetectedLanguage>` on this backend is therefore not the collection: it
//! is the ELEMENT's own comparison. There is no `MongoDetectedLanguageComparison` and no
//! `MongoLanguageIdComparison` — see that family's SQL crossing, whose `Db: sqlx::Database` bound is
//! written to leave room for one — so the `any` slot has nothing to hold. That is a family half
//! rather than a line, and it is recorded rather than started beside a naming fix.

use bson::{Bson, Document, doc};

use super::super::{MongoListComparison, MongoSetComparison};
use crate::{
  At, ComplexComparison, FieldName, Mongo, MongoCarrier, RecordAddress, Timestamp,
  comparison::mongo::{MongoConjunction, MongoScalarComparison},
};

const ROW: RecordAddress = RecordAddress::new(FieldName::new("take"), &[FieldName::new("id")]);

/// A sequence of instants, whose members are guarded by a scalar comparison over one segment.
type Sequence = MongoListComparison<Timestamp, MongoScalarComparison<i64>>;
/// A set of them, likewise.
type Set = MongoSetComparison<Timestamp, MongoScalarComparison<i64>>;

fn rendered<C: ComplexComparison<Mongo>>(comparison: &C) -> Document {
  let frame = At::new(&ROW).frame(FieldName::new("stamps"));
  comparison
    .apply(frame.at(), MongoConjunction::new())
    .expect("nothing here refuses")
    .into_document()
}

fn at(second: i64, nanosecond: i32) -> Timestamp {
  Timestamp::from_parts(second, nanosecond).expect("a legal instant")
}

/// One element is ONE sub-document, under the same two names the SQL columns are joined from.
#[test]
fn one_element_is_a_subdocument() {
  assert_eq!(
    at(10, 250).to_bson(),
    Bson::Document(doc! { "secs": 10i64, "nanos": 250i32 }),
    "the instant's own two segments, nested rather than joined"
  );
}

/// **MEMBERSHIP is the bare sub-document match**, which is what implicit traversal already is — so a
/// composite element's containment needs no operator of its own here and no scope to ask it in.
#[test]
fn membership_is_the_bare_subdocument() {
  let mut comparison = Sequence::new();
  comparison.has_element = Some(at(10, 250));

  assert_eq!(
    rendered(&comparison),
    doc! { "stamps": { "$eq": { "secs": 10i64, "nanos": 250i32 } } }
  );
}

/// **The whole ARRAY is the operand**, and it needs no encoding of its own: a list of composite
/// operands and a stored sub-document array are the same BSON value.
#[test]
fn whole_collection_equality_binds_the_subdocument_array() {
  let mut comparison = Sequence::new();
  comparison.eq = Some(Box::new([at(10, 250), at(90, 0)]));

  assert_eq!(
    rendered(&comparison),
    doc! {
      "stamps": {
        "$eq": [
          { "secs": 10i64, "nanos": 250i32 },
          { "secs": 90i64, "nanos": 0i32 },
        ]
      }
    }
  );
}

/// **The element's OWN comparison rides `$elemMatch`**, which is this backend's element scope — the
/// same slot the SQL family opens a derived table for, asking one question of one member.
#[test]
fn the_element_scope_carries_the_elements_own_comparison() {
  let mut element = MongoScalarComparison::<i64>::new();
  element.gt = Some(1_700_000_000);
  let mut comparison = Sequence::new();
  comparison.any = Some(element);

  assert_eq!(
    rendered(&comparison),
    doc! { "stamps": { "$elemMatch": { "$gt": 1_700_000_000i64 } } }
  );
}

/// A SET's own containment operators take the same array operand, so both SHAPES reach the
/// sub-document array without either naming a width.
#[test]
fn a_set_of_composites_encloses_and_overlaps() {
  let mut comparison = Set::new();
  comparison.encloses = Some(Box::new([at(10, 250)]));
  comparison.overlaps = Some(Box::new([at(90, 0)]));

  assert_eq!(
    rendered(&comparison),
    doc! {
      "stamps": {
        "$all": [{ "secs": 10i64, "nanos": 250i32 }],
        "$in": [{ "secs": 90i64, "nanos": 0i32 }],
      }
    }
  );
}