index_meta.cpp 2.4 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. */

//
羽飞's avatar
羽飞 已提交
12
// Created by Wangyunlai.wyl on 2021/5/18.
羽飞's avatar
羽飞 已提交
13 14
//

羽飞's avatar
羽飞 已提交
15 16 17
#include "storage/index/index_meta.h"
#include "storage/field/field_meta.h"
#include "storage/table/table_meta.h"
羽飞's avatar
羽飞 已提交
18 19 20 21 22 23 24
#include "common/lang/string.h"
#include "common/log/log.h"
#include "json/json.h"

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

L
Longda 已提交
25 26 27 28
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
羽飞 已提交
29 30 31 32 33 34 35 36
    return RC::INVALID_ARGUMENT;
  }

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

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

L
Longda 已提交
43 44
RC IndexMeta::from_json(const TableMeta &table, const Json::Value &json_value, IndexMeta &index)
{
羽飞's avatar
羽飞 已提交
45 46 47 48
  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());
羽飞's avatar
羽飞 已提交
49
    return RC::INTERNAL;
羽飞's avatar
羽飞 已提交
50 51 52 53
  }

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

  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 已提交
68 69
const char *IndexMeta::name() const
{
羽飞's avatar
羽飞 已提交
70 71 72
  return name_.c_str();
}

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

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