table_meta.cpp 8.6 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

羽飞's avatar
羽飞 已提交
18
#include "storage/table/table_meta.h"
羽飞's avatar
羽飞 已提交
19 20 21 22
#include "json/json.h"
#include "common/log/log.h"
#include "storage/trx/trx.h"

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

25
static const Json::StaticString FIELD_TABLE_ID("table_id");
羽飞's avatar
羽飞 已提交
26 27 28 29
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 已提交
30 31 32
TableMeta::TableMeta(const TableMeta &other)
    : name_(other.name_), fields_(other.fields_), indexes_(other.indexes_), record_size_(other.record_size_)
{}
羽飞's avatar
羽飞 已提交
33

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

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

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

  RC rc = RC::SUCCESS;
羽飞's avatar
羽飞 已提交
55 56 57 58 59 60 61 62 63 64 65
  
  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
羽飞 已提交
66 67
    }

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

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

    field_offset += attr_info.length;
  }

  record_size_ = field_offset;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  Json::Value table_value;
193
  table_value[FIELD_TABLE_ID]   = table_id_;
羽飞's avatar
羽飞 已提交
194 195 196
  table_value[FIELD_TABLE_NAME] = name_;

  Json::Value fields_value;
L
Longda 已提交
197
  for (const FieldMeta &field : fields_) {
羽飞's avatar
羽飞 已提交
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
    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 已提交
224 225
int TableMeta::deserialize(std::istream &is)
{
羽飞's avatar
羽飞 已提交
226 227 228 229 230 231 232 233 234 235
  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;
  }

236 237 238 239 240 241 242 243
  const Json::Value &table_id_value = table_value[FIELD_TABLE_ID];
  if (!table_id_value.isInt()) {
    LOG_ERROR("Invalid table id. json value=%s", table_id_value.toStyledString().c_str());
    return -1;
  }

  int32_t table_id = table_id_value.asInt();

羽飞's avatar
羽飞 已提交
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
  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
羽飞 已提交
272 273
  auto comparator = [](const FieldMeta &f1, const FieldMeta &f2) { return f1.offset() < f2.offset(); };
  std::sort(fields.begin(), fields.end(), comparator);
羽飞's avatar
羽飞 已提交
274

275
  table_id_ = table_id;
羽飞's avatar
羽飞 已提交
276 277
  name_.swap(table_name);
  fields_.swap(fields);
L
Longda 已提交
278
  record_size_ = fields_.back().offset() + fields_.back().len() - fields_.begin()->offset();
羽飞's avatar
羽飞 已提交
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303

  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 已提交
304 305
int TableMeta::get_serial_size() const
{
羽飞's avatar
羽飞 已提交
306 307 308
  return -1;
}

L
Longda 已提交
309 310
void TableMeta::to_string(std::string &output) const
{}
羽飞's avatar
羽飞 已提交
311

L
Longda 已提交
312 313
void TableMeta::desc(std::ostream &os) const
{
羽飞's avatar
羽飞 已提交
314
  os << name_ << '(' << std::endl;
L
Longda 已提交
315
  for (const auto &field : fields_) {
羽飞's avatar
羽飞 已提交
316 317 318 319 320
    os << '\t';
    field.desc(os);
    os << std::endl;
  }

L
Longda 已提交
321
  for (const auto &index : indexes_) {
羽飞's avatar
羽飞 已提交
322 323 324 325 326 327
    os << '\t';
    index.desc(os);
    os << std::endl;
  }
  os << ')' << std::endl;
}