/Users/deen/code/yugabyte-db/src/yb/util/fast_varint.h
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright (c) YugaByte, Inc. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
4 | | // in compliance with the License. You may obtain a copy of the License at |
5 | | // |
6 | | // http://www.apache.org/licenses/LICENSE-2.0 |
7 | | // |
8 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
9 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
10 | | // or implied. See the License for the specific language governing permissions and limitations |
11 | | // under the License. |
12 | | // |
13 | | |
14 | | #ifndef YB_UTIL_FAST_VARINT_H |
15 | | #define YB_UTIL_FAST_VARINT_H |
16 | | |
17 | | #include <string> |
18 | | |
19 | | #include <glog/logging.h> |
20 | | |
21 | | #include "yb/util/cast.h" |
22 | | #include "yb/util/slice.h" |
23 | | #include "yb/util/status.h" |
24 | | |
25 | | namespace yb { |
26 | | namespace util { |
27 | | |
28 | | constexpr size_t kMaxVarIntBufferSize = 16; |
29 | | |
30 | | // Computes the number of bytes needed to represent the given number as a signed VarInt. |
31 | | int SignedPositiveVarIntLength(uint64_t v); |
32 | | |
33 | | void FastEncodeSignedVarInt(int64_t v, uint8_t *dest, size_t *size); |
34 | | std::string FastEncodeSignedVarIntToStr(int64_t v); |
35 | | |
36 | | template <class Buffer> |
37 | 287M | void FastAppendSignedVarIntToBuffer(int64_t v, Buffer* dest) { |
38 | 287M | char buf[kMaxVarIntBufferSize]; |
39 | 287M | size_t len = 0; |
40 | 287M | FastEncodeSignedVarInt(v, to_uchar_ptr(buf), &len); |
41 | 287M | DCHECK_LE(len, 10); |
42 | 287M | dest->append(buf, len); |
43 | 287M | } _ZN2yb4util30FastAppendSignedVarIntToBufferINSt3__112basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEEEEvxPT_ Line | Count | Source | 37 | 270 | void FastAppendSignedVarIntToBuffer(int64_t v, Buffer* dest) { | 38 | 270 | char buf[kMaxVarIntBufferSize]; | 39 | 270 | size_t len = 0; | 40 | 270 | FastEncodeSignedVarInt(v, to_uchar_ptr(buf), &len); | 41 | 270 | DCHECK_LE(len, 10); | 42 | 270 | dest->append(buf, len); | 43 | 270 | } |
_ZN2yb4util30FastAppendSignedVarIntToBufferINS_10ByteBufferILm64EEEEEvxPT_ Line | Count | Source | 37 | 287M | void FastAppendSignedVarIntToBuffer(int64_t v, Buffer* dest) { | 38 | 287M | char buf[kMaxVarIntBufferSize]; | 39 | 287M | size_t len = 0; | 40 | 287M | FastEncodeSignedVarInt(v, to_uchar_ptr(buf), &len); | 41 | 287M | DCHECK_LE(len, 10); | 42 | 287M | dest->append(buf, len); | 43 | 287M | } |
|
44 | | |
45 | | // Returns status, decoded value and size consumed from source. |
46 | | // Might use effective performance optimization that reads before src, but not before |
47 | | // read_allowed_from. |
48 | | CHECKED_STATUS FastDecodeSignedVarInt( |
49 | | const uint8_t* src, size_t src_size, const uint8_t* read_allowed_from, int64_t* v, |
50 | | size_t* decoded_size); |
51 | | |
52 | | inline CHECKED_STATUS FastDecodeSignedVarInt( |
53 | | const char* src, size_t src_size, const char* read_allowed_from, int64_t* v, |
54 | 371k | size_t* decoded_size) { |
55 | 371k | return FastDecodeSignedVarInt( |
56 | 371k | to_uchar_ptr(src), src_size, to_uchar_ptr(read_allowed_from), v, |
57 | 371k | decoded_size); |
58 | 371k | } |
59 | | |
60 | | // WARNING: |
61 | | // FastDecodeSignedVarIntUnsafe functions below are optimized for performance, but require from |
62 | | // caller to guarantee that we can read some bytes (up to 7) before src. |
63 | | |
64 | | // Consumes decoded part of the slice. |
65 | | Result<int64_t> FastDecodeSignedVarIntUnsafe(Slice* slice); |
66 | | CHECKED_STATUS FastDecodeSignedVarIntUnsafe(const uint8_t* src, |
67 | | size_t src_size, |
68 | | int64_t* v, |
69 | | size_t* decoded_size); |
70 | | |
71 | | // The same as FastDecodeSignedVarIntUnsafe but takes a regular char pointer. |
72 | | inline CHECKED_STATUS FastDecodeSignedVarIntUnsafe( |
73 | 0 | const char* src, size_t src_size, int64_t* v, size_t* decoded_size) { |
74 | 0 | return FastDecodeSignedVarIntUnsafe(to_uchar_ptr(src), src_size, v, decoded_size); |
75 | 0 | } |
76 | | |
77 | | CHECKED_STATUS FastDecodeSignedVarIntUnsafe( |
78 | | const std::string& encoded, int64_t* v, size_t* decoded_size); |
79 | | |
80 | | // Encoding a "descending VarInt" is simply decoding -v as a VarInt. |
81 | 602M | inline char* FastEncodeDescendingSignedVarInt(int64_t v, char *buf) { |
82 | 602M | size_t size = 0; |
83 | 602M | FastEncodeSignedVarInt(-v, to_uchar_ptr(buf), &size); |
84 | 602M | return buf + size; |
85 | 602M | } |
86 | | |
87 | 22.1k | inline void FastEncodeDescendingSignedVarInt(int64_t v, std::string *dest) { |
88 | 22.1k | char buf[kMaxVarIntBufferSize]; |
89 | 22.1k | auto* end = FastEncodeDescendingSignedVarInt(v, buf); |
90 | 22.1k | dest->append(buf, end); |
91 | 22.1k | } |
92 | | |
93 | | // Decode a "descending VarInt" encoded by FastEncodeDescendingVarInt. |
94 | | CHECKED_STATUS FastDecodeDescendingSignedVarIntUnsafe(Slice *slice, int64_t *dest); |
95 | | Result<int64_t> FastDecodeDescendingSignedVarIntUnsafe(Slice* slice); |
96 | | |
97 | | size_t UnsignedVarIntLength(uint64_t v); |
98 | | void FastAppendUnsignedVarIntToStr(uint64_t v, std::string* dest); |
99 | | void FastEncodeUnsignedVarInt(uint64_t v, uint8_t *dest, size_t *size); |
100 | | CHECKED_STATUS FastDecodeUnsignedVarInt( |
101 | | const uint8_t* src, size_t src_size, uint64_t* v, size_t* decoded_size); |
102 | | Result<uint64_t> FastDecodeUnsignedVarInt(Slice* slice); |
103 | | Result<uint64_t> FastDecodeUnsignedVarInt(const Slice& slice); |
104 | | |
105 | | } // namespace util |
106 | | } // namespace yb |
107 | | |
108 | | #endif // YB_UTIL_FAST_VARINT_H |