/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 | | struct ValueControlFields { |
25 | | static const MonoDelta kMaxTtl; |
26 | | // kResetTtl is useful for CQL when zero TTL indicates no TTL. |
27 | | static const MonoDelta kResetTtl; |
28 | | static const int64_t kInvalidUserTimestamp; |
29 | | |
30 | | static const uint64_t kTtlFlag = 0x1; |
31 | | |
32 | | // A place to store various merge flags; in particular, the MERGE flag currently used for TTL. |
33 | | // 0x1 = TTL-only entry |
34 | | // 0x3 = Value-only entry (potentially) |
35 | | uint64_t merge_flags = 0; |
36 | | |
37 | | // If this value was written using a transaction, |
38 | | // this field stores the original intent doc hybrid time. |
39 | | DocHybridTime intent_doc_ht = DocHybridTime(); |
40 | | |
41 | | // The ttl of the Value. kMaxTtl is the default value. TTL is not included in encoded |
42 | | // form if it is equal to kMax. |
43 | | // The unit is milliseconds. |
44 | | MonoDelta ttl = kMaxTtl; |
45 | | |
46 | | // The timestamp provided by the user as part of a 'USING TIMESTAMP' clause in CQL. |
47 | | UserTimeMicros user_timestamp = kInvalidUserTimestamp; |
48 | | |
49 | | void AppendEncoded(std::string* out) const; |
50 | | std::string ToString() const; |
51 | | |
52 | | // Decode value control fields, w/o decrypting actual value. |
53 | | static Result<ValueControlFields> Decode(Slice* slice); |
54 | | |
55 | 0 | bool has_ttl() const { |
56 | 0 | return !ttl.Equals(kMaxTtl); |
57 | 0 | } |
58 | | |
59 | 830M | bool has_user_timestamp() const { |
60 | 830M | return user_timestamp != kInvalidUserTimestamp; |
61 | 830M | } |
62 | | }; |
63 | | |
64 | | // This class represents the data stored in the value portion of rocksdb. It consists of the TTL |
65 | | // for the given key, the user specified timestamp and finally the value. These items are encoded |
66 | | // into a RocksDB Slice in the order mentioned above. The TTL and user timestamp are optional. |
67 | | class Value { |
68 | | public: |
69 | 749M | Value() = default; |
70 | | |
71 | | explicit Value(const PrimitiveValue& primitive_value, |
72 | | const ValueControlFields& control_fields = ValueControlFields()) |
73 | | : primitive_value_(primitive_value), |
74 | 628k | control_fields_(control_fields) { |
75 | 628k | } |
76 | | |
77 | 0 | const ValueControlFields& control_fields() const { |
78 | 0 | return control_fields_; |
79 | 0 | } |
80 | | |
81 | 747M | MonoDelta ttl() const { return control_fields_.ttl; } |
82 | | |
83 | 0 | MonoDelta* mutable_ttl() { return &control_fields_.ttl; } |
84 | | |
85 | 2.11M | UserTimeMicros user_timestamp() const { return control_fields_.user_timestamp; } |
86 | | |
87 | 0 | bool has_ttl() const { return control_fields_.has_ttl(); } |
88 | | |
89 | 614M | bool has_user_timestamp() const { return control_fields_.has_user_timestamp(); } |
90 | | |
91 | 2.09G | ValueType value_type() const { return primitive_value_.value_type(); } |
92 | | |
93 | 682M | PrimitiveValue* mutable_primitive_value() { return &primitive_value_; } |
94 | | |
95 | 2.36M | const PrimitiveValue& primitive_value() const { return primitive_value_; } |
96 | | |
97 | 0 | uint64_t merge_flags() const { return control_fields_.merge_flags; } |
98 | | |
99 | 0 | const DocHybridTime& intent_doc_ht() const { return control_fields_.intent_doc_ht; } |
100 | | |
101 | 0 | void set_intent_doc_ht(DocHybridTime intent_doc_ht) { |
102 | 0 | control_fields_.intent_doc_ht = intent_doc_ht; |
103 | 0 | } |
104 | | |
105 | | // Decode the entire value |
106 | | CHECKED_STATUS Decode(const Slice& rocksdb_value); |
107 | | |
108 | | std::string ToString() const; |
109 | | |
110 | | // Decodes the ValueType of the primitive value stored in the |
111 | | // given RocksDB value and any other values before it. |
112 | | static Result<ValueType> DecodePrimitiveValueType(const Slice& rocksdb_value); |
113 | | |
114 | | static const Value& Tombstone(); |
115 | | static const std::string& EncodedTombstone(); |
116 | | |
117 | | static std::string DebugSliceToString(const Slice& encoded_value); |
118 | | |
119 | | static Result<bool> IsTombstoned(const Slice& slice); |
120 | | |
121 | | private: |
122 | | // Consume the timestamp portion of the slice assuming the beginning of the slice points to |
123 | | // the timestamp. |
124 | | PrimitiveValue primitive_value_; |
125 | | ValueControlFields control_fields_; |
126 | | }; |
127 | | |
128 | | } // namespace docdb |
129 | | } // namespace yb |
130 | | |
131 | | #endif // YB_DOCDB_VALUE_H_ |