smollerGraph 0.20.2
A small graph database, from scratch
Loading...
Searching...
No Matches
smollergraph Namespace Reference

Classes

class  BufferPool
 Keeps some of a file's pages in memory, in frames, so reading or changing a page there needs no system call. More...
 
struct  Cell
 A value a query returns, and the node it's a property of. More...
 
struct  Column
 A column of what a plan returns: its heading, such as b.name, p or length(p), and the property of the node in a slot that it returns, or with no slot, the row's path, or a function of it, its length or its cost. More...
 
struct  Comparison
 A comparison in WHERE, such as b.weight_g < 1000: a property, an operator, which is one of = <> < <= > >=, and a value. More...
 
class  Graph
 A graph kept in three files of records, one for nodes, one for relationships and one for their properties, with an index that finds a node by its name, and a store each for the tokens of labels and types. More...
 
struct  Hop
 A step from a node along one of its relationships: the relationship, and the node at its other end. More...
 
class  NameIndex
 An index from names to ids, kept in a file of pages as a B+ tree: leaves of names in order, each with its id, and pages above them that say which page holds which names. More...
 
struct  NodePattern
 A node in a pattern, such as (a:Animal {name: 'Red fox'}). More...
 
struct  NodeRecord
 A node on disk: its name, its label's token, which says what kind of node it is, the first of the relationships that start at it, the first of those that end at it, and the first of its properties. More...
 
class  PageHandle
 A page pinned in a buffer pool. More...
 
class  Pager
 A file of pages, each read and written whole, by its number. More...
 
struct  Path
 A path through the graph: its nodes, from its start to its end, and the relationships between them, one fewer. More...
 
struct  PathSearch
 What a search for a path found: the path, if there is one, and how many nodes the search reached, which is most of what it cost. More...
 
struct  Plan
 How to answer a query: where to start, and where to go from there. More...
 
struct  Property
 
struct  PropertyLookup
 A variable's property, such as a.name or b.weight_g. More...
 
struct  PropertyRecord
 A property on disk: its key, its value, and the next property of the node or relationship it belongs to, so their properties form a chain too. More...
 
struct  Query
 A query in our subset of Cypher, such as MATCH (a {name: 'Red fox'})-[:EATS]->(b) WHERE b.weight_g < 1000 RETURN DISTINCT b.name with EXPLAIN in front of it to ask for its plan. More...
 
class  RecordFile
 A file of fixed-size records, packed into pages after a header page, which holds how many records there are. More...
 
struct  RelationshipPattern
 A relationship from a node in a pattern to the next one, such as -[:EATS]->, or -[:EATS*2]->, which is two of them in a row. More...
 
struct  RelationshipRecord
 A relationship on disk: its type's token, the nodes it joins, the next relationship that starts at from and the next that ends at to, so it's in a chain of each of its nodes, then the first of its properties. More...
 
struct  Result
 What a query returns: a row for each match of its pattern, with a cell for each column. More...
 
struct  ReturnItem
 What a query returns in a column: a variable's property, such as b.name, or with no key, a path's variable, such as p, which returns the path, or a function of the path: length(p), how many relationships it has, or cost(p), what its weights add up to. More...
 
struct  Step
 One step of a plan. More...
 
struct  Token
 
struct  TokenRecord
 A token's name on disk. Its id in the file is the token. More...
 
class  TokenStore
 Names and the tokens that stand for them, kept in a file of records, and in memory both ways, as there are only a few of them. More...
 

Typedefs

using NodeId = uint32_t
 
using RelationshipId = uint32_t
 
using PropertyId = uint32_t
 
using Embedding = std::array< float, kEmbeddingSize >
 A node's embedding: a vector of numbers that stands for where the node is in the graph, so that nodes with similar neighbourhoods get vectors that point in similar directions.
 
using Value = std::variant< int64_t, std::string >
 A property's value, as the graph hands it out: a whole number or a string.
 
using PageId = uint32_t
 
using Page = std::array< std::byte, kPageSize >
 
using TokenId = uint32_t
 A number that stands for a name many records share, such as the label Species, so a record keeps four bytes in place of the name.
 

Enumerations

enum class  ValueType : uint8_t { kInteger , kString }
 
enum class  Direction { kOut , kIn , kBoth }
 A node and a property take 64 bytes, and a relationship 24, so a page holds 170 relationships, with 16 bytes left at its end. More...
 
enum class  TokenKind {
  kWord , kString , kNumber , kSymbol ,
  kEnd
}
 The kinds of token in our subset of Cypher. More...
 

Functions

std::string JsonString (std::string_view text)
 What sg serve answers with, as JSON.
 
std::string PropertiesJson (const std::vector< Property > &properties)
 Returns properties as a JSON object, with a field for each key.
 
std::string ErrorJson (std::string_view message)
 Returns an error's message as JSON, for the UI to show.
 
std::string NamesJson (const Graph &graph, std::string_view prefix)
 Returns a few of the graph's names that start with prefix, in order, for the UI to suggest.
 
absl::StatusOr< std::string > NodeJson (const Graph &graph, std::string_view id)
 Returns node id's record as it's kept on disk: its label, its page, its offset in the page and its bytes, then its chains of relationships out and in, newest first, each with its properties, the node at its other end and where it's kept, and its chain of properties, newest first, each with where it's kept.
 
absl::StatusOr< std::string > QueryJson (const Graph &graph, std::string_view text)
 
absl::StatusOr< std::string > SimilarJson (const Graph &graph, std::string_view id)
 Returns the kSimilar nodes whose embeddings are most like node id's, the most alike first, each with its name, its label and its cosine.
 
std::vector< Embedding > FastRP (const Graph &graph, std::string_view type, uint32_t seed)
 Returns an embedding for each node, in the order of their ids, computed with FastRP, as Neo4j's Graph Data Science library does: a sparse random vector for each node, then five rounds, in each of which a node's vector is the sum of its neighbours' vectors from the round before, along relationships of type followed both ways, scaled to a length of one.
 
float Cosine (const Embedding &a, const Embedding &b)
 How alike two embeddings are: the cosine of the angle between them, 1 when they point the same way, 0 at right angles, and -1 when they're opposite.
 
std::vector< std::pair< NodeId, float > > MostSimilar (const std::vector< Embedding > &embeddings, NodeId node, size_t k)
 Returns the k nodes whose embeddings are most like node's, with their cosines, the most alike first.
 
absl::Status Export (const Graph &graph, const std::string &path)
 Writes the graph into the folder at path, for programs outside the database to read, such as a script that trains a model: nodes.jsonl, a line of JSON for each node, with its id, name, label and properties, and relationships.csv, a line for each relationship, with the ids of the nodes it joins and its type.
 
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), or a node on its own, such as (Rabbit).
 
Direction Reversed (Direction direction)
 The direction to follow a relationship in from its other end: out becomes in, in becomes out, and both stays both.
 
Value ValueOf (const PropertyRecord &record)
 Reads the value a property's record holds.
 
absl::StatusOr< std::vector< Token > > Lex (std::string_view query)
 Splits a query into tokens, ending with a kEnd token.
 
absl::StatusOr< Query > Parse (std::string_view text)
 Reads a query.
 
absl::StatusOr< std::vector< Property > > ParseProperties (std::string_view text)
 Reads properties as Cypher writes them, in braces, such as {legs: 4, diet: 'omnivore'}, with a string or a whole number for each key.
 
absl::StatusOr< Plan > PlanQuery (const Query &query)
 Plans a query, starting from the node of its pattern that's cheapest to find, or for a shortest path, finding both its nodes, and then the path.
 
std::string Explain (const Plan &plan)
 The plan's steps, a line each, in the order they run, as EXPLAIN shows them.
 
std::string ValueText (const Value &value)
 A value as sg query prints it: a number in digits, and a string as it is.
 
Result Run (const Plan &plan, const Graph &graph)
 Runs a plan on a graph, a step at a time, each on all of the rows at once.
 
absl::StatusOr< Result > Execute (const Graph &graph, std::string_view text)
 Parses, plans and runs a query in our subset of Cypher on a graph, in one call, as Kùzu's connection does with execute.
 
std::vector< NodeId > Walk (const Graph &graph, NodeId start, std::string_view type, int hops)
 Returns the nodes exactly hops relationships of type away from start, each once, in the order a breadth-first walk reaches them.
 
std::vector< std::vector< NodeId > > WalkHops (const Graph &graph, NodeId start, std::string_view type, int hops)
 Returns the nodes each hop of that walk reaches: the start on its own, then the nodes one hop away, and so on, so the last of them is what Walk returns.
 
PathSearch ShortestPath (const Graph &graph, NodeId from, NodeId to, const std::vector< std::string > &types, Direction direction)
 Returns a shortest path from from to to, along relationships of any of types followed in direction: one with as few hops as any other.
 
PathSearch CheapestPath (const Graph &graph, NodeId from, NodeId to, const std::vector< std::string > &types, Direction direction, std::string_view key)
 Returns a cheapest path from from to to, along relationships of any of types followed in direction: one whose relationships' weights, their property key, add up to as little as any other's, found with Dijkstra's algorithm.
 
std::string PathText (const Graph &graph, const Path &path)
 A path as Cypher writes it, with the nodes' names, and each relationship's arrow pointing the way it goes, with its properties in braces.
 
absl::Status Serve (const Graph &graph, const std::string &address, int port)
 Answers HTTP requests for the graph on address and port, until the process is stopped.
 

Variables

constexpr size_t kMaxRows = 1000
 Parses, plans and runs a query in our subset of Cypher, from a request.
 
constexpr size_t kSimilar = 10
 How many of the most similar nodes the page draws.
 
constexpr size_t kMaxDrawn = 500
 
constexpr RelationshipId kNoRelationship = UINT32_MAX
 The ids at the end of a chain, where there's no next relationship, or no next property.
 
constexpr PropertyId kNoProperty = UINT32_MAX
 
constexpr size_t kEmbeddingSize = 256
 How many numbers a node's embedding has.
 
constexpr size_t kPageSize = 4096
 What the database reads and writes at a time: 4 KiB, the size of a block on most file systems and disks.
 
constexpr int kAnyHops = 0
 The hops of a relationship written with a star and no number, such as -[:KNOWS*]-, which stands for any number of them.
 
constexpr TokenId kNoToken = UINT32_MAX
 The token for no name, such as the label of a node that hasn't got one.
 

Typedef Documentation

◆ Embedding

using smollergraph::Embedding = typedef std::array<float, kEmbeddingSize>

A node's embedding: a vector of numbers that stands for where the node is in the graph, so that nodes with similar neighbourhoods get vectors that point in similar directions.

On disk, it's a record of its own, a kibibyte, which has the node's id, so four fit in a page.

Definition at line 96 of file graph.h.

◆ NodeId

using smollergraph::NodeId = typedef uint32_t

Definition at line 20 of file graph.h.

◆ Page

using smollergraph::Page = typedef std::array<std::byte, kPageSize>

Definition at line 20 of file pager.h.

◆ PageId

using smollergraph::PageId = typedef uint32_t

Definition at line 19 of file pager.h.

◆ PropertyId

using smollergraph::PropertyId = typedef uint32_t

Definition at line 22 of file graph.h.

◆ RelationshipId

using smollergraph::RelationshipId = typedef uint32_t

Definition at line 21 of file graph.h.

◆ TokenId

using smollergraph::TokenId = typedef uint32_t

A number that stands for a name many records share, such as the label Species, so a record keeps four bytes in place of the name.

Definition at line 20 of file token_store.h.

◆ Value

using smollergraph::Value = typedef std::variant<int64_t, std::string>

A property's value, as the graph hands it out: a whole number or a string.

Definition at line 100 of file graph.h.

Enumeration Type Documentation

◆ Direction

enum class smollergraph::Direction
strong

A node and a property take 64 bytes, and a relationship 24, so a page holds 170 relationships, with 16 bytes left at its end.

Which of a node's relationships to follow: those that start at it, those that end at it, or both.

Enumerator
kOut 
kIn 
kBoth 

Definition at line 72 of file graph.h.

◆ TokenKind

enum class smollergraph::TokenKind
strong

The kinds of token in our subset of Cypher.

Enumerator
kWord 
kString 
kNumber 
kSymbol 
kEnd 

Definition at line 14 of file lexer.h.

◆ ValueType

enum class smollergraph::ValueType : uint8_t
strong
Enumerator
kInteger 
kString 

Definition at line 52 of file graph.h.

Function Documentation

◆ CheapestPath()

PathSearch smollergraph::CheapestPath ( const Graph &  graph,
NodeId  from,
NodeId  to,
const std::vector< std::string > &  types,
Direction  direction,
std::string_view  key 
)

Returns a cheapest path from from to to, along relationships of any of types followed in direction: one whose relationships' weights, their property key, add up to as little as any other's, found with Dijkstra's algorithm.

A relationship without a weight that's a whole number of zero or more isn't followed.

Definition at line 196 of file query.cc.

◆ Cosine()

float smollergraph::Cosine ( const Embedding &  a,
const Embedding &  b 
)

How alike two embeddings are: the cosine of the angle between them, 1 when they point the same way, 0 at right angles, and -1 when they're opposite.

Definition at line 68 of file embed.cc.

◆ ErrorJson()

std::string smollergraph::ErrorJson ( std::string_view  message)

Returns an error's message as JSON, for the UI to show.

Definition at line 126 of file api.cc.

◆ Execute()

absl::StatusOr< Result > smollergraph::Execute ( const Graph &  graph,
std::string_view  text 
)

Parses, plans and runs a query in our subset of Cypher on a graph, in one call, as Kùzu's connection does with execute.

Returns the parser's or the planner's error for a query it can't run, and an error for a query with EXPLAIN in front, as that asks for the plan, which PlanQuery and Explain give.

Definition at line 387 of file plan.cc.

◆ Explain()

std::string smollergraph::Explain ( const Plan &  plan)

The plan's steps, a line each, in the order they run, as EXPLAIN shows them.

Definition at line 399 of file plan.cc.

◆ Export()

absl::Status smollergraph::Export ( const Graph &  graph,
const std::string &  path 
)

Writes the graph into the folder at path, for programs outside the database to read, such as a script that trains a model: nodes.jsonl, a line of JSON for each node, with its id, name, label and properties, and relationships.csv, a line for each relationship, with the ids of the nodes it joins and its type.

Definition at line 15 of file export.cc.

◆ FastRP()

std::vector< Embedding > smollergraph::FastRP ( const Graph &  graph,
std::string_view  type,
uint32_t  seed 
)

Returns an embedding for each node, in the order of their ids, computed with FastRP, as Neo4j's Graph Data Science library does: a sparse random vector for each node, then five rounds, in each of which a node's vector is the sum of its neighbours' vectors from the round before, along relationships of type followed both ways, scaled to a length of one.

The embedding adds up the rounds from the second on. A seed gives the same vectors each time.

Definition at line 25 of file embed.cc.

◆ JsonString()

std::string smollergraph::JsonString ( std::string_view  text)

What sg serve answers with, as JSON.

It's kept apart from HTTP, so the tests can check it without a server. Returns text as a JSON string: in quotes, with quotes, backslashes and control characters escaped.

Definition at line 90 of file api.cc.

◆ Lex()

absl::StatusOr< std::vector< Token > > smollergraph::Lex ( std::string_view  query)

Splits a query into tokens, ending with a kEnd token.

Returns an error that says what's wrong and where, at a character it doesn't know, or at a string with no closing quote.

Definition at line 25 of file lexer.cc.

◆ LoadFacts()

absl::Status smollergraph::LoadFacts ( std::istream &  in,
Graph &  graph 
)

Reads facts into graph, one a line, written as Cypher patterns such as (Red fox)-[:EATS]->(Rabbit), or a node on its own, such as (Rabbit).

A node can have a label after a colon, and nodes and relationships can have properties in braces, as in (Rabbit:Animal {legs: 4}). Blank lines and lines starting with # are skipped. On a line it can't read, it stops, and returns an error with the line as its message.

Definition at line 67 of file facts.cc.

◆ MostSimilar()

std::vector< std::pair< NodeId, float > > smollergraph::MostSimilar ( const std::vector< Embedding > &  embeddings,
NodeId  node,
size_t  k 
)

Returns the k nodes whose embeddings are most like node's, with their cosines, the most alike first.

It compares node with each of the others.

Definition at line 81 of file embed.cc.

◆ NamesJson()

std::string smollergraph::NamesJson ( const Graph &  graph,
std::string_view  prefix 
)

Returns a few of the graph's names that start with prefix, in order, for the UI to suggest.

Definition at line 116 of file api.cc.

◆ NodeJson()

absl::StatusOr< std::string > smollergraph::NodeJson ( const Graph &  graph,
std::string_view  id 
)

Returns node id's record as it's kept on disk: its label, its page, its offset in the page and its bytes, then its chains of relationships out and in, newest first, each with its properties, the node at its other end and where it's kept, and its chain of properties, newest first, each with where it's kept.

An id the graph doesn't have is an error.

Definition at line 130 of file api.cc.

◆ Parse()

absl::StatusOr< Query > smollergraph::Parse ( std::string_view  text)

Reads a query.

Returns an error that says what's wrong and where, when it isn't one our subset has.

Definition at line 284 of file parser.cc.

◆ ParseProperties()

absl::StatusOr< std::vector< Property > > smollergraph::ParseProperties ( std::string_view  text)

Reads properties as Cypher writes them, in braces, such as {legs: 4, diet: 'omnivore'}, with a string or a whole number for each key.

Definition at line 293 of file parser.cc.

◆ PathText()

std::string smollergraph::PathText ( const Graph &  graph,
const Path &  path 
)

A path as Cypher writes it, with the nodes' names, and each relationship's arrow pointing the way it goes, with its properties in braces.

Definition at line 239 of file query.cc.

◆ PlanQuery()

absl::StatusOr< Plan > smollergraph::PlanQuery ( const Query &  query)

Plans a query, starting from the node of its pattern that's cheapest to find, or for a shortest path, finding both its nodes, and then the path.

Returns an error when the query compares or returns a variable its pattern doesn't have, gives two nodes one variable, or has a pattern it can't plan.

Definition at line 209 of file plan.cc.

◆ PropertiesJson()

std::string smollergraph::PropertiesJson ( const std::vector< Property > &  properties)

Returns properties as a JSON object, with a field for each key.

Definition at line 107 of file api.cc.

◆ QueryJson()

absl::StatusOr< std::string > smollergraph::QueryJson ( const Graph &  graph,
std::string_view  text 
)

Definition at line 158 of file api.cc.

◆ Reversed()

Direction smollergraph::Reversed ( Direction  direction)
inline

The direction to follow a relationship in from its other end: out becomes in, in becomes out, and both stays both.

Definition at line 76 of file graph.h.

◆ Run()

Result smollergraph::Run ( const Plan &  plan,
const Graph &  graph 
)

Runs a plan on a graph, a step at a time, each on all of the rows at once.

Definition at line 291 of file plan.cc.

◆ Serve()

absl::Status smollergraph::Serve ( const Graph &  graph,
const std::string &  address,
int  port 
)

Answers HTTP requests for the graph on address and port, until the process is stopped.

Returns an error when it can't listen there.

Definition at line 32 of file server.cc.

◆ ShortestPath()

PathSearch smollergraph::ShortestPath ( const Graph &  graph,
NodeId  from,
NodeId  to,
const std::vector< std::string > &  types,
Direction  direction 
)

Returns a shortest path from from to to, along relationships of any of types followed in direction: one with as few hops as any other.

Definition at line 166 of file query.cc.

◆ SimilarJson()

absl::StatusOr< std::string > smollergraph::SimilarJson ( const Graph &  graph,
std::string_view  id 
)

Returns the kSimilar nodes whose embeddings are most like node id's, the most alike first, each with its name, its label and its cosine.

An id the graph doesn't have is an error, and so is a graph without embeddings.

Definition at line 234 of file api.cc.

◆ ValueOf()

Value smollergraph::ValueOf ( const PropertyRecord &  record)

Reads the value a property's record holds.

Definition at line 41 of file graph.cc.

◆ ValueText()

std::string smollergraph::ValueText ( const Value &  value)

A value as sg query prints it: a number in digits, and a string as it is.

Definition at line 202 of file plan.cc.

◆ Walk()

std::vector< NodeId > smollergraph::Walk ( const Graph &  graph,
NodeId  start,
std::string_view  type,
int  hops 
)

Returns the nodes exactly hops relationships of type away from start, each once, in the order a breadth-first walk reaches them.

Definition at line 142 of file query.cc.

◆ WalkHops()

std::vector< std::vector< NodeId > > smollergraph::WalkHops ( const Graph &  graph,
NodeId  start,
std::string_view  type,
int  hops 
)

Returns the nodes each hop of that walk reaches: the start on its own, then the nodes one hop away, and so on, so the last of them is what Walk returns.

Definition at line 148 of file query.cc.

Variable Documentation

◆ kAnyHops

constexpr int smollergraph::kAnyHops = 0
inlineconstexpr

The hops of a relationship written with a star and no number, such as -[:KNOWS*]-, which stands for any number of them.

Definition at line 24 of file parser.h.

◆ kEmbeddingSize

constexpr size_t smollergraph::kEmbeddingSize = 256
inlineconstexpr

How many numbers a node's embedding has.

Definition at line 90 of file graph.h.

◆ kMaxDrawn

constexpr size_t smollergraph::kMaxDrawn = 500
inlineconstexpr

Definition at line 47 of file api.h.

◆ kMaxRows

constexpr size_t smollergraph::kMaxRows = 1000
inlineconstexpr

Parses, plans and runs a query in our subset of Cypher, from a request.

It answers with the columns and up to kMaxRows of the rows, each value with the id of its node, and the nodes the rows return, a path's nodes among them, up to kMaxDrawn of them, with the relationships between them and their properties, for the page to draw. It says how long the query took on the server, and how many pages it read. After EXPLAIN, it answers with the plan, and it says what's wrong with a query it can't run.

Definition at line 44 of file api.h.

◆ kNoProperty

constexpr PropertyId smollergraph::kNoProperty = UINT32_MAX
inlineconstexpr

Definition at line 27 of file graph.h.

◆ kNoRelationship

constexpr RelationshipId smollergraph::kNoRelationship = UINT32_MAX
inlineconstexpr

The ids at the end of a chain, where there's no next relationship, or no next property.

Definition at line 26 of file graph.h.

◆ kNoToken

constexpr TokenId smollergraph::kNoToken = UINT32_MAX
inlineconstexpr

The token for no name, such as the label of a node that hasn't got one.

Definition at line 23 of file token_store.h.

◆ kPageSize

constexpr size_t smollergraph::kPageSize = 4096
inlineconstexpr

What the database reads and writes at a time: 4 KiB, the size of a block on most file systems and disks.

Definition at line 17 of file pager.h.

◆ kSimilar

constexpr size_t smollergraph::kSimilar = 10
inlineconstexpr

How many of the most similar nodes the page draws.

Definition at line 46 of file api.h.