index_meta.cpp 2.4 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.wyl on 2021/5/18.
羽飞's avatar
羽飞 已提交
13 14 15 16 17 18 19 20 21 22 23 24 25
//

#include "storage/common/index_meta.h"
#include "storage/common/field_meta.h"
#include "storage/common/table_meta.h"
#include "common/lang/string.h"
#include "common/log/log.h"
#include "rc.h"
#include "json/json.h"

const static Json::StaticString FIELD_NAME("name");
const static Json::StaticString FIELD_FIELD_NAME("field_name");

L
Longda 已提交
26 27 28 29
RC IndexMeta::init(const char *name, const FieldMeta &field)
{
  if (common::is_blank(name)) {
    LOG_ERROR("Failed to init index, name is empty.");
羽飞's avatar
羽飞 已提交
30 31 32 33 34 35 36 37
    return RC::INVALID_ARGUMENT;
  }

  name_ = name;
  field_ = field.name();
  return RC::SUCCESS;
}

L
Longda 已提交
38 39
void IndexMeta::to_json(Json::Value &json_value) const
{
羽飞's avatar
羽飞 已提交
40 41 42 43
  json_value[FIELD_NAME] = name_;
  json_value[FIELD_FIELD_NAME] = field_;
}

L
Longda 已提交
44 45
RC IndexMeta::from_json(const TableMeta &table, const Json::Value &json_value, IndexMeta &index)
{
羽飞's avatar
羽飞 已提交
46 47 48 49 50 51 52 53 54
  const Json::Value &name_value = json_value[FIELD_NAME];
  const Json::Value &field_value = json_value[FIELD_FIELD_NAME];
  if (!name_value.isString()) {
    LOG_ERROR("Index name is not a string. json value=%s", name_value.toStyledString().c_str());
    return RC::GENERIC_ERROR;
  }

  if (!field_value.isString()) {
    LOG_ERROR("Field name of index [%s] is not a string. json value=%s",
L
Longda 已提交
55 56
        name_value.asCString(),
        field_value.toStyledString().c_str());
羽飞's avatar
羽飞 已提交
57 58 59 60 61 62 63 64 65 66 67 68
    return RC::GENERIC_ERROR;
  }

  const FieldMeta *field = table.field(field_value.asCString());
  if (nullptr == field) {
    LOG_ERROR("Deserialize index [%s]: no such field: %s", name_value.asCString(), field_value.asCString());
    return RC::SCHEMA_FIELD_MISSING;
  }

  return index.init(name_value.asCString(), *field);
}

L
Longda 已提交
69 70
const char *IndexMeta::name() const
{
羽飞's avatar
羽飞 已提交
71 72 73
  return name_.c_str();
}

L
Longda 已提交
74 75
const char *IndexMeta::field() const
{
羽飞's avatar
羽飞 已提交
76 77 78
  return field_.c_str();
}

L
Longda 已提交
79 80 81
void IndexMeta::desc(std::ostream &os) const
{
  os << "index name=" << name_ << ", field=" << field_;
羽飞's avatar
羽飞 已提交
82
}