Internals

raftdb-exec, filter plans and the executor

The plan format, the JSON filter language and its strict typing, batched execution over document masks, ordering, and the SQL subset that compiles to the same plan.

raftdb-exec depends only on raftdb-doc and is #![forbid(unsafe_code)]. The batched design follows the DuckDB study.

Modules#

ModuleRole
lib.rsPlan, Node, Op, Lit, Path, DocSet, the executor (count, select)
filter.rscompile(json_text, &Dict): JSON filter to plan
json.rsA small JSON reader (depth limit 32); json::encode writes parsed JSON as a document
sql.rssql::parse: a SQL subset compiled to the same plan
orderThe total order used by ORDER BY

The plan#

Plan { nodes: Vec<Node>, root: u32 }
Node = True
     | Cmp { path, op: Eq|Lt|Le|Gt|Ge, lit }
     | In { path, lits } | Exists { path, yes }
     | And(a, b) | Or(a, b) | Not(a)        (children are indexes into nodes)
Path { first: u32, rest: [u32] }            (dictionary ids)
Lit  = Null | Bool | Int | F64 | Str

A plan is plain data: it can be cached, compared, printed and sent. Plan::required_leaves lists the comparisons a document must satisfy (found by walking And nodes from the root; a leaf under Or or Not is not listed). A query planner uses them to fetch candidates from an index (Secondary indexes).

The JSON filter language#

{"age":{"$gt":30},"active":true}: fields are ANDed; several operators on one field are ANDed; operators are $eq $ne $gt $gte $lt $lte $in $nin $exists $not; $and and $or take arrays; "address.zip" reaches nested objects; {} matches everything. $regex is not supported yet. Errors are returned as text, never as a panic.

Semantics#

  • Strict types: a comparison is true only if the field exists and has the literal's type. An integer literal against a float field is false (decision D5).
  • $ne, $nin and $not are true for missing or differently typed values.
  • An unknown field name compiles to id u32::MAX: nothing matches, and $exists:false matches everything.

Execution#

Documents are processed 1024 at a time. A mask holds one bit per document. A leaf node visits only the candidate bits (a word of 64 bits with no candidates is skipped). And(a, b) evaluates b on the survivors of a; Or(a, b) evaluates b on the documents a rejected; Not flips within the candidates. count(lo, hi) and select(lo, hi, out) run any range, so shards and threads can split a corpus freely. Plan::count and Plan::select are generic over the Docs trait (len(), doc(i)), so there is no dynamic dispatch; DocSet and raftdb_store::Snapshot implement it.

Measured: 67 M documents per second on 6 threads over 223 MB; a plan-cache hit costs 0.03 us.

SQL subset#

SELECT * | COUNT(*) FROM <table> [WHERE <condition>] [ORDER BY ...] [LIMIT n [OFFSET m]] compiles straight into a Plan, so SQL and JSON filters share one executor, one set of typing rules and the same indexes. Conditions: AND, OR, NOT, parentheses, comparison operators, IN, NOT IN, BETWEEN, IS [NOT] NULL, dotted and quoted column names. Integers (30) and doubles (30.0) are kept apart. a != 5 means NOT (a = 5), so rows without a match, as with $ne. Refused with a message and a byte position: column lists, GROUP BY, LIKE, other functions and writes. (The server adds column lists on top of this; see raftdb-server.)

Ordering#

cmp_val is the total order of ORDER BY, ascending: missing, null, false, true, numbers (integers and doubles together by value), timestamps, text (bytewise), bytes (bytewise), arrays and objects (all equal). DESC reverses it. Rows that tie are ordered by primary key, so paging is stable while the data does not change.