field_meta.cpp 4.5 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
//

L
Longda 已提交
15
#include <common/lang/string.h>
羽飞's avatar
羽飞 已提交
16 17
#include "storage/common/field_meta.h"
#include "common/log/log.h"
羽飞's avatar
羽飞 已提交
18
#include "sql/parser/parse_defs.h"
羽飞's avatar
羽飞 已提交
19 20 21 22 23 24 25 26 27

#include "json/json.h"

const static Json::StaticString FIELD_NAME("name");
const static Json::StaticString FIELD_TYPE("type");
const static Json::StaticString FIELD_OFFSET("offset");
const static Json::StaticString FIELD_LEN("len");
const static Json::StaticString FIELD_VISIBLE("visible");

L
Longda 已提交
28 29
FieldMeta::FieldMeta() : attr_type_(AttrType::UNDEFINED), attr_offset_(-1), attr_len_(0), visible_(false)
{}
羽飞's avatar
羽飞 已提交
30

羽飞's avatar
羽飞 已提交
31 32 33 34 35 36
FieldMeta::FieldMeta(const char *name, AttrType attr_type, int attr_offset, int attr_len, bool visible)
{
  [[maybe_unused]] RC rc = this->init(name, attr_type, attr_offset, attr_len, visible);
  ASSERT(rc == RC::SUCCESS, "failed to init field meta. rc=%s", strrc(rc));
}

L
Longda 已提交
37 38 39
RC FieldMeta::init(const char *name, AttrType attr_type, int attr_offset, int attr_len, bool visible)
{
  if (common::is_blank(name)) {
羽飞's avatar
羽飞 已提交
40 41 42 43 44
    LOG_WARN("Name cannot be empty");
    return RC::INVALID_ARGUMENT;
  }

  if (AttrType::UNDEFINED == attr_type || attr_offset < 0 || attr_len <= 0) {
L
Longda 已提交
45 46
    LOG_WARN(
        "Invalid argument. name=%s, attr_type=%d, attr_offset=%d, attr_len=%d", name, attr_type, attr_offset, attr_len);
羽飞's avatar
羽飞 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59
    return RC::INVALID_ARGUMENT;
  }

  name_ = name;
  attr_type_ = attr_type;
  attr_len_ = attr_len;
  attr_offset_ = attr_offset;
  visible_ = visible;

  LOG_INFO("Init a field with name=%s", name);
  return RC::SUCCESS;
}

L
Longda 已提交
60 61
const char *FieldMeta::name() const
{
羽飞's avatar
羽飞 已提交
62 63 64
  return name_.c_str();
}

L
Longda 已提交
65 66
AttrType FieldMeta::type() const
{
羽飞's avatar
羽飞 已提交
67 68 69
  return attr_type_;
}

L
Longda 已提交
70 71
int FieldMeta::offset() const
{
羽飞's avatar
羽飞 已提交
72 73 74
  return attr_offset_;
}

L
Longda 已提交
75 76
int FieldMeta::len() const
{
羽飞's avatar
羽飞 已提交
77 78 79
  return attr_len_;
}

L
Longda 已提交
80 81
bool FieldMeta::visible() const
{
羽飞's avatar
羽飞 已提交
82 83 84
  return visible_;
}

L
Longda 已提交
85 86 87
void FieldMeta::desc(std::ostream &os) const
{
  os << "field name=" << name_ << ", type=" << attr_type_to_string(attr_type_) << ", len=" << attr_len_
羽飞's avatar
羽飞 已提交
88 89 90
     << ", visible=" << (visible_ ? "yes" : "no");
}

L
Longda 已提交
91 92
void FieldMeta::to_json(Json::Value &json_value) const
{
羽飞's avatar
羽飞 已提交
93 94 95
  json_value[FIELD_NAME] = name_;
  json_value[FIELD_TYPE] = attr_type_to_string(attr_type_);
  json_value[FIELD_OFFSET] = attr_offset_;
L
Longda 已提交
96
  json_value[FIELD_LEN] = attr_len_;
羽飞's avatar
羽飞 已提交
97 98 99
  json_value[FIELD_VISIBLE] = visible_;
}

L
Longda 已提交
100 101
RC FieldMeta::from_json(const Json::Value &json_value, FieldMeta &field)
{
羽飞's avatar
羽飞 已提交
102
  if (!json_value.isObject()) {
L
Longda 已提交
103
    LOG_ERROR("Failed to deserialize field. json is not an object. json value=%s", json_value.toStyledString().c_str());
羽飞's avatar
羽飞 已提交
104
    return RC::INTERNAL;
羽飞's avatar
羽飞 已提交
105 106 107 108 109 110 111 112 113 114
  }

  const Json::Value &name_value = json_value[FIELD_NAME];
  const Json::Value &type_value = json_value[FIELD_TYPE];
  const Json::Value &offset_value = json_value[FIELD_OFFSET];
  const Json::Value &len_value = json_value[FIELD_LEN];
  const Json::Value &visible_value = json_value[FIELD_VISIBLE];

  if (!name_value.isString()) {
    LOG_ERROR("Field name is not a string. json value=%s", name_value.toStyledString().c_str());
羽飞's avatar
羽飞 已提交
115
    return RC::INTERNAL;
羽飞's avatar
羽飞 已提交
116 117 118
  }
  if (!type_value.isString()) {
    LOG_ERROR("Field type is not a string. json value=%s", type_value.toStyledString().c_str());
羽飞's avatar
羽飞 已提交
119
    return RC::INTERNAL;
羽飞's avatar
羽飞 已提交
120 121 122 123
  }

  if (!offset_value.isInt()) {
    LOG_ERROR("Offset is not an integer. json value=%s", offset_value.toStyledString().c_str());
羽飞's avatar
羽飞 已提交
124
    return RC::INTERNAL;
羽飞's avatar
羽飞 已提交
125 126 127
  }
  if (!len_value.isInt()) {
    LOG_ERROR("Len is not an integer. json value=%s", len_value.toStyledString().c_str());
羽飞's avatar
羽飞 已提交
128
    return RC::INTERNAL;
羽飞's avatar
羽飞 已提交
129 130 131
  }
  if (!visible_value.isBool()) {
    LOG_ERROR("Visible field is not a bool value. json value=%s", visible_value.toStyledString().c_str());
羽飞's avatar
羽飞 已提交
132
    return RC::INTERNAL;
羽飞's avatar
羽飞 已提交
133 134 135 136 137
  }

  AttrType type = attr_type_from_string(type_value.asCString());
  if (UNDEFINED == type) {
    LOG_ERROR("Got invalid field type. type=%d", type);
羽飞's avatar
羽飞 已提交
138
    return RC::INTERNAL;
羽飞's avatar
羽飞 已提交
139 140 141 142 143 144 145 146
  }

  const char *name = name_value.asCString();
  int offset = offset_value.asInt();
  int len = len_value.asInt();
  bool visible = visible_value.asBool();
  return field.init(name, type, offset, len, visible);
}