LevelDBソース分析:Slice実装の理解-効率的なLevelDBパラメータオブジェクト

21523 ワード

概要
SliceはLevelDBで効率的なパラメータオブジェクトとして設計されており、leveldb::Sliceオブジェクトを作成するために任意のデータ型を使用することができ、これらのオブジェクトはLevelDBの多くのインタフェースでパラメータとして渡されます.ここでは、LevelDBのバージョン1.20に関する重要なパラメータオブジェクトSliceの実装について説明します.
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オブジェクトが1つある場合は、初期化されたポインタデータも保持され、コンテキストがスレッドの安全であることを保証する必要があります.これも、スレッド間で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はデータのポインタのみを含んでいる.上記のバイナリデータの処理方法を参考にして、他の任意のタイプのデータを処理することができます.