13#include "absl/status/status.h"
14#include "absl/strings/str_cat.h"
22void CopyField(
char (&field)[N], std::string_view
text) {
23 std::memset(field, 0, N);
24 std::memcpy(field,
text.data(), std::min(
text.size(), N - 1));
28void Encode(
const Value& value, PropertyRecord& record) {
29 if (
const int64_t* number = std::get_if<int64_t>(&value)) {
31 std::memset(record.value, 0,
sizeof(record.value));
32 std::memcpy(record.value, number,
sizeof(*number));
35 CopyField(record.value, std::get<std::string>(value));
44 std::memcpy(&number, record.
value,
sizeof(number));
47 return std::string(record.
value);
51 absl::Status status = nodes_.OpenTemporary();
52 if (status.ok()) status = relationships_.OpenTemporary();
53 if (status.ok()) status = properties_.OpenTemporary();
59 std::fprintf(stderr,
"smollergraph: no temporary files: %s\n",
60 status.ToString().c_str());
67 if (mkdir(
path.c_str(), 0755) != 0 && errno != EEXIST) {
68 return absl::ErrnoToStatus(errno, absl::StrCat(
"can't make ",
path));
72 absl::Status status = nodes_.Open(
path +
"/nodes");
73 if (status.ok()) status = relationships_.Open(
path +
"/relationships");
74 if (status.ok()) status = properties_.Open(
path +
"/properties");
75 if (status.ok()) status = names_.
Open(
path +
"/names");
76 if (status.ok()) status = labels_.
Open(
path +
"/labels");
77 if (status.ok()) status = types_.
Open(
path +
"/types");
78 if (status.ok()) status = embeddings_.
Open(
path +
"/embeddings");
83 if (std::optional<NodeId> found =
Find(
name))
return *found;
90 NodeId id = nodes_.Append(record);
108 nodes_.Write(from, start);
114 nodes_.Write(to, end);
115 relationships_.Append(record);
122 nodes_.Write(
node, record);
128 nodes_.Write(
node, record);
132 std::string_view key,
const Value& value) {
138void Graph::Set(
PropertyId& first, std::string_view key,
const Value& value) {
153 property.next = first;
154 first = properties_.Append(
property);
161 embeddings_.
Append(embedding);
166 std::vector<Embedding> embeddings;
167 if (embeddings_.
count() != nodes_.count())
return embeddings;
168 embeddings.reserve(embeddings_.
count());
170 embeddings.push_back(embeddings_.
Read(
node));
176 std::string_view key)
const {
178 if (key ==
"name")
return std::string(record.
name);
184 return Lookup(relationships_.Read(
relationship).first_property, key);
187std::optional<Value> Graph::Lookup(
PropertyId first,
188 std::string_view key)
const {
199 return Chain(nodes_.Read(
node).first_property);
204 return Chain(relationships_.Read(
relationship).first_property);
207std::vector<Property> Graph::Chain(
PropertyId first)
const {
208 std::vector<Property> found;
216 std::reverse(found.begin(), found.end());
226 std::vector<Hop> hops;
229 std::vector<TokenId> tokens;
230 for (
const std::string&
type : types) {
231 if (std::optional<TokenId> token = types_.
Find(
type)) {
232 tokens.push_back(*token);
235 if (tokens.empty())
return hops;
238 Follow(record.
first_out,
true, tokens, hops);
241 Follow(record.
first_in,
false, tokens, hops);
247 const std::vector<TokenId>& types,
248 std::vector<Hop>& hops)
const {
249 std::vector<Hop> chain;
253 if (std::find(types.begin(), types.end(), record.
type) != types.end()) {
254 chain.push_back({id, out ? record.
to : record.
from});
259 hops.insert(hops.end(), chain.rbegin(), chain.rend());
264 std::vector<NodeId> found;
266 found.push_back(hop.node);
NodeId AddNode(std::string_view name)
Returns the node with this name, adding it the first time.
void SetRelationshipProperty(RelationshipId relationship, std::string_view key, const Value &value)
PropertyRecord property(PropertyId id) const
std::optional< Value > FindProperty(NodeId node, std::string_view key) const
Returns a node's property, if it has one. Its name is a property too.
std::optional< NodeId > Find(std::string_view name) const
Returns the node with this name, if the graph has one.
void SetEmbedding(NodeId node, const Embedding &embedding)
Keeps a node's embedding, in place of the one it had.
std::vector< Property > Properties(NodeId node) const
Returns the properties of a node or a relationship, in the order they were first set.
std::optional< Value > FindRelationshipProperty(RelationshipId relationship, std::string_view key) const
Returns a relationship's property, if it has one, such as its weight.
RelationshipRecord relationship(RelationshipId id) const
std::string type(RelationshipId relationship) const
NodeRecord node(NodeId id) const
RelationshipId AddRelationship(NodeId from, std::string_view type, NodeId to)
std::vector< NodeId > Neighbours(NodeId node, std::string_view type, Direction direction=Direction::kOut) const
Returns the nodes the hops along relationships of type lead to.
std::string name(NodeId node) const
absl::Status Open(const std::string &path)
Opens the graph in the folder at path, creating it when it isn't there.
void SetLabel(NodeId node, std::string_view label)
Gives a node its label, such as Species, in place of the one it had.
std::vector< Hop > Hops(NodeId node, const std::vector< std::string > &types, Direction direction) const
Returns the hops from node along relationships of any of types: those that start at it,...
Graph()
A graph in temporary files, which are deleted with it.
std::vector< Property > RelationshipProperties(RelationshipId relationship) const
void SetProperty(NodeId node, std::string_view key, const Value &value)
Sets a property of a node or a relationship, replacing the value it had.
std::vector< Embedding > Embeddings() const
Returns the embeddings of all the nodes, in the order of their ids, or none unless each node has one.
absl::Status OpenTemporary()
Opens an index in a file with no name, which is deleted once it's closed.
absl::Status Open(const std::string &path)
Opens the index in the file at path, creating it when it isn't there.
void Insert(std::string_view name, uint32_t id)
Keeps id for name, which the index doesn't have yet.
std::optional< uint32_t > Find(std::string_view name) const
Returns the id kept for name, if there's one.
absl::Status Open(const std::string &path)
Opening another file writes back the changes to the one that was open.
absl::Status OpenTemporary()
void Write(uint32_t id, const Record &record)
uint32_t Append(const Record &record)
Adds a record after the last one, and returns its id.
Record Read(uint32_t id) const
Reads record id, which has to be below count().
TokenId Add(std::string_view name)
Returns the token for name, adding it the first time.
absl::Status OpenTemporary()
std::optional< TokenId > Find(std::string_view name) const
Returns the token for name, if the store has one.
absl::Status Open(const std::string &path)
Opens the store in the file at path, creating it when it isn't there.
std::array< float, kEmbeddingSize > Embedding
A node's embedding: a vector of numbers that stands for where the node is in the graph,...
constexpr TokenId kNoToken
The token for no name, such as the label of a node that hasn't got one.
Value ValueOf(const PropertyRecord &record)
Reads the value a property's record holds.
std::variant< int64_t, std::string > Value
A property's value, as the graph hands it out: a whole number or a string.
constexpr RelationshipId kNoRelationship
The ids at the end of a chain, where there's no next relationship, or no next property.
Direction
A node and a property take 64 bytes, and a relationship 24, so a page holds 170 relationships,...
constexpr PropertyId kNoProperty
A step from a node along one of its relationships: the relationship, and the node at its other end.
A node on disk: its name, its label's token, which says what kind of node it is, the first of the rel...
PropertyId first_property
A property on disk: its key, its value, and the next property of the node or relationship it belongs ...
A relationship on disk: its type's token, the nodes it joins, the next relationship that starts at fr...
PropertyId first_property