ssdb底层实现——ssdb底层是leveldb,leveldb根本上是skiplist(例如为存储多个list items,必然有多个item key,而非暴力string cat),用它来做redis的list和set等,势必在数据结构和算法层面上有诸多不适

本文涉及的产品
云数据库 Tair(兼容Redis),内存型 2GB
Redis 开源版,标准版 2GB
推荐场景:
搭建游戏排行榜
简介:
 我已经在用ssdb的hash结构,存储了很多数据了,但是我现在的用法正确吗? 我使用hash结构合理吗?

1. ssdb数据库说是类似redis,而且他们都有hash结构,但是他们的命名有点不同,ssdb 是(name,key,value) ,其实相对应的redis是(key,field,value),当然了对于使用函数上还是很像的;

   那么问题来了,ssdb的hash 和redis的hash结构,使用上一样吗?
   ssdb中(name,key)都是不能超过 SSDB_KEY_LEN_MAX= 255, redis就没这个限制。

2. ssdb中hash结构是(name,key,value),但leveldb是跳表结构(SkipList)存储的只有(key,value);

  (leveldb的 key 实际上是好长的拼装,对应到ssdb 是 name+key,占用了很多空间);
   std::string dbkey = encode_hash_key(name, key);
   leveldb::Status s = db->Get(leveldb::ReadOptions(), dbkey, val);
   std::string encode_hash_key(const Bytes &name, const Bytes &key){
    std::string buf;
    buf.append(1, DataType::HASH);
    buf.append(1, (uint8_t)name.size());
    buf.append(name.data(), name.size());
    buf.append(1, '=');
    buf.append(key.data(), key.size());
    return buf;
  }

3. ssdb中multi_hget 最好不要用,效率不高 应该用 hscan,下面这段是multi_hget,看得出是在循环调用( serv->ssdb->hget)

  int proc_multi_hget(NetworkServer *net, Link *link, const Request &req, Response *resp){
    CHECK_NUM_PARAMS(3);
    SSDBServer *serv = (SSDBServer *)net->data;
    resp->push_back("ok");
    Request::const_iterator it=req.begin() + 1;
    const Bytes name = *it;
    it ++;
    for(; it!=req.end(); it+=1){
       const Bytes &key = *it;
       std::string val;
       int ret = serv->ssdb->hget(name, key, &val);
       if(ret == 1){
         resp->push_back(key.String());
         resp->push_back(val);
       }
      }
      return 0;
    }
   应该使用hscan ,它的实现是这样的:
   HIterator* SSDBImpl::hscan(const Bytes &name, const Bytes &start, const Bytes &end, uint64_t limit){
    std::string key_start, key_end;
    key_start = encode_hash_key(name, start);
    if(!end.empty()){
        key_end = encode_hash_key(name, end);
    }
    return new HIterator(this->iterator(key_start, key_end, limit), name);
    }
    Iterator* SSDBImpl::iterator(const std::string &start, const std::string &end, uint64_t limit){
    leveldb::Iterator *it;
    leveldb::ReadOptions iterate_options;
    iterate_options.fill_cache = false;
    it = db->NewIterator(iterate_options);
    it->Seek(start);
    if(it->Valid() && it->key() == start){
       it->Next();
    }
    return new Iterator(it, end, limit);
    }
    template<typename Key, class Comparator>
    inline void SkipList<Key,Comparator>::Iterator::Next() {
     assert(Valid());
     node_ = node_->Next(0);
    }

原来看zset 的写入其实是更新了三个数据:

  1. 记录zset的记录总数。 std::string encode_zsize_key(const Bytes &name){ std::string buf; buf.append(1, DataType::ZSIZE); buf.append(name.data(), name.size()); return buf; }

  2. 按照分数排序的排行榜 key=(name+score+key) `std::string encode_zscore_key(const Bytes & name, const Bytes &key, const Bytes &score){ std::string buf; buf.append(1, DataType::ZSCORE); buf.append(1, (uint8_t)name.size()); buf.append(name.data(), name.size());

    	int64_t s = score.Int64();
    	if(s < 0){
    		buf.append(1, '-');
    	}else{
    		buf.append(1, '=');
    	}
    	s = encode_score(s);
    
    	buf.append((char *)&s, sizeof(int64_t));
    	buf.append(1, '=');
    	buf.append(key.data(), key.size());
    	return buf;
    }` 
    
  3. 按照(name + key)对应score值的(kv存储) std::string encode_zset_key(const Bytes &name, const Bytes &key){ std::string buf; buf.append(1, DataType::ZSET); buf.append(1, (uint8_t)name.size()); buf.append(name.data(), name.size()); buf.append(1, (uint8_t)key.size()); buf.append(key.data(), key.size()); return buf; }

下面以zset写入命令看,是如何更新这个三块数据库的。 // returns the number of newly added items static int zset_one(SSDBImpl *ssdb, const Bytes &name, const Bytes &key, const Bytes &new_score, char log_type){ int found = ssdb->zget(name, key, &old_score); if(found == 0 || old_score != new_score){ if(found){ // delete zscore key k1 = encode_zscore_key(name, key, old_score); ssdb->binlogs->Delete(k1); } // add zscore key k2 = encode_zscore_key(name, key, new_score); ssdb->binlogs->Put(k2, ""); // update zset k0 = encode_zset_key(name, key); ssdb->binlogs->Put(k0, new_score); ssdb->binlogs->add_log(log_type, BinlogCommand::ZSET, k0); return found? 0 : 1; } return 0; } int SSDBImpl::zset(const Bytes &name, const Bytes &key, const Bytes &score, char log_type){ Transaction trans(binlogs); int ret = zset_one(this, name, key, score, log_type); if(ret >= 0){ if(ret > 0){ if(incr_zsize(this, name, ret) == -1){ return -1; } } leveldb::Status s = binlogs->commit(); if(!s.ok()){ log_error("zset error: %s", s.ToString().c_str()); return -1; } } return ret; }

发现这种查询用户排行多少这种时,效率就非常差了; int64_t SSDBImpl::zrrank(const Bytes &name, const Bytes &key){ ZIterator *it = ziterator(this, name, "", "", "", INT_MAX, Iterator::BACKWARD); uint64_t ret = 0; while(true){ if(it->next() == false){ ret = -1; break; } if(key == it->key){ break; } ret ++; } delete it; return ret; }

总结: 按照score分数范围遍历是很高效的, 查询用户score分数是 很快的。 但是查询用户的rank排行,效率就很差,要从小到大遍历。

 

转自:https://github.com/sunwsh/sunwsh.github.io/wiki/ssdb%E6%BA%90%E7%A0%81%E5%AD%A6%E4%B9%A0--%E7%AC%AC%E4%B8%80%E5%A4%A9%EF%BC%88hash%E7%BB%93%E6%9E%84%EF%BC%89
















本文转自张昺华-sky博客园博客,原文链接:http://www.cnblogs.com/bonelee/p/6898116.html,如需转载请自行联系原作者

相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore &nbsp; &nbsp; ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库&nbsp;ECS 实例和一台目标数据库&nbsp;RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&amp;RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
相关文章
|
2月前
|
消息中间件 NoSQL Redis
redis数据结构-List
redis数据结构-List
38 1
|
1天前
|
NoSQL 关系型数据库 MySQL
Redis 列表(List)
10月更文挑战第16天
9 2
|
4天前
|
存储 算法 Java
Set接口及其主要实现类(如HashSet、TreeSet)如何通过特定数据结构和算法确保元素唯一性
Java Set因其“无重复”特性在集合框架中独树一帜。本文解析了Set接口及其主要实现类(如HashSet、TreeSet)如何通过特定数据结构和算法确保元素唯一性,并提供了最佳实践建议,包括选择合适的Set实现类和正确实现自定义对象的hashCode()与equals()方法。
17 4
|
4天前
|
消息中间件 存储 监控
redis 的List类型 实现 排行榜
【10月更文挑战第8天】
19 2
|
1月前
|
消息中间件 存储 NoSQL
剖析 Redis List 消息队列的三种消费线程模型
Redis 列表(List)是一种简单的字符串列表,它的底层实现是一个双向链表。 生产环境,很多公司都将 Redis 列表应用于轻量级消息队列 。这篇文章,我们聊聊如何使用 List 命令实现消息队列的功能以及剖析消费者线程模型 。
84 20
剖析 Redis List 消息队列的三种消费线程模型
|
13天前
|
存储 分布式计算 NoSQL
大数据-40 Redis 类型集合 string list set sorted hash 指令列表 执行结果 附截图
大数据-40 Redis 类型集合 string list set sorted hash 指令列表 执行结果 附截图
21 3
|
27天前
|
算法
你对Collection中Set、List、Map理解?
你对Collection中Set、List、Map理解?
31 5
|
4天前
|
存储
ES6中的Set数据结构的常用方法和使用场景
ES6中的Set数据结构的常用方法和使用场景
|
26天前
|
消息中间件 存储 NoSQL
4)深度解密 Redis 的列表(List)
4)深度解密 Redis 的列表(List)
20 1
|
29天前
|
存储 JSON NoSQL
redis基本数据结构(String,Hash,Set,List,SortedSet)【学习笔记】
这篇文章是关于Redis基本数据结构的学习笔记,包括了String、Hash、Set、List和SortedSet的介绍和常用命令。文章解释了每种数据结构的特点和使用场景,并通过命令示例演示了如何在Redis中操作这些数据结构。此外,还提供了一些练习示例,帮助读者更好地理解和应用这些数据结构。
redis基本数据结构(String,Hash,Set,List,SortedSet)【学习笔记】