diff --git a/arangod/Aql/IndexBlock.cpp b/arangod/Aql/IndexBlock.cpp index 7d82430ea9..7db3ce78a4 100644 --- a/arangod/Aql/IndexBlock.cpp +++ b/arangod/Aql/IndexBlock.cpp @@ -512,10 +512,8 @@ AqlItemBlock* IndexBlock::getSome(size_t atLeast, size_t atMost) { } } if (_cursor->collection()->readDocument(_trx, token, *_mmdr)) { - uint8_t const* vpack = _mmdr->vpack(); //back(); - // TODO use internal toAQL function res->setValue(_returned, static_cast(curRegs), - AqlValue(VPackSlice(vpack))); + _mmdr->createAqlValue()); if (_returned > 0) { // re-use already copied AqlValues @@ -531,10 +529,8 @@ AqlItemBlock* IndexBlock::getSome(size_t atLeast, size_t atMost) { callback = [&](DocumentIdentifierToken const& token) { TRI_ASSERT(res.get() != nullptr); if (_cursor->collection()->readDocument(_trx, token, *_mmdr)) { - uint8_t const* vpack = _mmdr->vpack(); //back(); - // TODO use internal toAQL function res->setValue(_returned, static_cast(curRegs), - AqlValue(VPackSlice(vpack))); + _mmdr->createAqlValue()); if (_returned > 0) { // re-use already copied AqlValues diff --git a/arangod/VocBase/ManagedDocumentResult.cpp b/arangod/VocBase/ManagedDocumentResult.cpp index f3bf76f39f..4af6d73594 100644 --- a/arangod/VocBase/ManagedDocumentResult.cpp +++ b/arangod/VocBase/ManagedDocumentResult.cpp @@ -22,12 +22,14 @@ //////////////////////////////////////////////////////////////////////////////// #include "ManagedDocumentResult.h" +#include "Aql/AqlValue.h" #include #include #include using namespace arangodb; +using namespace arangodb::aql; void ManagedDocumentResult::clone(ManagedDocumentResult& cloned) const { cloned.reset(); @@ -100,3 +102,17 @@ void ManagedDocumentResult::addToBuilder(velocypack::Builder& builder, bool allo builder.add(velocypack::Slice(_vpack)); } } + +// @brief Creates an AQLValue with the content of this ManagedDocumentResult +// The caller is responsible to properly destroy() the +// returned value +AqlValue ManagedDocumentResult::createAqlValue() const { + TRI_ASSERT(!empty()); + if (canUseInExternal()) { + // No need to copy. Underlying structure guarantees that Slices stay + // valid + return AqlValue(_vpack, AqlValueFromManagedDocument()); + } + // Do copy. Otherwise the slice may go out of scope + return AqlValue(VPackSlice(_vpack)); +} diff --git a/arangod/VocBase/ManagedDocumentResult.h b/arangod/VocBase/ManagedDocumentResult.h index e1e16a3201..30e3a17990 100644 --- a/arangod/VocBase/ManagedDocumentResult.h +++ b/arangod/VocBase/ManagedDocumentResult.h @@ -32,6 +32,10 @@ namespace velocypack { class Builder; } +namespace aql { +struct AqlValue; +} + class ManagedDocumentResult { public: ManagedDocumentResult() : @@ -91,6 +95,11 @@ class ManagedDocumentResult { void addToBuilder(velocypack::Builder& builder, bool allowExternals) const; + // @brief Creates an AQLValue with the content of this ManagedDocumentResult + // The caller is responsible to properly destroy() the + // returned value + aql::AqlValue createAqlValue() const; + private: uint64_t _length; TRI_voc_rid_t _lastRevisionId;