2016-02-26 06:49:35 +08:00
|
|
|
#pragma once
|
|
|
|
|
2016-07-17 08:22:43 +08:00
|
|
|
// TODO: wrap fmt format
|
|
|
|
#include <fmt/format.h>
|
|
|
|
|
2016-02-26 14:45:43 +08:00
|
|
|
#include "code.hpp"
|
2016-02-26 06:49:35 +08:00
|
|
|
#include "cypher/visitor/traverser.hpp"
|
2016-07-17 08:22:43 +08:00
|
|
|
#include "query_engine/state_machine/cypher.hpp"
|
2016-03-12 17:57:09 +08:00
|
|
|
#include "query_engine/util.hpp"
|
2016-07-17 08:22:43 +08:00
|
|
|
#include "utils/underlying_cast.hpp"
|
|
|
|
|
|
|
|
using namespace entity_search;
|
2016-02-26 06:49:35 +08:00
|
|
|
|
2016-02-26 14:45:43 +08:00
|
|
|
class ReadTraverser : public Traverser, public Code
|
2016-02-26 06:49:35 +08:00
|
|
|
{
|
2016-03-12 17:57:09 +08:00
|
|
|
private:
|
2016-07-17 08:22:43 +08:00
|
|
|
// TODO: change int to something bigger (int64_t)
|
|
|
|
std::map<std::string, int> internal_ids;
|
|
|
|
|
|
|
|
std::set<std::string> entities;
|
|
|
|
CypherStateMachine csm;
|
2016-03-12 17:57:09 +08:00
|
|
|
uint32_t index{0};
|
2016-07-17 08:22:43 +08:00
|
|
|
std::map<std::string, uint32_t> property_indices;
|
2016-03-12 17:57:09 +08:00
|
|
|
|
|
|
|
public:
|
|
|
|
std::string code;
|
|
|
|
|
2016-07-17 08:22:43 +08:00
|
|
|
void update_entities(const std::string &name)
|
2016-03-12 17:57:09 +08:00
|
|
|
{
|
2016-07-17 08:22:43 +08:00
|
|
|
std::cout << name << std::endl;
|
|
|
|
if (entities.find(name) == entities.end()) {
|
|
|
|
csm.init_cost(name);
|
|
|
|
property_indices[name] = index++;
|
|
|
|
entities.insert(std::move(name));
|
|
|
|
}
|
|
|
|
}
|
2016-03-12 17:57:09 +08:00
|
|
|
|
2016-07-17 08:22:43 +08:00
|
|
|
void visit(ast::Match &match) override
|
|
|
|
{
|
|
|
|
code += LINE(code::transaction_begin);
|
2016-03-12 17:57:09 +08:00
|
|
|
Traverser::visit(match);
|
|
|
|
};
|
|
|
|
|
2016-07-17 08:22:43 +08:00
|
|
|
void visit(ast::Node &node) override
|
2016-03-12 17:57:09 +08:00
|
|
|
{
|
2016-07-17 08:22:43 +08:00
|
|
|
auto identifier = node.idn;
|
|
|
|
if (identifier == nullptr) return;
|
|
|
|
|
|
|
|
auto name = identifier->name;
|
|
|
|
update_entities(name);
|
2016-03-12 17:57:09 +08:00
|
|
|
|
2016-07-17 08:22:43 +08:00
|
|
|
if (node.labels != nullptr && node.labels->value != nullptr)
|
|
|
|
csm.search_cost(name, search_label_index, label_cost);
|
2016-03-12 17:57:09 +08:00
|
|
|
}
|
|
|
|
|
2016-07-17 08:22:43 +08:00
|
|
|
void visit(ast::InternalIdExpr &internal_id_expr) override
|
2016-07-12 03:10:53 +08:00
|
|
|
{
|
2016-07-17 08:22:43 +08:00
|
|
|
if (internal_id_expr.identifier == nullptr) return;
|
|
|
|
auto name = internal_id_expr.identifier->name;
|
|
|
|
|
|
|
|
// value has to exist
|
|
|
|
if (internal_id_expr.value == nullptr) {
|
|
|
|
// TODO: raise exception
|
|
|
|
}
|
|
|
|
|
|
|
|
update_entities(name);
|
|
|
|
csm.search_cost(name, search_internal_id, internal_id_cost);
|
2016-07-12 03:10:53 +08:00
|
|
|
}
|
|
|
|
|
2016-07-17 08:22:43 +08:00
|
|
|
void visit(ast::Return &ret) override
|
2016-03-12 17:57:09 +08:00
|
|
|
{
|
2016-07-17 08:22:43 +08:00
|
|
|
for (auto const &entity : entities) {
|
|
|
|
std::cout << entity << std::endl;
|
|
|
|
std::cout << underlying_cast(csm.min(entity)) << std::endl;
|
|
|
|
if (csm.min(entity) == search_internal_id) {
|
|
|
|
auto property_index = property_indices.at(entity);
|
|
|
|
code += LINE(
|
|
|
|
fmt::format(code::args_id, std::to_string(property_index)));
|
|
|
|
code += LINE(code::vertex_accessor_args_id);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (csm.min(entity) == search_label_index) {
|
|
|
|
code += LINE(fmt::format(code::todo, "search label index"));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (csm.min(entity) == search_main_storage) {
|
|
|
|
code += LINE(fmt::format(code::todo, "return all vertices"));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
code::debug_print_vertex_labels();
|
|
|
|
|
|
|
|
code += LINE(code::transaction_commit);
|
|
|
|
code += LINE(code::return_empty_result);
|
2016-03-12 17:57:09 +08:00
|
|
|
}
|
2016-02-26 06:49:35 +08:00
|
|
|
};
|