Macaw.Data.Source.Subscriptions (macaw_data v0.21.1)
View SourceReaders' windows of one source, as a value (dco-tek/macaw/macaw#108, spec/01 §6.4.1): each reader's window and budget, and the demand they make together, so a source manager knows all demand before it reads.
- The axis is the chunks' slots (spec/01 §1.1.3).
new/1takes the window field'stype:, its chunkspan:and thenow:sliding windows are resolved at. Slot k holds the window values from k spans to k + 1 from the fixed origin a source's table is laid from, by the same arithmetic, so a chunk's slot is exactly the slot a window demands. A source with no span is demanded whole: every window is{nil, nil}. - A window is
nil(everything, rows with no window value included),[from, to]of the field's type, either boundnilfor open, or{:last, span}, the span up tonow, moved byat/2. - Every change answers the change in demand,
%{needed: ranges, released: ranges}, fromMacaw.Data.Source.Demand: the slots no reader demanded before, and those no reader demands now. covering/2is the reverse index: the readers whose window covers a chunk, given by its id or its slot, so an event is routed to exactly the readers it concerns.
Errors are values: every function answers its faults by path, nothing changed.
iex> alias Macaw.Data.Source.Subscriptions
iex> {:ok, subs} = Subscriptions.new(type: :number, span: 10)
iex> {:ok, subs, change} = Subscriptions.subscribe(subs, :a, [100, 110], nil)
iex> change
%{needed: [{10, 12}], released: []}
iex> {:ok, subs, change} = Subscriptions.subscribe(subs, :b, [105, 125], 500)
iex> change
%{needed: [{12, 13}], released: []}
iex> Subscriptions.demand(subs)
[{10, 13}]
iex> Enum.sort(Subscriptions.covering(subs, {:source, 1, 110}))
[:a, :b]
Summary
Types
The change in demand one call made.
Slots from lo up to but not including hi, either nil for unbounded.
A reader's window: everything, a range of the window field, or a sliding span.
Functions
Every sliding window moved to now: {:ok, subs, change}, every new range added
before any old one is removed, so a slot one window leaves and another reaches is
neither released nor needed. A now not of the window field's type is
{[:now], {:type, type}}.
The budget reader subscribed with, nil for one not subscribed or none.
How many readers cover a chunk: its reference count.
The readers whose window covers a chunk, given as its id {source, resolution, slot}
or its slot, in no set order: of a source demanded whole, every reader; of the slot
nil, rows with no window value, the readers whose window is nil.
The demand: every window's slots, sorted, disjoint, merged where they meet.
reader's window and budget moved: {:ok, subs, change}, the new window added before
the old is removed, so the slots both cover are neither released nor needed. A reader
not subscribed is {[:reader], :unknown}.
No subscriptions over a window field of type: (:time, :number, or nil and
:point, which have no slots), its chunk span: (a positive Duration of fixed length
over a time, a positive number over a number) and the now: sliding windows are
resolved at. With no span the source is demanded whole. A value it can't use is
{[key], {:type, type}}, every fault at once.
Whether any reader covers a chunk: pinned while one does.
The slots reader's window demands now, nil for one not subscribed.
Every reader subscribed, in no set order.
The slot a chunk's slot term starts ({source, resolution, slot}'s third element), as
a slot number on this axis; nil for a source demanded whole, the slot nil, or a
term of no kind the axis takes.
Where a slot starts on the window field, as a value a request's window takes: a UTC
DateTime over a time, a number over a number; nil for an unbounded end.
reader subscribed to window with budget: {:ok, subs, change}, or its faults
with nothing changed. A reader already subscribed is {[:reader], :subscribed}.
reader unsubscribed: {:ok, subs, change}, the slots no reader demands now released.
A reader not subscribed changes nothing.
The window reader subscribed, nil for one not subscribed (or for everything).
Types
The change in demand one call made.
@type range() :: Macaw.Data.Source.Demand.range()
Slots from lo up to but not including hi, either nil for unbounded.
@opaque t()
@type window() :: nil | [term()] | {:last, Duration.t() | number()}
A reader's window: everything, a range of the window field, or a sliding span.
Functions
Every sliding window moved to now: {:ok, subs, change}, every new range added
before any old one is removed, so a slot one window leaves and another reaches is
neither released nor needed. A now not of the window field's type is
{[:now], {:type, type}}.
@spec budget(t(), term()) :: pos_integer() | nil
The budget reader subscribed with, nil for one not subscribed or none.
@spec count(t(), term()) :: non_neg_integer()
How many readers cover a chunk: its reference count.
The readers whose window covers a chunk, given as its id {source, resolution, slot}
or its slot, in no set order: of a source demanded whole, every reader; of the slot
nil, rows with no window value, the readers whose window is nil.
The demand: every window's slots, sorted, disjoint, merged where they meet.
@spec move(t(), term(), window(), pos_integer() | nil) :: {:ok, t(), change()} | {:error, [fault(), ...]}
reader's window and budget moved: {:ok, subs, change}, the new window added before
the old is removed, so the slots both cover are neither released nor needed. A reader
not subscribed is {[:reader], :unknown}.
No subscriptions over a window field of type: (:time, :number, or nil and
:point, which have no slots), its chunk span: (a positive Duration of fixed length
over a time, a positive number over a number) and the now: sliding windows are
resolved at. With no span the source is demanded whole. A value it can't use is
{[key], {:type, type}}, every fault at once.
Whether any reader covers a chunk: pinned while one does.
The slots reader's window demands now, nil for one not subscribed.
Every reader subscribed, in no set order.
The slot a chunk's slot term starts ({source, resolution, slot}'s third element), as
a slot number on this axis; nil for a source demanded whole, the slot nil, or a
term of no kind the axis takes.
Where a slot starts on the window field, as a value a request's window takes: a UTC
DateTime over a time, a number over a number; nil for an unbounded end.
@spec subscribe(t(), term(), window(), pos_integer() | nil) :: {:ok, t(), change()} | {:error, [fault(), ...]}
reader subscribed to window with budget: {:ok, subs, change}, or its faults
with nothing changed. A reader already subscribed is {[:reader], :subscribed}.
reader unsubscribed: {:ok, subs, change}, the slots no reader demands now released.
A reader not subscribed changes nothing.
The window reader subscribed, nil for one not subscribed (or for everything).