table.h 3.8 KB
Newer Older
羽飞's avatar
羽飞 已提交
1 2 3 4 5 6 7 8 9 10 11
/* 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. */

//
12
// Created by Meiyi & Wangyunlai on 2021/5/12.
羽飞's avatar
羽飞 已提交
13 14 15 16 17 18 19
//

#ifndef __OBSERVER_STORAGE_COMMON_TABLE_H__
#define __OBSERVER_STORAGE_COMMON_TABLE_H__

#include "storage/common/table_meta.h"

W
wangyunlai.wyl 已提交
20
struct RID;
羽飞's avatar
羽飞 已提交
21
class Record;
羽飞's avatar
羽飞 已提交
22 23
class DiskBufferPool;
class RecordFileHandler;
W
wangyunlai.wyl 已提交
24
class RecordFileScanner;
羽飞's avatar
羽飞 已提交
25 26 27 28 29 30 31 32 33
class ConditionFilter;
class DefaultConditionFilter;
class Index;
class IndexScanner;
class RecordDeleter;
class Trx;

class Table {
public:
羽飞's avatar
羽飞 已提交
34
  Table() = default;
羽飞's avatar
羽飞 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
  ~Table();

  /**
   * 创建一个表
   * @param path 元数据保存的文件(完整路径)
   * @param name 表名
   * @param base_dir 表数据存放的路径
   * @param attribute_count 字段个数
   * @param attributes 字段
   */
  RC create(const char *path, const char *name, const char *base_dir, int attribute_count, const AttrInfo attributes[]);

  /**
   * 打开一个表
   * @param meta_file 保存表元数据的文件完整路径
   * @param base_dir 表所在的文件夹,表记录数据文件、索引数据文件存放位置
   */
  RC open(const char *meta_file, const char *base_dir);
53

羽飞's avatar
羽飞 已提交
54
  RC insert_record(Trx *trx, int value_num, const Value *values);
55 56
  RC update_record(Trx *trx, const char *attribute_name, const Value *value, int condition_num,
      const Condition conditions[], int *updated_count);
羽飞's avatar
羽飞 已提交
57
  RC delete_record(Trx *trx, ConditionFilter *filter, int *deleted_count);
羽飞's avatar
羽飞 已提交
58
  RC delete_record(Trx *trx, Record *record);
羽飞's avatar
羽飞 已提交
59

60 61
  RC scan_record(Trx *trx, ConditionFilter *filter, int limit, void *context,
      void (*record_reader)(const char *data, void *context));
羽飞's avatar
羽飞 已提交
62 63 64

  RC create_index(Trx *trx, const char *index_name, const char *attribute_name);

W
wangyunlai.wyl 已提交
65 66
  RC get_record_scanner(RecordFileScanner &scanner);

羽飞's avatar
羽飞 已提交
67 68 69 70 71
  RecordFileHandler *record_handler() const
  {
    return record_handler_;
  }

羽飞's avatar
羽飞 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84 85
public:
  const char *name() const;

  const TableMeta &table_meta() const;

  RC sync();

public:
  RC commit_insert(Trx *trx, const RID &rid);
  RC commit_delete(Trx *trx, const RID &rid);
  RC rollback_insert(Trx *trx, const RID &rid);
  RC rollback_delete(Trx *trx, const RID &rid);

private:
86 87 88 89
  RC scan_record(
      Trx *trx, ConditionFilter *filter, int limit, void *context, RC (*record_reader)(Record *record, void *context));
  RC scan_record_by_index(Trx *trx, IndexScanner *scanner, ConditionFilter *filter, int limit, void *context,
      RC (*record_reader)(Record *record, void *context));
羽飞's avatar
羽飞 已提交
90 91 92 93 94 95 96 97 98 99 100
  IndexScanner *find_index_for_scan(const ConditionFilter *filter);
  IndexScanner *find_index_for_scan(const DefaultConditionFilter &filter);

  RC insert_record(Trx *trx, Record *record);

private:
  friend class RecordUpdater;
  friend class RecordDeleter;

  RC insert_entry_of_indexes(const char *record, const RID &rid);
  RC delete_entry_of_indexes(const char *record, const RID &rid, bool error_on_not_exists);
101

羽飞's avatar
羽飞 已提交
102 103
private:
  RC init_record_handler(const char *base_dir);
104
  RC make_record(int value_num, const Value *values, char *&record_out);
羽飞's avatar
羽飞 已提交
105

羽飞's avatar
羽飞 已提交
106
public:
羽飞's avatar
羽飞 已提交
107
  Index *find_index(const char *index_name) const;
羽飞's avatar
羽飞 已提交
108
  Index *find_index_by_field(const char *field_name) const;
羽飞's avatar
羽飞 已提交
109 110

private:
111 112
  std::string base_dir_;
  TableMeta table_meta_;
羽飞's avatar
羽飞 已提交
113 114
  DiskBufferPool *data_buffer_pool_ = nullptr;  /// 数据文件关联的buffer pool
  RecordFileHandler *record_handler_ = nullptr;  /// 记录操作
115
  std::vector<Index *> indexes_;
羽飞's avatar
羽飞 已提交
116 117
};

羽飞's avatar
羽飞 已提交
118
#endif  // __OBSERVER_STORAGE_COMMON_TABLE_H__