天天看點

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_() { }

  // 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_ == ; }

  // 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_ = ; }

  // 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_) == ));
  }

 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()) == ));
}

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 == ) {
    if (size_ < b.size_) r = -;
    else if (size_ > b.size_) r = +;
  }
  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_ = ; } /*這種在聲明成員函數時直接寫出定義式,屬于隐式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 == ) {
    if (size_ < b.size_) r = -;
    else if (size_ > b.size_) r = +;
  }
  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函數。 此條款尤其适用算數操作符重載。

繼續閱讀