MiniOB 1
MiniOB is one mini database, helping developers to learn how database works.
载入中...
搜索中...
未找到
record.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//
12// Created by Wangyunlai on 2022/5/4.
13//
14
15#pragma once
16
17#include <stddef.h>
18#include <vector>
19#include <limits>
20#include <sstream>
21
22#include "common/rc.h"
23#include "common/types.h"
24#include "common/log/log.h"
25#include "storage/index/index_meta.h"
26#include "storage/field/field_meta.h"
27
28class Field;
29
34struct RID
35{
36 PageNum page_num; // record's page number
37 SlotNum slot_num; // record's slot number
38
39 RID() = default;
40 RID(const PageNum _page_num, const SlotNum _slot_num) : page_num(_page_num), slot_num(_slot_num) {}
41
42 const std::string to_string() const
43 {
44 std::stringstream ss;
45 ss << "PageNum:" << page_num << ", SlotNum:" << slot_num;
46 return ss.str();
47 }
48
49 bool operator==(const RID &other) const { return page_num == other.page_num && slot_num == other.slot_num; }
50
51 bool operator!=(const RID &other) const { return !(*this == other); }
52
53 static int compare(const RID *rid1, const RID *rid2)
54 {
55 int page_diff = rid1->page_num - rid2->page_num;
56 if (page_diff != 0) {
57 return page_diff;
58 } else {
59 return rid1->slot_num - rid2->slot_num;
60 }
61 }
62
68 static RID *min()
69 {
70 static RID rid{0, 0};
71 return &rid;
72 }
73
78 static RID *max()
79 {
80 static RID rid{std::numeric_limits<PageNum>::max(), std::numeric_limits<SlotNum>::max()};
81 return &rid;
82 }
83};
84
91class Record
92{
93public:
94 Record() = default;
95 ~Record()
96 {
97 if (owner_ && data_ != nullptr) {
98 free(data_);
99 data_ = nullptr;
100 }
101 }
102
103 Record(const Record &other)
104 {
105 rid_ = other.rid_;
106 data_ = other.data_;
107 len_ = other.len_;
108 owner_ = other.owner_;
109
110 if (other.owner_) {
111 char *tmp = (char *)malloc(other.len_);
112 ASSERT(nullptr != tmp, "failed to allocate memory. size=%d", other.len_);
113 memcpy(tmp, other.data_, other.len_);
114 data_ = tmp;
115 }
116 }
117
118 Record &operator=(const Record &other)
119 {
120 if (this == &other) {
121 return *this;
122 }
123
124 this->~Record();
125 new (this) Record(other);
126 return *this;
127 }
128
129 void set_data(char *data, int len = 0)
130 {
131 this->data_ = data;
132 this->len_ = len;
133 }
134 void set_data_owner(char *data, int len)
135 {
136 ASSERT(len != 0, "the len of data should not be 0");
137 this->~Record();
138
139 this->data_ = data;
140 this->len_ = len;
141 this->owner_ = true;
142 }
143
144 char *data() { return this->data_; }
145 const char *data() const { return this->data_; }
146 int len() const { return this->len_; }
147
148 void set_rid(const RID &rid) { this->rid_ = rid; }
149 void set_rid(const PageNum page_num, const SlotNum slot_num)
150 {
151 this->rid_.page_num = page_num;
152 this->rid_.slot_num = slot_num;
153 }
154 RID &rid() { return rid_; }
155 const RID &rid() const { return rid_; }
156
157private:
158 RID rid_;
159
160 char *data_ = nullptr;
161 int len_ = 0;
162 bool owner_ = false;
163};
字段
Definition: field.h:25
表示一个记录 当前的记录都是连续存放的空间(内存或磁盘上)。 为了提高访问的效率,record通常直接记录指向页面上的内存,但是需要保证访问这种数据时,拿着锁资源。 为了方便,也提供了复制内存的方法。可...
Definition: record.h:92
bool owner_
如果不是record自己来管理内存,这个字段可能是无效的
Definition: record.h:162
标识一个记录的位置 一个记录是放在某个文件的某个页面的某个槽位。这里不记录文件信息,记录页面和槽位信息
Definition: record.h:35
static RID * min()
Definition: record.h:68
static RID * max()
返回一个“最大的”RID 我们假设page num和slot num都不会使用对应数值类型的最大值
Definition: record.h:78