Skip to content

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.

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.

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?

0: app1: cli2: ui3: service4: model5: storage6: json7: util
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); // true
long size = Files.size(file); // 100 - bytes, for 8 nodes and 14 edges
Full example
// 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; // true
let size = fs::metadata(file)?.len(); // 100 - bytes, for 8 nodes and 14 edges
Full example
const 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); // true
const size = statSync(file).size; // 100 - bytes, for 8 nodes and 14 edges
Full example
using (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 edges
long size = new FileInfo(file).Length;
Full example

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.

  • 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.WriteTo writes to anything with a write method 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.ReadFrom takes 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. Use subarray to 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.