1# Chapter 8: postjevsql-core, the part that only thinks
2
3If chapters 5 to 7 were the hands, this is the head. Every decision in here
4is a pure function: given these questions, this is how the row is sent;
5given these bytes, this is the cache key; given this clock reading and this
6word, this request may go now or must wait so long; given these answers, ask
7these Choices next. No Postgres, no network, no clock, no randomness. The
8caller supplies all of those as arguments.
9
10That restriction is what makes the hard parts testable. The label-tree
11search, for one, is checked against an exhaustive search over every
12combination of answers, which would be hopeless if every step needed a
13database and a model on the other end. Here it is a unit test that runs in
14about a second on any laptop.
15
16```mermaid
17flowchart LR
18  subgraph core["postjevsql-core (pure)"]
19    B["batch: how a row is sent"]
20    K["cache_key: what was sent, hashed"]
21    T["token_ratio: tokens from characters"]
22    G["gcra: may this request go now?"]
23    LT["label_tree: Choice over a tree of labels"]
24    SL["shortlist: Choice over a long flat list"]
25    GA["gate: does any label fit?"]
26  end
27  ext["postjevsql (the judge)"] --> B
28  ext --> K
29  ext --> T
30  ext --> LT
31  ext --> SL
32  pg["postjevsql-pg (ratelimit.rs)"] --> G
33  LT --> GA
34  SL --> GA
35```
36
37> **Aside: sans-I/O.** The searches here are written in a style called
38> sans-I/O: the search never sends anything. It says "ask these Choices",
39> you go and ask them however you like, and you hand it the answers. It then
40> says what to ask next, or that it is done. The extension drives it through
41> Postgres and HTTP/2; the tests drive it with a closure that makes answers
42> up. Same search, no idea which is which.
43
44> **Try it.** `cargo test -p postjevsql-core`, from a fresh clone, no
45> database and no key. About fifty tests, in about a second.
46
47## For the people who maintain it
48
49| Path | What |
50| --- | --- |
51| [src/](src/) | The six decisions, one file each, and the gate both searches share. Chapter 9. |
52| [BUCK](BUCK) | The library, and `:test`, its unit tests, run as plain `rust_test`. Its only dependencies are `jev-protocol` and `sha2`. |
53| [Cargo.toml](Cargo.toml) | Generated by `tools/cargo-gen` (chapter 20). |
54
55← Previous: [Chapter 7, scan/](../postjevsql-pg/src/scan/) · Up: [crates](../) · Next: [Chapter 9, postjevsql-core/src/](src/) →