smollerGraph 0.20.2
A small graph database, from scratch
Loading...
Searching...
No Matches
query.h
Go to the documentation of this file.
1#ifndef SMOLLERGRAPH_QUERY_H_
2#define SMOLLERGRAPH_QUERY_H_
3
4#include <cstddef>
5#include <cstdint>
6#include <optional>
7#include <string>
8#include <string_view>
9#include <vector>
10
11#include "smollergraph/graph.h"
12
13namespace smollergraph {
14
15// Returns the nodes exactly `hops` relationships of `type` away from `start`,
16// each once, in the order a breadth-first walk reaches them.
17std::vector<NodeId> Walk(const Graph& graph, NodeId start,
18 std::string_view type, int hops);
19
20// Returns the nodes each hop of that walk reaches: the start on its own, then
21// the nodes one hop away, and so on, so the last of them is what Walk returns.
22std::vector<std::vector<NodeId>> WalkHops(const Graph& graph, NodeId start,
23 std::string_view type, int hops);
24
25// A path through the graph: its nodes, from its start to its end, and the
26// relationships between them, one fewer. A cheapest path has its cost too,
27// its relationships' weights added up.
28struct Path {
29 std::vector<NodeId> nodes;
30 std::vector<RelationshipId> relationships;
31 int64_t cost = 0;
32};
33
34// What a search for a path found: the path, if there is one, and how many
35// nodes the search reached, which is most of what it cost.
36struct PathSearch {
37 std::optional<Path> path;
38 size_t reached = 0;
39};
40
41// Returns a shortest path from `from` to `to`, along relationships of any of
42// `types` followed in `direction`: one with as few hops as any other.
43PathSearch ShortestPath(const Graph& graph, NodeId from, NodeId to,
44 const std::vector<std::string>& types,
46
47// Returns a cheapest path from `from` to `to`, along relationships of any of
48// `types` followed in `direction`: one whose relationships' weights, their
49// property `key`, add up to as little as any other's, found with Dijkstra's
50// algorithm. A relationship without a weight that's a whole number of zero or
51// more isn't followed.
52PathSearch CheapestPath(const Graph& graph, NodeId from, NodeId to,
53 const std::vector<std::string>& types,
54 Direction direction, std::string_view key);
55
56// A path as Cypher writes it, with the nodes' names, and each relationship's
57// arrow pointing the way it goes, with its properties in braces.
58std::string PathText(const Graph& graph, const Path& path);
59
60} // namespace smollergraph
61
62#endif // SMOLLERGRAPH_QUERY_H_
A graph kept in three files of records, one for nodes, one for relationships and one for their proper...
Definition graph.h:113
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 ...
Definition query.cc:239
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 bread...
Definition query.cc:142
uint32_t NodeId
Definition graph.h:20
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: o...
Definition query.cc:196
Direction
A node and a property take 64 bytes, and a relationship 24, so a page holds 170 relationships,...
Definition graph.h:72
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: o...
Definition query.cc:166
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,...
Definition query.cc:148
Path path
Definition plan.cc:115
Direction direction
Definition query.cc:42
What a search for a path found: the path, if there is one, and how many nodes the search reached,...
Definition query.h:36
std::optional< Path > path
Definition query.h:37
A path through the graph: its nodes, from its start to its end, and the relationships between them,...
Definition query.h:28
std::vector< NodeId > nodes
Definition query.h:29
int64_t cost
Definition query.h:31
std::vector< RelationshipId > relationships
Definition query.h:30