Macaw.Data.Source.Subscriptions (macaw_data v0.21.1)

View Source

Readers' 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/1 takes the window field's type:, its chunk span: and the now: 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 bound nil for open, or {:last, span}, the span up to now, moved by at/2.
  • Every change answers the change in demand, %{needed: ranges, released: ranges}, from Macaw.Data.Source.Demand: the slots no reader demanded before, and those no reader demands now.
  • covering/2 is 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.

t()

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

change()

@type change() :: %{needed: [range()], released: [range()]}

The change in demand one call made.

fault()

@type fault() :: {list(), term()}

range()

@type range() :: Macaw.Data.Source.Demand.range()

Slots from lo up to but not including hi, either nil for unbounded.

t()

@opaque t()

window()

@type window() :: nil | [term()] | {:last, Duration.t() | number()}

A reader's window: everything, a range of the window field, or a sliding span.

Functions

at(subs, now)

@spec at(t(), term()) :: {:ok, t(), change()} | {:error, [fault(), ...]}

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}}.

budget(subscriptions, reader)

@spec budget(t(), term()) :: pos_integer() | nil

The budget reader subscribed with, nil for one not subscribed or none.

count(subs, chunk)

@spec count(t(), term()) :: non_neg_integer()

How many readers cover a chunk: its reference count.

covering(subs, slot)

@spec covering(t(), term()) :: [term()]

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.

demand(subscriptions)

@spec demand(t()) :: [range()]

The demand: every window's slots, sorted, disjoint, merged where they meet.

move(subs, reader, window, budget)

@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}.

new(opts \\ [])

@spec new(keyword()) :: {:ok, t()} | {:error, [fault(), ...]}

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.

pinned?(subs, chunk)

@spec pinned?(t(), term()) :: boolean()

Whether any reader covers a chunk: pinned while one does.

range(subscriptions, reader)

@spec range(t(), term()) :: range() | nil

The slots reader's window demands now, nil for one not subscribed.

readers(subscriptions)

@spec readers(t()) :: [term()]

Every reader subscribed, in no set order.

slot_index(subscriptions, slot)

@spec slot_index(t(), term()) :: integer() | nil

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.

start(subscriptions, k)

@spec start(t(), integer() | nil) :: term()

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.

subscribe(subs, reader, window, budget)

@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}.

unsubscribe(subs, reader)

@spec unsubscribe(t(), term()) :: {:ok, t(), change()}

reader unsubscribed: {:ok, subs, change}, the slots no reader demands now released. A reader not subscribed changes nothing.

window(subscriptions, reader)

@spec window(t(), term()) :: window()

The window reader subscribed, nil for one not subscribed (or for everything).