DagrA guide to binary formats, and to Dagrdraft

3 Chapter three

Records: names, labels, tables

A record is a few values that belong together. Writing the values is the easy part. The hard part is the reader's question: which of these bytes is the age? There are four well-worn answers, and the differences between binary formats are mostly the differences between them.

One record

Here is a person: a name, an age, a score. The name is always there. The other two may be missing.

Person:
    name:  text
    age:   a number from 0 to 255, optional
    score: a number up to about four billion, optional

Chapter 2 showed how to write each value. Put the three one after another and you have bytes, but not yet a record anyone can read: nothing says where one value ends, which values are present, or which is which.

Four answers

The panel writes the same person all five ways. Change the values, leave a field out, and pick a format. The colours are the ones this guide uses throughout: your data is numbers and text, and everything else was added by the format.

a reader wants only:

What to look for:

Finding one field

Size is one cost. The other is what a reader has to do to get a single field out. Under the list of formats, choose what the reader wants: the bytes it never has to look at fade.

A year later

So far the struct looks like the winner: the smallest, and quick to read. Its weakness shows when the schema changes, which every schema eventually does.

Suppose version 2 of the program adds an email to the person. New programs write it. Old programs, which know only name, age and score, are still running and will be handed the new bytes. Below, a new writer writes Ada with her email, and an old reader reads the result. Choose where in the schema the new field was added.

the new field was added:

What each answer pays

by nameby labelby tableby position
needs a schemanoyesyesyes
overhead for each fieldits namea byte or twoa table entry, sharedabout a bit
getting one fieldwalk, compare nameswalk the labelsa few jumpscompute
adding a field lateranywhereanywhereat the endnot safely

"Shared" in the third row matters more than this panel can show. A thousand records with the same fields present can all point at one table, so its cost is paid once. You will see that happen in chapter 5.

No column wins every row, and that is the point of this chapter. A format that picks one answer picks its weaknesses too.

Where Dagr stands

Dagr always has a schema, so it does not write names. For data whose shape is not known in advance there is another way, in chapter 10. Among the other three answers it does not pick. It offers each of them as a layout that you choose for each type:

So a type that is finished can go by position, a message that will grow can go by label, and a large record you want to pick single fields out of can have a table, all in one buffer. Chapter 5 shows them in Dagr's own bytes.

What these panels are.

Try it

  1. Give Ada a score of 5, then of 4,000,000,000. Which formats change size, and by how much?
  2. Untick age. What does each format write for a missing field? Which of them writes something?
  3. Make the name twelve letters long. In FlatBuffers, watch the padding change as you type. What is each run of padding lining up?
  4. In the second panel, the old struct reader gets a wrong age. Work out from the bytes which byte it read, and what that byte really is.
  5. Protocol Buffers survives a field added anywhere. Think of a change to the schema that it does not survive.

What comes next

A record with a list of records inside it is a tree, and all four answers extend to trees without much trouble. Real data is often not a tree: two orders refer to the same customer, a person's friend has that person as a friend. Chapter 4 is about what happens then, and chapter 5 is where Dagr's layouts appear in full.