/Users/deen/code/yugabyte-db/src/yb/yql/pggate/pg_op.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_YQL_PGGATE_PG_OP_H |
15 | | #define YB_YQL_PGGATE_PG_OP_H |
16 | | |
17 | | #include "yb/util/status.h" |
18 | | |
19 | | #include "yb/common/common_fwd.h" |
20 | | #include "yb/common/pgsql_protocol.pb.h" |
21 | | #include "yb/common/read_hybrid_time.h" |
22 | | |
23 | | #include "yb/rpc/rpc_fwd.h" |
24 | | |
25 | | #include "yb/yql/pggate/pg_gate_fwd.h" |
26 | | |
27 | | namespace yb { |
28 | | namespace pggate { |
29 | | |
30 | | class PgsqlOp { |
31 | | public: |
32 | 11.7M | PgsqlOp() = default; |
33 | 11.7M | virtual ~PgsqlOp() = default; |
34 | | |
35 | | PgsqlOp(const PgsqlOp&) = delete; |
36 | | void operator=(const PgsqlOp&) = delete; |
37 | | |
38 | | virtual bool is_read() const = 0; |
39 | | |
40 | | virtual bool need_transaction() const = 0; |
41 | | |
42 | 9.59M | bool is_write() const { |
43 | 9.59M | return !is_read(); |
44 | 9.59M | } |
45 | | |
46 | 19.8M | PgsqlResponsePB& response() { |
47 | 19.8M | return response_; |
48 | 19.8M | } |
49 | | |
50 | 0 | const PgsqlResponsePB& response() const { |
51 | 0 | return response_; |
52 | 0 | } |
53 | | |
54 | 13.5M | rpc::SidecarPtr& rows_data() { |
55 | 13.5M | return rows_data_; |
56 | 13.5M | } |
57 | | |
58 | 316k | bool is_active() const { |
59 | 316k | return active_; |
60 | 316k | } |
61 | | |
62 | 20.3M | void set_active(bool value) { |
63 | 20.3M | active_ = value; |
64 | 20.3M | } |
65 | | |
66 | 64.7k | void set_read_time(const ReadHybridTime& value) { |
67 | 64.7k | read_time_ = value; |
68 | 64.7k | } |
69 | | |
70 | 11.6M | const ReadHybridTime& read_time() const { |
71 | 11.6M | return read_time_; |
72 | 11.6M | } |
73 | | |
74 | | std::string ToString() const; |
75 | | |
76 | | virtual CHECKED_STATUS InitPartitionKey(const PgTableDesc& table) = 0; |
77 | | |
78 | | private: |
79 | | virtual std::string RequestToString() const = 0; |
80 | | |
81 | | bool active_ = false; |
82 | | PgsqlResponsePB response_; |
83 | | rpc::SidecarPtr rows_data_; |
84 | | ReadHybridTime read_time_; |
85 | | }; |
86 | | |
87 | | class PgsqlReadOp : public PgsqlOp { |
88 | | public: |
89 | 2.95M | PgsqlReadOp() = default; |
90 | | explicit PgsqlReadOp(const PgTableDesc& desc); |
91 | | |
92 | 37.4M | PgsqlReadRequestPB& read_request() { |
93 | 37.4M | return read_request_; |
94 | 37.4M | } |
95 | | |
96 | 3.52M | const PgsqlReadRequestPB& read_request() const { |
97 | 3.52M | return read_request_; |
98 | 3.52M | } |
99 | | |
100 | 15.3M | bool is_read() const override { |
101 | 15.3M | return true; |
102 | 15.3M | } |
103 | | |
104 | 6.07M | bool need_transaction() const override { |
105 | 6.07M | return true; |
106 | 6.07M | } |
107 | | |
108 | 27 | void set_read_from_followers() { |
109 | 27 | read_from_followers_ = true; |
110 | 27 | } |
111 | | |
112 | 4.35M | bool read_from_followers() const { |
113 | 4.35M | return read_from_followers_; |
114 | 4.35M | } |
115 | | |
116 | 2.95M | PgsqlOpPtr DeepCopy() const { |
117 | 2.95M | auto result = std::make_shared<PgsqlReadOp>(); |
118 | 2.95M | result->read_request() = read_request(); |
119 | 2.95M | result->read_from_followers_ = read_from_followers_; |
120 | 2.95M | return result; |
121 | 2.95M | } |
122 | | |
123 | | std::string RequestToString() const override; |
124 | | |
125 | | private: |
126 | | CHECKED_STATUS InitPartitionKey(const PgTableDesc& table) override; |
127 | | |
128 | | PgsqlReadRequestPB read_request_; |
129 | | bool read_from_followers_ = false; |
130 | | }; |
131 | | |
132 | | using PgsqlReadOpPtr = std::shared_ptr<PgsqlReadOp>; |
133 | | |
134 | | std::shared_ptr<PgsqlReadRequestPB> InitSelect( |
135 | | const PgsqlReadOpPtr& read_op, const PgTableDesc& desc); |
136 | | |
137 | | class PgsqlWriteOp : public PgsqlOp { |
138 | | public: |
139 | 7.20M | explicit PgsqlWriteOp(bool need_transaction) : need_transaction_(need_transaction) {} |
140 | | |
141 | 21.5M | PgsqlWriteRequestPB& write_request() { |
142 | 21.5M | return write_request_; |
143 | 21.5M | } |
144 | | |
145 | 601 | const PgsqlWriteRequestPB& write_request() const { |
146 | 601 | return write_request_; |
147 | 601 | } |
148 | | |
149 | 14.4M | bool is_read() const override { |
150 | 14.4M | return false; |
151 | 14.4M | } |
152 | | |
153 | 14.4M | bool need_transaction() const override { |
154 | 14.4M | return need_transaction_; |
155 | 14.4M | } |
156 | | |
157 | 0 | PgsqlOpPtr DeepCopy() const { |
158 | 0 | auto result = std::make_shared<PgsqlWriteOp>(need_transaction_); |
159 | 0 | result->write_request() = write_request(); |
160 | 0 | return result; |
161 | 0 | } |
162 | | |
163 | 7.57M | HybridTime write_time() const { |
164 | 7.57M | return write_time_; |
165 | 7.57M | } |
166 | | |
167 | 371k | void SetWriteTime(HybridTime value) { |
168 | 371k | write_time_ = value; |
169 | 371k | } |
170 | | |
171 | | std::string RequestToString() const override; |
172 | | |
173 | | private: |
174 | | CHECKED_STATUS InitPartitionKey(const PgTableDesc& table) override; |
175 | | |
176 | | PgsqlWriteRequestPB write_request_; |
177 | | bool need_transaction_; |
178 | | HybridTime write_time_; |
179 | | }; |
180 | | |
181 | | using PgsqlWriteOpPtr = std::shared_ptr<PgsqlWriteOp>; |
182 | | |
183 | | CHECKED_STATUS ReviewResponsePagingState(const PgTableDesc& table, PgsqlReadOp* op); |
184 | | |
185 | | } // namespace pggate |
186 | | } // namespace yb |
187 | | |
188 | | #endif // YB_YQL_PGGATE_PG_OP_H |