redis sorted set source code analysis

来源:互联网 发布:dota2 数据bld 编辑:程序博客网 时间:2024/05/21 21:34
 sorted set in t_zset.c file。

Insert operation:
zaddCommand -----> zaddGenericCommand
{  zzlFind
{  zzlDelete
{  zzlInsert
-------- { ziplist : http://blog.nosqlfan.com/html/3919.html
-------- { zzlGetScore
-------- { zzlInsertAt
--------------- { ziplistPush
--------------- { ziplistInsert
{ dictAdd
{ dictFind
{ dictGetKey
{ dictGetVal
{  zslInsert : zskiplist Insert
------------:   http://www.cnblogs.com/xuqiang/archive/2011/05/22/2053516.html
{ zsetConvert : if the element is too large or the list becomes too long executing zzlInsert
-------------- { dictCreate
-------------- { zslCreate
 
#############################################################
args:
@ zs -> dict : 
---------------:  zs -----> zset struct
@ ele -> redisObject
@ &znode -> score
@ elements : add element number
@ redisObject :  http://tech.it168.com/a2011/0818/1234/000001234478_all.shtml
@ zobj is redisObject 
-------- zobj->encoding is the type of sort set key
 #############################################################
Program process:
1. Parsing all the scores
2. lookup the key exist ?
---- if not exist
-------- if has not ziplist or ziplist value < len
------------creatzsetobject
-------- else createzsetziplistobject
-------- dbadd key
3. Insert the key
---- for to insert all element
---- if the node is less the max-ziplist-entries or node value less max-ziplist-value
-------- use ziplist struct 
--------  else use skiplist
---- else if the key already encoding in REDIS_ENCODING_SKINGLIST
-------- insert the node in skiplist and dict
##############################################################
Summary:
1. the time complexity is log(n) because of skiplist
2. the time of dict is 0(1). use mapping like: key ----> score