YugabyteDB (2.13.1.0-b60, 21121d69985fbf76aa6958d8f04a9bfa936293b5)

Coverage Report

Created: 2022-03-22 16:43

/Users/deen/code/yugabyte-db/src/yb/rocksdb/utilities/document/json_document_builder.cc
Line
Count
Source (jump to first uncovered line)
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
// The following only applies to changes made to this file as part of YugaByte development.
7
//
8
// Portions Copyright (c) YugaByte, Inc.
9
//
10
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
11
// in compliance with the License.  You may obtain a copy of the License at
12
//
13
// http://www.apache.org/licenses/LICENSE-2.0
14
//
15
// Unless required by applicable law or agreed to in writing, software distributed under the License
16
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
17
// or implied.  See the License for the specific language governing permissions and limitations
18
// under the License.
19
//
20
21
#ifndef ROCKSDB_LITE
22
#include <assert.h>
23
#include <limits>
24
#include <stdint.h>
25
#include "yb/rocksdb/utilities/json_document.h"
26
#include "yb/rocksdb/third-party/fbson/FbsonWriter.h"
27
28
namespace rocksdb {
29
JSONDocumentBuilder::JSONDocumentBuilder()
30
11
: writer_(new fbson::FbsonWriter()) {
31
11
}
32
33
JSONDocumentBuilder::JSONDocumentBuilder(fbson::FbsonOutStream* out)
34
0
: writer_(new fbson::FbsonWriter(*out)) {
35
0
}
36
37
10
void JSONDocumentBuilder::Reset() {
38
10
  writer_->reset();
39
10
}
40
41
2
bool JSONDocumentBuilder::WriteStartArray() {
42
2
  return writer_->writeStartArray();
43
2
}
44
45
2
bool JSONDocumentBuilder::WriteEndArray() {
46
2
  return writer_->writeEndArray();
47
2
}
48
49
21
bool JSONDocumentBuilder::WriteStartObject() {
50
21
  return writer_->writeStartObject();
51
21
}
52
53
21
bool JSONDocumentBuilder::WriteEndObject() {
54
21
  return writer_->writeEndObject();
55
21
}
56
57
bool JSONDocumentBuilder::WriteKeyValue(const std::string& key,
58
55
                                        const JSONDocument& value) {
59
55
  assert(key.size() <= std::numeric_limits<uint8_t>::max());
60
0
  size_t bytesWritten = writer_->writeKey(key.c_str(),
61
55
    static_cast<uint8_t>(key.size()));
62
55
  if (bytesWritten == 0) {
63
0
    return false;
64
0
  }
65
55
  return WriteJSONDocument(value);
66
55
}
67
68
59
bool JSONDocumentBuilder::WriteJSONDocument(const JSONDocument& value) {
69
59
  switch (value.type()) {
70
0
    case JSONDocument::kNull:
71
0
      return writer_->writeNull() != 0;
72
40
    case JSONDocument::kInt64:
73
40
      return writer_->writeInt64(value.GetInt64());
74
6
    case JSONDocument::kDouble:
75
6
      return writer_->writeDouble(value.GetDouble());
76
0
    case JSONDocument::kBool:
77
0
      return writer_->writeBool(value.GetBool());
78
13
    case JSONDocument::kString:
79
13
    {
80
13
      bool res = writer_->writeStartString();
81
13
      if (!res) {
82
0
        return false;
83
0
      }
84
13
      const std::string& str = value.GetString();
85
13
      res = writer_->writeString(str.c_str(),
86
13
                  static_cast<uint32_t>(str.size()));
87
13
      if (!res) {
88
0
        return false;
89
0
      }
90
13
      return writer_->writeEndString();
91
13
    }
92
0
    case JSONDocument::kArray:
93
0
    {
94
0
      bool res = WriteStartArray();
95
0
      if (!res) {
96
0
        return false;
97
0
      }
98
0
      for (size_t i = 0; i < value.Count(); ++i) {
99
0
        res = WriteJSONDocument(value[i]);
100
0
        if (!res) {
101
0
          return false;
102
0
        }
103
0
      }
104
0
      return WriteEndArray();
105
0
    }
106
0
    case JSONDocument::kObject:
107
0
    {
108
0
      bool res = WriteStartObject();
109
0
      if (!res) {
110
0
        return false;
111
0
      }
112
0
      for (auto keyValue : value.Items()) {
113
0
        WriteKeyValue(keyValue.first, keyValue.second);
114
0
      }
115
0
      return WriteEndObject();
116
0
    }
117
0
    default:
118
0
      assert(false);
119
59
  }
120
0
  return false;
121
59
}
122
123
21
JSONDocument JSONDocumentBuilder::GetJSONDocument() {
124
21
  fbson::FbsonValue* value =
125
21
      fbson::FbsonDocument::createValue(writer_->getOutput()->getBuffer(),
126
21
                       static_cast<uint32_t>(writer_->getOutput()->getSize()));
127
21
  return JSONDocument(value, true);
128
21
}
129
130
11
JSONDocumentBuilder::~JSONDocumentBuilder() {
131
11
}
132
133
}  // namespace rocksdb
134
135
#endif  // ROCKSDB_LITE