tuple.h 2.5 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/14.
羽飞's avatar
羽飞 已提交
13 14
//

羽飞's avatar
羽飞 已提交
15
#pragma once
羽飞's avatar
羽飞 已提交
16 17 18 19

#include <memory>
#include <vector>

羽飞's avatar
羽飞 已提交
20
#include "common/log/log.h"
羽飞's avatar
羽飞 已提交
21 22
#include "sql/parser/parse.h"
#include "sql/executor/value.h"
羽飞's avatar
羽飞 已提交
23 24
#include "storage/common/field.h"
#include "storage/common/record.h"
羽飞's avatar
羽飞 已提交
25 26 27

class Table;

羽飞's avatar
羽飞 已提交
28 29 30 31 32 33 34
class TupleCellSpec
{
  
};

class Tuple
{
羽飞's avatar
羽飞 已提交
35 36
public:
  Tuple() = default;
羽飞's avatar
羽飞 已提交
37
  virtual ~Tuple() = default;
羽飞's avatar
羽飞 已提交
38

羽飞's avatar
羽飞 已提交
39 40
  virtual int cell_num() const = 0; 
  virtual RC  cell_at(int index, TupleCell &cell) const = 0;
羽飞's avatar
羽飞 已提交
41

羽飞's avatar
羽飞 已提交
42 43
  //virtual RC  cell_spec_at(int index, TupleCellSpec &spec) const;
};
羽飞's avatar
羽飞 已提交
44

羽飞's avatar
羽飞 已提交
45 46 47 48 49 50 51
class RowTuple : public Tuple
{
public:
  RowTuple() = default;
  virtual ~RowTuple() = default;
  
  void set_record(Record *record)
52
  {
羽飞's avatar
羽飞 已提交
53
    this->record_ = record;
羽飞's avatar
羽飞 已提交
54 55
  }

羽飞's avatar
羽飞 已提交
56
  void set_table(Table *table)
57
  {
羽飞's avatar
羽飞 已提交
58
    this->table_ = table;
羽飞's avatar
羽飞 已提交
59 60
  }

羽飞's avatar
羽飞 已提交
61
  void set_schema(const std::vector<FieldMeta> *fields)
62
  {
羽飞's avatar
羽飞 已提交
63
    this->fields_ = fields;
羽飞's avatar
羽飞 已提交
64 65
  }

羽飞's avatar
羽飞 已提交
66
  int cell_num() const override
67
  {
羽飞's avatar
羽飞 已提交
68
    return fields_->size();
羽飞's avatar
羽飞 已提交
69 70
  }

羽飞's avatar
羽飞 已提交
71
  RC cell_at(int index, TupleCell &cell) const override
72
  {
羽飞's avatar
羽飞 已提交
73 74 75 76 77 78 79 80 81 82
    if (index < 0 || index >= fields_->size()) {
      LOG_WARN("invalid argument. index=%d", index);
      return RC::INVALID_ARGUMENT;
    }

    const FieldMeta &field_meta = (*fields_)[index];
    cell.set_table(table_);
    cell.set_type(field_meta.type());
    cell.set_data(this->record_->data() + field_meta.offset());
    return RC::SUCCESS;
羽飞's avatar
羽飞 已提交
83 84
  }

羽飞's avatar
羽飞 已提交
85
  RC cell_spec_at(int index, TupleCellSpec &spec) const
86
  {
羽飞's avatar
羽飞 已提交
87 88 89 90 91 92 93
    if (index < 0 || index >= fields_->size()) {
      LOG_WARN("invalid argument. index=%d", index);
      return RC::INVALID_ARGUMENT;
    }
    const FieldMeta &field_meta = (*fields_)[index];
    // TODO
    return RC::SUCCESS;
羽飞's avatar
羽飞 已提交
94 95
  }

羽飞's avatar
羽飞 已提交
96
  Record &record()
97
  {
羽飞's avatar
羽飞 已提交
98
    return *record_;
羽飞's avatar
羽飞 已提交
99 100
  }

羽飞's avatar
羽飞 已提交
101
  const Record &record() const
102
  {
羽飞's avatar
羽飞 已提交
103
    return *record_;
羽飞's avatar
羽飞 已提交
104 105
  }
private:
羽飞's avatar
羽飞 已提交
106 107 108
  Record *record_ = nullptr;
  Table *table_ = nullptr;
  const std::vector<FieldMeta> *fields_ = nullptr;
羽飞's avatar
羽飞 已提交
109 110
};

羽飞's avatar
羽飞 已提交
111 112 113
/*
class CompositeTuple : public Tuple
{
羽飞's avatar
羽飞 已提交
114 115
public:
private:
羽飞's avatar
羽飞 已提交
116
  std::vector<Tuple *> tuples_;
羽飞's avatar
羽飞 已提交
117
};
羽飞's avatar
羽飞 已提交
118
*/