LevelDB : Status

本文详细解析了 LevelDB 中 Status 类的设计与实现细节,包括其成员变量 status_ 的作用、构造函数、拷贝构造函数、析构函数以及静态方法等,并介绍了如何通过 Status 对象获取错误信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Status使用示例

leveldb::Status status = leveldb::DB::Open(options, "/tmp/testdb", &db);
if (!status.ok())
{
    cerr << status.ToString() << endl;
}

上面的代码用到了:
0. DB::Open内部应该用到了Status的某个静态方法,构造了一个Status对象返回
1. 拷贝构造函数
2. ok()
3. ToString()

Code

Status是对返回值信息的封装。

  enum Code {
    kOk = 0,
    kNotFound = 1,
    kCorruption = 2,
    kNotSupported = 3,
    kInvalidArgument = 4,
    kIOError = 5
  };

0代表没有出错,其他的表示错误。

ToString

将Code转换成相应的string

std::string Status::ToString() const {
  if (state_ == NULL) {
    return "OK";
  } else {
    char tmp[30];
    const char* type; // 消息类型描述
    switch (code()) {
      case kOk:
        type = "OK";
        break;
      case kNotFound:
        type = "NotFound: ";
        break;
      case kCorruption:
        type = "Corruption: ";
        break;
      case kNotSupported:
        type = "Not implemented: ";
        break;
      case kInvalidArgument:
        type = "Invalid argument: ";
        break;
      case kIOError:
        type = "IO error: ";
        break;
      default:
        snprintf(tmp, sizeof(tmp), "Unknown code(%d): ",
                 static_cast<int>(code())); // 使用snprintf比sprintf安全,防止缓冲区溢出
        type = tmp;
        break;
    }
    std::string result(type); 
    uint32_t length;
    memcpy(&length, state_, sizeof(length));
    result.append(state_ + 5, length); // 最后加上消息具体内容,下面会介绍state_成员,它是一个char数组,下标从5开始表示消息具体内容
    return result;
  }
}

status_

Status类只有一个成员变量 status_,它是一个char型数组,下标为0~3的字节表示消息长度(共4字节),下标为4的字节表示错误代码code,下标为5的字节是具体消息的开头

  // OK status has a NULL state_.  Otherwise, state_ is a new[] array
  // of the following form:
  //    state_[0..3] == length of message
  //    state_[4]    == code
  //    state_[5..]  == message
  const char* state_;

默认构造函数/析构函数

  // Create a success status.
  Status() : state_(NULL) { }
  ~Status() { delete[] state_; } // 不用判断status_是否为NULL,因为delete NULL也不会出错。

copy constructor / copy assignment operator

允许拷贝,所以提供了拷贝控制成员:copy constructor(拷贝构造函数)和 copy assignment operator(拷贝赋值操作符)

inline Status::Status(const Status& s) {
  state_ = (s.state_ == NULL) ? NULL : CopyState(s.state_);
}
inline void Status::operator=(const Status& s) {
  // The following condition catches both aliasing (when this == &s),
  // and the common case where both s and *this are ok.
  if (state_ != s.state_) { // 自赋值检查
    delete[] state_;        // 删除原来的资源
    state_ = (s.state_ == NULL) ? NULL : CopyState(s.state_); // 删除之后指针置为空(好习惯)
  }
}

const char* Status::CopyState(const char* state) {
  uint32_t size;
  memcpy(&size, state, sizeof(size)); // 获得message大小
  char* result = new char[size + 5];  // 重新分配空间
  memcpy(result, state, size + 5);    // 复制message到新空间
  return result;                      // 返回新空间地址
}

将相同的操作提取出来,放到函数CopyState中,供拷贝构造和赋值操作符使用。

静态方法

这些静态方法会构造具体类型的Status对象

// Return a success status.
  static Status OK() { return Status(); }

  // Return error status of an appropriate type.
  static Status NotFound(const Slice& msg, const Slice& msg2 = Slice()) {
    return Status(kNotFound, msg, msg2);
  }
  static Status Corruption(const Slice& msg, const Slice& msg2 = Slice()) {
    return Status(kCorruption, msg, msg2);
  }
  static Status NotSupported(const Slice& msg, const Slice& msg2 = Slice()) {
    return Status(kNotSupported, msg, msg2);
  }
  static Status InvalidArgument(const Slice& msg, const Slice& msg2 = Slice()) {
    return Status(kInvalidArgument, msg, msg2);
  }
  static Status IOError(const Slice& msg, const Slice& msg2 = Slice()) {
    return Status(kIOError, msg, msg2);
  }

它们都是调用了下面的构造函数:

Status::Status(Code code, const Slice& msg, const Slice& msg2) {
  assert(code != kOk);
  const uint32_t len1 = msg.size();
  const uint32_t len2 = msg2.size();
  const uint32_t size = len1 + (len2 ? (2 + len2) : 0);
  char* result = new char[size + 5];
  memcpy(result, &size, sizeof(size));
  result[4] = static_cast<char>(code);
  memcpy(result + 5, msg.data(), len1);
  if (len2) {
    result[5 + len1] = ':';
    result[6 + len1] = ' ';
    memcpy(result + 7 + len1, msg2.data(), len2);
  }
  state_ = result;
}

该构造函数可以接受两个message,最后拼接成这样”msg: msg2”,中间多了:和 空格

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值