/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 | 4.14M | PgsqlOp() = default; |
33 | 4.14M | 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 | 2.73M | bool is_write() const { |
43 | 2.73M | return !is_read(); |
44 | 2.73M | } |
45 | | |
46 | 7.53M | PgsqlResponsePB& response() { |
47 | 7.53M | return response_; |
48 | 7.53M | } |
49 | | |
50 | 0 | const PgsqlResponsePB& response() const { |
51 | 0 | return response_; |
52 | 0 | } |
53 | | |
54 | 4.82M | rpc::SidecarPtr& rows_data() { |
55 | 4.82M | return rows_data_; |
56 | 4.82M | } |
57 | | |
58 | 120k | bool is_active() const { |
59 | 120k | return active_; |
60 | 120k | } |
61 | | |
62 | 8.18M | void set_active(bool value) { |
63 | 8.18M | active_ = value; |
64 | 8.18M | } |
65 | | |
66 | 23.9k | void set_read_time(const ReadHybridTime& value) { |
67 | 23.9k | read_time_ = value; |
68 | 23.9k | } |
69 | | |
70 | 4.05M | const ReadHybridTime& read_time() const { |
71 | 4.05M | return read_time_; |
72 | 4.05M | } |
73 | | |
74 | | std::string ToString() const; |
75 | | |
76 | | virtual CHECKED_STATUS InitPartitionKey(const PgTableDesc& table) = 0; |
77 | | |
78 | | private: |
79 | | bool active_ = false; |
80 | | PgsqlResponsePB response_; |
81 | | rpc::SidecarPtr rows_data_; |
82 | | ReadHybridTime read_time_; |
83 | | }; |
84 | | |
85 | | class PgsqlReadOp : public PgsqlOp { |
86 | | public: |
87 | 1.45M | PgsqlReadOp() = default; |
88 | | explicit PgsqlReadOp(const PgTableDesc& desc); |
89 | | |
90 | 15.9M | PgsqlReadRequestPB& read_request() { |
91 | 15.9M | return read_request_; |
92 | 15.9M | } |
93 | | |
94 | 1.64M | const PgsqlReadRequestPB& read_request() const { |
95 | 1.64M | return read_request_; |
96 | 1.64M | } |
97 | | |
98 | 6.50M | bool is_read() const override { |
99 | 6.50M | return true; |
100 | 6.50M | } |
101 | | |
102 | 618k | bool need_transaction() const override { |
103 | 618k | return true; |
104 | 618k | } |
105 | | |
106 | 0 | void set_read_from_followers() { |
107 | 0 | read_from_followers_ = true; |
108 | 0 | } |
109 | | |
110 | 1.89M | bool read_from_followers() const { |
111 | 1.89M | return read_from_followers_; |
112 | 1.89M | } |
113 | | |
114 | 1.45M | PgsqlOpPtr DeepCopy() const { |
115 | 1.45M | auto result = std::make_shared<PgsqlReadOp>(); |
116 | 1.45M | result->read_request() = read_request(); |
117 | 1.45M | result->read_from_followers_ = read_from_followers_; |
118 | 1.45M | return result; |
119 | 1.45M | } |
120 | | |
121 | | private: |
122 | | CHECKED_STATUS InitPartitionKey(const PgTableDesc& table) override; |
123 | | |
124 | | PgsqlReadRequestPB read_request_; |
125 | | bool read_from_followers_ = false; |
126 | | }; |
127 | | |
128 | | using PgsqlReadOpPtr = std::shared_ptr<PgsqlReadOp>; |
129 | | |
130 | | std::shared_ptr<PgsqlReadRequestPB> InitSelect( |
131 | | const PgsqlReadOpPtr& read_op, const PgTableDesc& desc); |
132 | | |
133 | | class PgsqlWriteOp : public PgsqlOp { |
134 | | public: |
135 | 2.12M | explicit PgsqlWriteOp(bool need_transaction) : need_transaction_(need_transaction) {} |
136 | | |
137 | 6.38M | PgsqlWriteRequestPB& write_request() { |
138 | 6.38M | return write_request_; |
139 | 6.38M | } |
140 | | |
141 | 122 | const PgsqlWriteRequestPB& write_request() const { |
142 | 122 | return write_request_; |
143 | 122 | } |
144 | | |
145 | 4.26M | bool is_read() const override { |
146 | 4.26M | return false; |
147 | 4.26M | } |
148 | | |
149 | 2.12M | bool need_transaction() const override { |
150 | 2.12M | return need_transaction_; |
151 | 2.12M | } |
152 | | |
153 | 0 | PgsqlOpPtr DeepCopy() const { |
154 | 0 | auto result = std::make_shared<PgsqlWriteOp>(need_transaction_); |
155 | 0 | result->write_request() = write_request(); |
156 | 0 | return result; |
157 | 0 | } |
158 | | |
159 | 2.23M | HybridTime write_time() const { |
160 | 2.23M | return write_time_; |
161 | 2.23M | } |
162 | | |
163 | 103k | void SetWriteTime(HybridTime value) { |
164 | 103k | write_time_ = value; |
165 | 103k | } |
166 | | |
167 | | private: |
168 | | CHECKED_STATUS InitPartitionKey(const PgTableDesc& table) override; |
169 | | |
170 | | PgsqlWriteRequestPB write_request_; |
171 | | bool need_transaction_; |
172 | | HybridTime write_time_; |
173 | | }; |
174 | | |
175 | | using PgsqlWriteOpPtr = std::shared_ptr<PgsqlWriteOp>; |
176 | | |
177 | | CHECKED_STATUS ReviewResponsePagingState(const PgTableDesc& table, PgsqlReadOp* op); |
178 | | |
179 | | } // namespace pggate |
180 | | } // namespace yb |
181 | | |
182 | | #endif // YB_YQL_PGGATE_PG_OP_H |