From 52d051d57146e6681b3c346e0836b0a3e4c94c4d Mon Sep 17 00:00:00 2001 From: Max Neunhoeffer Date: Thu, 18 Feb 2016 14:56:37 +0100 Subject: [PATCH] Add insertCluster method to transaction. Change OperationResult. --- arangod/Utils/OperationResult.h | 34 +++++++++++++++++++++++----- arangod/Utils/Transaction.cpp | 39 ++++++++++++++++++++++++++++----- 2 files changed, 61 insertions(+), 12 deletions(-) diff --git a/arangod/Utils/OperationResult.h b/arangod/Utils/OperationResult.h index 55b5bb15ca..4e1f6bee45 100644 --- a/arangod/Utils/OperationResult.h +++ b/arangod/Utils/OperationResult.h @@ -34,12 +34,33 @@ namespace arangodb { struct OperationResult { - OperationResult() : buffer(), customTypeHandler(nullptr), code(TRI_ERROR_NO_ERROR), wasSynchronous(false) {} - OperationResult(std::shared_ptr> buffer, VPackCustomTypeHandler* handler) : buffer(buffer), customTypeHandler(handler), code(TRI_ERROR_NO_ERROR), wasSynchronous(false) {} - OperationResult(int code, std::shared_ptr> buffer) : buffer(buffer), customTypeHandler(nullptr), code(code), wasSynchronous(false) {} - OperationResult(std::shared_ptr> buffer, bool wasSynchronous) : buffer(buffer), customTypeHandler(nullptr), code(TRI_ERROR_NO_ERROR), wasSynchronous(wasSynchronous) {} - explicit OperationResult(std::shared_ptr> buffer) : OperationResult(TRI_ERROR_NO_ERROR, buffer) {} - explicit OperationResult(int code) : OperationResult(code, nullptr) { } + // OperationResult() : buffer(), customTypeHandler(nullptr), code(TRI_ERROR_NO_ERROR), wasSynchronous(false) {} + //OperationResult(std::shared_ptr> buffer, VPackCustomTypeHandler* handler) : buffer(buffer), customTypeHandler(handler), code(TRI_ERROR_NO_ERROR), wasSynchronous(false) {} + //OperationResult(int code, std::shared_ptr> buffer) : buffer(buffer), customTypeHandler(nullptr), code(code), wasSynchronous(false) {} + //OperationResult(std::shared_ptr> buffer, bool wasSynchronous) : buffer(buffer), customTypeHandler(nullptr), code(TRI_ERROR_NO_ERROR), wasSynchronous(wasSynchronous) {} + //explicit OperationResult(std::shared_ptr> buffer) : OperationResult(TRI_ERROR_NO_ERROR, buffer) {} + + explicit OperationResult(int code) + : customTypeHandler(nullptr), code(code), wasSynchronous(false) { + if (code != TRI_ERROR_NO_ERROR) { + errorMessage = TRI_errno_string(code); + } + } + + OperationResult(int code, std::string const& message) + : customTypeHandler(nullptr), errorMessage(message), code(code), + wasSynchronous(false) { + TRI_ASSERT(code != TRI_ERROR_NO_ERROR); + } + + OperationResult(std::shared_ptr> buffer, + VPackCustomTypeHandler* handler, + std::string const& message, + int code, + bool wasSynchronous) + : buffer(buffer), customTypeHandler(handler), errorMessage(message), + code(code), wasSynchronous(wasSynchronous) { + } ~OperationResult() { // TODO: handle destruction of customTypeHandler @@ -60,6 +81,7 @@ struct OperationResult { std::shared_ptr> buffer; VPackCustomTypeHandler* customTypeHandler; + std::string errorMessage; int code; bool wasSynchronous; }; diff --git a/arangod/Utils/Transaction.cpp b/arangod/Utils/Transaction.cpp index ee9fffe42b..0c58ad0372 100644 --- a/arangod/Utils/Transaction.cpp +++ b/arangod/Utils/Transaction.cpp @@ -25,6 +25,7 @@ #include "Indexes/PrimaryIndex.h" #include "Storage/Marker.h" #include "VocBase/KeyGenerator.h" +#include "Cluster/ClusterMethods.h" #include #include @@ -514,7 +515,7 @@ OperationResult Transaction::documentLocal(std::string const& collectionName, resultBuilder.add(VPackSlice(mptr.vpack())); } - return OperationResult(resultBuilder.steal(), StorageOptions::getCustomTypeHandler(_vocbase)); + return OperationResult(resultBuilder.steal(), StorageOptions::getCustomTypeHandler(_vocbase), "", TRI_ERROR_NO_ERROR, false); } ////////////////////////////////////////////////////////////////////////////// @@ -556,8 +557,31 @@ OperationResult Transaction::insert(std::string const& collectionName, OperationResult Transaction::insertCoordinator(std::string const& collectionName, VPackSlice const& value, OperationOptions& options) { - // TODO - THROW_ARANGO_EXCEPTION(TRI_ERROR_NOT_IMPLEMENTED); + std::map headers; + arangodb::rest::HttpResponse::HttpResponseCode responseCode; + std::map resultHeaders; + std::string resultBody; + + int res = arangodb::createDocumentOnCoordinator( + _vocbase->_name, collectionName, options.waitForSync, + value, headers, responseCode, resultHeaders, resultBody); + + if (res == TRI_ERROR_NO_ERROR) { + VPackParser parser; + try { + parser.parse(resultBody); + auto bui = parser.steal(); + auto buf = bui->steal(); + return OperationResult(buf, nullptr, "", TRI_ERROR_NO_ERROR, + responseCode == arangodb::rest::HttpResponse::CREATED); + } + catch (VPackException& e) { + std::string message = "JSON from DBserver not parseable: " + + resultBody + ":" + e.what(); + return OperationResult(TRI_ERROR_INTERNAL, message); + } + } + return OperationResult(res); } ////////////////////////////////////////////////////////////////////////////// @@ -638,7 +662,8 @@ OperationResult Transaction::insertLocal(std::string const& collectionName, resultBuilder.add(TRI_VOC_ATTRIBUTE_KEY, VPackValue(resultKey)); resultBuilder.close(); - return OperationResult(resultBuilder.steal(), options.waitForSync); + return OperationResult(resultBuilder.steal(), nullptr, "", TRI_ERROR_NO_ERROR, + options.waitForSync || document->_info.waitForSync()); } OperationResult Transaction::replace(std::string const& collectionName, @@ -784,7 +809,8 @@ OperationResult Transaction::updateLocal(std::string const& collectionName, resultBuilder.add(TRI_VOC_ATTRIBUTE_KEY, VPackValue(resultKey)); resultBuilder.close(); - return OperationResult(resultBuilder.steal(), options.waitForSync); + return OperationResult(resultBuilder.steal(), nullptr, "", TRI_ERROR_NO_ERROR, + options.waitForSync || document->_info.waitForSync()); } ////////////////////////////////////////////////////////////////////////////// @@ -896,6 +922,7 @@ OperationResult Transaction::removeLocal(std::string const& collectionName, resultBuilder.add(TRI_VOC_ATTRIBUTE_KEY, VPackValue(key)); resultBuilder.close(); - return OperationResult(resultBuilder.steal(), options.waitForSync); + return OperationResult(resultBuilder.steal(), nullptr, "", TRI_ERROR_NO_ERROR, + options.waitForSync || document->_info.waitForSync()); }