leveldb Status 分析
版權(quán)聲明:本文為 cheng-zhi 原創(chuàng)文章熄守,可以隨意轉(zhuǎn)載幢码,但必須在明確位置注明出處但绕!
Status 簡(jiǎn)介
在 leveldb
中柑蛇,你可以使用 Status
這個(gè)類來(lái)得到你的函數(shù)的返回的狀態(tài)弊琴,它的基本用法如下:
leveldb::Status s = function();
if (!s.ok())
cerr << s.ToString() << endl;
你可在 leveldb/include/status.h
中找到 Status
的定義
Status 的狀態(tài)
leveldb
用下面的一個(gè) enum
類型來(lái)表示所有應(yīng)該出現(xiàn)的狀態(tài):
enum Code {
kOk = 0,
kNotFound = 1,
kCorruption = 2,
kNotSupported = 3,
kInvalidArgument = 4,
kIOError = 5
}
leveldb
使用下面的一組函數(shù)來(lái)分別返回這些狀態(tài):
// 返回索引為 4 的字節(jié)兆龙,代表消息的類型,后面會(huì)有解釋敲董。
Code code() const {
return (state_ == NULL) ? kOk : static_cast<Code>(state_[4]);
}
// Returns true iff the status indicates success.
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; }
Status 的本質(zhì)
在 Status.h
源碼中有一段這樣的注釋:
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
const char* state_;
可以看出 Stauts
本身就是一個(gè)字符串紫皇,只不過(guò) Google
工程師給這個(gè)字符串的某些字節(jié)區(qū)域限定了含義:
- state_[0, 3] 字節(jié)代表消息的長(zhǎng)度,這個(gè)長(zhǎng)度是從 state_[5, ...] 開(kāi)始的腋寨,前面的 5 個(gè)字節(jié)不算聪铺。
- state_[4] 字節(jié)代表消息的類型,就是上面介紹的
enum Code
的 6 種類型萄窜。 - state_[5, ...] 代表實(shí)際的消息體铃剔。
一個(gè)關(guān)鍵的函數(shù)
下面的這一組函數(shù)用來(lái)組合指定的狀態(tài)信息:
// 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);
}
這里都調(diào)用了下面這個(gè)重要的函數(shù),用它來(lái)完成 msg
和 msg2
的拼接:
#include "leveldb/db/leveldbutil.cc"
Status::Status(Code code, const Slice& msg, const Slice& msg2) {
assert(code != kOk);
// 分別得到長(zhǎng)度
const uint32_t len1 = msg.size();
const uint32_t len2 = msg2.size();
// 如果 len2 不為 0查刻,就在消息中加上 ": " 這兩個(gè)字節(jié)
const uint32_t size = len1 + (len2 ? (2 + len2) : 0);
// 實(shí)際的消息需要加上前面的 5 個(gè)字節(jié)键兜,所以要多分配 5 個(gè)
char* result = new char[size + 5];
// 先拷貝前面的 4 個(gè)字節(jié)作為長(zhǎng)度, uint32_t 為 4B 大小
memcpy(result, &size, sizeof(size));
// 索引為 4 的字節(jié)存儲(chǔ)消息的類型穗泵,就是那 6 種之一
result[4] = static_cast<char>(code);
// 拷貝實(shí)際的消息體普气,從索引為 5 的字節(jié)開(kāi)始
memcpy(result + 5, msg.data(), len1);
if (len2) {
result[5 + len1] = ':';
result[6 + len1] = ' ';
// len2 不為 0,就拼接上 ": "
memcpy(result + 7 + len1, msg2.data(), len2);
}
state_ = result;
}
附加的工具函數(shù)
Status
還提供了下面兩個(gè)函數(shù)作為工具佃延,作為學(xué)習(xí)還是不錯(cuò)的:
#include "leveldb/db/leveldbutil.cc"
// 拷貝狀態(tài)现诀,因?yàn)橛址峙淞诵碌膬?nèi)存夷磕,相當(dāng)于深拷貝
const char* Status::CopyState(const char* state) {
uint32_t size;
// 得到 state 的消息體的長(zhǎng)度,消息體長(zhǎng)度存儲(chǔ)在前面的 4 個(gè)字節(jié)中
memcpy(&size, state, sizeof(size));
char* result = new char[size + 5];
// 拷貝消息體
memcpy(result, state, size + 5);
return result;
}
這個(gè)函數(shù)將 Code
的最新的狀態(tài)轉(zhuǎn)換為字符串仔沿,我們常用 Status s = ...
來(lái)接受函數(shù)的返回值坐桩,然后輸出 s.ToString()
:
#include "leveldb/db/leveldbutil.cc"
std::string Status::ToString() const {
if (state_ == NULL) {
return "OK";
} else {
char tmp[30];
const char* type;
// 判斷當(dāng)前的狀態(tài)
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()));
type = tmp;
break;
}
// 拷貝到 result 中
std::string result(type);
uint32_t length;
memcpy(&length, state_, sizeof(length));
result.append(state_ + 5, length);
return result;
}
}
總結(jié)
Status
這個(gè)類既然是用來(lái)表示狀態(tài)的,所以我們?cè)谑褂玫氖褂梅怙保蟛糠智闆r都是得到一個(gè)函數(shù)的返回的狀態(tài)值绵跷,然后進(jìn)行相應(yīng)的判斷即可。