Fixed Query compiler to use new interface.

This commit is contained in:
Kruno Tomola Fabro
2016-08-19 18:40:04 +01:00
parent e5f161974f
commit eba7fd8be4
25 changed files with 292 additions and 161 deletions

View File

@@ -4,6 +4,9 @@
#include <map>
#include <string>
#include "query_engine/code_generator/namer.hpp"
#include "storage/model/properties/flags.hpp"
// main states that are used while ast is traversed
// in order to generate ActionSequence
enum class CypherState : uint8_t
@@ -36,10 +39,11 @@ class CypherStateData
private:
std::map<std::string, EntityStatus> entity_status;
std::map<std::string, EntityType> entity_type;
// TODO: container that keeps track about c++ variable names
public:
bool exist(const std::string& name) const
bool exist(const std::string &name) const
{
return entity_status.find(name) != entity_status.end();
}
@@ -52,7 +56,7 @@ public:
return entity_status.at(name);
}
EntityType type(const std::string &name)
EntityType type(const std::string &name) const
{
if (entity_type.find(name) == entity_type.end())
return EntityType::NotFound;
@@ -60,6 +64,11 @@ public:
return entity_type.at(name);
}
const std::map<std::string, EntityType> &all_typed_enteties()
{
return entity_type;
}
void node_matched(const std::string &name)
{
entity_type[name] = EntityType::Node;

View File

@@ -1,9 +1,9 @@
#pragma once
#include "query_engine/code_generator/handlers/create.hpp"
#include "query_engine/code_generator/handlers/delete.hpp"
#include "query_engine/code_generator/handlers/match.hpp"
#include "query_engine/code_generator/handlers/return.hpp"
#include "query_engine/code_generator/handlers/set.hpp"
#include "query_engine/code_generator/handlers/delete.hpp"
#include "query_engine/code_generator/handlers/transaction_begin.hpp"
#include "query_engine/code_generator/handlers/transaction_commit.hpp"

View File

@@ -16,7 +16,7 @@ auto create_query_action =
code += code_line(code::create_vertex, name);
// update properties
code += update_properties(action_data, name);
code += update_properties(cypher_data, action_data, name);
// update labels
auto entity_data = action_data.get_entity_property(name);
@@ -30,19 +30,7 @@ auto create_query_action =
}
if (kv.second == ClauseAction::CreateRelationship) {
// create relationship
auto name = kv.first;
code += code_line(code::create_edge, name);
// update properties
code += update_properties(action_data, name);
// update tag
auto entity_data = action_data.get_entity_property(name);
for (auto &tag : entity_data.tags) {
code += code_line(code::find_type, tag);
code += code_line(code::set_type, name, tag);
}
// find start and end node
auto &relationships_data = action_data.relationship_data;
@@ -63,19 +51,32 @@ auto create_query_action =
" can't be found");
}
// define direction
if (relationship_data.direction == Direction::Right) {
code += code_line(code::node_out, left_node, name);
code += code_line(code::node_in, right_node, name);
code += code_line(code::edge_from, name, left_node);
code += code_line(code::edge_to, name, right_node);
} else if (relationship_data.direction == Direction::Left) {
code += code_line(code::node_out, right_node, name);
code += code_line(code::node_in, left_node, name);
code += code_line(code::edge_from, name, right_node);
code += code_line(code::edge_to, name, left_node);
// create relationship
code += code_line(code::create_edge, name, left_node, right_node);
// update properties
code += update_properties(cypher_data, action_data, name);
// update tag
auto entity_data = action_data.get_entity_property(name);
for (auto &tag : entity_data.tags) {
code += code_line(code::find_type, tag);
code += code_line(code::set_type, name, tag);
}
// define direction
// if (relationship_data.direction == Direction::Right) {
// code += code_line(code::node_out, left_node, name);
// code += code_line(code::node_in, right_node, name);
// code += code_line(code::edge_from, name, left_node);
// code += code_line(code::edge_to, name, right_node);
// } else if (relationship_data.direction == Direction::Left) {
// code += code_line(code::node_out, right_node, name);
// code += code_line(code::node_in, left_node, name);
// code += code_line(code::edge_from, name, right_node);
// code += code_line(code::edge_to, name, left_node);
// }
// mark relationship as created
cypher_data.relationship_created(name);
}

View File

@@ -6,11 +6,12 @@
#include <utility>
#include <vector>
#include "query_engine/util.hpp"
#include "query_engine/code_generator/cypher_state.hpp"
#include "query_engine/code_generator/namer.hpp"
#include "query_engine/code_generator/query_action_data.hpp"
#include "query_engine/traverser/code.hpp"
#include "query_engine/exceptions/errors.hpp"
#include "query_engine/traverser/code.hpp"
#include "query_engine/util.hpp"
using ParameterIndexKey::Type::InternalId;
using Direction = RelationshipData::Direction;
@@ -18,7 +19,8 @@ using Direction = RelationshipData::Direction;
namespace
{
auto update_properties(const QueryActionData &action_data,
auto update_properties(const CypherStateData &cypher_state,
const QueryActionData &action_data,
const std::string &name)
{
std::string code = "";
@@ -27,10 +29,14 @@ auto update_properties(const QueryActionData &action_data,
for (auto &property : entity_data.properties) {
auto index =
action_data.parameter_index.at(ParameterIndexKey(name, property));
code += code_line(code::set_property, name, property, index);
auto tmp_name = name::unique();
code += code_line((cypher_state.type(name) == EntityType::Node
? code::vertex_property_key
: code::edge_property_key),
tmp_name, property, index);
code += code_line(code::set_property, name, tmp_name, index);
}
return code;
}
}

View File

@@ -20,7 +20,6 @@ auto fetch_internal_index(const QueryActionData &action_data,
{
return action_data.parameter_index.at(ParameterIndexKey(InternalId, name));
}
}
auto match_query_action =
@@ -40,8 +39,7 @@ auto match_query_action =
auto place = action_data.csm.min(kv.first);
if (place == entity_search::search_internal_id) {
auto index = fetch_internal_index(action_data, name);
code +=
code_line(code::match_vertex_by_id, name, index);
code += code_line(code::match_vertex_by_id, name, index);
}
}

View File

@@ -12,7 +12,7 @@ auto return_query_action =
code += code_line("// number of elements {}", elements.size());
// TODO: call bolt serialization
for (const auto& element : elements) {
for (const auto &element : elements) {
auto &entity = element.entity;
if (!cypher_data.exist(entity)) {
throw SemanticError(
@@ -26,6 +26,6 @@ auto return_query_action =
// code += code_line(code::print_property, entity, property);
}
}
return code;
};

View File

@@ -13,13 +13,13 @@ auto set_query_action = [](CypherStateData &cypher_data,
if (kv.second == ClauseAction::UpdateNode &&
cypher_data.status(name) == EntityStatus::Matched &&
cypher_data.type(name) == EntityType::Node) {
code += update_properties(action_data, name);
code += update_properties(cypher_data, action_data, name);
}
if (kv.second == ClauseAction::UpdateRelationship &&
cypher_data.status(name) == EntityStatus::Matched &&
cypher_data.type(name) == EntityType::Relationship) {
code += update_properties(action_data, name);
code += update_properties(cypher_data, action_data, name);
}
}

View File

@@ -0,0 +1,18 @@
#pragma once
#include "storage/model/properties/flags.hpp"
// This namespace names stuff
namespace
{
namespace name
{
std::string variable_property_key(const std::string &property_name, Type type)
{
return "prop_" + property_name + "_" + type.to_str();
}
std::string unique() { return "unique_" + std::to_string(std::rand()); }
}
}

View File

@@ -4,9 +4,9 @@
#include <map>
#include <vector>
#include "query_engine/exceptions/exceptions.hpp"
#include "query_engine/code_generator/clause_action.hpp"
#include "query_engine/code_generator/entity_search.hpp"
#include "query_engine/exceptions/exceptions.hpp"
#include "storage/model/properties/all.hpp"
#include "utils/assert.hpp"
#include "utils/underlying_cast.hpp"
@@ -97,9 +97,9 @@ struct RelationshipData
struct ReturnElement
{
ReturnElement(const std::string& entity) : entity(entity) {}
ReturnElement(const std::string& entity, const std::string& property) :
entity(entity), property(property) {};
ReturnElement(const std::string &entity) : entity(entity) {}
ReturnElement(const std::string &entity, const std::string &property)
: entity(entity), property(property){};
std::string entity;
std::string property;
@@ -143,7 +143,7 @@ struct QueryActionData
}
// TODO: refactor name
auto get_entity_property(const std::string& entity) const
auto get_entity_property(const std::string &entity) const
{
if (entity_data.find(entity) == entity_data.end())
throw CppGeneratorException("Entity " + entity + " doesn't exist");