Datumo.Reader (datumo v0.1.0)

Copy Markdown View Source

Reads from the event store by picking the right index instead of walking the ledger.

The problem it solves

Fact.read(db, {:query, %Fact.QueryItem{}}) turns the query into a function and walks the entire ledger, applying it event by event. It works, but it's O(n) on store size. Measured:

tag query, store of    200 events →   1.3 ms
tag query, store of 20,000 events → 235.0 ms
the same, via the tag index       →   0.07 ms   (3,300×)

FACT already maintains indices on disk (indices/event_tags, indices/event_type); they just aren't used unless you ask. This module picks the most selective one and filters in memory whatever the index can't express.

It matters because every write starts with a read: Decide reads and folds before deciding, and reads again to wait for visibility. Without this, each saved command gets more expensive as history grows.

How it picks

  • With tags → the first tag's index, then the remaining tags and the types are filtered in memory. This is the case for almost every slice, and the one that gains most: tags carry ids, so the index returns a handful of events instead of the whole store.
  • Types only → one index per type, merged and ordered by position. Here the gain isn't in finding but in not reading other people's events.
  • Anything else (:all, :none, data queries) → the original path.

On data queries (Fact.QueryItem.data/1): avoid them. If the database was created without a data indexer their behaviour depends on store state — sometimes they return empty, sometimes they raise. Do your classification with tags, which are indexed. If you ever need them, create the database with the matching indexer and measure before relying on them.

What doesn't change

The result is identical to the scan: same events, same order by store_position, same fields. That isn't an aspiration — assert it in test/datumo/reader_test.exs by comparing both paths query by query. If they ever stop matching, the test says so.

Summary

Functions

Reads the events matching query, in store_position order.

Whether query selects event (a record as read/2 returns it), with FACT's semantics: the items of a list combine with OR; inside an item, types with OR, tags with AND, and data pairs with AND across keys and OR within a key.

Functions

read(db, query)

Reads the events matching query, in store_position order.

Accepts the same shapes as Fact.read/2 in its {:query, ...} form: a %Fact.QueryItem{}, a list of them (a composite query, OR-ed), or :all / :none.

selects?(items, event)

Whether query selects event (a record as read/2 returns it), with FACT's semantics: the items of a list combine with OR; inside an item, types with OR, tags with AND, and data pairs with AND across keys and OR within a key.

It's the one in-memory match: read/2 uses it to finish what an index can't express, and Datumo.Scenario uses it as the test's store, so a scenario reads its given through the same rules production does. test/datumo/reader_test.exs holds it against FACT's own scan.