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.h
Line
Count
Source
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
#ifndef YB_COMMON_QL_ROWBLOCK_H
17
#define YB_COMMON_QL_ROWBLOCK_H
18
19
#include <memory>
20
#include <vector>
21
22
#include "yb/common/common_fwd.h"
23
#include "yb/common/common_types.pb.h"
24
25
#include "yb/util/status_fwd.h"
26
27
namespace yb {
28
29
class Slice;
30
class faststring;
31
32
//------------------------------------------ QL row ----------------------------------------
33
// A QL row. It uses QLValue to store the column values.
34
class QLRow {
35
 public:
36
  explicit QLRow(const std::shared_ptr<const Schema>& schema);
37
  QLRow(const QLRow& row);
38
  QLRow(QLRow&& row);
39
  ~QLRow();
40
41
  // Row columns' schema
42
45.1M
  const Schema& schema() const { return *schema_.get(); }
43
44
  // Column count
45
  size_t column_count() const;
46
47
  // Column's datatype
48
  const std::shared_ptr<QLType>& column_type(const size_t col_idx) const;
49
50
  // Get a mutable/non-mutable column value.
51
  const QLValue& column(const size_t col_idx) const;
52
  QLValue* mutable_column(const size_t col_idx);
53
54
  void SetColumn(size_t col_idx, QLValuePB value);
55
56
  QLRow& operator=(const QLRow& other);
57
  QLRow& operator=(QLRow&& other);
58
59
  void SetColumnValues(const std::vector<QLValue>& column_values);
60
61
  //------------------------------------ debug string ---------------------------------------
62
  // Return a string for debugging.
63
  std::string ToString() const;
64
65
 private:
66
  friend class QLRowBlock;
67
68
  //----------------------------- serializer / deserializer ---------------------------------
69
  // Note: QLRow's serialize / deserialize methods are private because we expect QL rows
70
  // to be serialized / deserialized as part of a row block. See QLRowBlock.
71
  void Serialize(QLClient client, faststring* buffer) const;
72
  CHECKED_STATUS Deserialize(QLClient client, Slice* data);
73
74
  std::shared_ptr<const Schema> schema_;
75
  std::vector<QLValue> values_;
76
};
77
78
//--------------------------------------- QL row block --------------------------------------
79
// A block of QL rows. The rows can be extended. The rows are stored in an ordered vector so
80
// it is sortable.
81
class QLRowBlock {
82
 public:
83
  // Create a row block for a table with the given schema and the selected column ids.
84
  QLRowBlock(const Schema& schema, const std::vector<ColumnId>& column_ids);
85
86
  // Create a row block for the given schema.
87
  explicit QLRowBlock(const Schema& schema);
88
89
  virtual ~QLRowBlock();
90
91
  // Row columns' schema
92
6.11k
  const Schema& schema() const { return *schema_.get(); }
93
94
  // Row count
95
5.82M
  size_t row_count() const { return rows_.size(); }
96
97
  // The rows
98
125k
  std::vector<QLRow>& rows() { return rows_; }
99
100
125k
  const std::vector<QLRow>& rows() const { return rows_; }
101
102
  // Return the row by index
103
2.94M
  QLRow& row(size_t idx) { return rows_[idx]; }
104
105
  // Extend row block by 1 emtpy row and return the new row.
106
  QLRow& Extend();
107
108
  // Optimization to reserve memory for up to this many rows.
109
  void Reserve(size_t size);
110
111
  // Add a row to the rowblock.
112
  CHECKED_STATUS AddRow(const QLRow& row);
113
114
  //------------------------------------ debug string ---------------------------------------
115
  // Return a string for debugging.
116
  std::string ToString() const;
117
118
  //----------------------------- serializer / deserializer ---------------------------------
119
  void Serialize(QLClient client, faststring* buffer) const;
120
  CHECKED_STATUS Deserialize(QLClient client, Slice* data);
121
122
  //-------------------------- utility functions for rows data ------------------------------
123
  // Return row count.
124
  static Result<size_t> GetRowCount(QLClient client, const std::string& data);
125
126
  // Append rows data. Caller should ensure the column schemas are the same.
127
  static CHECKED_STATUS AppendRowsData(QLClient client, const std::string& src, std::string* dst);
128
129
  // Return rows data of 0 (empty) rows.
130
  static std::string ZeroRowsData(QLClient client);
131
132
 private:
133
  // Schema of the selected columns. (Note: this schema has no key column definitions)
134
  SchemaPtr schema_;
135
  // Rows in this block.
136
  std::vector<QLRow> rows_;
137
};
138
139
} // namespace yb
140
141
#endif // YB_COMMON_QL_ROWBLOCK_H