be/src/storage/index/snii/encoding/varint.h
Line | Count | Source |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | |
18 | | #pragma once |
19 | | |
20 | | #include <cstddef> |
21 | | #include <cstdint> |
22 | | |
23 | | #include "common/status.h" |
24 | | |
25 | | namespace doris::snii { |
26 | | |
27 | | // LEB128 variable-length integer encoding + zigzag. out buffer must be >=10 bytes; returns number of bytes written. |
28 | | size_t varint_len(uint64_t v); |
29 | | size_t encode_varint32(uint32_t v, uint8_t* out); |
30 | | size_t encode_varint64(uint64_t v, uint8_t* out); |
31 | | |
32 | | // Decode a varint from the range [p, end); on success *next points to the next byte after the consumed input. |
33 | | Status decode_varint32(const uint8_t* p, const uint8_t* end, uint32_t* v, const uint8_t** next); |
34 | | Status decode_varint64(const uint8_t* p, const uint8_t* end, uint64_t* v, const uint8_t** next); |
35 | | |
36 | 3.22M | inline uint64_t zigzag_encode(int64_t v) { |
37 | 3.22M | return (static_cast<uint64_t>(v) << 1) ^ static_cast<uint64_t>(v >> 63); |
38 | 3.22M | } |
39 | 1.96M | inline int64_t zigzag_decode(uint64_t v) { |
40 | 1.96M | return static_cast<int64_t>(v >> 1) ^ -static_cast<int64_t>(v & 1); |
41 | 1.96M | } |
42 | | |
43 | | } // namespace doris::snii |