]> git.saurik.com Git - redis.git/blob - src/ziplist.c
Import linenoise as dependency for redis-cli
[redis.git] / src / ziplist.c
1 /* The ziplist is a specially encoded dually linked list that is designed
2 * to be very memory efficient. It stores both strings and integer values,
3 * where integers are encoded as actual integers instead of a series of
4 * characters. It allows push and pop operations on either side of the list
5 * in O(1) time. However, because every operation requires a reallocation of
6 * the memory used by the ziplist, the actual complexity is related to the
7 * amount of memory used by the ziplist.
8 *
9 * ----------------------------------------------------------------------------
10 *
11 * ZIPLIST OVERALL LAYOUT:
12 * The general layout of the ziplist is as follows:
13 * <zlbytes><zltail><zllen><entry><entry><zlend>
14 *
15 * <zlbytes> is an unsigned integer to hold the number of bytes that the
16 * ziplist occupies. This value needs to be stored to be able to resize the
17 * entire structure without the need to traverse it first.
18 *
19 * <zltail> is the offset to the last entry in the list. This allows a pop
20 * operation on the far side of the list without the need for full traversal.
21 *
22 * <zllen> is the number of entries.When this value is larger than 2**16-2,
23 * we need to traverse the entire list to know how many items it holds.
24 *
25 * <zlend> is a single byte special value, equal to 255, which indicates the
26 * end of the list.
27 *
28 * ZIPLIST ENTRIES:
29 * Every entry in the ziplist is prefixed by a header that contains two pieces
30 * of information. First, the length of the previous entry is stored to be
31 * able to traverse the list from back to front. Second, the encoding with an
32 * optional string length of the entry itself is stored.
33 *
34 * The length of the previous entry is encoded in the following way:
35 * If this length is smaller than 254 bytes, it will only consume a single
36 * byte that takes the length as value. When the length is greater than or
37 * equal to 254, it will consume 5 bytes. The first byte is set to 254 to
38 * indicate a larger value is following. The remaining 4 bytes take the
39 * length of the previous entry as value.
40 *
41 * The other header field of the entry itself depends on the contents of the
42 * entry. When the entry is a string, the first 2 bits of this header will hold
43 * the type of encoding used to store the length of the string, followed by the
44 * actual length of the string. When the entry is an integer the first 2 bits
45 * are both set to 1. The following 2 bits are used to specify what kind of
46 * integer will be stored after this header. An overview of the different
47 * types and encodings is as follows:
48 *
49 * |00pppppp| - 1 byte
50 * String value with length less than or equal to 63 bytes (6 bits).
51 * |01pppppp|qqqqqqqq| - 2 bytes
52 * String value with length less than or equal to 16383 bytes (14 bits).
53 * |10______|qqqqqqqq|rrrrrrrr|ssssssss|tttttttt| - 5 bytes
54 * String value with length greater than or equal to 16384 bytes.
55 * |1100____| - 1 byte
56 * Integer encoded as int16_t (2 bytes).
57 * |1101____| - 1 byte
58 * Integer encoded as int32_t (4 bytes).
59 * |1110____| - 1 byte
60 * Integer encoded as int64_t (8 bytes).
61 */
62
63 #include <stdio.h>
64 #include <stdlib.h>
65 #include <string.h>
66 #include <stdint.h>
67 #include <assert.h>
68 #include <limits.h>
69 #include "zmalloc.h"
70 #include "ziplist.h"
71
72 int ll2string(char *s, size_t len, long long value);
73
74 #define ZIP_END 255
75 #define ZIP_BIGLEN 254
76
77 /* Different encoding/length possibilities */
78 #define ZIP_STR_06B (0 << 6)
79 #define ZIP_STR_14B (1 << 6)
80 #define ZIP_STR_32B (2 << 6)
81 #define ZIP_INT_16B (0xc0 | 0<<4)
82 #define ZIP_INT_32B (0xc0 | 1<<4)
83 #define ZIP_INT_64B (0xc0 | 2<<4)
84
85 /* Macro's to determine type */
86 #define ZIP_IS_STR(enc) (((enc) & 0xc0) < 0xc0)
87 #define ZIP_IS_INT(enc) (!ZIP_IS_STR(enc) && ((enc) & 0x30) < 0x30)
88
89 /* Utility macros */
90 #define ZIPLIST_BYTES(zl) (*((uint32_t*)(zl)))
91 #define ZIPLIST_TAIL_OFFSET(zl) (*((uint32_t*)((zl)+sizeof(uint32_t))))
92 #define ZIPLIST_LENGTH(zl) (*((uint16_t*)((zl)+sizeof(uint32_t)*2)))
93 #define ZIPLIST_HEADER_SIZE (sizeof(uint32_t)*2+sizeof(uint16_t))
94 #define ZIPLIST_ENTRY_HEAD(zl) ((zl)+ZIPLIST_HEADER_SIZE)
95 #define ZIPLIST_ENTRY_TAIL(zl) ((zl)+ZIPLIST_TAIL_OFFSET(zl))
96 #define ZIPLIST_ENTRY_END(zl) ((zl)+ZIPLIST_BYTES(zl)-1)
97
98 /* We know a positive increment can only be 1 because entries can only be
99 * pushed one at a time. */
100 #define ZIPLIST_INCR_LENGTH(zl,incr) { \
101 if (ZIPLIST_LENGTH(zl) < UINT16_MAX) ZIPLIST_LENGTH(zl)+=incr; }
102
103 typedef struct zlentry {
104 unsigned int prevrawlensize, prevrawlen;
105 unsigned int lensize, len;
106 unsigned int headersize;
107 unsigned char encoding;
108 unsigned char *p;
109 } zlentry;
110
111 /* Return the encoding pointer to by 'p'. */
112 static unsigned int zipEntryEncoding(unsigned char *p) {
113 /* String encoding: 2 MSBs */
114 unsigned char b = p[0] & 0xc0;
115 if (b < 0xc0) {
116 return b;
117 } else {
118 /* Integer encoding: 4 MSBs */
119 return p[0] & 0xf0;
120 }
121 assert(NULL);
122 }
123
124 /* Return bytes needed to store integer encoded by 'encoding' */
125 static unsigned int zipIntSize(unsigned char encoding) {
126 switch(encoding) {
127 case ZIP_INT_16B: return sizeof(int16_t);
128 case ZIP_INT_32B: return sizeof(int32_t);
129 case ZIP_INT_64B: return sizeof(int64_t);
130 }
131 assert(NULL);
132 }
133
134 /* Decode the encoded length pointed by 'p'. If a pointer to 'lensize' is
135 * provided, it is set to the number of bytes required to encode the length. */
136 static unsigned int zipDecodeLength(unsigned char *p, unsigned int *lensize) {
137 unsigned char encoding = zipEntryEncoding(p);
138 unsigned int len;
139
140 if (ZIP_IS_STR(encoding)) {
141 switch(encoding) {
142 case ZIP_STR_06B:
143 len = p[0] & 0x3f;
144 if (lensize) *lensize = 1;
145 break;
146 case ZIP_STR_14B:
147 len = ((p[0] & 0x3f) << 8) | p[1];
148 if (lensize) *lensize = 2;
149 break;
150 case ZIP_STR_32B:
151 len = (p[1] << 24) | (p[2] << 16) | (p[3] << 8) | p[4];
152 if (lensize) *lensize = 5;
153 break;
154 default:
155 assert(NULL);
156 }
157 } else {
158 len = zipIntSize(encoding);
159 if (lensize) *lensize = 1;
160 }
161 return len;
162 }
163
164 /* Encode the length 'l' writing it in 'p'. If p is NULL it just returns
165 * the amount of bytes required to encode such a length. */
166 static unsigned int zipEncodeLength(unsigned char *p, unsigned char encoding, unsigned int rawlen) {
167 unsigned char len = 1, buf[5];
168
169 if (ZIP_IS_STR(encoding)) {
170 /* Although encoding is given it may not be set for strings,
171 * so we determine it here using the raw length. */
172 if (rawlen <= 0x3f) {
173 if (!p) return len;
174 buf[0] = ZIP_STR_06B | rawlen;
175 } else if (rawlen <= 0x3fff) {
176 len += 1;
177 if (!p) return len;
178 buf[0] = ZIP_STR_14B | ((rawlen >> 8) & 0x3f);
179 buf[1] = rawlen & 0xff;
180 } else {
181 len += 4;
182 if (!p) return len;
183 buf[0] = ZIP_STR_32B;
184 buf[1] = (rawlen >> 24) & 0xff;
185 buf[2] = (rawlen >> 16) & 0xff;
186 buf[3] = (rawlen >> 8) & 0xff;
187 buf[4] = rawlen & 0xff;
188 }
189 } else {
190 /* Implies integer encoding, so length is always 1. */
191 if (!p) return len;
192 buf[0] = encoding;
193 }
194
195 /* Store this length at p */
196 memcpy(p,buf,len);
197 return len;
198 }
199
200 /* Decode the length of the previous element stored at "p". */
201 static unsigned int zipPrevDecodeLength(unsigned char *p, unsigned int *lensize) {
202 unsigned int len = *p;
203 if (len < ZIP_BIGLEN) {
204 if (lensize) *lensize = 1;
205 } else {
206 if (lensize) *lensize = 1+sizeof(len);
207 memcpy(&len,p+1,sizeof(len));
208 }
209 return len;
210 }
211
212 /* Encode the length of the previous entry and write it to "p". Return the
213 * number of bytes needed to encode this length if "p" is NULL. */
214 static unsigned int zipPrevEncodeLength(unsigned char *p, unsigned int len) {
215 if (p == NULL) {
216 return (len < ZIP_BIGLEN) ? 1 : sizeof(len)+1;
217 } else {
218 if (len < ZIP_BIGLEN) {
219 p[0] = len;
220 return 1;
221 } else {
222 p[0] = ZIP_BIGLEN;
223 memcpy(p+1,&len,sizeof(len));
224 return 1+sizeof(len);
225 }
226 }
227 }
228
229 /* Encode the length of the previous entry and write it to "p". This only
230 * uses the larger encoding (required in __ziplistCascadeUpdate). */
231 static void zipPrevEncodeLengthForceLarge(unsigned char *p, unsigned int len) {
232 if (p == NULL) return;
233 p[0] = ZIP_BIGLEN;
234 memcpy(p+1,&len,sizeof(len));
235 }
236
237 /* Return the difference in number of bytes needed to store the new length
238 * "len" on the entry pointed to by "p". */
239 static int zipPrevLenByteDiff(unsigned char *p, unsigned int len) {
240 unsigned int prevlensize;
241 zipPrevDecodeLength(p,&prevlensize);
242 return zipPrevEncodeLength(NULL,len)-prevlensize;
243 }
244
245 /* Check if string pointed to by 'entry' can be encoded as an integer.
246 * Stores the integer value in 'v' and its encoding in 'encoding'. */
247 static int zipTryEncoding(unsigned char *entry, unsigned int entrylen, long long *v, unsigned char *encoding) {
248 long long value;
249 char *eptr;
250 char buf[32];
251
252 if (entrylen >= 32 || entrylen == 0) return 0;
253 if (entry[0] == '-' || (entry[0] >= '0' && entry[0] <= '9')) {
254 int slen;
255
256 /* Perform a back-and-forth conversion to make sure that
257 * the string turned into an integer is not losing any info. */
258 memcpy(buf,entry,entrylen);
259 buf[entrylen] = '\0';
260 value = strtoll(buf,&eptr,10);
261 if (eptr[0] != '\0') return 0;
262 slen = ll2string(buf,32,value);
263 if (entrylen != (unsigned)slen || memcmp(buf,entry,slen)) return 0;
264
265 /* Great, the string can be encoded. Check what's the smallest
266 * of our encoding types that can hold this value. */
267 if (value >= INT16_MIN && value <= INT16_MAX) {
268 *encoding = ZIP_INT_16B;
269 } else if (value >= INT32_MIN && value <= INT32_MAX) {
270 *encoding = ZIP_INT_32B;
271 } else {
272 *encoding = ZIP_INT_64B;
273 }
274 *v = value;
275 return 1;
276 }
277 return 0;
278 }
279
280 /* Store integer 'value' at 'p', encoded as 'encoding' */
281 static void zipSaveInteger(unsigned char *p, int64_t value, unsigned char encoding) {
282 int16_t i16;
283 int32_t i32;
284 int64_t i64;
285 if (encoding == ZIP_INT_16B) {
286 i16 = value;
287 memcpy(p,&i16,sizeof(i16));
288 } else if (encoding == ZIP_INT_32B) {
289 i32 = value;
290 memcpy(p,&i32,sizeof(i32));
291 } else if (encoding == ZIP_INT_64B) {
292 i64 = value;
293 memcpy(p,&i64,sizeof(i64));
294 } else {
295 assert(NULL);
296 }
297 }
298
299 /* Read integer encoded as 'encoding' from 'p' */
300 static int64_t zipLoadInteger(unsigned char *p, unsigned char encoding) {
301 int16_t i16;
302 int32_t i32;
303 int64_t i64, ret;
304 if (encoding == ZIP_INT_16B) {
305 memcpy(&i16,p,sizeof(i16));
306 ret = i16;
307 } else if (encoding == ZIP_INT_32B) {
308 memcpy(&i32,p,sizeof(i32));
309 ret = i32;
310 } else if (encoding == ZIP_INT_64B) {
311 memcpy(&i64,p,sizeof(i64));
312 ret = i64;
313 } else {
314 assert(NULL);
315 }
316 return ret;
317 }
318
319 /* Return a struct with all information about an entry. */
320 static zlentry zipEntry(unsigned char *p) {
321 zlentry e;
322 e.prevrawlen = zipPrevDecodeLength(p,&e.prevrawlensize);
323 e.len = zipDecodeLength(p+e.prevrawlensize,&e.lensize);
324 e.headersize = e.prevrawlensize+e.lensize;
325 e.encoding = zipEntryEncoding(p+e.prevrawlensize);
326 e.p = p;
327 return e;
328 }
329
330 /* Return the total number of bytes used by the entry at "p". */
331 static unsigned int zipRawEntryLength(unsigned char *p) {
332 zlentry e = zipEntry(p);
333 return e.headersize + e.len;
334 }
335
336 /* Create a new empty ziplist. */
337 unsigned char *ziplistNew(void) {
338 unsigned int bytes = ZIPLIST_HEADER_SIZE+1;
339 unsigned char *zl = zmalloc(bytes);
340 ZIPLIST_BYTES(zl) = bytes;
341 ZIPLIST_TAIL_OFFSET(zl) = ZIPLIST_HEADER_SIZE;
342 ZIPLIST_LENGTH(zl) = 0;
343 zl[bytes-1] = ZIP_END;
344 return zl;
345 }
346
347 /* Resize the ziplist. */
348 static unsigned char *ziplistResize(unsigned char *zl, unsigned int len) {
349 zl = zrealloc(zl,len);
350 ZIPLIST_BYTES(zl) = len;
351 zl[len-1] = ZIP_END;
352 return zl;
353 }
354
355 /* When an entry is inserted, we need to set the prevlen field of the next
356 * entry to equal the length of the inserted entry. It can occur that this
357 * length cannot be encoded in 1 byte and the next entry needs to be grow
358 * a bit larger to hold the 5-byte encoded prevlen. This can be done for free,
359 * because this only happens when an entry is already being inserted (which
360 * causes a realloc and memmove). However, encoding the prevlen may require
361 * that this entry is grown as well. This effect may cascade throughout
362 * the ziplist when there are consecutive entries with a size close to
363 * ZIP_BIGLEN, so we need to check that the prevlen can be encoded in every
364 * consecutive entry.
365 *
366 * Note that this effect can also happen in reverse, where the bytes required
367 * to encode the prevlen field can shrink. This effect is deliberately ignored,
368 * because it can cause a "flapping" effect where a chain prevlen fields is
369 * first grown and then shrunk again after consecutive inserts. Rather, the
370 * field is allowed to stay larger than necessary, because a large prevlen
371 * field implies the ziplist is holding large entries anyway.
372 *
373 * The pointer "p" points to the first entry that does NOT need to be
374 * updated, i.e. consecutive fields MAY need an update. */
375 static unsigned char *__ziplistCascadeUpdate(unsigned char *zl, unsigned char *p) {
376 unsigned int curlen = ZIPLIST_BYTES(zl), rawlen, rawlensize;
377 unsigned int offset, noffset, extra;
378 unsigned char *np;
379 zlentry cur, next;
380
381 while (p[0] != ZIP_END) {
382 cur = zipEntry(p);
383 rawlen = cur.headersize + cur.len;
384 rawlensize = zipPrevEncodeLength(NULL,rawlen);
385
386 /* Abort if there is no next entry. */
387 if (p[rawlen] == ZIP_END) break;
388 next = zipEntry(p+rawlen);
389
390 /* Abort when "prevlen" has not changed. */
391 if (next.prevrawlen == rawlen) break;
392
393 if (next.prevrawlensize < rawlensize) {
394 /* The "prevlen" field of "next" needs more bytes to hold
395 * the raw length of "cur". */
396 offset = p-zl;
397 extra = rawlensize-next.prevrawlensize;
398 zl = ziplistResize(zl,curlen+extra);
399 ZIPLIST_TAIL_OFFSET(zl) += extra;
400 p = zl+offset;
401
402 /* Move the tail to the back. */
403 np = p+rawlen;
404 noffset = np-zl;
405 memmove(np+rawlensize,
406 np+next.prevrawlensize,
407 curlen-noffset-next.prevrawlensize-1);
408 zipPrevEncodeLength(np,rawlen);
409
410 /* Advance the cursor */
411 p += rawlen;
412 curlen += extra;
413 } else {
414 if (next.prevrawlensize > rawlensize) {
415 /* This would result in shrinking, which we want to avoid.
416 * So, set "rawlen" in the available bytes. */
417 zipPrevEncodeLengthForceLarge(p+rawlen,rawlen);
418 } else {
419 zipPrevEncodeLength(p+rawlen,rawlen);
420 }
421
422 /* Stop here, as the raw length of "next" has not changed. */
423 break;
424 }
425 }
426 return zl;
427 }
428
429 /* Delete "num" entries, starting at "p". Returns pointer to the ziplist. */
430 static unsigned char *__ziplistDelete(unsigned char *zl, unsigned char *p, unsigned int num) {
431 unsigned int i, totlen, deleted = 0;
432 int offset, nextdiff = 0;
433 zlentry first, tail;
434
435 first = zipEntry(p);
436 for (i = 0; p[0] != ZIP_END && i < num; i++) {
437 p += zipRawEntryLength(p);
438 deleted++;
439 }
440
441 totlen = p-first.p;
442 if (totlen > 0) {
443 if (p[0] != ZIP_END) {
444 /* Tricky: storing the prevlen in this entry might reduce or
445 * increase the number of bytes needed, compared to the current
446 * prevlen. Note that we can always store this length because
447 * it was previously stored by an entry that is being deleted. */
448 nextdiff = zipPrevLenByteDiff(p,first.prevrawlen);
449 zipPrevEncodeLength(p-nextdiff,first.prevrawlen);
450
451 /* Update offset for tail */
452 ZIPLIST_TAIL_OFFSET(zl) -= totlen;
453
454 /* When the tail contains more than one entry, we need to take
455 * "nextdiff" in account as well. Otherwise, a change in the
456 * size of prevlen doesn't have an effect on the *tail* offset. */
457 tail = zipEntry(p);
458 if (p[tail.headersize+tail.len] != ZIP_END)
459 ZIPLIST_TAIL_OFFSET(zl) += nextdiff;
460
461 /* Move tail to the front of the ziplist */
462 memmove(first.p,p-nextdiff,ZIPLIST_BYTES(zl)-(p-zl)-1+nextdiff);
463 } else {
464 /* The entire tail was deleted. No need to move memory. */
465 ZIPLIST_TAIL_OFFSET(zl) = (first.p-zl)-first.prevrawlen;
466 }
467
468 /* Resize and update length */
469 offset = first.p-zl;
470 zl = ziplistResize(zl, ZIPLIST_BYTES(zl)-totlen+nextdiff);
471 ZIPLIST_INCR_LENGTH(zl,-deleted);
472 p = zl+offset;
473
474 /* When nextdiff != 0, the raw length of the next entry has changed, so
475 * we need to cascade the update throughout the ziplist */
476 if (nextdiff != 0)
477 zl = __ziplistCascadeUpdate(zl,p);
478 }
479 return zl;
480 }
481
482 /* Insert item at "p". */
483 static unsigned char *__ziplistInsert(unsigned char *zl, unsigned char *p, unsigned char *s, unsigned int slen) {
484 unsigned int curlen = ZIPLIST_BYTES(zl), reqlen, prevlen = 0;
485 unsigned int offset, nextdiff = 0;
486 unsigned char encoding = 0;
487 long long value;
488 zlentry entry, tail;
489
490 /* Find out prevlen for the entry that is inserted. */
491 if (p[0] != ZIP_END) {
492 entry = zipEntry(p);
493 prevlen = entry.prevrawlen;
494 } else {
495 unsigned char *ptail = ZIPLIST_ENTRY_TAIL(zl);
496 if (ptail[0] != ZIP_END) {
497 prevlen = zipRawEntryLength(ptail);
498 }
499 }
500
501 /* See if the entry can be encoded */
502 if (zipTryEncoding(s,slen,&value,&encoding)) {
503 /* 'encoding' is set to the appropriate integer encoding */
504 reqlen = zipIntSize(encoding);
505 } else {
506 /* 'encoding' is untouched, however zipEncodeLength will use the
507 * string length to figure out how to encode it. */
508 reqlen = slen;
509 }
510 /* We need space for both the length of the previous entry and
511 * the length of the payload. */
512 reqlen += zipPrevEncodeLength(NULL,prevlen);
513 reqlen += zipEncodeLength(NULL,encoding,slen);
514
515 /* When the insert position is not equal to the tail, we need to
516 * make sure that the next entry can hold this entry's length in
517 * its prevlen field. */
518 nextdiff = (p[0] != ZIP_END) ? zipPrevLenByteDiff(p,reqlen) : 0;
519
520 /* Store offset because a realloc may change the address of zl. */
521 offset = p-zl;
522 zl = ziplistResize(zl,curlen+reqlen+nextdiff);
523 p = zl+offset;
524
525 /* Apply memory move when necessary and update tail offset. */
526 if (p[0] != ZIP_END) {
527 /* Subtract one because of the ZIP_END bytes */
528 memmove(p+reqlen,p-nextdiff,curlen-offset-1+nextdiff);
529
530 /* Encode this entry's raw length in the next entry. */
531 zipPrevEncodeLength(p+reqlen,reqlen);
532
533 /* Update offset for tail */
534 ZIPLIST_TAIL_OFFSET(zl) += reqlen;
535
536 /* When the tail contains more than one entry, we need to take
537 * "nextdiff" in account as well. Otherwise, a change in the
538 * size of prevlen doesn't have an effect on the *tail* offset. */
539 tail = zipEntry(p+reqlen);
540 if (p[reqlen+tail.headersize+tail.len] != ZIP_END)
541 ZIPLIST_TAIL_OFFSET(zl) += nextdiff;
542 } else {
543 /* This element will be the new tail. */
544 ZIPLIST_TAIL_OFFSET(zl) = p-zl;
545 }
546
547 /* When nextdiff != 0, the raw length of the next entry has changed, so
548 * we need to cascade the update throughout the ziplist */
549 if (nextdiff != 0) {
550 offset = p-zl;
551 zl = __ziplistCascadeUpdate(zl,p+reqlen);
552 p = zl+offset;
553 }
554
555 /* Write the entry */
556 p += zipPrevEncodeLength(p,prevlen);
557 p += zipEncodeLength(p,encoding,slen);
558 if (ZIP_IS_STR(encoding)) {
559 memcpy(p,s,slen);
560 } else {
561 zipSaveInteger(p,value,encoding);
562 }
563 ZIPLIST_INCR_LENGTH(zl,1);
564 return zl;
565 }
566
567 unsigned char *ziplistPush(unsigned char *zl, unsigned char *s, unsigned int slen, int where) {
568 unsigned char *p;
569 p = (where == ZIPLIST_HEAD) ? ZIPLIST_ENTRY_HEAD(zl) : ZIPLIST_ENTRY_END(zl);
570 return __ziplistInsert(zl,p,s,slen);
571 }
572
573 /* Returns an offset to use for iterating with ziplistNext. When the given
574 * index is negative, the list is traversed back to front. When the list
575 * doesn't contain an element at the provided index, NULL is returned. */
576 unsigned char *ziplistIndex(unsigned char *zl, int index) {
577 unsigned char *p;
578 zlentry entry;
579 if (index < 0) {
580 index = (-index)-1;
581 p = ZIPLIST_ENTRY_TAIL(zl);
582 if (p[0] != ZIP_END) {
583 entry = zipEntry(p);
584 while (entry.prevrawlen > 0 && index--) {
585 p -= entry.prevrawlen;
586 entry = zipEntry(p);
587 }
588 }
589 } else {
590 p = ZIPLIST_ENTRY_HEAD(zl);
591 while (p[0] != ZIP_END && index--) {
592 p += zipRawEntryLength(p);
593 }
594 }
595 return (p[0] == ZIP_END || index > 0) ? NULL : p;
596 }
597
598 /* Return pointer to next entry in ziplist. */
599 unsigned char *ziplistNext(unsigned char *zl, unsigned char *p) {
600 ((void) zl);
601
602 /* "p" could be equal to ZIP_END, caused by ziplistDelete,
603 * and we should return NULL. Otherwise, we should return NULL
604 * when the *next* element is ZIP_END (there is no next entry). */
605 if (p[0] == ZIP_END) {
606 return NULL;
607 } else {
608 p = p+zipRawEntryLength(p);
609 return (p[0] == ZIP_END) ? NULL : p;
610 }
611 }
612
613 /* Return pointer to previous entry in ziplist. */
614 unsigned char *ziplistPrev(unsigned char *zl, unsigned char *p) {
615 zlentry entry;
616
617 /* Iterating backwards from ZIP_END should return the tail. When "p" is
618 * equal to the first element of the list, we're already at the head,
619 * and should return NULL. */
620 if (p[0] == ZIP_END) {
621 p = ZIPLIST_ENTRY_TAIL(zl);
622 return (p[0] == ZIP_END) ? NULL : p;
623 } else if (p == ZIPLIST_ENTRY_HEAD(zl)) {
624 return NULL;
625 } else {
626 entry = zipEntry(p);
627 assert(entry.prevrawlen > 0);
628 return p-entry.prevrawlen;
629 }
630 }
631
632 /* Get entry pointer to by 'p' and store in either 'e' or 'v' depending
633 * on the encoding of the entry. 'e' is always set to NULL to be able
634 * to find out whether the string pointer or the integer value was set.
635 * Return 0 if 'p' points to the end of the zipmap, 1 otherwise. */
636 unsigned int ziplistGet(unsigned char *p, unsigned char **sstr, unsigned int *slen, long long *sval) {
637 zlentry entry;
638 if (p == NULL || p[0] == ZIP_END) return 0;
639 if (sstr) *sstr = NULL;
640
641 entry = zipEntry(p);
642 if (ZIP_IS_STR(entry.encoding)) {
643 if (sstr) {
644 *slen = entry.len;
645 *sstr = p+entry.headersize;
646 }
647 } else {
648 if (sval) {
649 *sval = zipLoadInteger(p+entry.headersize,entry.encoding);
650 }
651 }
652 return 1;
653 }
654
655 /* Insert an entry at "p". */
656 unsigned char *ziplistInsert(unsigned char *zl, unsigned char *p, unsigned char *s, unsigned int slen) {
657 return __ziplistInsert(zl,p,s,slen);
658 }
659
660 /* Delete a single entry from the ziplist, pointed to by *p.
661 * Also update *p in place, to be able to iterate over the
662 * ziplist, while deleting entries. */
663 unsigned char *ziplistDelete(unsigned char *zl, unsigned char **p) {
664 unsigned int offset = *p-zl;
665 zl = __ziplistDelete(zl,*p,1);
666
667 /* Store pointer to current element in p, because ziplistDelete will
668 * do a realloc which might result in a different "zl"-pointer.
669 * When the delete direction is back to front, we might delete the last
670 * entry and end up with "p" pointing to ZIP_END, so check this. */
671 *p = zl+offset;
672 return zl;
673 }
674
675 /* Delete a range of entries from the ziplist. */
676 unsigned char *ziplistDeleteRange(unsigned char *zl, unsigned int index, unsigned int num) {
677 unsigned char *p = ziplistIndex(zl,index);
678 return (p == NULL) ? zl : __ziplistDelete(zl,p,num);
679 }
680
681 /* Compare entry pointer to by 'p' with 'entry'. Return 1 if equal. */
682 unsigned int ziplistCompare(unsigned char *p, unsigned char *sstr, unsigned int slen) {
683 zlentry entry;
684 unsigned char sencoding;
685 long long zval, sval;
686 if (p[0] == ZIP_END) return 0;
687
688 entry = zipEntry(p);
689 if (ZIP_IS_STR(entry.encoding)) {
690 /* Raw compare */
691 if (entry.len == slen) {
692 return memcmp(p+entry.headersize,sstr,slen) == 0;
693 } else {
694 return 0;
695 }
696 } else {
697 /* Try to compare encoded values */
698 if (zipTryEncoding(sstr,slen,&sval,&sencoding)) {
699 if (entry.encoding == sencoding) {
700 zval = zipLoadInteger(p+entry.headersize,entry.encoding);
701 return zval == sval;
702 }
703 }
704 }
705 return 0;
706 }
707
708 /* Return length of ziplist. */
709 unsigned int ziplistLen(unsigned char *zl) {
710 unsigned int len = 0;
711 if (ZIPLIST_LENGTH(zl) < UINT16_MAX) {
712 len = ZIPLIST_LENGTH(zl);
713 } else {
714 unsigned char *p = zl+ZIPLIST_HEADER_SIZE;
715 while (*p != ZIP_END) {
716 p += zipRawEntryLength(p);
717 len++;
718 }
719
720 /* Re-store length if small enough */
721 if (len < UINT16_MAX) ZIPLIST_LENGTH(zl) = len;
722 }
723 return len;
724 }
725
726 /* Return size in bytes of ziplist. */
727 unsigned int ziplistSize(unsigned char *zl) {
728 return ZIPLIST_BYTES(zl);
729 }
730
731 void ziplistRepr(unsigned char *zl) {
732 unsigned char *p;
733 int index = 0;
734 zlentry entry;
735
736 printf(
737 "{total bytes %d} "
738 "{length %u}\n"
739 "{tail offset %u}\n",
740 ZIPLIST_BYTES(zl),
741 ZIPLIST_LENGTH(zl),
742 ZIPLIST_TAIL_OFFSET(zl));
743 p = ZIPLIST_ENTRY_HEAD(zl);
744 while(*p != ZIP_END) {
745 entry = zipEntry(p);
746 printf(
747 "{"
748 "addr 0x%08lx, "
749 "index %2d, "
750 "offset %5ld, "
751 "rl: %5u, "
752 "hs %2u, "
753 "pl: %5u, "
754 "pls: %2u, "
755 "payload %5u"
756 "} ",
757 (long unsigned)p,
758 index,
759 (unsigned long) (p-zl),
760 entry.headersize+entry.len,
761 entry.headersize,
762 entry.prevrawlen,
763 entry.prevrawlensize,
764 entry.len);
765 p += entry.headersize;
766 if (ZIP_IS_STR(entry.encoding)) {
767 if (entry.len > 40) {
768 if (fwrite(p,40,1,stdout) == 0) perror("fwrite");
769 printf("...");
770 } else {
771 if (entry.len &&
772 fwrite(p,entry.len,1,stdout) == 0) perror("fwrite");
773 }
774 } else {
775 printf("%lld", (long long) zipLoadInteger(p,entry.encoding));
776 }
777 printf("\n");
778 p += entry.len;
779 index++;
780 }
781 printf("{end}\n\n");
782 }
783
784 #ifdef ZIPLIST_TEST_MAIN
785 #include <sys/time.h>
786 #include "adlist.h"
787 #include "sds.h"
788
789 #define debug(f, ...) { if (DEBUG) printf(f, __VA_ARGS__); }
790
791 unsigned char *createList() {
792 unsigned char *zl = ziplistNew();
793 zl = ziplistPush(zl, (unsigned char*)"foo", 3, ZIPLIST_TAIL);
794 zl = ziplistPush(zl, (unsigned char*)"quux", 4, ZIPLIST_TAIL);
795 zl = ziplistPush(zl, (unsigned char*)"hello", 5, ZIPLIST_HEAD);
796 zl = ziplistPush(zl, (unsigned char*)"1024", 4, ZIPLIST_TAIL);
797 return zl;
798 }
799
800 unsigned char *createIntList() {
801 unsigned char *zl = ziplistNew();
802 char buf[32];
803
804 sprintf(buf, "100");
805 zl = ziplistPush(zl, (unsigned char*)buf, strlen(buf), ZIPLIST_TAIL);
806 sprintf(buf, "128000");
807 zl = ziplistPush(zl, (unsigned char*)buf, strlen(buf), ZIPLIST_TAIL);
808 sprintf(buf, "-100");
809 zl = ziplistPush(zl, (unsigned char*)buf, strlen(buf), ZIPLIST_HEAD);
810 sprintf(buf, "4294967296");
811 zl = ziplistPush(zl, (unsigned char*)buf, strlen(buf), ZIPLIST_HEAD);
812 sprintf(buf, "non integer");
813 zl = ziplistPush(zl, (unsigned char*)buf, strlen(buf), ZIPLIST_TAIL);
814 sprintf(buf, "much much longer non integer");
815 zl = ziplistPush(zl, (unsigned char*)buf, strlen(buf), ZIPLIST_TAIL);
816 return zl;
817 }
818
819 long long usec(void) {
820 struct timeval tv;
821 gettimeofday(&tv,NULL);
822 return (((long long)tv.tv_sec)*1000000)+tv.tv_usec;
823 }
824
825 void stress(int pos, int num, int maxsize, int dnum) {
826 int i,j,k;
827 unsigned char *zl;
828 char posstr[2][5] = { "HEAD", "TAIL" };
829 long long start;
830 for (i = 0; i < maxsize; i+=dnum) {
831 zl = ziplistNew();
832 for (j = 0; j < i; j++) {
833 zl = ziplistPush(zl,(unsigned char*)"quux",4,ZIPLIST_TAIL);
834 }
835
836 /* Do num times a push+pop from pos */
837 start = usec();
838 for (k = 0; k < num; k++) {
839 zl = ziplistPush(zl,(unsigned char*)"quux",4,pos);
840 zl = ziplistDeleteRange(zl,0,1);
841 }
842 printf("List size: %8d, bytes: %8d, %dx push+pop (%s): %6lld usec\n",
843 i,ZIPLIST_BYTES(zl),num,posstr[pos],usec()-start);
844 zfree(zl);
845 }
846 }
847
848 void pop(unsigned char *zl, int where) {
849 unsigned char *p, *vstr;
850 unsigned int vlen;
851 long long vlong;
852
853 p = ziplistIndex(zl,where == ZIPLIST_HEAD ? 0 : -1);
854 if (ziplistGet(p,&vstr,&vlen,&vlong)) {
855 if (where == ZIPLIST_HEAD)
856 printf("Pop head: ");
857 else
858 printf("Pop tail: ");
859
860 if (vstr)
861 if (vlen && fwrite(vstr,vlen,1,stdout) == 0) perror("fwrite");
862 else
863 printf("%lld", vlong);
864
865 printf("\n");
866 ziplistDeleteRange(zl,-1,1);
867 } else {
868 printf("ERROR: Could not pop\n");
869 exit(1);
870 }
871 }
872
873 void randstring(char *target, unsigned int min, unsigned int max) {
874 int p, len = min+rand()%(max-min+1);
875 int minval, maxval;
876 switch(rand() % 3) {
877 case 0:
878 minval = 0;
879 maxval = 255;
880 break;
881 case 1:
882 minval = 48;
883 maxval = 122;
884 break;
885 case 2:
886 minval = 48;
887 maxval = 52;
888 break;
889 default:
890 assert(NULL);
891 }
892
893 while(p < len)
894 target[p++] = minval+rand()%(maxval-minval+1);
895 return;
896 }
897
898
899 int main(int argc, char **argv) {
900 unsigned char *zl, *p;
901 unsigned char *entry;
902 unsigned int elen;
903 long long value;
904
905 /* If an argument is given, use it as the random seed. */
906 if (argc == 2)
907 srand(atoi(argv[1]));
908
909 zl = createIntList();
910 ziplistRepr(zl);
911
912 zl = createList();
913 ziplistRepr(zl);
914
915 pop(zl,ZIPLIST_TAIL);
916 ziplistRepr(zl);
917
918 pop(zl,ZIPLIST_HEAD);
919 ziplistRepr(zl);
920
921 pop(zl,ZIPLIST_TAIL);
922 ziplistRepr(zl);
923
924 pop(zl,ZIPLIST_TAIL);
925 ziplistRepr(zl);
926
927 printf("Get element at index 3:\n");
928 {
929 zl = createList();
930 p = ziplistIndex(zl, 3);
931 if (!ziplistGet(p, &entry, &elen, &value)) {
932 printf("ERROR: Could not access index 3\n");
933 return 1;
934 }
935 if (entry) {
936 if (elen && fwrite(entry,elen,1,stdout) == 0) perror("fwrite");
937 printf("\n");
938 } else {
939 printf("%lld\n", value);
940 }
941 printf("\n");
942 }
943
944 printf("Get element at index 4 (out of range):\n");
945 {
946 zl = createList();
947 p = ziplistIndex(zl, 4);
948 if (p == NULL) {
949 printf("No entry\n");
950 } else {
951 printf("ERROR: Out of range index should return NULL, returned offset: %ld\n", p-zl);
952 return 1;
953 }
954 printf("\n");
955 }
956
957 printf("Get element at index -1 (last element):\n");
958 {
959 zl = createList();
960 p = ziplistIndex(zl, -1);
961 if (!ziplistGet(p, &entry, &elen, &value)) {
962 printf("ERROR: Could not access index -1\n");
963 return 1;
964 }
965 if (entry) {
966 if (elen && fwrite(entry,elen,1,stdout) == 0) perror("fwrite");
967 printf("\n");
968 } else {
969 printf("%lld\n", value);
970 }
971 printf("\n");
972 }
973
974 printf("Get element at index -4 (first element):\n");
975 {
976 zl = createList();
977 p = ziplistIndex(zl, -4);
978 if (!ziplistGet(p, &entry, &elen, &value)) {
979 printf("ERROR: Could not access index -4\n");
980 return 1;
981 }
982 if (entry) {
983 if (elen && fwrite(entry,elen,1,stdout) == 0) perror("fwrite");
984 printf("\n");
985 } else {
986 printf("%lld\n", value);
987 }
988 printf("\n");
989 }
990
991 printf("Get element at index -5 (reverse out of range):\n");
992 {
993 zl = createList();
994 p = ziplistIndex(zl, -5);
995 if (p == NULL) {
996 printf("No entry\n");
997 } else {
998 printf("ERROR: Out of range index should return NULL, returned offset: %ld\n", p-zl);
999 return 1;
1000 }
1001 printf("\n");
1002 }
1003
1004 printf("Iterate list from 0 to end:\n");
1005 {
1006 zl = createList();
1007 p = ziplistIndex(zl, 0);
1008 while (ziplistGet(p, &entry, &elen, &value)) {
1009 printf("Entry: ");
1010 if (entry) {
1011 if (elen && fwrite(entry,elen,1,stdout) == 0) perror("fwrite");
1012 } else {
1013 printf("%lld", value);
1014 }
1015 p = ziplistNext(zl,p);
1016 printf("\n");
1017 }
1018 printf("\n");
1019 }
1020
1021 printf("Iterate list from 1 to end:\n");
1022 {
1023 zl = createList();
1024 p = ziplistIndex(zl, 1);
1025 while (ziplistGet(p, &entry, &elen, &value)) {
1026 printf("Entry: ");
1027 if (entry) {
1028 if (elen && fwrite(entry,elen,1,stdout) == 0) perror("fwrite");
1029 } else {
1030 printf("%lld", value);
1031 }
1032 p = ziplistNext(zl,p);
1033 printf("\n");
1034 }
1035 printf("\n");
1036 }
1037
1038 printf("Iterate list from 2 to end:\n");
1039 {
1040 zl = createList();
1041 p = ziplistIndex(zl, 2);
1042 while (ziplistGet(p, &entry, &elen, &value)) {
1043 printf("Entry: ");
1044 if (entry) {
1045 if (elen && fwrite(entry,elen,1,stdout) == 0) perror("fwrite");
1046 } else {
1047 printf("%lld", value);
1048 }
1049 p = ziplistNext(zl,p);
1050 printf("\n");
1051 }
1052 printf("\n");
1053 }
1054
1055 printf("Iterate starting out of range:\n");
1056 {
1057 zl = createList();
1058 p = ziplistIndex(zl, 4);
1059 if (!ziplistGet(p, &entry, &elen, &value)) {
1060 printf("No entry\n");
1061 } else {
1062 printf("ERROR\n");
1063 }
1064 printf("\n");
1065 }
1066
1067 printf("Iterate from back to front:\n");
1068 {
1069 zl = createList();
1070 p = ziplistIndex(zl, -1);
1071 while (ziplistGet(p, &entry, &elen, &value)) {
1072 printf("Entry: ");
1073 if (entry) {
1074 if (elen && fwrite(entry,elen,1,stdout) == 0) perror("fwrite");
1075 } else {
1076 printf("%lld", value);
1077 }
1078 p = ziplistPrev(zl,p);
1079 printf("\n");
1080 }
1081 printf("\n");
1082 }
1083
1084 printf("Iterate from back to front, deleting all items:\n");
1085 {
1086 zl = createList();
1087 p = ziplistIndex(zl, -1);
1088 while (ziplistGet(p, &entry, &elen, &value)) {
1089 printf("Entry: ");
1090 if (entry) {
1091 if (elen && fwrite(entry,elen,1,stdout) == 0) perror("fwrite");
1092 } else {
1093 printf("%lld", value);
1094 }
1095 zl = ziplistDelete(zl,&p);
1096 p = ziplistPrev(zl,p);
1097 printf("\n");
1098 }
1099 printf("\n");
1100 }
1101
1102 printf("Delete inclusive range 0,0:\n");
1103 {
1104 zl = createList();
1105 zl = ziplistDeleteRange(zl, 0, 1);
1106 ziplistRepr(zl);
1107 }
1108
1109 printf("Delete inclusive range 0,1:\n");
1110 {
1111 zl = createList();
1112 zl = ziplistDeleteRange(zl, 0, 2);
1113 ziplistRepr(zl);
1114 }
1115
1116 printf("Delete inclusive range 1,2:\n");
1117 {
1118 zl = createList();
1119 zl = ziplistDeleteRange(zl, 1, 2);
1120 ziplistRepr(zl);
1121 }
1122
1123 printf("Delete with start index out of range:\n");
1124 {
1125 zl = createList();
1126 zl = ziplistDeleteRange(zl, 5, 1);
1127 ziplistRepr(zl);
1128 }
1129
1130 printf("Delete with num overflow:\n");
1131 {
1132 zl = createList();
1133 zl = ziplistDeleteRange(zl, 1, 5);
1134 ziplistRepr(zl);
1135 }
1136
1137 printf("Delete foo while iterating:\n");
1138 {
1139 zl = createList();
1140 p = ziplistIndex(zl,0);
1141 while (ziplistGet(p,&entry,&elen,&value)) {
1142 if (entry && strncmp("foo",(char*)entry,elen) == 0) {
1143 printf("Delete foo\n");
1144 zl = ziplistDelete(zl,&p);
1145 } else {
1146 printf("Entry: ");
1147 if (entry) {
1148 if (elen && fwrite(entry,elen,1,stdout) == 0)
1149 perror("fwrite");
1150 } else {
1151 printf("%lld",value);
1152 }
1153 p = ziplistNext(zl,p);
1154 printf("\n");
1155 }
1156 }
1157 printf("\n");
1158 ziplistRepr(zl);
1159 }
1160
1161 printf("Regression test for >255 byte strings:\n");
1162 {
1163 char v1[257],v2[257];
1164 memset(v1,'x',256);
1165 memset(v2,'y',256);
1166 zl = ziplistNew();
1167 zl = ziplistPush(zl,(unsigned char*)v1,strlen(v1),ZIPLIST_TAIL);
1168 zl = ziplistPush(zl,(unsigned char*)v2,strlen(v2),ZIPLIST_TAIL);
1169
1170 /* Pop values again and compare their value. */
1171 p = ziplistIndex(zl,0);
1172 assert(ziplistGet(p,&entry,&elen,&value));
1173 assert(strncmp(v1,(char*)entry,elen) == 0);
1174 p = ziplistIndex(zl,1);
1175 assert(ziplistGet(p,&entry,&elen,&value));
1176 assert(strncmp(v2,(char*)entry,elen) == 0);
1177 printf("SUCCESS\n\n");
1178 }
1179
1180 printf("Create long list and check indices:\n");
1181 {
1182 zl = ziplistNew();
1183 char buf[32];
1184 int i,len;
1185 for (i = 0; i < 1000; i++) {
1186 len = sprintf(buf,"%d",i);
1187 zl = ziplistPush(zl,(unsigned char*)buf,len,ZIPLIST_TAIL);
1188 }
1189 for (i = 0; i < 1000; i++) {
1190 p = ziplistIndex(zl,i);
1191 assert(ziplistGet(p,NULL,NULL,&value));
1192 assert(i == value);
1193
1194 p = ziplistIndex(zl,-i-1);
1195 assert(ziplistGet(p,NULL,NULL,&value));
1196 assert(999-i == value);
1197 }
1198 printf("SUCCESS\n\n");
1199 }
1200
1201 printf("Compare strings with ziplist entries:\n");
1202 {
1203 zl = createList();
1204 p = ziplistIndex(zl,0);
1205 if (!ziplistCompare(p,(unsigned char*)"hello",5)) {
1206 printf("ERROR: not \"hello\"\n");
1207 return 1;
1208 }
1209 if (ziplistCompare(p,(unsigned char*)"hella",5)) {
1210 printf("ERROR: \"hella\"\n");
1211 return 1;
1212 }
1213
1214 p = ziplistIndex(zl,3);
1215 if (!ziplistCompare(p,(unsigned char*)"1024",4)) {
1216 printf("ERROR: not \"1024\"\n");
1217 return 1;
1218 }
1219 if (ziplistCompare(p,(unsigned char*)"1025",4)) {
1220 printf("ERROR: \"1025\"\n");
1221 return 1;
1222 }
1223 printf("SUCCESS\n\n");
1224 }
1225
1226 printf("Stress with random payloads of different encoding:\n");
1227 {
1228 int i,j,len,where;
1229 unsigned char *p;
1230 char buf[1024];
1231 list *ref;
1232 listNode *refnode;
1233
1234 /* Hold temp vars from ziplist */
1235 unsigned char *sstr;
1236 unsigned int slen;
1237 long long sval;
1238
1239 /* In the regression for the cascade bug, it was triggered
1240 * with a random seed of 2. */
1241 srand(2);
1242
1243 for (i = 0; i < 20000; i++) {
1244 zl = ziplistNew();
1245 ref = listCreate();
1246 listSetFreeMethod(ref,sdsfree);
1247 len = rand() % 256;
1248
1249 /* Create lists */
1250 for (j = 0; j < len; j++) {
1251 where = (rand() & 1) ? ZIPLIST_HEAD : ZIPLIST_TAIL;
1252 switch(rand() % 4) {
1253 case 0:
1254 sprintf(buf,"%lld",(0LL + rand()) >> 20);
1255 break;
1256 case 1:
1257 sprintf(buf,"%lld",(0LL + rand()));
1258 break;
1259 case 2:
1260 sprintf(buf,"%lld",(0LL + rand()) << 20);
1261 break;
1262 case 3:
1263 randstring(buf,0,256);
1264 break;
1265 default:
1266 assert(NULL);
1267 }
1268
1269 /* Add to ziplist */
1270 zl = ziplistPush(zl, (unsigned char*)buf, strlen(buf), where);
1271
1272 /* Add to reference list */
1273 if (where == ZIPLIST_HEAD) {
1274 listAddNodeHead(ref,sdsnew(buf));
1275 } else if (where == ZIPLIST_TAIL) {
1276 listAddNodeTail(ref,sdsnew(buf));
1277 } else {
1278 assert(NULL);
1279 }
1280 }
1281
1282 assert(listLength(ref) == ziplistLen(zl));
1283 for (j = 0; j < len; j++) {
1284 /* Naive way to get elements, but similar to the stresser
1285 * executed from the Tcl test suite. */
1286 p = ziplistIndex(zl,j);
1287 refnode = listIndex(ref,j);
1288
1289 assert(ziplistGet(p,&sstr,&slen,&sval));
1290 if (sstr == NULL) {
1291 sprintf(buf,"%lld",sval);
1292 } else {
1293 memcpy(buf,sstr,slen);
1294 buf[slen] = '\0';
1295 }
1296 assert(strcmp(buf,listNodeValue(refnode)) == 0);
1297 }
1298 zfree(zl);
1299 listRelease(ref);
1300 }
1301 printf("SUCCESS\n\n");
1302 }
1303
1304 printf("Stress with variable ziplist size:\n");
1305 {
1306 stress(ZIPLIST_HEAD,100000,16384,256);
1307 stress(ZIPLIST_TAIL,100000,16384,256);
1308 }
1309
1310 return 0;
1311 }
1312
1313 #endif