Redis源代码分析

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

Redis(huwu)(yuhanzou)Jun17,2011Redis·Arch_Platform1/32Jun17,2011Redis·Arch_Platform2/321.Redis!42.!42.1.(adlist.h/adlist.c)!42.2.(sds.h/sds.c)!52.3.(dict.h/dict.c)!62.4.(zmalloc.h/zmalloc.h)!93.!113.1.(ae.h/ae.c)!113.2.(anet.h/anet.c)!153.3.(networking.h/networking.c,redis.c/redis.h)!153.4.!174.!194.1.!204.2.!235.!245.1.Snapshot!245.2.AOF!266.!276.1.!276.2.!306.3.!31Redis·Arch_Platform3/321.RedisRedis()Key-Valuestringhashlistsetsortedset2.2.1.(adlist.h/adlist.c)(list)Redisadlist.hadlist.clistNode(next)(prev)void*typedefstructlistNode{structlistNode*prev;structlistNode*next;void*value;}listNode;list(head)(tail)(dup)(free)(match)(value)lentypedefstructlist{listNode*head;listNode*tail;void*(*dup)(void*ptr);void(*free)(void*ptr);int(*match)(void*ptr,void*key);unsignedintlen;}list;listIter(next)direction(AL_START_HEADAL_START_TAILtypedefstructlistIter{listNode*next;intdirection;}listIter;Redislist(listCreate)Rediszmalloc()(listRelease)(listDelNode)(list)freeRedis(value)Rediszfree()listGetIterator()listNext()listReleaseIterator()listRewind()listRewindTail()listiter=listGetIterator(list,AL_START_HEAD);//Redis·Arch_Platform4/32while((node=listNext(iter))!=NULL){DoItWithValue(listNodeValue(node));//DoItWithValue}listReleaseIterator(iter);listDup()dupvaluelistSearchKey()O(N)match2.2.(sds.h/sds.c)RedisRediskeyvaluekeyRedis(BinarySafe)256(8bit)[2]RedisvaluekeyRedissds.hsds.csdschar*typedefchar*sds;sdsnewlen()Redissdshdrlenfreesdshdrsizeof(int)+sizeof(int)+sizeof(char*)+len+freestructsdshdr{intlen;intfree;charbuf[];};sdsnewlen(constvoid*init,size_tinitlen)sizeof(structsdshdr)+initlen+1*initbufbuf’\0’sdsnewlen()sdshdr-bufsdsnewlen(“qqmail”,6)64Redis246+160qqmailsdshdr-buf”qqmail”bufsdshdrsdslen()size_tsdslen(constsdss){structsdshdr*sh=(void*)(s-(sizeof(structsdshdr)));returnsh-len;}(s-(sizeof(structsdshdr))sdshdrsdshdrRedisbufsdschar*Redis·Arch_Platform5/32sdssdssdsnewlen(constvoid*init,size_tinitlen);sdssdsnew(constchar*init);sdssdsempty();zmalloc()sdsempty()voidsdsfree(sdss)size_tsdslen(constsdss)size_tsdsavail(sdss)sdssdssdsgrowzero(sdss,size_tlen);sdssdscatlen(sdss,void*t,size_tlen);sdssdscat(sdss,char*t);sdssdscpylen(sdss,char*t,size_tlen);sdssdscpy(sdss,char*t);sdsgrowzero()sds’\0’freesdscat()tssdscpy()tsssdsgrowzero()sds2.3.(dict.h/dict.c)Redis0/1dicttypedicththt[2]rehashidxiteratorstypedefstructdict{dictType*type;void*privdata;dicththt[2];intrehashidx;/*rehashingnotinprogressifrehashidx==-1*/intiterators;/*numberofiteratorscurrentlyrunning*/}dict;dicthttablesize2sizemarksize-1sizeusedtypedefstructdictht{dictEntry**table;unsignedlongsize;unsignedlongsizemask;unsignedlongused;}dictht;dictTypeHashKeyValueKeyKeyValuetypetypedefstructdictType{unsignedint(*hashFunction)(constvoid*key);void*(*keyDup)(void*privdata,constvoid*key);Redis·Arch_Platform6/32void*(*valDup)(void*privdata,constvoid*obj);int(*keyCompare)(void*privdata,constvoid*key1,constvoid*key2);void(*keyDestructor)(void*privdata,void*key);void(*valDestructor)(void*privdata,void*obj);}dictType;externdictTypedictTypeHeapStringCopyKey;externdictTypedictTypeHeapStrings;externdictTypedictTypeHeapStringCopyKeyValue;RedisValueAPIRedisdict_can_resizedictEnableResize()dictDisableResize()dictResize()dictExpand()dictRehashMilliseconds()dictAdd()_dictExpandIfNeeded()_dictExpandIfNeededsize00used=sizecan_resize==1used/size5max(used,size)dictExpand()if(dictIsRehashing(d))returnDICT_OK;if(d-ht[0].size==0)returndictExpand(d,DICT_HT_INITIAL_SIZE);if(d-ht[0].used=d-ht[0].size&&(dict_can_resize||d-ht[0].used/d-ht[0].sizedict_force_resize_ratio)){returndictExpand(d,((d-ht[0].sized-ht[0].used)?d-ht[0].size:d-ht[0].used)*2);}returnDICT_OK;dictExpand()size2ht[0]ht[0]ht[1]ht[0]ht[1]rehashidx=0ht[0]-table[rehashidx]dicthtn;/*thenewhashtable*/unsignedlongrealsize=_dictNextPower(size);if(dictIsRehashing(d)||d-ht[0].usedsize)returnDICT_ERR;n.size=realsize;n.sizemask=realsize-1;n.table=zcalloc(realsize*sizeof(dictEntry*));n.used=0;if(d-ht[0].table==NULL){d-ht[0]=n;Redis·Arch_Platform7/32returnDICT_OK;}d-ht[1]=n;d-rehashidx=0;returnDICT_OK;dictIsRehashing()rehashidxdictAdd()_dictRehashStep()iterators==0ht[0]ht[1]dictRehash()0ht[0]ht[1]rehashidx-11ht[0]-table[rehashidx]ht[1]rehashidx++used==0ht[1]dictRehash()intdictRehash(dict*d,intn){if(!dictIsRehashing(d))return0;while(n--){dictEntry*de,*nextde;if(d-ht[0].used==0){zfree(d-ht[0].table);d-ht[0]=d-ht[1];_dictReset(&d-ht[1]);d-rehashidx=-1;return0;}while(d-ht[0].table[d-rehashidx]==NULL)d-rehashidx++;de=d-ht[0].table[d-rehashidx];while(de){unsignedinth;nextde=de-next;/*Gettheindexinthenewhashtable*/h=dictHashKey(d,de-key)&d-ht[1].sizemask;de-next=d-ht[1].table[h];d-ht[1].table[h]=de;d-ht[0].used--;d-ht[1].used++;de=nextde;}d-ht[0].table[d-rehashidx]=NULL;d-rehashidx++;}return1;}Redis·Arch_Platform8/32dictGetIterator()dictNext(dictIterator*)0ht[0]table[0]table[1],table[2],...,table[size]ht[1]2.4.(zmalloc.h/zmalloc.h)Redis,zmalloc(),zrealloc()zcalloc()zfree(),Cmalloc(),realloc()callo

1 / 32
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功