levelDB源码分析-Slice

来源:互联网 发布:淘宝卖项链的白金 编辑:程序博客网 时间:2024/06/09 19:01
        levelDB中Slice非常简单的数据结构,它包括length和一个指向外部字节数组的指针。为什么使用Slice,而不直接使用std::string呢?
        1、相比返回string,返回Slice的开销会小的多(没有拷贝,Slice中没有实际数据,只有指向数据的指针,开销低)。

        2、leveldb允许key和value包含'\0',不能返回以null结尾的c风格字符串。


string和以null结尾的C风格字符串可以很方便的转换成Slice:
   leveldb::Slice s1 = "hello";  
   std::string str("world");
   leveldb::Slice s2 = str;
Slice也很容易转换成C++风格string:
   std::string str = s1.ToString();
   assert(str == std::string("hello"));
        使用Slice时需要格外小心,因为Slice引用的外部数组是由Slice的使用者保证在Slice的生命周期内外部数组是有效的。比如下面的代码中存在bug:
   leveldb::Slice slice;
   if (...) {
     std::string str = ...;
     slice = str;
   }
   Use(slice);
        当if语句的作用域结束时,str会被析构,slice指向的外部空间就不存在了。

        声明及定义如下:


  
class Slice {public:// Create an empty slice.Slice() : data_(""), size_(0) { }// 创建一个空的Slice// Create a slice that refers to d[0,n-1].Slice(const char* d, size_t n) : data_(d), size_(n) { }// 根据d[0,n-1]创建一个Slice// Create a slice that refers to the contents of "s"Slice(const std::string& s) : data_(s.data()), size_(s.size()) { }// 根据string创建一个Slice// Create a slice that refers to s[0,strlen(s)-1]Slice(const char* s) : data_(s), size_(strlen(s)) { }// 根据s[0, strlen(s)-1]创建一个Slice// Return a pointer to the beginning of the referenced dataconst char* data() const { return data_; }// 返回数据// Return the length (in bytes) of the referenced datasize_t size() const { return size_; }// 返回数据长度// Return true iff the length of the referenced data is zerobool 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 arrayvoid clear() { data_ = ""; size_ = 0; }// 重置// Drop the first "n" bytes from this slice.void remove_prefix(size_t n) {// 删除前缀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_); }// 返回string字符串// Three-way comparison.  Returns value://   <  0 iff "*this" <  "b",//   == 0 iff "*this" == "b",//   >  0 iff "*this" >  "b"int compare(const Slice& b) const;// Slice比较// Return true iff "x" is a prefix of "*this"bool starts_with(const Slice& x) const {// *this是否以Slice x为首return ((size_ >= x.size_) &&(memcmp(data_, x.data_, x.size_) == 0));}private:const char* data_;size_t size_;// 拷贝构造函数及=操作符重载没写,采用默认的操作(直接赋值)// Intentionally copyable};// slice == 运算符重载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 int 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;}


原创粉丝点击