smollerGraph 0.20.2
A small graph database, from scratch
Loading...
Searching...
No Matches
facts.cc
Go to the documentation of this file.
2
3#include <string>
4#include <string_view>
5#include <utility>
6#include <vector>
7
8#include "absl/status/statusor.h"
10
11namespace smollergraph {
12namespace {
13
14// A node or a relationship type in a fact, with a node's label after a colon,
15// and the properties in braces after that, as in "Red fox:Animal {legs: 4}".
16struct Part {
17 std::string_view text;
18 std::string_view label;
19 std::vector<Property> properties;
20};
21
22// Reads a part of a fact, and returns false when it can't. Its braces are
23// written as in Cypher, so our parser reads them. The text has to be at most
24// `max` bytes, without brackets, a label has to fit in a node's record, and
25// the keys and strings in property records.
26bool ReadPart(std::string_view text, size_t max, Part& part) {
27 part = {text, {}, {}};
28 if (size_t open = text.find(" {"); open != std::string_view::npos) {
29 absl::StatusOr<std::vector<Property>> properties =
30 ParseProperties(text.substr(open + 1));
31 if (!properties.ok()) return false;
32 part = {text.substr(0, open), {}, *std::move(properties)};
33 }
34 if (size_t colon = part.text.find(':'); colon != std::string_view::npos) {
35 part.label = part.text.substr(colon + 1);
36 part.text = part.text.substr(0, colon);
37 if (part.label.empty() || part.label.size() > Graph::kMaxLabel) {
38 return false;
39 }
40 }
41 if (part.text.empty() || part.text.size() > max ||
42 part.text.find_first_of("()[]{}") != std::string_view::npos) {
43 return false;
44 }
45 for (const Property& property : part.properties) {
46 const std::string* string = std::get_if<std::string>(&property.value);
47 // A node's name is in its record, so it isn't a property to set.
48 if (property.key == "name" || property.key.size() > Graph::kMaxKey ||
49 (string != nullptr && string->size() > Graph::kMaxString)) {
50 return false;
51 }
52 }
53 return true;
54}
55
56NodeId AddWithProperties(Graph& graph, const Part& part) {
57 NodeId node = graph.AddNode(part.text);
58 if (!part.label.empty()) graph.SetLabel(node, part.label);
59 for (const Property& property : part.properties) {
60 graph.SetProperty(node, property.key, property.value);
61 }
62 return node;
63}
64
65} // namespace
66
67absl::Status LoadFacts(std::istream& in, Graph& graph) {
68 std::string line;
69 while (std::getline(in, line)) {
70 if (line.empty() || line.starts_with('#')) continue;
71 if (!line.starts_with('(') || !line.ends_with(')')) {
72 return absl::InvalidArgumentError(line);
73 }
74 // What's inside the first and the last bracket.
75 std::string_view text = std::string_view(line).substr(1, line.size() - 2);
76 size_t open = text.find(")-[:");
77 size_t close = text.find("]->(");
78 // A fact is a node on its own, or a node, a relationship and a node.
79 std::vector<Part> parts;
80 bool read = false;
81 if (open == std::string_view::npos && close == std::string_view::npos) {
82 parts.resize(1);
83 read = ReadPart(text, Graph::kMaxName, parts[0]);
84 } else if (open != std::string_view::npos &&
85 close != std::string_view::npos && open < close) {
86 parts.resize(3);
87 read = ReadPart(text.substr(0, open), Graph::kMaxName, parts[0]) &&
88 ReadPart(text.substr(open + 4, close - open - 4), Graph::kMaxType,
89 parts[1]) &&
90 ReadPart(text.substr(close + 4), Graph::kMaxName, parts[2]);
91 }
92 if (!read) return absl::InvalidArgumentError(line);
93
94 NodeId from = AddWithProperties(graph, parts[0]);
95 if (parts.size() == 1) continue;
96 NodeId to = AddWithProperties(graph, parts[2]);
97 RelationshipId relationship =
98 graph.AddRelationship(from, parts[1].text, to);
99 for (const Property& property : parts[1].properties) {
100 graph.SetRelationshipProperty(relationship, property.key, property.value);
101 }
102 }
103 return absl::OkStatus();
104}
105
106} // 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
static constexpr size_t kMaxType
Definition graph.h:118
void SetRelationshipProperty(RelationshipId relationship, std::string_view key, const Value &value)
Definition graph.cc:131
static constexpr size_t kMaxLabel
Definition graph.h:117
static constexpr size_t kMaxKey
The longest key and string that fit in a property's record.
Definition graph.h:120
static constexpr size_t kMaxName
The longest name, label and relationship type that fit in their records.
Definition graph.h:116
RelationshipId AddRelationship(NodeId from, std::string_view type, NodeId to)
Definition graph.cc:95
static constexpr size_t kMaxString
Definition graph.h:121
std::string_view label
Definition facts.cc:18
std::vector< Property > properties
Definition facts.cc:19
std::string_view text
Definition facts.cc:17
uint32_t RelationshipId
Definition graph.h:21
uint32_t NodeId
Definition graph.h:20
absl::StatusOr< std::vector< Property > > ParseProperties(std::string_view text)
Reads properties as Cypher writes them, in braces, such as {legs: 4, diet: 'omnivore'},...
Definition parser.cc:293
absl::Status LoadFacts(std::istream &in, Graph &graph)
Reads facts into graph, one a line, written as Cypher patterns such as (Red fox)-[:EATS]->(Rabbit),...
Definition facts.cc:67