DagrA guide to binary formats, and to Dagrdraft

20 Chapter twenty

Tools: making binary as convenient as text

The first chapter said why text formats are everywhere: you can open the file and see your data, with tools you already have. A binary file shown in an editor is noise. That is a real disadvantage, and no property of the format removes it. Tools do. This last chapter goes through the ones Dagr has, each next to the thing people do with a text file that it replaces.

What text gives you for nothing

Consider what happens to a JSON file in an ordinary week, with no special software.

None of this is the format's doing. It is the work of editors, search tools and version control, all written for text over decades. A binary format starts without any of it, and each item has to be supplied.

Dagr's tools all come as one command, dagr, and all work from the same thing: your schema. Everything in this chapter was run on one small project, a team of people as a graph and a stream of events as a sink.



Opening a file: what does each byte mean?

dagr explain takes a file and the schema and accounts for every byte: what it is, which node and field it belongs to, what value it holds, and where it points. Below is its real output for a team of three, 63 bytes. Point at any byte.

You have used this tool already. The coloured bytes in chapters 4 and 5 came from it.

Browsing, searching and editing

Explain is for when you care about bytes. Usually you care about the data. For that, Dagr writes an explorer: a single web page, generated for your schema, that reads files of that schema in the browser. Nothing is uploaded and no server is involved.

Both are here to try, as the tool wrote them. Open one and drop the file on it.

This is the answer to "open the file and there is your data". It takes one more step than a text editor, the page has to be generated once for each schema, and after that it shows more than a text editor would: types, links between things, and the same node reached by two paths.

Comparing and merging

Version control is where binary files are usually given up on. Git shows "binary files differ" and offers to keep one version or the other.

dagr git-tools builds a small program for your schema and tells Git to use it for files of that kind. It does three things. The first is to print a file as text, one line for each value, with its path from the root:


A line such as $.members[0] -> &$.lead says that the first member is not a copy of the lead. It is the same node. A text format has no way to say that, and a tool that compared two files as trees would report changes that are not there, or miss ones that are.

The second is a comparison of two versions, and the third a merge of two people's changes. Both are below. Somebody started from the team above. Choose what was done to it.


  

With text, a merge works on lines, and it can produce a file that is no longer valid JSON, or valid and wrong. A merge that works on values and knows the schema cannot produce a broken file. On this point the binary format is ahead of text.

Writing one by hand

Nobody types a binary file. There are three substitutes.



Starting, and keeping promises

The rest of the command is about the schema itself.



Where text is still ahead

Why these tools exist

A list like this used to belong only to formats with a company behind them. An explorer, a diff, a merge with a page for conflicts, a fuzzer and four importers are each a project, and none is the format itself. For a format made by one person they would have stayed on a list of things to do some day.

They were built the way the previous chapter described, each with a plan written first and a test that settles whether it works: the explanation must cover every byte, a merge must equal the file built directly with both changes, an exported edit must read back as what was shown. With an agent to do the writing, each took days.

That changes the argument of chapter 1. Text was convenient because its tools already existed, and a binary format could not afford to build its own. That cost has fallen a long way. What remains to text is that its tools are general and already installed, which is worth a great deal, and is no longer everything.

Where this leaves you

That is the whole of Dagr: one schema language, three containers, and a set of tools around them.

None of them is the best at everything, and the chapters tried to say where each loses. For a single message, there are formats that are smaller, formats that read faster, and formats with far more users. What is unusual about Dagr is the three together: one schema for a document, the log of changes to it and the memory two programs share while working on it. All three are checked by the same receipt, and the first two are opened, searched and compared with the tools of this chapter. That combination, more than any one measurement, is the reason to choose it.

The next step is to write a schema of your own: the repository has the tool, the specification and examples in every language Dagr generates.

What these panels are.

Try it

  1. In the first panel, find the three bytes that say where the three members are. Two of the people have a manager. Where do those pointers lead?
  2. Open the graph explorer and drop the team on it. Press "Expand all". How many boxes are there for the four mentions of Ada?
  3. In the merge panel, look at "my change". Why is the new member's manager shown with an arrow and not with a name?
  4. The merged team is larger than either changed version. By how much, and what accounts for it?
  5. Open the sink explorer with the forty events and add a filter: page views that took more than 400 milliseconds. What would the same question cost with a text editor and a file of JSON Lines?