1
0
Fork 0

updated vpack library

This commit is contained in:
Jan Steemann 2015-11-24 12:03:20 +01:00
parent d24b46bb71
commit 2552c3493b
5 changed files with 47 additions and 39 deletions

View File

@ -110,16 +110,20 @@ class Builder {
// at position base, also determine the length len of the attribute. // at position base, also determine the length len of the attribute.
// This takes into account the different possibilities for the format // This takes into account the different possibilities for the format
// of attribute names: // of attribute names:
static uint8_t const* findAttrName(uint8_t const* base, uint64_t& len); static uint8_t const* findAttrName(uint8_t const* base, uint64_t& len,
Options const*);
static void sortObjectIndexShort(uint8_t* objBase, static void sortObjectIndexShort(uint8_t* objBase,
std::vector<ValueLength>& offsets); std::vector<ValueLength>& offsets,
Options const*);
static void sortObjectIndexLong(uint8_t* objBase, static void sortObjectIndexLong(uint8_t* objBase,
std::vector<ValueLength>& offsets); std::vector<ValueLength>& offsets,
Options const*);
static void sortObjectIndex(uint8_t* objBase, static void sortObjectIndex(uint8_t* objBase,
std::vector<ValueLength>& offsets); std::vector<ValueLength>& offsets,
Options const*);
public: public:
Options const* options; Options const* options;

View File

@ -42,8 +42,8 @@ std::string Builder::toString() const {
void Builder::doActualSort(std::vector<SortEntry>& entries) { void Builder::doActualSort(std::vector<SortEntry>& entries) {
VELOCYPACK_ASSERT(entries.size() > 1); VELOCYPACK_ASSERT(entries.size() > 1);
std::sort(entries.begin(), entries.end(), std::sort(entries.begin(), entries.end(), [](SortEntry const& a,
[](SortEntry const& a, SortEntry const& b) { SortEntry const& b) {
// return true iff a < b: // return true iff a < b:
uint8_t const* pa = a.nameStart; uint8_t const* pa = a.nameStart;
uint64_t sizea = a.nameSize; uint64_t sizea = a.nameSize;
@ -56,7 +56,8 @@ void Builder::doActualSort(std::vector<SortEntry>& entries) {
}); });
}; };
uint8_t const* Builder::findAttrName(uint8_t const* base, uint64_t& len) { uint8_t const* Builder::findAttrName(uint8_t const* base, uint64_t& len,
Options const* options) {
uint8_t const b = *base; uint8_t const b = *base;
if (b >= 0x40 && b <= 0xbe) { if (b >= 0x40 && b <= 0xbe) {
// short UTF-8 string // short UTF-8 string
@ -72,11 +73,15 @@ uint8_t const* Builder::findAttrName(uint8_t const* base, uint64_t& len) {
} }
return base + 1 + 8; // string starts here return base + 1 + 8; // string starts here
} }
throw Exception(Exception::NotImplemented, "Invalid Object key type");
// translate attribute name
Slice s(base, options);
return findAttrName(s.makeKey().start(), len, options);
} }
void Builder::sortObjectIndexShort(uint8_t* objBase, void Builder::sortObjectIndexShort(uint8_t* objBase,
std::vector<ValueLength>& offsets) { std::vector<ValueLength>& offsets,
Options const* options) {
auto cmp = [&](ValueLength a, ValueLength b) -> bool { auto cmp = [&](ValueLength a, ValueLength b) -> bool {
uint8_t const* aa = objBase + a; uint8_t const* aa = objBase + a;
uint8_t const* bb = objBase + b; uint8_t const* bb = objBase + b;
@ -88,8 +93,8 @@ void Builder::sortObjectIndexShort(uint8_t* objBase,
} else { } else {
uint64_t lena; uint64_t lena;
uint64_t lenb; uint64_t lenb;
aa = findAttrName(aa, lena); aa = findAttrName(aa, lena, options);
bb = findAttrName(bb, lenb); bb = findAttrName(bb, lenb, options);
uint64_t m = (std::min)(lena, lenb); uint64_t m = (std::min)(lena, lenb);
int c = memcmp(aa, bb, m); int c = memcmp(aa, bb, m);
return (c < 0 || (c == 0 && lena < lenb)); return (c < 0 || (c == 0 && lena < lenb));
@ -99,7 +104,8 @@ void Builder::sortObjectIndexShort(uint8_t* objBase,
} }
void Builder::sortObjectIndexLong(uint8_t* objBase, void Builder::sortObjectIndexLong(uint8_t* objBase,
std::vector<ValueLength>& offsets) { std::vector<ValueLength>& offsets,
Options const* options) {
// on some platforms we can use a thread-local vector // on some platforms we can use a thread-local vector
#if __llvm__ == 1 #if __llvm__ == 1
// nono thread local // nono thread local
@ -116,7 +122,7 @@ void Builder::sortObjectIndexLong(uint8_t* objBase,
for (ValueLength i = 0; i < offsets.size(); i++) { for (ValueLength i = 0; i < offsets.size(); i++) {
SortEntry e; SortEntry e;
e.offset = offsets[i]; e.offset = offsets[i];
e.nameStart = findAttrName(objBase + e.offset, e.nameSize); e.nameStart = findAttrName(objBase + e.offset, e.nameSize, options);
entries.push_back(e); entries.push_back(e);
} }
VELOCYPACK_ASSERT(entries.size() == offsets.size()); VELOCYPACK_ASSERT(entries.size() == offsets.size());
@ -129,11 +135,12 @@ void Builder::sortObjectIndexLong(uint8_t* objBase,
} }
void Builder::sortObjectIndex(uint8_t* objBase, void Builder::sortObjectIndex(uint8_t* objBase,
std::vector<ValueLength>& offsets) { std::vector<ValueLength>& offsets,
Options const* options) {
if (offsets.size() > 32) { if (offsets.size() > 32) {
sortObjectIndexLong(objBase, offsets); sortObjectIndexLong(objBase, offsets, options);
} else { } else {
sortObjectIndexShort(objBase, offsets); sortObjectIndexShort(objBase, offsets, options);
} }
} }
@ -304,7 +311,7 @@ void Builder::close() {
if (!options->sortAttributeNames) { if (!options->sortAttributeNames) {
_start[tos] = 0x0f; // unsorted _start[tos] = 0x0f; // unsorted
} else if (index.size() >= 2 && options->sortAttributeNames) { } else if (index.size() >= 2 && options->sortAttributeNames) {
sortObjectIndex(_start + tos, index); sortObjectIndex(_start + tos, index, options);
} }
} }
for (size_t i = 0; i < index.size(); i++) { for (size_t i = 0; i < index.size(); i++) {
@ -376,7 +383,7 @@ bool Builder::hasKey(std::string const& key) const {
} }
for (size_t i = 0; i < index.size(); ++i) { for (size_t i = 0; i < index.size(); ++i) {
Slice s(_start + tos + index[i], options); Slice s(_start + tos + index[i], options);
if (s.isString() && s.isEqualString(key)) { if (s.makeKey().isEqualString(key)) {
return true; return true;
} }
} }
@ -398,10 +405,7 @@ Slice Builder::getKey(std::string const& key) const {
} }
for (size_t i = 0; i < index.size(); ++i) { for (size_t i = 0; i < index.size(); ++i) {
Slice s(_start + tos + index[i], options); Slice s(_start + tos + index[i], options);
if (!s.isString()) { if (s.makeKey().isEqualString(key)) {
s = s.makeKey();
}
if (s.isString() && s.isEqualString(key)) {
return Slice(s.start() + s.byteSize(), options); return Slice(s.start() + s.byteSize(), options);
} }
} }
@ -742,6 +746,7 @@ void Builder::checkAttributeUniqueness(Slice const& obj) const {
std::unordered_set<std::string> keys; std::unordered_set<std::string> keys;
for (size_t i = 0; i < n; ++i) { for (size_t i = 0; i < n; ++i) {
// note: keyAt() already translates integer attributes
Slice key = obj.keyAt(i); Slice key = obj.keyAt(i);
if (!key.isString()) { if (!key.isString()) {
throw Exception(Exception::BuilderUnexpectedType, throw Exception(Exception::BuilderUnexpectedType,

View File

@ -48,10 +48,6 @@ std::ostream& operator<<(std::ostream& stream, HexDump const* hexdump) {
for (uint8_t it : hexdump->slice) { for (uint8_t it : hexdump->slice) {
if (current != 0) { if (current != 0) {
stream << hexdump->separator; stream << hexdump->separator;
}
stream << HexDump::toHex(it);
++current;
if (hexdump->valuesPerLine > 0 && current == hexdump->valuesPerLine) { if (hexdump->valuesPerLine > 0 && current == hexdump->valuesPerLine) {
stream << std::endl; stream << std::endl;
@ -59,6 +55,10 @@ std::ostream& operator<<(std::ostream& stream, HexDump const* hexdump) {
} }
} }
stream << HexDump::toHex(it);
++current;
}
return stream; return stream;
} }

View File

@ -214,7 +214,9 @@ unsigned int const Slice::FirstSubMap[32] = {
// translates an integer key into a string // translates an integer key into a string
Slice Slice::translate() const { Slice Slice::translate() const {
VELOCYPACK_ASSERT(isSmallInt() || isUInt()); if (!isSmallInt() && !isUInt()) {
throw Exception(Exception::NeedAttributeTranslator);
}
uint64_t id = getUInt(); uint64_t id = getUInt();
if (options->attributeTranslator == nullptr) { if (options->attributeTranslator == nullptr) {
@ -287,7 +289,7 @@ Slice Slice::get(std::string const& attribute) const {
if (!key.isEqualString(attribute)) { if (!key.isEqualString(attribute)) {
return Slice(); return Slice();
} }
} else if (key.isInteger()) { } else if (key.isSmallInt() || key.isUInt()) {
// translate key // translate key
if (!key.translate().isEqualString(attribute)) { if (!key.translate().isEqualString(attribute)) {
return Slice(); return Slice();
@ -435,7 +437,7 @@ Slice Slice::getFromCompactObject(std::string const& attribute) const {
if (key.isEqualString(attribute)) { if (key.isEqualString(attribute)) {
return Slice(key.start() + key.byteSize(), options); return Slice(key.start() + key.byteSize(), options);
} }
} else if (key.isInteger()) { } else if (key.isSmallInt() || key.isUInt()) {
if (key.translate().isEqualString(attribute)) { if (key.translate().isEqualString(attribute)) {
return Slice(key.start() + key.byteSize(), options); return Slice(key.start() + key.byteSize(), options);
} }
@ -536,7 +538,7 @@ Slice Slice::makeKey() const {
if (isString()) { if (isString()) {
return *this; return *this;
} }
if (isInteger()) { if (isSmallInt() || isUInt()) {
return translate(); return translate();
} }
@ -580,7 +582,7 @@ Slice Slice::searchObjectKeyLinear(std::string const& attribute,
if (!key.isEqualString(attribute)) { if (!key.isEqualString(attribute)) {
continue; continue;
} }
} else if (key.isInteger()) { } else if (key.isSmallInt() || key.isUInt()) {
// translate key // translate key
if (!key.translate().isEqualString(attribute)) { if (!key.translate().isEqualString(attribute)) {
continue; continue;
@ -618,7 +620,7 @@ Slice Slice::searchObjectKeyBinary(std::string const& attribute,
int res; int res;
if (key.isString()) { if (key.isString()) {
res = key.compareString(attribute); res = key.compareString(attribute);
} else if (key.isInteger()) { } else if (key.isSmallInt() || key.isUInt()) {
// translate key // translate key
res = key.translate().compareString(attribute); res = key.translate().compareString(attribute);
} else { } else {

View File

@ -47,12 +47,9 @@ int fpconv_dtoa(double fp, char dest[24]);
static uint64_t tens[] = { static uint64_t tens[] = {
10000000000000000000U, 1000000000000000000U, 100000000000000000U, 10000000000000000000U, 1000000000000000000U, 100000000000000000U,
10000000000000000U, 1000000000000000U, 100000000000000U, 10000000000000000U, 1000000000000000U, 100000000000000U, 10000000000000U,
10000000000000U, 1000000000000U, 100000000000U, 1000000000000U, 100000000000U, 10000000000U, 1000000000U, 100000000U,
10000000000U, 1000000000U, 100000000U, 10000000U, 1000000U, 100000U, 10000U, 1000U, 100U, 10U, 1U};
10000000U, 1000000U, 100000U,
10000U, 1000U, 100U,
10U, 1U};
static inline uint64_t get_dbits(double d) { static inline uint64_t get_dbits(double d) {
union { union {