/Users/deen/code/yugabyte-db/src/yb/docdb/redis_operation.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_REDIS_OPERATION_H |
15 | | #define YB_DOCDB_REDIS_OPERATION_H |
16 | | |
17 | | #include <boost/optional/optional.hpp> |
18 | | |
19 | | #include "yb/common/redis_protocol.pb.h" |
20 | | |
21 | | #include "yb/docdb/deadline_info.h" |
22 | | #include "yb/docdb/doc_operation.h" |
23 | | #include "yb/docdb/expiration.h" |
24 | | #include "yb/docdb/key_bounds.h" |
25 | | |
26 | | #include "yb/rocksdb/cache.h" |
27 | | |
28 | | namespace yb { |
29 | | namespace docdb { |
30 | | |
31 | | // Redis value data with attached type of this value. |
32 | | // Used internally by RedisWriteOperation. |
33 | | struct RedisValue { |
34 | | RedisDataType type = static_cast<RedisDataType>(0); |
35 | | std::string value; |
36 | | Expiration exp; |
37 | | int64_t internal_index = 0; |
38 | | }; |
39 | | |
40 | | class RedisWriteOperation : |
41 | | public DocOperationBase<DocOperationType::REDIS_WRITE_OPERATION, RedisWriteRequestPB> { |
42 | | public: |
43 | | // Construct a RedisWriteOperation. Content of request will be swapped out by the constructor. |
44 | | explicit RedisWriteOperation(std::reference_wrapper<const RedisWriteRequestPB> request) |
45 | 61.5k | : DocOperationBase(request) { |
46 | 61.5k | } |
47 | | |
48 | 61.5k | bool RequireReadSnapshot() const override { return false; } |
49 | | |
50 | | CHECKED_STATUS Apply(const DocOperationApplyData& data) override; |
51 | | |
52 | | CHECKED_STATUS GetDocPaths( |
53 | | GetDocPathsMode mode, DocPathsToLock *paths, IsolationLevel *level) const override; |
54 | | |
55 | 61.5k | RedisResponsePB &response() { return response_; } |
56 | | |
57 | | private: |
58 | 0 | void ClearResponse() override { |
59 | 0 | response_.Clear(); |
60 | 0 | } |
61 | | |
62 | | void InitializeIterator(const DocOperationApplyData& data); |
63 | | Result<RedisDataType> GetValueType(const DocOperationApplyData& data, |
64 | | int subkey_index = kNilSubkeyIndex); |
65 | | Result<RedisValue> GetValue(const DocOperationApplyData& data, |
66 | | int subkey_index = kNilSubkeyIndex, Expiration* exp = nullptr); |
67 | | |
68 | | CHECKED_STATUS ApplySetTtl(const DocOperationApplyData& data); |
69 | | CHECKED_STATUS ApplySet(const DocOperationApplyData& data); |
70 | | CHECKED_STATUS ApplyGetSet(const DocOperationApplyData& data); |
71 | | CHECKED_STATUS ApplyAppend(const DocOperationApplyData& data); |
72 | | CHECKED_STATUS ApplyDel(const DocOperationApplyData& data); |
73 | | CHECKED_STATUS ApplySetRange(const DocOperationApplyData& data); |
74 | | CHECKED_STATUS ApplyIncr(const DocOperationApplyData& data); |
75 | | CHECKED_STATUS ApplyPush(const DocOperationApplyData& data); |
76 | | CHECKED_STATUS ApplyInsert(const DocOperationApplyData& data); |
77 | | CHECKED_STATUS ApplyPop(const DocOperationApplyData& data); |
78 | | CHECKED_STATUS ApplyAdd(const DocOperationApplyData& data); |
79 | | CHECKED_STATUS ApplyRemove(const DocOperationApplyData& data); |
80 | | |
81 | | RedisResponsePB response_; |
82 | | // TODO: Currently we have a separate iterator per operation, but in future, we leave the option |
83 | | // open for operations to share iterators. |
84 | | std::unique_ptr<IntentAwareIterator> iterator_; |
85 | | |
86 | 92.6k | rocksdb::QueryId redis_query_id() { return reinterpret_cast<rocksdb::QueryId > (&request_); } |
87 | | }; |
88 | | |
89 | | class RedisReadOperation { |
90 | | public: |
91 | | explicit RedisReadOperation(const yb::RedisReadRequestPB& request, |
92 | | const DocDB& doc_db, |
93 | | CoarseTimePoint deadline, |
94 | | const ReadHybridTime& read_time) |
95 | 42.9k | : request_(request), doc_db_(doc_db), deadline_(deadline), read_time_(read_time) {} |
96 | | |
97 | | CHECKED_STATUS Execute(); |
98 | | |
99 | | const RedisResponsePB &response(); |
100 | | |
101 | | private: |
102 | | Result<RedisDataType> GetValueType(int subkey_index = kNilSubkeyIndex); |
103 | | |
104 | | // GetValue when always_override should be true. |
105 | | // This is particularly relevant for the Timeseries datatype, for which |
106 | | // child TTLs override parent TTLs. |
107 | | // TODO: Once the timeseries bug is fixed, this function as well as the |
108 | | // corresponding field can be safely removed. |
109 | | Result<RedisValue>GetOverrideValue(int subkey_index = kNilSubkeyIndex); |
110 | | |
111 | | Result<RedisValue> GetValue(int subkey_index = kNilSubkeyIndex); |
112 | | |
113 | | CHECKED_STATUS ExecuteGet(); |
114 | | CHECKED_STATUS ExecuteGet(const RedisGetRequestPB& get_request); |
115 | | CHECKED_STATUS ExecuteGet(RedisGetRequestPB::GetRequestType type); |
116 | | CHECKED_STATUS ExecuteGetForRename(); |
117 | | CHECKED_STATUS ExecuteGetTtl(); |
118 | | // Used to implement HGETALL, HKEYS, HVALS, SMEMBERS, HLEN, SCARD |
119 | | CHECKED_STATUS ExecuteHGetAllLikeCommands( |
120 | | ValueType value_type, |
121 | | bool add_keys, |
122 | | bool add_values); |
123 | | CHECKED_STATUS ExecuteStrLen(); |
124 | | CHECKED_STATUS ExecuteExists(); |
125 | | CHECKED_STATUS ExecuteGetRange(); |
126 | | CHECKED_STATUS ExecuteCollectionGetRange(); |
127 | | CHECKED_STATUS ExecuteCollectionGetRangeByBounds( |
128 | | RedisCollectionGetRangeRequestPB::GetRangeRequestType request_type, bool add_keys); |
129 | | CHECKED_STATUS ExecuteCollectionGetRangeByBounds( |
130 | | RedisCollectionGetRangeRequestPB::GetRangeRequestType request_type, |
131 | | const RedisSubKeyBoundPB& lower_bound, const RedisSubKeyBoundPB& upper_bound, bool add_keys); |
132 | | CHECKED_STATUS ExecuteKeys(); |
133 | | |
134 | 42.9k | rocksdb::QueryId redis_query_id() { return reinterpret_cast<rocksdb::QueryId> (&request_); } |
135 | | |
136 | | const RedisReadRequestPB& request_; |
137 | | RedisResponsePB response_; |
138 | | const DocDB doc_db_; |
139 | | CoarseTimePoint deadline_; |
140 | | ReadHybridTime read_time_; |
141 | | // TODO: Move iterator_ to a superclass of RedisWriteOperation RedisReadOperation |
142 | | // Make these two classes similar in terms of how rocksdb state is passed to them. |
143 | | // Currently ReadOperations get the state during construction, but Write operations get them when |
144 | | // calling Apply(). Apply() and Execute() should be more similar() in definition. |
145 | | std::unique_ptr<IntentAwareIterator> iterator_; |
146 | | |
147 | | boost::optional<DeadlineInfo> deadline_info_; |
148 | | }; |
149 | | |
150 | | } // namespace docdb |
151 | | } // namespace yb |
152 | | |
153 | | #endif // YB_DOCDB_REDIS_OPERATION_H |