2016-07-24 10:47:48 +08:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
#include <map>
|
|
|
|
#include <string>
|
2016-08-30 12:34:08 +08:00
|
|
|
#include <vector>
|
2016-07-24 10:47:48 +08:00
|
|
|
|
2016-08-20 01:40:04 +08:00
|
|
|
#include "query_engine/code_generator/namer.hpp"
|
|
|
|
#include "storage/model/properties/flags.hpp"
|
2016-08-30 12:34:08 +08:00
|
|
|
#include "query_engine/exceptions/exceptions.hpp"
|
2016-08-20 01:40:04 +08:00
|
|
|
|
2016-07-24 10:47:48 +08:00
|
|
|
// main states that are used while ast is traversed
|
|
|
|
// in order to generate ActionSequence
|
|
|
|
enum class CypherState : uint8_t
|
|
|
|
{
|
|
|
|
Undefined,
|
|
|
|
Match,
|
|
|
|
Where,
|
|
|
|
Create,
|
|
|
|
Set,
|
|
|
|
Return,
|
|
|
|
Delete
|
|
|
|
};
|
|
|
|
|
|
|
|
enum class EntityStatus : uint8_t
|
|
|
|
{
|
2016-08-30 08:01:03 +08:00
|
|
|
None,
|
2016-07-24 10:47:48 +08:00
|
|
|
Matched,
|
|
|
|
Created
|
|
|
|
};
|
|
|
|
|
|
|
|
enum class EntityType : uint8_t
|
|
|
|
{
|
2016-08-30 08:01:03 +08:00
|
|
|
None,
|
2016-07-24 10:47:48 +08:00
|
|
|
Node,
|
|
|
|
Relationship
|
|
|
|
};
|
|
|
|
|
2016-08-30 08:01:03 +08:00
|
|
|
// where OR how entity can be found
|
|
|
|
enum class EntitySource : uint8_t
|
|
|
|
{
|
|
|
|
None,
|
|
|
|
InternalId,
|
|
|
|
LabelIndex,
|
2016-08-31 03:52:46 +08:00
|
|
|
TypeIndex,
|
2016-08-30 08:01:03 +08:00
|
|
|
MainStorage
|
|
|
|
};
|
|
|
|
|
2016-10-16 02:16:32 +08:00
|
|
|
// TODO: reduce copying
|
2016-07-24 10:47:48 +08:00
|
|
|
class CypherStateData
|
|
|
|
{
|
2016-10-16 02:16:32 +08:00
|
|
|
public:
|
|
|
|
using tags_type = std::vector<std::string>;
|
|
|
|
using properties_type = std::map<std::string, int64_t>;
|
|
|
|
|
2016-07-24 10:47:48 +08:00
|
|
|
private:
|
|
|
|
std::map<std::string, EntityStatus> entity_status;
|
|
|
|
std::map<std::string, EntityType> entity_type;
|
2016-08-30 08:01:03 +08:00
|
|
|
std::map<std::string, EntitySource> entity_source;
|
2016-10-16 02:16:32 +08:00
|
|
|
std::map<std::string, tags_type> entity_tags;
|
|
|
|
std::map<std::string, properties_type> entity_properties;
|
2016-08-20 01:40:04 +08:00
|
|
|
|
2016-07-24 10:47:48 +08:00
|
|
|
public:
|
2016-08-20 01:40:04 +08:00
|
|
|
bool exist(const std::string &name) const
|
2016-07-24 10:47:48 +08:00
|
|
|
{
|
|
|
|
return entity_status.find(name) != entity_status.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
EntityStatus status(const std::string &name)
|
|
|
|
{
|
|
|
|
if (entity_status.find(name) == entity_status.end())
|
2016-08-30 08:01:03 +08:00
|
|
|
return EntityStatus::None;
|
2016-07-24 10:47:48 +08:00
|
|
|
|
|
|
|
return entity_status.at(name);
|
|
|
|
}
|
|
|
|
|
2016-08-20 01:40:04 +08:00
|
|
|
EntityType type(const std::string &name) const
|
2016-07-24 10:47:48 +08:00
|
|
|
{
|
|
|
|
if (entity_type.find(name) == entity_type.end())
|
2016-08-30 08:01:03 +08:00
|
|
|
return EntityType::None;
|
2016-07-24 10:47:48 +08:00
|
|
|
|
|
|
|
return entity_type.at(name);
|
|
|
|
}
|
|
|
|
|
2016-08-30 08:01:03 +08:00
|
|
|
EntitySource source(const std::string &name) const
|
|
|
|
{
|
|
|
|
if (entity_source.find(name) == entity_source.end())
|
|
|
|
return EntitySource::None;
|
|
|
|
return entity_source.at(name);
|
|
|
|
}
|
|
|
|
|
2016-10-16 02:16:32 +08:00
|
|
|
const std::map<std::string, EntityType> &all_typed_enteties()
|
2016-08-20 01:40:04 +08:00
|
|
|
{
|
|
|
|
return entity_type;
|
|
|
|
}
|
|
|
|
|
2016-07-24 10:47:48 +08:00
|
|
|
void node_matched(const std::string &name)
|
|
|
|
{
|
|
|
|
entity_type[name] = EntityType::Node;
|
|
|
|
entity_status[name] = EntityStatus::Matched;
|
|
|
|
}
|
|
|
|
|
|
|
|
void node_created(const std::string &name)
|
|
|
|
{
|
|
|
|
entity_type[name] = EntityType::Node;
|
|
|
|
entity_status[name] = EntityStatus::Created;
|
|
|
|
}
|
|
|
|
|
|
|
|
void relationship_matched(const std::string &name)
|
|
|
|
{
|
|
|
|
entity_type[name] = EntityType::Relationship;
|
|
|
|
entity_status[name] = EntityStatus::Matched;
|
|
|
|
}
|
|
|
|
|
|
|
|
void relationship_created(const std::string &name)
|
|
|
|
{
|
|
|
|
entity_type[name] = EntityType::Relationship;
|
|
|
|
entity_status[name] = EntityStatus::Created;
|
|
|
|
}
|
2016-08-30 08:01:03 +08:00
|
|
|
|
|
|
|
void source(const std::string& name, EntitySource source)
|
|
|
|
{
|
|
|
|
entity_source[name] = source;
|
|
|
|
}
|
2016-10-16 02:16:32 +08:00
|
|
|
|
|
|
|
// entity tags
|
|
|
|
auto tags(const std::string& name) const
|
|
|
|
{
|
|
|
|
if (entity_tags.find(name) == entity_tags.end())
|
|
|
|
throw CppGeneratorException("No tags for specified entity");
|
|
|
|
return entity_tags.at(name);
|
|
|
|
}
|
2016-08-30 12:34:08 +08:00
|
|
|
|
2016-10-16 02:16:32 +08:00
|
|
|
void tags(const std::string& name, tags_type tags)
|
2016-08-30 12:34:08 +08:00
|
|
|
{
|
|
|
|
entity_tags[name] = tags;
|
|
|
|
}
|
2016-10-13 21:42:19 +08:00
|
|
|
|
|
|
|
void tag(const std::string& name, const std::string& new_tag)
|
|
|
|
{
|
|
|
|
if (entity_tags.find(name) != entity_tags.end())
|
|
|
|
{
|
|
|
|
entity_tags[name] = std::vector<std::string>{};
|
|
|
|
}
|
|
|
|
entity_tags[name].emplace_back(new_tag);
|
|
|
|
}
|
2016-10-16 02:16:32 +08:00
|
|
|
|
|
|
|
// entity properties
|
|
|
|
auto properties(const std::string& name) const
|
|
|
|
{
|
|
|
|
if (entity_properties.find(name) == entity_properties.end())
|
|
|
|
throw CppGeneratorException("No properties for specified entity");
|
|
|
|
return entity_properties.at(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
void properties(const std::string& name, properties_type properties)
|
|
|
|
{
|
|
|
|
entity_properties[name] = properties;
|
|
|
|
}
|
|
|
|
|
|
|
|
void index(const std::string& entity, const std::string& property, int64_t index)
|
|
|
|
{
|
|
|
|
if (entity_properties.find(entity) != entity_properties.end())
|
|
|
|
{
|
|
|
|
entity_properties[entity] = properties_type{};
|
|
|
|
}
|
|
|
|
entity_properties[entity][property] = index;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto index(const std::string& entity, const std::string& property_name)
|
|
|
|
{
|
|
|
|
if (entity_properties.find(entity) != entity_properties.end())
|
|
|
|
throw CppGeneratorException("No properties for specified entity");
|
|
|
|
|
|
|
|
auto properties = entity_properties.at(entity);
|
|
|
|
|
|
|
|
if (properties.find(property_name) != properties.end())
|
|
|
|
throw CppGeneratorException("No property for specified property name");
|
|
|
|
|
|
|
|
return properties[property_name];
|
|
|
|
}
|
|
|
|
|
2016-07-24 10:47:48 +08:00
|
|
|
};
|