YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/docdb/primitive_value.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_DOCDB_PRIMITIVE_VALUE_H_
15
#define YB_DOCDB_PRIMITIVE_VALUE_H_
16
17
#include <ostream>
18
#include <string>
19
#include <vector>
20
21
#include <glog/logging.h>
22
23
#include "yb/common/common_fwd.h"
24
#include "yb/common/column_id.h"
25
#include "yb/common/doc_hybrid_time.h"
26
#include "yb/common/hybrid_time.h"
27
28
#include "yb/docdb/docdb_fwd.h"
29
30
#include "yb/util/algorithm_util.h"
31
#include "yb/util/net/inetaddress.h"
32
#include "yb/util/slice.h"
33
#include "yb/util/strongly_typed_bool.h"
34
#include "yb/util/timestamp.h"
35
#include "yb/util/uuid.h"
36
37
namespace yb {
38
namespace docdb {
39
40
// Used for extending a list.
41
// PREPEND prepends the arguments one by one (PREPEND a b c) will prepend [c b a] to the list,
42
// while PREPEND_BLOCK prepends the arguments together, so it will prepend [a b c] to the list.
43
YB_DEFINE_ENUM(ListExtendOrder, (APPEND)(PREPEND_BLOCK)(PREPEND))
44
45
// A necessary use of a forward declaration to avoid circular inclusion.
46
class SubDocument;
47
48
enum class SystemColumnIds : ColumnIdRep {
49
  kLivenessColumn = 0  // Stores the TTL for QL rows inserted using an INSERT statement.
50
};
51
52
class PrimitiveValue {
53
 public:
54
  static const PrimitiveValue kInvalid;
55
  static const PrimitiveValue kTombstone;
56
  static const PrimitiveValue kObject;
57
  static const PrimitiveValue kLivenessColumn;
58
59
  // Flags for jsonb.
60
  // Indicates that the stored jsonb is the complete jsonb value and not a partial update to jsonb.
61
  static constexpr int64_t kCompleteJsonb = 1;
62
63
  PrimitiveValue();
64
  explicit PrimitiveValue(ValueType value_type);
65
66
  PrimitiveValue(const PrimitiveValue& other);
67
68
732M
  PrimitiveValue(PrimitiveValue&& other) {
69
732M
    MoveFrom(&other);
70
732M
  }
71
72
42
  PrimitiveValue& operator =(const PrimitiveValue& other) {
73
42
    this->~PrimitiveValue();
74
42
    new(this) PrimitiveValue(other);
75
42
    return *this;
76
42
  }
77
78
14.5M
  PrimitiveValue& operator =(PrimitiveValue&& other) {
79
14.5M
    this->~PrimitiveValue();
80
14.5M
    MoveFrom(&other);
81
14.5M
    return *this;
82
14.5M
  }
83
84
  explicit PrimitiveValue(const Slice& s,
85
                          SortOrder sort_order = SortOrder::kAscending,
86
                          bool is_collate = false);
87
88
  explicit PrimitiveValue(const std::string& s,
89
                          SortOrder sort_order = SortOrder::kAscending,
90
                          bool is_collate = false);
91
92
  explicit PrimitiveValue(const char* s,
93
                          SortOrder sort_order = SortOrder::kAscending,
94
                          bool is_collate = false);
95
96
  explicit PrimitiveValue(int64_t v, SortOrder sort_order = SortOrder::kAscending);
97
98
  explicit PrimitiveValue(const Timestamp& timestamp,
99
                          SortOrder sort_order = SortOrder::kAscending);
100
101
  explicit PrimitiveValue(const InetAddress& inetaddress,
102
                          SortOrder sort_order = SortOrder::kAscending);
103
104
  explicit PrimitiveValue(const Uuid& uuid,
105
                          SortOrder sort_order = SortOrder::kAscending);
106
107
  explicit PrimitiveValue(const HybridTime& hybrid_time);
108
  explicit PrimitiveValue(const DocHybridTime& hybrid_time);
109
  explicit PrimitiveValue(const ColumnId column_id);
110
111
  static PrimitiveValue NullValue(SortingType sorting);
112
113
  // Converts a SortingType to its SortOrder equivalent.
114
  // SortingType::kAscending and SortingType::kNotSpecified get
115
  // converted to SortOrder::kAscending.
116
  // SortingType::kDescending gets converted to SortOrder::kDescending.
117
  static SortOrder SortOrderFromColumnSchemaSortingType(SortingType sorting_type);
118
119
  // Construct a primitive value from a QLValuePB.
120
  static PrimitiveValue FromQLValuePB(const QLValuePB& value,
121
                                      SortingType sorting_type);
122
123
  // Set a primitive value in a QLValuePB.
124
  static void ToQLValuePB(const PrimitiveValue& pv,
125
                          const std::shared_ptr<QLType>& ql_type,
126
                          QLValuePB* ql_val);
127
128
2.25G
  ValueType value_type() const { return type_; }
129
130
  void AppendToKey(KeyBytes* key_bytes) const;
131
132
  std::string ToValue() const;
133
134
  // Convert this value to a human-readable string for logging / debugging.
135
  std::string ToString(AutoDecodeKeys auto_decode_keys = AutoDecodeKeys::kFalse) const;
136
137
  ~PrimitiveValue();
138
139
  // Decodes a primitive value from the given slice representing a RocksDB key in our key encoding
140
  // format and consumes a prefix of the slice.
141
  static CHECKED_STATUS DecodeKey(rocksdb::Slice* slice, PrimitiveValue* out);
142
  CHECKED_STATUS DecodeFromKey(rocksdb::Slice* slice);
143
144
  // Decodes a primitive value from the given slice representing a RocksDB value in our value
145
  // encoding format. Expects the entire slice to be consumed and returns an error otherwise.
146
  CHECKED_STATUS DecodeFromValue(const rocksdb::Slice& rocksdb_slice);
147
148
  static PrimitiveValue Double(double d, SortOrder sort_order = SortOrder::kAscending);
149
  static PrimitiveValue Float(float f, SortOrder sort_order = SortOrder::kAscending);
150
  // decimal_str represents a human readable string representing the decimal number, e.g. "0.03".
151
  static PrimitiveValue Decimal(const std::string& decimal_str, SortOrder sort_order);
152
  static PrimitiveValue VarInt(const std::string& varint_str, SortOrder sort_order);
153
  static PrimitiveValue ArrayIndex(int64_t index);
154
  static PrimitiveValue UInt16Hash(uint16_t hash);
155
  static PrimitiveValue SystemColumnId(ColumnId column_id);
156
  static PrimitiveValue SystemColumnId(SystemColumnIds system_column_id);
157
  static PrimitiveValue Int32(int32_t v, SortOrder sort_order = SortOrder::kAscending);
158
  static PrimitiveValue UInt32(uint32_t v, SortOrder sort_order = SortOrder::kAscending);
159
  static PrimitiveValue UInt64(uint64_t v, SortOrder sort_order = SortOrder::kAscending);
160
  static PrimitiveValue TransactionId(Uuid transaction_id);
161
  static PrimitiveValue TableId(Uuid table_id);
162
  static PrimitiveValue PgTableOid(const PgTableOid pgtable_id);
163
  static PrimitiveValue Jsonb(const std::string& json);
164
  static PrimitiveValue GinNull(uint8_t v);
165
166
  KeyBytes ToKeyBytes() const;
167
168
  DocHybridTime hybrid_time() const;
169
170
  // As strange as it may sound, an instance of this class may sometimes contain a single byte that
171
  // indicates an empty data structure of a certain type (object, array), or a tombstone. This
172
  // method can tell whether what's stored here is an actual primitive value.
173
  bool IsPrimitive() const;
174
175
  bool IsTombstoneOrPrimitive() const;
176
177
  bool IsInfinity() const;
178
179
  int CompareTo(const PrimitiveValue& other) const;
180
181
  // Assuming this PrimitiveValue represents a string, return a Slice pointing to it.
182
  // This returns a YB slice, not a RocksDB slice, based on what was needed when this function was
183
  // implemented. This distinction should go away if we merge RocksDB and YB Slice classes.
184
0
  Slice GetStringAsSlice() const {
185
0
    DCHECK(IsString());
186
0
    return Slice(str_val_);
187
0
  }
188
189
  bool IsInt64() const;
190
191
  bool IsString() const;
192
193
  bool IsDouble() const;
194
195
43.3M
  const std::string& GetString() const {
196
43.3M
    DCHECK(IsString());
197
43.3M
    return str_val_;
198
43.3M
  }
199
200
  int32_t GetInt32() const;
201
202
  uint32_t GetUInt32() const;
203
204
  int64_t GetInt64() const;
205
206
  uint64_t GetUInt64() const;
207
208
  uint16_t GetUInt16() const;
209
210
1.47M
  double GetDouble() const {
211
1.47M
    DCHECK(IsDouble());
212
1.47M
    return double_val_;
213
1.47M
  }
214
215
  float GetFloat() const;
216
217
  const std::string& GetDecimal() const;
218
219
  const std::string& GetVarInt() const;
220
221
  Timestamp GetTimestamp() const;
222
223
  const InetAddress* GetInetaddress() const;
224
225
  const std::string& GetJson() const;
226
227
  const Uuid& GetUuid() const;
228
229
  ColumnId GetColumnId() const;
230
231
  uint8_t GetGinNull() const;
232
233
3.59G
  bool operator <(const PrimitiveValue& other) const {
234
3.59G
    return CompareTo(other) < 0;
235
3.59G
  }
236
237
31.0M
  bool operator <=(const PrimitiveValue& other) const {
238
31.0M
    return CompareTo(other) <= 0;
239
31.0M
  }
240
241
5.02M
  bool operator >(const PrimitiveValue& other) const {
242
5.02M
    return CompareTo(other) > 0;
243
5.02M
  }
244
245
31.0M
  bool operator >=(const PrimitiveValue& other) const {
246
31.0M
    return CompareTo(other) >= 0;
247
31.0M
  }
248
249
  bool operator==(const PrimitiveValue& other) const;
250
251
0
  bool operator!=(const PrimitiveValue& other) const { return !(*this == other); }
252
253
206
  ListExtendOrder GetExtendOrder() const {
254
206
    return extend_order_;
255
206
  }
256
257
203M
  int64_t GetTtl() const {
258
203M
    return ttl_seconds_;
259
203M
  }
260
261
203M
  bool IsWriteTimeSet() const {
262
203M
    return write_time_ != kUninitializedWriteTime;
263
203M
  }
264
265
181M
  int64_t GetWriteTime() const {
266
181M
    DCHECK_NE(kUninitializedWriteTime, write_time_);
267
181M
    return write_time_;
268
181M
  }
269
270
26.0M
  void SetTtl(const int64_t ttl_seconds) {
271
26.0M
    ttl_seconds_ = ttl_seconds;
272
26.0M
  }
273
274
5
  void SetExtendOrder(const ListExtendOrder extend_order) const {
275
5
    extend_order_ = extend_order;
276
5
  }
277
278
216M
  void SetWriteTime(const int64_t write_time) {
279
216M
    write_time_ = write_time;
280
216M
  }
281
  typedef std::vector<PrimitiveValue> FrozenContainer;
282
283
 protected:
284
285
  static constexpr int64_t kUninitializedWriteTime = std::numeric_limits<int64_t>::min();
286
287
  // Column attributes.
288
  int64_t ttl_seconds_ = -1;
289
  int64_t write_time_ = kUninitializedWriteTime;
290
291
  // TODO: make PrimitiveValue extend SubDocument and put this field
292
  // in SubDocument.
293
  // This field gives the extension order of elements of a list and
294
  // is applicable only to SubDocuments of type kArray.
295
  mutable ListExtendOrder extend_order_ = ListExtendOrder::APPEND;
296
297
  ValueType type_;
298
299
  // TODO: do we have to worry about alignment here?
300
  union {
301
    int32_t int32_val_;
302
    uint32_t uint32_val_;
303
    int64_t int64_val_;
304
    uint64_t uint64_val_;
305
    uint16_t uint16_val_;
306
    DocHybridTime hybrid_time_val_;
307
    std::string str_val_;
308
    float float_val_;
309
    double double_val_;
310
    Timestamp timestamp_val_;
311
    InetAddress* inetaddress_val_;
312
    Uuid uuid_val_;
313
    FrozenContainer* frozen_val_;
314
    // This is used in SubDocument to hold a pointer to a map or a vector.
315
    void* complex_data_structure_;
316
    ColumnId column_id_val_;
317
    std::string decimal_val_;
318
    std::string varint_val_;
319
    std::string json_val_;
320
    uint8_t gin_null_val_;
321
  };
322
323
 private:
324
325
  // This is used in both the move constructor and the move assignment operator. Assumes this object
326
  // has not been constructed, or that the destructor has just been called.
327
  void MoveFrom(PrimitiveValue* other);
328
};
329
330
0
inline std::ostream& operator<<(std::ostream& out, const PrimitiveValue& primitive_value) {
331
0
  out << primitive_value.ToString();
332
0
  return out;
333
0
}
334
335
0
inline std::ostream& operator<<(std::ostream& out, const SortOrder sort_order) {
336
0
  string sort_order_name = sort_order == SortOrder::kAscending ? "kAscending" : "kDescending";
337
0
  out << sort_order_name;
338
0
  return out;
339
0
}
340
341
// A variadic template utility for creating vectors with PrimitiveValue elements out of arbitrary
342
// sequences of arguments of supported types.
343
21.2M
inline void AppendPrimitiveValues(std::vector<PrimitiveValue>* dest) {}
344
345
template <class T, class ...U>
346
inline void AppendPrimitiveValues(std::vector<PrimitiveValue>* dest,
347
                                  T first_arg,
348
21.0M
                                  U... more_args) {
349
21.0M
  dest->emplace_back(first_arg);
350
21.0M
  AppendPrimitiveValues(dest, more_args...);
351
21.0M
}
Unexecuted instantiation: _ZN2yb5docdb21AppendPrimitiveValuesIiJPKciS3_EEEvPNSt3__16vectorINS0_14PrimitiveValueENS4_9allocatorIS6_EEEET_DpT0_
Unexecuted instantiation: _ZN2yb5docdb21AppendPrimitiveValuesIPKcJiS3_EEEvPNSt3__16vectorINS0_14PrimitiveValueENS4_9allocatorIS6_EEEET_DpT0_
Unexecuted instantiation: _ZN2yb5docdb21AppendPrimitiveValuesIiJPKcEEEvPNSt3__16vectorINS0_14PrimitiveValueENS4_9allocatorIS6_EEEET_DpT0_
Unexecuted instantiation: _ZN2yb5docdb21AppendPrimitiveValuesIPKcJEEEvPNSt3__16vectorINS0_14PrimitiveValueENS4_9allocatorIS6_EEEET_DpT0_
Unexecuted instantiation: _ZN2yb5docdb21AppendPrimitiveValuesIPKcJiS3_iEEEvPNSt3__16vectorINS0_14PrimitiveValueENS4_9allocatorIS6_EEEET_DpT0_
Unexecuted instantiation: _ZN2yb5docdb21AppendPrimitiveValuesIiJPKciEEEvPNSt3__16vectorINS0_14PrimitiveValueENS4_9allocatorIS6_EEEET_DpT0_
Unexecuted instantiation: _ZN2yb5docdb21AppendPrimitiveValuesIPKcJiEEEvPNSt3__16vectorINS0_14PrimitiveValueENS4_9allocatorIS6_EEEET_DpT0_
Unexecuted instantiation: _ZN2yb5docdb21AppendPrimitiveValuesIiJEEEvPNSt3__16vectorINS0_14PrimitiveValueENS2_9allocatorIS4_EEEET_DpT0_
Unexecuted instantiation: _ZN2yb5docdb21AppendPrimitiveValuesIPKcJS3_EEEvPNSt3__16vectorINS0_14PrimitiveValueENS4_9allocatorIS6_EEEET_DpT0_
Unexecuted instantiation: _ZN2yb5docdb21AppendPrimitiveValuesINSt3__112basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEEJEEEvPNS2_6vectorINS0_14PrimitiveValueENS6_ISA_EEEET_DpT0_
_ZN2yb5docdb21AppendPrimitiveValuesINS0_14PrimitiveValueEJEEEvPNSt3__16vectorIS2_NS3_9allocatorIS2_EEEET_DpT0_
Line
Count
Source
348
21.0M
                                  U... more_args) {
349
21.0M
  dest->emplace_back(first_arg);
350
21.0M
  AppendPrimitiveValues(dest, more_args...);
351
21.0M
}
Unexecuted instantiation: _ZN2yb5docdb21AppendPrimitiveValuesINS0_14PrimitiveValueEJS2_S2_EEEvPNSt3__16vectorIS2_NS3_9allocatorIS2_EEEET_DpT0_
Unexecuted instantiation: _ZN2yb5docdb21AppendPrimitiveValuesINS0_14PrimitiveValueEJS2_EEEvPNSt3__16vectorIS2_NS3_9allocatorIS2_EEEET_DpT0_
Unexecuted instantiation: _ZN2yb5docdb21AppendPrimitiveValuesINSt3__112basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEEJxEEEvPNS2_6vectorINS0_14PrimitiveValueENS6_ISA_EEEET_DpT0_
Unexecuted instantiation: _ZN2yb5docdb21AppendPrimitiveValuesIxJEEEvPNSt3__16vectorINS0_14PrimitiveValueENS2_9allocatorIS4_EEEET_DpT0_
Unexecuted instantiation: _ZN2yb5docdb21AppendPrimitiveValuesItJEEEvPNSt3__16vectorINS0_14PrimitiveValueENS2_9allocatorIS4_EEEET_DpT0_
Unexecuted instantiation: _ZN2yb5docdb21AppendPrimitiveValuesIiJiiiiEEEvPNSt3__16vectorINS0_14PrimitiveValueENS2_9allocatorIS4_EEEET_DpT0_
Unexecuted instantiation: _ZN2yb5docdb21AppendPrimitiveValuesIiJiiiEEEvPNSt3__16vectorINS0_14PrimitiveValueENS2_9allocatorIS4_EEEET_DpT0_
Unexecuted instantiation: _ZN2yb5docdb21AppendPrimitiveValuesIiJiiEEEvPNSt3__16vectorINS0_14PrimitiveValueENS2_9allocatorIS4_EEEET_DpT0_
Unexecuted instantiation: _ZN2yb5docdb21AppendPrimitiveValuesIiJiEEEvPNSt3__16vectorINS0_14PrimitiveValueENS2_9allocatorIS4_EEEET_DpT0_
Unexecuted instantiation: _ZN2yb5docdb21AppendPrimitiveValuesINSt3__112basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEEJiEEEvPNS2_6vectorINS0_14PrimitiveValueENS6_ISA_EEEET_DpT0_
352
353
template <class ...T>
354
0
inline std::vector<PrimitiveValue> PrimitiveValues(T... args) {
355
0
  std::vector<PrimitiveValue> v;
356
0
  AppendPrimitiveValues(&v, args...);
357
0
  return v;
358
0
}
Unexecuted instantiation: _ZN2yb5docdb15PrimitiveValuesIJiPKciS3_EEENSt3__16vectorINS0_14PrimitiveValueENS4_9allocatorIS6_EEEEDpT_
Unexecuted instantiation: _ZN2yb5docdb15PrimitiveValuesIJPKciS3_iEEENSt3__16vectorINS0_14PrimitiveValueENS4_9allocatorIS6_EEEEDpT_
Unexecuted instantiation: _ZN2yb5docdb15PrimitiveValuesIJPKciS3_EEENSt3__16vectorINS0_14PrimitiveValueENS4_9allocatorIS6_EEEEDpT_
Unexecuted instantiation: _ZN2yb5docdb15PrimitiveValuesIJPKcS3_EEENSt3__16vectorINS0_14PrimitiveValueENS4_9allocatorIS6_EEEEDpT_
Unexecuted instantiation: _ZN2yb5docdb15PrimitiveValuesIJNSt3__112basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEEEEENS2_6vectorINS0_14PrimitiveValueENS6_ISA_EEEEDpT_
Unexecuted instantiation: _ZN2yb5docdb15PrimitiveValuesIJNS0_14PrimitiveValueEEEENSt3__16vectorIS2_NS3_9allocatorIS2_EEEEDpT_
Unexecuted instantiation: _ZN2yb5docdb15PrimitiveValuesIJEEENSt3__16vectorINS0_14PrimitiveValueENS2_9allocatorIS4_EEEEDpT_
Unexecuted instantiation: _ZN2yb5docdb15PrimitiveValuesIJNSt3__112basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEExEEENS2_6vectorINS0_14PrimitiveValueENS6_ISA_EEEEDpT_
Unexecuted instantiation: _ZN2yb5docdb15PrimitiveValuesIJPKcEEENSt3__16vectorINS0_14PrimitiveValueENS4_9allocatorIS6_EEEEDpT_
Unexecuted instantiation: _ZN2yb5docdb15PrimitiveValuesIJPKciEEENSt3__16vectorINS0_14PrimitiveValueENS4_9allocatorIS6_EEEEDpT_
Unexecuted instantiation: _ZN2yb5docdb15PrimitiveValuesIJtEEENSt3__16vectorINS0_14PrimitiveValueENS2_9allocatorIS4_EEEEDpT_
Unexecuted instantiation: _ZN2yb5docdb15PrimitiveValuesIJiiiiiEEENSt3__16vectorINS0_14PrimitiveValueENS2_9allocatorIS4_EEEEDpT_
Unexecuted instantiation: _ZN2yb5docdb15PrimitiveValuesIJiiiEEENSt3__16vectorINS0_14PrimitiveValueENS2_9allocatorIS4_EEEEDpT_
Unexecuted instantiation: _ZN2yb5docdb15PrimitiveValuesIJiEEENSt3__16vectorINS0_14PrimitiveValueENS2_9allocatorIS4_EEEEDpT_
Unexecuted instantiation: _ZN2yb5docdb15PrimitiveValuesIJNSt3__112basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEEiEEENS2_6vectorINS0_14PrimitiveValueENS6_ISA_EEEEDpT_
359
360
}  // namespace docdb
361
}  // namespace yb
362
363
#endif  // YB_DOCDB_PRIMITIVE_VALUE_H_