From a4fb7b60b1b541134bc8acf175671bd9e3c004ee Mon Sep 17 00:00:00 2001
From: Marko Budiselic <mbudiselicbuda@gmail.com>
Date: Sat, 12 Mar 2016 10:57:09 +0100
Subject: [PATCH] query engine work in progress

---
 data_structures/skiplist/skiplist.hpp       |  2 +-
 mvcc/version_list.hpp                       |  1 -
 query_engine/main.cpp                       | 23 +++++++++++---
 query_engine/traverser/read_traverser.hpp   | 34 +++++++++++++++++++++
 query_engine/traverser/update_traverser.hpp | 11 +++++++
 storage/vertices.hpp                        |  4 +--
 6 files changed, 66 insertions(+), 9 deletions(-)

diff --git a/data_structures/skiplist/skiplist.hpp b/data_structures/skiplist/skiplist.hpp
index 6dce15f12..8d13e7d34 100644
--- a/data_structures/skiplist/skiplist.hpp
+++ b/data_structures/skiplist/skiplist.hpp
@@ -307,7 +307,7 @@ public:
         Iterator(const Iterator&) = default;
     };
 
-    SkipList() : header(Node::create(K(), std::move(T()), H)) {}
+    SkipList() : header(Node::create(K(), std::move(T(0)), H)) {}
 
     friend class Accessor;
 
diff --git a/mvcc/version_list.hpp b/mvcc/version_list.hpp
index c0e0940fa..4e8d071e9 100644
--- a/mvcc/version_list.hpp
+++ b/mvcc/version_list.hpp
@@ -91,7 +91,6 @@ public:
     VersionList(VersionList&& other) : id(other.id)
     {
         this->head = other.head.load();
-        this->identifier = other.id();
         other.head = nullptr;
     }
 
diff --git a/query_engine/main.cpp b/query_engine/main.cpp
index 82436c06c..9d9866df2 100644
--- a/query_engine/main.cpp
+++ b/query_engine/main.cpp
@@ -9,6 +9,7 @@
 
 using std::cout;
 using std::endl;
+using std::cin;
 
 int main(int argc, char** argv)
 {   
@@ -16,22 +17,34 @@ int main(int argc, char** argv)
     auto arguments = all_arguments(argc, argv);
 
     // query extraction
-    auto cypher_query = extract_query(arguments);
-    cout << "QUERY: " << cypher_query << endl;
+    // auto cypher_query = extract_query(arguments);
+    // cout << "QUERY: " << cypher_query << endl;
 
     QueryEngine engine;
     // engine.execute(cypher_query);
 
-    using std::placeholders::_1;
-    auto f = std::bind(&QueryEngine::execute, &engine, _1);
+    // using std::placeholders::_1;
+    // auto f = std::bind(&QueryEngine::execute, &engine, _1);
 
-    cout << std::fixed << timer(f, cypher_query) << endl;
+    // cout << std::fixed << timer(f, cypher_query) << endl;
 
     // double counter = 0;
     // for (int i = 0; i < 1000000; ++i) {
     //     counter += timer(f, cypher_query);
     // }
     // cout << 1000000 / (counter / 1000000000) << "create_transactions per sec" << endl;
+    
+    // shell
+    // std::string command;
+    // cout << "-- Memgraph query engine --" << endl;
+    // do {
+    //     cout << "> ";
+    //     std::getline(cin, command);
+    //     engine.execute(command);
+    // } while (command != "quit");
+    
+    engine.execute("CREATE (n{id:2}) RETURN n");
+    engine.execute("MATCH (n{id:0}) RETURN n");
 
     return 0;
 }
diff --git a/query_engine/traverser/read_traverser.hpp b/query_engine/traverser/read_traverser.hpp
index 594cb5ad2..047e44e85 100644
--- a/query_engine/traverser/read_traverser.hpp
+++ b/query_engine/traverser/read_traverser.hpp
@@ -2,7 +2,41 @@
 
 #include "code.hpp"
 #include "cypher/visitor/traverser.hpp"
+#include "query_engine/util.hpp"
 
 class ReadTraverser : public Traverser, public Code
 {
+private:
+    uint32_t index{0};
+
+public:
+    std::string code;
+
+    void visit(ast::Match& match) override
+    {
+        code += line("auto& t = db.tx_engine.begin();");
+
+        Traverser::visit(match);
+    };
+
+    void visit(ast::Property& property) override
+    {
+        code += line("auto id = args[" + std::to_string(index) + "]->as<Int32>();");
+        code += line("auto vertex_accessor = db.graph.vertices.find(t, id.value);");
+
+        ++index;
+    }
+
+    void visit(ast::Return& ret) override
+    {
+        code += line("t.commit();");
+        code += line("auto &properties = vertex_accessor.properties();");
+        code += line("ResultList::data_t data = {&properties};");
+        code += line("auto result_data = "
+                     "std::make_shared<ResultList>(std::move(data));");
+        code += line("QueryResult::data_t query_data = {{\"" +
+                     ret.return_list->value->name + "\", result_data}};");
+        code += line("return std::make_shared<QueryResult>"
+                     "(std::move(query_data));");
+    }
 };
diff --git a/query_engine/traverser/update_traverser.hpp b/query_engine/traverser/update_traverser.hpp
index 5e1a5b681..b5516b7be 100644
--- a/query_engine/traverser/update_traverser.hpp
+++ b/query_engine/traverser/update_traverser.hpp
@@ -5,4 +5,15 @@
 
 class UpdateTraverser : public Traverser, public Code
 {
+    void visit(ast::Match& match) override
+    {
+    }
+
+    void visit(ast::Set& set) override
+    {
+    }
+
+    void visit(ast::Return& ret) override
+    {
+    }
 };
diff --git a/storage/vertices.hpp b/storage/vertices.hpp
index 10f922fdd..555426aa4 100644
--- a/storage/vertices.hpp
+++ b/storage/vertices.hpp
@@ -27,8 +27,8 @@ public:
         auto next = counter.next(std::memory_order_acquire);
 
         // create new vertex record
-        VertexRecord vertex_record;
-        vertex_record.id(next);
+        VertexRecord vertex_record(next);
+        // vertex_record.id(next);
 
         // insert the new vertex record into the vertex store
         auto vertices_accessor = vertices.access();