persist.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 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
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. */

//
// Created by qiling on 2021/4/13.
//
#include <errno.h>
#include <string.h>
#include <unistd.h>

#include "persist.h"
#include "common/log/log.h"

PersistHandler::PersistHandler()
{}

PersistHandler::~PersistHandler()
{
  close_file();
}

RC PersistHandler::create_file(const char *file_name)
{
  RC rc = RC::SUCCESS;
  if (file_name == nullptr) {
    LOG_ERROR("Failed to create file, because file name is null.");
    rc = RC::FILE_NAME;
  } else if (!file_name_.empty()) {
    LOG_ERROR("Failed to create %s, because a file is already bound.", file_name);
    rc = RC::FILE_BOUND;
  } else {
    int fd;
    fd = open(file_name, O_RDWR | O_CREAT | O_EXCL, S_IREAD | S_IWRITE);
    if (fd < 0) {
      LOG_ERROR("Failed to create %s, due to %s.", file_name, strerror(errno));
L
Longda Feng 已提交
43
      rc = RC::FILE_CREATE;
羽飞's avatar
羽飞 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
    } else {
      file_name_ = file_name;
      close(fd);
      LOG_INFO("Successfully create %s.", file_name);
    }
  }

  return rc;
}

RC PersistHandler::open_file(const char *file_name)
{
  int fd;
  RC rc = RC::SUCCESS;
  if (file_name == nullptr) {
    if (file_name_.empty()) {
      LOG_ERROR("Failed to open file, because no file name.");
L
Longda Feng 已提交
61
      rc = RC::FILE_NAME;
羽飞's avatar
羽飞 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
    } else {
      if ((fd = open(file_name_.c_str(), O_RDWR)) < 0) {
        LOG_ERROR("Failed to open file %s, because %s.", file_name_.c_str(), strerror(errno));
        rc = RC::FILE_OPEN;
      } else {
        file_desc_ = fd;
        LOG_INFO("Successfully open file %s.", file_name_.c_str());
      }
    }
  } else {
    if (!file_name_.empty()) {
      LOG_ERROR("Failed to open file, because a file is already bound.");
      rc = RC::FILE_BOUND;
    } else {
      if ((fd = open(file_name, O_RDWR)) < 0) {
        LOG_ERROR("Failed to open file %s, because %s.", file_name, strerror(errno));
        return RC::FILE_OPEN;
      } else {
        file_name_ = file_name;
        file_desc_ = fd;
        LOG_INFO("Successfully open file %s.", file_name);
      }
    }
  }
L
Longda Feng 已提交
86

羽飞's avatar
羽飞 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
  return rc;
}

RC PersistHandler::close_file()
{
  RC rc = RC::SUCCESS;
  if (file_desc_ >= 0) {
    if (close(file_desc_) < 0) {
      LOG_ERROR("Failed to close file %d:%s, error:%s", file_desc_, file_name_.c_str(), strerror(errno));
      rc = RC::FILE_CLOSE;
    } else {
      file_desc_ = -1;
      LOG_INFO("Successfully close file %d:%s.", file_desc_, file_name_.c_str());
    }
  }

  return rc;
}

RC PersistHandler::remove_file(const char *file_name)
{
  RC rc = RC::SUCCESS;

  if (file_name != nullptr) {
    if (remove(file_name) == 0) {
      LOG_INFO("Successfully remove file %s.", file_name);
    } else {
      LOG_ERROR("Failed to remove file %s, error:%s", file_name, strerror(errno));
      rc = RC::FILE_REMOVE;
    }
  } else if (!file_name_.empty()) {
    if (file_desc_ < 0 || (rc = close_file()) == RC::SUCCESS) {
      if (remove(file_name_.c_str()) == 0) {
        LOG_INFO("Successfully remove file %s.", file_name_.c_str());
      } else {
        LOG_ERROR("Failed to remove file %s, error:%s", file_name_.c_str(), strerror(errno));
        rc = RC::FILE_REMOVE;
      }
    }
  }

  return rc;
}

RC PersistHandler::write_file(int size, const char *data, int64_t *out_size)
{
  RC rc = RC::SUCCESS;
  if (file_name_.empty()) {
    LOG_ERROR("Failed to write, because file is not exist.");
    rc = RC::FILE_NOT_EXIST;
  } else if (file_desc_ < 0) {
    LOG_ERROR("Failed to write, because file is not opened.");
    rc = RC::FILE_NOT_OPENED;
  } else {
    int64_t write_size = 0;
    if ((write_size = write(file_desc_, data, size)) != size) {
L
Longda Feng 已提交
143 144 145 146 147
      LOG_ERROR("Failed to write %d:%s due to %s. Write size: %lld",
          file_desc_,
          file_name_.c_str(),
          strerror(errno),
          write_size);
羽飞's avatar
羽飞 已提交
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
      rc = RC::FILE_WRITE;
    }
    if (out_size != nullptr) {
      *out_size = write_size;
    }
  }

  return rc;
}

RC PersistHandler::write_at(uint64_t offset, int size, const char *data, int64_t *out_size)
{
  RC rc = RC::SUCCESS;
  if (file_name_.empty()) {
    LOG_ERROR("Failed to write, because file is not exist.");
    rc = RC::FILE_NOT_EXIST;
  } else if (file_desc_ < 0) {
    LOG_ERROR("Failed to write, because file is not opened.");
    rc = RC::FILE_NOT_OPENED;
  } else {
    if (lseek(file_desc_, offset, SEEK_SET) == off_t(-1)) {
L
Longda Feng 已提交
169 170 171 172 173
      LOG_ERROR("Failed to write %lld of %d:%s due to failed to seek %s.",
          offset,
          file_desc_,
          file_name_.c_str(),
          strerror(errno));
羽飞's avatar
羽飞 已提交
174 175 176 177
      rc = RC::FILE_SEEK;
    } else {
      int64_t write_size = 0;
      if ((write_size = write(file_desc_, data, size)) != size) {
L
Longda Feng 已提交
178 179 180 181 182 183
        LOG_ERROR("Failed to write %llu of %d:%s due to %s. Write size: %lld",
            offset,
            file_desc_,
            file_name_.c_str(),
            strerror(errno),
            write_size);
羽飞's avatar
羽飞 已提交
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
        rc = RC::FILE_WRITE;
      }
      if (out_size != nullptr) {
        *out_size = write_size;
      }
    }
  }

  return rc;
}

RC PersistHandler::append(int size, const char *data, int64_t *out_size)
{
  RC rc = RC::SUCCESS;
  if (file_name_.empty()) {
    LOG_ERROR("Failed to write, because file is not exist.");
    rc = RC::FILE_NOT_EXIST;
  } else if (file_desc_ < 0) {
    LOG_ERROR("Failed to append, because file is not opened.");
    rc = RC::FILE_NOT_OPENED;
  } else {
    if (lseek(file_desc_, 0, SEEK_END) == off_t(-1)) {
L
Longda Feng 已提交
206 207
      LOG_ERROR(
          "Failed to append file %d:%s due to failed to seek: %s.", file_desc_, file_name_.c_str(), strerror(errno));
羽飞's avatar
羽飞 已提交
208 209 210 211
      rc = RC::FILE_SEEK;
    } else {
      int64_t write_size = 0;
      if ((write_size = write(file_desc_, data, size)) != size) {
L
Longda Feng 已提交
212 213 214 215 216
        LOG_ERROR("Failed to append file %d:%s due to %s. Write size: %lld",
            file_desc_,
            file_name_.c_str(),
            strerror(errno),
            write_size);
羽飞's avatar
羽飞 已提交
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
        rc = RC::FILE_WRITE;
      }
      if (out_size != nullptr) {
        *out_size = write_size;
      }
    }
  }

  return rc;
}

RC PersistHandler::read_file(int size, char *data, int64_t *out_size)
{
  RC rc = RC::SUCCESS;
  if (file_name_.empty()) {
    LOG_ERROR("Failed to read, because file is not exist.");
    rc = RC::FILE_NOT_EXIST;
  } else if (file_desc_ < 0) {
    LOG_ERROR("Failed to read, because file is not opened.");
    rc = RC::FILE_NOT_OPENED;
  } else {
    int64_t read_size = 0;
    if ((read_size = read(file_desc_, data, size)) != size) {
      LOG_ERROR("Failed to read file %d:%s due to %s.", file_desc_, file_name_.c_str(), strerror(errno));
      rc = RC::FILE_READ;
    }
    if (out_size != nullptr) {
      *out_size = read_size;
    }
  }

  return rc;
}

RC PersistHandler::read_at(uint64_t offset, int size, char *data, int64_t *out_size)
{
  RC rc = RC::SUCCESS;
  if (file_name_.empty()) {
    LOG_ERROR("Failed to read, because file is not exist.");
    rc = RC::FILE_NOT_EXIST;
  } else if (file_desc_ < 0) {
    LOG_ERROR("Failed to read, because file is not opened.");
    rc = RC::FILE_NOT_OPENED;
  } else {
    if (lseek(file_desc_, offset, SEEK_SET) == off_t(-1)) {
L
Longda Feng 已提交
262 263 264 265 266
      LOG_ERROR("Failed to read %llu of %d:%s due to failed to seek %s.",
          offset,
          file_desc_,
          file_name_.c_str(),
          strerror(errno));
羽飞's avatar
羽飞 已提交
267 268
      return RC::FILE_SEEK;
    } else {
羽飞's avatar
羽飞 已提交
269 270 271 272 273 274
      ssize_t read_size = read(file_desc_, data, size);
      if (read_size == 0) {
        LOG_TRACE("read file touch the end. file name=%s", file_name_.c_str());
      } else if (read_size < 0) {
        LOG_WARN("failed to read file. file name=%s, offset=%lld, size=%d, error=%s", file_name_.c_str(), offset, size,
                 strerror(errno));
羽飞's avatar
羽飞 已提交
275
        rc = RC::FILE_READ;
羽飞's avatar
羽飞 已提交
276
      } else if (out_size != nullptr) {
羽飞's avatar
羽飞 已提交
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
        *out_size = read_size;
      }
    }
  }

  return rc;
}

RC PersistHandler::seek(uint64_t offset)
{
  RC rc = RC::SUCCESS;
  if (file_name_.empty()) {
    LOG_ERROR("Failed to seek, because file is not exist.");
    rc = RC::FILE_NOT_EXIST;
  } else if (file_desc_ < 0) {
    LOG_ERROR("Failed to seek, because file is not opened.");
    rc = RC::FILE_NOT_OPENED;
  } else if (lseek(file_desc_, offset, SEEK_SET) == off_t(-1)) {
    LOG_ERROR("Failed to seek %llu of %d:%s due to %s.", offset, file_desc_, file_name_.c_str(), strerror(errno));
    rc = RC::FILE_SEEK;
  }

  return rc;
}