table.h 3.6 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 20 21 22 23 24 25 26 27 28 29 30 31 32
//

#ifndef __OBSERVER_STORAGE_COMMON_TABLE_H__
#define __OBSERVER_STORAGE_COMMON_TABLE_H__

#include "storage/common/table_meta.h"

class DiskBufferPool;
class RecordFileHandler;
class ConditionFilter;
class DefaultConditionFilter;
struct Record;
struct RID;
class Index;
class IndexScanner;
class RecordDeleter;
class Trx;

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

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

58 59
  RC scan_record(Trx *trx, ConditionFilter *filter, int limit, void *context,
      void (*record_reader)(const char *data, void *context));
羽飞's avatar
羽飞 已提交
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76

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

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:
77 78 79 80
  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
羽飞 已提交
81 82 83 84 85 86 87 88 89 90 91 92
  IndexScanner *find_index_for_scan(const ConditionFilter *filter);
  IndexScanner *find_index_for_scan(const DefaultConditionFilter &filter);

  RC insert_record(Trx *trx, Record *record);
  RC delete_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);
93

羽飞's avatar
羽飞 已提交
94 95
private:
  RC init_record_handler(const char *base_dir);
96
  RC make_record(int value_num, const Value *values, char *&record_out);
羽飞's avatar
羽飞 已提交
97 98 99 100 101

private:
  Index *find_index(const char *index_name) const;

private:
102 103
  std::string base_dir_;
  TableMeta table_meta_;
羽飞's avatar
羽飞 已提交
104 105
  DiskBufferPool *data_buffer_pool_ = nullptr;  /// 数据文件关联的buffer pool
  RecordFileHandler *record_handler_ = nullptr;  /// 记录操作
106
  std::vector<Index *> indexes_;
羽飞's avatar
羽飞 已提交
107 108
};

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