levelDB之基础数据结构-Slice

Slice是levelDB中用于操作字符串的数据结构,以字节为单位。

定义与实现

namespace leveldb {

class LEVELDB_EXPORT 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)) {}

  //拷贝函数的定义,默认是浅拷贝
  Slice(const Slice&) = default;
  Slice& operator=(const Slice&) = default;

  // 返回底层的字符数组指针
  const char* data() const { return data_; }

  // 返回data的字节数
  size_t size() const { return size_; }

  // 返回字符串是否为空
  bool empty() const { return size_ == 0; }

  // 重载下标运算符,用于获取第n个字节的字符
  char operator[](size_t n) const {
    assert(n < size());
    return data_[n];
  }

  // 清空slice
  void clear() {
    data_ = "";
    size_ = 0;
  }

  // 移除前n个字节
  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_;
};

// 比较两个slice是否相同
inline bool operator==(const Slice& x, const Slice& y) {
  return ((x.size() == y.size()) &&
          (memcmp(x.data(), y.data(), x.size()) == 0));
}

// 重载 != ,用于比较两个slice不相等
inline bool operator!=(const Slice& x, const Slice& y) { return !(x == y); }

// 比较两个字符串的大小
// 按照字典序比较,0 表示相等;1 表示大于b;-1表示小于b
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是levelDB实现用来操作字符串的封装类,内部通过维护一个char*和字符串所占的字节数size来表示一个slice结构,这样做其实和Redis对与字符串的实现有些许相似之处。

  • C和CPP中字符串都是不能包含’\0’的,而通过维护一个size,字符串中可以包含任何字符
  • 在返回字符串时,通过返回字符串的首地址和字节数减少因为长字符串复制带来的开销
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值