raftdb-proto, the wire protocol codec
The binary frame format, every request and reply kind, the parsing rules, protocol version 2 (streams) and measured codec speed.
raftdb-proto turns bytes into requests and replies and back. It depends only on raftdb-doc (for varints), does no I/O, and has no unsafe code.
Frame#
[len u32 LE][kind u8][id u32 LE][body]. len counts everything after itself: at least 5 and at most MAX_FRAME (16 MiB). The client chooses id and the server echoes it, so replies may arrive in any order and a slow scan does not block a point read behind it. Integers in bodies are varints; byte strings are [len varint][bytes]. frame(buf) returns Ok(None) when more bytes are needed, so pipelined frames are taken one after another.
Requests (kind below 0x80)#
| Request | Kind | Body |
|---|---|---|
Hello | 0x01 | [version][token bytes] |
Ping | 0x02 | empty |
Get / GetBatch | 0x03 / 0x04 | key / count and keys |
Put / Insert | 0x05 / 0x06 | [key][document]; Insert fails if the key exists |
Delete | 0x07 | key |
Filter | 0x08 | flags (count only, JSON text, SQL), limit, cursor, filter text |
Cancel | 0x09 | target id |
FieldId | 0x0A | field name; the server allocates an id if new |
PutJson | 0x0B | key and JSON object text |
CreateIndex | 0x0C | [ordered u8][field name] |
Use | 0x0D | database and table of this connection (new connections start on main.documents) |
Stream, Defer, Sync, Project | 0x13 to 0x16 | version 2 only, see below |
Replies (kind 0x80 and above)#
HelloOk 0x81, Pong 0x82, Found 0x83, NotFound 0x84, Docs 0x85 (count, documents, next cursor; cursor 0 means no more pages), Done 0x86 (a write applied and durable), Count 0x87, Error 0x88 (code and message), FieldId 0x89.
Documents travel in the stored binary form of raftdb-doc, or as JSON text when asked. The server needs no conversion for the binary form.
Parsing rules#
Parsing is zero-copy and does not allocate; parsed values borrow the input. Every length is checked against the bytes actually present, a body must be consumed exactly, a list count larger than the bytes left is rejected before any loop, flags must be 0 or 1 and text must be UTF-8. Errors are BadLength, BadKind, BadBody, BadUtf8. Damaged input never panics: tests flip every single bit of valid frames and feed 200,000 random inputs.
Version 2: streams#
A client that puts many callers on one connection says version 2 (VERSION_STREAMS) in Hello. A server that knows it answers with the same number; an older server answers BAD_STATE and keeps the connection, so the client can say 1. Stream selects which caller the following requests belong to (no reply). Defer makes the server answer writes when they are applied instead of when durable, and Sync answers when the answered-early writes are durable. Project is a Filter for binary documents, each cut down to the named field ids. See raftdb-client for the client side.
Measured#
raftdb-bench proto, one core, one operation = encode, find the frame, parse: Get with a 13-byte key 49.9 M/s (target 20 M/s); Put with 13 + 111 bytes 37.9 M/s; Found with a 111-byte document 52.9 M/s; GetBatch of 32 keys 93 M keys/s.
Not supported yet#
Compression, streaming of very large documents (a document must fit in one frame), a change-feed message, version negotiation beyond "equal or refuse", and TLS (the client and server send credentials in clear text).