一,levelDB源码分析(slice)
2016-12-26 13:07
375 查看
#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:
// 默认构造函数
Slice() : data_(""), size_(0) { }
// 构造函数
Slice(const char* d, size_t n) : data_(d), size_(n) { }
//构造函数
Slice(const std::string& s) : data_(s.data()), size_(s.size()) { }
// 构造函数
Slice(const char* s) : data_(s), size_(strlen(s)) { }
//返回字符串指针
const char* data() const { return data_; }
// 返回字符串的大小
size_t size() const { return size_; }
// 检查字符串是否为空
bool empty() const { return size_ == 0; }
//重载赋值运算符
// REQUIRES: n < size()
char operator[](size_t n) const {
assert(n < size());
return data_
;
}
// 清楚字符串
void clear() { data_ = ""; size_ = 0; }
// 字符串往右边移动N个字符,然后size减少N
void remove_prefix(size_t n) {
assert(n <= size());
data_ += n;
size_ -= n;
}
// 转换为string对象
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_
一,slice 是leveldb的基本数据结构,实在了leveldb的字符串封装,实现了const string 和char *与slice的转换
二,对外的API包括构造函数,data(),empty(),size()这几个
#define STORAGE_LEVELDB_INCLUDE_SLICE_H_
#include <assert.h>
#include <stddef.h>
#include <string.h>
#include <string>
namespace leveldb {
class Slice {
public:
// 默认构造函数
Slice() : data_(""), size_(0) { }
// 构造函数
Slice(const char* d, size_t n) : data_(d), size_(n) { }
//构造函数
Slice(const std::string& s) : data_(s.data()), size_(s.size()) { }
// 构造函数
Slice(const char* s) : data_(s), size_(strlen(s)) { }
//返回字符串指针
const char* data() const { return data_; }
// 返回字符串的大小
size_t size() const { return size_; }
// 检查字符串是否为空
bool empty() const { return size_ == 0; }
//重载赋值运算符
// REQUIRES: n < size()
char operator[](size_t n) const {
assert(n < size());
return data_
;
}
// 清楚字符串
void clear() { data_ = ""; size_ = 0; }
// 字符串往右边移动N个字符,然后size减少N
void remove_prefix(size_t n) {
assert(n <= size());
data_ += n;
size_ -= n;
}
// 转换为string对象
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_
一,slice 是leveldb的基本数据结构,实在了leveldb的字符串封装,实现了const string 和char *与slice的转换
二,对外的API包括构造函数,data(),empty(),size()这几个
相关文章推荐
- 谷歌LevelDB之源码分析之1______Slice封装
- Leveldb源码分析之Slice
- LevelDB源码分析3-Slice.md
- levelDB源码分析-Slice
- levelDB源码分析-提纲
- leveldb源码分析 之 入门使用
- Leveldb源码分析--2
- levelDB源码分析-Memtable
- Leveldb源码分析--19
- 非专业levelDB源码解析2:slice
- [Leveldb] 源码分析之一接口文件介绍
- Leveldb源码分析--14
- Leveldb源码分析--13
- Leveldb源码分析--17
- Leveldb源码分析--18
- Leveldb源码分析--12
- levelDB源码分析-Skiplist
- Leveldb源码分析--11
- levelDB源码分析-Arena
- leveldb源码分析 之 入门使用