系列目錄
redis源碼分析序
(一)Redis結(jié)構(gòu)解析
(二)結(jié)構(gòu)體分析
(三)dict哈希結(jié)構(gòu)1
(三)dict哈希結(jié)構(gòu)2
(三)dict哈希結(jié)構(gòu)3
(三)dict哈希結(jié)構(gòu)4
(四)sds字符串
(五)sparkline微線圖
(六)ziplist壓縮列表
(七)zipmap壓縮圖
(八)t_hash哈希轉(zhuǎn)換
(九)t_list,t_string的分析
(十)testhelp.h小型測(cè)試框架和redis-check-aof.c日志檢測(cè)
(十一)memtest內(nèi)存檢測(cè)
(十二)redis-check-dump本地?cái)?shù)據(jù)庫(kù)檢測(cè)
(十三)redis-benchmark性能測(cè)試
(十四)rdb.c本地?cái)?shù)據(jù)庫(kù)操
(十五)aof-append only file解析
(十六)config配置文件
(十七)multi事務(wù)操作
(十八)db.c內(nèi)存數(shù)據(jù)庫(kù)操作
(十九)replication主從數(shù)據(jù)復(fù)制的實(shí)現(xiàn)
(二十) ae事件驅(qū)動(dòng)
(二十一)anet網(wǎng)絡(luò)通信的封裝
(二十二)networking網(wǎng)絡(luò)協(xié)議傳輸
(二十三)CRC循環(huán)冗余算法和RAND隨機(jī)數(shù)算法
(二十四)tool工具類(lèi)
(二十五)zmalloc內(nèi)存分配實(shí)現(xiàn)
(二十六)slowLog和hyperloglog
(二十七)rio系統(tǒng)I/O的封裝
(二十八)object創(chuàng)建和釋放redisObject對(duì)象
(二十九)bio后臺(tái)I/O服務(wù)的實(shí)現(xiàn)
(三十) pubsub發(fā)布訂閱模式
(三十一)latency延遲分析處理
(三十二)redis-cli.c客戶(hù)端命令行接口的實(shí)現(xiàn)(1)
(三十三)redis-cli.c客戶(hù)端命令行接口的實(shí)現(xiàn)(2)
(三十四)redis.h服務(wù)端的實(shí)現(xiàn)分析(1)
(三十五)redis.c服務(wù)端的實(shí)現(xiàn)分析(2)
(三十六)Redis中的優(yōu)秀設(shè)計(jì)之處
如果有看過(guò)之前我分析的ziplist壓縮列表的分析的話,理解這個(gè)我覺(jué)得不是什么特別的難題。ziplist壓縮列表和zipmap都采用了動(dòng)態(tài)分配字節(jié)的做法表示長(zhǎng)度,比如通過(guò)固定的字節(jié)表示節(jié)省了不少的空間。同樣帶來(lái)的問(wèn)題就是復(fù)雜的指針移動(dòng),和字符位置移動(dòng)。但總的來(lái)說(shuō),一定是利大于弊了,要不然設(shè)計(jì)者也不會(huì)這么做。ziplist保存的使用一個(gè)列表,zipmap就保存的則是一個(gè)個(gè)鍵值對(duì),通過(guò)key:value key:value的形式連著。下面我給出zipmap的結(jié)構(gòu)構(gòu)成,zipmap其實(shí)也就是一個(gè)超級(jí)長(zhǎng)的字符串。
里面涉及了幾個(gè)變量zmlen,len,free,下面給出完整的解釋?zhuān)?/p>
1/* String -> String Map data structure optimized for size.
2 * This file implements a data structure mapping strings to other strings
3 * implementing an O(n) lookup data structure designed to be very memory
4 * efficient.
5 *
6 * The Redis Hash type uses this data structure for hashes composed of a small
7 * number of elements, to switch to a hash table once a given number of
8 * elements is reached.
9 *
10 * Given that many times Redis Hashes are used to represent objects composed
11 * of few fields, this is a very big win in terms of used memory.
12 *
13 * zipmap壓縮表和ziplist十分類(lèi)似,都做到了內(nèi)存操作效率比較高的
14 * --------------------------------------------------------------------------
15 *
16 * Copyright (c) 2009-2010, Salvatore Sanfilippo
17 * All rights reserved.
18 *
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions are met:
21 *
22 * * Redistributions of source code must retain the above copyright notice,
23 * this list of conditions and the following disclaimer.
24 * * Redistributions in binary form must reproduce the above copyright
25 * notice, this list of conditions and the following disclaimer in the
26 * documentation and/or other materials provided with the distribution.
27 * * Neither the name of Redis nor the names of its contributors may be used
28 * to endorse or promote products derived from this software without
29 * specific prior written permission.
30 *
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS'
32 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
33 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
35 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
36 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
37 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
38 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
39 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGE.
42 */
1/* Memory layout of a zipmap, for the map 'foo' => 'bar', 'hello' => 'world':
2 *
3 * 'foo''bar''hello''world'
4 *
5 * is 1 byte length that holds the current size of the zipmap.
6 * When the zipmap length is greater than or equal to 254, this value
7 * is not used and the zipmap needs to be traversed to find out the length.
8 * 占有著1個(gè)字節(jié),所以他的最多可代表的數(shù)量是254,當(dāng)zipmap中的元素記錄超過(guò)這個(gè)數(shù)時(shí),
9 * 那只能從前往后后遍歷算大小了,和ziplist是不一樣的。
10 *
11 * is the length of the following string (key or value).
12 * lengths are encoded in a single value or in a 5 bytes value.
13 * If the first byte value (as an unsigned 8 bit value) is between 0 and
14 * 252, it's a single-byte length. If it is 253 then a four bytes unsigned
15 * integer follows (in the host byte ordering). A value of 255 is used to
16 * signal the end of the hash. The special value 254 is used to mark
17 * empty space that can be used to add new key/value pairs.
18 * 代表了后面字符串key 或 value的值的長(zhǎng)度,長(zhǎng)度一般被編碼1個(gè)字節(jié)或5個(gè)字節(jié)表示,這個(gè)和ziplist類(lèi)似
19 * 如果后面的字符串長(zhǎng)度小于等于252個(gè),可與用單字節(jié)表示,其他253,254等長(zhǎng)度被用來(lái)表示其他作用了,當(dāng)超過(guò)這個(gè)數(shù)時(shí)候
20 * 則直接按5字節(jié)的方式存儲(chǔ)長(zhǎng)度。
21 *
22 * is the number of free unused bytes after the string, resulting
23 * from modification of values associated to a key. For instance if 'foo'
24 * is set to 'bar', and later 'foo' will be set to 'hi', it will have a
25 * free byte to use if the value will enlarge again later, or even in
26 * order to add a key/value pair if it fits.
27 * 一般來(lái)表示后面的value長(zhǎng)度的空閑值,當(dāng)key:value=“foo”:'bar',后來(lái)被改為“foo”:'hi',空閑長(zhǎng)度就為1了
28 *
29 * is always an unsigned 8 bit number, because if after an
30 * update operation there are more than a few free bytes, the zipmap will be
31 * reallocated to make sure it is as small as possible.
32 * 的數(shù)字一般比較小,如果空閑太大,zipmap會(huì)進(jìn)行調(diào)整大小使map整體變得盡可能小
33 *
34 * The most compact representation of the above two elements hash is actually:
35 * 這是一個(gè)例子:
36 * 'foo''bar''hello''world'
37 * <總鍵值對(duì)數(shù)><第一個(gè)key的長(zhǎng)度>key字符<第一個(gè)value的長(zhǎng)度><空閑長(zhǎng)度開(kāi)始都為0>后面同前
38 * '\x02\x03foo\x03\x00bar\x05hello\x05\x00world\xff'
39 *
40 * Note that because keys and values are prefixed length 'objects',
41 * the lookup will take O(N) where N is the number of elements
42 * in the zipmap and *not* the number of bytes needed to represent the zipmap.
43 * This lowers the constant times considerably.
44 */
說(shuō)到鍵值對(duì),里面最最重要的方法當(dāng)然是根據(jù)key ,setValue的方法了,方法如下:
1/* Set key to value, creating the key if it does not already exist.
2 * If 'update' is not NULL, *update is set to 1 if the key was
3 * already preset, otherwise to 0. */
4unsigned char *zipmapSet(unsigned char *zm, unsigned char *key, unsigned int klen, unsigned char *val, unsigned int vlen, int *update) {
5 unsigned int zmlen, offset;
6 unsigned int freelen, reqlen = zipmapRequiredLength(klen,vlen);
7 unsigned int empty, vempty;
8 unsigned char *p;
9
10 freelen = reqlen;
11 if (update) *update = 0;
12 //尋找key的位置
13 p = zipmapLookupRaw(zm,key,klen,&zmlen);
14 if (p == NULL) {
15 /* Key not found: enlarge */
16 //key的位置沒(méi)有找到,調(diào)整zipmap的大小,準(zhǔn)備添加操作
17 zm = zipmapResize(zm, zmlen+reqlen);
18 p = zm+zmlen-1;
19 zmlen = zmlen+reqlen;
20
21 /* Increase zipmap length (this is an insert) */
22 //如果頭字節(jié)還沒(méi)有達(dá)到最大值,則遞增
23 if (zm[0] < ZIPMAP_BIGLEN) zm[0]++;
24 } else {
25 /* Key found. Is there enough space for the new value? */
26 /* Compute the total length: */
27 if (update) *update = 1;
28 //key的位置以及找到,判斷是否有空間插入新的值
29 freelen = zipmapRawEntryLength(p);
30 if (freelen < reqlen) {
31 /* Store the offset of this key within the current zipmap, so
32 * it can be resized. Then, move the tail backwards so this
33 * pair fits at the current position. */
34 //如果沒(méi)有空間插入新的值,則調(diào)整大小
35 offset = p-zm;
36 zm = zipmapResize(zm, zmlen-freelen+reqlen);
37 p = zm+offset;
38
39 /* The +1 in the number of bytes to be moved is caused by the
40 * end-of-zipmap byte. Note: the *original* zmlen is used. */
41 //移動(dòng)空間以便增加新的值
42 memmove(p+reqlen, p+freelen, zmlen-(offset+freelen+1));
43 zmlen = zmlen-freelen+reqlen;
44 freelen = reqlen;
45 }
46 }
47
48 /* We now have a suitable block where the key/value entry can
49 * be written. If there is too much free space, move the tail
50 * of the zipmap a few bytes to the front and shrink the zipmap,
51 * as we want zipmaps to be very space efficient. */
52 empty = freelen-reqlen;
53 if (empty >= ZIPMAP_VALUE_MAX_FREE) {
54 /* First, move the tail bytes to the front, then resize
55 * the zipmap to be bytes smaller. */
56 offset = p-zm;
57 memmove(p+reqlen, p+freelen, zmlen-(offset+freelen+1));
58 zmlen -= empty;
59 zm = zipmapResize(zm, zmlen);
60 p = zm+offset;
61 vempty = 0;
62 } else {
63 vempty = empty;
64 }
65
66 /* Just write the key + value and we are done. */
67 /* Key: */
68 //定位到插入的位置,首先寫(xiě)入key值
69 p += zipmapEncodeLength(p,klen);
70 memcpy(p,key,klen);
71 p += klen;
72 /* Value: */
73 //key值后面是value值,再次寫(xiě)入
74 p += zipmapEncodeLength(p,vlen);
75 *p++ = vempty;
76 memcpy(p,val,vlen);
77 return zm;
78}
map里返回長(zhǎng)度的方法有點(diǎn)特別,就直接定位了就用一個(gè)字節(jié)存儲(chǔ)長(zhǎng)度:
1/* Return the number of entries inside a zipmap */
2/* 返回map的長(zhǎng)度 */
3unsigned int zipmapLen(unsigned char *zm) {
4 unsigned int len = 0;
5 //如果第一個(gè)長(zhǎng)度小于最大值,則直接返回
6 if (zm[0] < ZIPMAP_BIGLEN) {
7 len = zm[0];
8 } else {
9 //否則變量計(jì)算長(zhǎng)度
10 unsigned char *p = zipmapRewind(zm);
11 while((p = zipmapNext(p,NULL,NULL,NULL,NULL)) != NULL) len++;
12
13 /* Re-store length if small enough */
14 if (len < ZIPMAP_BIGLEN) zm[0] = len;
15 }
16 return len;
17}
平常我們?cè)趓edis客戶(hù)端執(zhí)行set key 'value'命令的時(shí)候,調(diào)用的其實(shí)就是set方法,如下:
1zm = zipmapSet(zm,(unsigned char*) 'name',4, (unsigned char*) 'foo',3,NULL);
2zm = zipmapSet(zm,(unsigned char*) 'surname',7, (unsigned char*) 'foo',3,NULL);
3zm = zipmapSet(zm,(unsigned char*) 'age',3, (unsigned char*) 'foo',3,NULL);
比ziplist方法簡(jiǎn)單許多了,最后給出頭文件
1/* String -> String Map data structure optimized for size.
2 *
3 * See zipmap.c for more info.
4 *
5 * --------------------------------------------------------------------------
6 *
7 * Copyright (c) 2009-2010, Salvatore Sanfilippo
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are met:
12 *
13 * * Redistributions of source code must retain the above copyright notice,
14 * this list of conditions and the following disclaimer.
15 * * Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * * Neither the name of Redis nor the names of its contributors may be used
19 * to endorse or promote products derived from this software without
20 * specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS'
23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 */
1#ifndef _ZIPMAP_H
2#define _ZIPMAP_H
3
4unsigned char *zipmapNew(void); //創(chuàng)建一個(gè)新的壓縮圖
5unsigned char *zipmapSet(unsigned char *zm, unsigned char *key, unsigned int klen, unsigned char *val, unsigned int vlen, int *update); //設(shè)置壓縮圖中的某個(gè)鍵值對(duì)
6unsigned char *zipmapDel(unsigned char *zm, unsigned char *key, unsigned int klen, int *deleted); //刪除壓縮圖上的某個(gè)鍵值對(duì)
7unsigned char *zipmapRewind(unsigned char *zm); //將在zipmapNext中被調(diào)用到
8unsigned char *zipmapNext(unsigned char *zm, unsigned char **key, unsigned int *klen, unsigned char **value, unsigned int *vlen); //取得此鍵值對(duì)的下一個(gè)鍵值對(duì)
9int zipmapGet(unsigned char *zm, unsigned char *key, unsigned int klen, unsigned char **value, unsigned int *vlen); //獲取某個(gè)鍵值對(duì)
10int zipmapExists(unsigned char *zm, unsigned char *key, unsigned int klen); //某個(gè)key值在zipmap中是否存在
11unsigned int zipmapLen(unsigned char *zm); //zipmap壓縮圖的總鍵值對(duì)數(shù)
12size_t zipmapBlobLen(unsigned char *zm); //壓縮圖的序列化到文件中所需大小
13void zipmapRepr(unsigned char *p); //輸出的壓縮圖的具體信息,用于測(cè)試
14
15#endif
最后,基于本人對(duì)redis源代碼分析有一段時(shí)間了,我把分析好的代碼,同步到了我的個(gè)人github上了,放上地址大家可以一起學(xué)習(xí):
github:https://github.com/linyiqun/Redis-Code