YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/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
41.0M
void AppendInt32ToKey(int32_t val, Buffer* dest) {
36
41.0M
  char buf[sizeof(int32_t)];
37
41.0M
  BigEndian::Store32(buf, val ^ kInt32SignBitFlipMask);
38
41.0M
  dest->append(buf, sizeof(buf));
39
41.0M
}
_ZN2yb4util16AppendInt32ToKeyINSt3__112basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEEEEviPT_
Line
Count
Source
35
7.56k
void AppendInt32ToKey(int32_t val, Buffer* dest) {
36
7.56k
  char buf[sizeof(int32_t)];
37
7.56k
  BigEndian::Store32(buf, val ^ kInt32SignBitFlipMask);
38
7.56k
  dest->append(buf, sizeof(buf));
39
7.56k
}
_ZN2yb4util16AppendInt32ToKeyINS_10ByteBufferILm64EEEEEviPT_
Line
Count
Source
35
41.0M
void AppendInt32ToKey(int32_t val, Buffer* dest) {
36
41.0M
  char buf[sizeof(int32_t)];
37
41.0M
  BigEndian::Store32(buf, val ^ kInt32SignBitFlipMask);
38
41.0M
  dest->append(buf, sizeof(buf));
39
41.0M
}
40
41
template <class Buffer>
42
9.21M
void AppendBigEndianUInt32(uint32_t u, Buffer* dest) {
43
9.21M
  char buf[sizeof(uint32_t)];
44
9.21M
  BigEndian::Store32(buf, u);
45
9.21M
  dest->append(buf, sizeof(buf));
46
9.21M
}
47
48
template <class Buffer>
49
716
void AppendFloatToKey(float val, Buffer* dest, bool descending = false) {
50
716
  char buf[sizeof(uint32_t)];
51
716
  uint32_t v = *(reinterpret_cast<uint32_t*>(&val));
52
716
  if (v >> 31) { // This is the sign bit: better than using val >= 0 (because -0, nulls denormals).
53
290
    v = ~v;
54
426
  } else {
55
426
    v ^= kInt32SignBitFlipMask;
56
426
  }
57
58
716
  if (descending) {
59
    // flip the bits to reverse the order.
60
420
    v = ~v;
61
420
  }
62
716
  BigEndian::Store32(buf, v);
63
716
  dest->append(buf, sizeof(buf));
64
716
}
_ZN2yb4util16AppendFloatToKeyINSt3__112basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEEEEvfPT_b
Line
Count
Source
49
43
void AppendFloatToKey(float val, Buffer* dest, bool descending = false) {
50
43
  char buf[sizeof(uint32_t)];
51
43
  uint32_t v = *(reinterpret_cast<uint32_t*>(&val));
52
43
  if (v >> 31) { // This is the sign bit: better than using val >= 0 (because -0, nulls denormals).
53
3
    v = ~v;
54
40
  } else {
55
40
    v ^= kInt32SignBitFlipMask;
56
40
  }
57
58
43
  if (descending) {
59
    // flip the bits to reverse the order.
60
0
    v = ~v;
61
0
  }
62
43
  BigEndian::Store32(buf, v);
63
43
  dest->append(buf, sizeof(buf));
64
43
}
_ZN2yb4util16AppendFloatToKeyINS_10ByteBufferILm64EEEEEvfPT_b
Line
Count
Source
49
673
void AppendFloatToKey(float val, Buffer* dest, bool descending = false) {
50
673
  char buf[sizeof(uint32_t)];
51
673
  uint32_t v = *(reinterpret_cast<uint32_t*>(&val));
52
673
  if (v >> 31) { // This is the sign bit: better than using val >= 0 (because -0, nulls denormals).
53
287
    v = ~v;
54
386
  } else {
55
386
    v ^= kInt32SignBitFlipMask;
56
386
  }
57
58
673
  if (descending) {
59
    // flip the bits to reverse the order.
60
420
    v = ~v;
61
420
  }
62
673
  BigEndian::Store32(buf, v);
63
673
  dest->append(buf, sizeof(buf));
64
673
}
65
66
template <class Buffer>
67
524k
void AppendDoubleToKey(double val, Buffer* dest, bool descending = false) {
68
524k
  char buf[sizeof(uint64_t)];
69
524k
  uint64_t v = *(reinterpret_cast<uint64_t*>(&val));
70
524k
  if (v >> 63) { // This is the sign bit: better than using val >= 0 (because -0, nulls denormals).
71
302
    v = ~v;
72
524k
  } else {
73
524k
    v ^= kInt64SignBitFlipMask;
74
524k
  }
75
76
524k
  if (descending) {
77
    // flip the bits to reverse the order.
78
420
    v = ~v;
79
420
  }
80
524k
  BigEndian::Store64(buf, v);
81
524k
  dest->append(buf, sizeof(buf));
82
524k
}
_ZN2yb4util17AppendDoubleToKeyINSt3__112basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEEEEvdPT_b
Line
Count
Source
67
12
void AppendDoubleToKey(double val, Buffer* dest, bool descending = false) {
68
12
  char buf[sizeof(uint64_t)];
69
12
  uint64_t v = *(reinterpret_cast<uint64_t*>(&val));
70
12
  if (v >> 63) { // This is the sign bit: better than using val >= 0 (because -0, nulls denormals).
71
0
    v = ~v;
72
12
  } else {
73
12
    v ^= kInt64SignBitFlipMask;
74
12
  }
75
76
12
  if (descending) {
77
    // flip the bits to reverse the order.
78
0
    v = ~v;
79
0
  }
80
12
  BigEndian::Store64(buf, v);
81
12
  dest->append(buf, sizeof(buf));
82
12
}
_ZN2yb4util17AppendDoubleToKeyINS_10ByteBufferILm64EEEEEvdPT_b
Line
Count
Source
67
524k
void AppendDoubleToKey(double val, Buffer* dest, bool descending = false) {
68
524k
  char buf[sizeof(uint64_t)];
69
524k
  uint64_t v = *(reinterpret_cast<uint64_t*>(&val));
70
524k
  if (v >> 63) { // This is the sign bit: better than using val >= 0 (because -0, nulls denormals).
71
302
    v = ~v;
72
524k
  } else {
73
524k
    v ^= kInt64SignBitFlipMask;
74
524k
  }
75
76
524k
  if (descending) {
77
    // flip the bits to reverse the order.
78
420
    v = ~v;
79
420
  }
80
524k
  BigEndian::Store64(buf, v);
81
524k
  dest->append(buf, sizeof(buf));
82
524k
}
83
84
8.10k
inline int32_t DecodeInt32FromKey(const rocksdb::Slice& slice) {
85
8.10k
  uint32_t v = BigEndian::Load32(slice.data());
86
8.10k
  return v ^ kInt32SignBitFlipMask;
87
8.10k
}
88
89
56.4M
inline int64_t DecodeInt64FromKey(const rocksdb::Slice& slice) {
90
56.4M
  uint64_t v = BigEndian::Load64(slice.data());
91
56.4M
  return v ^ kInt64SignBitFlipMask;
92
56.4M
}
93
94
4.21M
inline double DecodeDoubleFromKey(const rocksdb::Slice& slice, bool descending = false) {
95
4.21M
  uint64_t v = BigEndian::Load64(slice.data());
96
4.21M
  if (descending) {
97
    // Flip the bits.
98
2.91k
    v = ~v;
99
2.91k
  }
100
101
4.21M
  if (v >> 63) { // This is the sign bit: better than using val >= 0 (because -0, nulls denormals).
102
4.20M
    v ^= kInt64SignBitFlipMask;
103
2.43k
  } else {
104
2.43k
    v = ~v;
105
2.43k
  }
106
4.21M
  return *(reinterpret_cast<double*>(&v));
107
4.21M
}
108
109
4.20k
inline float DecodeFloatFromKey(const rocksdb::Slice& slice, bool descending = false) {
110
4.20k
  uint32_t v = BigEndian::Load32(slice.data());
111
4.20k
  if (descending) {
112
    // Flip the bits.
113
2.91k
    v = ~v;
114
2.91k
  }
115
116
4.20k
  if (v >> 31) { // This is the sign bit: better than using val >= 0 (because -0, nulls denormals).
117
2.26k
    v ^= kInt32SignBitFlipMask;
118
1.93k
  } else {
119
1.93k
    v = ~v;
120
1.93k
  }
121
4.20k
  return *(reinterpret_cast<float*>(&v));
122
4.20k
}
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
2.19M
inline void AppendInt64ToKey(int64_t val, Buffer* dest) {
129
2.19M
  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
2.19M
  BigEndian::Store64(buf, val ^ kInt64SignBitFlipMask);
133
2.19M
  dest->append(buf, sizeof(buf));
134
2.19M
}
_ZN2yb4util16AppendInt64ToKeyINS_10ByteBufferILm64EEEEEvxPT_
Line
Count
Source
128
2.19M
inline void AppendInt64ToKey(int64_t val, Buffer* dest) {
129
2.19M
  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
2.19M
  BigEndian::Store64(buf, val ^ kInt64SignBitFlipMask);
133
2.19M
  dest->append(buf, sizeof(buf));
134
2.19M
}
_ZN2yb4util16AppendInt64ToKeyINSt3__112basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEEEEvxPT_
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
218k
inline void AppendBigEndianUInt64(uint64_t u, std::string* dest) {
137
218k
  char buf[sizeof(uint64_t)];
138
218k
  BigEndian::Store64(buf, u);
139
218k
  dest->append(buf, sizeof(buf));
140
218k
}
141
142
} // namespace util
143
} // namespace yb
144
145
#endif // YB_UTIL_KV_UTIL_H