#pragma once #include "includes.hpp" namespace hardcode { auto load_basic_functions(Db &db) { query_functions_t functions; // CREATE (n {prop: 0}) RETURN n auto create_node = [&db](properties_t &&args) { DbAccessor t(db); auto property_key = t.vertex_property_key("prop", args[0].key.flags()); auto vertex_accessor = t.vertex_insert(); vertex_accessor.set(property_key, std::move(args[0])); return t.commit(); }; functions[3191791685918807343u] = create_node; // CREATE (n:LABEL {name: "TEST"}) RETURN n; auto create_labeled_and_named_node = [&db](properties_t &&args) { DbAccessor t(db); auto property_key = t.vertex_property_key("name", args[0].key.flags()); auto &label = t.label_find_or_create("LABEL"); auto vertex_accessor = t.vertex_insert(); vertex_accessor.set(property_key, std::move(args[0])); vertex_accessor.add_label(label); return t.commit(); }; functions[8273374963505210457u] = create_labeled_and_named_node; // CREATE (n:OTHER {name: "cleaner_test"}) RETURN n auto create_node_with_other_label = [&db](properties_t &&args) { DbAccessor t(db); auto property_key = t.vertex_property_key("name", args[0].key.flags()); auto &label = t.label_find_or_create("OTHER"); auto vertex_accessor = t.vertex_insert(); vertex_accessor.set(property_key, std::move(args[0])); vertex_accessor.add_label(label); return t.commit(); }; functions[6237439055665132277u] = create_node_with_other_label; // CREATE (n:OTHER {name: "TEST"}) RETURN n; auto create_labeled_and_named_node_v2 = [&db](properties_t &&args) { DbAccessor t(db); auto property_key = t.vertex_property_key("name", args[0].key.flags()); auto &label = t.label_find_or_create("OTHER"); auto vertex_accessor = t.vertex_insert(); vertex_accessor.set(property_key, std::move(args[0])); vertex_accessor.add_label(label); return t.commit(); }; functions[832997784138269151u] = create_labeled_and_named_node_v2; // CREATE (n:ACCOUNT {id: 2322, name: "TEST", country: "Croatia", "created_at": 2352352}) RETURN n auto create_account = [&db](properties_t &&args) { DbAccessor t(db); auto prop_id = t.vertex_property_key("id", args[0].key.flags()); auto prop_name = t.vertex_property_key("name", args[1].key.flags()); auto prop_country = t.vertex_property_key("country", args[2].key.flags()); auto prop_created = t.vertex_property_key("created_at", args[3].key.flags()); auto &label = t.label_find_or_create("ACCOUNT"); auto vertex_accessor = t.vertex_insert(); vertex_accessor.set(prop_id, std::move(args[0])); vertex_accessor.set(prop_name, std::move(args[1])); vertex_accessor.set(prop_country, std::move(args[2])); vertex_accessor.set(prop_created, std::move(args[3])); vertex_accessor.add_label(label); return t.commit(); }; functions[16701745788564313211u] = create_account; // TODO: inconsistency but it doesn't affect the integration tests // this is not a unique case // MATCH (n) WHERE ID(n) = 1 RETURN n // MATCH (n {id: 0}) RETURN n auto find_node_by_internal_id = [&db](properties_t &&args) { DbAccessor t(db); auto maybe_va = t.vertex_find(Id(args[0].as().value())); if (!option_fill(maybe_va)) { cout << "vertex doesn't exist" << endl; t.commit(); return false; } auto vertex_accessor = maybe_va.get(); // cout_properties(vertex_accessor.properties()); cout << "LABELS:" << endl; for (auto label_ref : vertex_accessor.labels()) { // cout << label_ref.get() << endl; } return t.commit(); }; functions[1444315501940151196u] = find_node_by_internal_id; functions[11624983287202420303u] = find_node_by_internal_id; // MATCH (a {id:0}), (p {id: 1}) CREATE (a)-[r:IS]->(p) RETURN r auto create_edge = [&db](properties_t &&args) { DbAccessor t(db); auto &edge_type = t.type_find_or_create("IS"); auto v1 = t.vertex_find(args[0].as().value()); if (!option_fill(v1)) return t.commit(), false; auto v2 = t.vertex_find(args[1].as().value()); if (!option_fill(v2)) return t.commit(), false; auto edge_accessor = t.edge_insert(v1.get(), v2.get()); edge_accessor.edge_type(edge_type); bool ret = t.commit(); // cout << edge_accessor.edge_type() << endl; // cout_properties(edge_accessor.properties()); return ret; }; functions[6972641167053231355u] = create_edge; // MATCH ()-[r]-() WHERE ID(r)=0 RETURN r auto find_edge_by_internal_id = [&db](properties_t &&args) { DbAccessor t(db); auto maybe_ea = t.edge_find(args[0].as().value()); if (!option_fill(maybe_ea)) return t.commit(), false; auto edge_accessor = maybe_ea.get(); // print edge type and properties // cout << "EDGE_TYPE: " << edge_accessor.edge_type() << endl; auto from = edge_accessor.from(); if (!from.fill()) return t.commit(), false; cout << "FROM:" << endl; // cout_properties(from->data.props); auto to = edge_accessor.to(); if (!to.fill()) return t.commit(), false; cout << "TO:" << endl; // cout_properties(to->data.props); return t.commit(); }; functions[15080095524051312786u] = find_edge_by_internal_id; // MATCH (n {id: 0}) SET n.name = "TEST102" RETURN n auto update_node = [&db](properties_t &&args) { DbAccessor t(db); auto prop_name = t.vertex_property_key("name", args[1].key.flags()); auto maybe_v = t.vertex_find(args[0].as().value()); if (!option_fill(maybe_v)) return t.commit(), false; auto v = maybe_v.get(); v.set(prop_name, std::move(args[1])); // cout_properties(v.properties()); return t.commit(); }; functions[2835161674800069655u] = update_node; // 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](properties_t &&args) { DbAccessor t(db); auto prop_age = t.edge_property_key("age", args[2].key.flags()); auto prop_weight = t.edge_property_key("weight", args[3].key.flags()); auto n1 = t.vertex_find(args[0].as().value()); if (!option_fill(n1)) return t.commit(), false; auto n2 = t.vertex_find(args[1].as().value()); if (!option_fill(n2)) return t.commit(), false; auto r = t.edge_insert(n2.get(), n1.get()); r.set(prop_age, std::move(args[2])); r.set(prop_weight, std::move(args[3])); auto &IS = t.type_find_or_create("IS"); r.edge_type(IS); return t.commit(); }; functions[10360716473890539004u] = create_edge_v2; // MATCH (n) RETURN n auto match_all_nodes = [&db](properties_t &&args) { DbAccessor t(db); t.vertex_access().fill().for_all( [&](auto vertex) { cout << vertex.id() << endl; }); return t.commit(); }; functions[5949923385370229113u] = match_all_nodes; // MATCH (n:LABEL) RETURN n auto match_by_label = [&db](properties_t &&args) { DbAccessor t(db); auto &label = t.label_find_or_create("LABEL"); auto property_key = t.vertex_property_key("name", Flags::String); cout << "VERTICES" << endl; label.index().for_range(t).for_all( [&](auto a) { cout << a.at(property_key) << endl; }); return t.commit(); }; functions[16533049303627288013u] = match_by_label; // MATCH (n) DELETE n auto match_all_delete = [&db](properties_t &&args) { DbAccessor t(db); // DETACH DELETE // t.edge_access().fill().for_all( // [&](auto e) { e.remove(); } // ); t.vertex_access().fill().isolated().for_all( [&](auto a) { a.remove(); }); return t.commit(); }; functions[16628411757092333638u] = match_all_delete; // MATCH (n:LABEL) DELETE n auto match_label_delete = [&db](properties_t &&args) { DbAccessor t(db); auto &label = t.label_find_or_create("LABEL"); label.index().for_range(t).isolated().for_all( [&](auto a) { a.remove(); }); return t.commit(); }; functions[10022871879682099034u] = match_label_delete; // MATCH (n) WHERE ID(n) = id DELETE n auto match_id_delete = [&db](properties_t &&args) { DbAccessor t(db); auto ov = t.vertex_find(args[0].as().value()); if (!option_fill(ov)) return t.commit(), false; auto v = ov.take(); if (!v.isolated()) return t.commit(), false; v.remove(); return t.commit(); }; functions[5375628876334795080u] = match_id_delete; // MATCH ()-[r]-() WHERE ID(r) = id DELETE r auto match_edge_id_delete = [&db](properties_t &&args) { DbAccessor t(db); auto ov = t.edge_find(args[0].as().value()); if (!option_fill(ov)) return t.commit(), false; auto v = ov.take(); v.remove(); return t.commit(); }; functions[11747491556476630933u] = match_edge_id_delete; // MATCH ()-[r]-() DELETE r auto match_edge_all_delete = [&db](properties_t &&) { DbAccessor t(db); t.edge_access().fill().for_all([&](auto a) { a.remove(); }); return t.commit(); }; functions[10064744449500095415u] = match_edge_all_delete; // MATCH ()-[r:TYPE]-() DELETE r auto match_edge_type_delete = [&db](properties_t &&args) { DbAccessor t(db); auto &type = t.type_find_or_create("TYPE"); type.index().for_range(t).for_all([&](auto a) { a.remove(); }); return t.commit(); }; functions[6084209470626828855u] = match_edge_type_delete; // MATCH (n)-[:TYPE]->(m) WHERE ID(n) = id RETURN m auto match_id_type_return = [&db](properties_t &&args) { DbAccessor t(db); auto &type = t.type_find_or_create("TYPE"); auto ov = t.vertex_find(args[0].as().value()); if (!option_fill(ov)) return t.commit(), false; auto v = ov.take(); auto results = v.out().fill().type(type).to(); // Example of Print resoult. // results.for_all([&](auto va) { // va is VertexAccessor // PRINT // }); return t.commit(); }; functions[2605621337795673948u] = match_id_type_return; // MATCH (n)-[:TYPE]->(m) WHERE n.name = "kruno" RETURN m auto match_name_type_return = [&db](properties_t &&args) { DbAccessor t(db); auto &type = t.type_find_or_create("TYPE"); auto prop_name = t.vertex_property_key("name", args[0].key.flags()); Option r; auto it_type = type.index() .for_range(t) .clone_to(r) // Savepoint .from() // Backtracing .has_property(prop_name, args[0]) .replace(r); // Load savepoint auto it_vertex = t.vertex_access() .fill() .has_property(prop_name, args[0]) .out() .type(type); if (it_type.count() > it_vertex.count()) { // TODO: Going through vertices wiil probably be faster it_vertex.to().for_all([&](auto m) { // PRINT m }); } else { // TODO: Going through edges will probably be faster it_type.to().for_all([&](auto m) { // PRINT m }); } return t.commit(); }; functions[17303982256920342123u] = match_name_type_return; // MATCH (n)-[:TYPE]->(m) WHERE n.name = "kruno" RETURN n,m auto match_name_type_return_cross = [&db](properties_t &&args) { DbAccessor t(db); auto &type = t.type_find_or_create("TYPE"); auto prop_name = t.vertex_property_key("name", args[0].key.flags()); Option n; Option r; // lazy load iterator auto it_type = type.index() .for_range(t) .clone_to(r) // Savepoint .from() // Backtracing .has_property(prop_name, args[0]) .clone_to(n) // Savepoint .replace(r); // Load savepoint // Above statments + .to().for_all([&](auto m) {}) will unrool into: // for(auto edge:type.index.for_range(t)){ // auto from_vertex=edge.from(); // if(from_vertex.fill()){ // auto &prop=from_vertex.at(prop_name); // if(prop==args[0]){ // auto to_vertex=edge.to(); // if(to_vertex.fill()){ // // Here you have all data. // // n == from_vertex // // m == to_vertex // } // } // } // } auto it_vertex = t.vertex_access() .fill() .has_property(prop_name, args[0]) .clone_to(n) // Savepoint .out() .type(type); // Above statments + .to().for_all([&](auto m) {}) will unrool into: // for(auto from_vertex:t.vertex_access(t)){ // if(from_vertex.fill()){ // auto &prop=from_vertex.at(prop_name); // if(prop==args[0]){ // for(auto edge:from_vertex.out()){ // if(edge.edge_type() == type){ // auto to_vertex=edge.to(); // if(to_vertex.fill()){ // // Here you have all data. // // n == from_vertex // // m == to_vertex // } // } // } // } // } // } if (it_type.count() > it_vertex.count()) { // Going through vertices wiil probably be faster it_vertex.to().for_all([&](auto m) { // m is changing // n is n // PRINT n, m }); } else { // Going through edges wiil probably be faster it_type.to().for_all([&](auto m) { // m is r // n is changing // PRINT n, m }); } return t.commit(); }; functions[17456874322957005665u] = match_name_type_return_cross; // MATCH (n:LABEL)-[:TYPE]->(m) RETURN n auto match_label_type_return = [&db](properties_t &&args) { DbAccessor t(db); try { auto &type = t.type_find_or_create("TYPE"); auto &label = t.label_find_or_create("LABEL"); Option bt; auto it_type = type.index().for_range(t).from().label(label); auto it_vertex = t.vertex_access() .fill() .label(label) .clone_to(bt) // Savepoint .out() .type(type) .replace(bt); // Load savepoint if (it_type.count() > it_vertex.count()) { // Going through vertices wiil probably be faster it_vertex.for_all([&](auto n) { // PRINT n }); } else { // Going through edges wiil probably be faster it_type.for_all([&](auto n) { // PRINT n }); } return t.commit(); } catch (...) { // Catch all exceptions // Print something to logger t.abort(); return false; } }; functions[4866842751631597263u] = match_label_type_return; // MATCH (n:LABEL {name: "TEST01"}) RETURN n; auto match_label_property = [&db](properties_t &&args) { indices_t indices{{"name", 0}}; auto properties = query_properties(indices, args); DbAccessor t(db); try { auto &label = t.label_find_or_create("LABEL"); label.index().for_range(t).properties_filter(t, properties).for_all( [&](auto vertex_accessor) -> void { std::cout << "MATCH" << std::endl; } ); return t.commit(); } catch (...) { t.abort(); return false; } }; functions[7710665404758409302u] = match_label_property; return functions; } }