YugabyteDB (2.13.1.0-b60, 21121d69985fbf76aa6958d8f04a9bfa936293b5)

Coverage Report

Created: 2022-03-22 16:43

/Users/deen/code/yugabyte-db/src/yb/util/kv_util.h
Line
Count
Source
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_KV_UTIL_H
15
#define YB_UTIL_KV_UTIL_H
16
17
#include <string>
18
19
#include "yb/gutil/endian.h"
20
#include "yb/util/byte_buffer.h"
21
#include "yb/util/slice.h"
22
23
namespace yb {
24
25
typedef ByteBuffer<64> KeyBuffer;
26
27
namespace util {
28
29
// We are flipping the sign bit of 64-bit integers appearing as object keys in a document so that
30
// negative numbers sort earlier.
31
constexpr uint64_t kInt64SignBitFlipMask = 0x8000000000000000L;
32
constexpr uint32_t kInt32SignBitFlipMask = 0x80000000;
33
34
template <class Buffer>
35
60.7M
void AppendInt32ToKey(int32_t val, Buffer* dest) {
36
60.7M
  char buf[sizeof(int32_t)];
37
60.7M
  BigEndian::Store32(buf, val ^ kInt32SignBitFlipMask);
38
60.7M
  dest->append(buf, sizeof(buf));
39
60.7M
}
void yb::util::AppendInt32ToKey<yb::ByteBuffer<64ul> >(int, yb::ByteBuffer<64ul>*)
Line
Count
Source
35
60.7M
void AppendInt32ToKey(int32_t val, Buffer* dest) {
36
60.7M
  char buf[sizeof(int32_t)];
37
60.7M
  BigEndian::Store32(buf, val ^ kInt32SignBitFlipMask);
38
60.7M
  dest->append(buf, sizeof(buf));
39
60.7M
}
void yb::util::AppendInt32ToKey<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >(int, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*)
Line
Count
Source
35
12.5k
void AppendInt32ToKey(int32_t val, Buffer* dest) {
36
12.5k
  char buf[sizeof(int32_t)];
37
12.5k
  BigEndian::Store32(buf, val ^ kInt32SignBitFlipMask);
38
12.5k
  dest->append(buf, sizeof(buf));
39
12.5k
}
40
41
template <class Buffer>
42
31.7M
void AppendBigEndianUInt32(uint32_t u, Buffer* dest) {
43
31.7M
  char buf[sizeof(uint32_t)];
44
31.7M
  BigEndian::Store32(buf, u);
45
31.7M
  dest->append(buf, sizeof(buf));
46
31.7M
}
47
48
template <class Buffer>
49
1.88k
void AppendFloatToKey(float val, Buffer* dest, bool descending = false) {
50
1.88k
  char buf[sizeof(uint32_t)];
51
1.88k
  uint32_t v = *(reinterpret_cast<uint32_t*>(&val));
52
1.88k
  if (v >> 31) { // This is the sign bit: better than using val >= 0 (because -0, nulls denormals).
53
324
    v = ~v;
54
1.55k
  } else {
55
1.55k
    v ^= kInt32SignBitFlipMask;
56
1.55k
  }
57
58
1.88k
  if (descending) {
59
    // flip the bits to reverse the order.
60
420
    v = ~v;
61
420
  }
62
1.88k
  BigEndian::Store32(buf, v);
63
1.88k
  dest->append(buf, sizeof(buf));
64
1.88k
}
void yb::util::AppendFloatToKey<yb::ByteBuffer<64ul> >(float, yb::ByteBuffer<64ul>*, bool)
Line
Count
Source
49
1.83k
void AppendFloatToKey(float val, Buffer* dest, bool descending = false) {
50
1.83k
  char buf[sizeof(uint32_t)];
51
1.83k
  uint32_t v = *(reinterpret_cast<uint32_t*>(&val));
52
1.83k
  if (v >> 31) { // This is the sign bit: better than using val >= 0 (because -0, nulls denormals).
53
318
    v = ~v;
54
1.51k
  } else {
55
1.51k
    v ^= kInt32SignBitFlipMask;
56
1.51k
  }
57
58
1.83k
  if (descending) {
59
    // flip the bits to reverse the order.
60
420
    v = ~v;
61
420
  }
62
1.83k
  BigEndian::Store32(buf, v);
63
1.83k
  dest->append(buf, sizeof(buf));
64
1.83k
}
void yb::util::AppendFloatToKey<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >(float, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*, bool)
Line
Count
Source
49
49
void AppendFloatToKey(float val, Buffer* dest, bool descending = false) {
50
49
  char buf[sizeof(uint32_t)];
51
49
  uint32_t v = *(reinterpret_cast<uint32_t*>(&val));
52
49
  if (v >> 31) { // This is the sign bit: better than using val >= 0 (because -0, nulls denormals).
53
6
    v = ~v;
54
43
  } else {
55
43
    v ^= kInt32SignBitFlipMask;
56
43
  }
57
58
49
  if (descending) {
59
    // flip the bits to reverse the order.
60
0
    v = ~v;
61
0
  }
62
49
  BigEndian::Store32(buf, v);
63
49
  dest->append(buf, sizeof(buf));
64
49
}
65
66
template <class Buffer>
67
681k
void AppendDoubleToKey(double val, Buffer* dest, bool descending = false) {
68
681k
  char buf[sizeof(uint64_t)];
69
681k
  uint64_t v = *(reinterpret_cast<uint64_t*>(&val));
70
681k
  if (v >> 63) { // This is the sign bit: better than using val >= 0 (because -0, nulls denormals).
71
409
    v = ~v;
72
681k
  } else {
73
681k
    v ^= kInt64SignBitFlipMask;
74
681k
  }
75
76
681k
  if (descending) {
77
    // flip the bits to reverse the order.
78
521
    v = ~v;
79
521
  }
80
681k
  BigEndian::Store64(buf, v);
81
681k
  dest->append(buf, sizeof(buf));
82
681k
}
void yb::util::AppendDoubleToKey<yb::ByteBuffer<64ul> >(double, yb::ByteBuffer<64ul>*, bool)
Line
Count
Source
67
681k
void AppendDoubleToKey(double val, Buffer* dest, bool descending = false) {
68
681k
  char buf[sizeof(uint64_t)];
69
681k
  uint64_t v = *(reinterpret_cast<uint64_t*>(&val));
70
681k
  if (v >> 63) { // This is the sign bit: better than using val >= 0 (because -0, nulls denormals).
71
406
    v = ~v;
72
681k
  } else {
73
681k
    v ^= kInt64SignBitFlipMask;
74
681k
  }
75
76
681k
  if (descending) {
77
    // flip the bits to reverse the order.
78
521
    v = ~v;
79
521
  }
80
681k
  BigEndian::Store64(buf, v);
81
681k
  dest->append(buf, sizeof(buf));
82
681k
}
void yb::util::AppendDoubleToKey<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >(double, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*, bool)
Line
Count
Source
67
18
void AppendDoubleToKey(double val, Buffer* dest, bool descending = false) {
68
18
  char buf[sizeof(uint64_t)];
69
18
  uint64_t v = *(reinterpret_cast<uint64_t*>(&val));
70
18
  if (v >> 63) { // This is the sign bit: better than using val >= 0 (because -0, nulls denormals).
71
3
    v = ~v;
72
15
  } else {
73
15
    v ^= kInt64SignBitFlipMask;
74
15
  }
75
76
18
  if (descending) {
77
    // flip the bits to reverse the order.
78
0
    v = ~v;
79
0
  }
80
18
  BigEndian::Store64(buf, v);
81
18
  dest->append(buf, sizeof(buf));
82
18
}
83
84
14.5k
inline int32_t DecodeInt32FromKey(const rocksdb::Slice& slice) {
85
14.5k
  uint32_t v = BigEndian::Load32(slice.data());
86
14.5k
  return v ^ kInt32SignBitFlipMask;
87
14.5k
}
88
89
84.3M
inline int64_t DecodeInt64FromKey(const rocksdb::Slice& slice) {
90
84.3M
  uint64_t v = BigEndian::Load64(slice.data());
91
84.3M
  return v ^ kInt64SignBitFlipMask;
92
84.3M
}
93
94
4.91M
inline double DecodeDoubleFromKey(const rocksdb::Slice& slice, bool descending = false) {
95
4.91M
  uint64_t v = BigEndian::Load64(slice.data());
96
4.91M
  if (descending) {
97
    // Flip the bits.
98
3.04k
    v = ~v;
99
3.04k
  }
100
101
4.91M
  if (v >> 63) { // This is the sign bit: better than using val >= 0 (because -0, nulls denormals).
102
4.91M
    v ^= kInt64SignBitFlipMask;
103
4.91M
  } else {
104
3.22k
    v = ~v;
105
3.22k
  }
106
4.91M
  return *(reinterpret_cast<double*>(&v));
107
4.91M
}
108
109
5.01k
inline float DecodeFloatFromKey(const rocksdb::Slice& slice, bool descending = false) {
110
5.01k
  uint32_t v = BigEndian::Load32(slice.data());
111
5.01k
  if (descending) {
112
    // Flip the bits.
113
2.91k
    v = ~v;
114
2.91k
  }
115
116
5.01k
  if (v >> 31) { // This is the sign bit: better than using val >= 0 (because -0, nulls denormals).
117
3.05k
    v ^= kInt32SignBitFlipMask;
118
3.05k
  } else {
119
1.96k
    v = ~v;
120
1.96k
  }
121
5.01k
  return *(reinterpret_cast<float*>(&v));
122
5.01k
}
123
124
// Encode and append the given signed 64-bit integer to the destination string holding a RocksDB
125
// key being constructed. We are flipping the sign bit so that negative numbers sort before positive
126
// ones.
127
template <class Buffer>
128
4.86M
inline void AppendInt64ToKey(int64_t val, Buffer* dest) {
129
4.86M
  char buf[sizeof(uint64_t)];
130
  // Flip the sign bit so that negative values sort before positive ones when compared as
131
  // big-endian byte sequences.
132
4.86M
  BigEndian::Store64(buf, val ^ kInt64SignBitFlipMask);
133
4.86M
  dest->append(buf, sizeof(buf));
134
4.86M
}
void yb::util::AppendInt64ToKey<yb::ByteBuffer<64ul> >(long long, yb::ByteBuffer<64ul>*)
Line
Count
Source
128
4.86M
inline void AppendInt64ToKey(int64_t val, Buffer* dest) {
129
4.86M
  char buf[sizeof(uint64_t)];
130
  // Flip the sign bit so that negative values sort before positive ones when compared as
131
  // big-endian byte sequences.
132
4.86M
  BigEndian::Store64(buf, val ^ kInt64SignBitFlipMask);
133
4.86M
  dest->append(buf, sizeof(buf));
134
4.86M
}
void yb::util::AppendInt64ToKey<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >(long long, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*)
Line
Count
Source
128
26
inline void AppendInt64ToKey(int64_t val, Buffer* dest) {
129
26
  char buf[sizeof(uint64_t)];
130
  // Flip the sign bit so that negative values sort before positive ones when compared as
131
  // big-endian byte sequences.
132
26
  BigEndian::Store64(buf, val ^ kInt64SignBitFlipMask);
133
26
  dest->append(buf, sizeof(buf));
134
26
}
135
136
327k
inline void AppendBigEndianUInt64(uint64_t u, std::string* dest) {
137
327k
  char buf[sizeof(uint64_t)];
138
327k
  BigEndian::Store64(buf, u);
139
327k
  dest->append(buf, sizeof(buf));
140
327k
}
141
142
} // namespace util
143
} // namespace yb
144
145
#endif // YB_UTIL_KV_UTIL_H