YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/common/ql_rowblock.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
// This file contains the classes that represent a QL row and a row block.
15
16
#include "yb/common/ql_rowblock.h"
17
18
#include "yb/bfql/bfql.h"
19
20
#include "yb/common/ql_protocol_util.h"
21
#include "yb/common/ql_value.h"
22
#include "yb/common/schema.h"
23
24
#include "yb/util/status_log.h"
25
26
namespace yb {
27
28
using std::shared_ptr;
29
using std::string;
30
31
//----------------------------------------- QL row ----------------------------------------
32
QLRow::QLRow(const shared_ptr<const Schema>& schema)
33
4.57M
    : schema_(schema), values_(schema->num_columns()) {
34
4.57M
}
35
36
7.76M
QLRow::QLRow(const QLRow& other) : schema_(other.schema_), values_(other.values_) {
37
7.76M
}
38
39
QLRow::QLRow(QLRow&& other)
40
0
    : schema_(std::move(other.schema_)), values_(std::move(other.values_)) {
41
0
}
42
43
12.2M
QLRow::~QLRow() {
44
12.2M
}
45
46
0
size_t QLRow::column_count() const {
47
0
  return schema_->num_columns();
48
0
}
49
50
// Column's datatype
51
16.3M
const std::shared_ptr<QLType>& QLRow::column_type(const size_t col_idx) const {
52
16.3M
  return schema_->column(col_idx).type();
53
16.3M
}
54
55
724
void QLRow::Serialize(const QLClient client, faststring* buffer) const {
56
2.42k
  for (size_t col_idx = 0; col_idx < schema_->num_columns(); ++col_idx) {
57
1.70k
    values_[col_idx].Serialize(column_type(col_idx), client, buffer);
58
1.70k
  }
59
724
}
60
61
2.01M
Status QLRow::Deserialize(const QLClient client, Slice* data) {
62
18.3M
  for (size_t col_idx = 0; col_idx < schema_->num_columns(); ++col_idx) {
63
16.3M
    RETURN_NOT_OK(values_[col_idx].Deserialize(column_type(col_idx), client, data));
64
16.3M
  }
65
2.01M
  return Status::OK();
66
2.01M
}
67
68
50.0k
string QLRow::ToString() const {
69
50.0k
  string s = "{ ";
70
209k
  for (size_t col_idx = 0; col_idx < schema_->num_columns(); ++col_idx) {
71
159k
    if (col_idx > 0) {
72
109k
      s+= ", ";
73
109k
    }
74
159k
    s += values_[col_idx].ToString();
75
159k
  }
76
50.0k
  s += " }";
77
50.0k
  return s;
78
50.0k
}
79
80
120k
QLRow& QLRow::operator=(const QLRow& other) {
81
120k
  this->~QLRow();
82
120k
  new(this) QLRow(other);
83
120k
  return *this;
84
120k
}
85
86
0
QLRow& QLRow::operator=(QLRow&& other) {
87
0
  this->~QLRow();
88
0
  new(this) QLRow(other);
89
0
  return *this;
90
0
}
91
92
22.3M
const QLValue& QLRow::column(const size_t col_idx) const {
93
22.3M
  return values_[col_idx];
94
22.3M
}
95
96
1.02k
QLValue* QLRow::mutable_column(const size_t col_idx) {
97
1.02k
  return &values_[col_idx];
98
1.02k
}
99
100
0
void QLRow::SetColumnValues(const std::vector<QLValue>& column_values) {
101
0
  values_ = column_values;
102
0
}
103
104
18.2M
void QLRow::SetColumn(size_t col_idx, QLValuePB value) {
105
18.2M
  values_[col_idx] = std::move(value);
106
18.2M
}
107
108
//-------------------------------------- QL row block --------------------------------------
109
QLRowBlock::QLRowBlock(const Schema& schema, const vector<ColumnId>& column_ids)
110
406
    : schema_(new Schema()) {
111
  // TODO: is there a better way to report errors here?
112
406
  CHECK_OK(schema.CreateProjectionByIdsIgnoreMissing(column_ids, schema_.get()));
113
406
}
114
115
442k
QLRowBlock::QLRowBlock(const Schema& schema) : schema_(new Schema(schema)) {
116
442k
}
117
118
441k
QLRowBlock::~QLRowBlock() {
119
441k
}
120
121
4.52M
QLRow& QLRowBlock::Extend() {
122
4.52M
  rows_.emplace_back(schema_);
123
4.52M
  return rows_.back();
124
4.52M
}
125
126
0
void QLRowBlock::Reserve(size_t size) {
127
0
  rows_.reserve(size);
128
0
}
129
130
123k
Status QLRowBlock::AddRow(const QLRow& row) {
131
  // TODO: check for schema compatibility between QLRow and QLRowBlock.
132
123k
  rows_.push_back(row);
133
123k
  return Status::OK();
134
123k
}
135
136
0
string QLRowBlock::ToString() const {
137
0
  string s = "{ ";
138
0
  for (size_t i = 0; i < rows_.size(); i++) {
139
0
    if (i > 0) { s+= ", "; }
140
0
    s += rows_[i].ToString();
141
0
  }
142
0
  s += " }";
143
0
  return s;
144
0
}
145
146
551
void QLRowBlock::Serialize(const QLClient client, faststring* buffer) const {
147
551
  CHECK_EQ(client, YQL_CLIENT_CQL);
148
551
  CQLEncodeLength(rows_.size(), buffer);
149
723
  for (const auto& row : rows_) {
150
723
    row.Serialize(client, buffer);
151
723
  }
152
551
}
153
154
191k
Status QLRowBlock::Deserialize(const QLClient client, Slice* data) {
155
191k
  CHECK_EQ(client, YQL_CLIENT_CQL);
156
191k
  int32_t count = 0;
157
191k
  RETURN_NOT_OK(CQLDecodeNum(sizeof(count), NetworkByteOrder::Load32, data, &count));
158
159
2.20M
  for (int32_t i = 0; i < count; ++i) {
160
2.01M
    RETURN_NOT_OK(Extend().Deserialize(client, data));
161
2.01M
  }
162
191k
  if (!data->empty()) {
163
0
    return STATUS(Corruption, "Extra data at the end of row block");
164
0
  }
165
191k
  return Status::OK();
166
191k
}
167
168
3.93M
Result<size_t> QLRowBlock::GetRowCount(const QLClient client, const string& data) {
169
3.93M
  CHECK_EQ(client, YQL_CLIENT_CQL);
170
3.93M
  Slice slice(data);
171
3.93M
  return VERIFY_RESULT(CQLDecodeLength(&slice));
172
3.93M
}
173
174
37.8k
Status QLRowBlock::AppendRowsData(const QLClient client, const string& src, string* dst) {
175
37.8k
  CHECK_EQ(client, YQL_CLIENT_CQL);
176
37.8k
  Slice src_slice(src);
177
37.8k
  const int32_t src_cnt = VERIFY_RESULT(CQLDecodeLength(&src_slice));
178
37.8k
  if (src_cnt > 0) {
179
9.06k
    Slice dst_slice(*dst);
180
9.06k
    int32_t dst_cnt = VERIFY_RESULT(CQLDecodeLength(&dst_slice));
181
9.06k
    if (dst_cnt == 0) {
182
2.26k
      *dst = src;
183
6.79k
    } else {
184
6.79k
      dst->append(src_slice.cdata(), src_slice.size());
185
6.79k
      dst_cnt += src_cnt;
186
6.79k
      CQLEncodeLength(dst_cnt, &(*dst)[0]);
187
6.79k
    }
188
9.06k
  }
189
37.8k
  return Status::OK();
190
37.8k
}
191
192
62
string QLRowBlock::ZeroRowsData(const QLClient client) {
193
62
  CHECK_EQ(client, YQL_CLIENT_CQL);
194
62
  return string(sizeof(int32_t), 0); // Encode 32-bit 0 length.
195
62
}
196
197
} // namespace yb