smollerGraph 0.20.2
A small graph database, from scratch
Loading...
Searching...
No Matches
graph.h
Go to the documentation of this file.
1#ifndef SMOLLERGRAPH_GRAPH_H_
2#define SMOLLERGRAPH_GRAPH_H_
3
4#include <array>
5#include <cstddef>
6#include <cstdint>
7#include <optional>
8#include <string>
9#include <string_view>
10#include <variant>
11#include <vector>
12
13#include "absl/status/status.h"
17
18namespace smollergraph {
19
20using NodeId = uint32_t;
21using RelationshipId = uint32_t;
22using PropertyId = uint32_t;
23
24// The ids at the end of a chain, where there's no next relationship, or no
25// next property.
26inline constexpr RelationshipId kNoRelationship = UINT32_MAX;
27inline constexpr PropertyId kNoProperty = UINT32_MAX;
28
29// A node on disk: its name, its label's token, which says what kind of node
30// it is, the first of the relationships that start at it, the first of those
31// that end at it, and the first of its properties.
39
40// A relationship on disk: its type's token, the nodes it joins, the next
41// relationship that starts at `from` and the next that ends at `to`, so it's
42// in a chain of each of its nodes, then the first of its properties.
51
52enum class ValueType : uint8_t { kInteger, kString };
53
54// A property on disk: its key, its value, and the next property of the node or
55// relationship it belongs to, so their properties form a chain too. A whole
56// number takes the first 8 bytes of the value, and a string runs to its zero.
58 char key[16];
60 char value[43];
62};
63
64// A node and a property take 64 bytes, and a relationship 24, so a page holds
65// 170 relationships, with 16 bytes left at its end.
66static_assert(sizeof(NodeRecord) == 64);
67static_assert(sizeof(RelationshipRecord) == 24);
68static_assert(sizeof(PropertyRecord) == 64);
69
70// Which of a node's relationships to follow: those that start at it, those
71// that end at it, or both.
72enum class Direction { kOut, kIn, kBoth };
73
74// The direction to follow a relationship in from its other end: out becomes
75// in, in becomes out, and both stays both.
81
82// A step from a node along one of its relationships: the relationship, and the
83// node at its other end.
88
89// How many numbers a node's embedding has.
90inline constexpr size_t kEmbeddingSize = 256;
91
92// A node's embedding: a vector of numbers that stands for where the node is in
93// the graph, so that nodes with similar neighbourhoods get vectors that point
94// in similar directions. On disk, it's a record of its own, a kibibyte, which
95// has the node's id, so four fit in a page.
96using Embedding = std::array<float, kEmbeddingSize>;
97static_assert(sizeof(Embedding) == 1024);
98
99// A property's value, as the graph hands it out: a whole number or a string.
100using Value = std::variant<int64_t, std::string>;
101
102struct Property {
103 std::string key;
105};
106
107// Reads the value a property's record holds.
108Value ValueOf(const PropertyRecord& record);
109
110// A graph kept in three files of records, one for nodes, one for
111// relationships and one for their properties, with an index that finds a node
112// by its name, and a store each for the tokens of labels and types.
113class Graph {
114 public:
115 // The longest name, label and relationship type that fit in their records.
116 static constexpr size_t kMaxName = sizeof(NodeRecord::name) - 1;
117 static constexpr size_t kMaxLabel = TokenStore::kMaxName;
118 static constexpr size_t kMaxType = TokenStore::kMaxName;
119 // The longest key and string that fit in a property's record.
120 static constexpr size_t kMaxKey = sizeof(PropertyRecord::key) - 1;
121 static constexpr size_t kMaxString = sizeof(PropertyRecord::value) - 1;
122
123 // A graph in temporary files, which are deleted with it.
124 Graph();
125 // Opens the graph in the folder at `path`, creating it when it isn't there.
126 absl::Status Open(const std::string& path);
127
128 // Writes the graph's changed pages to its files, which closing it does too.
129 void Flush() {
130 embeddings_.Flush();
131 nodes_.Flush();
132 relationships_.Flush();
133 properties_.Flush();
134 names_.Flush();
135 labels_.Flush();
136 types_.Flush();
137 }
138
139 // Returns the node with this name, adding it the first time. The name has to
140 // be at most kMaxName bytes long.
141 NodeId AddNode(std::string_view name);
142 RelationshipId AddRelationship(NodeId from, std::string_view type, NodeId to);
143 // Gives a node its label, such as Species, in place of the one it had. The
144 // label has to be at most kMaxLabel bytes long.
145 void SetLabel(NodeId node, std::string_view label);
146
147 // Sets a property of a node or a relationship, replacing the value it had.
148 // The key has to be at most kMaxKey bytes long, and a string kMaxString. A
149 // node's name is in its record already, so it's not a key to set.
150 void SetProperty(NodeId node, std::string_view key, const Value& value);
152 std::string_view key, const Value& value);
153
154 // Keeps a node's embedding, in place of the one it had. The nodes are given
155 // theirs in the order of their ids.
156 void SetEmbedding(NodeId node, const Embedding& embedding);
157 // Returns the embeddings of all the nodes, in the order of their ids, or
158 // none unless each node has one.
159 std::vector<Embedding> Embeddings() const;
160
161 // Returns a node's property, if it has one. Its name is a property too.
162 std::optional<Value> FindProperty(NodeId node, std::string_view key) const;
163 // Returns a relationship's property, if it has one, such as its weight.
165 std::string_view key) const;
166 // Returns the properties of a node or a relationship, in the order they
167 // were first set.
168 std::vector<Property> Properties(NodeId node) const;
169 std::vector<Property> RelationshipProperties(
171
172 // Returns the node with this name, if the graph has one.
173 std::optional<NodeId> Find(std::string_view name) const;
174
175 // Returns up to `limit` names that start with `prefix`, in order.
176 std::vector<std::string> NamesStartingWith(std::string_view prefix,
177 size_t limit) const {
178 return names_.NamesStartingWith(prefix, limit);
179 }
180
181 // Returns the hops from `node` along relationships of any of `types`: those
182 // that start at it, those that end at it with kIn, or with kBoth, the ones
183 // out and then the ones in.
184 std::vector<Hop> Hops(NodeId node, const std::vector<std::string>& types,
185 Direction direction) const;
186 // Returns the nodes the hops along relationships of `type` lead to.
187 std::vector<NodeId> Neighbours(NodeId node, std::string_view type,
189
190 std::string name(NodeId node) const { return nodes_.Read(node).name; }
191 std::string label(NodeId node) const {
192 return labels_.Name(nodes_.Read(node).label);
193 }
194 std::string type(RelationshipId relationship) const {
195 return types_.Name(relationships_.Read(relationship).type);
196 }
197 NodeRecord node(NodeId id) const { return nodes_.Read(id); }
199 return relationships_.Read(id);
200 }
201 PropertyRecord property(PropertyId id) const { return properties_.Read(id); }
202 size_t node_count() const { return nodes_.count(); }
203 size_t relationship_count() const { return relationships_.count(); }
204 // How many pages the graph's files have read and written.
205 uint64_t page_reads() const {
206 return embeddings_.page_reads() + nodes_.page_reads() +
207 relationships_.page_reads() + properties_.page_reads() +
208 names_.page_reads() + labels_.page_reads() + types_.page_reads();
209 }
210 uint64_t page_writes() const {
211 return embeddings_.page_writes() + nodes_.page_writes() +
212 relationships_.page_writes() + properties_.page_writes() +
213 names_.page_writes() + labels_.page_writes() + types_.page_writes();
214 }
215
216 private:
217 // Sets `key` in the chain of properties that starts at `first`. A key the
218 // chain doesn't have yet gets a record of its own, at the front.
219 void Set(PropertyId& first, std::string_view key, const Value& value);
220 std::vector<Property> Chain(PropertyId first) const;
221 // Returns the value of `key` in the chain of properties that starts at
222 // `first`, if the chain has it.
223 std::optional<Value> Lookup(PropertyId first, std::string_view key) const;
224 // Adds the hops along the relationships in the chain that starts at `first`
225 // whose type is one of the tokens in `types`, out or in, in the order they
226 // were added.
227 void Follow(RelationshipId first, bool out, const std::vector<TokenId>& types,
228 std::vector<Hop>& hops) const;
229
231 RecordFile<RelationshipRecord> relationships_;
232 RecordFile<PropertyRecord> properties_;
233 // Names to node ids, in a file of their own.
234 NameIndex names_;
235 TokenStore labels_;
236 TokenStore types_;
237 RecordFile<Embedding> embeddings_;
238};
239
240} // namespace smollergraph
241
242#endif // SMOLLERGRAPH_GRAPH_H_
A graph kept in three files of records, one for nodes, one for relationships and one for their proper...
Definition graph.h:113
NodeId AddNode(std::string_view name)
Returns the node with this name, adding it the first time.
Definition graph.cc:82
static constexpr size_t kMaxType
Definition graph.h:118
uint64_t page_writes() const
Definition graph.h:210
void SetRelationshipProperty(RelationshipId relationship, std::string_view key, const Value &value)
Definition graph.cc:131
std::vector< std::string > NamesStartingWith(std::string_view prefix, size_t limit) const
Returns up to limit names that start with prefix, in order.
Definition graph.h:176
size_t relationship_count() const
Definition graph.h:203
size_t node_count() const
Definition graph.h:202
PropertyRecord property(PropertyId id) const
Definition graph.h:201
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.
Definition graph.cc:175
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
std::optional< NodeId > Find(std::string_view name) const
Returns the node with this name, if the graph has one.
Definition graph.cc:220
void SetEmbedding(NodeId node, const Embedding &embedding)
Keeps a node's embedding, in place of the one it had.
Definition graph.cc:157
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
std::optional< Value > FindRelationshipProperty(RelationshipId relationship, std::string_view key) const
Returns a relationship's property, if it has one, such as its weight.
Definition graph.cc:182
RelationshipRecord relationship(RelationshipId id) const
Definition graph.h:198
std::string type(RelationshipId relationship) const
Definition graph.h:194
NodeRecord node(NodeId id) const
Definition graph.h:197
RelationshipId AddRelationship(NodeId from, std::string_view type, NodeId to)
Definition graph.cc:95
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.
Definition graph.cc:262
static constexpr size_t kMaxString
Definition graph.h:121
std::string name(NodeId node) const
Definition graph.h:190
void Flush()
Writes the graph's changed pages to its files, which closing it does too.
Definition graph.h:129
absl::Status Open(const std::string &path)
Opens the graph in the folder at path, creating it when it isn't there.
Definition graph.cc:65
void SetLabel(NodeId node, std::string_view label)
Gives a node its label, such as Species, in place of the one it had.
Definition graph.cc:119
uint64_t page_reads() const
How many pages the graph's files have read and written.
Definition graph.h:205
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,...
Definition graph.cc:224
Graph()
A graph in temporary files, which are deleted with it.
Definition graph.cc:50
std::vector< Property > RelationshipProperties(RelationshipId relationship) const
Definition graph.cc:202
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.
Definition graph.cc:125
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.
Definition graph.cc:165
An index from names to ids, kept in a file of pages as a B+ tree: leaves of names in order,...
Definition name_index.h:20
std::vector< std::string > NamesStartingWith(std::string_view prefix, size_t limit) const
Returns up to limit names that start with prefix, in order.
uint64_t page_reads() const
Definition name_index.h:31
uint64_t page_writes() const
Definition name_index.h:32
void Flush()
Writes the changed pages to the file.
Definition name_index.h:30
A file of fixed-size records, packed into pages after a header page, which holds how many records the...
Definition record_file.h:22
uint64_t page_reads() const
Definition record_file.h:53
uint64_t page_writes() const
Definition record_file.h:54
void Flush()
Writes the changed pages to the file.
Definition record_file.h:50
Names and the tokens that stand for them, kept in a file of records, and in memory both ways,...
Definition token_store.h:33
std::string Name(TokenId token) const
Returns the name token stands for, or nothing for kNoToken.
Definition token_store.h:51
uint64_t page_reads() const
Definition token_store.h:42
uint64_t page_writes() const
Definition token_store.h:43
static constexpr size_t kMaxName
The longest name a token can stand for.
Definition token_store.h:36
std::string_view label
Definition facts.cc:18
constexpr size_t kEmbeddingSize
How many numbers a node's embedding has.
Definition graph.h:90
uint32_t RelationshipId
Definition graph.h:21
std::array< float, kEmbeddingSize > Embedding
A node's embedding: a vector of numbers that stands for where the node is in the graph,...
Definition graph.h:96
uint32_t NodeId
Definition graph.h:20
Value ValueOf(const PropertyRecord &record)
Reads the value a property's record holds.
Definition graph.cc:41
std::variant< int64_t, std::string > Value
A property's value, as the graph hands it out: a whole number or a string.
Definition graph.h:100
constexpr RelationshipId kNoRelationship
The ids at the end of a chain, where there's no next relationship, or no next property.
Definition graph.h:26
uint32_t TokenId
A number that stands for a name many records share, such as the label Species, so a record keeps four...
Definition token_store.h:20
Direction
A node and a property take 64 bytes, and a relationship 24, so a page holds 170 relationships,...
Definition graph.h:72
constexpr PropertyId kNoProperty
Definition graph.h:27
Direction Reversed(Direction direction)
The direction to follow a relationship in from its other end: out becomes in, in becomes out,...
Definition graph.h:76
uint32_t PropertyId
Definition graph.h:22
Path path
Definition plan.cc:115
Direction direction
Definition query.cc:42
A step from a node along one of its relationships: the relationship, and the node at its other end.
Definition graph.h:84
RelationshipId relationship
Definition graph.h:85
NodeId node
Definition graph.h:86
A node on disk: its name, its label's token, which says what kind of node it is, the first of the rel...
Definition graph.h:32
RelationshipId first_out
Definition graph.h:35
RelationshipId first_in
Definition graph.h:36
PropertyId first_property
Definition graph.h:37
A property on disk: its key, its value, and the next property of the node or relationship it belongs ...
Definition graph.h:57
std::string key
Definition graph.h:103
A relationship on disk: its type's token, the nodes it joins, the next relationship that starts at fr...
Definition graph.h:43