/Users/deen/code/yugabyte-db/src/yb/docdb/key_bytes.cc
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 | | #include "yb/docdb/key_bytes.h" |
15 | | |
16 | | #include <cstdint> |
17 | | #include <string> |
18 | | #include <vector> |
19 | | |
20 | | #include <glog/logging.h> |
21 | | |
22 | | #include "yb/common/column_id.h" |
23 | | #include "yb/common/doc_hybrid_time.h" |
24 | | #include "yb/common/hybrid_time.h" |
25 | | |
26 | | #include "yb/docdb/doc_kv_util.h" |
27 | | #include "yb/docdb/value_type.h" |
28 | | |
29 | | #include "yb/util/bytes_formatter.h" |
30 | | #include "yb/util/enums.h" |
31 | | #include "yb/util/fast_varint.h" |
32 | | #include "yb/util/monotime.h" |
33 | | #include "yb/util/slice.h" |
34 | | #include "yb/util/strongly_typed_bool.h" |
35 | | |
36 | | namespace yb { |
37 | | namespace docdb { |
38 | | |
39 | 109k | void AppendDocHybridTime(const DocHybridTime& doc_ht, KeyBytes* key) { |
40 | 109k | key->AppendValueType(ValueType::kHybridTime); |
41 | 109k | doc_ht.AppendEncodedInDocDbFormat(key->mutable_data()); |
42 | 109k | } |
43 | | |
44 | 47.1M | void AppendHash(uint16_t hash, KeyBytes* key) { |
45 | 47.1M | key->AppendValueType(ValueType::kUInt16Hash); |
46 | 47.1M | key->AppendUInt16(hash); |
47 | 47.1M | } |
48 | | |
49 | 10 | void KeyBytes::AppendUInt64AsVarInt(uint64_t value) { |
50 | 10 | unsigned char buf[util::kMaxVarIntBufferSize]; |
51 | 10 | size_t len = 0; |
52 | 10 | util::FastEncodeUnsignedVarInt(value, buf, &len); |
53 | 10 | AppendRawBytes(Slice(buf, len)); |
54 | 10 | } |
55 | | |
56 | 820M | void KeyBytes::AppendColumnId(ColumnId column_id) { |
57 | 820M | util::FastAppendSignedVarIntToBuffer(column_id.rep(), &data_); |
58 | 820M | } |
59 | | |
60 | 2.68G | void KeyBytes::AppendValueType(ValueType value_type) { |
61 | 2.68G | data_.push_back(static_cast<char>(value_type)); |
62 | 2.68G | } |
63 | | |
64 | 8.69M | void KeyBytes::AppendValueTypeBeforeGroupEnd(ValueType value_type) { |
65 | 8.69M | if (data_.empty() || data_.back() != ValueTypeAsChar::kGroupEnd8.68M ) { |
66 | 7.50k | AppendValueType(value_type); |
67 | 7.50k | AppendValueType(ValueType::kGroupEnd); |
68 | 8.68M | } else { |
69 | 8.68M | data_.back() = static_cast<char>(value_type); |
70 | 8.68M | data_.push_back(ValueTypeAsChar::kGroupEnd); |
71 | 8.68M | } |
72 | 8.69M | } |
73 | | |
74 | 9.68M | void KeyBytes::AppendHybridTime(const DocHybridTime& hybrid_time) { |
75 | 9.68M | hybrid_time.AppendEncodedInDocDbFormat(&data_); |
76 | 9.68M | } |
77 | | |
78 | 135M | void KeyBytes::RemoveValueTypeSuffix(ValueType value_type) { |
79 | 135M | CHECK_GE(data_.size(), sizeof(char)); |
80 | 135M | CHECK_EQ(data_.back(), static_cast<char>(value_type)); |
81 | 135M | data_.pop_back(); |
82 | 135M | } |
83 | | |
84 | 8.02k | std::string KeyBytes::ToString() const { |
85 | 8.02k | return FormatSliceAsStr(data_.AsSlice()); |
86 | 8.02k | } |
87 | | |
88 | 39.2M | void KeyBytes::AppendString(const std::string& raw_string) { |
89 | 39.2M | ZeroEncodeAndAppendStrToKey(raw_string, &data_); |
90 | 39.2M | } |
91 | | |
92 | 99.5k | void KeyBytes::AppendDescendingString(const std::string &raw_string) { |
93 | 99.5k | ComplementZeroEncodeAndAppendStrToKey(raw_string, &data_); |
94 | 99.5k | } |
95 | | |
96 | 2.05k | void KeyBytes::AppendUInt64(uint64_t x) { |
97 | 2.05k | AppendUInt64ToKey(x, &data_); |
98 | 2.05k | } |
99 | | |
100 | 0 | void KeyBytes::AppendDescendingUInt64(uint64_t x) { |
101 | 0 | AppendUInt64ToKey(~x, &data_); |
102 | 0 | } |
103 | | |
104 | 17.1M | void KeyBytes::AppendUInt32(uint32_t x) { |
105 | 17.1M | AppendUInt32ToKey(x, &data_); |
106 | 17.1M | } |
107 | | |
108 | 120 | void KeyBytes::AppendDescendingUInt32(uint32_t x) { |
109 | 120 | AppendUInt32ToKey(~x, &data_); |
110 | 120 | } |
111 | | |
112 | 47.3M | void KeyBytes::AppendUInt16(uint16_t x) { |
113 | 47.3M | AppendUInt16ToKey(x, &data_); |
114 | 47.3M | } |
115 | | |
116 | 117M | void KeyBytes::AppendGroupEnd() { |
117 | 117M | AppendValueType(ValueType::kGroupEnd); |
118 | 117M | } |
119 | | |
120 | 1.56G | void KeyBytes::Truncate(size_t new_size) { |
121 | 1.56G | DCHECK_LE(new_size, data_.size()); |
122 | 1.56G | data_.Truncate(new_size); |
123 | 1.56G | } |
124 | | |
125 | 179M | void KeyBytes::RemoveLastByte() { |
126 | 179M | DCHECK(!data_.empty()); |
127 | 179M | data_.pop_back(); |
128 | 179M | } |
129 | | |
130 | | } // namespace docdb |
131 | | } // namespace yb |