1#ifndef SMOLLERGRAPH_GRAPH_H_
2#define SMOLLERGRAPH_GRAPH_H_
13#include "absl/status/status.h"
100using Value = std::variant<int64_t, std::string>;
126 absl::Status
Open(
const std::string&
path);
132 relationships_.Flush();
152 std::string_view key,
const Value& value);
165 std::string_view key)
const;
173 std::optional<NodeId>
Find(std::string_view
name)
const;
177 size_t limit)
const {
184 std::vector<Hop>
Hops(
NodeId node,
const std::vector<std::string>& types,
192 return labels_.
Name(nodes_.Read(
node).label);
199 return relationships_.Read(
id);
206 return embeddings_.
page_reads() + nodes_.page_reads() +
207 relationships_.page_reads() + properties_.page_reads() +
211 return embeddings_.
page_writes() + nodes_.page_writes() +
212 relationships_.page_writes() + properties_.page_writes() +
220 std::vector<Property> Chain(
PropertyId first)
const;
223 std::optional<Value> Lookup(
PropertyId first, std::string_view key)
const;
227 void Follow(
RelationshipId first,
bool out,
const std::vector<TokenId>& types,
228 std::vector<Hop>& hops)
const;
A graph kept in three files of records, one for nodes, one for relationships and one for their proper...
NodeId AddNode(std::string_view name)
Returns the node with this name, adding it the first time.
static constexpr size_t kMaxType
uint64_t page_writes() const
void SetRelationshipProperty(RelationshipId relationship, std::string_view key, const Value &value)
std::vector< std::string > NamesStartingWith(std::string_view prefix, size_t limit) const
Returns up to limit names that start with prefix, in order.
size_t relationship_count() const
size_t node_count() const
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.
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.
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::string label(NodeId node) const
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.
static constexpr size_t kMaxString
std::string name(NodeId node) const
void Flush()
Writes the graph's changed pages to its files, which closing it does too.
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.
uint64_t page_reads() const
How many pages the graph's files have read and written.
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.
An index from names to ids, kept in a file of pages as a B+ tree: leaves of names in order,...
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
uint64_t page_writes() const
void Flush()
Writes the changed pages to the file.
A file of fixed-size records, packed into pages after a header page, which holds how many records the...
uint64_t page_reads() const
uint64_t page_writes() const
void Flush()
Writes the changed pages to the file.
Names and the tokens that stand for them, kept in a file of records, and in memory both ways,...
std::string Name(TokenId token) const
Returns the name token stands for, or nothing for kNoToken.
uint64_t page_reads() const
uint64_t page_writes() const
static constexpr size_t kMaxName
The longest name a token can stand for.
constexpr size_t kEmbeddingSize
How many numbers a node's embedding has.
std::array< float, kEmbeddingSize > Embedding
A node's embedding: a vector of numbers that stands for where the node is in the graph,...
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.
uint32_t TokenId
A number that stands for a name many records share, such as the label Species, so a record keeps four...
Direction
A node and a property take 64 bytes, and a relationship 24, so a page holds 170 relationships,...
constexpr PropertyId kNoProperty
Direction Reversed(Direction direction)
The direction to follow a relationship in from its other end: out becomes in, in becomes out,...
A step from a node along one of its relationships: the relationship, and the node at its other end.
RelationshipId relationship
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