簡介
Slice在LevelDB中作爲高效的參數對象而設計,你可以使用任何數據類型來創建leveldb::Slice對象,而且這些對象在LevelDB的很多接口中作爲參數來進行傳遞。本文將介紹LevelDB重要的參數對象Slice的實現,涉及的LevelDB的版本爲1.20。
Slice實現
Slice類的實現在include/leveldb/slice.h中:
class Slice {
public:
// Create an empty slice.
Slice() : data_(""), size_(0) { }
// Create a slice that refers to d[0,n-1].
Slice(const char* d, size_t n) : data_(d), size_(n) { }
// Create a slice that refers to the contents of "s"
Slice(const std::string& s) : data_(s.data()), size_(s.size()) { }
// Create a slice that refers to s[0,strlen(s)-1]
Slice(const char* s) : data_(s), size_(strlen(s)) { }
// Return a pointer to the beginning of the referenced data
const char* data() const { return data_; }
// Return the length (in bytes) of the referenced data
size_t size() const { return size_; }
// Return true iff the length of the referenced data is zero
bool empty() const { return size_ == 0; }
// Return the ith byte in the referenced data.
// REQUIRES: n < size()
char operator[](size_t n) const {
assert(n < size());
return data_[n];
}
// Change this slice to refer to an empty array
void clear() { data_ = ""; size_ = 0; }
// Drop the first "n" bytes from this slice.
void remove_prefix(size_t n) {
assert(n <= size());
data_ += n;
size_ -= n;
}
// Return a string that contains the copy of the referenced data.
std::string ToString() const { return std::string(data_, size_); }
// Three-way comparison. Returns value:
// < 0 iff "*this" < "b",
// == 0 iff "*this" == "b",
// > 0 iff "*this" > "b"
int compare(const Slice& b) const;
// Return true iff "x" is a prefix of "*this"
bool starts_with(const Slice& x) const {
return ((size_ >= x.size_) &&
(memcmp(data_, x.data_, x.size_) == 0));
}
private:
const char* data_;
size_t size_;
// Intentionally copyable
};
inline bool operator==(const Slice& x, const Slice& y) {
return ((x.size() == y.size()) &&
(memcmp(x.data(), y.data(), x.size()) == 0));
}
inline bool operator!=(const Slice& x, const Slice& y) {
return !(x == y);
}
inline int Slice::compare(const Slice& b) const {
const size_t min_len = (size_ < b.size_) ? size_ : b.size_;
int r = memcmp(data_, b.data_, min_len);
if (r == 0) {
if (size_ < b.size_) r = -1;
else if (size_ > b.size_) r = +1;
}
return r;
}
Slice對象僅包含長度和字符指針類型的成員,對象的複製非常高效,但也非常危險。如果擁有一個Slice對象,需要確保其初始化的指針數據也能夠保留,並且要保證其上下文是線程安全的。這也是爲什麼不要在線程間共享Slice對象的原因:它們都具有Slice字符串指針引用的內存存儲,並且這些內存區域可能會被其它線程進行修改。
Slice類提供了4種構造函數,除缺省構造函數外,還支持以字節數組形式的構造函數Slice(const char* d, size_t n),以及支持C++字符串類型的Slice(const std::string& s)和C字符串類型的Slice(const char* s)。
Slice類的函數均以內聯函數的形式提供,提供的方法非常類似於C++的String類。
使用Get和Put操作二進制數據
在理解了Slice的工作機制後,來考慮一下如何使用Get和Put來操作二進制數據。
struct binValues {
int intVal;
double realVal;
};
binValues b = {-99, 3.14};
Slice binSlice((const char*)&b, sizeof(binValues) );
assert( db->Put(WriteOptions(), "BinSample", binSlice).ok() );
std::string binRead;
assert( db->Get(ReadOptions(), "BinSample", &binRead).ok() );
// treat the std::string as a container for arbitary binary data
binValues* b2 = (binValues*)binRead.data();
cout << "Read back binary structure " << b2->intVal << " "
<< b2->realVal << " binary size=" << binRead.size() << endl;
二進制數據可以像字符串一樣存儲,Slice包含了字節數據和長度信息。不同的在於std::string擁有數據,可以認爲是一個安全的數據容器,而Slice僅包含了數據的指針。
參考上面的二進制數據的處理方式,可以處理其它任何類型的數據了。