DagrA guide to binary formats, and to Dagrdraft

12 Chapter twelve

DataSink: data that keeps arriving

Everything so far was one value: a person, a team, a token. It is built, written, and then read. A lot of data is not like that. A log gets a line every time something happens. A stream of events has no last event. For data that is never finished, Dagr has a second container, the DataSink.

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.

read by:
    which kind, tagslengthsnumberstextframingfaded: stepped overdashed: not a whole record

    Three properties come from those two small numbers in front of each record.

    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.

    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.

    records in the stream:
    wanted: the last
    the reader starts:
    the length again, for a reader coming from the endfaded: never looked at

    Next to the usual ways

    Two other ways of writing such a stream are in wide use.

    JSON LinesProtobuf with lengthsDataSink
    readable without toolsyesnono
    step over a recordscan to the line endby its lengthby its length
    kind of recorda field you choosea wrapper messagein front of each record
    names in every recordyesnono
    read from the endscan backwards for line endsnoif doubly linked

    What a sink is not

    What these panels are.

    Try it

    1. Add a click and point at its bytes. Which byte says it is a click, and which says how long it is?
    2. Add two purchases and switch to the older reader. How many bytes of a purchase does it look at before stepping over it?
    3. 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?
    4. In the second panel, compare 6 records with 24, reading the last 3 from each end. Which count changes?
    5. 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.