From fccfca0e47ced5bb7f901d6d484d1e007059d05b Mon Sep 17 00:00:00 2001 From: Jan Steemann Date: Mon, 18 Aug 2014 13:22:40 +0200 Subject: [PATCH] refactoring --- arangod/Aql/ExecutionBlock.cpp | 116 ++++++++++++++++----------------- arangod/Aql/ExecutionBlock.h | 16 +++++ arangod/Aql/ExecutionNode.cpp | 4 +- 3 files changed, 74 insertions(+), 62 deletions(-) diff --git a/arangod/Aql/ExecutionBlock.cpp b/arangod/Aql/ExecutionBlock.cpp index d63d7ba905..3d357a79cd 100644 --- a/arangod/Aql/ExecutionBlock.cpp +++ b/arangod/Aql/ExecutionBlock.cpp @@ -1930,6 +1930,55 @@ AqlItemBlock* ModificationBlock::getSome (size_t atLeast, } } +//////////////////////////////////////////////////////////////////////////////// +/// @brief resolve a collection name and return cid and document key +//////////////////////////////////////////////////////////////////////////////// + +int ModificationBlock::resolve (char const* handle, + TRI_voc_cid_t& cid, + std::string& key) const { + char const* p = strchr(handle, TRI_DOCUMENT_HANDLE_SEPARATOR_CHR); + if (p == nullptr || *p == '\0') { + return TRI_ERROR_ARANGO_DOCUMENT_HANDLE_BAD; + } + + if (*handle >= '0' && *handle <= '9') { + cid = triagens::basics::StringUtils::uint64(handle, p - handle); + } + else { + std::string const name(handle, p - handle); + cid = _trx->resolver()->getCollectionIdCluster(name); + } + + if (cid == 0) { + return TRI_ERROR_ARANGO_COLLECTION_NOT_FOUND; + } + + key = std::string(p + 1); + + return TRI_ERROR_NO_ERROR; +}; + +//////////////////////////////////////////////////////////////////////////////// +/// @brief extract a key from the AqlValue passed +//////////////////////////////////////////////////////////////////////////////// + +int ModificationBlock::extractKey (AqlValue const& value, + TRI_document_collection_t const* document, + std::string& key) const { + Json member(value.extractArrayMember(_trx, document, TRI_VOC_ATTRIBUTE_KEY)); + + // TODO: allow _id, too + + TRI_json_t const* json = member.json(); + if (TRI_IsStringJson(json)) { + key = std::string(json->_value._string.data, json->_value._string.length - 1); + return TRI_ERROR_NO_ERROR; + } + + return TRI_ERROR_ARANGO_DOCUMENT_KEY_MISSING; +} + // ----------------------------------------------------------------------------- // --SECTION-- class RemoveBlock // ----------------------------------------------------------------------------- @@ -1974,17 +2023,7 @@ void RemoveBlock::work (std::vector& blocks) { if (a.isArray()) { // value is an array. now extract the _key attribute - Json member(a.extractArrayMember(_trx, document, "_key")); - - // TODO: allow _id, too - - TRI_json_t const* json = member.json(); - if (TRI_IsStringJson(json)) { - key = std::string(json->_value._string.data, json->_value._string.length - 1); - } - else { - errorCode = TRI_ERROR_ARANGO_DOCUMENT_KEY_MISSING; - } + errorCode = extractKey(a, document, key); } else if (a.isString()) { // value is a string @@ -2046,30 +2085,6 @@ void InsertBlock::work (std::vector& blocks) { bool const isEdgeCollection = _collection->isEdgeCollection(); - auto resolve = [&](char const* handle, TRI_voc_cid_t& cid, std::string& key) -> int { - char const* p = strchr(handle, TRI_DOCUMENT_HANDLE_SEPARATOR_CHR); - if (p == nullptr || *p == '\0') { - return TRI_ERROR_ARANGO_DOCUMENT_HANDLE_BAD; - } - - if (*handle >= '0' && *handle <= '9') { - cid = triagens::basics::StringUtils::uint64(handle, p - handle); - } - else { - std::string const name(handle, p - handle); - cid = _trx->resolver()->getCollectionIdCluster(name); - } - - if (cid == 0) { - return TRI_ERROR_ARANGO_COLLECTION_NOT_FOUND; - } - - key = std::string(p + 1); - - return TRI_ERROR_NO_ERROR; - }; - - if (ep->_outVariable == nullptr) { // don't return anything @@ -2100,10 +2115,10 @@ void InsertBlock::work (std::vector& blocks) { // value is an array if (isEdgeCollection) { - // array must have "_from" and "_to" + // array must have _from and _to attributes TRI_json_t const* json; - Json member(a.extractArrayMember(_trx, document, "_from")); + Json member(a.extractArrayMember(_trx, document, TRI_VOC_ATTRIBUTE_FROM)); json = member.json(); if (TRI_IsStringJson(json)) { @@ -2114,7 +2129,7 @@ void InsertBlock::work (std::vector& blocks) { } if (errorCode == TRI_ERROR_NO_ERROR) { - Json member(a.extractArrayMember(_trx, document, "_to")); + Json member(a.extractArrayMember(_trx, document, TRI_VOC_ATTRIBUTE_TO)); json = member.json(); if (TRI_IsStringJson(json)) { errorCode = resolve(json->_value._string.data, edge._toCid, to); @@ -2202,17 +2217,7 @@ void UpdateBlock::work (std::vector& blocks) { if (a.isArray()) { // value is an array. now extract the _key attribute - Json member(a.extractArrayMember(_trx, document, "_key")); - - // TODO: allow _id, too - - TRI_json_t const* json = member.json(); - if (TRI_IsStringJson(json)) { - key = std::string(json->_value._string.data, json->_value._string.length - 1); - } - else { - errorCode = TRI_ERROR_ARANGO_DOCUMENT_KEY_MISSING; - } + errorCode = extractKey(a, document, key); } else { errorCode = TRI_ERROR_ARANGO_DOCUMENT_TYPE_INVALID; @@ -2280,20 +2285,11 @@ void ReplaceBlock::work (std::vector& blocks) { AqlValue a = res->getValue(i, registerId); int errorCode = TRI_ERROR_NO_ERROR; + std::string key; if (a.isArray()) { // value is an array. now extract the _key attribute - Json member(a.extractArrayMember(_trx, document, "_key")); - - // TODO: allow _id, too - - TRI_json_t const* json = member.json(); - if (TRI_IsStringJson(json)) { - key = std::string(json->_value._string.data, json->_value._string.length - 1); - } - else { - errorCode = TRI_ERROR_ARANGO_DOCUMENT_KEY_MISSING; - } + errorCode = extractKey(a, document, key); } else { errorCode = TRI_ERROR_ARANGO_DOCUMENT_TYPE_INVALID; diff --git a/arangod/Aql/ExecutionBlock.h b/arangod/Aql/ExecutionBlock.h index 9389324712..6d62f5bdc2 100644 --- a/arangod/Aql/ExecutionBlock.h +++ b/arangod/Aql/ExecutionBlock.h @@ -1070,6 +1070,22 @@ namespace triagens { virtual void work (std::vector&) = 0; +//////////////////////////////////////////////////////////////////////////////// +/// @brief resolve a collection name and return cid and document key +//////////////////////////////////////////////////////////////////////////////// + + int resolve (char const*, + TRI_voc_cid_t&, + std::string&) const; + +//////////////////////////////////////////////////////////////////////////////// +/// @brief extract a key from the AqlValue passed +//////////////////////////////////////////////////////////////////////////////// + + int extractKey (AqlValue const&, + TRI_document_collection_t const*, + std::string&) const; + // ----------------------------------------------------------------------------- // --SECTION-- protected variables // ----------------------------------------------------------------------------- diff --git a/arangod/Aql/ExecutionNode.cpp b/arangod/Aql/ExecutionNode.cpp index f96160dff2..656bab3194 100644 --- a/arangod/Aql/ExecutionNode.cpp +++ b/arangod/Aql/ExecutionNode.cpp @@ -294,7 +294,7 @@ void IndexRangeNode::toJsonHelper (std::map& indexTab, // put together the range info . . . Json ranges(Json::List); - +/* for (auto x : *_ranges) { Json item(Json::Array); item("name", Json(x._name)) @@ -304,7 +304,7 @@ void IndexRangeNode::toJsonHelper (std::map& indexTab, ("highOpen", Json(x._highOpen)); ranges(item); } - +*/ // Now put info about vocbase and cid in there json("database", Json(_vocbase->_name)) ("collection", Json(_collection->name))