default_handler.cpp 6.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 & Longda on 2021/4/13.
羽飞's avatar
羽飞 已提交
13 14 15 16 17 18 19 20 21
//

#include "storage/default/default_handler.h"

#include <string>

#include "common/os/path.h"
#include "common/log/log.h"
#include "common/lang/string.h"
羽飞's avatar
羽飞 已提交
22
#include "storage/record/record_manager.h"
羽飞's avatar
羽飞 已提交
23
#include "storage/index/bplus_tree.h"
羽飞's avatar
羽飞 已提交
24 25 26
#include "storage/common/table.h"
#include "storage/common/condition_filter.h"

羽飞's avatar
羽飞 已提交
27 28 29 30 31 32 33 34 35 36 37
static DefaultHandler *default_handler = nullptr;

void DefaultHandler::set_default(DefaultHandler *handler)
{
  if (default_handler != nullptr && handler != nullptr) {
    LOG_ERROR("default handler is setted");
    abort();
  }
  default_handler = handler;
}

38 39
DefaultHandler &DefaultHandler::get_default()
{
羽飞's avatar
羽飞 已提交
40
  return *default_handler;
羽飞's avatar
羽飞 已提交
41 42
}

43 44
DefaultHandler::DefaultHandler()
{}
羽飞's avatar
羽飞 已提交
45

46 47
DefaultHandler::~DefaultHandler() noexcept
{
羽飞's avatar
羽飞 已提交
48 49 50
  destroy();
}

51 52
RC DefaultHandler::init(const char *base_dir)
{
羽飞's avatar
羽飞 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
  // 检查目录是否存在,或者创建
  std::string tmp(base_dir);
  tmp += "/db";
  if (!common::check_directory(tmp)) {
    LOG_ERROR("Cannot access base dir: %s. msg=%d:%s", tmp.c_str(), errno, strerror(errno));
    return RC::GENERIC_ERROR;
  }

  base_dir_ = base_dir;
  db_dir_ = tmp + "/";

  LOG_INFO("Default handler init with %s success", base_dir);
  return RC::SUCCESS;
}

68 69
void DefaultHandler::destroy()
{
羽飞's avatar
羽飞 已提交
70 71
  sync();

72
  for (const auto &iter : opened_dbs_) {
羽飞's avatar
羽飞 已提交
73 74 75 76 77
    delete iter.second;
  }
  opened_dbs_.clear();
}

78 79
RC DefaultHandler::create_db(const char *dbname)
{
羽飞's avatar
羽飞 已提交
80 81 82 83 84 85 86 87 88 89 90 91 92 93
  if (nullptr == dbname || common::is_blank(dbname)) {
    LOG_WARN("Invalid db name");
    return RC::INVALID_ARGUMENT;
  }

  // 如果对应名录已经存在,返回错误
  std::string dbpath = db_dir_ + dbname;
  if (common::is_directory(dbpath.c_str())) {
    LOG_WARN("Db already exists: %s", dbname);
    return RC::SCHEMA_DB_EXIST;
  }

  if (!common::check_directory(dbpath)) {
    LOG_ERROR("Create db fail: %s", dbpath.c_str());
94
    return RC::GENERIC_ERROR;  // io error
羽飞's avatar
羽飞 已提交
95 96 97 98
  }
  return RC::SUCCESS;
}

99 100
RC DefaultHandler::drop_db(const char *dbname)
{
羽飞's avatar
羽飞 已提交
101 102 103
  return RC::GENERIC_ERROR;
}

104 105
RC DefaultHandler::open_db(const char *dbname)
{
羽飞's avatar
羽飞 已提交
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
  if (nullptr == dbname || common::is_blank(dbname)) {
    LOG_WARN("Invalid db name");
    return RC::INVALID_ARGUMENT;
  }

  if (opened_dbs_.find(dbname) != opened_dbs_.end()) {
    return RC::SUCCESS;
  }

  std::string dbpath = db_dir_ + dbname;
  if (!common::is_directory(dbpath.c_str())) {
    return RC::SCHEMA_DB_NOT_EXIST;
  }

  // open db
  Db *db = new Db();
  RC ret = RC::SUCCESS;
  if ((ret = db->init(dbname, dbpath.c_str())) != RC::SUCCESS) {
    LOG_ERROR("Failed to open db: %s. error=%d", dbname, ret);
  }
羽飞's avatar
羽飞 已提交
126 127 128 129
  if ((ret = db->recover()) != RC::SUCCESS) {
    LOG_ERROR("Failed to recover db: %s. error=%d", dbname, ret);
  }

羽飞's avatar
羽飞 已提交
130 131 132 133
  opened_dbs_[dbname] = db;
  return RC::SUCCESS;
}

134 135
RC DefaultHandler::close_db(const char *dbname)
{
羽飞's avatar
羽飞 已提交
136 137 138
  return RC::GENERIC_ERROR;
}

139 140
RC DefaultHandler::execute(const char *sql)
{
羽飞's avatar
羽飞 已提交
141 142 143
  return RC::GENERIC_ERROR;
}

144 145 146
RC DefaultHandler::create_table(
    const char *dbname, const char *relation_name, int attribute_count, const AttrInfo *attributes)
{
羽飞's avatar
羽飞 已提交
147 148 149 150 151 152 153
  Db *db = find_db(dbname);
  if (db == nullptr) {
    return RC::SCHEMA_DB_NOT_OPENED;
  }
  return db->create_table(relation_name, attribute_count, attributes);
}

154 155
RC DefaultHandler::drop_table(const char *dbname, const char *relation_name)
{
羽飞's avatar
羽飞 已提交
156 157 158
  return RC::GENERIC_ERROR;
}

159 160 161
RC DefaultHandler::create_index(
    Trx *trx, const char *dbname, const char *relation_name, const char *index_name, const char *attribute_name)
{
羽飞's avatar
羽飞 已提交
162 163 164 165 166 167 168
  Table *table = find_table(dbname, relation_name);
  if (nullptr == table) {
    return RC::SCHEMA_TABLE_NOT_EXIST;
  }
  return table->create_index(trx, index_name, attribute_name);
}

169 170
RC DefaultHandler::drop_index(Trx *trx, const char *dbname, const char *relation_name, const char *index_name)
{
羽飞's avatar
羽飞 已提交
171 172 173 174

  return RC::GENERIC_ERROR;
}

175 176 177
RC DefaultHandler::insert_record(
    Trx *trx, const char *dbname, const char *relation_name, int value_num, const Value *values)
{
羽飞's avatar
羽飞 已提交
178 179 180 181 182 183 184
  Table *table = find_table(dbname, relation_name);
  if (nullptr == table) {
    return RC::SCHEMA_TABLE_NOT_EXIST;
  }

  return table->insert_record(trx, value_num, values);
}
185 186 187
RC DefaultHandler::delete_record(Trx *trx, const char *dbname, const char *relation_name, int condition_num,
    const Condition *conditions, int *deleted_count)
{
羽飞's avatar
羽飞 已提交
188 189 190 191 192 193 194 195 196 197 198 199 200
  Table *table = find_table(dbname, relation_name);
  if (nullptr == table) {
    return RC::SCHEMA_TABLE_NOT_EXIST;
  }

  CompositeConditionFilter condition_filter;
  RC rc = condition_filter.init(*table, conditions, condition_num);
  if (rc != RC::SUCCESS) {
    return rc;
  }
  return table->delete_record(trx, &condition_filter, deleted_count);
}

201 202 203
RC DefaultHandler::update_record(Trx *trx, const char *dbname, const char *relation_name, const char *attribute_name,
    const Value *value, int condition_num, const Condition *conditions, int *updated_count)
{
羽飞's avatar
羽飞 已提交
204 205 206 207 208 209 210 211
  Table *table = find_table(dbname, relation_name);
  if (nullptr == table) {
    return RC::SCHEMA_TABLE_NOT_EXIST;
  }

  return table->update_record(trx, attribute_name, value, condition_num, conditions, updated_count);
}

212 213 214
Db *DefaultHandler::find_db(const char *dbname) const
{
  std::map<std::string, Db *>::const_iterator iter = opened_dbs_.find(dbname);
羽飞's avatar
羽飞 已提交
215 216 217 218 219 220
  if (iter == opened_dbs_.end()) {
    return nullptr;
  }
  return iter->second;
}

221 222
Table *DefaultHandler::find_table(const char *dbname, const char *table_name) const
{
羽飞's avatar
羽飞 已提交
223 224 225 226 227 228 229 230 231 232 233 234
  if (dbname == nullptr || table_name == nullptr) {
    LOG_WARN("Invalid argument. dbname=%p, table_name=%p", dbname, table_name);
    return nullptr;
  }
  Db *db = find_db(dbname);
  if (nullptr == db) {
    return nullptr;
  }

  return db->find_table(table_name);
}

235 236
RC DefaultHandler::sync()
{
羽飞's avatar
羽飞 已提交
237
  RC rc = RC::SUCCESS;
238
  for (const auto &db_pair : opened_dbs_) {
羽飞's avatar
羽飞 已提交
239 240 241 242 243 244 245 246
    Db *db = db_pair.second;
    rc = db->sync();
    if (rc != RC::SUCCESS) {
      LOG_ERROR("Failed to sync db. name=%s, rc=%d:%s", db->name(), rc, strrc(rc));
      return rc;
    }
  }
  return rc;
羽飞's avatar
羽飞 已提交
247
}