12#include "absl/status/status.h"
13#include "absl/status/statusor.h"
14#include "absl/strings/numbers.h"
15#include "absl/strings/str_cat.h"
16#include "absl/strings/str_replace.h"
28using Clock = std::chrono::steady_clock;
32 <<
"usage: sg load <graph folder> <facts file>\n"
33 " sg walk <graph folder> <start> <relationship type> <hops>\n"
34 " sg path <graph folder> <from> <to> <relationship type> "
36 " sg embed <graph folder> <relationship type>\n"
37 " sg similar <graph folder> <name> <k>\n"
38 " sg export <graph folder> <folder>\n"
39 " sg bench <graph folder> <starts file> <query with $name>\n"
40 " sg serve <graph folder> <port> [address]\n"
41 " sg query <graph folder> <query>\n";
46bool ParseHops(std::string_view
text,
int& hops) {
47 if (absl::SimpleAtoi(
text, &hops) && hops >= 0)
return true;
48 std::cerr <<
"sg: hops must be a whole number, not " <<
text <<
'\n';
54 std::ifstream file(
path);
56 std::cerr <<
"sg: can't open " <<
path <<
'\n';
59 Clock::time_point start = Clock::now();
62 std::cerr <<
"sg: can't read the fact " << status.message() <<
'\n';
68 std::chrono::duration<double> took = Clock::now() - start;
70 <<
" relationships, in " << std::fixed << std::setprecision(1)
71 << took.count() <<
" s\n"
79 std::string_view
text) {
80 std::optional<smollergraph::NodeId> node = graph.
Find(start);
82 std::cerr <<
"sg: there's no node called " << start <<
'\n';
86 if (!ParseHops(
text, hops))
return 1;
89 std::cout << graph.
name(found) <<
'\n';
99 const char* type,
const char* weight) {
100 std::optional<smollergraph::NodeId> start = graph.
Find(from);
101 std::optional<smollergraph::NodeId> end = graph.
Find(to);
102 if (!start || !end) {
103 std::cerr <<
"sg: there's no node called " << (start ? to : from) <<
'\n';
106 Clock::time_point begin = Clock::now();
114 std::chrono::duration<double, std::milli> took = Clock::now() - begin;
117 << search.
path->relationships.size() <<
" hops, ";
118 if (weight !=
nullptr) std::cout <<
"cost " << search.
path->cost <<
", ";
120 std::cout <<
"No path, ";
122 std::cout << search.
reached <<
" nodes reached in " << std::fixed
123 << std::setprecision(2) << took.count() <<
" ms, "
124 << graph.
page_reads() - reads <<
" pages read\n";
131 Clock::time_point begin = Clock::now();
132 std::vector<smollergraph::Embedding> embeddings =
138 std::chrono::duration<double> took = Clock::now() - begin;
139 std::cout << embeddings.size() <<
" embeddings of "
141 << std::setprecision(1) << took.count() <<
" s\n"
150 std::string_view
text) {
151 std::optional<smollergraph::NodeId> node = graph.
Find(name);
153 std::cerr <<
"sg: there's no node called " << name <<
'\n';
157 if (!absl::SimpleAtoi(
text, &k) || k < 1) {
158 std::cerr <<
"sg: k is a number of nodes, not " <<
text <<
'\n';
161 std::vector<smollergraph::Embedding> embeddings = graph.
Embeddings();
162 if (embeddings.empty()) {
163 std::cerr <<
"sg: the graph has no embeddings, which sg embed keeps\n";
167 std::cout << std::fixed << std::setprecision(3) << cosine <<
" "
168 << graph.
name(other) <<
" (" << graph.
label(other) <<
")\n";
176 Clock::time_point begin = Clock::now();
178 std::cerr <<
"sg: " << status.message() <<
'\n';
181 std::chrono::duration<double, std::milli> took = Clock::now() - begin;
183 <<
" relationships, in " << std::fixed << std::setprecision(1)
184 << took.count() <<
" ms\n";
190 std::string_view
text) {
192 if (!query.ok())
return query.status();
194 if (!plan.ok())
return plan.status();
204 std::string_view
text,
double open_ms) {
205 std::ifstream file(
path);
207 std::cerr <<
"sg: can't open " <<
path <<
'\n';
210 std::vector<std::string> queries;
211 for (std::string name; std::getline(file, name);) {
213 absl::StrReplaceAll(
text, {{
"$name", absl::StrCat(
"'", name,
"'")}}));
215 if (queries.empty()) {
216 std::cerr <<
"sg: " <<
path <<
" names no nodes\n";
221 for (
const std::string& query : queries) {
222 if (absl::StatusOr<size_t> rows = CountRows(graph, query); !rows.ok()) {
223 std::cerr <<
"sg: " << rows.status().message() <<
'\n';
228 std::vector<double> micros;
230 for (
const std::string& query : queries) {
231 Clock::time_point begin = Clock::now();
232 results += CountRows(graph, query).value_or(0);
233 std::chrono::duration<double, std::micro> took = Clock::now() - begin;
234 micros.push_back(took.count());
236 double reads =
static_cast<double>(graph.
page_reads() - reads_before);
237 std::sort(micros.begin(), micros.end());
238 std::cout << std::fixed << std::setprecision(1) <<
"{\"open_ms\": " << open_ms
239 <<
", \"queries\": " << micros.size()
240 <<
", \"results\": " << results
241 <<
", \"median_us\": " << micros[micros.size() / 2]
242 <<
", \"p95_us\": " << micros[micros.size() * 95 / 100]
243 <<
", \"page_reads\": " << reads / micros.size() <<
"}\n";
250 const char* address) {
252 if (!absl::SimpleAtoi(
text, &port) || port < 1 || port > 65535) {
253 std::cerr <<
"sg: a port is a number from 1 to 65535, not " <<
text <<
'\n';
258 std::cerr <<
"sg: " << status.message() <<
'\n';
267 Clock::time_point start = Clock::now();
270 absl::StatusOr<smollergraph::Plan> plan =
273 std::cerr <<
"sg: " << plan.status().message() <<
'\n';
276 if (query->explain) {
281 std::chrono::duration<double, std::milli> took = Clock::now() - start;
282 for (
size_t i = 0; i < result.
columns.size(); ++i) {
283 std::cout << (i > 0 ?
" | " :
"") << result.
columns[i];
286 for (
const std::vector<smollergraph::Cell>& row : result.rows) {
287 for (
size_t i = 0; i < row.size(); ++i) {
288 const std::optional<smollergraph::Value>& value = row[i].value;
289 std::cout << (i > 0 ?
" | " :
"")
297 std::cout << result.
rows.size()
298 << (result.
rows.size() == 1 ?
" row" :
" rows") <<
" in "
299 << std::fixed << std::setprecision(2) << took.count() <<
" ms, "
300 << pages << (pages == 1 ?
" page" :
" pages") <<
" read\n";
306int main(
int argc,
char** argv) {
307 std::string_view command = argc > 1 ? argv[1] :
"";
308 bool load = command ==
"load" && argc == 4;
309 bool walk = command ==
"walk" && argc == 6;
310 bool path = command ==
"path" && (argc == 6 || argc == 7);
311 bool embed = command ==
"embed" && argc == 4;
312 bool similar = command ==
"similar" && argc == 5;
313 bool to_folder = command ==
"export" && argc == 4;
314 bool bench = command ==
"bench" && argc == 5;
315 bool serve = command ==
"serve" && (argc == 4 || argc == 5);
316 bool query = command ==
"query" && argc == 4;
317 if (!load && !walk && !
path && !embed && !similar && !to_folder && !bench &&
323 Clock::time_point opening = Clock::now();
324 if (absl::Status status = graph.
Open(argv[2]); !status.ok()) {
325 std::cerr <<
"sg: can't open the graph in " << argv[2] <<
": "
326 << status.message() <<
'\n';
329 std::chrono::duration<double, std::milli> opened = Clock::now() - opening;
330 if (load)
return Load(graph, argv[3]);
331 if (walk)
return Walk(graph, argv[3], argv[4], argv[5]);
333 return FindPath(graph, argv[3], argv[4], argv[5],
334 argc == 7 ? argv[6] :
nullptr);
336 if (embed)
return Embed(graph, argv[3]);
337 if (similar)
return Similar(graph, argv[3], argv[4]);
338 if (to_folder)
return ExportTo(graph, argv[3]);
339 if (serve)
return Serve(graph, argv[3], argc == 5 ? argv[4] :
"127.0.0.1");
340 if (query)
return RunQuery(graph, argv[3]);
341 return Bench(graph, argv[3], argv[4], opened.count());
A graph kept in three files of records, one for nodes, one for relationships and one for their proper...
uint64_t page_writes() const
size_t relationship_count() const
size_t node_count() const
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::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.
uint64_t page_reads() const
How many pages the graph's files have read and written.
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.
int main(int argc, char **argv)
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 ...
constexpr size_t kEmbeddingSize
How many numbers a node's embedding has.
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,...
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...
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< Query > Parse(std::string_view text)
Reads a query.
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.
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...
std::string ValueText(const Value &value)
A value as sg query prints it: a number in digits, and a string as it is.
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,...
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...
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,...
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.
std::string Explain(const Plan &plan)
The plan's steps, a line each, in the order they run, as EXPLAIN shows them.
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),...
What a search for a path found: the path, if there is one, and how many nodes the search reached,...
std::optional< Path > path
What a query returns: a row for each match of its pattern, with a cell for each column.
std::vector< std::vector< Cell > > rows
std::vector< std::string > columns