/Users/deen/code/yugabyte-db/src/yb/tablet/tablet-test-util.cc
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 | | #include "yb/tablet/tablet-test-util.h" |
15 | | |
16 | | #include "yb/common/ql_expr.h" |
17 | | #include "yb/common/ql_value.h" |
18 | | |
19 | | #include "yb/docdb/ql_rowwise_iterator_interface.h" |
20 | | |
21 | | #include "yb/gutil/strings/join.h" |
22 | | |
23 | | #include "yb/tablet/operations/change_metadata_operation.h" |
24 | | #include "yb/tablet/tablet.h" |
25 | | #include "yb/tablet/tablet_metadata.h" |
26 | | |
27 | | #include "yb/tserver/tserver_admin.pb.h" |
28 | | |
29 | | #include "yb/util/status_log.h" |
30 | | |
31 | | DECLARE_bool(enable_data_block_fsync); |
32 | | |
33 | | namespace yb { |
34 | | namespace tablet { |
35 | | |
36 | | YBTabletTest::YBTabletTest(const Schema& schema, TableType table_type) |
37 | | : schema_(schema), |
38 | | client_schema_(schema), |
39 | 68 | table_type_(table_type) { |
40 | 68 | const_cast<Schema&>(schema_).InitColumnIdsByDefault(); |
41 | | // Keep unit tests fast, but only if no one has set the flag explicitly. |
42 | 68 | if (google::GetCommandLineFlagInfoOrDie("enable_data_block_fsync").is_default) { |
43 | 68 | FLAGS_enable_data_block_fsync = false; |
44 | 68 | } |
45 | 68 | } |
46 | | |
47 | 68 | void YBTabletTest::SetUp() { |
48 | 68 | YBTest::SetUp(); |
49 | | |
50 | 68 | SetUpTestTablet(); |
51 | 68 | } |
52 | | |
53 | 74 | void YBTabletTest::CreateTestTablet(const std::string& root_dir) { |
54 | 74 | string dir = root_dir.empty() ? GetTestPath("fs_root") : root_dir0 ; |
55 | 74 | TabletHarness::Options opts(dir); |
56 | 74 | opts.enable_metrics = true; |
57 | 74 | opts.table_type = table_type_; |
58 | 74 | bool first_time = harness_ == NULL; |
59 | 74 | harness_.reset(new TabletHarness(schema_, opts)); |
60 | 74 | CHECK_OK(harness_->Create(first_time)); |
61 | 74 | } |
62 | | |
63 | 68 | void YBTabletTest::SetUpTestTablet(const std::string& root_dir) { |
64 | 68 | CreateTestTablet(root_dir); |
65 | 68 | CHECK_OK(harness_->Open()); |
66 | 68 | } |
67 | | |
68 | 2 | void YBTabletTest::AlterSchema(const Schema& schema) { |
69 | 2 | ChangeMetadataRequestPB req; |
70 | 2 | req.set_schema_version(tablet()->metadata()->schema_version() + 1); |
71 | | |
72 | 2 | ChangeMetadataOperation operation(nullptr, nullptr, &req); |
73 | 2 | ASSERT_OK(tablet()->CreatePreparedChangeMetadata(&operation, &schema)); |
74 | 2 | ASSERT_OK(tablet()->AlterSchema(&operation)); |
75 | 2 | operation.Release(); |
76 | 2 | } |
77 | | |
78 | | Status IterateToStringList( |
79 | 34 | docdb::YQLRowwiseIteratorIf* iter, std::vector<std::string> *out, int limit) { |
80 | 34 | out->clear(); |
81 | 34 | Schema schema = iter->schema(); |
82 | 34 | int fetched = 0; |
83 | 34 | std::vector<std::pair<QLValue, std::string>> temp; |
84 | 34 | QLTableRow row; |
85 | 5.19k | while (VERIFY_RESULT(iter->HasNext()) && fetched < limit5.16k ) { |
86 | 5.16k | RETURN_NOT_OK(iter->NextRow(&row)); |
87 | 5.16k | QLValue key; |
88 | 5.16k | RETURN_NOT_OK(row.GetValue(schema.column_id(0), &key)); |
89 | 5.16k | temp.emplace_back(key, row.ToString(schema)); |
90 | 5.16k | fetched++; |
91 | 5.16k | } |
92 | 53.4k | std::sort(temp.begin(), temp.end(), [](const auto& lhs, const auto& rhs) 34 { |
93 | 53.4k | return lhs.first < rhs.first; |
94 | 53.4k | }); |
95 | 5.16k | for (auto& p : temp) { |
96 | 5.16k | out->push_back(std::move(p.second)); |
97 | 5.16k | } |
98 | 34 | return Status::OK(); |
99 | 34 | } |
100 | | |
101 | | // Dump all of the rows of the tablet into the given vector. |
102 | 10 | Status DumpTablet(const Tablet& tablet, const Schema& projection, std::vector<std::string>* out) { |
103 | 10 | auto iter = tablet.NewRowIterator(projection); |
104 | 10 | RETURN_NOT_OK(iter); |
105 | 10 | std::vector<string> rows; |
106 | 10 | RETURN_NOT_OK(IterateToStringList(iter->get(), &rows)); |
107 | 10 | std::sort(rows.begin(), rows.end()); |
108 | 10 | out->swap(rows); |
109 | 10 | return Status::OK(); |
110 | 10 | } |
111 | | |
112 | | } // namespace tablet |
113 | | } // namespace yb |