/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.48M | : schema_(schema), values_(schema->num_columns()) { |
34 | 4.48M | } |
35 | | |
36 | 7.57M | QLRow::QLRow(const QLRow& other) : schema_(other.schema_), values_(other.values_) { |
37 | 7.57M | } |
38 | | |
39 | | QLRow::QLRow(QLRow&& other) |
40 | 0 | : schema_(std::move(other.schema_)), values_(std::move(other.values_)) { |
41 | 0 | } |
42 | | |
43 | 11.9M | QLRow::~QLRow() { |
44 | 11.9M | } |
45 | | |
46 | 0 | size_t QLRow::column_count() const { |
47 | 0 | return schema_->num_columns(); |
48 | 0 | } |
49 | | |
50 | | // Column's datatype |
51 | 15.6M | const std::shared_ptr<QLType>& QLRow::column_type(const size_t col_idx) const { |
52 | 15.6M | return schema_->column(col_idx).type(); |
53 | 15.6M | } |
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_idx1.70k ) { |
57 | 1.70k | values_[col_idx].Serialize(column_type(col_idx), client, buffer); |
58 | 1.70k | } |
59 | 724 | } |
60 | | |
61 | 1.89M | Status QLRow::Deserialize(const QLClient client, Slice* data) { |
62 | 17.5M | for (size_t col_idx = 0; col_idx < schema_->num_columns(); ++col_idx15.6M ) { |
63 | 15.6M | RETURN_NOT_OK(values_[col_idx].Deserialize(column_type(col_idx), client, data)); |
64 | 15.6M | } |
65 | 1.89M | return Status::OK(); |
66 | 1.89M | } |
67 | | |
68 | 49.9k | string QLRow::ToString() const { |
69 | 49.9k | string s = "{ "; |
70 | 209k | for (size_t col_idx = 0; col_idx < schema_->num_columns(); ++col_idx159k ) { |
71 | 159k | if (col_idx > 0) { |
72 | 110k | s+= ", "; |
73 | 110k | } |
74 | 159k | s += values_[col_idx].ToString(); |
75 | 159k | } |
76 | 49.9k | s += " }"; |
77 | 49.9k | return s; |
78 | 49.9k | } |
79 | | |
80 | 66.9k | QLRow& QLRow::operator=(const QLRow& other) { |
81 | 66.9k | this->~QLRow(); |
82 | 66.9k | new(this) QLRow(other); |
83 | 66.9k | return *this; |
84 | 66.9k | } |
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.2M | const QLValue& QLRow::column(const size_t col_idx) const { |
93 | 22.2M | return values_[col_idx]; |
94 | 22.2M | } |
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.4M | void QLRow::SetColumn(size_t col_idx, QLValuePB value) { |
105 | 18.4M | values_[col_idx] = std::move(value); |
106 | 18.4M | } |
107 | | |
108 | | //-------------------------------------- QL row block -------------------------------------- |
109 | | QLRowBlock::QLRowBlock(const Schema& schema, const vector<ColumnId>& column_ids) |
110 | 432 | : schema_(new Schema()) { |
111 | | // TODO: is there a better way to report errors here? |
112 | 432 | CHECK_OK(schema.CreateProjectionByIdsIgnoreMissing(column_ids, schema_.get())); |
113 | 432 | } |
114 | | |
115 | 439k | QLRowBlock::QLRowBlock(const Schema& schema) : schema_(new Schema(schema)) { |
116 | 439k | } |
117 | | |
118 | 437k | QLRowBlock::~QLRowBlock() { |
119 | 437k | } |
120 | | |
121 | 4.41M | QLRow& QLRowBlock::Extend() { |
122 | 4.41M | rows_.emplace_back(schema_); |
123 | 4.41M | return rows_.back(); |
124 | 4.41M | } |
125 | | |
126 | 0 | void QLRowBlock::Reserve(size_t size) { |
127 | 0 | rows_.reserve(size); |
128 | 0 | } |
129 | | |
130 | 153k | Status QLRowBlock::AddRow(const QLRow& row) { |
131 | | // TODO: check for schema compatibility between QLRow and QLRowBlock. |
132 | 153k | rows_.push_back(row); |
133 | 153k | return Status::OK(); |
134 | 153k | } |
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 | 573 | void QLRowBlock::Serialize(const QLClient client, faststring* buffer) const { |
147 | 573 | CHECK_EQ(client, YQL_CLIENT_CQL); |
148 | 573 | CQLEncodeLength(rows_.size(), buffer); |
149 | 724 | for (const auto& row : rows_) { |
150 | 724 | row.Serialize(client, buffer); |
151 | 724 | } |
152 | 573 | } |
153 | | |
154 | 186k | Status QLRowBlock::Deserialize(const QLClient client, Slice* data) { |
155 | 186k | CHECK_EQ(client, YQL_CLIENT_CQL); |
156 | 186k | int32_t count = 0; |
157 | 186k | RETURN_NOT_OK(CQLDecodeNum(sizeof(count), NetworkByteOrder::Load32, data, &count)); |
158 | | |
159 | 2.08M | for (int32_t i = 0; 186k i < count; ++i1.89M ) { |
160 | 1.89M | RETURN_NOT_OK(Extend().Deserialize(client, data)); |
161 | 1.89M | } |
162 | 186k | if (!data->empty()) { |
163 | 0 | return STATUS(Corruption, "Extra data at the end of row block"); |
164 | 0 | } |
165 | 186k | return Status::OK(); |
166 | 186k | } |
167 | | |
168 | 7.54M | Result<size_t> QLRowBlock::GetRowCount(const QLClient client, const string& data) { |
169 | 7.54M | CHECK_EQ(client, YQL_CLIENT_CQL); |
170 | 7.54M | Slice slice(data); |
171 | 7.54M | return VERIFY_RESULT(CQLDecodeLength(&slice)); |
172 | 7.54M | } |
173 | | |
174 | 38.5k | Status QLRowBlock::AppendRowsData(const QLClient client, const string& src, string* dst) { |
175 | 38.5k | CHECK_EQ(client, YQL_CLIENT_CQL); |
176 | 38.5k | Slice src_slice(src); |
177 | 38.5k | const int32_t src_cnt = VERIFY_RESULT(CQLDecodeLength(&src_slice)); |
178 | 38.5k | if (src_cnt > 0) { |
179 | 8.89k | Slice dst_slice(*dst); |
180 | 8.89k | int32_t dst_cnt = VERIFY_RESULT(CQLDecodeLength(&dst_slice)); |
181 | 8.89k | if (dst_cnt == 0) { |
182 | 2.32k | *dst = src; |
183 | 6.57k | } else { |
184 | 6.57k | dst->append(src_slice.cdata(), src_slice.size()); |
185 | 6.57k | dst_cnt += src_cnt; |
186 | 6.57k | CQLEncodeLength(dst_cnt, &(*dst)[0]); |
187 | 6.57k | } |
188 | 8.89k | } |
189 | 38.5k | return Status::OK(); |
190 | 38.5k | } |
191 | | |
192 | 64 | string QLRowBlock::ZeroRowsData(const QLClient client) { |
193 | 64 | CHECK_EQ(client, YQL_CLIENT_CQL); |
194 | 64 | return string(sizeof(int32_t), 0); // Encode 32-bit 0 length. |
195 | 64 | } |
196 | | |
197 | | } // namespace yb |