/Users/deen/code/yugabyte-db/src/yb/docdb/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_VALUE_H_ |
15 | | #define YB_DOCDB_VALUE_H_ |
16 | | |
17 | | #include "yb/common/typedefs.h" |
18 | | #include "yb/docdb/primitive_value.h" |
19 | | #include "yb/util/monotime.h" |
20 | | |
21 | | namespace yb { |
22 | | namespace docdb { |
23 | | |
24 | | // This class represents the data stored in the value portion of rocksdb. It consists of the TTL |
25 | | // for the given key, the user specified timestamp and finally the value. These items are encoded |
26 | | // into a RocksDB Slice in the order mentioned above. The TTL and user timestamp are optional. |
27 | | class Value { |
28 | | public: |
29 | | Value() : primitive_value_(), |
30 | | ttl_(kMaxTtl), |
31 | | user_timestamp_(kInvalidUserTimestamp), |
32 | 250M | merge_flags_(0) { |
33 | 250M | } |
34 | | |
35 | | explicit Value(PrimitiveValue primitive_value, |
36 | | MonoDelta ttl = kMaxTtl, |
37 | | UserTimeMicros user_timestamp = kInvalidUserTimestamp, |
38 | | uint64_t merge_flags = 0) |
39 | | : primitive_value_(primitive_value), |
40 | | ttl_(ttl), |
41 | | user_timestamp_(user_timestamp), |
42 | 21.5M | merge_flags_(merge_flags) { |
43 | 21.5M | } |
44 | | |
45 | | static const uint64_t kTtlFlag = 0x1; |
46 | | |
47 | | static const MonoDelta kMaxTtl; |
48 | | // kResetTtl is useful for CQL when zero TTL indicates no TTL. |
49 | | static const MonoDelta kResetTtl; |
50 | | static const int64_t kInvalidUserTimestamp; |
51 | | static constexpr int kBytesPerInt64 = sizeof(int64_t); |
52 | | |
53 | 249M | MonoDelta ttl() const { return ttl_; } |
54 | | |
55 | 0 | MonoDelta* mutable_ttl() { return &ttl_; } |
56 | | |
57 | 64.6M | UserTimeMicros user_timestamp() const { return user_timestamp_; } |
58 | | |
59 | 0 | bool has_ttl() const { return !ttl_.Equals(kMaxTtl); } |
60 | | |
61 | 237M | bool has_user_timestamp() const { return user_timestamp_ != kInvalidUserTimestamp; } |
62 | | |
63 | 710M | ValueType value_type() const { return primitive_value_.value_type(); } |
64 | | |
65 | 242M | PrimitiveValue* mutable_primitive_value() { return &primitive_value_; } |
66 | | |
67 | 43.7M | const PrimitiveValue& primitive_value() const { return primitive_value_; } |
68 | | |
69 | 0 | uint64_t merge_flags() const { return merge_flags_; } |
70 | | |
71 | 20.9k | const DocHybridTime& intent_doc_ht() const { return intent_doc_ht_; } |
72 | | |
73 | 0 | void set_intent_doc_ht(DocHybridTime intent_doc_ht) { |
74 | 0 | intent_doc_ht_ = intent_doc_ht; |
75 | 0 | } |
76 | | |
77 | | void ClearIntentDocHt(); |
78 | | |
79 | | // Consume the merge_flags portion of the slice if it exists and return it. |
80 | | static CHECKED_STATUS DecodeMergeFlags(rocksdb::Slice* slice, uint64_t* merge_flags); |
81 | | |
82 | | // A version that doesn't mutate the slice. |
83 | | static CHECKED_STATUS DecodeMergeFlags(const rocksdb::Slice& slice, uint64_t* merge_flags); |
84 | | |
85 | | // Consume the Ttl portion of the slice if it exists and return it. |
86 | | static CHECKED_STATUS DecodeTTL(rocksdb::Slice* rocksdb_value, MonoDelta* ttl); |
87 | | |
88 | | // A version that doesn't mutate the slice. |
89 | | static CHECKED_STATUS DecodeTTL(const rocksdb::Slice& rocksdb_value, MonoDelta* ttl); |
90 | | |
91 | | // Decode the entire value |
92 | | CHECKED_STATUS Decode(const Slice& rocksdb_value); |
93 | | |
94 | | // Decode value control fields, w/o decrypting actual value. |
95 | | CHECKED_STATUS DecodeControlFields(Slice* slice); |
96 | | |
97 | | std::string ToString() const; |
98 | | |
99 | | // Encode and return result as string. |
100 | | // If external_value is not null, then it is appended to end of result instead of |
101 | | // the primitive_value_ field of this object. |
102 | | std::string Encode(const Slice* external_value = nullptr) const; |
103 | | |
104 | | // Appends encoded value to value_bytes. |
105 | | // If external_value is not null, then it is appended to end of value_bytes instead of |
106 | | // the primitive_value_ field of this object. |
107 | | void EncodeAndAppend(std::string* value_bytes, const Slice* external_value = nullptr) const; |
108 | | |
109 | | // Decodes the ValueType of the primitive value stored in the |
110 | | // given RocksDB value and any other values before it. |
111 | | static CHECKED_STATUS DecodePrimitiveValueType( |
112 | | const rocksdb::Slice& rocksdb_value, |
113 | | ValueType* value_type, |
114 | | uint64_t* merge_flags = nullptr, |
115 | | MonoDelta* ttl = nullptr, |
116 | | int64_t* user_timestamp = nullptr); |
117 | | |
118 | | // Return the user timestamp portion from a slice that points to the RocksDB value. |
119 | | static CHECKED_STATUS DecodeUserTimestamp(const rocksdb::Slice& rocksdb_value, |
120 | | UserTimeMicros* user_timestamp); |
121 | | |
122 | | static const Value& Tombstone(); |
123 | | static const std::string& EncodedTombstone(); |
124 | | |
125 | | static std::string DebugSliceToString(const Slice& encoded_value); |
126 | | |
127 | | static Result<bool> IsTombstoned(const Slice& slice); |
128 | | |
129 | | private: |
130 | | // Consume the timestamp portion of the slice assuming the beginning of the slice points to |
131 | | // the timestamp. |
132 | | static CHECKED_STATUS DecodeUserTimestamp(rocksdb::Slice* slice, UserTimeMicros* user_timestamp); |
133 | | PrimitiveValue primitive_value_; |
134 | | |
135 | | // The ttl of the Value. kMaxTtl is the default value. TTL is not included in encoded |
136 | | // form if it is equal to kMax. |
137 | | // The unit is milliseconds. |
138 | | MonoDelta ttl_; |
139 | | |
140 | | // The timestamp provided by the user as part of a 'USING TIMESTAMP' clause in CQL. |
141 | | UserTimeMicros user_timestamp_; |
142 | | |
143 | | // A place to store various merge flags; in particular, the MERGE flag currently used for TTL. |
144 | | // 0x1 = TTL-only entry |
145 | | // 0x3 = Value-only entry (potentially) |
146 | | uint64_t merge_flags_; |
147 | | |
148 | | // If this value was written using a transaction, |
149 | | // this field stores the original intent doc hybrid time. |
150 | | DocHybridTime intent_doc_ht_; |
151 | | }; |
152 | | |
153 | | } // namespace docdb |
154 | | } // namespace yb |
155 | | |
156 | | #endif // YB_DOCDB_VALUE_H_ |