smollerGraph 0.20.2
A small graph database, from scratch
Loading...
Searching...
No Matches
export.cc
Go to the documentation of this file.
2
3#include <sys/stat.h>
4
5#include <cerrno>
6#include <fstream>
7#include <string>
8
9#include "absl/status/status.h"
10#include "absl/strings/str_cat.h"
11#include "smollergraph/api.h"
12
13namespace smollergraph {
14
15absl::Status Export(const Graph& graph, const std::string& path) {
16 // A folder that's there already, from an earlier run, is fine.
17 if (mkdir(path.c_str(), 0755) != 0 && errno != EEXIST) {
18 return absl::ErrnoToStatus(errno, absl::StrCat("can't make ", path));
19 }
20 std::ofstream nodes(path + "/nodes.jsonl");
21 std::ofstream relationships(path + "/relationships.csv");
22 if (!nodes || !relationships) {
23 return absl::UnavailableError(absl::StrCat("can't write in ", path));
24 }
25 // JSON Lines: each line is a JSON object of its own, so a reader can take
26 // the file a line at a time.
27 for (NodeId node = 0; node < graph.node_count(); ++node) {
28 nodes << "{\"id\": " << node
29 << ", \"name\": " << JsonString(graph.name(node))
30 << ", \"label\": " << JsonString(graph.label(node))
31 << ", \"properties\": " << PropertiesJson(graph.Properties(node))
32 << "}\n";
33 }
34 relationships << "from,to,type\n";
35 for (RelationshipId id = 0; id < graph.relationship_count(); ++id) {
36 RelationshipRecord record = graph.relationship(id);
37 relationships << record.from << ',' << record.to << ',' << graph.type(id)
38 << '\n';
39 }
40 return absl::OkStatus();
41}
42
43} // namespace smollergraph
A graph kept in three files of records, one for nodes, one for relationships and one for their proper...
Definition graph.h:113
size_t relationship_count() const
Definition graph.h:203
size_t node_count() const
Definition graph.h:202
std::string label(NodeId node) const
Definition graph.h:191
std::vector< Property > Properties(NodeId node) const
Returns the properties of a node or a relationship, in the order they were first set.
Definition graph.cc:198
RelationshipRecord relationship(RelationshipId id) const
Definition graph.h:198
std::string type(RelationshipId relationship) const
Definition graph.h:194
std::string name(NodeId node) const
Definition graph.h:190
std::string PropertiesJson(const std::vector< Property > &properties)
Returns properties as a JSON object, with a field for each key.
Definition api.cc:107
uint32_t RelationshipId
Definition graph.h:21
uint32_t NodeId
Definition graph.h:20
std::string JsonString(std::string_view text)
What sg serve answers with, as JSON.
Definition api.cc:90
absl::Status Export(const Graph &graph, const std::string &path)
Writes the graph into the folder at path, for programs outside the database to read,...
Definition export.cc:15
std::vector< NodeId > nodes
Definition plan.cc:114
Path path
Definition plan.cc:115
A relationship on disk: its type's token, the nodes it joins, the next relationship that starts at fr...
Definition graph.h:43