Jinlong's Blog

Redis源代码分析之ziplist

定义

ziplist.c的头部给出了ziplist的定义

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/* ZIPLIST OVERALL LAYOUT:
* The general layout of the ziplist is as follows:
* <zlbytes><zltail><zllen><entry><entry><zlend>
*
* <zlbytes> is an unsigned integer to hold the number of bytes that the
* ziplist occupies. This value needs to be stored to be able to resize the
* entire structure without the need to traverse it first.
*
* <zltail> is the offset to the last entry in the list. This allows a pop
* operation on the far side of the list without the need for full traversal.
*
* <zllen> is the number of entries.When this value is larger than 2**16-2,
* we need to traverse the entire list to know how many items it holds.
*
* <zlend> is a single byte special value, equal to 255, which indicates the
* end of the list.
*
* ZIPLIST ENTRIES:
* Every entry in the ziplist is prefixed by a header that contains two pieces
* of information. First, the length of the previous entry is stored to be
* able to traverse the list from back to front. Second, the encoding with an
* optional string length of the entry itself is stored.
*
* The length of the previous entry is encoded in the following way:
* If this length is smaller than 254 bytes, it will only consume a single
* byte that takes the length as value. When the length is greater than or
* equal to 254, it will consume 5 bytes. The first byte is set to 254 to
* indicate a larger value is following. The remaining 4 bytes take the
* length of the previous entry as value.
*
* The other header field of the entry itself depends on the contents of the
* entry. When the entry is a string, the first 2 bits of this header will hold
* the type of encoding used to store the length of the string, followed by the
* actual length of the string. When the entry is an integer the first 2 bits
* are both set to 1. The following 2 bits are used to specify what kind of
* integer will be stored after this header. An overview of the different
* types and encodings is as follows:
*
* |00pppppp| - 1 byte
* String value with length less than or equal to 63 bytes (6 bits).
* |01pppppp|qqqqqqqq| - 2 bytes
* String value with length less than or equal to 16383 bytes (14 bits).
* |10______|qqqqqqqq|rrrrrrrr|ssssssss|tttttttt| - 5 bytes
* String value with length greater than or equal to 16384 bytes.
* |11000000| - 1 byte
* Integer encoded as int16_t (2 bytes).
* |11010000| - 1 byte
* Integer encoded as int32_t (4 bytes).
* |11100000| - 1 byte
* Integer encoded as int64_t (8 bytes).
* |11110000| - 1 byte
* Integer encoded as 24 bit signed (3 bytes).
* |11111110| - 1 byte
* Integer encoded as 8 bit signed (1 byte).
* |1111xxxx| - (with xxxx between 0000 and 1101) immediate 4 bit integer.
* Unsigned integer from 0 to 12. The encoded value is actually from
* 1 to 13 because 0000 and 1111 can not be used, so 1 should be
* subtracted from the encoded 4 bit value to obtain the right value.
* |11111111| - End of ziplist.
*
* All the integers are represented in little endian byte order.
*/

由上面的的定义可以看出,ziplist大概看起来就是下面这样子
ziplist

  • 其中zllen为4个字节,保存ziplist字节数;
  • zltail保存的是尾节点偏移量,4个字节;
  • zllen保存的是ziplist的长度,2个字节,ziplist的长度超出范围,只能通过遍历来知道ziplist的长度;
  • 接着是一个个的entry,这是ziplist的节点,entry包含三个字段,第一个是prevlen,记录前一个节点的长度,方便逆向遍历,长度为1个字节或者5个字节,第二个是encoding,里面记录了data段的长度,1个字节到5个字节,最后的一个字段是data段,记录数据;

  • 最后是zlend,结束标志,值为255

源代码分析

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
#define ZIP_END 255 //ziplist结尾zlend
#define ZIP_BIGLEN 254 //zllen如果超过ZIP_BIGLEN的话,将会使用5个字节存储,不能使255,否则就跟zlend重复了
/* Different encoding/length possibilities */
#define ZIP_STR_MASK 0xc0
#define ZIP_INT_MASK 0x30
#define ZIP_STR_06B (0 << 6)
#define ZIP_STR_14B (1 << 6)
#define ZIP_STR_32B (2 << 6)
#define ZIP_INT_16B (0xc0 | 0<<4)
#define ZIP_INT_32B (0xc0 | 1<<4)
#define ZIP_INT_64B (0xc0 | 2<<4)
#define ZIP_INT_24B (0xc0 | 3<<4)
#define ZIP_INT_8B 0xfe
/* 4 bit integer immediate encoding */
#define ZIP_INT_IMM_MASK 0x0f
#define ZIP_INT_IMM_MIN 0xf1 /* 11110001 */
#define ZIP_INT_IMM_MAX 0xfd /* 11111101 */
#define ZIP_INT_IMM_VAL(v) (v & ZIP_INT_IMM_MASK)
#define INT24_MAX 0x7fffff
#define INT24_MIN (-INT24_MAX - 1)
/* Macro to determine type */
#define ZIP_IS_STR(enc) (((enc) & ZIP_STR_MASK) < ZIP_STR_MASK)
/* Utility macros */
#define ZIPLIST_BYTES(zl) (*((uint32_t*)(zl))) //返回ziplist字节数
#define ZIPLIST_TAIL_OFFSET(zl) (*((uint32_t*)((zl)+sizeof(uint32_t)))) //返回尾节点偏移量
#define ZIPLIST_LENGTH(zl) (*((uint16_t*)((zl)+sizeof(uint32_t)*2))) //返回ziplist的长度
#define ZIPLIST_HEADER_SIZE (sizeof(uint32_t)*2+sizeof(uint16_t)) //返回ziplist的头部长度,即zlbytes><zltail><zllen>的长度
#define ZIPLIST_END_SIZE (sizeof(uint8_t)) //zlend的长度
#define ZIPLIST_ENTRY_HEAD(zl) ((zl)+ZIPLIST_HEADER_SIZE) //ziplist节点的开始位置
#define ZIPLIST_ENTRY_TAIL(zl) ((zl)+intrev32ifbe(ZIPLIST_TAIL_OFFSET(zl))) //ziplist为尾部节点
#define ZIPLIST_ENTRY_END(zl) ((zl)+intrev32ifbe(ZIPLIST_BYTES(zl))-1) //ziplist结束符zlend
//增加ziplist的长度
#define ZIPLIST_INCR_LENGTH(zl,incr) { \
//如果zllen的长度小于2**16 - 1,那么zllen = zllen + 1,否则不加1,这样的话求ziplist长度时候需要遍历整个ziplist才能知道zllen
if (ZIPLIST_LENGTH(zl) < UINT16_MAX) \
ZIPLIST_LENGTH(zl) = intrev16ifbe(intrev16ifbe(ZIPLIST_LENGTH(zl))+incr); \
}
//ziplist节点结构体(注:并非用来存储ziplist的原生数据)
typedef struct zlentry {
unsigned int prevrawlensize, prevrawlen; //编码前一个节点的长度所需要的字节数,前一个节点的长度
unsigned int lensize, len; //编码本节点长度所需要的字节数,本字节的长度
unsigned int headersize; //ziplist的头长度,包括zlbytes,zltail,zllen
unsigned char encoding; //数据编码
unsigned char *p; //节点数据
} zlentry;
//初始化zlentry
#define ZIPLIST_ENTRY_ZERO(zle) { \
(zle)->prevrawlensize = (zle)->prevrawlen = 0; \
(zle)->lensize = (zle)->len = (zle)->headersize = 0; \
(zle)->encoding = 0; \
(zle)->p = NULL; \
}
/* Extract the encoding from the byte pointed by 'ptr' and set it into
* 'encoding'. */
#define ZIP_ENTRY_ENCODING(ptr, encoding) do { \
(encoding) = (ptr[0]); \
if ((encoding) < ZIP_STR_MASK) (encoding) &= ZIP_STR_MASK; \
} while(0)
void ziplistRepr(unsigned char *zl);
//根据encoding来判断编码整数数据所需要的长度
static unsigned int zipIntSize(unsigned char encoding) {
switch(encoding) {
case ZIP_INT_8B: return 1; //1个字节的整数
case ZIP_INT_16B: return 2; //2
case ZIP_INT_24B: return 3; //...
case ZIP_INT_32B: return 4;
case ZIP_INT_64B: return 8;
default: return 0; /* 4 bit immediate */
}
assert(NULL);
return 0;
}
//给p写入encoding数据(字符串和整数分情况),如果p为空,则直接返回存储encoding所需要的长度
static unsigned int zipEncodeLength(unsigned char *p, unsigned char encoding, unsigned int rawlen) {
unsigned char len = 1, buf[5];
if (ZIP_IS_STR(encoding)) {
/* Although encoding is given it may not be set for strings,
* so we determine it here using the raw length. */
if (rawlen <= 0x3f) {
if (!p) return len;
buf[0] = ZIP_STR_06B | rawlen;
} else if (rawlen <= 0x3fff) {
len += 1;
if (!p) return len;
buf[0] = ZIP_STR_14B | ((rawlen >> 8) & 0x3f);
buf[1] = rawlen & 0xff;
} else {
len += 4;
if (!p) return len;
buf[0] = ZIP_STR_32B;
buf[1] = (rawlen >> 24) & 0xff;
buf[2] = (rawlen >> 16) & 0xff;
buf[3] = (rawlen >> 8) & 0xff;
buf[4] = rawlen & 0xff;
}
} else {
//如果是整数直接返回1(整数encoding只需要一个字节来保存)
if (!p) return len;
buf[0] = encoding;
}
/* Store this length at p */
memcpy(p,buf,len);
return len;
}
//解码ptr位置的encoding,把编码长度赋值给lensize,把data段长度赋值给len
#define ZIP_DECODE_LENGTH(ptr, encoding, lensize, len) do { \
ZIP_ENTRY_ENCODING((ptr), (encoding)); \
if ((encoding) < ZIP_STR_MASK) { \
if ((encoding) == ZIP_STR_06B) { \
(lensize) = 1; \
(len) = (ptr)[0] & 0x3f; \
} else if ((encoding) == ZIP_STR_14B) { \
(lensize) = 2; \
(len) = (((ptr)[0] & 0x3f) << 8) | (ptr)[1]; \
} else if (encoding == ZIP_STR_32B) { \
(lensize) = 5; \
(len) = ((ptr)[1] << 24) | \
((ptr)[2] << 16) | \
((ptr)[3] << 8) | \
((ptr)[4]); \
} else { \
assert(NULL); \
} \
} else { \
(lensize) = 1; \
(len) = zipIntSize(encoding); \
} \
} while(0);
//根据前一个节点的长度len在指针p的位置编码prevlen,如果p为NULL,则返回编码该prevlen所需要的长度
static unsigned int zipPrevEncodeLength(unsigned char *p, unsigned int len) {
if (p == NULL) {
//如果len小于254,则用一个字节编码即可,如果不是则要5个字节
return (len < ZIP_BIGLEN) ? 1 : sizeof(len)+1;
} else {
if (len < ZIP_BIGLEN) {
//写数据
p[0] = len;
return 1;
} else {
p[0] = ZIP_BIGLEN;
memcpy(p+1,&len,sizeof(len));
memrev32ifbe(p+1);
return 1+sizeof(len);
}
}
}
//prevlen的编码,用于prevlen如果大于254的情况下
static void zipPrevEncodeLengthForceLarge(unsigned char *p, unsigned int len) {
if (p == NULL) return;
//前一个字节赋值为254,后面的四个字节保存len
p[0] = ZIP_BIGLEN;
memcpy(p+1,&len,sizeof(len));
memrev32ifbe(p+1);
}
//返回在ptr位置的prevlen的编码长度
#define ZIP_DECODE_PREVLENSIZE(ptr, prevlensize) do { \
if ((ptr)[0] < ZIP_BIGLEN) { \
(prevlensize) = 1; \
} else { \
(prevlensize) = 5; \
} \
} while(0);
//解码在ptr位置的prevlen,把它的值保存在prevlen变量中
#define ZIP_DECODE_PREVLEN(ptr, prevlensize, prevlen) do { \
//先获取编码长度
ZIP_DECODE_PREVLENSIZE(ptr, prevlensize); \
if ((prevlensize) == 1) { \
(prevlen) = (ptr)[0]; \
} else if ((prevlensize) == 5) { \
assert(sizeof((prevlensize)) == 4); \
memcpy(&(prevlen), ((char*)(ptr)) + 1, 4); \
memrev32ifbe(&prevlen); \
} \
} while(0);
//计算新的prevlen与旧的prevlen相差的字节数
static int zipPrevLenByteDiff(unsigned char *p, unsigned int len) {
unsigned int prevlensize;
//先获取旧的prevlen的编码长度
ZIP_DECODE_PREVLENSIZE(p, prevlensize);
//用现在需要的长度减去旧的prevlen的编码长度,算出diff
return zipPrevEncodeLength(NULL, len) - prevlensize;
}
//算出整个节点的长度
static unsigned int zipRawEntryLength(unsigned char *p) {
unsigned int prevlensize, encoding, lensize, len;
//先获取prevlen的编码长度
ZIP_DECODE_PREVLENSIZE(p, prevlensize);
//在获取encoding的编码长度lensize,和解码出data段的长度,len
ZIP_DECODE_LENGTH(p + prevlensize, encoding, lensize, len);
//加起来就是一个节点的总长度,
return prevlensize + lensize + len;
}
//尝试把字符串entry转换成整形,存在v中
static int zipTryEncoding(unsigned char *entry, unsigned int entrylen, long long *v, unsigned char *encoding) {
long long value;
//字符串长度超过32位或者字符串长度为0的时候,直接返回0
if (entrylen >= 32 || entrylen == 0) return 0;
//如果可以转换的话
if (string2ll((char*)entry,entrylen,&value)) {
//判断转换出来的整数处于什么范围,选择最短的编码长度
if (value >= 0 && value <= 12) {
*encoding = ZIP_INT_IMM_MIN+value;
} else if (value >= INT8_MIN && value <= INT8_MAX) {
*encoding = ZIP_INT_8B;
} else if (value >= INT16_MIN && value <= INT16_MAX) {
*encoding = ZIP_INT_16B;
} else if (value >= INT24_MIN && value <= INT24_MAX) {
*encoding = ZIP_INT_24B;
} else if (value >= INT32_MIN && value <= INT32_MAX) {
*encoding = ZIP_INT_32B;
} else {
*encoding = ZIP_INT_64B;
}
*v = value;
return 1;
}
return 0;
}
//保存值在p位置,以encoding的格式保存
static void zipSaveInteger(unsigned char *p, int64_t value, unsigned char encoding) {
int16_t i16;
int32_t i32;
int64_t i64;
//判断encoding的类型,根据类型来写入value值在data段
if (encoding == ZIP_INT_8B) {
((int8_t*)p)[0] = (int8_t)value;
} else if (encoding == ZIP_INT_16B) {
i16 = value;
memcpy(p,&i16,sizeof(i16));
memrev16ifbe(p);
} else if (encoding == ZIP_INT_24B) {
i32 = value<<8;
memrev32ifbe(&i32);
memcpy(p,((uint8_t*)&i32)+1,sizeof(i32)-sizeof(uint8_t));
} else if (encoding == ZIP_INT_32B) {
i32 = value;
memcpy(p,&i32,sizeof(i32));
memrev32ifbe(p);
} else if (encoding == ZIP_INT_64B) {
i64 = value;
memcpy(p,&i64,sizeof(i64));
memrev64ifbe(p);
} else if (encoding >= ZIP_INT_IMM_MIN && encoding <= ZIP_INT_IMM_MAX) {
/* Nothing to do, the value is stored in the encoding itself. */
} else {
assert(NULL);
}
}
//根据encoding来读取Integer,原理同上,注释略
static int64_t zipLoadInteger(unsigned char *p, unsigned char encoding) {
int16_t i16;
int32_t i32;
int64_t i64, ret = 0;
if (encoding == ZIP_INT_8B) {
ret = ((int8_t*)p)[0];
} else if (encoding == ZIP_INT_16B) {
memcpy(&i16,p,sizeof(i16));
memrev16ifbe(&i16);
ret = i16;
} else if (encoding == ZIP_INT_32B) {
memcpy(&i32,p,sizeof(i32));
memrev32ifbe(&i32);
ret = i32;
} else if (encoding == ZIP_INT_24B) {
i32 = 0;
memcpy(((uint8_t*)&i32)+1,p,sizeof(i32)-sizeof(uint8_t));
memrev32ifbe(&i32);
ret = i32>>8;
} else if (encoding == ZIP_INT_64B) {
memcpy(&i64,p,sizeof(i64));
memrev64ifbe(&i64);
ret = i64;
} else if (encoding >= ZIP_INT_IMM_MIN && encoding <= ZIP_INT_IMM_MAX) {
ret = (encoding & ZIP_INT_IMM_MASK)-1;
} else {
assert(NULL);
}
return ret;
}
//解码节点信息,保存在结构体e中
static void zipEntry(unsigned char *p, zlentry *e) {
//获取prevlen,以及其编码长度
ZIP_DECODE_PREVLEN(p, e->prevrawlensize, e->prevrawlen);
//获取encoding和其编码长度,和data段的长度len
ZIP_DECODE_LENGTH(p + e->prevrawlensize, e->encoding, e->lensize, e->len);
//计算节点“头”的长度,即prevlen的编码长度加上encoding的编码长度
e->headersize = e->prevrawlensize + e->lensize;
e->p = p;
}
//创建一个新的ziplist
unsigned char *ziplistNew(void) {
unsigned int bytes = ZIPLIST_HEADER_SIZE+1;
unsigned char *zl = zmalloc(bytes);
ZIPLIST_BYTES(zl) = intrev32ifbe(bytes);
ZIPLIST_TAIL_OFFSET(zl) = intrev32ifbe(ZIPLIST_HEADER_SIZE);
ZIPLIST_LENGTH(zl) = 0;
zl[bytes-1] = ZIP_END;
return zl;
}
//从新分配zl的大小
static unsigned char *ziplistResize(unsigned char *zl, unsigned int len) {
zl = zrealloc(zl,len);
ZIPLIST_BYTES(zl) = intrev32ifbe(len);
zl[len-1] = ZIP_END;
return zl;
}
//连锁更新函数,因为当插入一个新的节点时候,新节点的后一个元素的prevlen要重新编码,由此
//新节点的后后个元素就又要更新prevlen了...由此导致一系列的连锁更新
static unsigned char *__ziplistCascadeUpdate(unsigned char *zl, unsigned char *p) {
size_t curlen = intrev32ifbe(ZIPLIST_BYTES(zl)), rawlen, rawlensize;
size_t offset, noffset, extra;
unsigned char *np;
zlentry cur, next;
//如果p还没到达尾部的话
while (p[0] != ZIP_END) {
//解码p位置上的节点,把相关信息保存在cur结构体中
zipEntry(p, &cur);
//整个节点的大小
rawlen = cur.headersize + cur.len;
//编码这个节点的大小所需要的字节数
rawlensize = zipPrevEncodeLength(NULL,rawlen);
//如果p位置所在的节点后面是zlend,也就是后面没有节点了,跳出
if (p[rawlen] == ZIP_END) break;
//把下一个节点的信息读取到next结构体中
zipEntry(p+rawlen, &next);
//如果下一个节点的prevlen编码长度与编码rawlen所需要的长度一样,跳出
if (next.prevrawlen == rawlen) break;
//如果下一个节点的prevlen编码长度不够
if (next.prevrawlensize < rawlensize) {
offset = p-zl;
//计算需要的额外空间extra
extra = rawlensize-next.prevrawlensize;
//重新分配
zl = ziplistResize(zl,curlen+extra);
p = zl+offset;
//np为下一个节点的位置
np = p+rawlen;
//noffset为下一个节点的偏移量
noffset = np-zl;
//如果np不是尾节点,则更新zltail,如果np是尾节点,则不用更新,因为np的位置相对之前没有变
if ((zl+intrev32ifbe(ZIPLIST_TAIL_OFFSET(zl))) != np) {
ZIPLIST_TAIL_OFFSET(zl) =
intrev32ifbe(intrev32ifbe(ZIPLIST_TAIL_OFFSET(zl))+extra);
}
//内存移动
memmove(np+rawlensize,
np+next.prevrawlensize,
curlen-noffset-next.prevrawlensize-1);
//把rawlen编码进np的prevlen字段中
zipPrevEncodeLength(np,rawlen);
//跟新p和curlen
p += rawlen;
curlen += extra;
} else {
//如果下一个节点的prevlen的编码长度比rawlensize要大
if (next.prevrawlensize > rawlensize) {
//这说明要缩小np的prevlen编码长度,但是我们采用懒惰策略,暂时不缩小prelen的编码长度,而是把rawlen先写入np的prevlen中
zipPrevEncodeLengthForceLarge(p+rawlen,rawlen);
} else {
//如果长度刚刚好,则编码rawlen到np的prevlen字段中
zipPrevEncodeLength(p+rawlen,rawlen);
}
/* Stop here, as the raw length of "next" has not changed. */
break;
}
}
return zl;
}
/* Delete "num" entries, starting at "p". Returns pointer to the ziplist. */
static unsigned char *__ziplistDelete(unsigned char *zl, unsigned char *p, unsigned int num) {
unsigned int i, totlen, deleted = 0;
size_t offset;
int nextdiff = 0;
zlentry first, tail;
zipEntry(p, &first);
for (i = 0; p[0] != ZIP_END && i < num; i++) {
p += zipRawEntryLength(p);
deleted++;
}
totlen = p-first.p;
if (totlen > 0) {
if (p[0] != ZIP_END) {
/* Storing `prevrawlen` in this entry may increase or decrease the
* number of bytes required compare to the current `prevrawlen`.
* There always is room to store this, because it was previously
* stored by an entry that is now being deleted. */
nextdiff = zipPrevLenByteDiff(p,first.prevrawlen);
p -= nextdiff;
zipPrevEncodeLength(p,first.prevrawlen);
/* Update offset for tail */
ZIPLIST_TAIL_OFFSET(zl) =
intrev32ifbe(intrev32ifbe(ZIPLIST_TAIL_OFFSET(zl))-totlen);
/* When the tail contains more than one entry, we need to take
* "nextdiff" in account as well. Otherwise, a change in the
* size of prevlen doesn't have an effect on the *tail* offset. */
zipEntry(p, &tail);
if (p[tail.headersize+tail.len] != ZIP_END) {
ZIPLIST_TAIL_OFFSET(zl) =
intrev32ifbe(intrev32ifbe(ZIPLIST_TAIL_OFFSET(zl))+nextdiff);
}
/* Move tail to the front of the ziplist */
memmove(first.p,p,
intrev32ifbe(ZIPLIST_BYTES(zl))-(p-zl)-1);
} else {
/* The entire tail was deleted. No need to move memory. */
ZIPLIST_TAIL_OFFSET(zl) =
intrev32ifbe((first.p-zl)-first.prevrawlen);
}
/* Resize and update length */
offset = first.p-zl;
zl = ziplistResize(zl, intrev32ifbe(ZIPLIST_BYTES(zl))-totlen+nextdiff);
ZIPLIST_INCR_LENGTH(zl,-deleted);
p = zl+offset;
/* When nextdiff != 0, the raw length of the next entry has changed, so
* we need to cascade the update throughout the ziplist */
if (nextdiff != 0)
zl = __ziplistCascadeUpdate(zl,p);
}
return zl;
}
/* Insert item at "p". */
//在位置p中插入节点,数据后移
static unsigned char *__ziplistInsert(unsigned char *zl, unsigned char *p, unsigned char *s, unsigned int slen) {
size_t curlen = intrev32ifbe(ZIPLIST_BYTES(zl)), reqlen;
unsigned int prevlensize, prevlen = 0;
size_t offset;
int nextdiff = 0;
unsigned char encoding = 0;
long long value = 123456789; /* initialized to avoid warning. Using a value
that is easy to see if for some reason
we use it uninitialized. */
zlentry tail;
//判断位置p是不是zlend
if (p[0] != ZIP_END) {
//把位置p中所在的节点的[编码前一个节点长度所需的字节数]和[前一个节点的长度]计算出来
ZIP_DECODE_PREVLEN(p, prevlensize, prevlen);
} else {
//如果位置p是zlend,
unsigned char *ptail = ZIPLIST_ENTRY_TAIL(zl); //返回尾节点
//如果尾节点不为zlend的话,也就是ziplist不为空时
if (ptail[0] != ZIP_END) {
//把尾部节点的长度计算出来
prevlen = zipRawEntryLength(ptail);
}
}
//判断字符串数据是否可以用整数来表示,如果可以,则用整数类型表示,以节省空间
if (zipTryEncoding(s,slen,&value,&encoding)) {
//获取存储该整数数据所需要的字节数
reqlen = zipIntSize(encoding);
} else {
//如果不能转化为整数数据,直接把字符串长度付给reqlen
reqlen = slen;
}
//reqlen加上编码prevlen需要的字节数
reqlen += zipPrevEncodeLength(NULL,prevlen);
//reqlen加上编码len需要的字节数
reqlen += zipEncodeLength(NULL,encoding,slen);
//因为加入新节点之后,新节点的后一个节点的prevlen字段先前保存的时候新节点的前一个节点的长度
//现在保存的是新节点的长度,所以长度可能有所改变,长度改变用nextdiff来计算
nextdiff = (p[0] != ZIP_END) ? zipPrevLenByteDiff(p,reqlen) : 0;
//对存储ziplist的内存进行realloc,因为zl的地址可能会改变,所以要先保存好p到zl的offset
offset = p-zl;
zl = ziplistResize(zl,curlen+reqlen+nextdiff);
p = zl+offset; //恢复p
//如果插入位置不在ziplist的尾部
if (p[0] != ZIP_END) {
//数据后移,为新节点腾出空间
memmove(p+reqlen,p-nextdiff,curlen-offset-1+nextdiff);
//更新新节点的后一个节点的prevlen
zipPrevEncodeLength(p+reqlen,reqlen);
//更新tail位置,看到这里大家可能有点懵逼,这里为什么不用加上nextdiff的长度的呢,别急,继续往下看
ZIPLIST_TAIL_OFFSET(zl) =
intrev32ifbe(intrev32ifbe(ZIPLIST_TAIL_OFFSET(zl))+reqlen);
/* When the tail contains more than one entry, we need to take
* "nextdiff" in account as well. Otherwise, a change in the
* size of prevlen doesn't have an effect on the *tail* offset. */
//把新节点后一个节点的信息提取到*tail结构体中
zipEntry(p+reqlen, &tail);
//如果新节点后一个节点不是尾节点,那么就要加上nextdiff(解答了上面的疑惑)
if (p[reqlen+tail.headersize+tail.len] != ZIP_END) {
ZIPLIST_TAIL_OFFSET(zl) =
intrev32ifbe(intrev32ifbe(ZIPLIST_TAIL_OFFSET(zl))+nextdiff);
}
} else {
//如果新节点就是ziplist的为节点,那么就更新zltail
ZIPLIST_TAIL_OFFSET(zl) = intrev32ifbe(p-zl);
}
//如果nextdiff不为0,那么就执行连锁更新
if (nextdiff != 0) {
offset = p-zl;
zl = __ziplistCascadeUpdate(zl,p+reqlen);
p = zl+offset;
}
//把prelen写入新节点中
p += zipPrevEncodeLength(p,prevlen);
//把len(encoding)写入新节点中
p += zipEncodeLength(p,encoding,slen);
//如果数据是字符串,那么宝贝数据到data段
if (ZIP_IS_STR(encoding)) {
memcpy(p,s,slen);
} else {
//如果是整数就整数
zipSaveInteger(p,value,encoding);
}
//给ziplist的长度加1
ZIPLIST_INCR_LENGTH(zl,1);
return zl;
}
//合并两个压缩表first和second,两个中比较大的一个压缩表会被realloc以足以存储合并后的结果,另一个会被释放掉
unsigned char *ziplistMerge(unsigned char **first, unsigned char **second) {
//任何一个参数为NULL的话,返回NULL
if (first == NULL || *first == NULL || second == NULL || *second == NULL)
return NULL;
//如果这两个ziplist是同一个ziplist的话,返回NULL
if (*first == *second)
return NULL;
//第一个ziplist的字节数
size_t first_bytes = intrev32ifbe(ZIPLIST_BYTES(*first));
//第一个ziplist的节点数
size_t first_len = intrev16ifbe(ZIPLIST_LENGTH(*first));
//第二个ziplist的字节数
size_t second_bytes = intrev32ifbe(ZIPLIST_BYTES(*second));
//第二个ziplist的节点数
size_t second_len = intrev16ifbe(ZIPLIST_LENGTH(*second));
int append;
unsigned char *source, *target;
size_t target_bytes, source_bytes;
/* Pick the largest ziplist so we can resize easily in-place.
* We must also track if we are now appending or prepending to
* the target ziplist. */
//如果第一个ziplist的节点数大于第二个ziplist的话
if (first_len >= second_len) {
//那么就把second合并到first
target = *first;
target_bytes = first_bytes;
source = *second;
source_bytes = second_bytes;
append = 1;
} else {
//否则,把first合并到second
target = *second;
target_bytes = second_bytes;
source = *first;
source_bytes = first_bytes;
append = 0;
}
//计算合并后的字节数
size_t zlbytes = first_bytes + second_bytes -
ZIPLIST_HEADER_SIZE - ZIPLIST_END_SIZE;
//计算合并后的节点数
size_t zllength = first_len + second_len;
//zllength应该小于2**16 - 1,否则把2**16 - 1赋给zllength
zllength = zllength < UINT16_MAX ? zllength : UINT16_MAX;
//分别求出firs和second的尾节点偏移
size_t first_offset = intrev32ifbe(ZIPLIST_TAIL_OFFSET(*first));
size_t second_offset = intrev32ifbe(ZIPLIST_TAIL_OFFSET(*second));
//给target分配空间(两个ziplist合起来的空间)
target = zrealloc(target, zlbytes);
if (append) {
//如果把second(source)加到first(target)
//把second拷贝到first后
memcpy(target + target_bytes - ZIPLIST_END_SIZE,
source + ZIPLIST_HEADER_SIZE,
source_bytes - ZIPLIST_HEADER_SIZE);
} else {
//如果把first(source)加到second(target)
//先把second后移,腾出空间给first
memmove(target + source_bytes - ZIPLIST_END_SIZE,
target + ZIPLIST_HEADER_SIZE,
target_bytes - ZIPLIST_HEADER_SIZE);
//把first拷贝到腾出的空间里面
memcpy(target, source, source_bytes - ZIPLIST_END_SIZE);
}
//更新ziplist的zlbytes
ZIPLIST_BYTES(target) = intrev32ifbe(zlbytes);
//更新ziplist的zllen
ZIPLIST_LENGTH(target) = intrev16ifbe(zllength);
//更新尾节点偏移
ZIPLIST_TAIL_OFFSET(target) = intrev32ifbe(
(first_bytes - ZIPLIST_END_SIZE) +
(second_offset - ZIPLIST_HEADER_SIZE));
//连锁更新
target = __ziplistCascadeUpdate(target, target+first_offset);
//释放资源
if (append) {
zfree(*second);
*second = NULL;
*first = target;
} else {
zfree(*first);
*first = NULL;
*second = target;
}
return target;
}
//把s加到ziplist开始或者末尾
unsigned char *ziplistPush(unsigned char *zl, unsigned char *s, unsigned int slen, int where) {
unsigned char *p;
p = (where == ZIPLIST_HEAD) ? ZIPLIST_ENTRY_HEAD(zl) : ZIPLIST_ENTRY_END(zl);
return __ziplistInsert(zl,p,s,slen);
}
//返回index位置上的节点,如果不存在返回NULL, 如果index未负数,从尾节点开始向前遍历
unsigned char *ziplistIndex(unsigned char *zl, int index) {
unsigned char *p;
unsigned int prevlensize, prevlen = 0;
//如果为负数,从后往前遍历
if (index < 0) {
index = (-index)-1;
p = ZIPLIST_ENTRY_TAIL(zl);
if (p[0] != ZIP_END) {
ZIP_DECODE_PREVLEN(p, prevlensize, prevlen);
while (prevlen > 0 && index--) {
p -= prevlen;
ZIP_DECODE_PREVLEN(p, prevlensize, prevlen);
}
}
} else {
p = ZIPLIST_ENTRY_HEAD(zl);
while (p[0] != ZIP_END && index--) {
p += zipRawEntryLength(p);
}
}
return (p[0] == ZIP_END || index > 0) ? NULL : p;
}
//返回在p位置的节点的下一个节点
unsigned char *ziplistNext(unsigned char *zl, unsigned char *p) {
((void) zl);
/* "p" could be equal to ZIP_END, caused by ziplistDelete,
* and we should return NULL. Otherwise, we should return NULL
* when the *next* element is ZIP_END (there is no next entry). */
if (p[0] == ZIP_END) {
return NULL;
}
p += zipRawEntryLength(p);
if (p[0] == ZIP_END) {
return NULL;
}
return p;
}
//返回上一个节点
unsigned char *ziplistPrev(unsigned char *zl, unsigned char *p) {
unsigned int prevlensize, prevlen = 0;
/* Iterating backwards from ZIP_END should return the tail. When "p" is
* equal to the first element of the list, we're already at the head,
* and should return NULL. */
if (p[0] == ZIP_END) {
p = ZIPLIST_ENTRY_TAIL(zl);
return (p[0] == ZIP_END) ? NULL : p;
} else if (p == ZIPLIST_ENTRY_HEAD(zl)) {
return NULL;
} else {
ZIP_DECODE_PREVLEN(p, prevlensize, prevlen);
assert(prevlen > 0);
return p-prevlen;
}
}
//获取p位置上的值,如果是字符串,则存于sstr中,如果是整数,则存于sval中
unsigned int ziplistGet(unsigned char *p, unsigned char **sstr, unsigned int *slen, long long *sval) {
zlentry entry;
if (p == NULL || p[0] == ZIP_END) return 0;
if (sstr) *sstr = NULL;
//提取节点信息
zipEntry(p, &entry);
//如果是字符串
if (ZIP_IS_STR(entry.encoding)) {
if (sstr) {
*slen = entry.len;
*sstr = p+entry.headersize;
}
} else {
if (sval) {
//如果是整数直接根据enconding提取p+entry.headersize上的数据存在sval中
*sval = zipLoadInteger(p+entry.headersize,entry.encoding);
}
}
return 1;
}
//包装函数,在p位置插入字符窜s,slen为s的长度
unsigned char *ziplistInsert(unsigned char *zl, unsigned char *p, unsigned char *s, unsigned int slen) {
return __ziplistInsert(zl,p,s,slen);
}
/* Delete a single entry from the ziplist, pointed to by *p.
* Also update *p in place, to be able to iterate over the
* ziplist, while deleting entries. */
unsigned char *ziplistDelete(unsigned char *zl, unsigned char **p) {
size_t offset = *p-zl;
zl = __ziplistDelete(zl,*p,1);
/* Store pointer to current element in p, because ziplistDelete will
* do a realloc which might result in a different "zl"-pointer.
* When the delete direction is back to front, we might delete the last
* entry and end up with "p" pointing to ZIP_END, so check this. */
*p = zl+offset;
return zl;
}
//删除指定位置上的节点
unsigned char *ziplistDeleteRange(unsigned char *zl, int index, unsigned int num) {
unsigned char *p = ziplistIndex(zl,index);
return (p == NULL) ? zl : __ziplistDelete(zl,p,num);
}
//比较p位置的节点与sstr的数据是否相等
unsigned int ziplistCompare(unsigned char *p, unsigned char *sstr, unsigned int slen) {
zlentry entry;
unsigned char sencoding;
long long zval, sval;
if (p[0] == ZIP_END) return 0;
//提取当前节点的信息到entry结构体
zipEntry(p, &entry);
if (ZIP_IS_STR(entry.encoding)) {
//如果是当前节点的数据段是字符串的话,进行字符串比较
if (entry.len == slen) {
return memcmp(p+entry.headersize,sstr,slen) == 0;
} else {
return 0;
}
} else {
//如果不是字符串,先尝试把sstr转换成整数,然后再对其进行比较
if (zipTryEncoding(sstr,slen,&sval,&sencoding)) {
zval = zipLoadInteger(p+entry.headersize,entry.encoding);
return zval == sval;
}
}
return 0;
}
//给出vstr,查找与之相等的节点,skip指定每隔skip个节点比较一次,如果没有找到,返回NULL
unsigned char *ziplistFind(unsigned char *p, unsigned char *vstr, unsigned int vlen, unsigned int skip) {
int skipcnt = 0;
unsigned char vencoding = 0;
long long vll = 0;
//如果没达到zlend
while (p[0] != ZIP_END) {
unsigned int prevlensize, encoding, lensize, len;
unsigned char *q;
//解码出当前节点的prevlensize
ZIP_DECODE_PREVLENSIZE(p, prevlensize);
//解码出len和lensize
ZIP_DECODE_LENGTH(p + prevlensize, encoding, lensize, len);
//求出数据段的开始地址q
q = p + prevlensize + lensize;
if (skipcnt == 0) {
//比较当前节点的数据是否跟给出的vstr相同,如果相同则返回当前节点的开始地址
//如果是字符串数据
if (ZIP_IS_STR(encoding)) {
if (len == vlen && memcmp(q, vstr, vlen) == 0) {
return p;
}
} else {
//接下来是编码输入数据vstr,当vencoding为0的时候,说明其还没有被编码,下面的代码对它进行编码
if (vencoding == 0) {
if (!zipTryEncoding(vstr, vlen, &vll, &vencoding)) {
//如果不能被转化成整形数据,那么把vencoding设成255,这样的话下次就不会再对其进行编码了
vencoding = UCHAR_MAX;
}
/* Must be non-zero by now */
assert(vencoding);
}
//如果vencoding不是UCHAR_MAX,那么说明vstr已经被转换成整形数据
if (vencoding != UCHAR_MAX) {
long long ll = zipLoadInteger(q, encoding);
//ll等于vll的话,那就返回当前节点的开始指针
if (ll == vll) {
return p;
}
}
}
/* Reset skip count */
skipcnt = skip;
} else {
/* Skip entry */
skipcnt--;
}
//更新p,移动到下一个节点
p = q + len;
}
return NULL;
}
//返回ziplist的长度
unsigned int ziplistLen(unsigned char *zl) {
unsigned int len = 0;
//如果zllen字段的长度小于UINT16_MAX(2**16 - 1),那么直接返回
if (intrev16ifbe(ZIPLIST_LENGTH(zl)) < UINT16_MAX) {
len = intrev16ifbe(ZIPLIST_LENGTH(zl));
} else {
//否则,需要遍历整个ziplist,然后返回其长度,整个过程很低效
unsigned char *p = zl+ZIPLIST_HEADER_SIZE;
while (*p != ZIP_END) {
p += zipRawEntryLength(p);
len++;
}
//如果遍历之后发现长度小于UINT16_MAX的话,重新更改zllen字段,改成实际长度,避免下次再次遍历
if (len < UINT16_MAX) ZIPLIST_LENGTH(zl) = intrev16ifbe(len);
}
return len;
}
//返回ziplist的字节数
size_t ziplistBlobLen(unsigned char *zl) {
return intrev32ifbe(ZIPLIST_BYTES(zl));
}
//列出ziplist的基本信息,注释略
void ziplistRepr(unsigned char *zl) {
unsigned char *p;
int index = 0;
zlentry entry;
printf(
"{total bytes %d} "
"{length %u}\n"
"{tail offset %u}\n",
intrev32ifbe(ZIPLIST_BYTES(zl)),
intrev16ifbe(ZIPLIST_LENGTH(zl)),
intrev32ifbe(ZIPLIST_TAIL_OFFSET(zl)));
p = ZIPLIST_ENTRY_HEAD(zl);
while(*p != ZIP_END) {
zipEntry(p, &entry);
printf(
"{"
"addr 0x%08lx, "
"index %2d, "
"offset %5ld, "
"rl: %5u, "
"hs %2u, "
"pl: %5u, "
"pls: %2u, "
"payload %5u"
"} ",
(long unsigned)p,
index,
(unsigned long) (p-zl),
entry.headersize+entry.len,
entry.headersize,
entry.prevrawlen,
entry.prevrawlensize,
entry.len);
p += entry.headersize;
if (ZIP_IS_STR(entry.encoding)) {
if (entry.len > 40) {
if (fwrite(p,40,1,stdout) == 0) perror("fwrite");
printf("...");
} else {
if (entry.len &&
fwrite(p,entry.len,1,stdout) == 0) perror("fwrite");
}
} else {
printf("%lld", (long long) zipLoadInteger(p,entry.encoding));
}
printf("\n");
p += entry.len;
index++;
}
printf("{end}\n\n");
}

总结

时间问题只写了几个核心函数的注释和一些小函数的注释,不过读懂了那几个函数其它函数其实也就大同小异,先出门喝杯咖啡散散步,什么时候有空补上没有写完的注释。

## 2016/11/22 已经更新完所有注释 ##

能力有限,可能会有错误,欢迎指出,大家一起交流学习。