1
0
Fork 0

Parsing the result in cluster is now done through VPack

This commit is contained in:
Michael Hackstein 2016-01-16 23:43:02 +01:00
parent e1a63aa5a0
commit fc8b45b06f
2 changed files with 35 additions and 34 deletions

View File

@ -38,6 +38,9 @@
#include "SimpleHttpClient/SimpleHttpClient.h" #include "SimpleHttpClient/SimpleHttpClient.h"
#include "SimpleHttpClient/SimpleHttpResult.h" #include "SimpleHttpClient/SimpleHttpResult.h"
#include <velocypack/Iterator.h>
#include <velocypack/velocypack-aliases.h>
using namespace triagens::arango; using namespace triagens::arango;
@ -152,7 +155,6 @@ std::string AgencyCommResult::errorMessage() const {
} catch (VPackException const&) { } catch (VPackException const&) {
return std::string("Out of memory"); return std::string("Out of memory");
} }
return result;
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
@ -200,22 +202,21 @@ void AgencyCommResult::clear() {
/// stripKeyPrefix is decoded, as is the _globalPrefix /// stripKeyPrefix is decoded, as is the _globalPrefix
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
bool AgencyCommResult::parseJsonNode(TRI_json_t const* node, bool AgencyCommResult::parseVelocyPackNode(VPackSlice const& node,
std::string const& stripKeyPrefix, std::string const& stripKeyPrefix,
bool withDirs) { bool withDirs) {
if (!TRI_IsObjectJson(node)) { if (!node.isObject()) {
return true; return true;
} }
// get "key" attribute // get "key" attribute
TRI_json_t const* key = TRI_LookupObjectJson(node, "key"); VPackSlice const key = node.get("key");
if (!TRI_IsStringJson(key)) { if (!key.isString()) {
return false; return false;
} }
std::string keydecoded = std::move(AgencyComm::decodeKey( std::string keydecoded = std::move(AgencyComm::decodeKey(key.copyString()));
std::string(key->_value._string.data, key->_value._string.length - 1)));
// make sure we don't strip more bytes than the key is long // make sure we don't strip more bytes than the key is long
size_t const offset = size_t const offset =
@ -230,8 +231,8 @@ bool AgencyCommResult::parseJsonNode(TRI_json_t const* node,
} }
// get "dir" attribute // get "dir" attribute
TRI_json_t const* dir = TRI_LookupObjectJson(node, "dir"); bool isDir =
bool isDir = (TRI_IsBooleanJson(dir) && dir->_value._boolean); triagens::basics::VelocyPackHelper::getBooleanValue(node, "dir", false);
if (isDir) { if (isDir) {
if (withDirs) { if (withDirs) {
@ -244,19 +245,15 @@ bool AgencyCommResult::parseJsonNode(TRI_json_t const* node,
} }
// is a directory, so there may be a "nodes" attribute // is a directory, so there may be a "nodes" attribute
TRI_json_t const* nodes = TRI_LookupObjectJson(node, "nodes"); VPackSlice const nodes = node.get("nodes");
if (!TRI_IsArrayJson(nodes)) { if (!nodes.isArray()) {
// if directory is empty... // if directory is empty...
return true; return true;
} }
size_t const n = TRI_LengthVector(&nodes->_value._objects); for (auto const& subNode : VPackArrayIterator(nodes)) {
if (!parseVelocyPackNode(subNode, stripKeyPrefix, withDirs)) {
for (size_t i = 0; i < n; ++i) {
if (!parseJsonNode(
(TRI_json_t const*)TRI_AtVector(&nodes->_value._objects, i),
stripKeyPrefix, withDirs)) {
return false; return false;
} }
} }
@ -264,17 +261,18 @@ bool AgencyCommResult::parseJsonNode(TRI_json_t const* node,
// not a directory // not a directory
// get "value" attribute // get "value" attribute
TRI_json_t const* value = TRI_LookupObjectJson(node, "value"); VPackSlice const value = node.get("value");
if (TRI_IsStringJson(value)) { if (value.isString()) {
if (!prefix.empty()) { if (!prefix.empty()) {
AgencyCommResultEntry entry; AgencyCommResultEntry entry;
// get "modifiedIndex" // get "modifiedIndex"
entry._index = entry._index =
triagens::basics::JsonHelper::stringUInt64(node, "modifiedIndex"); triagens::basics::VelocyPackHelper::stringUInt64(node.get("value"));
entry._json = triagens::basics::JsonHelper::fromString( std::string tmp = value.copyString();
value->_value._string.data, value->_value._string.length - 1); entry._json =
triagens::basics::JsonHelper::fromString(tmp.c_str(), tmp.size());
entry._isDir = false; entry._isDir = false;
_values.emplace(prefix, entry); _values.emplace(prefix, entry);
@ -291,20 +289,23 @@ bool AgencyCommResult::parseJsonNode(TRI_json_t const* node,
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
bool AgencyCommResult::parse(std::string const& stripKeyPrefix, bool withDirs) { bool AgencyCommResult::parse(std::string const& stripKeyPrefix, bool withDirs) {
TRI_json_t* json = TRI_JsonString(TRI_UNKNOWN_MEM_ZONE, _body.c_str()); std::shared_ptr<VPackBuilder> parsedBody;
try {
parsedBody = VPackParser::fromJson(_body.c_str());
} catch (...) {
return false;
}
if (!TRI_IsObjectJson(json)) { VPackSlice slice = parsedBody->slice();
if (json != nullptr) {
TRI_FreeJson(TRI_UNKNOWN_MEM_ZONE, json); if(!slice.isObject()) {
}
return false; return false;
} }
// get "node" attribute // get "node" attribute
TRI_json_t const* node = TRI_LookupObjectJson(json, "node"); VPackSlice const node = slice.get("node");
bool const result = parseJsonNode(node, stripKeyPrefix, withDirs); bool const result = parseVelocyPackNode(node, stripKeyPrefix, withDirs);
TRI_FreeJson(TRI_UNKNOWN_MEM_ZONE, json);
return result; return result;
} }

View File

@ -177,12 +177,12 @@ struct AgencyCommResult {
void clear(); void clear();
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////
/// @brief recursively flatten the JSON response into a map /// @brief recursively flatten the VelocyPack response into a map
/// ///
/// stripKeyPrefix is decoded, as is the _globalPrefix /// stripKeyPrefix is decoded, as is the _globalPrefix
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////
bool parseJsonNode(TRI_json_t const*, std::string const&, bool); bool parseVelocyPackNode(VPackSlice const&, std::string const&, bool);
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////
/// parse an agency result /// parse an agency result