二,leveldb源码分析(status)

// Copyright (c) 2011 The LevelDB Authors. All rights reserved.// Use of this source code is governed by a BSD-style license that can be// found in the LICENSE file. See the AUTHORS file for names of contributors. A Status encapsulates the result of an operation. It may indicate success,// or it may indicate an error with an associated error message. Multiple threads can invoke const methods on a Status without// external synchronization, but if any of the threads may call a// non-const method, all threads accessing the same Status must use// external synchronization.#ifndef STORAGE_LEVELDB_INCLUDE_STATUS_H_#define STORAGE_LEVELDB_INCLUDE_STATUS_H_#include #include "leveldb/slice.h"namespace leveldb {class Status { public: // 创建一个status对象默认构造函数 Status() : state_(NULL) { } //析构函数 ~Status() { delete[] state_; } // 拷贝构造函数和赋值运算符 Status(const Status& s); void operator=(const Status& s); // 返回 一个成功的状态 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); } // NULL 就是OK的状态 各种状态检查 bool ok() const { return (state_ == NULL); } // Returns true iff the status indicates a NotFound error. bool IsNotFound() const { return code() == kNotFound; } // Returns true iff the status indicates a Corruption error. bool IsCorruption() const { return code() == kCorruption; } // Returns true iff the status indicates an IOError. bool IsIOError() const { return code() == kIOError; } // Returns true iff the status indicates a NotSupportedError. bool IsNotSupportedError() const { return code() == kNotSupported; } // Returns true iff the status indicates an InvalidArgument. bool IsInvalidArgument() const { return code() == kInvalidArgument; } // Return a string representation of this status suitable for printing. // Returns the string "OK" for success. std::string ToString() const; private: // 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 //把状态消息长度,code码,消息内容,封装到message里面 0-3 是消息长度 4是code码 5以后是消息内容 const char* state_; //枚举的5个状态 enum Code { kOk = 0, kNotFound = 1, kCorruption = 2, kNotSupported = 3, kInvalidArgument = 4, kIOError = 5 }; //由于第四个字节是错误码,返回第一个字节 Code code() const { return (state_ == NULL) ? kOk : static_cast (state_[4]); } //复制一个status Status(Code code, const Slice& msg, const Slice& msg2); static const char* CopyState(const char* s);};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_); }}} // namespace leveldb#endif // STORAGE_LEVELDB_INCLUDE_STATUS_H_// Copyright (c) 2011 The LevelDB Authors. All rights reserved.// Use of this source code is governed by a BSD-style license that can be// found in the LICENSE file. See the AUTHORS file for names of contributors.#include #include "port/port.h"#include "leveldb/status.h"namespace leveldb {//状态copyconst char* Status::CopyState(const char* state) { uint32_t size; memcpy(&size, state, sizeof(size));//从state中取出4个字节给size char* result = new char[size + 5];//+5是因为包含了前面消息长度(4个字节),和code码一个字节 memcpy(result, state, size + 5);// return result;}Status::Status(Code code, const Slice& msg, const Slice& msg2) { assert(code != kOk); const uint32_t len1 = msg.size();//msg的长度 const uint32_t len2 = msg2.size();//msg2的长度 const uint32_t size = len1 + (len2 ? (2 + len2) : 0); char* result = new char[size + 5]; memcpy(result, &size, sizeof(size)); result[4] = static_cast(code); memcpy(result + 5, msg.data(), len1); if (len2) { result[5 + len1] = ':'; result[6 + len1] = ' '; memcpy(result + 7 + len1, msg2.data(), len2); } state_ = result;}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(code())); type = tmp; break; } std::string result(type); uint32_t length; memcpy(&length, state_, sizeof(length)); result.append(state_ + 5, length); return result; }}} // namespace leveldb
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值