/Users/deen/code/yugabyte-db/src/yb/client/ql-dml-test-base.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_CLIENT_QL_DML_TEST_BASE_H |
15 | | #define YB_CLIENT_QL_DML_TEST_BASE_H |
16 | | |
17 | | #include <algorithm> |
18 | | #include <functional> |
19 | | #include <vector> |
20 | | |
21 | | #include <gtest/gtest.h> |
22 | | |
23 | | #include "yb/client/callbacks.h" |
24 | | #include "yb/client/table_handle.h" |
25 | | #include "yb/common/ql_protocol.pb.h" |
26 | | #include "yb/common/ql_rowblock.h" |
27 | | |
28 | | #include "yb/server/server_fwd.h" |
29 | | |
30 | | #include "yb/integration-tests/external_mini_cluster.h" |
31 | | #include "yb/integration-tests/mini_cluster.h" |
32 | | #include "yb/integration-tests/yb_mini_cluster_test_base.h" |
33 | | #include "yb/master/mini_master.h" |
34 | | #include "yb/tablet/tablet_fwd.h" |
35 | | #include "yb/util/result.h" |
36 | | #include "yb/util/test_util.h" |
37 | | |
38 | | namespace yb { |
39 | | namespace client { |
40 | | |
41 | | extern const client::YBTableName kTableName; |
42 | | |
43 | | template <class MiniClusterType> |
44 | | class QLDmlTestBase : public MiniClusterTestWithClient<MiniClusterType> { |
45 | | public: |
46 | | QLDmlTestBase(); |
47 | | void SetUp() override; |
48 | | void DoTearDown() override; |
49 | | |
50 | 12 | virtual ~QLDmlTestBase() {} yb::client::QLDmlTestBase<yb::MiniCluster>::~QLDmlTestBase() Line | Count | Source | 50 | 6 | virtual ~QLDmlTestBase() {} |
yb::client::QLDmlTestBase<yb::ExternalMiniCluster>::~QLDmlTestBase() Line | Count | Source | 50 | 6 | virtual ~QLDmlTestBase() {} |
|
51 | | |
52 | | protected: |
53 | | virtual void SetFlags(); |
54 | | void StartCluster(); |
55 | | |
56 | | using MiniClusterTestWithClient<MiniClusterType>::client_; |
57 | | typename MiniClusterType::Options mini_cluster_opt_; |
58 | | }; |
59 | | |
60 | | YB_STRONGLY_TYPED_BOOL(Transactional); |
61 | | YB_DEFINE_ENUM(WriteOpType, (INSERT)(UPDATE)(DELETE)); |
62 | | YB_STRONGLY_TYPED_BOOL(Flush); |
63 | | |
64 | | namespace kv_table_test { |
65 | | |
66 | | constexpr const auto kKeyColumn = "key"; |
67 | | constexpr const auto kValueColumn = "value"; |
68 | | |
69 | | YB_DEFINE_ENUM(Partitioning, (kHash)(kRange)) |
70 | | |
71 | | void BuildSchema(Partitioning partitioning, Schema* schema); |
72 | | |
73 | | CHECKED_STATUS CreateTable( |
74 | | const Schema& schema, int num_tablets, YBClient* client, |
75 | | TableHandle* table, const YBTableName& table_name = kTableName); |
76 | | |
77 | | void CreateTable( |
78 | | Transactional transactional, int num_tablets, YBClient* client, TableHandle* table, |
79 | | const YBTableName& table_name = kTableName); |
80 | | |
81 | | void CreateIndex( |
82 | | Transactional transactional, int indexed_column_index, bool use_mangled_names, |
83 | | const TableHandle& table, YBClient* client, TableHandle* index); |
84 | | |
85 | | // Insert/update a full, single row, equivalent to the statement below. Return a YB write op that |
86 | | // has been applied. |
87 | | // op_type == WriteOpType::INSERT: insert into t values (key, value); |
88 | | // op_type == WriteOpType::UPDATE: update t set v=value where k=key; |
89 | | // op_type == WriteOpType::DELETE: delete from t where k=key; (parameter "value" is unused). |
90 | | Result<YBqlWriteOpPtr> WriteRow( |
91 | | TableHandle* table, const YBSessionPtr& session, int32_t key, int32_t value, |
92 | | const WriteOpType op_type = WriteOpType::INSERT, Flush flush = Flush::kTrue); |
93 | | |
94 | | Result<YBqlWriteOpPtr> DeleteRow(TableHandle* table, const YBSessionPtr& session, int32_t key); |
95 | | |
96 | | Result<YBqlWriteOpPtr> UpdateRow( |
97 | | TableHandle* table, const YBSessionPtr& session, int32_t key, int32_t value); |
98 | | |
99 | | // Select the specified columns of a row using a primary key, equivalent to the select statement |
100 | | // below. Return a YB read op that has been applied. |
101 | | // select <columns...> from t where h1 = <h1> and h2 = <h2> and r1 = <r1> and r2 = <r2>; |
102 | | Result<int32_t> SelectRow( |
103 | | TableHandle* table, const YBSessionPtr& session, int32_t key, |
104 | | const std::string& column = kValueColumn); |
105 | | |
106 | | // Selects all rows from test table, returning map key => value. |
107 | | Result<std::map<int32_t, int32_t>> SelectAllRows(TableHandle* table, const YBSessionPtr& session); |
108 | | |
109 | | Result<YBqlWriteOpPtr> Increment( |
110 | | TableHandle* table, const YBSessionPtr& session, int32_t key, int32_t delta = 1, |
111 | | Flush flush = Flush::kFalse); |
112 | | |
113 | | } // namespace kv_table_test |
114 | | |
115 | | template <class MiniClusterType> |
116 | | class KeyValueTableTest : public QLDmlTestBase<MiniClusterType> { |
117 | | protected: |
118 | | void CreateTable(Transactional transactional); |
119 | | |
120 | | CHECKED_STATUS CreateTable(const Schema& schema); |
121 | | |
122 | | void CreateIndex(Transactional transactional, |
123 | | int indexed_column_index = 1, |
124 | | bool use_mangled_names = true); |
125 | | |
126 | | void PrepareIndex(Transactional transactional, |
127 | | const YBTableName& index_name, |
128 | | int indexed_column_index = 1, |
129 | | bool use_mangled_names = true); |
130 | | |
131 | | Result<YBqlWriteOpPtr> WriteRow( |
132 | | const YBSessionPtr& session, int32_t key, int32_t value, |
133 | | const WriteOpType op_type = WriteOpType::INSERT, |
134 | 0 | Flush flush = Flush::kTrue) { |
135 | 0 | return kv_table_test::WriteRow(&table_, session, key, value, op_type, flush); |
136 | 0 | } Unexecuted instantiation: yb::client::KeyValueTableTest<yb::MiniCluster>::WriteRow(std::__1::shared_ptr<yb::client::YBSession> const&, int, int, yb::client::WriteOpType, yb::StronglyTypedBool<yb::client::Flush_Tag>) Unexecuted instantiation: yb::client::KeyValueTableTest<yb::ExternalMiniCluster>::WriteRow(std::__1::shared_ptr<yb::client::YBSession> const&, int, int, yb::client::WriteOpType, yb::StronglyTypedBool<yb::client::Flush_Tag>) |
137 | | |
138 | 0 | Result<YBqlWriteOpPtr> DeleteRow(const YBSessionPtr& session, int32_t key) { |
139 | 0 | return kv_table_test::DeleteRow(&table_, session, key); |
140 | 0 | } Unexecuted instantiation: yb::client::KeyValueTableTest<yb::MiniCluster>::DeleteRow(std::__1::shared_ptr<yb::client::YBSession> const&, int) Unexecuted instantiation: yb::client::KeyValueTableTest<yb::ExternalMiniCluster>::DeleteRow(std::__1::shared_ptr<yb::client::YBSession> const&, int) |
141 | | |
142 | 0 | Result<YBqlWriteOpPtr> UpdateRow(const YBSessionPtr& session, int32_t key, int32_t value) { |
143 | 0 | return kv_table_test::UpdateRow(&table_, session, key, value); |
144 | 0 | } Unexecuted instantiation: yb::client::KeyValueTableTest<yb::MiniCluster>::UpdateRow(std::__1::shared_ptr<yb::client::YBSession> const&, int, int) Unexecuted instantiation: yb::client::KeyValueTableTest<yb::ExternalMiniCluster>::UpdateRow(std::__1::shared_ptr<yb::client::YBSession> const&, int, int) |
145 | | |
146 | | // Select the specified columns of a row using a primary key, equivalent to the select statement |
147 | | // below. Return a YB read op that has been applied. |
148 | | // select <columns...> from t where h1 = <h1> and h2 = <h2> and r1 = <r1> and r2 = <r2>; |
149 | | Result<int32_t> SelectRow(const YBSessionPtr& session, int32_t key, |
150 | 0 | const std::string& column = kValueColumn) { |
151 | 0 | return kv_table_test::SelectRow(&table_, session, key, column); |
152 | 0 | } Unexecuted instantiation: yb::client::KeyValueTableTest<yb::MiniCluster>::SelectRow(std::__1::shared_ptr<yb::client::YBSession> const&, int, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) Unexecuted instantiation: yb::client::KeyValueTableTest<yb::ExternalMiniCluster>::SelectRow(std::__1::shared_ptr<yb::client::YBSession> const&, int, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) |
153 | | |
154 | | YBSessionPtr CreateSession(const YBTransactionPtr& transaction = nullptr, |
155 | | const server::ClockPtr& clock = nullptr); |
156 | | |
157 | | // Selects all rows from test table, returning map key => value. |
158 | 0 | Result<std::map<int32_t, int32_t>> SelectAllRows(const YBSessionPtr& session) { |
159 | 0 | return kv_table_test::SelectAllRows(&table_, session); |
160 | 0 | } Unexecuted instantiation: yb::client::KeyValueTableTest<yb::MiniCluster>::SelectAllRows(std::__1::shared_ptr<yb::client::YBSession> const&) Unexecuted instantiation: yb::client::KeyValueTableTest<yb::ExternalMiniCluster>::SelectAllRows(std::__1::shared_ptr<yb::client::YBSession> const&) |
161 | | |
162 | | virtual int NumTablets(); |
163 | | |
164 | | // Sets number of tablets to use for test table creation. |
165 | 70 | void SetNumTablets(int num_tablets) { |
166 | 70 | num_tablets_ = num_tablets; |
167 | 70 | } yb::client::KeyValueTableTest<yb::MiniCluster>::SetNumTablets(int) Line | Count | Source | 165 | 54 | void SetNumTablets(int num_tablets) { | 166 | 54 | num_tablets_ = num_tablets; | 167 | 54 | } |
yb::client::KeyValueTableTest<yb::ExternalMiniCluster>::SetNumTablets(int) Line | Count | Source | 165 | 16 | void SetNumTablets(int num_tablets) { | 166 | 16 | num_tablets_ = num_tablets; | 167 | 16 | } |
|
168 | | |
169 | | using MiniClusterTestWithClient<MiniClusterType>::client_; |
170 | | |
171 | | static const std::string kKeyColumn; |
172 | | static const std::string kValueColumn; |
173 | | TableHandle table_; |
174 | | TableHandle index_; |
175 | | int num_tablets_ = CalcNumTablets(3); |
176 | | }; |
177 | | |
178 | | extern template class KeyValueTableTest<MiniCluster>; |
179 | | extern template class KeyValueTableTest<ExternalMiniCluster>; |
180 | | |
181 | | CHECKED_STATUS CheckOp(YBqlOp* op); |
182 | | |
183 | | } // namespace client |
184 | | } // namespace yb |
185 | | |
186 | | #endif // YB_CLIENT_QL_DML_TEST_BASE_H |