memgraph demo transactional endpoint
This commit is contained in:
parent
763b3a143a
commit
a762b328b6
99
api/resources/demo.hpp
Normal file
99
api/resources/demo.hpp
Normal file
@ -0,0 +1,99 @@
|
||||
#pragma once
|
||||
|
||||
#include <unordered_map>
|
||||
|
||||
#include "api/restful/resource.hpp"
|
||||
#include "query_engine/query_stripper.hpp"
|
||||
#include "query_engine/query_stripped.hpp"
|
||||
#include "database/db.hpp"
|
||||
#include "utils/hashing/fnv.hpp"
|
||||
#include "threading/task.hpp"
|
||||
#include "storage/model/properties/all.hpp"
|
||||
|
||||
#pragma url /transaction/commit
|
||||
class Demo : public Resource<Demo, POST>
|
||||
{
|
||||
public:
|
||||
QueryStripper<int, int, int, int> stripper;
|
||||
std::unordered_map<uint64_t,
|
||||
std::function<std::string(const code_args_t&)>> query_f;
|
||||
|
||||
Demo(Task::sptr task, Db::sptr db) :
|
||||
Resource(task, db),
|
||||
stripper(make_query_stripper(TK_INT, TK_FLOAT, TK_STR, TK_BOOL))
|
||||
{
|
||||
query_f[7130961997552177283] = [db](const code_args_t& args) {
|
||||
auto& t = db->tx_engine.begin();
|
||||
auto vertex_accessor = db->graph.vertices.insert(t);
|
||||
vertex_accessor.property(
|
||||
"id", args[0]
|
||||
);
|
||||
t.commit();
|
||||
return "EXECUTED: CREATE (n{id:X}) RETURN n";
|
||||
};
|
||||
|
||||
query_f[11198568396549106428ull] = [db](const code_args_t& args) {
|
||||
auto& t = db->tx_engine.begin();
|
||||
auto id = args[0]->as<Int32>();
|
||||
std::cout << id.value << std::endl;
|
||||
auto vertex_accessor = db->graph.vertices.find(t, id.value);
|
||||
t.commit();
|
||||
return "EXECUTED: MATCH (n{id:X}) RETURN n";
|
||||
};
|
||||
|
||||
query_f[11637140396624918705ull] = [db](const code_args_t& args) {
|
||||
auto& t = db->tx_engine.begin();
|
||||
auto id = args[0]->as<Int32>();
|
||||
auto vertex_accessor = db->graph.vertices.find(t, id.value);
|
||||
if (!vertex_accessor) {
|
||||
t.commit();
|
||||
return "FAIL TO FIND NODE";
|
||||
}
|
||||
vertex_accessor.property(
|
||||
"prop", args[1]
|
||||
);
|
||||
t.commit();
|
||||
return "EXECUTED: MATCH (n{id:X}) SET n.prop=Y RETURN n";
|
||||
};
|
||||
|
||||
query_f[784140140862470291ull] = [db](const code_args_t& args) {
|
||||
auto& t = db->tx_engine.begin();
|
||||
auto id1 = args[0]->as<Int32>();
|
||||
auto v1 = db->graph.vertices.find(t, id1.value);
|
||||
if (!v1) {
|
||||
t.commit();
|
||||
return "FAIL TO FIND NODE";
|
||||
}
|
||||
auto id2 = args[1]->as<Int32>();
|
||||
auto v2 = db->graph.vertices.find(t, id2.value);
|
||||
if (!v2) {
|
||||
t.commit();
|
||||
return "FAIL TO FIND NODE";
|
||||
}
|
||||
auto edge_accessor = db->graph.edges.insert(t);
|
||||
v1.vlist->access(t).update()->data.out.add(edge_accessor.vlist);
|
||||
v2.vlist->access(t).update()->data.in.add(edge_accessor.vlist);
|
||||
edge_accessor.from(v1.vlist);
|
||||
edge_accessor.to(v2.vlist);
|
||||
edge_accessor.edge_type(EdgeType("test"));
|
||||
t.commit();
|
||||
return "EXECUTED: EDGE CREATED";
|
||||
};
|
||||
|
||||
query_f[16940444920835972350ull] = [db](const code_args_t& args) {
|
||||
auto& t = db->tx_engine.begin();
|
||||
auto id = args[0]->as<Int32>();
|
||||
auto v = db->graph.vertices.find(t, id.value);
|
||||
t.commit();
|
||||
return "EXECUTED: QUERY: MATCH (n{id:X})-[r]->(m) RETURN count(r)";
|
||||
};
|
||||
}
|
||||
|
||||
void post(sp::Request& req, sp::Response& res)
|
||||
{
|
||||
auto query = req.json["statements"][0]["statement"].GetString();
|
||||
auto strip = stripper.strip(query);
|
||||
auto hash = fnv(strip.query);
|
||||
return res.send(http::Status::Ok, query_f[hash](strip.arguments));
|
||||
}
|
||||
};
|
@ -18,8 +18,8 @@ int main()
|
||||
std::string("MATCH (n{id:3}) RETURN n"),
|
||||
std::string("MATCH (n{id:3}) SET n.prop = \"\" RETURN n"),
|
||||
std::string("MATCH (n{id:3}) DELETE n"),
|
||||
std::string("MATCH (n{id:3}),(m{id:2}) CREATE (n)-[r:TEST]->(m) RETURN r"),
|
||||
std::string("MATCH (n{id:3})-[r]->(m{id:2}) RETURN count(r)")
|
||||
std::string("MATCH (n{id:3}),(m{id:2}) CREATE (n)-[r:test]->(m) RETURN r"),
|
||||
std::string("MATCH (n{id:3})-[r]->(m) RETURN count(r)")
|
||||
}};
|
||||
|
||||
auto stripper = make_query_stripper(TK_INT, TK_FLOAT, TK_STR, TK_BOOL);
|
||||
@ -33,25 +33,25 @@ int main()
|
||||
}
|
||||
|
||||
auto create_node = [&db](uint64_t id) {
|
||||
auto& t = db.tx_engine.begin();
|
||||
auto vertex_accessor = db.graph.vertices.insert(t);
|
||||
vertex_accessor.property<Int64>(
|
||||
"id", Int64(id)
|
||||
);
|
||||
t.commit();
|
||||
auto& t = db.tx_engine.begin();
|
||||
auto vertex_accessor = db.graph.vertices.insert(t);
|
||||
vertex_accessor.property<Int64>(
|
||||
"id", Int64(id)
|
||||
);
|
||||
t.commit();
|
||||
return vertex_accessor;
|
||||
};
|
||||
|
||||
auto find_node = [&db](uint64_t id) {
|
||||
auto& t = db.tx_engine.begin();
|
||||
auto vertex_accessor = db.graph.vertices.find(t, id);
|
||||
t.commit();
|
||||
auto vertex_accessor = db.graph.vertices.find(t, id);
|
||||
t.commit();
|
||||
return vertex_accessor;
|
||||
};
|
||||
|
||||
auto edit_node = [&db](uint64_t id, std::string prop) {
|
||||
auto& t = db.tx_engine.begin();
|
||||
auto vertex_accessor = db.graph.vertices.find(t, id);
|
||||
auto vertex_accessor = db.graph.vertices.find(t, id);
|
||||
if (!vertex_accessor) {
|
||||
t.commit();
|
||||
return vertex_accessor;
|
||||
@ -65,7 +65,7 @@ int main()
|
||||
|
||||
auto delete_node = [&db](uint64_t id) {
|
||||
auto& t = db.tx_engine.begin();
|
||||
auto vertex_accessor = db.graph.vertices.find(t, id);
|
||||
auto vertex_accessor = db.graph.vertices.find(t, id);
|
||||
if (!vertex_accessor)
|
||||
return vertex_accessor;
|
||||
vertex_accessor.remove(t);
|
||||
@ -74,7 +74,7 @@ int main()
|
||||
};
|
||||
|
||||
auto create_edge = [&db](uint64_t id1, uint64_t id2, std::string type) {
|
||||
auto& t = db.tx_engine.begin();
|
||||
auto& t = db.tx_engine.begin();
|
||||
auto v1 = db.graph.vertices.find(t, id1);
|
||||
if (!v1)
|
||||
return Edge::Accessor();
|
||||
@ -91,7 +91,7 @@ int main()
|
||||
return edge_accessor;
|
||||
};
|
||||
|
||||
auto count_edges = [&db](uint64_t id) {
|
||||
auto vertex_out_degree = [&db](uint64_t id) {
|
||||
auto& t = db.tx_engine.begin();
|
||||
auto v = db.graph.vertices.find(t, id);
|
||||
t.commit();
|
||||
@ -112,7 +112,7 @@ int main()
|
||||
auto edge_accessor = create_edge(0, 1, "test");
|
||||
if (edge_accessor)
|
||||
cout << edge_accessor.record->data.edge_type << endl;
|
||||
cout << count_edges(0) << endl;
|
||||
cout << vertex_out_degree(0) << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -30,7 +30,7 @@ public:
|
||||
const Id& id() const
|
||||
{
|
||||
assert(!empty());
|
||||
return vlist->id();
|
||||
return vlist->id;
|
||||
}
|
||||
|
||||
Derived update(tx::Transaction& t) const
|
||||
|
Loading…
Reference in New Issue
Block a user