/root/doris/be/src/util/jsonb_stream.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 2014, Facebook, Inc. |
3 | | * All rights reserved. |
4 | | * |
5 | | * This source code is licensed under the BSD-style license found in the |
6 | | * LICENSE file in the root directory of this source tree. An additional grant |
7 | | * of patent rights can be found in the PATENTS file in the same directory. |
8 | | * |
9 | | */ |
10 | | |
11 | | /* |
12 | | * This header file defines JsonbInBuffer and JsonbOutStream classes. |
13 | | * |
14 | | * ** Input Buffer ** |
15 | | * JsonbInBuffer is a customer input buffer to wrap raw character buffer. Its |
16 | | * object instances are used to create std::istream objects interally. |
17 | | * |
18 | | * ** Output Stream ** |
19 | | * JsonbOutStream is a custom output stream classes, to contain the JSONB |
20 | | * serialized binary. The class is conveniently used to specialize templates of |
21 | | * JsonbParser and JsonbWriter. |
22 | | * |
23 | | * @author Tian Xia <tianx@fb.com> |
24 | | * this file is copied from |
25 | | * https://github.com/facebook/mysql-5.6/blob/fb-mysql-5.6.35/fbson/FbsonStream.h |
26 | | * and modified by Doris |
27 | | */ |
28 | | |
29 | | #ifndef JSONB_JSONBSTREAM_H |
30 | | #define JSONB_JSONBSTREAM_H |
31 | | |
32 | | #ifndef __STDC_FORMAT_MACROS |
33 | | #define __STDC_FORMAT_MACROS |
34 | | #endif |
35 | | |
36 | | #include <assert.h> |
37 | | #include <fmt/format.h> |
38 | | #include <string.h> |
39 | | |
40 | | #include <algorithm> |
41 | | #include <cinttypes> |
42 | | #include <iostream> |
43 | | |
44 | | namespace doris { |
45 | | |
46 | | using int128_t = __int128; |
47 | | |
48 | | // lengths includes sign |
49 | 2.50k | #define MAX_INT_DIGITS 11 |
50 | 1.69M | #define MAX_INT64_DIGITS 20 |
51 | 0 | #define MAX_INT128_DIGITS 40 |
52 | 1.53M | #define MAX_DOUBLE_DIGITS 23 // 1(sign)+16(significant)+1(decimal)+5(exponent) |
53 | | |
54 | | /* |
55 | | * JSONB's implementation of input buffer |
56 | | */ |
57 | | class JsonbInBuffer : public std::streambuf { |
58 | | public: |
59 | 73.9k | JsonbInBuffer(const char* str, uint32_t len) { |
60 | | // this is read buffer and the str will not be changed |
61 | | // so we use const_cast (ugly!) to remove constness |
62 | 73.9k | char* pch(const_cast<char*>(str)); |
63 | 73.9k | setg(pch, pch, pch + len); |
64 | 73.9k | } |
65 | | }; |
66 | | |
67 | | /* |
68 | | * JSONB's implementation of output stream. |
69 | | * |
70 | | * This is a wrapper of a char buffer. By default, the buffer capacity is 1024 |
71 | | * bytes. We will double the buffer if realloc is needed for writes. |
72 | | */ |
73 | | class JsonbOutStream : public std::ostream { |
74 | | public: |
75 | | explicit JsonbOutStream(uint32_t capacity = 1024) |
76 | 479k | : std::ostream(nullptr), head_(nullptr), size_(0), capacity_(capacity), alloc_(true) { |
77 | 479k | if (capacity_ == 0) { Branch (77:13): [True: 0, False: 479k]
|
78 | 0 | capacity_ = 1024; |
79 | 0 | } |
80 | | |
81 | 479k | head_ = (char*)malloc(capacity_); |
82 | 479k | } Unexecuted instantiation: _ZN5doris14JsonbOutStreamC2Ej _ZN5doris14JsonbOutStreamC1Ej Line | Count | Source | 76 | 479k | : std::ostream(nullptr), head_(nullptr), size_(0), capacity_(capacity), alloc_(true) { | 77 | 479k | if (capacity_ == 0) { Branch (77:13): [True: 0, False: 479k]
| 78 | 0 | capacity_ = 1024; | 79 | 0 | } | 80 | | | 81 | 479k | head_ = (char*)malloc(capacity_); | 82 | 479k | } |
|
83 | | |
84 | | JsonbOutStream(char* buffer, uint32_t capacity) |
85 | 78.0k | : std::ostream(nullptr), head_(buffer), size_(0), capacity_(capacity), alloc_(false) { |
86 | 78.0k | assert(buffer && capacity_ > 0); |
87 | 78.0k | } Unexecuted instantiation: _ZN5doris14JsonbOutStreamC2EPcj _ZN5doris14JsonbOutStreamC1EPcj Line | Count | Source | 85 | 78.0k | : std::ostream(nullptr), head_(buffer), size_(0), capacity_(capacity), alloc_(false) { | 86 | 78.0k | assert(buffer && capacity_ > 0); | 87 | 78.0k | } |
|
88 | | |
89 | 557k | ~JsonbOutStream() { |
90 | 557k | if (alloc_) { Branch (90:13): [True: 485k, False: 72.3k]
|
91 | 485k | free(head_); |
92 | 485k | } |
93 | 557k | } |
94 | | |
95 | 111M | void put(char c) { write(&c, 1); } |
96 | | |
97 | 0 | void write(const char* c_str) { write(c_str, (uint32_t)strlen(c_str)); } |
98 | | |
99 | 128M | void write(const char* bytes, uint32_t len) { |
100 | 128M | if (len == 0) return; Branch (100:13): [True: 237k, False: 128M]
|
101 | | |
102 | 128M | if (size_ + len > capacity_) { Branch (102:13): [True: 62.1k, False: 128M]
|
103 | 62.1k | realloc(len); |
104 | 62.1k | } |
105 | | |
106 | 128M | memcpy(head_ + size_, bytes, len); |
107 | 128M | size_ += len; |
108 | 128M | } |
109 | | |
110 | | // write the integer to string |
111 | 1.25k | void write(int i) { |
112 | | // snprintf automatically adds a NULL, so we need one more char |
113 | 1.25k | if (size_ + MAX_INT_DIGITS + 1 > capacity_) { Line | Count | Source | 49 | 1.25k | #define MAX_INT_DIGITS 11 |
Branch (113:13): [True: 0, False: 1.25k]
|
114 | 0 | realloc(MAX_INT_DIGITS + 1); Line | Count | Source | 49 | 0 | #define MAX_INT_DIGITS 11 |
|
115 | 0 | } |
116 | | |
117 | 1.25k | int len = snprintf(head_ + size_, MAX_INT_DIGITS + 1, "%d", i); Line | Count | Source | 49 | 1.25k | #define MAX_INT_DIGITS 11 |
|
118 | 1.25k | assert(len > 0); |
119 | 0 | size_ += len; |
120 | 1.25k | } |
121 | | |
122 | | // write the 64bit integer to string |
123 | 846k | void write(int64_t l) { |
124 | | // snprintf automatically adds a NULL, so we need one more char |
125 | 846k | if (size_ + MAX_INT64_DIGITS + 1 > capacity_) { Line | Count | Source | 50 | 846k | #define MAX_INT64_DIGITS 20 |
Branch (125:13): [True: 3.79k, False: 842k]
|
126 | 3.79k | realloc(MAX_INT64_DIGITS + 1); Line | Count | Source | 50 | 3.79k | #define MAX_INT64_DIGITS 20 |
|
127 | 3.79k | } |
128 | | |
129 | 846k | int len = snprintf(head_ + size_, MAX_INT64_DIGITS + 1, "%" PRIi64, l); Line | Count | Source | 50 | 846k | #define MAX_INT64_DIGITS 20 |
|
130 | 846k | assert(len > 0); |
131 | 0 | size_ += len; |
132 | 846k | } |
133 | | |
134 | 0 | void write(int128_t l) { |
135 | | // snprintf automatically adds a NULL, so we need one more char |
136 | 0 | if (size_ + MAX_INT128_DIGITS + 1 > capacity_) { Line | Count | Source | 51 | 0 | #define MAX_INT128_DIGITS 40 |
Branch (136:13): [True: 0, False: 0]
|
137 | 0 | realloc(MAX_INT128_DIGITS + 1); Line | Count | Source | 51 | 0 | #define MAX_INT128_DIGITS 40 |
|
138 | 0 | } |
139 | |
|
140 | 0 | const auto result = fmt::format_to_n(head_ + size_, MAX_INT128_DIGITS, "{}", l); Line | Count | Source | 51 | 0 | #define MAX_INT128_DIGITS 40 |
|
141 | 0 | assert(result.size > 0); |
142 | 0 | size_ += result.size; |
143 | 0 | } |
144 | | |
145 | | // write the double to string |
146 | 765k | void write(double d) { |
147 | | // snprintf automatically adds a NULL, so we need one more char |
148 | 765k | if (size_ + MAX_DOUBLE_DIGITS + 1 > capacity_) { Line | Count | Source | 52 | 765k | #define MAX_DOUBLE_DIGITS 23 // 1(sign)+16(significant)+1(decimal)+5(exponent) |
Branch (148:13): [True: 3.30k, False: 762k]
|
149 | 3.30k | realloc(MAX_DOUBLE_DIGITS + 1); Line | Count | Source | 52 | 3.30k | #define MAX_DOUBLE_DIGITS 23 // 1(sign)+16(significant)+1(decimal)+5(exponent) |
|
150 | 3.30k | } |
151 | | |
152 | 765k | int len = snprintf(head_ + size_, MAX_DOUBLE_DIGITS + 1, "%.15g", d); Line | Count | Source | 52 | 765k | #define MAX_DOUBLE_DIGITS 23 // 1(sign)+16(significant)+1(decimal)+5(exponent) |
|
153 | 765k | assert(len > 0); |
154 | 0 | size_ += len; |
155 | 765k | } |
156 | | |
157 | 9.67M | pos_type tellp() const { return size_; } |
158 | | |
159 | 9.54M | void seekp(pos_type pos) { size_ = (uint32_t)pos; } |
160 | | |
161 | 522k | const char* getBuffer() const { return head_; } |
162 | | |
163 | 444k | pos_type getSize() const { return tellp(); } |
164 | | |
165 | | private: |
166 | 69.2k | void realloc(uint32_t len) { |
167 | 69.2k | assert(capacity_ > 0); |
168 | | |
169 | 0 | capacity_ *= 2; |
170 | 75.3k | while (capacity_ < size_ + len) { Branch (170:16): [True: 6.07k, False: 69.2k]
|
171 | 6.07k | capacity_ *= 2; |
172 | 6.07k | } |
173 | | |
174 | 69.2k | if (alloc_) { Branch (174:13): [True: 63.6k, False: 5.65k]
|
175 | 63.6k | char* new_buf = (char*)::realloc(head_, capacity_); |
176 | 63.6k | assert(new_buf); |
177 | 0 | head_ = new_buf; |
178 | 63.6k | } else { |
179 | 5.65k | char* new_buf = (char*)::malloc(capacity_); |
180 | 5.65k | assert(new_buf); |
181 | 0 | memcpy(new_buf, head_, size_); |
182 | 5.65k | head_ = new_buf; |
183 | 5.65k | alloc_ = true; |
184 | 5.65k | } |
185 | 69.2k | } |
186 | | |
187 | | private: |
188 | | char* head_ = nullptr; |
189 | | uint32_t size_; |
190 | | uint32_t capacity_; |
191 | | bool alloc_; |
192 | | }; |
193 | | |
194 | | } // namespace doris |
195 | | |
196 | | #endif // JSONB_JSONBSTREAM_H |