table_meta.cpp 8.2 KB
Newer Older
羽飞's avatar
羽飞 已提交
1
/* Copyright (c) 2021 OceanBase and/or its affiliates. All rights reserved.
羽飞's avatar
羽飞 已提交
2 3 4 5 6 7 8 9 10 11
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
//

#include <algorithm>
L
Longda 已提交
16
#include <common/lang/string.h>
羽飞's avatar
羽飞 已提交
17 18 19 20 21 22

#include "storage/common/table_meta.h"
#include "json/json.h"
#include "common/log/log.h"
#include "storage/trx/trx.h"

羽飞's avatar
羽飞 已提交
23 24
using namespace std;

羽飞's avatar
羽飞 已提交
25 26 27 28
static const Json::StaticString FIELD_TABLE_NAME("table_name");
static const Json::StaticString FIELD_FIELDS("fields");
static const Json::StaticString FIELD_INDEXES("indexes");

L
Longda 已提交
29 30 31
TableMeta::TableMeta(const TableMeta &other)
    : name_(other.name_), fields_(other.fields_), indexes_(other.indexes_), record_size_(other.record_size_)
{}
羽飞's avatar
羽飞 已提交
32

L
Longda 已提交
33 34
void TableMeta::swap(TableMeta &other) noexcept
{
羽飞's avatar
羽飞 已提交
35 36 37 38 39 40
  name_.swap(other.name_);
  fields_.swap(other.fields_);
  indexes_.swap(other.indexes_);
  std::swap(record_size_, other.record_size_);
}

L
Longda 已提交
41 42 43
RC TableMeta::init(const char *name, int field_num, const AttrInfo attributes[])
{
  if (common::is_blank(name)) {
羽飞's avatar
羽飞 已提交
44 45 46 47 48
    LOG_ERROR("Name cannot be empty");
    return RC::INVALID_ARGUMENT;
  }

  if (field_num <= 0 || nullptr == attributes) {
L
Longda 已提交
49
    LOG_ERROR("Invalid argument. name=%s, field_num=%d, attributes=%p", name, field_num, attributes);
羽飞's avatar
羽飞 已提交
50 51 52 53
    return RC::INVALID_ARGUMENT;
  }

  RC rc = RC::SUCCESS;
羽飞's avatar
羽飞 已提交
54 55 56 57 58 59 60 61 62 63 64
  
  int field_offset = 0;
  int trx_field_num = 0;
  const vector<FieldMeta> *trx_fields = TrxKit::instance()->trx_fields();
  if (trx_fields != nullptr) {
    fields_.resize(field_num + trx_fields->size());

    for (size_t i = 0; i < trx_fields->size(); i++) {
      const FieldMeta &field_meta = (*trx_fields)[i];
      fields_[i] = FieldMeta(field_meta.name(), field_meta.type(), field_offset, field_meta.len(), false/*visible*/);
      field_offset += field_meta.len();
羽飞's avatar
羽飞 已提交
65 66
    }

羽飞's avatar
羽飞 已提交
67 68 69
    trx_field_num = static_cast<int>(trx_fields->size());
  } else {
    fields_.resize(field_num);
羽飞's avatar
羽飞 已提交
70 71 72 73
  }

  for (int i = 0; i < field_num; i++) {
    const AttrInfo &attr_info = attributes[i];
羽飞's avatar
羽飞 已提交
74 75
    rc = fields_[i + trx_field_num].init(attr_info.name.c_str(), 
            attr_info.type, field_offset, attr_info.length, true/*visible*/);
羽飞's avatar
羽飞 已提交
76
    if (rc != RC::SUCCESS) {
羽飞's avatar
羽飞 已提交
77
      LOG_ERROR("Failed to init field meta. table name=%s, field name: %s", name, attr_info.name.c_str());
羽飞's avatar
羽飞 已提交
78 79 80 81 82 83 84 85 86
      return rc;
    }

    field_offset += attr_info.length;
  }

  record_size_ = field_offset;

  name_ = name;
L
Longda 已提交
87
  LOG_INFO("Sussessfully initialized table meta. table name=%s", name);
羽飞's avatar
羽飞 已提交
88 89 90
  return RC::SUCCESS;
}

L
Longda 已提交
91 92
RC TableMeta::add_index(const IndexMeta &index)
{
羽飞's avatar
羽飞 已提交
93 94 95 96
  indexes_.push_back(index);
  return RC::SUCCESS;
}

L
Longda 已提交
97 98
const char *TableMeta::name() const
{
羽飞's avatar
羽飞 已提交
99 100 101
  return name_.c_str();
}

L
Longda 已提交
102 103
const FieldMeta *TableMeta::trx_field() const
{
羽飞's avatar
羽飞 已提交
104 105 106
  return &fields_[0];
}

羽飞's avatar
羽飞 已提交
107 108 109 110 111
const std::pair<const FieldMeta *, int> TableMeta::trx_fields() const
{
  return std::pair<const FieldMeta *, int>{fields_.data(), sys_field_num()};
}

L
Longda 已提交
112 113
const FieldMeta *TableMeta::field(int index) const
{
羽飞's avatar
羽飞 已提交
114 115
  return &fields_[index];
}
L
Longda 已提交
116 117
const FieldMeta *TableMeta::field(const char *name) const
{
羽飞's avatar
羽飞 已提交
118 119 120 121 122 123 124 125 126 127 128
  if (nullptr == name) {
    return nullptr;
  }
  for (const FieldMeta &field : fields_) {
    if (0 == strcmp(field.name(), name)) {
      return &field;
    }
  }
  return nullptr;
}

L
Longda 已提交
129 130
const FieldMeta *TableMeta::find_field_by_offset(int offset) const
{
羽飞's avatar
羽飞 已提交
131 132 133 134 135 136 137
  for (const FieldMeta &field : fields_) {
    if (field.offset() == offset) {
      return &field;
    }
  }
  return nullptr;
}
L
Longda 已提交
138 139
int TableMeta::field_num() const
{
羽飞's avatar
羽飞 已提交
140 141 142
  return fields_.size();
}

L
Longda 已提交
143 144
int TableMeta::sys_field_num() const
{
羽飞's avatar
羽飞 已提交
145 146 147 148 149
  const vector<FieldMeta> *trx_fields = TrxKit::instance()->trx_fields();
  if (nullptr == trx_fields) {
    return 0;
  }
  return static_cast<int>(trx_fields->size());
羽飞's avatar
羽飞 已提交
150 151
}

L
Longda 已提交
152 153
const IndexMeta *TableMeta::index(const char *name) const
{
羽飞's avatar
羽飞 已提交
154 155 156 157 158 159 160 161
  for (const IndexMeta &index : indexes_) {
    if (0 == strcmp(index.name(), name)) {
      return &index;
    }
  }
  return nullptr;
}

L
Longda 已提交
162 163
const IndexMeta *TableMeta::find_index_by_field(const char *field) const
{
羽飞's avatar
羽飞 已提交
164 165 166 167 168 169 170 171
  for (const IndexMeta &index : indexes_) {
    if (0 == strcmp(index.field(), field)) {
      return &index;
    }
  }
  return nullptr;
}

L
Longda 已提交
172 173
const IndexMeta *TableMeta::index(int i) const
{
羽飞's avatar
羽飞 已提交
174 175 176
  return &indexes_[i];
}

L
Longda 已提交
177 178
int TableMeta::index_num() const
{
羽飞's avatar
羽飞 已提交
179 180 181
  return indexes_.size();
}

L
Longda 已提交
182 183
int TableMeta::record_size() const
{
羽飞's avatar
羽飞 已提交
184 185 186
  return record_size_;
}

L
Longda 已提交
187 188
int TableMeta::serialize(std::ostream &ss) const
{
羽飞's avatar
羽飞 已提交
189 190 191 192 193

  Json::Value table_value;
  table_value[FIELD_TABLE_NAME] = name_;

  Json::Value fields_value;
L
Longda 已提交
194
  for (const FieldMeta &field : fields_) {
羽飞's avatar
羽飞 已提交
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
    Json::Value field_value;
    field.to_json(field_value);
    fields_value.append(std::move(field_value));
  }

  table_value[FIELD_FIELDS] = std::move(fields_value);

  Json::Value indexes_value;
  for (const auto &index : indexes_) {
    Json::Value index_value;
    index.to_json(index_value);
    indexes_value.append(std::move(index_value));
  }
  table_value[FIELD_INDEXES] = std::move(indexes_value);

  Json::StreamWriterBuilder builder;
  Json::StreamWriter *writer = builder.newStreamWriter();

  std::streampos old_pos = ss.tellp();
  writer->write(table_value, &ss);
  int ret = (int)(ss.tellp() - old_pos);

  delete writer;
  return ret;
}

L
Longda 已提交
221 222
int TableMeta::deserialize(std::istream &is)
{
羽飞's avatar
羽飞 已提交
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
  Json::Value table_value;
  Json::CharReaderBuilder builder;
  std::string errors;

  std::streampos old_pos = is.tellg();
  if (!Json::parseFromStream(builder, is, &table_value, &errors)) {
    LOG_ERROR("Failed to deserialize table meta. error=%s", errors.c_str());
    return -1;
  }

  const Json::Value &table_name_value = table_value[FIELD_TABLE_NAME];
  if (!table_name_value.isString()) {
    LOG_ERROR("Invalid table name. json value=%s", table_name_value.toStyledString().c_str());
    return -1;
  }

  std::string table_name = table_name_value.asString();

  const Json::Value &fields_value = table_value[FIELD_FIELDS];
  if (!fields_value.isArray() || fields_value.size() <= 0) {
    LOG_ERROR("Invalid table meta. fields is not array, json value=%s", fields_value.toStyledString().c_str());
    return -1;
  }

  RC rc = RC::SUCCESS;
  int field_num = fields_value.size();
  std::vector<FieldMeta> fields(field_num);
  for (int i = 0; i < field_num; i++) {
    FieldMeta &field = fields[i];

    const Json::Value &field_value = fields_value[i];
    rc = FieldMeta::from_json(field_value, field);
    if (rc != RC::SUCCESS) {
      LOG_ERROR("Failed to deserialize table meta. table name =%s", table_name.c_str());
      return -1;
    }
  }

羽飞's avatar
羽飞 已提交
261 262
  auto comparator = [](const FieldMeta &f1, const FieldMeta &f2) { return f1.offset() < f2.offset(); };
  std::sort(fields.begin(), fields.end(), comparator);
羽飞's avatar
羽飞 已提交
263 264 265

  name_.swap(table_name);
  fields_.swap(fields);
L
Longda 已提交
266
  record_size_ = fields_.back().offset() + fields_.back().len() - fields_.begin()->offset();
羽飞's avatar
羽飞 已提交
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291

  const Json::Value &indexes_value = table_value[FIELD_INDEXES];
  if (!indexes_value.empty()) {
    if (!indexes_value.isArray()) {
      LOG_ERROR("Invalid table meta. indexes is not array, json value=%s", fields_value.toStyledString().c_str());
      return -1;
    }
    const int index_num = indexes_value.size();
    std::vector<IndexMeta> indexes(index_num);
    for (int i = 0; i < index_num; i++) {
      IndexMeta &index = indexes[i];

      const Json::Value &index_value = indexes_value[i];
      rc = IndexMeta::from_json(*this, index_value, index);
      if (rc != RC::SUCCESS) {
        LOG_ERROR("Failed to deserialize table meta. table name=%s", table_name.c_str());
        return -1;
      }
    }
    indexes_.swap(indexes);
  }

  return (int)(is.tellg() - old_pos);
}

L
Longda 已提交
292 293
int TableMeta::get_serial_size() const
{
羽飞's avatar
羽飞 已提交
294 295 296
  return -1;
}

L
Longda 已提交
297 298
void TableMeta::to_string(std::string &output) const
{}
羽飞's avatar
羽飞 已提交
299

L
Longda 已提交
300 301
void TableMeta::desc(std::ostream &os) const
{
羽飞's avatar
羽飞 已提交
302
  os << name_ << '(' << std::endl;
L
Longda 已提交
303
  for (const auto &field : fields_) {
羽飞's avatar
羽飞 已提交
304 305 306 307 308
    os << '\t';
    field.desc(os);
    os << std::endl;
  }

L
Longda 已提交
309
  for (const auto &index : indexes_) {
羽飞's avatar
羽飞 已提交
310 311 312 313 314 315
    os << '\t';
    index.desc(os);
    os << std::endl;
  }
  os << ')' << std::endl;
}