Two declarations have valid time. Both require
key.
A series written twice at the same key and instant keeps the later commit’s sample.
The clock may report the same instant for several commits.
Use a timeline for state that holds over a period: a price, a contract, a
certification. Use a series for measurements at instants. Keep an event, such
as a decision or a receipt, in an ordinary table.
Timelines
bijection/tariffs.ts
Write a period
ctx.put(point.over({ from, to }), fact) makes the fact hold throughout the
half-open period [from, to). ctx.retract(point.over({ from, to })) makes
nothing hold throughout it. Both keep what stands outside the period. null
is unbounded: to: null replaces every later plan for the key.
Write several periods with one call each, in order. A later call wins where it
overlaps an earlier one.
to is at or before its from is refused.
A bound may be the commit’s stamp: frame.now.add({ seconds: 0 }), or frame.now.add(d) for
the stamp plus an exact duration. The host places it when the commit lands,
so a period that starts when the write lands, or lasts an hour from then,
never starts at the entry instant ctx.frame.now.entry:
ctx.put and ctx.retract take a bound at frame.now.add({ seconds: 0 }): a read or an
assumption cannot see the stamp (ClockTermBlind).
Read what is in force
A timeline point without.at is in force at the frame’s at, which is now
unless the caller chose another instant. .over reads the segments over a
period, each with its from and to, clipped to the period.
Series
A series holds one sample per key and instant.key names the fields of a
series, one to four of them; every other field is a value. Declare no time
field: the instant is the sample’s place, written with .at(instant) and
read as at.
bijection/readings.ts
key, label (a label or a family), lifetime (how long a
sample is kept after it is written), history and mark. It takes no
total, machine, pin or erasable. See
Table options.
Write, correct and retract
ctx.putandctx.retractread nothing, so they never refuse and a retry is harmless. A reader sees them exactly: one conflicts only when it changes what that reader read.- Putting the same key at the same instant again replaces the sample. That is how you correct one. Two puts of one key at one instant in one transaction write one sample.
.over({ from, to })is half-open; anullbound leaves that end open.- No write is refused by the clock: a sample in the future or far in the past is accepted. A series that must refuse late or future samples is a strict series.
ctx.insert,ctx.replace,ctx.patchandctx.deletetake the same points, and assume the sample absent or present as they do on any table.
Read samples
- A sample reads as its fields and
at, aTemporal.Instant. .overanswers oldest first, and.descending()newest first.ctx.pageanswers{ facts, next }; passnextback ascursor.readings.where({ at: { from, to } })selects every key’s samples in an interval, ordered byatthen the key. No index needs declaring: every series is ordered by valid time across its keys.- The last sample at or before an instant
tisctx.list(readings(k).over({ from: null, to: t.add({ microseconds: 1 }) }).descending().first(1)):.overis half-open and instants are whole microseconds, so the bound one microsecond pasttincludes a sample att..at(t)keeps its one meaning, the sample exactly att.
mark keeps the valid-time buckets each
write touched: see Marks.
State that holds between changes (a setpoint, a status, a price) is not a
series: declare a timeline. Use
windowed derivations to publish an aligned
result series from several series and timeline inputs.
The valid instant of a call
A function’s valid instant isctx.frame.at, or null at the present. A
caller sets it for one call with client.at(instant), and nested calls
inherit it; ctx.call(fn, args, { at }) sets it for one inner call. A read at
the present answers what is valid when the transaction commits: if the next
boundary it read passes first, the host runs the transaction again. A watched
query re-runs when its reads change, and when the next valid boundary it read
passes.
Earlier versions
“What did we know then” is the same read in an earlier version. Store the version a decision read,ctx.frame.version, with the decision, and read in it
later with .in(version) or client.in(version). A version stays readable
for the table’s history, or while a pin holds it. See
History and lifetime.
The series library
@bijectionhq/library-series builds on series tables: rollups that stay
exact under late writes, retention per tier, frames for dense telemetry, read
helpers and a strict layer. Each instance is a subtree you place in the app
tree.
Declare a library series
series({ by, values, … }) from @bijectionhq/library-series declares a
series table of its own (its points) and everything it derives, and
returns a subtree:
bijection/app.ts
The subtree:
Any function of the app may write the points directly:
ctx.put(readings.points({ site, machine }).at(at), { celsius, rpm }). The
in-transaction helpers are on readings.helpers:
write(ctx, facts)takes facts{ ...by, ...values, at }, writes them and answers{ written, late }.retract(ctx, by, at)removes one instant, andretract(ctx, by, { from, to })an interval, up to 4,000 samples per transaction, finishing the rest in the background.range,lastandwindoware the readers below.readings.argsholds their value types for your own functions.
Readers and rollups
The readers answer columns.range answers one page of [from, to):
- A value missing from a sample is
nullin its column. sizeis 1 to 8,192 samples (default 2,000), andorderis"ascending"or"descending".nextis the cursor of the following page.readings.helpers.last(ctx, by, { at, within })is the last sample at or beforeat(default now), ornull; withwithin,nullwhen it is that old or older.
window reads aggregate buckets. A tier keeps, per bucket and per rolled
value, the count, sum, minimum, maximum, first and last:
count, sum, mean, min, max, first, last and
rms. from and to must be multiples of every, and a window answers
every bucket, empty ones included.
The read rule. With rollups, the points declare mark at the finest
tier’s width, so the host records every bucket a write touches, and a range
retraction records its span in points.spans. A window reads the coarsest
tier whose width divides every, and refolds from a finer tier or from the
samples any bucket that is still marked or overlapped by a dirty span. A
window is therefore exact at its version whatever the roller’s lag: a late
sample or a correction shows up in the next read. A window that would fold
more than 16,000 facts is refused with SeriesNeedsRollup, which names the
tier to declare. A window answers at most 8,192 buckets.
The roller folds marked buckets once they are older than the tier width plus
lateness. A tier is named by a count and a unit ("15m", "1h", "1d")
that spells its width. Declare tiers finest first, each a multiple of the one
before. Readers answer the declared lateness, so a chart can draw a line at
now minus lateness.
To pivot samples you read yourself, use columns(facts, fields):
bijection/reading-columns.ts
Retention
The table’slifetime erases each raw sample that long after it was written.
retention: { "1m": { days: 730 } } keeps minute rollups for two years, as
the tier table’s lifetime. A tier may outlive its samples. See
History and lifetime.
Frames for dense telemetry
Above about one sample per second per series, store frames: one fact holds up to 8,192 samples.- A frame’s instant is its first sample’s.
spanis the longest one frame covers, at most the finest tier’s width.dtis the interval between samples; without it, each frame carriesoffsetsfromat. - Readers answer flat sample columns, so a chart does not know the series is framed.
- Sending the same frame again changes nothing; sending it changed corrects the whole frame. Frames of one series should not overlap.
A series per member
Give the series a family label, and every sample, mark and rollup fact takes its member’s label. Every function then runs under the caller’s member (client.under(tenant.of("acme"))).
energy.helpers.write arms each key it writes, and
a direct writer arms its key with energy.helpers.tend(ctx, { meter }). Each
active key has one pending tendTurn task.
Ingest connector records
Mount the Streams app to acquire topic records. A reaction on its committed records (on: records.changes) writes the series
with ctx.put. A producer that writes over HTTP calls the series’ write
function, or a route reaction of your own.
Live charts
UseuseWatch with a query that returns a bounded window of series buckets.
The chart chooses the time bounds and bucket width. A watch answers
undefined while loading, then the query’s Result as its buckets change.
Use aligned bounds when the query requires a whole number of buckets.
Query with SQL
The points table and every tier are ordinary tables to SQL, which sees their stored facts. A sample’s instant is the columnat. A tier table
holds, per rolled value f, the columns f_n, f_sum, f_sumsq, f_min,
f_max, f_first and f_last, plus count. SQL does not apply the read
rule: a marked bucket has a stale fact or none. Use window when you need
exact buckets.
The strict layer
Use a strict series when the clock is not enough evidence: when you need to know which source delivered what, refuse contradictory or late deliveries, or state that a range is complete.temperatures.admission.admit,
produce (a producer’s route under a source it opened) and retract, with
the in-transaction helpers on temperatures.strict. A guard the subtree
declares refuses any write to its points whose writer is not one of its own
functions.
- An observation may carry an
order. A higher order corrects, an equal order with other content is refused, and a lower one is superseded. Replaying a delivery is an inert duplicate. futurerefuses a point too far ahead of the clock, andlate: "refuse"refuses one older thanlateness. These two rules exist only here.- A source settles how far its deliveries are complete, and the series’ frontier answers how far the series is complete.
- Monitors keep episodes of a condition over the values and journal each
transition. A transition reaches your mutation (
onTransition) as a task whoseoncenames the transition.
Watches over a series
watches({...}) from @bijectionhq/library-watches keeps alert episodes
over a condition, evaluated on a clock or on a strict series’ monitor
transitions. It returns a subtree with watches (insert, edit, pause,
delete, evaluate, list, get) and alerts (list, get,
acknowledge, assign, snooze, resolve, note), exposed to its
operators, responders and viewers.
From the command line
The exposed functions are ordinary queries and mutations, sobijection query and bijection mutate call them by path
(readings.range, readings.write).
Limits
Not supported yet
- A span retraction made outside the library on a family series arms no tend turn. Readers honour its dirty span at once, and the key’s next tend turn rolls it.
- Tier retention runs from when a tier fact was last written, not from its bucket’s start.
- Frames in a strict series, and a family label on a strict series.