big step forward, we are now able to pass Db into the shared library, next step is to implement CodeCompiler and CodeGenerator

This commit is contained in:
Marko Budiselic 2016-02-06 20:48:40 +01:00
parent 183ec03730
commit dbe1464f9b
12 changed files with 184 additions and 34 deletions

View File

@ -11,4 +11,10 @@ public:
Graph graph;
tx::Engine tx_engine;
// only for test purposes
std::string identifier()
{
return "memgraph";
}
};

View File

@ -16,6 +16,15 @@ public:
{
}
// TODO debug why this doesn't work
typename T::lib_object* instance()
{
// TODO singleton, lazy, concurrency
this->load();
lib_object = this->produce_method();
return lib_object;
}
void load()
{
load_lib();
@ -23,9 +32,17 @@ public:
load_destruct_func();
}
~DynamicLib()
{
if (lib_object != nullptr) {
destruct_method(lib_object);
}
}
private:
std::string lib_path;
void *dynamic_lib;
typename T::lib_object* lib_object;
void load_lib()
{

View File

@ -0,0 +1,20 @@
#pragma once
#include <string>
#include "i_code_cpu.hpp"
#include "database/db.hpp"
class CodeExecutor
{
public:
void execute(ICodeCPU *code_cpu)
{
code_cpu->name();
code_cpu->run(db);
}
private:
Db db;
};

View File

@ -0,0 +1,63 @@
#pragma once
#include <string>
#include <unordered_map>
#include <memory>
#include "memgraph_dynamic_lib.hpp"
#include "query_stripper.hpp"
#include "code_compiler.hpp"
#include "code_generator.hpp"
#include "utils/hashing/fnv.hpp"
using std::string;
using std::cout;
using std::endl;
class CodeLoader
{
public:
using sptr_code_lib = std::shared_ptr<CodeLib>;
CodeLoader()
: stripper(make_query_stripper(TK_INT, TK_FLOAT, TK_STR))
{
}
ICodeCPU* load_code_cpu(const string& query)
{
// TODO implement me
// for now returns already compiled code
auto stripped = stripper.strip(query);
// TODO move to logger
cout << "Stripped query is: " << stripped << endl;
auto stripped_hash = fnv(stripped);
// TODO move to logger
cout << "Query hash is: " << stripped_hash << endl;
auto code_lib = load_code_lib("./compiled/cpu/create_return.so");
code_libs.insert({{stripped_hash, code_lib}});
return code_lib->instance();
}
private:
// TODO somehow remove int.. from here
QueryStripper<int, int, int> stripper;
// TODO ifdef MEMGRAPH64 problem, how to use this kind
// of ifdef functions?
// uint64_t depends on fnv function
std::unordered_map<uint64_t, sptr_code_lib> code_libs;
CodeGenerator code_generator;
CodeCompiler code_compiler;
sptr_code_lib load_code_lib(const string& path)
{
sptr_code_lib code_lib = std::make_shared<CodeLib>(path);
code_lib->load();
return code_lib;
}
};

View File

@ -1,4 +1,7 @@
#!/bin/bash
# TODO: create Makefile or cmake script
clang++ -std=c++1y -g -I../ main.cpp ../cypher/cypher.cpp -o engine
cd compiled/cpu
clang++ -std=c++1y create_return.cpp -o create_return.so -I../../../ -shared -fPIC
cd ../..
clang++ -std=c++1y -g -I../ main.cpp ../cypher/cypher.cpp -o engine -ldl

View File

@ -0,0 +1,33 @@
#include <iostream>
#include "query_engine/i_code_cpu.hpp"
using std::cout;
using std::endl;
class CreateReturn : public ICodeCPU
{
public:
void name() const override
{
cout << "CRETE RETURN QUERY" << endl;
}
void run(Db& db) const override
{
cout << db.identifier() << endl;
}
~CreateReturn() {}
};
extern "C" ICodeCPU* produce()
{
return new CreateReturn();
}
extern "C" void destruct(ICodeCPU* p)
{
delete p;
}

Binary file not shown.

View File

@ -0,0 +1,14 @@
#pragma once
#include "database/db.hpp"
class ICodeCPU
{
public:
virtual void name() const = 0;
virtual void run(Db& db) const = 0;
virtual ~ICodeCPU() {}
};
typedef ICodeCPU* (*produce_t)();
typedef void (*destruct_t)(ICodeCPU*);

View File

@ -0,0 +1,18 @@
#pragma once
#include "i_code_cpu.hpp"
#include "dc/dynamic_lib.hpp"
class MemgraphDynamicLib
{
public:
const static std::string produce_name;
const static std::string destruct_name;
using produce = produce_t;
using destruct = destruct_t;
using lib_object = ICodeCPU;
};
const std::string MemgraphDynamicLib::produce_name = "produce";
const std::string MemgraphDynamicLib::destruct_name = "destruct";
using CodeLib = DynamicLib<MemgraphDynamicLib>;

View File

@ -1,49 +1,30 @@
#pragma once
#include <iostream>
#include "query_stripper.hpp"
#include "query_traverser.hpp"
#include "code_generator.hpp"
#include "code_compiler.hpp"
#include "query_executor.hpp"
#include "code_loader.hpp"
#include "code_executor.hpp"
#include "query_result.hpp"
#include "utils/hashing/fnv.hpp"
using std::cout;
using std::endl;
//
// Current arhitecture:
// query -> traverser -> [generator] -> [compiler] -> executor
// query -> code_loader -> query_stripper -> [code_generator]
// -> [code_compiler] -> code_executor
//
class QueryEngine
{
public:
QueryEngine()
: stripper(make_query_stripper(TK_INT, TK_FLOAT, TK_STR))
{
}
QueryResult execute(const std::string& query)
QueryResult* execute(const std::string& query)
{
cout << "QUERY ENGINE EXECUTE" << endl;
auto stripped = stripper.strip(query);
cout << "STRIPPED: " << stripped << endl;
auto stripped_hash = fnv(stripped);
cout << "STRIPPED HASH: " << stripped_hash << endl;
executor.execute(loader.load_code_cpu(query));
// traverser.build_tree(query);
// traverser.traverse();
return QueryResult();
throw std::runtime_error("implement me");
}
private:
// TODO: use IoC or something similar
QueryStripper<int, int, int> stripper;
QueryTraverser traverser;
CodeGenerator generator;
CodeCompiler compiler;
QueryExecutor executor;
CodeLoader loader;
CodeExecutor executor;
};

View File

@ -1,5 +0,0 @@
#pragma once
class QueryExecutor
{
};

View File