MiniOB 1
MiniOB is one mini database, helping developers to learn how database works.
载入中...
搜索中...
未找到
column.h
1/* Copyright (c) 2021 OceanBase and/or its affiliates. All rights reserved.
2miniob is licensed under Mulan PSL v2.
3You can use this software according to the terms and conditions of the Mulan PSL v2.
4You may obtain a copy of Mulan PSL v2 at:
5 http://license.coscl.org.cn/MulanPSL2
6THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
7EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
8MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
9See the Mulan PSL v2 for more details. */
10
11#pragma once
12
13#include <string.h>
14
15#include "storage/field/field_meta.h"
16#include "storage/common/vector_buffer.h"
17
21// TODO: `Column` currently only support fixed-length type.
22class Column
23{
24public:
25 enum class Type
26 {
27 NORMAL_COLUMN,
29 };
30
31 Column() = default;
32
33 Column(const Column &other)
34 {
35 count_ = other.count_;
36 capacity_ = other.capacity_;
37 own_ = true;
38 attr_type_ = other.attr_type_;
39 attr_len_ = other.attr_len_;
41 data_ = new char[capacity_ * attr_len_];
42 memcpy(data_, other.data_, capacity_ * attr_len_);
43 vector_buffer_ = make_unique<VectorBuffer>();
44 }
45 Column(Column &&other)
46 {
47 data_ = other.data_;
48 count_ = other.count_;
49 capacity_ = other.capacity_;
50 own_ = other.own_;
51 attr_type_ = other.attr_type_;
52 attr_len_ = other.attr_len_;
53 column_type_ = other.column_type_;
54 vector_buffer_ = std::move(other.vector_buffer_);
55 other.data_ = nullptr;
56 other.count_ = 0;
57 other.capacity_ = 0;
58 other.own_ = false;
59 }
60
61 Column(const FieldMeta &meta, size_t size = DEFAULT_CAPACITY);
62 Column(AttrType attr_type, int attr_len, size_t size = DEFAULT_CAPACITY);
63
64 void init(const FieldMeta &meta, size_t size = DEFAULT_CAPACITY);
65 void init(AttrType attr_type, int attr_len, size_t size = DEFAULT_CAPACITY);
66 void init(const Value &value, size_t size);
67
68 unique_ptr<Column> clone() const { return make_unique<Column>(*this); }
69
70 virtual ~Column() { reset(); }
71
72 void reset();
73
74 RC append_one(const char *data);
75
76 RC append_value(const Value &val);
77
83 RC append(const char *data, int count);
84
88 Value get_value(int index) const;
89
90 RC copy_to(void *dest, int start_rows, int insert_rows) const
91 {
92 memcpy(dest, data_ + start_rows * attr_len_, insert_rows * attr_len_);
93 return RC::SUCCESS;
94 }
95
99 int data_len() const { return count_ * attr_len_; }
100
101 char *data() const { return data_; }
102
103 string_t add_text(const char *str, int len);
104
109 {
110 count_ = 0;
111 vector_buffer_ = nullptr;
112 }
113
117 void reference(const Column &column);
118
119 void set_column_type(Type column_type) { column_type_ = column_type; }
120 void set_attr_type(AttrType attr_type) { attr_type_ = attr_type; }
121 void set_count(int count) { count_ = count; }
122
123 int count() const { return count_; }
124 int capacity() const { return capacity_; }
125 AttrType attr_type() const { return attr_type_; }
126 int attr_len() const { return attr_len_; }
127 Type column_type() const { return column_type_; }
128 static constexpr size_t DEFAULT_CAPACITY = 8192;
129
130private:
131 char *data_ = nullptr;
133 int count_ = 0;
135 int capacity_ = 0;
137 bool own_ = true;
139 AttrType attr_type_ = AttrType::UNDEFINED;
141 int attr_len_ = -1;
143 Type column_type_ = Type::NORMAL_COLUMN;
144 unique_ptr<VectorBuffer> vector_buffer_ = nullptr;
145};
A column contains multiple values in contiguous memory with a specified type.
Definition: column.h:23
int capacity_
当前容量,count_ <= capacity_
Definition: column.h:135
void reset_data()
重置列数据,但不修改元信息
Definition: column.h:108
int count_
当前列值数量
Definition: column.h:133
RC append(const char *data, int count)
向 Column 追加写入数据
Definition: column.cpp:103
int attr_len_
列属性类型长度(目前只支持定长)
Definition: column.h:141
AttrType attr_type_
列属性类型
Definition: column.h:139
Type
Definition: column.h:26
@ CONSTANT_COLUMN
Normal column represents a list of fixed-length values
int data_len() const
获取列数据的实际大小(字节)
Definition: column.h:99
bool own_
是否拥有内存
Definition: column.h:137
Value get_value(int index) const
获取 index 位置的列值
Definition: column.cpp:149
Type column_type_
列类型
Definition: column.h:143
void reference(const Column &column)
引用另一个 Column
Definition: column.cpp:160
字段元数据
Definition: field_meta.h:30
属性的值
Definition: value.h:31
Definition: string_t.h:18