/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 | | inline void encode_fixed8(uint8_t* buf, uint8_t val) { |
28 | | *buf = val; |
29 | | } |
30 | | |
31 | 524k | inline void encode_fixed16_le(uint8_t* buf, uint16_t val) { |
32 | 524k | val = to_endian<std::endian::little>(val); |
33 | 524k | memcpy(buf, &val, sizeof(val)); |
34 | 524k | } |
35 | | |
36 | 123M | inline void encode_fixed32_le(uint8_t* buf, uint32_t val) { |
37 | 123M | val = to_endian<std::endian::little>(val); |
38 | 123M | memcpy(buf, &val, sizeof(val)); |
39 | 123M | } |
40 | | |
41 | 410k | inline void encode_fixed64_le(uint8_t* buf, uint64_t val) { |
42 | 410k | val = to_endian<std::endian::little>(val); |
43 | 410k | memcpy(buf, &val, sizeof(val)); |
44 | 410k | } |
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 | 6.99M | inline uint8_t decode_fixed8(const uint8_t* buf) { |
52 | 6.99M | return *buf; |
53 | 6.99M | } |
54 | | |
55 | 574k | inline uint16_t decode_fixed16_le(const uint8_t* buf) { |
56 | 574k | uint16_t res; |
57 | 574k | memcpy(&res, buf, sizeof(res)); |
58 | 574k | return to_endian<std::endian::little>(res); |
59 | 574k | } |
60 | | |
61 | 299M | inline uint32_t decode_fixed32_le(const uint8_t* buf) { |
62 | 299M | uint32_t res; |
63 | 299M | memcpy(&res, buf, sizeof(res)); |
64 | 299M | return to_endian<std::endian::little>(res); |
65 | 299M | } |
66 | | |
67 | 3.68G | inline uint64_t decode_fixed64_le(const uint8_t* buf) { |
68 | 3.68G | uint64_t res; |
69 | 3.68G | memcpy(&res, buf, sizeof(res)); |
70 | 3.68G | return to_endian<std::endian::little>(res); |
71 | 3.68G | } |
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 | 109M | void put_fixed32_le(T* dst, uint32_t val) { |
81 | 109M | uint8_t buf[sizeof(val)]; |
82 | 109M | encode_fixed32_le(buf, val); |
83 | 109M | dst->append((char*)buf, sizeof(buf)); |
84 | 109M | } _ZN5doris14put_fixed32_leINS_10faststringEEEvPT_j Line | Count | Source | 80 | 104M | void put_fixed32_le(T* dst, uint32_t val) { | 81 | 104M | uint8_t buf[sizeof(val)]; | 82 | 104M | encode_fixed32_le(buf, val); | 83 | 104M | dst->append((char*)buf, sizeof(buf)); | 84 | 104M | } |
_ZN5doris14put_fixed32_leINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEEvPT_j Line | Count | Source | 80 | 4.72M | void put_fixed32_le(T* dst, uint32_t val) { | 81 | 4.72M | uint8_t buf[sizeof(val)]; | 82 | 4.72M | encode_fixed32_le(buf, val); | 83 | 4.72M | dst->append((char*)buf, sizeof(buf)); | 84 | 4.72M | } |
|
85 | | |
86 | | template <typename T> |
87 | 25.7k | void put_fixed64_le(T* dst, uint64_t val) { |
88 | 25.7k | uint8_t buf[sizeof(val)]; |
89 | 25.7k | encode_fixed64_le(buf, val); |
90 | 25.7k | dst->append((char*)buf, sizeof(buf)); |
91 | 25.7k | } _ZN5doris14put_fixed64_leINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEEvPT_m Line | Count | Source | 87 | 1.23k | void put_fixed64_le(T* dst, uint64_t val) { | 88 | 1.23k | uint8_t buf[sizeof(val)]; | 89 | 1.23k | encode_fixed64_le(buf, val); | 90 | 1.23k | dst->append((char*)buf, sizeof(buf)); | 91 | 1.23k | } |
_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 | 57 | inline int varint_length(uint64_t v) { |
95 | 57 | int len = 1; |
96 | 57 | while (v >= 128) { |
97 | 0 | v >>= 7; |
98 | 0 | len++; |
99 | 0 | } |
100 | 57 | return len; |
101 | 57 | } |
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 | 3.98M | inline uint8_t* encode_varint64(uint8_t* dst, uint64_t v) { |
114 | 3.98M | static const unsigned int B = 128; |
115 | 8.95M | 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 | 4.97M | *(dst++) = v | B; |
120 | 4.97M | v >>= 7; |
121 | 4.97M | } |
122 | 3.98M | *(dst++) = static_cast<unsigned char>(v); |
123 | 3.98M | return dst; |
124 | 3.98M | } |
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 | 246M | uint32_t* value) { |
131 | 246M | if (ptr < limit) { |
132 | 246M | uint32_t result = *ptr; |
133 | 246M | if ((result & 128) == 0) { |
134 | 246M | *value = result; |
135 | 246M | return ptr + 1; |
136 | 246M | } |
137 | 246M | } |
138 | 18.4E | return decode_varint32_ptr_fallback(ptr, limit, value); |
139 | 246M | } |
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 | 66.6M | void put_varint32(T* dst, uint32_t v) { |
145 | 66.6M | uint8_t buf[16]; |
146 | 66.6M | uint8_t* ptr = encode_varint32(buf, v); |
147 | 66.6M | dst->append((char*)buf, static_cast<size_t>(ptr - buf)); |
148 | 66.6M | } |
149 | | |
150 | | template <typename T> |
151 | | void put_varint64(T* dst, uint64_t v) { |
152 | | uint8_t buf[16]; |
153 | | uint8_t* ptr = encode_varint64(buf, v); |
154 | | dst->append((char*)buf, static_cast<size_t>(ptr - buf)); |
155 | | } |
156 | | |
157 | | template <typename T> |
158 | 3.98M | void put_length_prefixed_slice(T* dst, const Slice& value) { |
159 | 3.98M | put_varint32(dst, value.get_size()); |
160 | 3.98M | dst->append(value.get_data(), value.get_size()); |
161 | 3.98M | } |
162 | | |
163 | | template <typename T> |
164 | 3.98M | void put_varint64_varint32(T* dst, uint64_t v1, uint32_t v2) { |
165 | 3.98M | uint8_t buf[16]; |
166 | 3.98M | uint8_t* ptr = encode_varint64(buf, v1); |
167 | 3.98M | ptr = encode_varint32(ptr, v2); |
168 | 3.98M | dst->append((char*)buf, static_cast<size_t>(ptr - buf)); |
169 | 3.98M | } |
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 | 786k | inline bool get_varint32(Slice* input, uint32_t* val) { |
175 | 786k | const uint8_t* p = (const uint8_t*)input->data; |
176 | 786k | const uint8_t* limit = p + input->size; |
177 | 786k | const uint8_t* q = decode_varint32_ptr(p, limit, val); |
178 | 786k | if (q == nullptr) { |
179 | 0 | return false; |
180 | 786k | } else { |
181 | 786k | *input = Slice(q, limit - q); |
182 | 786k | return true; |
183 | 786k | } |
184 | 786k | } |
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 | 284k | inline bool get_varint64(Slice* input, uint64_t* val) { |
190 | 284k | const uint8_t* p = (const uint8_t*)input->data; |
191 | 284k | const uint8_t* limit = p + input->size; |
192 | 284k | const uint8_t* q = decode_varint64_ptr(p, limit, val); |
193 | 284k | if (q == nullptr) { |
194 | 0 | return false; |
195 | 284k | } else { |
196 | 284k | *input = Slice(q, limit - q); |
197 | 284k | return true; |
198 | 284k | } |
199 | 284k | } |
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 | 287k | inline bool get_length_prefixed_slice(Slice* input, Slice* val) { |
205 | 287k | uint32_t len; |
206 | 287k | if (get_varint32(input, &len) && input->get_size() >= len) { |
207 | 285k | *val = Slice(input->get_data(), len); |
208 | 285k | input->remove_prefix(len); |
209 | 285k | return true; |
210 | 285k | } else { |
211 | 2.68k | return false; |
212 | 2.68k | } |
213 | 287k | } |
214 | | |
215 | | } // namespace doris |