8000 update velocypack version by jsteemann · Pull Request #9379 · arangodb/arangodb · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 51 additions & 20 deletions 3rdParty/velocypack/include/velocypack/Builder.h
8000
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "velocypack/Buffer.h"
#include "velocypack/Exception.h"
#include "velocypack/Options.h"
#include "velocypack/Serializable.h"
#include "velocypack/Slice.h"
#include "velocypack/StringRef.h"
#include "velocypack/Value.h"
Expand Down Expand Up @@ -74,7 +75,7 @@ class Builder {
// buffer. Whenever the stack is empty, one can use the start,
// size and slice methods to get out the ready built VPack
// object(s).

private:
std::shared_ptr<Buffer<uint8_t>> _buffer; // Here we collect the result
Buffer<uint8_t>* _bufferPtr; // used for quicker access than shared_ptr
Expand All @@ -89,10 +90,10 @@ class Builder {

public:
Options const* options;
// create an empty Builder, using default Options

// create an empty Builder, using default Options
Builder();

explicit Builder(Options const* options);
explicit Builder(std::shared_ptr<Buffer<uint8_t>> const& buffer,
Options const* options = &Options::Defaults);
Expand Down Expand Up @@ -208,15 +209,15 @@ class Builder {
ValueLength const tos = _stack.back();
return _start[tos] == 0x0b || _start[tos] == 0x14;
}

inline void openArray(bool unindexed = false) {
openCompoundValue(unindexed ? 0x13 : 0x06);
}

inline void openObject(bool unindexed = false) {
openCompoundValue(unindexed ? 0x14 : 0x0b);
}

template <typename T>
uint8_t* addUnchecked(char const* attrName, std::size_t attrLength, T const& sub) {
bool haveReported = false;
Expand Down Expand Up @@ -251,7 +252,7 @@ class Builder {
inline uint8_t* add(char const* attrName, std::size_t attrLength, Value const& sub) {
return addInternal<Value>(attrName, attrLength, sub);
}

// Add a subvalue into an object from a Slice:
inline uint8_t* add(std::string const& attrName, Slice const& sub) {
return addInternal<Slice>(attrName, sub);
Expand All @@ -265,7 +266,7 @@ class Builder {
inline uint8_t* add(char const* attrName, std::size_t attrLength, Slice const& sub) {
return addInternal<Slice>(attrName, attrLength, sub);
}

// Add a subvalue into an object from a ValuePair:
inline uint8_t* add(std::string const& attrName, ValuePair const& sub) {
return addInternal<ValuePair>(attrName, sub);
Expand All @@ -279,22 +280,41 @@ class Builder {
inline uint8_t* add(char const* attrName, std::size_t attrLength, ValuePair const& sub) {
return addInternal<ValuePair>(attrName, attrLength, sub);
}


// Add a subvalue into an object from a Serializable:
inline uint8_t* add(std::string const& attrName, Serialize const& sub) {
return addInternal<Serializable>(attrName, sub._sable);
}
inline uint8_t* add(StringRef const& attrName, Serialize const& sub) {
return addInternal<Serializable>(attrName, sub._sable);
}
inline uint8_t* add(char const* attrName, Serialize const& sub) {
return addInternal<Serializable>(attrName, sub._sable);
}
inline uint8_t* add(char const* attrName, std::size_t attrLength, Serialize const& sub) {
return addInternal<Serializable>(attrName, attrLength, sub._sable);
}

// Add a subvalue into an array from a Value:
inline uint8_t* add(Value const& sub) {
return addInternal<Value>(sub);
return addInternal<Value>(sub);
}

// Add a slice to an array
inline uint8_t* add(Slice const& sub) {
return addInternal<Slice>(sub);
return addInternal<Slice>(sub);
}

// Add a subvalue into an array from a ValuePair:
inline uint8_t* add(ValuePair const& sub) {
return addInternal<ValuePair>(sub);
}

// Add a subvalue into an array from a Serializable:
inline uint8_t* add(Serialize const& sub) {
return addInternal<Serializable>(sub._sable);
}

// Add an External slice to an array
uint8_t* addExternal(uint8_t const* sub) {
if (options->disallowExternals) {
Expand Down Expand Up @@ -327,7 +347,7 @@ class Builder {
throw;
}
}

// Add all subkeys and subvalues into an object from an ObjectIterator
// and leaves open the object intentionally
uint8_t* add(ObjectIterator&& sub);
Expand Down Expand Up @@ -445,18 +465,23 @@ class Builder {

void addInt(int64_t v) {
if (v >= 0 && v <= 9) {
// SmallInt
appendByte(static_cast<uint8_t>(0x30 + v));
} else if (v < 0 && v >= -6) {
// SmallInt
appendByte(static_cast<uint8_t>(0x40 + v));
} else {
// regular int
appendInt(v, 0x1f);
}
}

void addUInt(uint64_t v) {
if (v <= 9) {
// SmallInt
appendByte(static_cast<uint8_t>(0x30 + v));
} else {
// regular UInt
appendUInt(v, 0x27);
}
}
Expand Down Expand Up @@ -524,7 +549,7 @@ class Builder {
uint8_t* addInternal(std::string const& attrName, T const& sub) {
return addInternal<T>(attrName.data(), attrName.size(), sub);
}

template <typename T>
uint8_t* addInternal(StringRef const& attrName, T const& sub) {
return addInternal<T>(attrName.data(), attrName.size(), sub);
Expand Down Expand Up @@ -579,7 +604,7 @@ class Builder {
throw;
}
}

void addCompoundValue(uint8_t type) {
reserve(9);
// an Array or Object is started:
Expand Down Expand Up @@ -624,6 +649,12 @@ class Builder {

uint8_t* set(Slice const& item);

uint8_t* set(Serializable const& sable) {
auto const oldPos = _pos;
sable.toVelocyPack(*this);
return _start + oldPos;
}

void cleanupAdd() noexcept {
std::size_t depth = _stack.size() - 1;
VELOCYPACK_ASSERT(!_index[depth].empty());
Expand All @@ -640,7 +671,7 @@ class Builder {
reserve(n);
appendLengthUnchecked<n>(v);
}

template <uint64_t n>
void appendLengthUnchecked(ValueLength v) {
for (uint64_t i = 0; i < n; ++i) {
Expand Down Expand Up @@ -695,18 +726,18 @@ class Builder {
x >>= 8;
}
}

inline void appendByte(uint8_t value) {
reserve(1);
appendByteUnchecked(value);
}

inline void appendByteUnchecked(uint8_t value) {
_start[_pos++] = value;
VELOCYPACK_ASSERT(_bufferPtr != nullptr);
_bufferPtr->advance();
}

inline void resetTo(std::size_t value) {
_pos = value;
VELOCYPACK_ASSERT(_bufferPtr != nullptr);
Expand All @@ -719,7 +750,7 @@ class Builder {
VELOCYPACK_ASSERT(_bufferPtr != nullptr);
_bufferPtr->advance(value);
}

// move byte position x bytes back
inline void rollback(std::size_t value) noexcept {
_pos -= value;
Expand Down
149 changes: 45 additions & 104 deletions 3rdParty/velocypack/include/velocypack/Compare.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,122 +28,63 @@
#define VELOCYPACK_COMPARE_H 1

#include "velocypack/velocypack-common.h"
#include "velocypack/Collection.h"
#include "velocypack/Exception.h"
#include "velocypack/Iterator.h"
#include "velocypack/Slice.h"
#include "velocypack/ValueType.h"

#include <set>

namespace arangodb {
namespace velocypack {
struct Options;
class Slice;

// helper struct for comparing VelocyPack Slices on a binary level
struct BinaryCompare {
// returns true if the two Slices are identical on the binary level
static bool equals(Slice lhs, Slice rhs);

struct Hash {
size_t operator()(arangodb::velocypack::Slice const&) const;
};

struct Equal {
arangodb::velocypack::Options const* _options;

Equal() : _options(nullptr) {}
explicit Equal(arangodb::velocypack::Options const* opts)
: _options(opts) {}

bool operator()(arangodb::velocypack::Slice const&,
arangodb::velocypack::Slice const&) const;
};

};

// helper struct for comparing VelocyPack Slices in a normalized way
struct NormalizedCompare {

static bool equalsNumbers(Slice lhs, Slice rhs) {
auto lhsType = lhs.type();
if (lhsType == rhs.type()) {
// both types are equal
if (lhsType == ValueType::Int || lhsType == ValueType::SmallInt) {
// use exact comparisons. no need to cast to double
return (lhs.getIntUnchecked() == rhs.getIntUnchecked());
}

if (lhsType == ValueType::UInt) {
// use exact comparisons. no need to cast to double
return (lhs.getUIntUnchecked() == rhs.getUIntUnchecked());
}
// fallthrough to double comparison
}

return (lhs.getNumericValue<double>() == rhs.getNumericValue<double>());
}
// function to compare two numeric values
static bool equalsNumbers(Slice lhs, Slice rhs);

static bool equalsStrings(Slice lhs, Slice rhs) {
ValueLength nl;
char const* left = lhs.getString(nl);
VELOCYPACK_ASSERT(left != nullptr);
ValueLength nr;
char const* right = rhs.getString(nr);
VELOCYPACK_ASSERT(right != nullptr);
return (nl == nr && (memcmp(left, right, nl) == 0));
}
// function to compare two string values
static bool equalsStrings(Slice lhs, Slice rhs);

static bool equals(Slice lhs, Slice rhs) {
lhs = lhs.resolveExternals();
rhs = rhs.resolveExternals();
ValueType lhsType = valueTypeGroup(lhs.type());
ValueType rhsType = valueTypeGroup(rhs.type());
// function to compare two arbitrary Slices
static bool equals(Slice lhs, Slice rhs);

if (lhsType != rhsType) {
// unequal types => not equal
return false;
}
struct Hash {
size_t operator()(arangodb::velocypack::Slice const&) const;
};

switch (lhsType) {
case ValueType::Illegal:
case ValueType::None:
case ValueType::Null:
// Null equals Null
case ValueType::MinKey:
case ValueType::MaxKey: {
return true;
}
case ValueType::Bool: {
return (lhs.getBoolean() == rhs.getBoolean());
}
case ValueType::Double:
case ValueType::Int:
case ValueType::UInt:
case ValueType::SmallInt: {
return equalsNumbers(lhs, rhs);
}
case ValueType::String: {
return equalsStrings(lhs, rhs);
}
case ValueType::Array: {
ArrayIterator lhsValue(lhs);
ArrayIterator rhsValue(rhs);

ValueLength const n = lhsValue.size();
if (n != rhsValue.size()) {
// unequal array lengths
return false;
}
for (ValueLength i = 0; i < n; ++i) {
// recurse
if (!equals(lhsValue.value(), rhsValue.value())) {
return false;
}
lhsValue.next();
rhsValue.next();
}
return true;
}
case ValueType::Object: {
std::set<std::string> keys;
// get all keys and bring them in order
Collection::unorderedKeys(lhs, keys);
Collection::unorderedKeys(rhs, keys);
for (auto const& key : keys) {
// recurse
if (!equals(lhs.get(key), rhs.get(key))) {
return false;
}
}
return true;
}
case ValueType::Custom: {
throw Exception(Exception::NotImplemented, "equals comparison for Custom type is not implemented");
}
default: {
throw Exception(Exception::InternalError, "invalid value type for equals comparison");
}
}
}
struct Equal {
arangodb::velocypack::Options const* _options;

Equal() : _options(nullptr) {}
explicit Equal(arangodb::velocypack::Options const* opts)
: _options(opts) {}

bool operator()(arangodb::velocypack::Slice const&,
arangodb::velocypack::Slice const&) const;
};

};

}
}

Expand Down
Loading
0