Chinaunix首页 | 论坛 | 博客
  • 博客访问: 87700
  • 博文数量: 12
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 200
  • 用 户 组: 普通用户
  • 注册时间: 2013-01-23 09:57
文章分类

全部博文(12)

文章存档

2015年(11)

2014年(1)

我的朋友

分类: 服务器与存储

2015-05-24 00:42:18

    SSDB类封装了对存储的各种操作,上文的结尾看到服务进程启动时会打开存储系统数据文件和元数据文件,本文将深入介绍SSDB类和其派生类SSDBImpl。
    SSDB是一个抽象类,它除了定义对存储进行操作的各个接口,还提供了打开数据库文件的方法SSDB::open。
    

点击(此处)折叠或打开

  1. class SSDB{
  2. public:
  3.     SSDB(){}
  4.     virtual ~SSDB(){};
  5.     static SSDB* open(const Options &opt, const std::string &base_dir);

  6.     // return (start, end], not include start
  7.     virtual Iterator* iterator(const std::string &start, const std::string &end, uint64_t limit) = 0;
  8.     virtual Iterator* rev_iterator(const std::string &start, const std::string &end, uint64_t limit) = 0;

  9.     //void flushdb();
  10.     virtual uint64_t size() = 0;
  11.     virtual std::vector<std::string> info() = 0;
  12.     virtual void compact() = 0;
  13.     virtual int key_range(std::vector<std::string> *keys) = 0;

  14.     /* raw operates */

  15.     // repl: whether to sync this operation to slaves
  16.     virtual int raw_set(const Bytes &key, const Bytes &val) = 0;
  17.     virtual int raw_del(const Bytes &key) = 0;
  18.     virtual int raw_get(const Bytes &key, std::string *val) = 0;

  19.     /* key value */

  20.     virtual int set(const Bytes &key, const Bytes &val, char log_type=BinlogType::SYNC) = 0;
  21.     virtual int setnx(const Bytes &key, const Bytes &val, char log_type=BinlogType::SYNC) = 0;
  22.     virtual int del(const Bytes &key, char log_type=BinlogType::SYNC) = 0;
  23.     // -1: error, 1: ok, 0: value is not an integer or out of range
  24.     virtual int incr(const Bytes &key, int64_t by, int64_t *new_val, char log_type=BinlogType::SYNC) = 0;
  25.     virtual int multi_set(const std::vector<Bytes> &kvs, int offset=0, char log_type=BinlogType::SYNC) = 0;
  26.     virtual int multi_del(const std::vector<Bytes> &keys, int offset=0, char log_type=BinlogType::SYNC) = 0;
  27.     virtual int setbit(const Bytes &key, int bitoffset, int on, char log_type=BinlogType::SYNC) = 0;
  28.     virtual int getbit(const Bytes &key, int bitoffset) = 0;
  29.     
  30.     virtual int get(const Bytes &key, std::string *val) = 0;
  31.     virtual int getset(const Bytes &key, std::string *val, const Bytes &newval, char log_type=BinlogType::SYNC) = 0;
  32.     // return (start, end]
  33.     virtual KIterator* scan(const Bytes &start, const Bytes &end, uint64_t limit) = 0;
  34.     virtual KIterator* rscan(const Bytes &start, const Bytes &end, uint64_t limit) = 0;

  35.     /* hash */

  36.     virtual int hset(const Bytes &name, const Bytes &key, const Bytes &val, char log_type=BinlogType::SYNC) = 0;
  37.     virtual int hdel(const Bytes &name, const Bytes &key, char log_type=BinlogType::SYNC) = 0;
  38.     // -1: error, 1: ok, 0: value is not an integer or out of range
  39.     virtual int hincr(const Bytes &name, const Bytes &key, int64_t by, int64_t *new_val, char log_type=BinlogType::SYNC) = 0;

  40.     virtual int64_t hsize(const Bytes &name) = 0;
  41.     virtual int64_t hclear(const Bytes &name) = 0;
  42.     virtual int hget(const Bytes &name, const Bytes &key, std::string *val) = 0;
  43.     virtual int hlist(const Bytes &name_s, const Bytes &name_e, uint64_t limit,
  44.             std::vector<std::string> *list) = 0;
  45.     virtual int hrlist(const Bytes &name_s, const Bytes &name_e, uint64_t limit,
  46.             std::vector<std::string> *list) = 0;
  47.     virtual HIterator* hscan(const Bytes &name, const Bytes &start, const Bytes &end, uint64_t limit) = 0;
  48.     virtual HIterator* hrscan(const Bytes &name, const Bytes &start, const Bytes &end, uint64_t limit) = 0;

  49.     /* zset */

  50.     virtual int zset(const Bytes &name, const Bytes &key, const Bytes &score, char log_type=BinlogType::SYNC) = 0;
  51.     virtual int zdel(const Bytes &name, const Bytes &key, char log_type=BinlogType::SYNC) = 0;
  52.     // -1: error, 1: ok, 0: value is not an integer or out of range
  53.     virtual int zincr(const Bytes &name, const Bytes &key, int64_t by, int64_t *new_val, char log_type=BinlogType::SYNC) = 0;
  54.     
  55.     virtual int64_t zsize(const Bytes &name) = 0;
  56.     /**
  57.      * @return -1: error; 0: not found; 1: found
  58.      */
  59.     virtual int zget(const Bytes &name, const Bytes &key, std::string *score) = 0;
  60.     virtual int64_t zrank(const Bytes &name, const Bytes &key) = 0;
  61.     virtual int64_t zrrank(const Bytes &name, const Bytes &key) = 0;
  62.     virtual ZIterator* zrange(const Bytes &name, uint64_t offset, uint64_t limit) = 0;
  63.     virtual ZIterator* zrrange(const Bytes &name, uint64_t offset, uint64_t limit) = 0;
  64.     /**
  65.      * scan by score, but won't return @key if key.score=score_start.
  66.      * return (score_start, score_end]
  67.      */
  68.     virtual ZIterator* zscan(const Bytes &name, const Bytes &key,
  69.             const Bytes &score_start, const Bytes &score_end, uint64_t limit) = 0;
  70.     virtual ZIterator* zrscan(const Bytes &name, const Bytes &key,
  71.             const Bytes &score_start, const Bytes &score_end, uint64_t limit) = 0;
  72.     virtual int zlist(const Bytes &name_s, const Bytes &name_e, uint64_t limit,
  73.             std::vector<std::string> *list) = 0;
  74.     virtual int zrlist(const Bytes &name_s, const Bytes &name_e, uint64_t limit,
  75.             std::vector<std::string> *list) = 0;
  76.     
  77.     virtual int64_t qsize(const Bytes &name) = 0;
  78.     // @return 0: empty queue, 1: item peeked, -1: error
  79.     virtual int qfront(const Bytes &name, std::string *item) = 0;
  80.     // @return 0: empty queue, 1: item peeked, -1: error
  81.     virtual int qback(const Bytes &name, std::string *item) = 0;
  82.     // @return -1: error, other: the new length of the queue
  83.     virtual int64_t qpush_front(const Bytes &name, const Bytes &item, char log_type=BinlogType::SYNC) = 0;
  84.     virtual int64_t qpush_back(const Bytes &name, const Bytes &item, char log_type=BinlogType::SYNC) = 0;
  85.     // @return 0: empty queue, 1: item popped, -1: error
  86.     virtual int qpop_front(const Bytes &name, std::string *item, char log_type=BinlogType::SYNC) = 0;
  87.     virtual int qpop_back(const Bytes &name, std::string *item, char log_type=BinlogType::SYNC) = 0;
  88.     virtual int qfix(const Bytes &name) = 0;
  89.     virtual int qlist(const Bytes &name_s, const Bytes &name_e, uint64_t limit,
  90.             std::vector<std::string> *list) = 0;
  91.     virtual int qrlist(const Bytes &name_s, const Bytes &name_e, uint64_t limit,
  92.             std::vector<std::string> *list) = 0;
  93.     virtual int qslice(const Bytes &name, int64_t offset, int64_t limit,
  94.             std::vector<std::string> *list) = 0;
  95.     virtual int qget(const Bytes &name, int64_t index, std::string *item) = 0;
  96.     virtual int qset(const Bytes &name, int64_t index, const Bytes &item, char log_type=BinlogType::SYNC) = 0;
  97.     virtual int qset_by_seq(const Bytes &name, uint64_t seq, const Bytes &item, char log_type=BinlogType::SYNC) = 0;
  98. };

SSDB::open

    SSDB::open函数根据配置参数和路径打开数据库文件,由于SSDB是基于leveldb的,所以实际上是调用leveldb的接口创建和打开数据库。

点击(此处)折叠或打开

  1. SSDB* SSDB::open(const Options &opt, const std::string &dir){
  2.     SSDBImpl *ssdb = new SSDBImpl();
  3.     // 如果数据库不存在则创建
  4.     ssdb->options.create_if_missing = true;

  5.     // 打开的.sst文件fd的最大数目
  6.     ssdb->options.max_open_files = opt.max_open_files;

  7.     // LevelDB增加了bloomfilter支持, 可以过滤掉一些key不存在的情况, 减少对磁盘的访问
  8.     ssdb->options.filter_policy = leveldb::NewBloomFilterPolicy(10);

  9.     // block cache 缓存的block数据,block是sstable文件内组织数据的单位,也是从磁盘中读取和写入的单位
  10.     // block默认大小为4KB,可以使用options.block_size设置,最小1KB,最大4MB。对于频繁做scan操作的应用,
  11.     // 可适当调大此参数,对大量小value随机读取的应用,也可尝试调小该参数
  12.     ssdb->options.block_cache = leveldb::NewLRUCache(opt.cache_size * 1048576);

  13.     ssdb->options.block_size = opt.block_size * 1024;
  14.     ssdb->options.write_buffer_size = opt.write_buffer_size * 1024 * 1024;
  15.     ssdb->options.compaction_speed = opt.compaction_speed;
  16.     if(opt.compression == "yes"){
  17.         ssdb->options.compression = leveldb::kSnappyCompression;
  18.     }else{
  19.         ssdb->options.compression = leveldb::kNoCompression;
  20.     }

  21.     leveldb::Status status;
  22.     // 打开leveldb数据库
  23.     status = leveldb::DB::Open(ssdb->options, dir, &ssdb->db);
  24.     if(!status.ok()){
  25.         log_error("open db failed");
  26.         goto err;
  27.     }
  28.     // 创建操作的日志队列,用于主从备份
  29.     ssdb->binlogs = new BinlogQueue(ssdb->db, opt.binlog);

  30.     return ssdb;
  31. err:
  32.     if(ssdb){
  33.         delete ssdb;
  34.     }
  35.     return NULL;
  36. }

open函数主要完成三个任务:
    1. 创建对应的SSDBImple对象;
    2. 利用传入的配置参数打开leveldb数据库作为存储(SSDB的数据文件和元数据文件各使用一个leveldb数据库?对这里还有疑问,MyApplication::run函数中的meta_db到底起到什么作用);
    3. 创建操作日志队列,用于主从同步时将这些操作在从服务器上执行一遍,从而备份数据到从服务器上;  

阅读(3421) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~