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) {
121 char *tmp = (
char *)malloc(other.len_);
122 ASSERT(
nullptr != tmp,
"failed to allocate memory. size=%d", other.len_);
123 memcpy(tmp, other.data_, other.len_);
130 if (
this == &other) {
134 if (!
owner_ || len_ != other.len_) {
139 this->rid_ = other.rid_;
140 this->key_ = other.key_;
141 memcpy(data_, other.data_, other.len_);
152 other.data_ =
nullptr;
158 other.data_ =
nullptr;
166 if (
this == &other) {
171 new (
this)
Record(std::move(other));
175 void set_data(
char *data,
int len = 0)
180 void set_data_owner(
char *data,
int len)
182 ASSERT(len != 0,
"the len of data should not be 0");
190 RC copy_data(
const char *data,
int len)
192 ASSERT(len!= 0,
"the len of data should not be 0");
193 char *tmp = (
char *)malloc(len);
194 if (
nullptr == tmp) {
195 LOG_WARN(
"failed to allocate memory. size=%d", len);
199 memcpy(tmp, data, len);
200 set_data_owner(tmp, len);
204 RC new_record(
int len)
206 ASSERT(len!= 0,
"the len of data should not be 0");
207 char *tmp = (
char *)malloc(len);
208 if (
nullptr == tmp) {
209 LOG_WARN(
"failed to allocate memory. size=%d", len);
212 set_data_owner(tmp, len);
216 RC set_field(
int field_offset,
int field_len,
char *data)
219 LOG_ERROR(
"cannot set field when record does not own the memory");
222 if (field_offset + field_len > len_) {
223 LOG_ERROR(
"invalid offset or length. offset=%d, length=%d, total length=%d", field_offset, field_len, len_);
224 return RC::INVALID_ARGUMENT;
227 memcpy(data_ + field_offset, data, field_len);
231 RC reset_filed(
int field_offset,
int field_len)
234 LOG_ERROR(
"cannot set field when record does not own the memory");
237 if (field_offset + field_len > len_) {
238 LOG_ERROR(
"invalid offset or length. offset=%d, length=%d, total length=%d", field_offset, field_len, len_);
239 return RC::INVALID_ARGUMENT;
242 memset(data_ + field_offset, 0, field_len);
246 char *data() {
return this->data_; }
247 const char *data()
const {
return this->data_; }
248 int len()
const {
return this->len_; }
250 void set_rid(
const RID &rid) { this->rid_ = rid; }
251 void set_rid(
const PageNum page_num,
const SlotNum slot_num)
253 this->rid_.page_num = page_num;
254 this->rid_.slot_num = slot_num;
257 RID &rid() {
return rid_; }
258 const RID &rid()
const {
return rid_; }
259 void set_key(
const string &key) { key_ = key; }
260 const string &key()
const {
return key_; }
265 char *data_ =
nullptr;
表示一个记录
Definition: record.h:101
bool owner_
如果不是record自己来管理内存,这个字段可能是无效的
Definition: record.h:267
标识一个记录的位置 一个记录是放在某个文件的某个页面的某个槽位。这里不记录文件信息,记录页面和槽位信息
Definition: record.h:35
static RID * min()
Definition: record.h:68
static RID * max()
返回一个“最大的”RID 我们假设page num和slot num都不会使用对应数值类型的最大值
Definition: record.h:78