Coverage Report

Created: 2024-11-18 11:49

/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
304
#define MAX_INT_DIGITS 11
50
30
#define MAX_INT64_DIGITS 20
51
0
#define MAX_INT128_DIGITS 40
52
114
#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
1.19k
    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
1.19k
        char* pch(const_cast<char*>(str));
63
1.19k
        setg(pch, pch, pch + len);
64
1.19k
    }
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
2.05k
            : std::ostream(nullptr), head_(nullptr), size_(0), capacity_(capacity), alloc_(true) {
77
2.05k
        if (capacity_ == 0) {
78
0
            capacity_ = 1024;
79
0
        }
80
81
2.05k
        head_ = (char*)malloc(capacity_);
82
2.05k
    }
Unexecuted instantiation: _ZN5doris14JsonbOutStreamC2Ej
_ZN5doris14JsonbOutStreamC1Ej
Line
Count
Source
76
2.05k
            : std::ostream(nullptr), head_(nullptr), size_(0), capacity_(capacity), alloc_(true) {
77
2.05k
        if (capacity_ == 0) {
78
0
            capacity_ = 1024;
79
0
        }
80
81
2.05k
        head_ = (char*)malloc(capacity_);
82
2.05k
    }
83
84
    JsonbOutStream(char* buffer, uint32_t capacity)
85
266
            : std::ostream(nullptr), head_(buffer), size_(0), capacity_(capacity), alloc_(false) {
86
266
        assert(buffer && capacity_ > 0);
87
266
    }
Unexecuted instantiation: _ZN5doris14JsonbOutStreamC2EPcj
_ZN5doris14JsonbOutStreamC1EPcj
Line
Count
Source
85
266
            : std::ostream(nullptr), head_(buffer), size_(0), capacity_(capacity), alloc_(false) {
86
266
        assert(buffer && capacity_ > 0);
87
266
    }
88
89
2.31k
    ~JsonbOutStream() {
90
2.31k
        if (alloc_) {
91
2.05k
            free(head_);
92
2.05k
        }
93
2.31k
    }
94
95
27.0k
    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
55.9k
    void write(const char* bytes, uint32_t len) {
100
55.9k
        if (len == 0) return;
101
102
55.9k
        if (size_ + len > capacity_) {
103
0
            realloc(len);
104
0
        }
105
106
55.9k
        memcpy(head_ + size_, bytes, len);
107
55.9k
        size_ += len;
108
55.9k
    }
109
110
    // write the integer to string
111
152
    void write(int i) {
112
        // snprintf automatically adds a NULL, so we need one more char
113
152
        if (size_ + MAX_INT_DIGITS + 1 > capacity_) {
114
0
            realloc(MAX_INT_DIGITS + 1);
115
0
        }
116
117
152
        int len = snprintf(head_ + size_, MAX_INT_DIGITS + 1, "%d", i);
118
152
        assert(len > 0);
119
0
        size_ += len;
120
152
    }
121
122
    // write the 64bit integer to string
123
15
    void write(int64_t l) {
124
        // snprintf automatically adds a NULL, so we need one more char
125
15
        if (size_ + MAX_INT64_DIGITS + 1 > capacity_) {
126
0
            realloc(MAX_INT64_DIGITS + 1);
127
0
        }
128
129
15
        int len = snprintf(head_ + size_, MAX_INT64_DIGITS + 1, "%" PRIi64, l);
130
15
        assert(len > 0);
131
0
        size_ += len;
132
15
    }
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_) {
137
0
            realloc(MAX_INT128_DIGITS + 1);
138
0
        }
139
140
0
        const auto result = fmt::format_to_n(head_ + size_, MAX_INT128_DIGITS, "{}", l);
141
0
        assert(result.size > 0);
142
0
        size_ += result.size;
143
0
    }
144
145
    // write the double to string
146
57
    void write(double d) {
147
        // snprintf automatically adds a NULL, so we need one more char
148
57
        if (size_ + MAX_DOUBLE_DIGITS + 1 > capacity_) {
149
0
            realloc(MAX_DOUBLE_DIGITS + 1);
150
0
        }
151
152
57
        int len = snprintf(head_ + size_, MAX_DOUBLE_DIGITS + 1, "%.15g", d);
153
57
        assert(len > 0);
154
0
        size_ += len;
155
57
    }
156
157
13.0k
    pos_type tellp() const { return size_; }
158
159
12.3k
    void seekp(pos_type pos) { size_ = (uint32_t)pos; }
160
161
2.49k
    const char* getBuffer() const { return head_; }
162
163
2.20k
    pos_type getSize() const { return tellp(); }
164
165
private:
166
0
    void realloc(uint32_t len) {
167
0
        assert(capacity_ > 0);
168
169
0
        capacity_ *= 2;
170
0
        while (capacity_ < size_ + len) {
171
0
            capacity_ *= 2;
172
0
        }
173
174
0
        if (alloc_) {
175
0
            char* new_buf = (char*)::realloc(head_, capacity_);
176
0
            assert(new_buf);
177
0
            head_ = new_buf;
178
0
        } else {
179
0
            char* new_buf = (char*)::malloc(capacity_);
180
0
            assert(new_buf);
181
0
            memcpy(new_buf, head_, size_);
182
0
            head_ = new_buf;
183
0
            alloc_ = true;
184
0
        }
185
0
    }
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