2016-07-07 08:58:26 +08:00
|
|
|
#pragma once
|
|
|
|
|
2016-08-28 22:47:13 +08:00
|
|
|
#include <iostream>
|
|
|
|
#include <map>
|
2016-07-07 08:58:26 +08:00
|
|
|
|
2016-08-28 22:47:13 +08:00
|
|
|
#include "barrier/barrier.hpp"
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
namespace barrier
|
|
|
|
{
|
2016-08-10 03:29:03 +08:00
|
|
|
auto load_queries(Db &db)
|
2016-07-07 08:58:26 +08:00
|
|
|
{
|
|
|
|
std::map<uint64_t, std::function<bool(const properties_t &)>> queries;
|
|
|
|
|
2016-08-01 01:58:12 +08:00
|
|
|
// CREATE (n {prop: 0}) RETURN n)
|
|
|
|
auto create_node = [&db](const properties_t &args) {
|
2016-08-13 06:01:39 +08:00
|
|
|
DbAccessor t(db);
|
2016-08-20 01:40:04 +08:00
|
|
|
auto prop_key = t.vertex_property_key("prop", args[0]->flags);
|
2016-08-18 22:34:36 +08:00
|
|
|
|
2016-08-13 06:01:39 +08:00
|
|
|
auto vertex_accessor = t.vertex_insert();
|
2016-08-18 22:34:36 +08:00
|
|
|
vertex_accessor.set(prop_key, args[0]);
|
2016-08-30 09:32:31 +08:00
|
|
|
return t.commit();
|
2016-08-01 01:58:12 +08:00
|
|
|
};
|
|
|
|
queries[11597417457737499503u] = create_node;
|
|
|
|
|
2016-07-07 08:58:26 +08:00
|
|
|
auto create_labeled_and_named_node = [&db](const properties_t &args) {
|
2016-08-13 06:01:39 +08:00
|
|
|
DbAccessor t(db);
|
2016-08-20 01:40:04 +08:00
|
|
|
auto prop_key = t.vertex_property_key("name", args[0]->flags);
|
|
|
|
auto &label = t.label_find_or_create("LABEL");
|
2016-08-18 22:34:36 +08:00
|
|
|
|
2016-08-13 06:01:39 +08:00
|
|
|
auto vertex_accessor = t.vertex_insert();
|
2016-08-18 22:34:36 +08:00
|
|
|
vertex_accessor.set(prop_key, args[0]);
|
2016-07-07 08:58:26 +08:00
|
|
|
vertex_accessor.add_label(label);
|
2016-08-28 22:47:13 +08:00
|
|
|
// cout_properties(vertex_accessor.properties());
|
2016-08-30 09:32:31 +08:00
|
|
|
return t.commit();
|
2016-07-07 08:58:26 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
auto create_account = [&db](const properties_t &args) {
|
2016-08-13 06:01:39 +08:00
|
|
|
DbAccessor t(db);
|
2016-08-20 01:40:04 +08:00
|
|
|
auto prop_id = t.vertex_property_key("id", args[0]->flags);
|
|
|
|
auto prop_name = t.vertex_property_key("name", args[1]->flags);
|
|
|
|
auto prop_country = t.vertex_property_key("country", args[2]->flags);
|
|
|
|
auto prop_created = t.vertex_property_key("created_at", args[3]->flags);
|
|
|
|
auto &label = t.label_find_or_create("ACCOUNT");
|
2016-08-18 22:34:36 +08:00
|
|
|
|
2016-08-13 06:01:39 +08:00
|
|
|
auto vertex_accessor = t.vertex_insert();
|
2016-08-18 22:34:36 +08:00
|
|
|
vertex_accessor.set(prop_id, args[0]);
|
|
|
|
vertex_accessor.set(prop_name, args[1]);
|
|
|
|
vertex_accessor.set(prop_country, args[2]);
|
|
|
|
vertex_accessor.set(prop_created, args[3]);
|
2016-07-07 08:58:26 +08:00
|
|
|
vertex_accessor.add_label(label);
|
2016-08-28 22:47:13 +08:00
|
|
|
// cout_properties(vertex_accessor.properties());
|
2016-08-30 09:32:31 +08:00
|
|
|
return t.commit();
|
2016-07-07 08:58:26 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
auto find_node_by_internal_id = [&db](const properties_t &args) {
|
2016-08-13 06:01:39 +08:00
|
|
|
DbAccessor t(db);
|
2016-08-18 22:34:36 +08:00
|
|
|
auto maybe_va = t.vertex_find(Id(args[0]->as<Int32>().value));
|
2016-08-15 07:09:58 +08:00
|
|
|
if (!option_fill(maybe_va)) {
|
2016-07-07 08:58:26 +08:00
|
|
|
cout << "vertex doesn't exist" << endl;
|
|
|
|
t.commit();
|
|
|
|
return false;
|
|
|
|
}
|
2016-08-15 07:09:58 +08:00
|
|
|
auto vertex_accessor = maybe_va.get();
|
2016-08-28 22:47:13 +08:00
|
|
|
// cout_properties(vertex_accessor.properties());
|
2016-07-07 08:58:26 +08:00
|
|
|
cout << "LABELS:" << endl;
|
|
|
|
for (auto label_ref : vertex_accessor.labels()) {
|
2016-08-28 22:47:13 +08:00
|
|
|
// cout << label_ref.get() << endl;
|
2016-07-07 08:58:26 +08:00
|
|
|
}
|
2016-08-30 09:32:31 +08:00
|
|
|
return t.commit();
|
2016-07-07 08:58:26 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
auto create_edge = [&db](const properties_t &args) {
|
2016-08-13 06:01:39 +08:00
|
|
|
DbAccessor t(db);
|
2016-08-20 01:40:04 +08:00
|
|
|
auto &edge_type = t.type_find_or_create("IS");
|
2016-07-07 08:58:26 +08:00
|
|
|
|
2016-08-13 06:01:39 +08:00
|
|
|
auto v1 = t.vertex_find(args[0]->as<Int32>().value);
|
2016-08-15 07:09:58 +08:00
|
|
|
if (!option_fill(v1)) return t.commit(), false;
|
2016-07-07 08:58:26 +08:00
|
|
|
|
2016-08-13 06:01:39 +08:00
|
|
|
auto v2 = t.vertex_find(args[1]->as<Int32>().value);
|
2016-08-15 07:09:58 +08:00
|
|
|
if (!option_fill(v2)) return t.commit(), false;
|
2016-07-07 08:58:26 +08:00
|
|
|
|
2016-08-15 07:09:58 +08:00
|
|
|
auto edge_accessor = t.edge_insert(v1.get(), v2.get());
|
2016-07-07 08:58:26 +08:00
|
|
|
|
2016-07-24 10:47:48 +08:00
|
|
|
edge_accessor.edge_type(edge_type);
|
2016-07-07 08:58:26 +08:00
|
|
|
|
2016-08-30 09:32:31 +08:00
|
|
|
bool ret = t.commit();
|
2016-07-07 08:58:26 +08:00
|
|
|
|
2016-08-28 22:47:13 +08:00
|
|
|
// cout << edge_accessor.edge_type() << endl;
|
2016-07-24 10:47:48 +08:00
|
|
|
|
2016-08-28 22:47:13 +08:00
|
|
|
// cout_properties(edge_accessor.properties());
|
2016-07-07 08:58:26 +08:00
|
|
|
|
2016-08-30 09:32:31 +08:00
|
|
|
return ret;
|
2016-07-07 08:58:26 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
auto find_edge_by_internal_id = [&db](const properties_t &args) {
|
2016-08-13 06:01:39 +08:00
|
|
|
DbAccessor t(db);
|
2016-08-15 07:09:58 +08:00
|
|
|
auto maybe_ea = t.edge_find(args[0]->as<Int32>().value);
|
|
|
|
if (!option_fill(maybe_ea)) return t.commit(), false;
|
|
|
|
auto edge_accessor = maybe_ea.get();
|
2016-07-07 08:58:26 +08:00
|
|
|
|
|
|
|
// print edge type and properties
|
2016-08-28 22:47:13 +08:00
|
|
|
// cout << "EDGE_TYPE: " << edge_accessor.edge_type() << endl;
|
2016-07-07 08:58:26 +08:00
|
|
|
|
2016-07-24 10:47:48 +08:00
|
|
|
auto from = edge_accessor.from();
|
2016-08-15 07:09:58 +08:00
|
|
|
if (!from.fill()) return t.commit(), false;
|
|
|
|
|
2016-07-07 08:58:26 +08:00
|
|
|
cout << "FROM:" << endl;
|
2016-08-28 22:47:13 +08:00
|
|
|
// cout_properties(from->data.props);
|
2016-07-07 08:58:26 +08:00
|
|
|
|
2016-07-24 10:47:48 +08:00
|
|
|
auto to = edge_accessor.to();
|
2016-08-15 07:09:58 +08:00
|
|
|
if (!to.fill()) return t.commit(), false;
|
|
|
|
|
2016-07-07 08:58:26 +08:00
|
|
|
cout << "TO:" << endl;
|
2016-08-28 22:47:13 +08:00
|
|
|
// cout_properties(to->data.props);
|
2016-07-07 08:58:26 +08:00
|
|
|
|
2016-08-30 09:32:31 +08:00
|
|
|
return t.commit();
|
2016-07-07 08:58:26 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
auto update_node = [&db](const properties_t &args) {
|
2016-08-13 06:01:39 +08:00
|
|
|
DbAccessor t(db);
|
2016-08-20 01:40:04 +08:00
|
|
|
auto prop_name = t.vertex_property_key("name", args[1]->flags);
|
2016-07-07 08:58:26 +08:00
|
|
|
|
2016-08-15 07:09:58 +08:00
|
|
|
auto maybe_v = t.vertex_find(args[0]->as<Int32>().value);
|
|
|
|
if (!option_fill(maybe_v)) return t.commit(), false;
|
|
|
|
auto v = maybe_v.get();
|
2016-07-07 08:58:26 +08:00
|
|
|
|
2016-08-18 22:34:36 +08:00
|
|
|
v.set(prop_name, args[1]);
|
2016-08-28 22:47:13 +08:00
|
|
|
// cout_properties(v.properties());
|
2016-07-07 08:58:26 +08:00
|
|
|
|
2016-08-30 09:32:31 +08:00
|
|
|
return t.commit();
|
2016-07-07 08:58:26 +08:00
|
|
|
};
|
|
|
|
|
2016-08-10 03:29:03 +08:00
|
|
|
// MATCH (n1), (n2) WHERE ID(n1)=0 AND ID(n2)=1 CREATE (n1)<-[r:IS {age: 25,
|
|
|
|
// weight: 70}]-(n2) RETURN r
|
|
|
|
auto create_edge_v2 = [&db](const properties_t &args) {
|
2016-08-13 06:01:39 +08:00
|
|
|
DbAccessor t(db);
|
2016-08-19 18:28:10 +08:00
|
|
|
|
2016-08-20 01:40:04 +08:00
|
|
|
auto prop_age = t.edge_property_key("age", args[2]->flags);
|
|
|
|
auto prop_weight = t.edge_property_key("weight", args[3]->flags);
|
2016-08-18 22:34:36 +08:00
|
|
|
|
2016-08-13 06:01:39 +08:00
|
|
|
auto n1 = t.vertex_find(args[0]->as<Int64>().value);
|
2016-08-15 07:09:58 +08:00
|
|
|
if (!option_fill(n1)) return t.commit(), false;
|
2016-08-13 06:01:39 +08:00
|
|
|
auto n2 = t.vertex_find(args[1]->as<Int64>().value);
|
2016-08-15 07:09:58 +08:00
|
|
|
if (!option_fill(n2)) return t.commit(), false;
|
|
|
|
auto r = t.edge_insert(n2.get(), n1.get());
|
2016-08-18 22:34:36 +08:00
|
|
|
r.set(prop_age, args[2]);
|
|
|
|
r.set(prop_weight, args[3]);
|
2016-08-13 06:01:39 +08:00
|
|
|
auto &IS = t.type_find_or_create("IS");
|
2016-08-01 01:58:12 +08:00
|
|
|
r.edge_type(IS);
|
2016-08-13 06:01:39 +08:00
|
|
|
|
2016-08-30 09:32:31 +08:00
|
|
|
return t.commit();
|
2016-08-01 01:58:12 +08:00
|
|
|
};
|
|
|
|
|
2016-08-08 16:32:34 +08:00
|
|
|
// MATCH (n) RETURN n
|
2016-08-10 03:29:03 +08:00
|
|
|
auto match_all_nodes = [&db](const properties_t &args) {
|
2016-08-13 06:01:39 +08:00
|
|
|
DbAccessor t(db);
|
2016-08-08 16:32:34 +08:00
|
|
|
|
2016-08-15 07:09:58 +08:00
|
|
|
iter::for_all(t.vertex_access(), [&](auto vertex) {
|
|
|
|
if (vertex.fill()) {
|
2016-08-29 01:50:54 +08:00
|
|
|
cout << vertex.id() << endl;
|
2016-08-15 07:09:58 +08:00
|
|
|
}
|
|
|
|
});
|
2016-08-08 16:32:34 +08:00
|
|
|
|
2016-08-30 09:32:31 +08:00
|
|
|
return t.commit();
|
2016-08-08 16:32:34 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// MATCH (n:LABEL) RETURN n
|
2016-08-29 01:50:54 +08:00
|
|
|
auto match_by_label = [&db](const properties_t &args) {
|
2016-08-13 06:01:39 +08:00
|
|
|
DbAccessor t(db);
|
2016-08-08 16:32:34 +08:00
|
|
|
|
2016-08-13 06:01:39 +08:00
|
|
|
auto &label = t.label_find_or_create("LABEL");
|
2016-08-20 01:40:04 +08:00
|
|
|
auto prop_key = t.vertex_property_key("name", Flags::String);
|
2016-08-08 16:32:34 +08:00
|
|
|
|
|
|
|
cout << "VERTICES" << endl;
|
2016-08-28 22:47:13 +08:00
|
|
|
iter::for_all(label.index().for_range(t),
|
2016-08-18 22:34:36 +08:00
|
|
|
[&](auto a) { cout << a.at(prop_key) << endl; });
|
2016-08-08 16:32:34 +08:00
|
|
|
|
2016-08-30 09:32:31 +08:00
|
|
|
return t.commit();
|
|
|
|
};
|
|
|
|
|
|
|
|
// MATCH (n) DELETE n
|
|
|
|
auto match_all_delete = [&db](const properties_t &args) {
|
|
|
|
DbAccessor t(db);
|
|
|
|
|
|
|
|
iter::for_all(t.vertex_access(), [&](auto a) {
|
|
|
|
if (a.fill()) {
|
|
|
|
a.remove();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return t.commit();
|
|
|
|
};
|
|
|
|
|
|
|
|
// MATCH (n:LABEL) DELETE n
|
|
|
|
auto match_label_delete = [&db](const properties_t &args) {
|
|
|
|
DbAccessor t(db);
|
|
|
|
|
|
|
|
auto &label = t.label_find_or_create("LABEL");
|
|
|
|
|
|
|
|
iter::for_all(label.index().for_range(t), [&](auto a) {
|
|
|
|
if (a.fill()) {
|
|
|
|
a.remove();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return t.commit();
|
|
|
|
};
|
|
|
|
|
|
|
|
// MATCH (n) WHERE ID(n) = id DELETE n
|
|
|
|
auto match_id_delete = [&db](const properties_t &args) {
|
|
|
|
DbAccessor t(db);
|
|
|
|
|
|
|
|
auto ov = t.vertex_find(args[0]->as<Int64>().value);
|
|
|
|
if (!option_fill(ov)) return t.commit(), false;
|
|
|
|
|
|
|
|
auto v = ov.take();
|
|
|
|
v.remove();
|
|
|
|
|
|
|
|
return t.commit();
|
|
|
|
};
|
|
|
|
|
2016-08-30 09:32:53 +08:00
|
|
|
// MATCH ()-[r]-() WHERE ID(r) = id DELETE r
|
2016-08-30 09:32:31 +08:00
|
|
|
auto match_edge_id_delete = [&db](const properties_t &args) {
|
|
|
|
DbAccessor t(db);
|
|
|
|
|
|
|
|
auto ov = t.edge_find(args[0]->as<Int64>().value);
|
|
|
|
if (!option_fill(ov)) return t.commit(), false;
|
|
|
|
|
|
|
|
auto v = ov.take();
|
|
|
|
v.remove();
|
|
|
|
|
|
|
|
return t.commit();
|
2016-08-08 16:32:34 +08:00
|
|
|
};
|
|
|
|
|
2016-08-30 09:32:31 +08:00
|
|
|
// MATCH ()-[r]-() DELETE r
|
|
|
|
auto match_edge_all_delete = [&db](const properties_t &args) {
|
|
|
|
DbAccessor t(db);
|
|
|
|
|
|
|
|
iter::for_all(t.edge_access(), [&](auto a) {
|
|
|
|
if (a.fill()) {
|
|
|
|
a.remove();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return t.commit();
|
|
|
|
};
|
|
|
|
|
|
|
|
// MATCH ()-[r:TYPE]-() DELETE r
|
|
|
|
auto match_edge_type_delete = [&db](const properties_t &args) {
|
|
|
|
DbAccessor t(db);
|
|
|
|
|
|
|
|
auto &type = t.type_find_or_create("TYPE");
|
|
|
|
|
|
|
|
iter::for_all(type.index().for_range(t), [&](auto a) {
|
|
|
|
if (a.fill()) {
|
|
|
|
a.remove();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return t.commit();
|
|
|
|
};
|
|
|
|
|
2016-08-30 09:32:53 +08:00
|
|
|
// MATCH (n)-[:TYPE]->(m) WHERE ID(n) = id RETURN m
|
|
|
|
auto match_type_id_return = [&db](const properties_t &args) {
|
2016-08-30 09:32:31 +08:00
|
|
|
DbAccessor t(db);
|
|
|
|
|
2016-08-30 09:32:53 +08:00
|
|
|
auto ov = t.vertex_find(args[0]->as<Int64>().value);
|
|
|
|
if (!option_fill(ov)) return t.commit(), false;
|
|
|
|
auto v = ov.take();
|
|
|
|
|
|
|
|
auto resoults = iter::make_f`
|
|
|
|
|
|
|
|
return t.commit();
|
2016-08-30 09:32:31 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// Blueprint:
|
|
|
|
// auto = [&db](const properties_t &args) {
|
|
|
|
// DbAccessor t(db);
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// return t.commit();
|
|
|
|
// };
|
|
|
|
|
2016-08-29 01:50:54 +08:00
|
|
|
queries[15284086425088081497u] = match_all_nodes;
|
|
|
|
queries[4857652843629217005u] = match_by_label;
|
|
|
|
queries[15648836733456301916u] = create_edge_v2;
|
2016-07-07 08:58:26 +08:00
|
|
|
queries[10597108978382323595u] = create_account;
|
|
|
|
queries[5397556489557792025u] = create_labeled_and_named_node;
|
|
|
|
queries[7939106225150551899u] = create_edge;
|
2016-07-24 10:47:48 +08:00
|
|
|
queries[6579425155585886196u] = create_edge;
|
2016-07-07 08:58:26 +08:00
|
|
|
queries[11198568396549106428u] = find_node_by_internal_id;
|
|
|
|
queries[8320600413058284114u] = find_edge_by_internal_id;
|
|
|
|
queries[6813335159006269041u] = update_node;
|
2016-08-30 09:32:31 +08:00
|
|
|
queries[10506105811763742758u] = match_all_delete;
|
|
|
|
queries[13742779491897528506u] = match_label_delete;
|
|
|
|
queries[11349462498691305864u] = match_id_delete;
|
|
|
|
queries[6963549500479100885u] = match_edge_id_delete;
|
|
|
|
queries[14897166600223619735u] = match_edge_all_delete;
|
|
|
|
queries[16888549834923624215u] = match_edge_type_delete;
|
2016-07-07 08:58:26 +08:00
|
|
|
|
|
|
|
return queries;
|
|
|
|
}
|
2016-08-28 22:47:13 +08:00
|
|
|
}
|