leveldb 01 slice 学习

// 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.
//
// Slice is a simple structure containing a pointer into some external
// storage and a size.  The user of a Slice must ensure that the slice
// is not used after the corresponding external storage has been
// deallocated.
//
// Multiple threads can invoke const methods on a Slice without
// external synchronization, but if any of the threads may call a
// non-const method, all threads accessing the same Slice must use
// external synchronization.

#ifndef STORAGE_LEVELDB_INCLUDE_SLICE_H_
#define STORAGE_LEVELDB_INCLUDE_SLICE_H_

#include <assert.h>
#include <stddef.h>
#include <string.h>
#include <string>

namespace leveldb {

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;
}

}  // namespace leveldb


#endif  // STORAGE_LEVELDB_INCLUDE_SLICE_H_

学习 巴山独钓 博客中的leveldb源码阅读文章levelDB源码分析-Slice,自己写学习心得。
1. slice.h 文件是声明和定义了class slice。 由于所有的成员函数都是采用了隐式inline 或者显式 inline定义,所以定义也都放在了.h文件中,没有额外的.cc文件。
2. 成员变量采用了data_这种带的命名方式。以及命名空间结束后使用// namespace leveldb注释说明,#endif结束后使用// STORAGE_LEVELDB_INCLUDE_SLICE_H注释说明,这些是属于谷歌开源代码规范里面的。
3.

void clear() { data_ = ""; size_ = 0; } /*这种在声明成员函数时直接写出定义式,属于隐式inline.*/
 int compare(const Slice& b) const; /*这一行是声明*/

/*这里显式用inline关键字,显式inline定义了此成员函数。*/ 
 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;
} 
/*这里的!=操作符以及另外一个==操作符是显式inline的全局函数,而不是类成员函数.*/
inline bool operator!=(const Slice& x, const Slice& y) {
  return !(x == y);
}

4.整个slice的成员变量只有一个指针data_和无符号整数size_,非常省内存。 data_所指向的内存,是由外部传入的,这样的话,此指向的内存的应该超过slice创建对象的生命周期,也就是指向的内存要比slice对象活的时间长。 slice本身不会去删除这块内存,目前看上去也不会修改这块内存。
5.所有的成员函数都带了const关键字,比如size_t size() const { return size_; } 在此处const表示该成员函数不会修改成员变量的值。
6.构造函数,使用string或者char*构造一个slice对象时,没有使用explicit关键字,这样满足可以隐式转换的需求。 这里是故意的,这样传入slice类型的函数,可以直接传入char*类型和string类型。
7. ==操作符和!=操作符,是故意写成全局函数的。因为考虑到==和!=的参数可以是string或者char*,并且可以隐式转换。参考effetive C++条款24,若所有参数都需类型转换,请为此采用non-member函数。 此条款尤其适用算数操作符重载。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值