19#include "common/log/log.h"
20#include "common/sys/rc.h"
21#include "common/types.h"
22#include "common/lang/vector.h"
23#include "common/lang/sstream.h"
24#include "common/lang/limits.h"
25#include "storage/field/field_meta.h"
26#include "storage/index/index_meta.h"
40 RID(
const PageNum _page_num,
const SlotNum _slot_num) : page_num(_page_num), slot_num(_slot_num) {}
42 const string to_string()
const
45 ss <<
"PageNum:" << page_num <<
", SlotNum:" << slot_num;
49 bool operator==(
const RID &other)
const {
return page_num == other.page_num && slot_num == other.slot_num; }
51 bool operator!=(
const RID &other)
const {
return !(*
this == other); }
53 static int compare(
const RID *rid1,
const RID *rid2)
55 int page_diff = rid1->page_num - rid2->page_num;
59 return rid1->slot_num - rid2->slot_num;
80 static RID rid{numeric_limits<PageNum>::max(), numeric_limits<SlotNum>::max()};
87 size_t operator()(
const RID &rid)
const noexcept
89 return hash<PageNum>()(rid.page_num) ^ hash<SlotNum>()(rid.slot_num);
106 if (
owner_ && data_ !=
nullptr) {
120 char *tmp = (
char *)malloc(other.len_);
121 ASSERT(
nullptr != tmp,
"failed to allocate memory. size=%d", other.len_);
122 memcpy(tmp, other.data_, other.len_);
129 if (
this == &other) {
133 if (!
owner_ || len_ != other.len_) {
138 this->rid_ = other.rid_;
139 memcpy(data_, other.data_, other.len_);
150 other.data_ =
nullptr;
156 other.data_ =
nullptr;
164 if (
this == &other) {
169 new (
this)
Record(std::move(other));
173 void set_data(
char *data,
int len = 0)
178 void set_data_owner(
char *data,
int len)
180 ASSERT(len != 0,
"the len of data should not be 0");
188 RC copy_data(
const char *data,
int len)
190 ASSERT(len!= 0,
"the len of data should not be 0");
191 char *tmp = (
char *)malloc(len);
192 if (
nullptr == tmp) {
193 LOG_WARN(
"failed to allocate memory. size=%d", len);
197 memcpy(tmp, data, len);
198 set_data_owner(tmp, len);
202 RC new_record(
int len)
204 ASSERT(len!= 0,
"the len of data should not be 0");
205 char *tmp = (
char *)malloc(len);
206 if (
nullptr == tmp) {
207 LOG_WARN(
"failed to allocate memory. size=%d", len);
210 set_data_owner(tmp, len);
214 RC set_field(
int field_offset,
int field_len,
char *data)
217 LOG_ERROR(
"cannot set field when record does not own the memory");
220 if (field_offset + field_len > len_) {
221 LOG_ERROR(
"invalid offset or length. offset=%d, length=%d, total length=%d", field_offset, field_len, len_);
222 return RC::INVALID_ARGUMENT;
225 memcpy(data_ + field_offset, data, field_len);
229 char *data() {
return this->data_; }
230 const char *data()
const {
return this->data_; }
231 int len()
const {
return this->len_; }
233 void set_rid(
const RID &rid) { this->rid_ = rid; }
234 void set_rid(
const PageNum page_num,
const SlotNum slot_num)
236 this->rid_.page_num = page_num;
237 this->rid_.slot_num = slot_num;
239 RID &rid() {
return rid_; }
240 const RID &rid()
const {
return rid_; }
245 char *data_ =
nullptr;
表示一个记录
Definition: record.h:101
bool owner_
如果不是record自己来管理内存,这个字段可能是无效的
Definition: record.h:247
标识一个记录的位置 一个记录是放在某个文件的某个页面的某个槽位。这里不记录文件信息,记录页面和槽位信息
Definition: record.h:35
static RID * min()
Definition: record.h:68
static RID * max()
返回一个“最大的”RID 我们假设page num和slot num都不会使用对应数值类型的最大值
Definition: record.h:78