YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/client/client-test-util.cc
Line
Count
Source (jump to first uncovered line)
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
//
18
// The following only applies to changes made to this file as part of YugaByte development.
19
//
20
// Portions Copyright (c) YugaByte, Inc.
21
//
22
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
23
// in compliance with the License.  You may obtain a copy of the License at
24
//
25
// http://www.apache.org/licenses/LICENSE-2.0
26
//
27
// Unless required by applicable law or agreed to in writing, software distributed under the License
28
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
29
// or implied.  See the License for the specific language governing permissions and limitations
30
// under the License.
31
//
32
33
#include "yb/client/client-test-util.h"
34
35
#include <stdint.h>
36
37
#include <memory>
38
#include <mutex>
39
#include <string>
40
#include <utility>
41
#include <vector>
42
43
#include <boost/function.hpp>
44
45
#include "yb/client/client_fwd.h"
46
#include "yb/client/error.h"
47
#include "yb/client/schema.h"
48
#include "yb/client/session.h"
49
#include "yb/client/table_handle.h"
50
#include "yb/client/yb_op.h"
51
52
#include "yb/common/common.pb.h"
53
#include "yb/common/ql_type.h"
54
#include "yb/common/ql_value.h"
55
56
#include "yb/util/enums.h"
57
#include "yb/util/monotime.h"
58
#include "yb/util/status.h"
59
#include "yb/util/status_callback.h"
60
#include "yb/util/status_log.h"
61
#include "yb/util/strongly_typed_bool.h"
62
63
namespace yb {
64
namespace client {
65
66
0
void LogSessionErrorsAndDie(const FlushStatus& flush_status) {
67
0
  const auto& s = flush_status.status;
68
0
  CHECK(!s.ok());
69
0
  const auto& errors = flush_status.errors;
70
71
  // Log only the first 10 errors.
72
0
  LOG(INFO) << errors.size() << " failed ops. First 10 errors follow";
73
0
  int i = 0;
74
0
  for (const auto& e : errors) {
75
0
    if (i == 10) {
76
0
      break;
77
0
    }
78
0
    LOG(INFO) << "Op " << e->failed_op().ToString()
79
0
              << " had status " << e->status().ToString();
80
0
    i++;
81
0
  }
82
0
  CHECK_OK(s); // will fail
83
0
}
84
85
void FlushSessionOrDie(const std::shared_ptr<YBSession>& session,
86
3.39k
                       const std::vector<std::shared_ptr<YBqlOp>>& ops) {
87
3.39k
  auto flush_status = session->FlushAndGetOpsErrors();
88
3.39k
  if (PREDICT_FALSE(!flush_status.status.ok())) {
89
0
    LogSessionErrorsAndDie(flush_status);
90
0
  }
91
84.8k
  for (auto& op : ops) {
92
0
    CHECK_EQ(QLResponsePB::YQL_STATUS_OK, op->response().status())
93
0
        << "Status: " << QLResponsePB::QLStatus_Name(op->response().status());
94
84.8k
  }
95
3.39k
}
96
97
21
void ScanTableToStrings(const TableHandle& table, std::vector<std::string>* row_strings) {
98
21
  row_strings->clear();
99
47
  for (const auto& row : TableRange(table)) {
100
47
    row_strings->push_back(row.ToString());
101
47
  }
102
21
}
103
104
0
std::vector<std::string> ScanTableToStrings(const TableHandle& table) {
105
0
  std::vector<std::string> result;
106
0
  ScanTableToStrings(table, &result);
107
0
  return result;
108
0
}
109
110
0
int64_t CountTableRows(const TableHandle& table) {
111
0
  std::vector<std::string> rows;
112
0
  ScanTableToStrings(table, &rows);
113
0
  return rows.size();
114
0
}
115
116
0
std::vector<std::string> ScanToStrings(const TableRange& range) {
117
0
  std::vector<std::pair<int32_t, std::string>> rows;
118
0
  for (const auto& row : range) {
119
0
    rows.emplace_back(row.column(0).int32_value(), row.ToString());
120
0
  }
121
0
  std::sort(rows.begin(), rows.end(),
122
0
            [](const auto& lhs, const auto& rhs) { return lhs.first < rhs.first; });
123
0
  std::vector<std::string> result;
124
0
  result.reserve(rows.size());
125
0
  for (auto& row : rows) {
126
0
    result.emplace_back(std::move(row.second));
127
0
  }
128
0
  return result;
129
0
}
130
131
57
YBSchema YBSchemaFromSchema(const Schema& schema) {
132
57
  return YBSchema(schema);
133
57
}
134
135
std::shared_ptr<YBqlReadOp> CreateReadOp(
136
63
    int32_t key, const TableHandle& table, const std::string& value_column) {
137
63
  auto op = table.NewReadOp();
138
63
  auto req = op->mutable_request();
139
63
  QLAddInt32HashValue(req, key);
140
63
  auto value_column_id = table.ColumnId(value_column);
141
63
  req->add_selected_exprs()->set_column_id(value_column_id);
142
63
  req->mutable_column_refs()->add_ids(value_column_id);
143
144
63
  QLRSColDescPB *rscol_desc = req->mutable_rsrow_desc()->add_rscol_descs();
145
63
  rscol_desc->set_name(value_column);
146
63
  table.ColumnType(value_column)->ToQLTypePB(rscol_desc->mutable_ql_type());
147
63
  return op;
148
63
}
149
150
}  // namespace client
151
}  // namespace yb