Coverage Report

Created: 2025-07-23 19:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/doris/be/src/util/coding.h
Line
Count
Source
1
//  Copyright (c) 2011-present, Facebook, Inc.  All rights reserved.
2
//  This source code is licensed under both the GPLv2 (found in the
3
//  COPYING file in the root directory) and Apache 2.0 License
4
//  (found in the LICENSE.Apache file in the root directory).
5
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
6
// Use of this source code is governed by a BSD-style license that can be
7
// found in the LICENSE file. See the AUTHORS file for names of contributors.
8
9
#pragma once
10
11
#include <bit>
12
#ifndef __APPLE__
13
#include <endian.h>
14
#endif
15
#include <stdint.h>
16
#include <string.h>
17
18
#include "olap/olap_common.h"
19
#include "util/slice.h"
20
21
namespace doris {
22
23
// TODO(zc): add encode big endian later when we need it
24
// use big endian when we have order requirement.
25
// little endian is more efficient when we use X86 CPU, so
26
// when we have no order needs, we prefer little endian encoding
27
1
inline void encode_fixed8(uint8_t* buf, uint8_t val) {
28
1
    *buf = val;
29
1
}
30
31
985
inline void encode_fixed16_le(uint8_t* buf, uint16_t val) {
32
985
    val = to_endian<std::endian::little>(val);
33
985
    memcpy(buf, &val, sizeof(val));
34
985
}
35
36
335k
inline void encode_fixed32_le(uint8_t* buf, uint32_t val) {
37
335k
    val = to_endian<std::endian::little>(val);
38
335k
    memcpy(buf, &val, sizeof(val));
39
335k
}
40
41
80.1k
inline void encode_fixed64_le(uint8_t* buf, uint64_t val) {
42
80.1k
    val = to_endian<std::endian::little>(val);
43
80.1k
    memcpy(buf, &val, sizeof(val));
44
80.1k
}
45
46
24.4k
inline void encode_fixed128_le(uint8_t* buf, uint128_t val) {
47
24.4k
    val = to_endian<std::endian::little>(val);
48
24.4k
    memcpy(buf, &val, sizeof(val));
49
24.4k
}
50
51
130k
inline uint8_t decode_fixed8(const uint8_t* buf) {
52
130k
    return *buf;
53
130k
}
54
55
47.5k
inline uint16_t decode_fixed16_le(const uint8_t* buf) {
56
47.5k
    uint16_t res;
57
47.5k
    memcpy(&res, buf, sizeof(res));
58
47.5k
    return to_endian<std::endian::little>(res);
59
47.5k
}
60
61
310k
inline uint32_t decode_fixed32_le(const uint8_t* buf) {
62
310k
    uint32_t res;
63
310k
    memcpy(&res, buf, sizeof(res));
64
310k
    return to_endian<std::endian::little>(res);
65
310k
}
66
67
366M
inline uint64_t decode_fixed64_le(const uint8_t* buf) {
68
366M
    uint64_t res;
69
366M
    memcpy(&res, buf, sizeof(res));
70
366M
    return to_endian<std::endian::little>(res);
71
366M
}
72
73
24.4k
inline uint128_t decode_fixed128_le(const uint8_t* buf) {
74
24.4k
    uint128_t res;
75
24.4k
    memcpy(&res, buf, sizeof(res));
76
24.4k
    return to_endian<std::endian::little>(res);
77
24.4k
}
78
79
template <typename T>
80
235k
void put_fixed32_le(T* dst, uint32_t val) {
81
235k
    uint8_t buf[sizeof(val)];
82
235k
    encode_fixed32_le(buf, val);
83
235k
    dst->append((char*)buf, sizeof(buf));
84
235k
}
_ZN5doris14put_fixed32_leINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEEvPT_j
Line
Count
Source
80
39.4k
void put_fixed32_le(T* dst, uint32_t val) {
81
39.4k
    uint8_t buf[sizeof(val)];
82
39.4k
    encode_fixed32_le(buf, val);
83
39.4k
    dst->append((char*)buf, sizeof(buf));
84
39.4k
}
_ZN5doris14put_fixed32_leINS_10faststringEEEvPT_j
Line
Count
Source
80
195k
void put_fixed32_le(T* dst, uint32_t val) {
81
195k
    uint8_t buf[sizeof(val)];
82
195k
    encode_fixed32_le(buf, val);
83
195k
    dst->append((char*)buf, sizeof(buf));
84
195k
}
85
86
template <typename T>
87
25.0k
void put_fixed64_le(T* dst, uint64_t val) {
88
25.0k
    uint8_t buf[sizeof(val)];
89
25.0k
    encode_fixed64_le(buf, val);
90
25.0k
    dst->append((char*)buf, sizeof(buf));
91
25.0k
}
_ZN5doris14put_fixed64_leINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEEvPT_m
Line
Count
Source
87
615
void put_fixed64_le(T* dst, uint64_t val) {
88
615
    uint8_t buf[sizeof(val)];
89
615
    encode_fixed64_le(buf, val);
90
615
    dst->append((char*)buf, sizeof(buf));
91
615
}
_ZN5doris14put_fixed64_leINS_10faststringEEEvPT_m
Line
Count
Source
87
24.4k
void put_fixed64_le(T* dst, uint64_t val) {
88
24.4k
    uint8_t buf[sizeof(val)];
89
24.4k
    encode_fixed64_le(buf, val);
90
24.4k
    dst->append((char*)buf, sizeof(buf));
91
24.4k
}
92
93
// Returns the length of the varint32 or varint64 encoding of "v"
94
13
inline int varint_length(uint64_t v) {
95
13
    int len = 1;
96
13
    while (v >= 128) {
97
0
        v >>= 7;
98
0
        len++;
99
0
    }
100
13
    return len;
101
13
}
102
103
template <typename T>
104
24.4k
void put_fixed128_le(T* dst, uint128_t val) {
105
24.4k
    uint8_t buf[sizeof(val)];
106
24.4k
    encode_fixed128_le(buf, val);
107
24.4k
    dst->append((char*)buf, sizeof(buf));
108
24.4k
}
109
110
extern uint8_t* encode_varint32(uint8_t* dst, uint32_t value);
111
extern uint8_t* encode_varint64(uint8_t* dst, uint64_t value);
112
113
46.8k
inline uint8_t* encode_varint64(uint8_t* dst, uint64_t v) {
114
46.8k
    static const unsigned int B = 128;
115
133k
    while (v >= B) {
116
        // Fetch low seven bits from current v, and the eight bit is marked as compression mark.
117
        // v | B is optimised from (v & (B-1)) | B, because result is assigned to uint8_t and other bits
118
        // is cleared by implicit conversion.
119
86.5k
        *(dst++) = v | B;
120
86.5k
        v >>= 7;
121
86.5k
    }
122
46.8k
    *(dst++) = static_cast<unsigned char>(v);
123
46.8k
    return dst;
124
46.8k
}
125
126
extern const uint8_t* decode_varint32_ptr_fallback(const uint8_t* p, const uint8_t* limit,
127
                                                   uint32_t* value);
128
129
inline const uint8_t* decode_varint32_ptr(const uint8_t* ptr, const uint8_t* limit,
130
510k
                                          uint32_t* value) {
131
510k
    if (ptr < limit) {
132
510k
        uint32_t result = *ptr;
133
510k
        if ((result & 128) == 0) {
134
481k
            *value = result;
135
481k
            return ptr + 1;
136
481k
        }
137
510k
    }
138
28.9k
    return decode_varint32_ptr_fallback(ptr, limit, value);
139
510k
}
140
141
extern const uint8_t* decode_varint64_ptr(const uint8_t* p, const uint8_t* limit, uint64_t* value);
142
143
template <typename T>
144
332k
void put_varint32(T* dst, uint32_t v) {
145
332k
    uint8_t buf[16];
146
332k
    uint8_t* ptr = encode_varint32(buf, v);
147
332k
    dst->append((char*)buf, static_cast<size_t>(ptr - buf));
148
332k
}
_ZN5doris12put_varint32INSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEEvPT_j
Line
Count
Source
144
1
void put_varint32(T* dst, uint32_t v) {
145
1
    uint8_t buf[16];
146
1
    uint8_t* ptr = encode_varint32(buf, v);
147
1
    dst->append((char*)buf, static_cast<size_t>(ptr - buf));
148
1
}
_ZN5doris12put_varint32INS_10faststringEEEvPT_j
Line
Count
Source
144
332k
void put_varint32(T* dst, uint32_t v) {
145
332k
    uint8_t buf[16];
146
332k
    uint8_t* ptr = encode_varint32(buf, v);
147
332k
    dst->append((char*)buf, static_cast<size_t>(ptr - buf));
148
332k
}
149
150
template <typename T>
151
1
void put_varint64(T* dst, uint64_t v) {
152
1
    uint8_t buf[16];
153
1
    uint8_t* ptr = encode_varint64(buf, v);
154
1
    dst->append((char*)buf, static_cast<size_t>(ptr - buf));
155
1
}
156
157
template <typename T>
158
46.8k
void put_length_prefixed_slice(T* dst, const Slice& value) {
159
46.8k
    put_varint32(dst, value.get_size());
160
46.8k
    dst->append(value.get_data(), value.get_size());
161
46.8k
}
162
163
template <typename T>
164
46.8k
void put_varint64_varint32(T* dst, uint64_t v1, uint32_t v2) {
165
46.8k
    uint8_t buf[16];
166
46.8k
    uint8_t* ptr = encode_varint64(buf, v1);
167
46.8k
    ptr = encode_varint32(ptr, v2);
168
46.8k
    dst->append((char*)buf, static_cast<size_t>(ptr - buf));
169
46.8k
}
_ZN5doris21put_varint64_varint32INS_10faststringEEEvPT_mj
Line
Count
Source
164
46.8k
void put_varint64_varint32(T* dst, uint64_t v1, uint32_t v2) {
165
46.8k
    uint8_t buf[16];
166
46.8k
    uint8_t* ptr = encode_varint64(buf, v1);
167
46.8k
    ptr = encode_varint32(ptr, v2);
168
46.8k
    dst->append((char*)buf, static_cast<size_t>(ptr - buf));
169
46.8k
}
_ZN5doris21put_varint64_varint32INSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEEvPT_mj
Line
Count
Source
164
1
void put_varint64_varint32(T* dst, uint64_t v1, uint32_t v2) {
165
1
    uint8_t buf[16];
166
1
    uint8_t* ptr = encode_varint64(buf, v1);
167
1
    ptr = encode_varint32(ptr, v2);
168
1
    dst->append((char*)buf, static_cast<size_t>(ptr - buf));
169
1
}
170
171
// parse a varint32 from the start of `input` into `val`.
172
// on success, return true and advance `input` past the parsed value.
173
// on failure, return false and `input` is not modified.
174
62.6k
inline bool get_varint32(Slice* input, uint32_t* val) {
175
62.6k
    const uint8_t* p = (const uint8_t*)input->data;
176
62.6k
    const uint8_t* limit = p + input->size;
177
62.6k
    const uint8_t* q = decode_varint32_ptr(p, limit, val);
178
62.6k
    if (q == nullptr) {
179
0
        return false;
180
62.6k
    } else {
181
62.6k
        *input = Slice(q, limit - q);
182
62.6k
        return true;
183
62.6k
    }
184
62.6k
}
185
186
// parse a varint64 from the start of `input` into `val`.
187
// on success, return true and advance `input` past the parsed value.
188
// on failure, return false and `input` is not modified.
189
20.4k
inline bool get_varint64(Slice* input, uint64_t* val) {
190
20.4k
    const uint8_t* p = (const uint8_t*)input->data;
191
20.4k
    const uint8_t* limit = p + input->size;
192
20.4k
    const uint8_t* q = decode_varint64_ptr(p, limit, val);
193
20.4k
    if (q == nullptr) {
194
0
        return false;
195
20.4k
    } else {
196
20.4k
        *input = Slice(q, limit - q);
197
20.4k
        return true;
198
20.4k
    }
199
20.4k
}
200
201
// parse a length-prefixed-slice from the start of `input` into `val`.
202
// on success, return true and advance `input` past the parsed value.
203
// on failure, return false and `input` may or may not be modified.
204
20.4k
inline bool get_length_prefixed_slice(Slice* input, Slice* val) {
205
20.4k
    uint32_t len;
206
20.4k
    if (get_varint32(input, &len) && input->get_size() >= len) {
207
20.4k
        *val = Slice(input->get_data(), len);
208
20.4k
        input->remove_prefix(len);
209
20.4k
        return true;
210
20.4k
    } else {
211
0
        return false;
212
0
    }
213
20.4k
}
214
215
} // namespace doris