redis sorted set source code analysis

 sorted set in t_zset.c file。

Insert operation:
zaddCommand -----> zaddGenericCommand
{  zzlFind
{  zzlDelete
{  zzlInsert
-------- { zzlGetScore
-------- { zzlInsertAt
--------------- { ziplistPush
--------------- { ziplistInsert
{ dictAdd
{ dictFind
{ dictGetKey
{ dictGetVal
{  zslInsert : zskiplist Insert
{ 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
@ 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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章