From 57740bcf95759e7016be58cb447dcd413f9964b2 Mon Sep 17 00:00:00 2001
From: Dominik Gleich <dominik.gleich@memgraph.io>
Date: Tue, 7 Mar 2017 14:14:56 +0100
Subject: [PATCH] Fix bug -db_accessor passed along as &

Summary: Db_accessor is tied to one transaction, and as such each query should work on seperate db_accessor.

Reviewers: florijan, buda

Reviewed By: buda

Subscribers: pullbot

Differential Revision: https://phabricator.memgraph.io/D96
---
 tests/integration/query_engine.cpp        |  3 +--
 tests/integration/query_engine_common.hpp | 21 ++++++++++++---------
 tests/manual/query_engine.cpp             |  4 ++--
 3 files changed, 15 insertions(+), 13 deletions(-)

diff --git a/tests/integration/query_engine.cpp b/tests/integration/query_engine.cpp
index d6591d0dd..9bfe1fb92 100644
--- a/tests/integration/query_engine.cpp
+++ b/tests/integration/query_engine.cpp
@@ -35,8 +35,7 @@ int main(int argc, char *argv[]) {
   // IMPORTANT: PrintRecordStream can be replaces with a smarter
   // object that can test the results
 
-  auto db_accessor = dbms.active();
-  WarmUpEngine(log, query_engine, db_accessor, stream);
+  WarmUpEngine(log, query_engine, dbms, stream);
 
   return 0;
 }
diff --git a/tests/integration/query_engine_common.hpp b/tests/integration/query_engine_common.hpp
index 2d9497411..770c80996 100644
--- a/tests/integration/query_engine_common.hpp
+++ b/tests/integration/query_engine_common.hpp
@@ -4,6 +4,7 @@
 #include <set>
 namespace fs = std::experimental::filesystem;
 #include "database/graph_db_accessor.hpp"
+#include "dbms/dbms.hpp"
 #include "logging/default.hpp"
 #include "logging/streams/stdout.cpp"
 #include "query/engine.hpp"
@@ -120,15 +121,14 @@ auto LoadQueryPlans(Logger &log, QueryEngineT &engine,
  *
  * @param log external logger reference
  * @param engine query engine
- * @param db_accessor a database accessor on which the query plans are executed
+ * @param dbms a database to execute queries on
  * @param path path a queries file
  * @param stream used by query plans to output the results
  *
  * @return void
  */
-auto ExecuteQueryPlans(Logger &log, QueryEngineT &engine,
-                       GraphDbAccessor &db_accessor, const fs::path &path,
-                       StreamT &stream) {
+auto ExecuteQueryPlans(Logger &log, QueryEngineT &engine, Dbms &dbms,
+                       const fs::path &path, StreamT &stream) {
   log.info("*** Execute the queries from the queries_file ***");
   // execute all queries from queries_file
   auto queries = utils::read_lines(path);
@@ -136,6 +136,9 @@ auto ExecuteQueryPlans(Logger &log, QueryEngineT &engine,
     if (query.empty()) continue;
     permanent_assert(engine.Loaded(trim(query)),
                      "Implementation wasn't loaded");
+    // Create new db_accessor since one query is associated with one
+    // transaction.
+    auto db_accessor = dbms.active();
     engine.Run(query, db_accessor, stream);
   }
 }
@@ -148,17 +151,17 @@ auto ExecuteQueryPlans(Logger &log, QueryEngineT &engine,
  *
  * @param log external logger reference
  * @param engine query engine
- * @param db_accessor a database accessor on which the query plans are executed
+ * @param dbms a database to execute queries on
  * @param stream used by query plans to output the results
  *
  * @return void
  */
-auto WarmUpEngine(Logger &log, QueryEngineT &engine,
-                  GraphDbAccessor &db_accessor, StreamT &stream) {
+auto WarmUpEngine(Logger &log, QueryEngineT &engine, Dbms &dbms,
+                  StreamT &stream) {
   // path to a file with queries
   auto queries_file = fs::path(
       GET_ARG("-q", "../data/queries/core/mg_basic_002.txt").get_string());
-  // forlder with query implementations
+  // folder with query implementations
   auto implementations_folder =
       fs::path(GET_ARG("-i", "../integration/hardcoded_query").get_string());
 
@@ -169,7 +172,7 @@ auto WarmUpEngine(Logger &log, QueryEngineT &engine,
   LoadQueryPlans(log, engine, query_hashes, implementations_folder);
 
   // execute all loaded query plasn
-  ExecuteQueryPlans(log, engine, db_accessor, queries_file, stream);
+  ExecuteQueryPlans(log, engine, dbms, queries_file, stream);
 }
 }
 }
diff --git a/tests/manual/query_engine.cpp b/tests/manual/query_engine.cpp
index f9d7c10f8..34f68eb5b 100644
--- a/tests/manual/query_engine.cpp
+++ b/tests/manual/query_engine.cpp
@@ -18,13 +18,12 @@ int main(int argc, char* argv[]) {
   // init engine
   auto log = init_logging("ManualQueryEngine");
   Dbms dbms;
-  auto db_accessor = dbms.active();
   StreamT stream(std::cout);  // inject path to data queries
   QueryEngineT query_engine;
   // IMPORTANT: PrintRecordStream can be replaces with a smarter
   // object that can test the results
 
-  WarmUpEngine(log, query_engine, db_accessor, stream);
+  WarmUpEngine(log, query_engine, dbms, stream);
 
   // init watcher
   FSWatcher watcher;
@@ -61,6 +60,7 @@ int main(int argc, char* argv[]) {
             query_engine.Unload(query);
             try {
               query_engine.ReloadCustom(query, event.path);
+              auto db_accessor = dbms.active();
               query_engine.Run(query, db_accessor, stream);
             } catch (PlanCompilationException& e) {
               log.info("Query compilation failed: {}", e.what());