8#include "absl/status/statusor.h"
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 =
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);
41 if (part.text.empty() || part.text.size() > max ||
42 part.text.find_first_of(
"()[]{}") != std::string_view::npos) {
45 for (
const Property& property : part.
properties) {
46 const std::string*
string = std::get_if<std::string>(&property.value);
48 if (property.key ==
"name" || property.key.size() >
Graph::kMaxKey ||
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);
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);
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(
"]->(");
79 std::vector<Part> parts;
81 if (open == std::string_view::npos && close == std::string_view::npos) {
84 }
else if (open != std::string_view::npos &&
85 close != std::string_view::npos && open < close) {
92 if (!read)
return absl::InvalidArgumentError(line);
94 NodeId from = AddWithProperties(graph, parts[0]);
95 if (parts.size() == 1)
continue;
96 NodeId to = AddWithProperties(graph, parts[2]);
99 for (
const Property& property : parts[1].properties) {
103 return absl::OkStatus();
A graph kept in three files of records, one for nodes, one for relationships and one for their proper...
static constexpr size_t kMaxType
void SetRelationshipProperty(RelationshipId relationship, std::string_view key, const Value &value)
static constexpr size_t kMaxLabel
static constexpr size_t kMaxKey
The longest key and string that fit in a property's record.
static constexpr size_t kMaxName
The longest name, label and relationship type that fit in their records.
RelationshipId AddRelationship(NodeId from, std::string_view type, NodeId to)
static constexpr size_t kMaxString
std::vector< Property > properties
absl::StatusOr< std::vector< Property > > ParseProperties(std::string_view text)
Reads properties as Cypher writes them, in braces, such as {legs: 4, diet: 'omnivore'},...
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),...