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 | | |
13 | | #ifndef __APPLE__ |
14 | | #include <endian.h> |
15 | | #endif |
16 | | #include <stdint.h> |
17 | | #include <string.h> |
18 | | |
19 | | #include "exec/common/endian.h" |
20 | | #include "storage/olap_common.h" |
21 | | #include "util/slice.h" |
22 | | |
23 | | namespace doris { |
24 | | // TODO(zc): add encode big endian later when we need it |
25 | | // use big endian when we have order requirement. |
26 | | // little endian is more efficient when we use X86 CPU, so |
27 | | // when we have no order needs, we prefer little endian encoding |
28 | | inline void encode_fixed8(uint8_t* buf, uint8_t val) { |
29 | | *buf = val; |
30 | | } |
31 | | |
32 | 10.1k | inline void encode_fixed16_le(uint8_t* buf, uint16_t val) { |
33 | 10.1k | val = to_endian<std::endian::little>(val); |
34 | 10.1k | memcpy(buf, &val, sizeof(val)); |
35 | 10.1k | } |
36 | | |
37 | 56.5M | inline void encode_fixed32_le(uint8_t* buf, uint32_t val) { |
38 | 56.5M | val = to_endian<std::endian::little>(val); |
39 | 56.5M | memcpy(buf, &val, sizeof(val)); |
40 | 56.5M | } |
41 | | |
42 | 88.0k | inline void encode_fixed64_le(uint8_t* buf, uint64_t val) { |
43 | 88.0k | val = to_endian<std::endian::little>(val); |
44 | 88.0k | memcpy(buf, &val, sizeof(val)); |
45 | 88.0k | } |
46 | | |
47 | 24.4k | inline void encode_fixed128_le(uint8_t* buf, uint128_t val) { |
48 | 24.4k | val = to_endian<std::endian::little>(val); |
49 | 24.4k | memcpy(buf, &val, sizeof(val)); |
50 | 24.4k | } |
51 | | |
52 | 3.94M | inline uint8_t decode_fixed8(const uint8_t* buf) { |
53 | 3.94M | return *buf; |
54 | 3.94M | } |
55 | | |
56 | 57.5k | inline uint16_t decode_fixed16_le(const uint8_t* buf) { |
57 | 57.5k | uint16_t res; |
58 | 57.5k | memcpy(&res, buf, sizeof(res)); |
59 | 57.5k | return to_endian<std::endian::little>(res); |
60 | 57.5k | } |
61 | | |
62 | 141M | inline uint32_t decode_fixed32_le(const uint8_t* buf) { |
63 | 141M | uint32_t res; |
64 | 141M | memcpy(&res, buf, sizeof(res)); |
65 | 141M | return to_endian<std::endian::little>(res); |
66 | 141M | } |
67 | | |
68 | 54.6k | inline uint64_t decode_fixed64_le(const uint8_t* buf) { |
69 | 54.6k | uint64_t res; |
70 | 54.6k | memcpy(&res, buf, sizeof(res)); |
71 | 54.6k | return to_endian<std::endian::little>(res); |
72 | 54.6k | } |
73 | | |
74 | 415 | inline void decode_fixed64_le_array(uint64_t* dst, const void* src, size_t n) { |
75 | 415 | if (n == 0) { |
76 | 0 | return; |
77 | 0 | } |
78 | 415 | if constexpr (std::endian::native == std::endian::little) { |
79 | 415 | memcpy(dst, src, sizeof(uint64_t) * n); |
80 | | } else { |
81 | | const auto* ptr = reinterpret_cast<const uint8_t*>(src); |
82 | | for (size_t i = 0; i < n; ++i) { |
83 | | dst[i] = decode_fixed64_le(ptr); |
84 | | ptr += sizeof(uint64_t); |
85 | | } |
86 | | } |
87 | 415 | } |
88 | | |
89 | 24.4k | inline uint128_t decode_fixed128_le(const uint8_t* buf) { |
90 | 24.4k | uint128_t res; |
91 | 24.4k | memcpy(&res, buf, sizeof(res)); |
92 | 24.4k | return to_endian<std::endian::little>(res); |
93 | 24.4k | } |
94 | | |
95 | | template <typename T> |
96 | 27.2M | void put_fixed32_le(T* dst, uint32_t val) { |
97 | 27.2M | uint8_t buf[sizeof(val)]; |
98 | 27.2M | encode_fixed32_le(buf, val); |
99 | 27.2M | dst->append((char*)buf, sizeof(buf)); |
100 | 27.2M | } _ZN5doris14put_fixed32_leINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEEvPT_j Line | Count | Source | 96 | 2.03M | void put_fixed32_le(T* dst, uint32_t val) { | 97 | 2.03M | uint8_t buf[sizeof(val)]; | 98 | 2.03M | encode_fixed32_le(buf, val); | 99 | 2.03M | dst->append((char*)buf, sizeof(buf)); | 100 | 2.03M | } |
_ZN5doris14put_fixed32_leINS_10faststringEEEvPT_j Line | Count | Source | 96 | 25.1M | void put_fixed32_le(T* dst, uint32_t val) { | 97 | 25.1M | uint8_t buf[sizeof(val)]; | 98 | 25.1M | encode_fixed32_le(buf, val); | 99 | 25.1M | dst->append((char*)buf, sizeof(buf)); | 100 | 25.1M | } |
|
101 | | |
102 | | template <typename T> |
103 | 25.9k | void put_fixed64_le(T* dst, uint64_t val) { |
104 | 25.9k | uint8_t buf[sizeof(val)]; |
105 | 25.9k | encode_fixed64_le(buf, val); |
106 | 25.9k | dst->append((char*)buf, sizeof(buf)); |
107 | 25.9k | } _ZN5doris14put_fixed64_leINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEEvPT_m Line | Count | Source | 103 | 1.51k | void put_fixed64_le(T* dst, uint64_t val) { | 104 | 1.51k | uint8_t buf[sizeof(val)]; | 105 | 1.51k | encode_fixed64_le(buf, val); | 106 | 1.51k | dst->append((char*)buf, sizeof(buf)); | 107 | 1.51k | } |
_ZN5doris14put_fixed64_leINS_10faststringEEEvPT_m Line | Count | Source | 103 | 24.4k | void put_fixed64_le(T* dst, uint64_t val) { | 104 | 24.4k | uint8_t buf[sizeof(val)]; | 105 | 24.4k | encode_fixed64_le(buf, val); | 106 | 24.4k | dst->append((char*)buf, sizeof(buf)); | 107 | 24.4k | } |
|
108 | | |
109 | | // Returns the length of the varint32 or varint64 encoding of "v" |
110 | 62 | inline int varint_length(uint64_t v) { |
111 | 62 | int len = 1; |
112 | 62 | while (v >= 128) { |
113 | 0 | v >>= 7; |
114 | 0 | len++; |
115 | 0 | } |
116 | 62 | return len; |
117 | 62 | } |
118 | | |
119 | | template <typename T> |
120 | 24.4k | void put_fixed128_le(T* dst, uint128_t val) { |
121 | 24.4k | uint8_t buf[sizeof(val)]; |
122 | 24.4k | encode_fixed128_le(buf, val); |
123 | 24.4k | dst->append((char*)buf, sizeof(buf)); |
124 | 24.4k | } |
125 | | |
126 | | extern uint8_t* encode_varint32(uint8_t* dst, uint32_t value); |
127 | | extern uint8_t* encode_varint64(uint8_t* dst, uint64_t value); |
128 | | |
129 | 1.75M | inline uint8_t* encode_varint64(uint8_t* dst, uint64_t v) { |
130 | 1.75M | static const unsigned int B = 128; |
131 | 3.87M | while (v >= B) { |
132 | | // Fetch low seven bits from current v, and the eight bit is marked as compression mark. |
133 | | // v | B is optimised from (v & (B-1)) | B, because result is assigned to uint8_t and other bits |
134 | | // is cleared by implicit conversion. |
135 | 2.11M | *(dst++) = uint8_t(v | B); |
136 | 2.11M | v >>= 7; |
137 | 2.11M | } |
138 | 1.75M | *(dst++) = static_cast<unsigned char>(v); |
139 | 1.75M | return dst; |
140 | 1.75M | } |
141 | | |
142 | | extern const uint8_t* decode_varint32_ptr_fallback(const uint8_t* p, const uint8_t* limit, |
143 | | uint32_t* value); |
144 | | |
145 | | inline const uint8_t* decode_varint32_ptr(const uint8_t* ptr, const uint8_t* limit, |
146 | 166M | uint32_t* value) { |
147 | 166M | if (ptr < limit) { |
148 | 166M | uint32_t result = *ptr; |
149 | 166M | if ((result & 128) == 0) { |
150 | 162M | *value = result; |
151 | 162M | return ptr + 1; |
152 | 162M | } |
153 | 166M | } |
154 | 3.65M | return decode_varint32_ptr_fallback(ptr, limit, value); |
155 | 166M | } |
156 | | |
157 | | extern const uint8_t* decode_varint64_ptr(const uint8_t* p, const uint8_t* limit, uint64_t* value); |
158 | | |
159 | | template <typename T> |
160 | 21.4M | void put_varint32(T* dst, uint32_t v) { |
161 | 21.4M | uint8_t buf[16]; |
162 | 21.4M | uint8_t* ptr = encode_varint32(buf, v); |
163 | 21.4M | dst->append((char*)buf, static_cast<size_t>(ptr - buf)); |
164 | 21.4M | } |
165 | | |
166 | | template <typename T> |
167 | | void put_varint64(T* dst, uint64_t v) { |
168 | | uint8_t buf[16]; |
169 | | uint8_t* ptr = encode_varint64(buf, v); |
170 | | dst->append((char*)buf, static_cast<size_t>(ptr - buf)); |
171 | | } |
172 | | |
173 | | template <typename T> |
174 | 1.75M | void put_length_prefixed_slice(T* dst, const Slice& value) { |
175 | 1.75M | put_varint32(dst, uint32_t(value.get_size())); |
176 | 1.75M | dst->append(value.get_data(), value.get_size()); |
177 | 1.75M | } |
178 | | |
179 | | template <typename T> |
180 | 1.75M | void put_varint64_varint32(T* dst, uint64_t v1, uint32_t v2) { |
181 | 1.75M | uint8_t buf[16]; |
182 | 1.75M | uint8_t* ptr = encode_varint64(buf, v1); |
183 | 1.75M | ptr = encode_varint32(ptr, v2); |
184 | 1.75M | dst->append((char*)buf, static_cast<size_t>(ptr - buf)); |
185 | 1.75M | } |
186 | | |
187 | | // parse a varint32 from the start of `input` into `val`. |
188 | | // on success, return true and advance `input` past the parsed value. |
189 | | // on failure, return false and `input` is not modified. |
190 | 8.17M | inline bool get_varint32(Slice* input, uint32_t* val) { |
191 | 8.17M | const auto* p = (const uint8_t*)input->data; |
192 | 8.17M | const uint8_t* limit = p + input->size; |
193 | 8.17M | const uint8_t* q = decode_varint32_ptr(p, limit, val); |
194 | 8.17M | if (q == nullptr) { |
195 | 0 | return false; |
196 | 8.17M | } else { |
197 | 8.17M | *input = Slice(q, limit - q); |
198 | 8.17M | return true; |
199 | 8.17M | } |
200 | 8.17M | } |
201 | | |
202 | | // parse a varint64 from the start of `input` into `val`. |
203 | | // on success, return true and advance `input` past the parsed value. |
204 | | // on failure, return false and `input` is not modified. |
205 | 3.61M | inline bool get_varint64(Slice* input, uint64_t* val) { |
206 | 3.61M | const auto* p = (const uint8_t*)input->data; |
207 | 3.61M | const uint8_t* limit = p + input->size; |
208 | 3.61M | const uint8_t* q = decode_varint64_ptr(p, limit, val); |
209 | 3.61M | if (q == nullptr) { |
210 | 0 | return false; |
211 | 3.61M | } else { |
212 | 3.61M | *input = Slice(q, limit - q); |
213 | 3.61M | return true; |
214 | 3.61M | } |
215 | 3.61M | } |
216 | | |
217 | | // parse a length-prefixed-slice from the start of `input` into `val`. |
218 | | // on success, return true and advance `input` past the parsed value. |
219 | | // on failure, return false and `input` may or may not be modified. |
220 | 3.60M | inline bool get_length_prefixed_slice(Slice* input, Slice* val) { |
221 | 3.60M | uint32_t len; |
222 | 3.61M | if (get_varint32(input, &len) && input->get_size() >= len) { |
223 | 3.60M | *val = Slice(input->get_data(), len); |
224 | 3.60M | input->remove_prefix(len); |
225 | 3.60M | return true; |
226 | 3.60M | } else { |
227 | 801 | return false; |
228 | 801 | } |
229 | 3.60M | } |
230 | | } // namespace doris |