]> git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/utilities/document/json_document_builder.cc
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / rocksdb / utilities / document / json_document_builder.cc
1 // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
2 // This source code is licensed under the BSD-style license found in the
3 // LICENSE file in the root directory of this source tree. An additional grant
4 // of patent rights can be found in the PATENTS file in the same directory.
5
6 #ifndef ROCKSDB_LITE
7 #include <assert.h>
8 #include <limits>
9 #include <stdint.h>
10 #include "rocksdb/utilities/json_document.h"
11 #include "third-party/fbson/FbsonWriter.h"
12
13 namespace rocksdb {
14 JSONDocumentBuilder::JSONDocumentBuilder()
15 : writer_(new fbson::FbsonWriter()) {
16 }
17
18 JSONDocumentBuilder::JSONDocumentBuilder(fbson::FbsonOutStream* out)
19 : writer_(new fbson::FbsonWriter(*out)) {
20 }
21
22 void JSONDocumentBuilder::Reset() {
23 writer_->reset();
24 }
25
26 bool JSONDocumentBuilder::WriteStartArray() {
27 return writer_->writeStartArray();
28 }
29
30 bool JSONDocumentBuilder::WriteEndArray() {
31 return writer_->writeEndArray();
32 }
33
34 bool JSONDocumentBuilder::WriteStartObject() {
35 return writer_->writeStartObject();
36 }
37
38 bool JSONDocumentBuilder::WriteEndObject() {
39 return writer_->writeEndObject();
40 }
41
42 bool JSONDocumentBuilder::WriteKeyValue(const std::string& key,
43 const JSONDocument& value) {
44 assert(key.size() <= std::numeric_limits<uint8_t>::max());
45 size_t bytesWritten = writer_->writeKey(key.c_str(),
46 static_cast<uint8_t>(key.size()));
47 if (bytesWritten == 0) {
48 return false;
49 }
50 return WriteJSONDocument(value);
51 }
52
53 bool JSONDocumentBuilder::WriteJSONDocument(const JSONDocument& value) {
54 switch (value.type()) {
55 case JSONDocument::kNull:
56 return writer_->writeNull() != 0;
57 case JSONDocument::kInt64:
58 return writer_->writeInt64(value.GetInt64());
59 case JSONDocument::kDouble:
60 return writer_->writeDouble(value.GetDouble());
61 case JSONDocument::kBool:
62 return writer_->writeBool(value.GetBool());
63 case JSONDocument::kString:
64 {
65 bool res = writer_->writeStartString();
66 if (!res) {
67 return false;
68 }
69 const std::string& str = value.GetString();
70 res = writer_->writeString(str.c_str(),
71 static_cast<uint32_t>(str.size()));
72 if (!res) {
73 return false;
74 }
75 return writer_->writeEndString();
76 }
77 case JSONDocument::kArray:
78 {
79 bool res = WriteStartArray();
80 if (!res) {
81 return false;
82 }
83 for (size_t i = 0; i < value.Count(); ++i) {
84 res = WriteJSONDocument(value[i]);
85 if (!res) {
86 return false;
87 }
88 }
89 return WriteEndArray();
90 }
91 case JSONDocument::kObject:
92 {
93 bool res = WriteStartObject();
94 if (!res) {
95 return false;
96 }
97 for (auto keyValue : value.Items()) {
98 WriteKeyValue(keyValue.first, keyValue.second);
99 }
100 return WriteEndObject();
101 }
102 default:
103 assert(false);
104 }
105 return false;
106 }
107
108 JSONDocument JSONDocumentBuilder::GetJSONDocument() {
109 fbson::FbsonValue* value =
110 fbson::FbsonDocument::createValue(writer_->getOutput()->getBuffer(),
111 static_cast<uint32_t>(writer_->getOutput()->getSize()));
112 return JSONDocument(value, true);
113 }
114
115 JSONDocumentBuilder::~JSONDocumentBuilder() {
116 }
117
118 } // namespace rocksdb
119
120 #endif // ROCKSDB_LITE