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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
//! The storage side of a [`MediaTimestamp`] query, behind the `sqlx` feature.
//!
//! ```text
//! MediaTimestampFilter                    SqlMediaTimestampComparison<Db>
//!   eq: "12345 @ 1/90000"   ──►        nanos: eq 137166666   ──► WHERE "ts_nanos" = ?
//!   gte: "0:01:30.5"        ──►        nanos: gte 90500000000 ──► AND "ts_nanos" >= ?
//!   pts: { gt: 0 }          ──►        pts: gt 0             ──► AND "ts_pts" > ?
//!   timebase: "MPEG_90K"    ──►        the nested citizen     ──► AND "ts_timebase_num" = ?
//!                                                                 AND "ts_timebase_den" = ?
//! ```
//!
//! # The eight whole-value operators cross into ONE scalar comparison
//!
//! Identity and ordering are four operators each and they are the eight
//! [`SqlScalarComparison`](crate::comparison::sql::SqlScalarComparison) has — over one column, in
//! one unit. That is not a coincidence to exploit but the shape the design already had: both groups
//! ask about a POSITION, and the only difference between them is which operands the wire admits,
//! which is settled a layer earlier and leaves nothing for the crossing to distinguish.
//!
//! So the identity/bound split has no trace here, and it should not: by the time a filter reaches
//! this crossing it holds [`MediaTimestamp`]s either way, and every one of them goes through
//! [`MediaTimestamp::to_nanos`] — the SAME function the write path binds the derived column with. That is
//! the law the timebase's reduction states, and it is why an identity operand is required to be an
//! exact pair: a pair goes through that function; a clock string arrives at a number by a different
//! road, and two roads to one column is the silent-miss shape.
//!
//! # What each facet reaches
//!
//! | facet | column | why there |
//! |---|---|---|
//! | identity + bound | `nanos` | the one unit every stored row shares |
//! | `pts` | `pts` | the declared count, which is a question about the encoding |
//! | `timebase` | `timebase_num`, `timebase_den` | the nested citizen, applied one frame down |
//!
//! The timebase facet is a whole [`SqlTimebaseComparison`] rather than two scalar slots, so
//! everything that citizen can answer is answerable here — its row-value memberships and its render
//! facet included — and none of it is restated. A composite holding a composite addresses it the way
//! any other holder does: one frame, and the citizen pushes its own segments below it.
//!
//! # Refusals
//!
//! Two, and both are inherited rather than invented. An OPERAND past `i64` nanoseconds is refused
//! here, at the last place it can be, rather than saturating into a bound nobody wrote — the same
//! posture the write path takes when it binds `NULL` for a value it cannot place. And the nested
//! citizen's own refusals arrive whole, carrying which part of the timebase filter was
//! unanswerable.
//!
//! [`MediaTimestampFilter`]: super::MediaTimestampFilter

use core::fmt::{self, Display};

use crate::{
  At, Backend, BindCeiling, Carrier, ComplexComparison, CompositeComparison, ScalarFilter,
  ToDatabaseComparison, TryFromFilter,
  comparison::sql::{SqlConjunction, SqlLoweringError, SqlScalarComparison, push_column},
};

use super::{
  super::{
    NanosecondOverflow, SqlTimebaseComparison, Timebase, TimebaseCrossingError,
    timebase::{TIMEBASE_DEN, TIMEBASE_NUM},
  },
  MediaTimestamp, MediaTimestampFilter,
  storage::{NANOS, PTS, SEGMENTS, TIMEBASE},
};

// The DIALECT this house's crossing tests render against is Postgres — its NUMBERED placeholders are
// what say which operand reached which column, where a `?` dialect can only say how many there were —
// so the module names `sqlx::Postgres` and needs that driver rather than the family root alone.
//
// The source crate carried a bare `#[cfg(test)]` here and never felt it, its own `sqlx` word arriving
// with a dialect behind it. In this crate `sqlx` is the family ROOT and a dialect is a word of its
// own, so the gate has to name the one it spells. It is the divergence the language family recorded
// on the same move — per-dialect gates replacing a single `sqlx` word — read one dialect over.
#[cfg(all(test, feature = "postgres"))]
mod tests;

/// [`MediaTimestampFilter`]'s storage-side twin: the derived position column, the declared count, and the
/// timebase citizen nested inside.
pub struct SqlMediaTimestampComparison<Db> {
  exact: Option<Encoding>,
  nanos: SqlScalarComparison<i64, Db>,
  pts: SqlScalarComparison<i64, Db>,
  timebase: SqlTimebaseComparison<Db>,
}

/// One encoding, in the three columns that hold it: the declared count and the REDUCED timebase.
///
/// Reduced because that is what the columns hold — the timebase citizen's own storage law, met one
/// level up. It costs nothing a client can observe: two spellings of one timebase are one value to
/// `mediatime`, so `2 @ 2/180000` and `2 @ 1/90000` are one timestamp and one row, and this facet
/// separates encodings that differ in their `pts` or in their timebase's VALUE — which is exactly
/// what the columns can tell apart.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
struct Encoding {
  pts: i64,
  num: i32,
  den: i32,
}

impl<Db> SqlMediaTimestampComparison<Db> {
  /// The empty comparison — no operator set on any facet.
  #[inline]
  pub const fn new() -> Self {
    Self {
      exact: None,
      nanos: SqlScalarComparison::new(),
      pts: SqlScalarComparison::new(),
      timebase: SqlTimebaseComparison::new(),
    }
  }

  /// The encoding the value columns must hold, or [`None`] where `exact` was not asked.
  ///
  /// Projected as the three storage numbers rather than as the domain value: the timebase half is
  /// REDUCED here, which is not always the timebase the client spelled.
  #[inline]
  pub const fn exact(&self) -> Option<(i64, i32, i32)> {
    match self.exact {
      Some(Encoding { pts, num, den }) => Some((pts, num, den)),
      None => None,
    }
  }

  /// The operators over the derived position column, in nanoseconds — where BOTH whole-value groups
  /// land, the identity four and the bound four alike.
  #[inline]
  pub const fn nanos(&self) -> &SqlScalarComparison<i64, Db> {
    &self.nanos
  }

  /// The operators over the declared count.
  #[inline]
  pub const fn pts(&self) -> &SqlScalarComparison<i64, Db> {
    &self.pts
  }

  /// The nested citizen's own comparison.
  #[inline]
  pub const fn timebase(&self) -> &SqlTimebaseComparison<Db> {
    &self.timebase
  }

  /// True while NO operator slot is set anywhere.
  #[inline]
  pub const fn is_empty(&self) -> bool {
    self.exact.is_none() && self.nanos.is_empty() && self.pts.is_empty() && self.timebase.is_empty()
  }

  /// How many conjuncts this comparison lowers to — the sum of what each facet opens, so the
  /// arithmetic here cannot describe a different rendering than the one that happens.
  #[inline]
  pub const fn conjuncts(&self) -> usize {
    self.exact.is_some() as usize * 3
      + self.nanos.conjuncts()
      + self.pts.conjuncts()
      + self.timebase.conjuncts()
  }
}

/// The lowering: three frames, in the order [`SEGMENTS`](super::storage) lists them.
///
/// The bound list is the nested citizen's own — a timebase filter can carry a row-value membership
/// and an affix pattern, so reaching one costs exactly what reaching one costs. `i64: Carrier<Db>`
/// is this type's only addition.
impl<Db> ComplexComparison<Db> for SqlMediaTimestampComparison<Db>
where
  Db: sqlx::Database
    + BindCeiling
    + for<'q> Backend<Builder<'q> = SqlConjunction<'q, Db>, Error = SqlLoweringError, Buffer: Display>,
  i32: Carrier<Db>,
  i64: Carrier<Db>,
  for<'a> &'a str: sqlx::Encode<'a, Db> + sqlx::Type<Db>,
{
  #[inline]
  fn conjuncts(&self) -> usize {
    Self::conjuncts(self)
  }

  fn apply<'q>(
    &'q self,
    at: At<'_>,
    builder: Db::Builder<'q>,
  ) -> Result<Db::Builder<'q>, Db::Error> {
    let mut builder = builder;

    // Rendered as its OWN three conjuncts rather than assigned into the `pts` and `timebase` slots
    // below. Assigning would make `exact` and a component's own `eq` collide over one slot, and the
    // choice would be between refusing a legitimate pair and keeping whichever was written last —
    // the interaction the range citizen's algebra already renders its way out of. Three independent
    // conjuncts have no such question: a contradiction returns no rows, which is the right answer.
    if let Some(Encoding { pts, num, den }) = self.exact {
      equals::<Db, i64>(&mut builder, at.frame(PTS).at(), pts);

      let timebase = at.frame(TIMEBASE);
      equals::<Db, i32>(&mut builder, timebase.at().frame(TIMEBASE_NUM).at(), num);
      equals::<Db, i32>(&mut builder, timebase.at().frame(TIMEBASE_DEN).at(), den);
    }

    let pts = at.frame(PTS);
    let builder = self.pts.apply(pts.at(), builder)?;
    let timebase = at.frame(TIMEBASE);
    let builder = self.timebase.apply(timebase.at(), builder)?;
    let nanos = at.frame(NANOS);
    self.nanos.apply(nanos.at(), builder)
  }
}

/// One column equal to one operand — the whole of what the structural facet renders.
///
/// The column is rendered through [`push_column`], as every leaf in the SQL family renders one, so
/// the joining and the bracketing stay the backend's.
fn equals<'q, Db, V>(builder: &mut SqlConjunction<'q, Db>, at: At<'_>, operand: V)
where
  Db: sqlx::Database + Backend<Buffer: Display>,
  V: Carrier<Db>,
{
  let qb = builder.open();
  push_column(qb, at);
  qb.push(" = ");
  operand.push_bind(qb);
}

/// The claim `flatten` needs, read off the one list the storage face states.
impl<Db> CompositeComparison<Db> for SqlMediaTimestampComparison<Db>
where
  Db: sqlx::Database
    + BindCeiling
    + for<'q> Backend<Builder<'q> = SqlConjunction<'q, Db>, Error = SqlLoweringError, Buffer: Display>,
  i32: Carrier<Db>,
  i64: Carrier<Db>,
  for<'a> &'a str: sqlx::Encode<'a, Db> + sqlx::Type<Db>,
{
  const SEGMENTS: &'static [crate::ColumnShape] = SEGMENTS;
}

impl<Db> Default for SqlMediaTimestampComparison<Db> {
  #[inline]
  fn default() -> Self {
    Self::new()
  }
}

/// Written out rather than derived, for the reason `ingraph`'s own kinds write theirs out: a derive
/// would bound `Db`, which is a marker no dialect type implements anything for.
impl<Db> fmt::Debug for SqlMediaTimestampComparison<Db> {
  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    f.debug_struct("SqlMediaTimestampComparison")
      .field("exact", &self.exact)
      .field("nanos", &self.nanos)
      .field("pts", &self.pts)
      .field("timebase", &self.timebase)
      .finish()
  }
}

impl<Db> Clone for SqlMediaTimestampComparison<Db> {
  fn clone(&self) -> Self {
    Self {
      exact: self.exact,
      nanos: self.nanos.clone(),
      pts: self.pts.clone(),
      timebase: self.timebase.clone(),
    }
  }
}

impl<Db> PartialEq for SqlMediaTimestampComparison<Db> {
  fn eq(&self, other: &Self) -> bool {
    self.exact == other.exact
      && self.nanos == other.nanos
      && self.pts == other.pts
      && self.timebase == other.timebase
  }
}

/// Every SQL dialect takes the timestamp filter to the timestamp comparison, and the storage type is
/// `i64` — the derived column's, which is the one a declaration names.
impl<Db> ToDatabaseComparison<i64, Db> for MediaTimestampFilter
where
  Db: sqlx::Database
    + BindCeiling
    + for<'q> Backend<Builder<'q> = SqlConjunction<'q, Db>, Error = SqlLoweringError, Buffer: Display>,
  i32: Carrier<Db>,
  i64: Carrier<Db>,
  for<'a> &'a str: sqlx::Encode<'a, Db> + sqlx::Type<Db>,
{
  type Comparison = SqlMediaTimestampComparison<Db>;
}

/// **The same comparison, keyed by the CITIZEN** — the row a field declared `MediaTimestamp`
/// resolves through, where the row above is the one findit's hand-flattened `at_pts` column
/// resolved through.
///
/// The comparison already reaches FOUR columns by path — the declared count, the nested timebase's
/// two, and the derived position — so what a whole-value declaration changes is which map entry the
/// expansion looks up and nothing about the statement it lowers into. That is why this row states
/// the identical projection rather than a second one: a composite field and a hand-flattened field
/// ask the same SQL, and the only thing that ever differed was who wrote the column names.
///
/// The two rows cannot overlap — [`MediaTimestamp`] is not `i64` — so an author reaches whichever
/// one their declaration names.
impl<Db> ToDatabaseComparison<MediaTimestamp, Db> for MediaTimestampFilter
where
  Db: sqlx::Database
    + BindCeiling
    + for<'q> Backend<Builder<'q> = SqlConjunction<'q, Db>, Error = SqlLoweringError, Buffer: Display>,
  i32: Carrier<Db>,
  i64: Carrier<Db>,
  for<'a> &'a str: sqlx::Encode<'a, Db> + sqlx::Type<Db>,
{
  type Comparison = SqlMediaTimestampComparison<Db>;
}

/// The crossing, and the only route into a [`SqlMediaTimestampComparison`].
///
/// # No `Db` bound
///
/// The crossing turns domain values into storage values, and which dialect eventually binds them
/// takes no part in that. So this impl holds for every `Db`, including one that is not a database.
impl<Db> TryFromFilter<MediaTimestampFilter> for SqlMediaTimestampComparison<Db> {
  type Error = MediaTimestampCrossingError;

  fn try_from_filter(filter: MediaTimestampFilter) -> Result<Self, Self::Error> {
    let MediaTimestampFilter {
      exact,
      eq,
      ne,
      in_,
      not_in,
      gt,
      gte,
      lt,
      lte,
      pts,
      timebase,
    } = filter;

    // A component slot is OPTIONAL on the wire, and an absent one asks nothing — which is the empty
    // filter, exactly what the hand-written parse this replaced wrote for a key a client omitted.
    let pts = pts.unwrap_or_default();
    let timebase = timebase.unwrap_or_default();

    // Both whole-value groups land in ONE scalar filter over the derived column, which is what the
    // eight slots ARE. Every operand crosses through `to_nanos`, the write path's own function.
    let mut nanos = ScalarFilter::<i64>::new();
    nanos.update_eq(position(eq)?);
    nanos.update_ne(position(ne)?);
    nanos.update_gt(position(gt.map(MediaTimestamp::from))?);
    nanos.update_gte(position(gte.map(MediaTimestamp::from))?);
    nanos.update_lt(position(lt.map(MediaTimestamp::from))?);
    nanos.update_lte(position(lte.map(MediaTimestamp::from))?);
    nanos.update_in_(positions(in_)?);
    nanos.update_not_in(positions(not_in)?);

    // The structural facet needs NO conversion and can refuse nothing: the value columns hold what
    // the operand already is. It is the only slot here that never meets the nanosecond ceiling, so
    // an instant past ±292 years is still findable by the encoding that wrote it.
    let exact = exact.map(|operand| {
      let (num, den) = Timebase::new(operand.get().timebase()).reduced();
      Encoding {
        pts: operand.get().pts(),
        num,
        den: den.get(),
      }
    });

    Ok(Self {
      exact,
      nanos: infallible(SqlScalarComparison::try_from_filter(nanos)),
      pts: infallible(SqlScalarComparison::try_from_filter(pts)),
      timebase: SqlTimebaseComparison::try_from_filter(timebase)
        .map_err(MediaTimestampCrossingError::Timebase)?,
    })
  }
}

/// One operand's position on the timeline, or the overflow that has none.
fn position(operand: Option<MediaTimestamp>) -> Result<Option<i64>, MediaTimestampCrossingError> {
  operand
    .map(|operand| operand.to_nanos())
    .transpose()
    .map_err(MediaTimestampCrossingError::Overflow)
}

/// A membership list's positions, element by element. The FIRST refusal is the answer, and an empty
/// list stays an empty list — `in: []` keeps asking for no rows.
fn positions(
  operands: Option<Box<[MediaTimestamp]>>,
) -> Result<Option<Box<[i64]>>, MediaTimestampCrossingError> {
  operands
    .map(|operands| {
      operands
        .iter()
        .map(|operand| operand.to_nanos())
        .collect::<Result<Box<[i64]>, _>>()
        .map_err(MediaTimestampCrossingError::Overflow)
    })
    .transpose()
}

/// The scalar halves cross with `Error = Infallible` — `i64: TryFrom<i64>` through std's reflexive
/// blanket. Matched out rather than unwrapped, so the impossibility stays the compiler's.
fn infallible<T>(crossed: Result<T, core::convert::Infallible>) -> T {
  match crossed {
    Ok(crossed) => crossed,
    Err(never) => match never {},
  }
}

/// A [`MediaTimestampFilter`] did not reach its columns, and this says which part of it did not.
///
/// Two variants, and neither is this layer's own invention: one is the nanosecond ceiling the
/// storage face already states, and the other is the nested citizen's refusal passed through whole.
///
/// [`MediaTimestampFilter`]: super::MediaTimestampFilter
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum MediaTimestampCrossingError {
  /// An operand is further from zero than `i64` nanoseconds reach — about ±292 years.
  ///
  /// Refused rather than saturated. A bound clamped to the ceiling is a different bound, and it
  /// would select rows the client did not ask for while reporting success.
  Overflow(NanosecondOverflow),
  /// The `timebase` facet did not cross — carrying the nested citizen's own refusal.
  Timebase(TimebaseCrossingError),
}

impl fmt::Display for MediaTimestampCrossingError {
  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    match self {
      Self::Overflow(overflow) => overflow.fmt(f),
      Self::Timebase(timebase) => timebase.fmt(f),
    }
  }
}

impl core::error::Error for MediaTimestampCrossingError {
  fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
    match self {
      Self::Overflow(overflow) => Some(overflow),
      Self::Timebase(timebase) => Some(timebase),
    }
  }
}