be/src/storage/row_cursor.h
Line | Count | Source |
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 | | #pragma once |
19 | | |
20 | | #include <butil/macros.h> |
21 | | #include <stddef.h> |
22 | | #include <stdint.h> |
23 | | |
24 | | #include <memory> |
25 | | #include <string> |
26 | | #include <vector> |
27 | | |
28 | | #include "common/consts.h" |
29 | | #include "common/status.h" |
30 | | #include "core/data_type/primitive_type.h" |
31 | | #include "core/field.h" |
32 | | #include "storage/olap_tuple.h" |
33 | | #include "storage/schema.h" |
34 | | #include "storage/tablet/tablet_schema.h" |
35 | | |
36 | | namespace doris { |
37 | | #include "common/compile_check_begin.h" |
38 | | class StorageField; |
39 | | |
40 | | // Delegate the operation of a row of data. |
41 | | // Stores values as core::Field objects instead of raw byte buffers. |
42 | | class RowCursor { |
43 | | public: |
44 | | RowCursor(); |
45 | | ~RowCursor(); |
46 | | RowCursor(const RowCursor&) = delete; |
47 | | RowCursor& operator=(const RowCursor&) = delete; |
48 | | RowCursor(RowCursor&&) noexcept; |
49 | | RowCursor& operator=(RowCursor&&) noexcept; |
50 | | |
51 | | // Initialize from OlapTuple (which now stores Fields). |
52 | | // Sets up the schema and copies Fields from the tuple. |
53 | | Status init(TabletSchemaSPtr schema, const OlapTuple& tuple); |
54 | | Status init(TabletSchemaSPtr schema, const OlapTuple& tuple, |
55 | | const std::shared_ptr<Schema>& shared_schema); |
56 | | |
57 | | // Initialize with schema and num_columns, creating null Fields. |
58 | | // Caller sets individual fields via mutable_field(). |
59 | | Status init(TabletSchemaSPtr schema, size_t num_columns); |
60 | | |
61 | | // Initialize from typed Fields directly. |
62 | | Status init_scan_key(TabletSchemaSPtr schema, std::vector<Field> fields); |
63 | | |
64 | 651k | const Field& field(uint32_t cid) const { return _fields[cid]; } |
65 | 651k | Field& mutable_field(uint32_t cid) { return _fields[cid]; } |
66 | | |
67 | 1 | size_t field_count() const { return _fields.size(); } |
68 | | |
69 | 0 | const StorageField* column_schema(uint32_t cid) const { return _schema->column(cid); } |
70 | 651k | const Schema* schema() const { return _schema.get(); } |
71 | | |
72 | | // Returns a deep copy of this RowCursor with the same schema and field values. |
73 | | RowCursor clone() const; |
74 | | |
75 | | // Pad all CHAR-type fields in-place to their declared column length using '\0'. |
76 | | // RowCursor holds CHAR values in compute format (unpadded). Call this before |
77 | | // comparing against storage-format data (e.g. _seek_block) where CHAR is padded. |
78 | | void pad_char_fields(); |
79 | | |
80 | | // Output row cursor content in string format |
81 | | std::string to_string() const; |
82 | | |
83 | | // Encode one row into binary according given num_keys. |
84 | | // Internally converts each core::Field to its storage representation via |
85 | | // PrimitiveTypeConvertor before passing to KeyCoder. |
86 | | // CHAR fields are zero-padded to column.length() for encoding. |
87 | | template <bool is_mow = false> |
88 | | void encode_key_with_padding(std::string* buf, size_t num_keys, bool padding_minimal) const; |
89 | | |
90 | | // Encode one row into binary according given num_keys. |
91 | | // Client must ensure that row contains the first num_keys columns. |
92 | | template <bool full_encode = false> |
93 | | void encode_key(std::string* buf, size_t num_keys) const; |
94 | | |
95 | | // Encode a single field at column index 'cid' into 'buf'. |
96 | 64.4k | void encode_single_field(uint32_t cid, std::string* buf, bool full_encode) const { |
97 | 64.4k | const auto& f = _fields[cid]; |
98 | | DCHECK(!f.is_null()); |
99 | 64.4k | _encode_field(_schema->column(cid), f, full_encode, buf); |
100 | 64.4k | } |
101 | | |
102 | | private: |
103 | | // Copy Fields from an OlapTuple into this cursor. |
104 | | Status from_tuple(const OlapTuple& tuple); |
105 | | |
106 | | void _init_schema(TabletSchemaSPtr schema, uint32_t column_count); |
107 | | void _init_schema(const std::shared_ptr<Schema>& shared_schema, uint32_t column_count); |
108 | | |
109 | | // Helper: encode a single non-null field for the given column. |
110 | | // Converts the core::Field to storage format and calls KeyCoder. |
111 | | void _encode_field(const StorageField* storage_field, const Field& f, bool full_encode, |
112 | | std::string* buf) const; |
113 | | |
114 | | std::unique_ptr<Schema> _schema; |
115 | | std::vector<Field> _fields; |
116 | | }; |
117 | | #include "common/compile_check_end.h" |
118 | | } // namespace doris |