smollerGraph 0.20.2
A small graph database, from scratch
Loading...
Searching...
No Matches
web.cc
Go to the documentation of this file.
1// smollerGraph in a browser: the graph runs in the page, as WebAssembly, and
2// answers the explorer's questions as sg serve answers them over HTTP. The
3// page's files live in Emscripten's file system, in memory, so the graph is
4// built from the facts each time the page opens, and nothing is kept.
5
6#include <emscripten/bind.h>
7
8#include <chrono>
9#include <fstream>
10#include <string>
11#include <vector>
12
13#include "absl/status/status.h"
14#include "absl/status/statusor.h"
15#include "absl/strings/str_cat.h"
16#include "smollergraph/api.h"
17#include "smollergraph/embed.h"
18#include "smollergraph/facts.h"
19#include "smollergraph/graph.h"
20
21namespace smollergraph {
22namespace {
23
24// The page's one graph. It's never destroyed, as the page just closes.
25Graph& TheGraph() {
26 static Graph* graph = new Graph;
27 return *graph;
28}
29
30// An answer as sg serve sends it: the JSON, or the error's message as JSON.
31std::string Answer(const absl::StatusOr<std::string>& json) {
32 return json.ok() ? *json : ErrorJson(json.status().message());
33}
34
35// Builds the graph in `folder` from the facts at `facts`, gives each node an
36// embedding along relationships of `type`, and says how big it is and how
37// long that took, in milliseconds.
38std::string Build(const std::string& folder, const std::string& facts,
39 const std::string& type) {
40 auto start = std::chrono::steady_clock::now();
41 Graph& graph = TheGraph();
42 if (absl::Status status = graph.Open(folder); !status.ok()) {
43 return ErrorJson(status.message());
44 }
45 std::ifstream file(facts);
46 if (absl::Status status = LoadFacts(file, graph); !status.ok()) {
47 return ErrorJson(status.message());
48 }
49 std::vector<Embedding> embeddings = FastRP(graph, type, 42);
50 for (NodeId node = 0; node < embeddings.size(); ++node) {
51 graph.SetEmbedding(node, embeddings[node]);
52 }
53 graph.Flush();
54 std::chrono::duration<double, std::milli> took =
55 std::chrono::steady_clock::now() - start;
56 return absl::StrCat("{\"nodes\":", graph.node_count(),
57 ",\"relationships\":", graph.relationship_count(),
58 ",\"ms\":", static_cast<int>(took.count()), "}");
59}
60
61std::string Names(const std::string& prefix) {
62 return NamesJson(TheGraph(), prefix);
63}
64
65std::string Query(const std::string& text) {
66 return Answer(QueryJson(TheGraph(), text));
67}
68
69std::string Node(const std::string& id) {
70 return Answer(NodeJson(TheGraph(), id));
71}
72
73std::string Similar(const std::string& id) {
74 return Answer(SimilarJson(TheGraph(), id));
75}
76
77} // namespace
78} // namespace smollergraph
79
81 emscripten::function("build", &smollergraph::Build);
82 emscripten::function("names", &smollergraph::Names);
83 emscripten::function("query", &smollergraph::Query);
84 emscripten::function("node", &smollergraph::Node);
85 emscripten::function("similar", &smollergraph::Similar);
86}
std::string_view text
Definition facts.cc:17
uint32_t NodeId
Definition graph.h:20
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,...
Definition api.cc:234
absl::StatusOr< std::string > QueryJson(const Graph &graph, std::string_view text)
Definition api.cc:158
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 by...
Definition api.cc:130
std::string ErrorJson(std::string_view message)
Returns an error's message as JSON, for the UI to show.
Definition api.cc:126
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,...
Definition embed.cc:25
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.
Definition api.cc:116
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),...
Definition facts.cc:67
A query in our subset of Cypher, such as MATCH (a {name: 'Red fox'})-[:EATS]->(b) WHERE b....
Definition parser.h:71
EMSCRIPTEN_BINDINGS(smollergraph)
Definition web.cc:80