转自:http://luodw.cc/2015/10/15/leveldb-02/

leveldb和redis这样的优秀开源框架都没有使用C++自带的字符串string,redis自己写了个sds,leveldb写了slice,本质上这三个实现原理都是一样的(当然sds是用C实现的),都有成员属性指向字符串的指针和这个字符串的长度。方法无非就是取字符串取字符串长度,字符串拼接等等。

我把slice作为leveldb源码部分的第一个讲解,主要是slice是这个源码最基础的部分,都是别人使用它,它不使用别人。而且相对简单。

我这先上class slice源码,我一一注释:

class Slice {
public:
// Create an empty slice.
//创建空的字符串,用法Slice slice;
Slice() : data_(""), size_(0) { }
// Create a slice that refers to d[0,n-1].
//用一个字符串指针和字符串长度初始化一个Slice
Slice(const char* d, size_t n) : data_(d), size_(n) { }
// Create a slice that refers to the contents of "s",
//用C++字符串初始化Slice
Slice(const std::string& s) : data_(s.data()), size_(s.size()) { }
// Create a slice that refers to s[0,strlen(s)-1]
//用一个字符串指针初始化Slice,
Slice(const char* s) : data_(s), size_(strlen(s)) { }
// Return a pointer to the beginning of the referenced data
// 获取Slice字符串,不能改变值

这前几个函数都是Slice的构造函数,用空字符串,C风格以NULL结尾的字符串,C++ string字符串 来构造Slice。

常用的几个函数

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
// 判断Slice是否为空,如果为空,返回true,如果不为空,则返回false;
bool empty() const { return size_ == 0; }
// Return the ith byte in the referenced data.
// REQUIRES: n < size()
// 重载[]操作符,用户通过slice[n]获取第n个字符 

特定情形下使用的函数。

char operator[](size_t n) const {
assert(n < size());
return data_[n];
}
// Change this slice to refer to an empty array
//将这个Slice清空
void clear() { data_ = ""; size_ = 0; }
// Drop the first "n" bytes from this slice.
//去除Slice前缀n个字符,例如为了取出Status信息,需要去除前5个前缀字符
void remove_prefix(size_t n) {
assert(n <= size());
data_ += n;//指针向前移动n个字符
size_ -= n;//长度减n
}
// Return a string that contains the copy of the referenced data.
//返回Slice的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"
// 判断这个Slice是否以字符串x为前缀
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
};
// 重载==操作符,用于判断Slice==Slice
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;
}

leveldb的字符串Slice还是算比较简单的,相比与redis的sds,sds还有字符串的拼接啦,长整形和字符串互转等等。 简单使用:

#include <cassert>
#include <iostream>
#include <string>
#include <leveldb/db.h>
using namespace std;
int main() {
const char* str = "hello world!";
leveldb::Slice slice(str);
cout << slice.data() << endl;
cout << slice.size() << endl;
return 0;
}

 

 

leveldb源码分析之Slice的更多相关文章

  1. leveldb源码分析--SSTable之block

    在SSTable中主要存储数据的地方是data block,block_builder就是这个专门进行block的组织的地方,我们来详细看看其中的内容,其主要有Add,Finish和CurrentSi ...

  2. leveldb源码分析--WriteBatch

    从[leveldb源码分析--插入删除流程]和WriteBatch其名我们就很轻易的知道,这个是leveldb内部的一个批量写的结构,在leveldb为了提高插入和删除的效率,在其插入过程中都采用了批 ...

  3. leveldb源码分析--Key结构

    [注]本文参考了sparkliang的专栏的Leveldb源码分析--3并进行了一定的重组和排版 经过上一篇文章的分析我们队leveldb的插入流程有了一定的认识,而该文设计最多的又是Batch的概念 ...

  4. Leveldb源码分析--1

    coming from http://blog.csdn.net/sparkliang/article/details/8567602 [前言:看了一点oceanbase,没有意志力继续坚持下去了,暂 ...

  5. leveldb源码分析--日志

    我们知道在一个数据库系统中为了保证数据的可靠性,我们都会记录对系统的操作日志.日志的功能就是用来在系统down掉的时候对数据进行恢复,所以日志系统对一个要求可靠性的存储系统是极其重要的.接下来我们分析 ...

  6. LevelDB源码分析--Cache及Get查找流程

    本打算接下来分析version相关的概念,但是在准备的过程中看到了VersionSet的table_cache_这个变量才想起还有这样一个模块尚未分析,经过权衡觉得leveldb的version相对C ...

  7. leveldb源码分析--SSTable之TableBuilder

    上一篇文章讲述了SSTable的格式以后,本文结合源码解析SSTable是如何生成的. void TableBuilder::Add(const Slice& key, const Slice ...

  8. leveldb源码分析之内存池Arena

    转自:http://luodw.cc/2015/10/15/leveldb-04/ 这篇博客主要讲解下leveldb内存池,内存池很多地方都有用到,像linux内核也有个内存池.内存池的存在主要就是减 ...

  9. 【转】Leveldb源码分析——1

    先来看看Leveldb的基本框架,几大关键组件,如图1-1所示. Leveldb是一种基于operation log的文件系统,是Log-Structured-Merge Tree的典型实现.LSM源 ...

随机推荐

  1. Spring 后台方法 重定向 与 转发

    一.重定向:重定向是客户端行为,在使用时,务必使用全路径,否则可能因为外部环境导致错误 1.URL改变为重定向的URL地址 2.前台页面不能使用Ajax请求提交, 应该使用form表单提交 方法一.参 ...

  2. java 框架-企业级搜索 Solr

    https://blog.csdn.net/cs_hnu_scw/article/details/79388080 一:Solr简介       Solr是一个独立的企业级搜索应用服务器,它对外提供类 ...

  3. ligerui.grid.extend.rowSpan

    扩展LigerUI的Grid中的相同列合并行功能,代码如下:$.extend($.ligerui.controls.Grid.prototype, { _getHtmlFromData:functio ...

  4. echarts重写图例点击事件

    echarts version: 3.1.2 修改图例点击事件样例代码: 当第一次点击图例时,只显示点击的图例. 当还剩一个图例被取消选中后,自动全选中所有图例. var triggerAction ...

  5. Array + two points leetcode.18 - 4Sum

    题面 Given an array nums of n integers and an integer target, are there elements a, b, c, and d in num ...

  6. 【Distributed】分布式任务调度平台

    一.概述 什么是定时任务 二.Java实现定时任务方式 2.1 Thread 2.2 TimerTask 2.3 ScheduledExecutorService 2.4 Quartz 引入maven ...

  7. windows系统编辑过的脚本文件,在linxu上执行报错 /bin/sh^M: bad interpreter: No such file or directory

    如题! 现象: 当时的场景是这样的:我在IDEA中编辑了项目中的脚本sh,然后利用maven打成zip包.把zip包上传到linux服务器解压运行. 当在linux服务器上运行该sh脚本文件时,提示错 ...

  8. 用python文件操作实现复制图片、视频

    图片复制 打开源图片: f_src = open('1.jpg','rb') 读取图片内容并存储到content变量 content = f_src.read() 打开复制后的图片,没有则创建 f_c ...

  9. Nginx系列1.1:ubuntu16.04编译nginx-rtmp流媒体服务器

    1.下载nginx和nginx-rtmp-module nginx官网:nginx.org tar.gz文件 解压缩命令: wget https://nginx.org/download/nginx- ...

  10. Mybatis报错: There is no getter for property named xxx

    在mapper文件中函数的形参上加上注解. 例如: 出现了如下错误:核心错误提示就是There is no getter for property named xxx     ### Error qu ...