10.1 Save a graph to a file and load it again
You write a graph in a compact binary form to a file or any other output, and read it back.
Problem
Section titled “Problem”You want to build a graph once, keep the result and load it again quickly. Building it can take long, for example when it means parsing a codebase, querying a database or crawling a network. You need this for:
- a cache of a graph that is expensive to build, such as the calls of a large codebase;
- a graph that one process hands to another, in a file or a message;
- a snapshot of a graph, to compare with a later version.
Example
Section titled “Example”The example graph dependencies models the dependencies between the eight packages of a small
program. It has an edge from each package to each package it uses. That makes 8 nodes and 14 edges.
The code writes the graph to a file and reads it back. Is the graph read back the same, and how
large is the file?
Solution
Section titled “Solution”try (OutputStream out = Files.newOutputStream(file)) { dependencies.writeTo(out);}Graph loaded;try (InputStream in = Files.newInputStream(file)) { loaded = Graph.readFrom(in);}boolean same = loaded.equals(dependencies); // truelong size = Files.size(file); // 100 - bytes, for 8 nodes and 14 edges// Each File is closed at the end of its statement.dependencies.write_to(File::create(file)?)?;let loaded = Graph::read_from(File::open(file)?)?;let same = loaded == dependencies; // truelet size = fs::metadata(file)?.len(); // 100 - bytes, for 8 nodes and 14 edgesconst fd = openSync(file, "w");try { dependencies.writeTo({ write: (chunk) => writeSync(fd, chunk) });} finally { closeSync(fd);}const loaded = Graph.readFrom(readFileSync(file));const same = loaded.equals(dependencies); // trueconst size = statSync(file).size; // 100 - bytes, for 8 nodes and 14 edgesusing (Stream output = File.Create(file)){ dependencies.WriteTo(output);}Graph loaded;using (Stream input = File.OpenRead(file)){ loaded = Graph.ReadFrom(input);}bool same = loaded.Equals(dependencies); // true// 100 - bytes, for 8 nodes and 14 edgeslong size = new FileInfo(file).Length;The graph read back is equal to the one written. The file holds the node count and the edge count, then the two arrays of the CSR layout (compressed sparse row) of the graph. That makes 100 bytes in all:
- 8 bytes for the two counts;
- 4 bytes for each of the 9 entries of the array of start positions (one per node and one more);
- 4 bytes for each of the 14 edges.
Reading checks the arrays as Graph.ofGraph::ofGraph.ofGraph.Of does.
Good to know
Section titled “Good to know”- The same on every machine. The numbers are always written in little-endian byte order, so a file written on one machine reads on any other.
- Nothing but the graph. Graphity writes no marker, version or checksum. Reading rejects only data that does not form a graph. A damaged file whose numbers still form a graph reads as a graph. Where the data may come from elsewhere or be damaged, write a marker or a checksum of your own next to the graph.
- Bi-adjacent graphs. A
BiAdjacentGraphBiAdjacentGraphBiAdjacentGraphBiAdjacentGraphis written without its in-nodes. Reading always returns a plainGraphGraphGraphGraph.BiAdjacentGraph.ofBiAdjacentGraph::ofBiAdjacentGraph.ofBiAdjacentGraph.Ofbuilds the in-nodes again where you need them.
- Not only files. Any output and input works, such as a file, a socket or a buffer in memory. Other data can come before or after the graph, such as your arrays of names. Reading takes only the bytes of the graph. Neither call closes a stream you go on using.
- Not only files. Any output and input works, such as a file, a socket or a buffer in memory. Other data can come before or after the graph, such as your arrays of names. Reading takes only the bytes of the graph. Neither call closes a stream you go on using.
- Not only files.
Graph.writeToGraph::write_toGraph.writeToGraph.WriteTowrites to anything with awritemethod that takes bytes, such as a file stream, a socket or an object that collects the chunks in memory. It closes nothing.Graph.readFromGraph::read_fromGraph.readFromGraph.ReadFromtakes no stream but the bytes themselves, such as the content of a file or of a message. Other data can come before or after the graph, such as your arrays of names. Usesubarrayto pass the bytes from where the graph starts. The data after the graph starts 8 + 4 x (node count + 1 + edge count) bytes later, and reading leaves it alone. The graph may keep a view of these bytes instead of a copy, so do not change them afterwards. To keep only the graph in memory, read from a copy that holds only the bytes of the graph.
- Not only files. Any output and input works, such as a file, a socket or a buffer in memory. Other data can come before or after the graph, such as your arrays of names. Reading takes only the bytes of the graph. Neither call closes a stream you go on using.