2016-02-09 23:12:00 +00:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
2017-07-15 23:03:42 +00:00
|
|
|
// This source code is licensed under both the GPLv2 (found in the
|
|
|
|
// COPYING file in the root directory) and Apache 2.0 License
|
|
|
|
// (found in the LICENSE.Apache file in the root directory).
|
2014-06-20 09:14:14 +00:00
|
|
|
#ifndef ROCKSDB_LITE
|
|
|
|
|
2014-07-23 14:21:38 +00:00
|
|
|
#include "rocksdb/utilities/json_document.h"
|
2014-06-20 09:14:14 +00:00
|
|
|
|
2014-09-05 06:14:37 +00:00
|
|
|
#ifndef __STDC_FORMAT_MACROS
|
2014-07-10 16:31:42 +00:00
|
|
|
#define __STDC_FORMAT_MACROS
|
2014-09-05 06:14:37 +00:00
|
|
|
#endif
|
|
|
|
|
2015-02-26 21:04:14 +00:00
|
|
|
#include <assert.h>
|
2014-07-10 16:31:42 +00:00
|
|
|
#include <inttypes.h>
|
2015-02-26 21:04:14 +00:00
|
|
|
#include <string.h>
|
2015-01-09 20:57:11 +00:00
|
|
|
|
2015-02-26 21:04:14 +00:00
|
|
|
#include <functional>
|
|
|
|
#include <limits>
|
2014-06-20 09:14:14 +00:00
|
|
|
#include <map>
|
2015-01-09 20:57:11 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
2014-06-20 09:14:14 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2015-02-26 21:04:14 +00:00
|
|
|
|
|
|
|
#include "third-party/fbson/FbsonDocument.h"
|
|
|
|
#include "third-party/fbson/FbsonJsonParser.h"
|
|
|
|
#include "third-party/fbson/FbsonUtil.h"
|
2014-06-20 09:14:14 +00:00
|
|
|
#include "util/coding.h"
|
|
|
|
|
2015-02-26 21:04:14 +00:00
|
|
|
using std::placeholders::_1;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
size_t ObjectNumElem(const fbson::ObjectVal& objectVal) {
|
|
|
|
size_t size = 0;
|
|
|
|
for (auto keyValuePair : objectVal) {
|
|
|
|
(void)keyValuePair;
|
|
|
|
++size;
|
|
|
|
}
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Func>
|
|
|
|
void InitJSONDocument(std::unique_ptr<char[]>* data,
|
|
|
|
fbson::FbsonValue** value,
|
|
|
|
Func f) {
|
|
|
|
// TODO(stash): maybe add function to FbsonDocument to avoid creating array?
|
|
|
|
fbson::FbsonWriter writer;
|
2015-02-27 00:56:39 +00:00
|
|
|
bool res __attribute__((unused)) = writer.writeStartArray();
|
2015-02-26 21:04:14 +00:00
|
|
|
assert(res);
|
|
|
|
uint32_t bytesWritten __attribute__((unused)) = f(writer);
|
|
|
|
assert(bytesWritten != 0);
|
|
|
|
res = writer.writeEndArray();
|
|
|
|
assert(res);
|
|
|
|
char* buf = new char[writer.getOutput()->getSize()];
|
|
|
|
memcpy(buf, writer.getOutput()->getBuffer(), writer.getOutput()->getSize());
|
|
|
|
|
|
|
|
*value = ((fbson::FbsonDocument *)buf)->getValue();
|
|
|
|
assert((*value)->isArray());
|
|
|
|
assert(((fbson::ArrayVal*)*value)->numElem() == 1);
|
|
|
|
*value = ((fbson::ArrayVal*)*value)->get(0);
|
|
|
|
data->reset(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
void InitString(std::unique_ptr<char[]>* data,
|
|
|
|
fbson::FbsonValue** value,
|
|
|
|
const std::string& s) {
|
|
|
|
InitJSONDocument(data, value, std::bind(
|
|
|
|
[](fbson::FbsonWriter& writer, const std::string& str) -> uint32_t {
|
2015-02-27 00:56:39 +00:00
|
|
|
bool res __attribute__((unused)) = writer.writeStartString();
|
2015-02-26 21:04:14 +00:00
|
|
|
assert(res);
|
|
|
|
auto bytesWritten = writer.writeString(str.c_str(),
|
|
|
|
static_cast<uint32_t>(str.length()));
|
|
|
|
res = writer.writeEndString();
|
|
|
|
assert(res);
|
|
|
|
// If the string is empty, then bytesWritten == 0, and assert in
|
|
|
|
// InitJsonDocument will fail.
|
|
|
|
return bytesWritten + static_cast<uint32_t>(str.empty());
|
|
|
|
},
|
|
|
|
_1, s));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsNumeric(fbson::FbsonValue* value) {
|
|
|
|
return value->isInt8() || value->isInt16() ||
|
|
|
|
value->isInt32() || value->isInt64();
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t GetInt64ValFromFbsonNumericType(fbson::FbsonValue* value) {
|
|
|
|
switch (value->type()) {
|
|
|
|
case fbson::FbsonType::T_Int8:
|
|
|
|
return reinterpret_cast<fbson::Int8Val*>(value)->val();
|
|
|
|
case fbson::FbsonType::T_Int16:
|
|
|
|
return reinterpret_cast<fbson::Int16Val*>(value)->val();
|
|
|
|
case fbson::FbsonType::T_Int32:
|
|
|
|
return reinterpret_cast<fbson::Int32Val*>(value)->val();
|
|
|
|
case fbson::FbsonType::T_Int64:
|
|
|
|
return reinterpret_cast<fbson::Int64Val*>(value)->val();
|
|
|
|
default:
|
|
|
|
assert(false);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsComparable(fbson::FbsonValue* left, fbson::FbsonValue* right) {
|
|
|
|
if (left->type() == right->type()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (IsNumeric(left) && IsNumeric(right)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CreateArray(std::unique_ptr<char[]>* data, fbson::FbsonValue** value) {
|
|
|
|
fbson::FbsonWriter writer;
|
2015-02-27 00:56:39 +00:00
|
|
|
bool res __attribute__((unused)) = writer.writeStartArray();
|
2015-02-26 21:04:14 +00:00
|
|
|
assert(res);
|
|
|
|
res = writer.writeEndArray();
|
|
|
|
assert(res);
|
|
|
|
data->reset(new char[writer.getOutput()->getSize()]);
|
|
|
|
memcpy(data->get(),
|
|
|
|
writer.getOutput()->getBuffer(),
|
|
|
|
writer.getOutput()->getSize());
|
|
|
|
*value = reinterpret_cast<fbson::FbsonDocument*>(data->get())->getValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CreateObject(std::unique_ptr<char[]>* data, fbson::FbsonValue** value) {
|
|
|
|
fbson::FbsonWriter writer;
|
2015-02-27 00:56:39 +00:00
|
|
|
bool res __attribute__((unused)) = writer.writeStartObject();
|
2015-02-26 21:04:14 +00:00
|
|
|
assert(res);
|
|
|
|
res = writer.writeEndObject();
|
|
|
|
assert(res);
|
|
|
|
data->reset(new char[writer.getOutput()->getSize()]);
|
|
|
|
memcpy(data->get(),
|
|
|
|
writer.getOutput()->getBuffer(),
|
|
|
|
writer.getOutput()->getSize());
|
|
|
|
*value = reinterpret_cast<fbson::FbsonDocument*>(data->get())->getValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2014-06-20 09:14:14 +00:00
|
|
|
namespace rocksdb {
|
|
|
|
|
2015-02-26 21:04:14 +00:00
|
|
|
|
|
|
|
// TODO(stash): find smth easier
|
|
|
|
JSONDocument::JSONDocument() {
|
|
|
|
InitJSONDocument(&data_,
|
|
|
|
&value_,
|
|
|
|
std::bind(&fbson::FbsonWriter::writeNull, _1));
|
|
|
|
}
|
|
|
|
|
|
|
|
JSONDocument::JSONDocument(bool b) {
|
|
|
|
InitJSONDocument(&data_,
|
|
|
|
&value_,
|
|
|
|
std::bind(&fbson::FbsonWriter::writeBool, _1, b));
|
|
|
|
}
|
|
|
|
|
|
|
|
JSONDocument::JSONDocument(double d) {
|
|
|
|
InitJSONDocument(&data_,
|
|
|
|
&value_,
|
|
|
|
std::bind(&fbson::FbsonWriter::writeDouble, _1, d));
|
|
|
|
}
|
|
|
|
|
|
|
|
JSONDocument::JSONDocument(int8_t i) {
|
|
|
|
InitJSONDocument(&data_,
|
|
|
|
&value_,
|
|
|
|
std::bind(&fbson::FbsonWriter::writeInt8, _1, i));
|
|
|
|
}
|
|
|
|
|
|
|
|
JSONDocument::JSONDocument(int16_t i) {
|
|
|
|
InitJSONDocument(&data_,
|
|
|
|
&value_,
|
|
|
|
std::bind(&fbson::FbsonWriter::writeInt16, _1, i));
|
|
|
|
}
|
|
|
|
|
|
|
|
JSONDocument::JSONDocument(int32_t i) {
|
|
|
|
InitJSONDocument(&data_,
|
|
|
|
&value_,
|
|
|
|
std::bind(&fbson::FbsonWriter::writeInt32, _1, i));
|
|
|
|
}
|
|
|
|
|
|
|
|
JSONDocument::JSONDocument(int64_t i) {
|
|
|
|
InitJSONDocument(&data_,
|
|
|
|
&value_,
|
|
|
|
std::bind(&fbson::FbsonWriter::writeInt64, _1, i));
|
2014-06-20 09:14:14 +00:00
|
|
|
}
|
2015-02-26 21:04:14 +00:00
|
|
|
|
|
|
|
JSONDocument::JSONDocument(const std::string& s) {
|
|
|
|
InitString(&data_, &value_, s);
|
|
|
|
}
|
|
|
|
|
|
|
|
JSONDocument::JSONDocument(const char* s) : JSONDocument(std::string(s)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void JSONDocument::InitFromValue(const fbson::FbsonValue* val) {
|
|
|
|
data_.reset(new char[val->numPackedBytes()]);
|
|
|
|
memcpy(data_.get(), val, val->numPackedBytes());
|
|
|
|
value_ = reinterpret_cast<fbson::FbsonValue*>(data_.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Private constructor
|
|
|
|
JSONDocument::JSONDocument(fbson::FbsonValue* val, bool makeCopy) {
|
|
|
|
if (makeCopy) {
|
|
|
|
InitFromValue(val);
|
|
|
|
} else {
|
|
|
|
value_ = val;
|
|
|
|
}
|
2014-06-20 09:14:14 +00:00
|
|
|
}
|
2015-02-26 21:04:14 +00:00
|
|
|
|
|
|
|
JSONDocument::JSONDocument(Type _type) {
|
2014-06-20 09:14:14 +00:00
|
|
|
// TODO(icanadi) make all of this better by using templates
|
2015-02-26 21:04:14 +00:00
|
|
|
switch (_type) {
|
2014-06-20 09:14:14 +00:00
|
|
|
case kNull:
|
2015-02-26 21:04:14 +00:00
|
|
|
InitJSONDocument(&data_, &value_,
|
|
|
|
std::bind(&fbson::FbsonWriter::writeNull, _1));
|
2014-06-20 09:14:14 +00:00
|
|
|
break;
|
|
|
|
case kObject:
|
2015-02-26 21:04:14 +00:00
|
|
|
CreateObject(&data_, &value_);
|
2014-06-20 09:14:14 +00:00
|
|
|
break;
|
|
|
|
case kBool:
|
2015-02-26 21:04:14 +00:00
|
|
|
InitJSONDocument(&data_, &value_,
|
|
|
|
std::bind(&fbson::FbsonWriter::writeBool, _1, false));
|
2014-06-20 09:14:14 +00:00
|
|
|
break;
|
|
|
|
case kDouble:
|
2015-02-26 21:04:14 +00:00
|
|
|
InitJSONDocument(&data_, &value_,
|
|
|
|
std::bind(&fbson::FbsonWriter::writeDouble, _1, 0.));
|
2014-06-20 09:14:14 +00:00
|
|
|
break;
|
|
|
|
case kArray:
|
2015-02-26 21:04:14 +00:00
|
|
|
CreateArray(&data_, &value_);
|
2014-06-20 09:14:14 +00:00
|
|
|
break;
|
|
|
|
case kInt64:
|
2015-02-26 21:04:14 +00:00
|
|
|
InitJSONDocument(&data_, &value_,
|
|
|
|
std::bind(&fbson::FbsonWriter::writeInt64, _1, 0));
|
2014-06-20 09:14:14 +00:00
|
|
|
break;
|
|
|
|
case kString:
|
2015-02-26 21:04:14 +00:00
|
|
|
InitString(&data_, &value_, "");
|
2014-06-20 09:14:14 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-26 21:04:14 +00:00
|
|
|
JSONDocument::JSONDocument(const JSONDocument& jsonDocument) {
|
|
|
|
if (jsonDocument.IsOwner()) {
|
|
|
|
InitFromValue(jsonDocument.value_);
|
|
|
|
} else {
|
|
|
|
value_ = jsonDocument.value_;
|
2014-06-20 09:14:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-26 21:04:14 +00:00
|
|
|
JSONDocument::JSONDocument(JSONDocument&& jsonDocument) {
|
|
|
|
value_ = jsonDocument.value_;
|
|
|
|
data_.swap(jsonDocument.data_);
|
2014-06-20 09:14:14 +00:00
|
|
|
}
|
|
|
|
|
2015-02-26 21:04:14 +00:00
|
|
|
JSONDocument& JSONDocument::operator=(JSONDocument jsonDocument) {
|
|
|
|
value_ = jsonDocument.value_;
|
|
|
|
data_.swap(jsonDocument.data_);
|
|
|
|
return *this;
|
2015-02-18 21:14:53 +00:00
|
|
|
}
|
2014-06-20 09:14:14 +00:00
|
|
|
|
2015-02-26 21:04:14 +00:00
|
|
|
JSONDocument::Type JSONDocument::type() const {
|
|
|
|
switch (value_->type()) {
|
|
|
|
case fbson::FbsonType::T_Null:
|
|
|
|
return JSONDocument::kNull;
|
|
|
|
|
|
|
|
case fbson::FbsonType::T_True:
|
|
|
|
case fbson::FbsonType::T_False:
|
|
|
|
return JSONDocument::kBool;
|
|
|
|
|
|
|
|
case fbson::FbsonType::T_Int8:
|
|
|
|
case fbson::FbsonType::T_Int16:
|
|
|
|
case fbson::FbsonType::T_Int32:
|
|
|
|
case fbson::FbsonType::T_Int64:
|
|
|
|
return JSONDocument::kInt64;
|
|
|
|
|
|
|
|
case fbson::FbsonType::T_Double:
|
|
|
|
return JSONDocument::kDouble;
|
|
|
|
|
|
|
|
case fbson::FbsonType::T_String:
|
|
|
|
return JSONDocument::kString;
|
|
|
|
|
|
|
|
case fbson::FbsonType::T_Object:
|
|
|
|
return JSONDocument::kObject;
|
2014-06-20 09:14:14 +00:00
|
|
|
|
2015-02-26 21:04:14 +00:00
|
|
|
case fbson::FbsonType::T_Array:
|
|
|
|
return JSONDocument::kArray;
|
|
|
|
|
|
|
|
case fbson::FbsonType::T_Binary:
|
|
|
|
default:
|
|
|
|
assert(false);
|
|
|
|
}
|
|
|
|
return JSONDocument::kNull;
|
2015-02-18 21:14:53 +00:00
|
|
|
}
|
|
|
|
|
2015-02-26 21:04:14 +00:00
|
|
|
bool JSONDocument::Contains(const std::string& key) const {
|
|
|
|
assert(IsObject());
|
|
|
|
auto objectVal = reinterpret_cast<fbson::ObjectVal*>(value_);
|
|
|
|
return objectVal->find(key.c_str()) != nullptr;
|
2014-06-20 09:14:14 +00:00
|
|
|
}
|
|
|
|
|
2015-02-26 21:04:14 +00:00
|
|
|
JSONDocument JSONDocument::operator[](const std::string& key) const {
|
|
|
|
assert(IsObject());
|
|
|
|
auto objectVal = reinterpret_cast<fbson::ObjectVal*>(value_);
|
|
|
|
auto foundValue = objectVal->find(key.c_str());
|
|
|
|
assert(foundValue != nullptr);
|
|
|
|
// No need to save paths in const objects
|
|
|
|
JSONDocument ans(foundValue, false);
|
2015-12-23 20:10:00 +00:00
|
|
|
return ans;
|
2014-06-20 09:14:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t JSONDocument::Count() const {
|
2015-02-26 21:04:14 +00:00
|
|
|
assert(IsObject() || IsArray());
|
|
|
|
if (IsObject()) {
|
|
|
|
// TODO(stash): add to fbson?
|
|
|
|
const fbson::ObjectVal& objectVal =
|
|
|
|
*reinterpret_cast<fbson::ObjectVal*>(value_);
|
|
|
|
return ObjectNumElem(objectVal);
|
|
|
|
} else if (IsArray()) {
|
|
|
|
auto arrayVal = reinterpret_cast<fbson::ArrayVal*>(value_);
|
|
|
|
return arrayVal->numElem();
|
2014-06-20 09:14:14 +00:00
|
|
|
}
|
|
|
|
assert(false);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-02-26 21:04:14 +00:00
|
|
|
JSONDocument JSONDocument::operator[](size_t i) const {
|
|
|
|
assert(IsArray());
|
|
|
|
auto arrayVal = reinterpret_cast<fbson::ArrayVal*>(value_);
|
|
|
|
auto foundValue = arrayVal->get(static_cast<int>(i));
|
|
|
|
JSONDocument ans(foundValue, false);
|
2015-12-23 20:10:00 +00:00
|
|
|
return ans;
|
2014-06-20 09:14:14 +00:00
|
|
|
}
|
|
|
|
|
2015-02-26 21:04:14 +00:00
|
|
|
bool JSONDocument::IsNull() const {
|
|
|
|
return value_->isNull();
|
2014-06-20 09:14:14 +00:00
|
|
|
}
|
|
|
|
|
2015-02-26 21:04:14 +00:00
|
|
|
bool JSONDocument::IsArray() const {
|
|
|
|
return value_->isArray();
|
2014-06-20 09:14:14 +00:00
|
|
|
}
|
|
|
|
|
2015-02-26 21:04:14 +00:00
|
|
|
bool JSONDocument::IsBool() const {
|
|
|
|
return value_->isTrue() || value_->isFalse();
|
2015-02-18 20:58:56 +00:00
|
|
|
}
|
|
|
|
|
2015-02-26 21:04:14 +00:00
|
|
|
bool JSONDocument::IsDouble() const {
|
|
|
|
return value_->isDouble();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool JSONDocument::IsInt64() const {
|
|
|
|
return value_->isInt8() || value_->isInt16() ||
|
|
|
|
value_->isInt32() || value_->isInt64();
|
2014-06-20 09:14:14 +00:00
|
|
|
}
|
|
|
|
|
2015-02-26 21:04:14 +00:00
|
|
|
bool JSONDocument::IsObject() const {
|
|
|
|
return value_->isObject();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool JSONDocument::IsString() const {
|
|
|
|
return value_->isString();
|
|
|
|
}
|
2014-06-20 09:14:14 +00:00
|
|
|
|
|
|
|
bool JSONDocument::GetBool() const {
|
|
|
|
assert(IsBool());
|
2015-02-26 21:04:14 +00:00
|
|
|
return value_->isTrue();
|
2014-06-20 09:14:14 +00:00
|
|
|
}
|
2015-02-26 21:04:14 +00:00
|
|
|
|
2014-06-20 09:14:14 +00:00
|
|
|
double JSONDocument::GetDouble() const {
|
|
|
|
assert(IsDouble());
|
2015-02-26 21:04:14 +00:00
|
|
|
return ((fbson::DoubleVal*)value_)->val();
|
2014-06-20 09:14:14 +00:00
|
|
|
}
|
2015-02-26 21:04:14 +00:00
|
|
|
|
2014-06-20 09:14:14 +00:00
|
|
|
int64_t JSONDocument::GetInt64() const {
|
|
|
|
assert(IsInt64());
|
2015-02-26 21:04:14 +00:00
|
|
|
return GetInt64ValFromFbsonNumericType(value_);
|
2014-06-20 09:14:14 +00:00
|
|
|
}
|
2015-02-26 21:04:14 +00:00
|
|
|
|
|
|
|
std::string JSONDocument::GetString() const {
|
2014-06-20 09:14:14 +00:00
|
|
|
assert(IsString());
|
2015-02-26 21:04:14 +00:00
|
|
|
fbson::StringVal* stringVal = (fbson::StringVal*)value_;
|
|
|
|
return std::string(stringVal->getBlob(), stringVal->getBlobLen());
|
2015-02-18 20:58:56 +00:00
|
|
|
}
|
|
|
|
|
2015-02-26 21:04:14 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
// FbsonValue can be int8, int16, int32, int64
|
|
|
|
bool CompareNumeric(fbson::FbsonValue* left, fbson::FbsonValue* right) {
|
|
|
|
assert(IsNumeric(left) && IsNumeric(right));
|
|
|
|
return GetInt64ValFromFbsonNumericType(left) ==
|
|
|
|
GetInt64ValFromFbsonNumericType(right);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CompareSimpleTypes(fbson::FbsonValue* left, fbson::FbsonValue* right) {
|
|
|
|
if (IsNumeric(left)) {
|
|
|
|
return CompareNumeric(left, right);
|
|
|
|
}
|
|
|
|
if (left->numPackedBytes() != right->numPackedBytes()) {
|
2015-02-18 20:58:56 +00:00
|
|
|
return false;
|
|
|
|
}
|
2015-02-26 21:04:14 +00:00
|
|
|
return memcmp(left, right, left->numPackedBytes()) == 0;
|
|
|
|
}
|
|
|
|
|
2015-02-27 00:56:39 +00:00
|
|
|
bool CompareFbsonValue(fbson::FbsonValue* left, fbson::FbsonValue* right) {
|
2015-02-26 21:04:14 +00:00
|
|
|
if (!IsComparable(left, right)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (left->type()) {
|
|
|
|
case fbson::FbsonType::T_True:
|
|
|
|
case fbson::FbsonType::T_False:
|
|
|
|
case fbson::FbsonType::T_Null:
|
|
|
|
return true;
|
|
|
|
case fbson::FbsonType::T_Int8:
|
|
|
|
case fbson::FbsonType::T_Int16:
|
|
|
|
case fbson::FbsonType::T_Int32:
|
|
|
|
case fbson::FbsonType::T_Int64:
|
|
|
|
return CompareNumeric(left, right);
|
|
|
|
case fbson::FbsonType::T_String:
|
|
|
|
case fbson::FbsonType::T_Double:
|
|
|
|
return CompareSimpleTypes(left, right);
|
|
|
|
case fbson::FbsonType::T_Object:
|
|
|
|
{
|
|
|
|
auto leftObject = reinterpret_cast<fbson::ObjectVal*>(left);
|
|
|
|
auto rightObject = reinterpret_cast<fbson::ObjectVal*>(right);
|
|
|
|
if (ObjectNumElem(*leftObject) != ObjectNumElem(*rightObject)) {
|
2014-06-20 09:14:14 +00:00
|
|
|
return false;
|
|
|
|
}
|
2015-02-26 21:04:14 +00:00
|
|
|
for (auto && keyValue : *leftObject) {
|
|
|
|
std::string str(keyValue.getKeyStr(), keyValue.klen());
|
|
|
|
if (rightObject->find(str.c_str()) == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-02-27 00:56:39 +00:00
|
|
|
if (!CompareFbsonValue(keyValue.value(),
|
|
|
|
rightObject->find(str.c_str()))) {
|
2014-06-20 09:14:14 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
2015-02-26 21:04:14 +00:00
|
|
|
}
|
|
|
|
case fbson::FbsonType::T_Array:
|
|
|
|
{
|
|
|
|
auto leftArr = reinterpret_cast<fbson::ArrayVal*>(left);
|
|
|
|
auto rightArr = reinterpret_cast<fbson::ArrayVal*>(right);
|
|
|
|
if (leftArr->numElem() != rightArr->numElem()) {
|
2014-06-20 09:14:14 +00:00
|
|
|
return false;
|
|
|
|
}
|
2015-02-26 21:04:14 +00:00
|
|
|
for (int i = 0; i < static_cast<int>(leftArr->numElem()); ++i) {
|
2015-02-27 00:56:39 +00:00
|
|
|
if (!CompareFbsonValue(leftArr->get(i), rightArr->get(i))) {
|
2014-06-20 09:14:14 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
2015-02-26 21:04:14 +00:00
|
|
|
}
|
2014-07-10 16:31:42 +00:00
|
|
|
default:
|
|
|
|
assert(false);
|
|
|
|
}
|
2015-02-18 21:14:53 +00:00
|
|
|
return false;
|
2015-02-18 20:58:56 +00:00
|
|
|
}
|
2014-06-20 09:14:14 +00:00
|
|
|
|
2015-02-26 21:04:14 +00:00
|
|
|
} // namespace
|
2014-06-20 09:14:14 +00:00
|
|
|
|
2015-02-26 21:04:14 +00:00
|
|
|
bool JSONDocument::operator==(const JSONDocument& rhs) const {
|
2015-02-27 00:56:39 +00:00
|
|
|
return CompareFbsonValue(value_, rhs.value_);
|
2014-06-20 09:14:14 +00:00
|
|
|
}
|
|
|
|
|
2015-02-26 21:04:14 +00:00
|
|
|
bool JSONDocument::operator!=(const JSONDocument& rhs) const {
|
|
|
|
return !(*this == rhs);
|
|
|
|
}
|
2015-02-18 21:14:53 +00:00
|
|
|
|
2015-02-26 21:04:14 +00:00
|
|
|
JSONDocument JSONDocument::Copy() const {
|
|
|
|
return JSONDocument(value_, true);
|
|
|
|
}
|
2015-02-18 21:14:53 +00:00
|
|
|
|
2015-02-26 21:04:14 +00:00
|
|
|
bool JSONDocument::IsOwner() const {
|
|
|
|
return data_.get() != nullptr;
|
|
|
|
}
|
2015-02-18 21:14:53 +00:00
|
|
|
|
2015-02-26 21:04:14 +00:00
|
|
|
std::string JSONDocument::DebugString() const {
|
|
|
|
fbson::FbsonToJson fbsonToJson;
|
|
|
|
return fbsonToJson.json(value_);
|
|
|
|
}
|
2014-06-20 09:14:14 +00:00
|
|
|
|
2015-02-26 21:04:14 +00:00
|
|
|
JSONDocument::ItemsIteratorGenerator JSONDocument::Items() const {
|
|
|
|
assert(IsObject());
|
2015-07-01 23:13:49 +00:00
|
|
|
return ItemsIteratorGenerator(*(reinterpret_cast<fbson::ObjectVal*>(value_)));
|
2015-02-18 21:14:53 +00:00
|
|
|
}
|
2014-06-20 09:14:14 +00:00
|
|
|
|
2015-02-26 21:04:14 +00:00
|
|
|
// TODO(icanadi) (perf) allocate objects with arena
|
|
|
|
JSONDocument* JSONDocument::ParseJSON(const char* json) {
|
|
|
|
fbson::FbsonJsonParser parser;
|
|
|
|
if (!parser.parse(json)) {
|
|
|
|
return nullptr;
|
2014-06-20 09:14:14 +00:00
|
|
|
}
|
|
|
|
|
2015-02-26 21:04:14 +00:00
|
|
|
auto fbsonVal = fbson::FbsonDocument::createValue(
|
|
|
|
parser.getWriter().getOutput()->getBuffer(),
|
|
|
|
static_cast<uint32_t>(parser.getWriter().getOutput()->getSize()));
|
2014-06-20 09:14:14 +00:00
|
|
|
|
2015-02-26 21:04:14 +00:00
|
|
|
if (fbsonVal == nullptr) {
|
|
|
|
return nullptr;
|
2015-02-18 21:14:53 +00:00
|
|
|
}
|
|
|
|
|
2015-02-26 21:04:14 +00:00
|
|
|
return new JSONDocument(fbsonVal, true);
|
2015-02-18 21:14:53 +00:00
|
|
|
}
|
|
|
|
|
2014-06-20 09:14:14 +00:00
|
|
|
void JSONDocument::Serialize(std::string* dst) const {
|
|
|
|
// first byte is reserved for header
|
|
|
|
// currently, header is only version number. that will help us provide
|
|
|
|
// backwards compatility. we might also store more information here if
|
|
|
|
// necessary
|
|
|
|
dst->push_back(kSerializationFormatVersion);
|
2015-02-26 21:04:14 +00:00
|
|
|
dst->push_back(FBSON_VER);
|
|
|
|
dst->append(reinterpret_cast<char*>(value_), value_->numPackedBytes());
|
2014-06-20 09:14:14 +00:00
|
|
|
}
|
|
|
|
|
2015-02-26 21:04:14 +00:00
|
|
|
const char JSONDocument::kSerializationFormatVersion = 2;
|
2014-06-20 09:14:14 +00:00
|
|
|
|
|
|
|
JSONDocument* JSONDocument::Deserialize(const Slice& src) {
|
|
|
|
Slice input(src);
|
|
|
|
if (src.size() == 0) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
char header = input[0];
|
2015-02-26 21:04:14 +00:00
|
|
|
if (header == 1) {
|
|
|
|
assert(false);
|
2014-06-20 09:14:14 +00:00
|
|
|
}
|
|
|
|
input.remove_prefix(1);
|
2015-02-26 21:04:14 +00:00
|
|
|
auto value = fbson::FbsonDocument::createValue(input.data(),
|
|
|
|
static_cast<uint32_t>(input.size()));
|
|
|
|
if (value == nullptr) {
|
2014-06-20 09:14:14 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
2015-02-26 21:04:14 +00:00
|
|
|
|
|
|
|
return new JSONDocument(value, true);
|
2014-06-20 09:14:14 +00:00
|
|
|
}
|
|
|
|
|
2015-02-26 21:04:14 +00:00
|
|
|
class JSONDocument::const_item_iterator::Impl {
|
|
|
|
public:
|
|
|
|
typedef fbson::ObjectVal::const_iterator It;
|
|
|
|
|
|
|
|
explicit Impl(It it) : it_(it) {}
|
|
|
|
|
|
|
|
const char* getKeyStr() const {
|
|
|
|
return it_->getKeyStr();
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t klen() const {
|
|
|
|
return it_->klen();
|
|
|
|
}
|
|
|
|
|
|
|
|
It& operator++() {
|
|
|
|
return ++it_;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(const Impl& other) {
|
|
|
|
return it_ != other.it_;
|
2015-02-18 20:58:56 +00:00
|
|
|
}
|
2015-02-26 21:04:14 +00:00
|
|
|
|
|
|
|
fbson::FbsonValue* value() const {
|
|
|
|
return it_->value();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
It it_;
|
|
|
|
};
|
|
|
|
|
|
|
|
JSONDocument::const_item_iterator::const_item_iterator(Impl* impl)
|
|
|
|
: it_(impl) {}
|
|
|
|
|
|
|
|
JSONDocument::const_item_iterator::const_item_iterator(const_item_iterator&& a)
|
|
|
|
: it_(std::move(a.it_)) {}
|
|
|
|
|
|
|
|
JSONDocument::const_item_iterator&
|
|
|
|
JSONDocument::const_item_iterator::operator++() {
|
|
|
|
++(*it_);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool JSONDocument::const_item_iterator::operator!=(
|
|
|
|
const const_item_iterator& other) {
|
|
|
|
return *it_ != *(other.it_);
|
|
|
|
}
|
|
|
|
|
|
|
|
JSONDocument::const_item_iterator::~const_item_iterator() {
|
|
|
|
}
|
|
|
|
|
|
|
|
JSONDocument::const_item_iterator::value_type
|
|
|
|
JSONDocument::const_item_iterator::operator*() {
|
2015-10-14 23:05:55 +00:00
|
|
|
return JSONDocument::const_item_iterator::value_type(std::string(it_->getKeyStr(), it_->klen()),
|
|
|
|
JSONDocument(it_->value(), false));
|
2015-02-26 21:04:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
JSONDocument::ItemsIteratorGenerator::ItemsIteratorGenerator(
|
|
|
|
const fbson::ObjectVal& object)
|
|
|
|
: object_(object) {}
|
|
|
|
|
|
|
|
JSONDocument::const_item_iterator
|
|
|
|
JSONDocument::ItemsIteratorGenerator::begin() const {
|
|
|
|
return const_item_iterator(new const_item_iterator::Impl(object_.begin()));
|
|
|
|
}
|
|
|
|
|
|
|
|
JSONDocument::const_item_iterator
|
|
|
|
JSONDocument::ItemsIteratorGenerator::end() const {
|
|
|
|
return const_item_iterator(new const_item_iterator::Impl(object_.end()));
|
2014-06-20 09:14:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace rocksdb
|
|
|
|
#endif // ROCKSDB_LITE
|