Why a graph will not do
A Dagr graph has one root, and everything hangs from it. To add one more event to a graph of a million events, you would write the whole graph again. A graph is also written from its end towards its beginning, which is how a parent can know where its children are. A file that grows does the opposite: new bytes go at the end, and what is already there is never touched.
So a DataSink is not a graph. It is a row of small, separate records, one after another.
Events = DataSink("Events", node_types=[
Node("Click", fields=["x" >> t.i32 >> required, "y" >> t.i32 >> required]),
Node("PageView", fields=["url" >> t.utf8 >> required, "ms" >> t.u32 >> required]),
Node("Purchase", fields=["item" >> t.utf8 >> required, "cents" >> t.u32 >> required]),
])
Earlier chapters used "record" loosely, for any node. In a sink it is a precise word: a node with its kind and its length in front. Each record is written as three things: which kind it is, how long it is, and its fields as a packed node from chapter 6. Add some records below and look at what the stream becomes.
Three properties come from those two small numbers in front of each record.
- Appending is only appending. A new record goes after the last one. Nothing before it changes, no count is updated, no table is rewritten. Several programs can read the file while one adds to it.
- An old reader survives new kinds of record. Choose the reader from before purchases existed. When it meets a kind it does not know, the length tells it how far to step, and it carries on with the next record. It can also keep those bytes and pass them on unchanged.
- A crash costs one record. Press "crash in the middle of the last write". The last record's length now promises more bytes than there are, so a reader knows it is not whole and ends the stream before it. Everything written earlier is intact, because nothing earlier was being changed.
Every record stands alone
Whatever layout a type declares, in a sink it is written packed. That makes each record one closed run of bytes with nothing pointing out of it. A record can be copied out of the stream, sent somewhere else, or stored in a database cell, and it is still complete.
The price is that records cannot refer to each other. There is no pointer from one event to an earlier one, and so no sharing and no cycles across records. If events belong together, they say so the way rows in a table do, with an id.
Growing the stream's schema
A log outlives the program that started it, so the rules for change matter more here than anywhere. Chapter 16 has the general rules; these are the ones for a stream.
- A new kind of record goes at the end of the list. A kind is identified by its position in the schema's list, exactly like a field in a packed node. Old readers step over it.
- A new field goes at the end of its record. Old readers ignore it and keep it, as in chapter 6.
- Nothing is removed or reordered. A file written last year still has to mean what it meant.
Reading from the end
A stream like this can only be read from the front. Each record's length is at its start, so to find the last record you must step over all the others. For a log that is often the wrong way round: the newest entries are the ones people want.
A sink can be declared doubly linked. Every record then carries its length a second time, at its end. A reader can start at the end of the file, read that number, jump back to where the record starts, and repeat.
- From the front, the work grows with the file. The reader looks at two bytes of every record just to get past it. In a log of a million records, that is a million steps to reach the last three.
- From the end, it grows with what you asked for. The reader touches the records it wants and nothing else, however long the file is.
- The second length is written backwards. A LEB128 number marks its last byte, which is no use to a reader arriving from the right: it would not know where the number begins. So the bytes are stored in reverse order, and the marked byte is the one that reader meets last.
- It costs a byte or two per record, and it is decided once. A plain stream and a doubly-linked one are different formats, and a file cannot change from one to the other.
Next to the usual ways
Two other ways of writing such a stream are in wide use.
- JSON Lines: one JSON object per line. It can be read by a person and searched with any text tool, which for logs is worth a great deal. Every line spells out every name again, and a reader has to parse each line to learn anything about it, including where it ends.
- Length-prefixed Protocol Buffers: each message with its length in front. This is close to a sink, and many systems use it. The length is a convention and not part of the format, so every project decides it again. The kind of record is not in front either: the usual answer is one wrapper message that contains whichever real message is meant.
| JSON Lines | Protobuf with lengths | DataSink | |
|---|---|---|---|
| readable without tools | yes | no | no |
| step over a record | scan to the line end | by its length | by its length |
| kind of record | a field you choose | a wrapper message | in front of each record |
| names in every record | yes | no | no |
| read from the end | scan backwards for line ends | no | if doubly linked |
What a sink is not
- It is not a database. There is no index. Finding the record with a given value means reading records until you meet it. Nothing can be changed or deleted.
- It does not protect itself. A crash at the end is handled, as you saw. A byte damaged in the middle is not: if a length is wrong, the reader lands in the wrong place and everything after it is misread. Dagr has no checksums of its own. A sink can have a header like the one in chapter 11, and it can carry records about the stream, such as a signature over everything before it. What goes in them is yours to decide, and the next chapter works one such design through.
- It does not replace a message queue. A sink is a format for bytes in a file or on a connection. Who may write, who has read how far, and what happens when a disk is full are questions for the program around it.
What these panels are.
- The bytes are computed in your browser and are checked against Dagr's reference writer for sinks, plain and doubly linked: identical.
- The readers are checked too: what a current reader and an older one see, what is left of a stream cut at every possible byte, and reading from the end, all against the reference reader.
Try it
- Add a click and point at its bytes. Which byte says it is a click, and which says how long it is?
- Add two purchases and switch to the older reader. How many bytes of a purchase does it look at before stepping over it?
- Press the crash button with the older reader selected and a purchase as the last record. Does it matter to that reader that it could not have understood the record anyway?
- In the second panel, compare 6 records with 24, reading the last 3 from each end. Which count changes?
- A record in a doubly-linked stream is 200 bytes long. How many bytes does its second length take, and what would the reader coming from the end see first?
What comes next
A log is often the record of who did what, and then someone will ask whether the log itself can be believed. Chapter 13 puts signed records into the stream, and finds out what they can and cannot prove.
