record.h 2.7 KB
Newer Older
W
wangyunlai.wyl 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved.
miniob is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
         http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details. */

//
// Created by Wangyunlai on 2022/5/4.
//

#pragma once

#include <stddef.h>
#include <vector>
羽飞's avatar
羽飞 已提交
19
#include <limits>
羽飞's avatar
羽飞 已提交
20
#include <sstream>
W
wangyunlai.wyl 已提交
21 22

#include "rc.h"
羽飞's avatar
羽飞 已提交
23
#include "defs.h"
W
wangyunlai.wyl 已提交
24 25
#include "storage/common/index_meta.h"
#include "storage/common/field_meta.h"
羽飞's avatar
羽飞 已提交
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83

class Field;

struct RID {
  PageNum page_num;  // record's page number
  SlotNum slot_num;  // record's slot number
  // bool    valid;    // true means a valid record

  RID() = default;
  RID(const PageNum _page_num, const SlotNum _slot_num)
    : page_num(_page_num), slot_num(_slot_num)
  {}

  const std::string to_string() const
  {
    std::stringstream ss;

    ss << "PageNum:" << page_num << ", SlotNum:" << slot_num;

    return ss.str();
  }

  bool operator==(const RID &other) const
  {
    return page_num == other.page_num && slot_num == other.slot_num;
  }

  bool operator!=(const RID &other) const
  {
    return !(*this == other);
  }

  static int compare(const RID *rid1, const RID *rid2)
  {
    int page_diff = rid1->page_num - rid2->page_num;
    if (page_diff != 0) {
      return page_diff;
    } else {
      return rid1->slot_num - rid2->slot_num;
    }
  }

  /**
   * 返回一个不可能出现的最小的RID
   * 虽然page num 0和slot num 0都是合法的,但是page num 0通常用于存放meta数据,所以对数据部分来说都是
   * 不合法的. 这里在bplus tree中查找时会用到。
   */
  static RID *min()
  {
    static RID rid{0, 0};
    return &rid;
  }
  static RID *max()
  {
    static RID rid{std::numeric_limits<PageNum>::max(), std::numeric_limits<SlotNum>::max()};
    return &rid;
  }
};
W
wangyunlai.wyl 已提交
84 85 86 87 88 89 90

class Record
{
public:
  Record() = default;
  ~Record() = default;

羽飞's avatar
羽飞 已提交
91 92 93 94 95 96 97 98
  void set_data(char *data) { this->data_ = data; }
  char *data() { return this->data_; }
  const char *data() const { return this->data_; }

  void set_rid(const RID &rid) { this->rid_ = rid; }
  void set_rid(const PageNum page_num, const SlotNum slot_num) { this->rid_.page_num = page_num; this->rid_.slot_num = slot_num; }
  RID & rid() { return rid_; }
  const RID &rid() const { return rid_; };
W
wangyunlai.wyl 已提交
99 100

private:
羽飞's avatar
羽飞 已提交
101
  RID                            rid_;
W
wangyunlai.wyl 已提交
102 103 104

  // the data buffer
  // record will not release the memory
羽飞's avatar
羽飞 已提交
105
  char *                         data_ = nullptr;
W
wangyunlai.wyl 已提交
106
};