]>
git.saurik.com Git - redis.git/blob - ziplist.c
1 /* Memory layout of a ziplist, containing "foo", "bar", "quux":
2 * <zlbytes><zllen><len>"foo"<len>"bar"<len>"quux"
4 * <zlbytes> is an unsigned integer to hold the number of bytes that
5 * the ziplist occupies. This is stored to not have to traverse the ziplist
6 * to know the new length when pushing.
8 * <zllen> is the number of items in the ziplist. When this value is
9 * greater than 254, we need to traverse the entire list to know
10 * how many items it holds.
12 * <len> is the number of bytes occupied by a single entry. When this
13 * number is greater than 253, the length will occupy 5 bytes, where
14 * the extra bytes contain an unsigned integer to hold the length.
27 /* Important note: the ZIP_END value is used to depict the end of the
28 * ziplist structure. When a pointer contains an entry, the first couple
29 * of bytes contain the encoded length of the previous entry. This length
30 * is encoded as ZIP_ENC_RAW length, so the first two bits will contain 00
31 * and the byte will therefore never have a value of 255. */
33 #define ZIP_BIGLEN 254
37 #define ZIP_ENC_INT16 1
38 #define ZIP_ENC_INT32 2
39 #define ZIP_ENC_INT64 3
40 #define ZIP_ENCODING(p) ((p)[0] >> 6)
42 /* Length encoding for raw entries */
43 #define ZIP_LEN_INLINE 0
44 #define ZIP_LEN_UINT16 1
45 #define ZIP_LEN_UINT32 2
48 #define ZIPLIST_BYTES(zl) (*((uint32_t*)(zl)))
49 #define ZIPLIST_TAIL_OFFSET(zl) (*((uint32_t*)((zl)+sizeof(uint32_t))))
50 #define ZIPLIST_LENGTH(zl) (*((uint16_t*)((zl)+sizeof(uint32_t)*2)))
51 #define ZIPLIST_HEADER_SIZE (sizeof(uint32_t)*2+sizeof(uint16_t))
52 #define ZIPLIST_ENTRY_HEAD(zl) ((zl)+ZIPLIST_HEADER_SIZE)
53 #define ZIPLIST_ENTRY_TAIL(zl) ((zl)+ZIPLIST_TAIL_OFFSET(zl))
54 #define ZIPLIST_ENTRY_END(zl) ((zl)+ZIPLIST_BYTES(zl)-1)
56 /* We know a positive increment can only be 1 because entries can only be
57 * pushed one at a time. */
58 #define ZIPLIST_INCR_LENGTH(zl,incr) { \
59 if (ZIPLIST_LENGTH(zl) < UINT16_MAX) ZIPLIST_LENGTH(zl)+=incr; }
61 typedef struct zlentry
{
62 unsigned int prevrawlensize
, prevrawlen
;
63 unsigned int lensize
, len
;
64 unsigned int headersize
;
65 unsigned char encoding
;
69 /* Return bytes needed to store integer encoded by 'encoding' */
70 static unsigned int zipEncodingSize(unsigned char encoding
) {
71 if (encoding
== ZIP_ENC_INT16
) {
72 return sizeof(int16_t);
73 } else if (encoding
== ZIP_ENC_INT32
) {
74 return sizeof(int32_t);
75 } else if (encoding
== ZIP_ENC_INT64
) {
76 return sizeof(int64_t);
81 /* Decode the encoded length pointed by 'p'. If a pointer to 'lensize' is
82 * provided, it is set to the number of bytes required to encode the length. */
83 static unsigned int zipDecodeLength(unsigned char *p
, unsigned int *lensize
) {
84 unsigned char encoding
= ZIP_ENCODING(p
), lenenc
;
87 if (encoding
== ZIP_ENC_RAW
) {
88 lenenc
= (p
[0] >> 4) & 0x3;
89 if (lenenc
== ZIP_LEN_INLINE
) {
91 if (lensize
) *lensize
= 1;
92 } else if (lenenc
== ZIP_LEN_UINT16
) {
93 len
= p
[1] | (p
[2] << 8);
94 if (lensize
) *lensize
= 3;
96 len
= p
[1] | (p
[2] << 8) | (p
[3] << 16) | (p
[4] << 24);
97 if (lensize
) *lensize
= 5;
100 len
= zipEncodingSize(encoding
);
101 if (lensize
) *lensize
= 1;
106 /* Encode the length 'l' writing it in 'p'. If p is NULL it just returns
107 * the amount of bytes required to encode such a length. */
108 static unsigned int zipEncodeLength(unsigned char *p
, char encoding
, unsigned int rawlen
) {
109 unsigned char len
= 1, lenenc
, buf
[5];
110 if (encoding
== ZIP_ENC_RAW
) {
113 lenenc
= ZIP_LEN_INLINE
;
115 } else if (rawlen
<= 0xffff) {
118 lenenc
= ZIP_LEN_UINT16
;
119 buf
[1] = (rawlen
) & 0xff;
120 buf
[2] = (rawlen
>> 8) & 0xff;
124 lenenc
= ZIP_LEN_UINT32
;
125 buf
[1] = (rawlen
) & 0xff;
126 buf
[2] = (rawlen
>> 8) & 0xff;
127 buf
[3] = (rawlen
>> 16) & 0xff;
128 buf
[4] = (rawlen
>> 24) & 0xff;
130 buf
[0] = (lenenc
<< 4) | (buf
[0] & 0xf);
134 /* Apparently we need to store the length in 'p' */
135 buf
[0] = (encoding
<< 6) | (buf
[0] & 0x3f);
140 /* Decode the length of the previous element stored at "p". */
141 static unsigned int zipPrevDecodeLength(unsigned char *p
, unsigned int *lensize
) {
142 unsigned int len
= *p
;
143 if (len
< ZIP_BIGLEN
) {
144 if (lensize
) *lensize
= 1;
146 if (lensize
) *lensize
= 1+sizeof(len
);
147 memcpy(&len
,p
+1,sizeof(len
));
152 /* Encode the length of the previous entry and write it to "p". Return the
153 * number of bytes needed to encode this length if "p" is NULL. */
154 static unsigned int zipPrevEncodeLength(unsigned char *p
, unsigned int len
) {
156 return (len
< ZIP_BIGLEN
) ? 1 : sizeof(len
)+1;
158 if (len
< ZIP_BIGLEN
) {
163 memcpy(p
+1,&len
,sizeof(len
));
164 return 1+sizeof(len
);
169 /* Return the difference in number of bytes needed to store the new length
170 * "len" on the entry pointed to by "p". */
171 static int zipPrevLenByteDiff(unsigned char *p
, unsigned int len
) {
172 unsigned int prevlensize
;
173 zipPrevDecodeLength(p
,&prevlensize
);
174 return zipPrevEncodeLength(NULL
,len
)-prevlensize
;
177 /* Check if string pointed to by 'entry' can be encoded as an integer.
178 * Stores the integer value in 'v' and its encoding in 'encoding'.
179 * Warning: this function requires a NULL-terminated string! */
180 static int zipTryEncoding(unsigned char *entry
, long long *v
, unsigned char *encoding
) {
184 if (entry
[0] == '-' || (entry
[0] >= '0' && entry
[0] <= '9')) {
185 value
= strtoll((char*)entry
,&eptr
,10);
186 if (eptr
[0] != '\0') return 0;
187 if (value
>= INT16_MIN
&& value
<= INT16_MAX
) {
188 *encoding
= ZIP_ENC_INT16
;
189 } else if (value
>= INT32_MIN
&& value
<= INT32_MAX
) {
190 *encoding
= ZIP_ENC_INT32
;
192 *encoding
= ZIP_ENC_INT64
;
200 /* Store integer 'value' at 'p', encoded as 'encoding' */
201 static void zipSaveInteger(unsigned char *p
, int64_t value
, unsigned char encoding
) {
205 if (encoding
== ZIP_ENC_INT16
) {
207 memcpy(p
,&i16
,sizeof(i16
));
208 } else if (encoding
== ZIP_ENC_INT32
) {
210 memcpy(p
,&i32
,sizeof(i32
));
211 } else if (encoding
== ZIP_ENC_INT64
) {
213 memcpy(p
,&i64
,sizeof(i64
));
219 /* Read integer encoded as 'encoding' from 'p' */
220 static int64_t zipLoadInteger(unsigned char *p
, unsigned char encoding
) {
224 if (encoding
== ZIP_ENC_INT16
) {
225 memcpy(&i16
,p
,sizeof(i16
));
227 } else if (encoding
== ZIP_ENC_INT32
) {
228 memcpy(&i32
,p
,sizeof(i32
));
230 } else if (encoding
== ZIP_ENC_INT64
) {
231 memcpy(&i64
,p
,sizeof(i64
));
239 /* Return a struct with all information about an entry. */
240 static zlentry
zipEntry(unsigned char *p
) {
242 e
.prevrawlen
= zipPrevDecodeLength(p
,&e
.prevrawlensize
);
243 e
.len
= zipDecodeLength(p
+e
.prevrawlensize
,&e
.lensize
);
244 e
.headersize
= e
.prevrawlensize
+e
.lensize
;
245 e
.encoding
= ZIP_ENCODING(p
+e
.prevrawlensize
);
250 /* Return the total number of bytes used by the entry at "p". */
251 static unsigned int zipRawEntryLength(unsigned char *p
) {
252 zlentry e
= zipEntry(p
);
253 return e
.headersize
+ e
.len
;
256 /* Create a new empty ziplist. */
257 unsigned char *ziplistNew(void) {
258 unsigned int bytes
= ZIPLIST_HEADER_SIZE
+1;
259 unsigned char *zl
= zmalloc(bytes
);
260 ZIPLIST_BYTES(zl
) = bytes
;
261 ZIPLIST_TAIL_OFFSET(zl
) = ZIPLIST_HEADER_SIZE
;
262 ZIPLIST_LENGTH(zl
) = 0;
263 zl
[bytes
-1] = ZIP_END
;
267 /* Resize the ziplist. */
268 static unsigned char *ziplistResize(unsigned char *zl
, unsigned int len
) {
269 zl
= zrealloc(zl
,len
);
270 ZIPLIST_BYTES(zl
) = len
;
275 /* Delete "num" entries, starting at "p". Returns pointer to the ziplist. */
276 static unsigned char *__ziplistDelete(unsigned char *zl
, unsigned char *p
, unsigned int num
) {
277 unsigned int i
, totlen
, deleted
= 0;
279 zlentry first
= zipEntry(p
);
280 for (i
= 0; p
[0] != ZIP_END
&& i
< num
; i
++) {
281 p
+= zipRawEntryLength(p
);
287 if (p
[0] != ZIP_END
) {
288 /* Tricky: storing the prevlen in this entry might reduce or
289 * increase the number of bytes needed, compared to the current
290 * prevlen. Note that we can always store this length because
291 * it was previously stored by an entry that is being deleted. */
292 nextdiff
= zipPrevLenByteDiff(p
,first
.prevrawlen
);
293 zipPrevEncodeLength(p
-nextdiff
,first
.prevrawlen
);
295 /* Update offset for tail */
296 ZIPLIST_TAIL_OFFSET(zl
) -= totlen
+nextdiff
;
298 /* Move tail to the front of the ziplist */
299 memmove(first
.p
,p
-nextdiff
,ZIPLIST_BYTES(zl
)-(p
-zl
)-1+nextdiff
);
301 /* The entire tail was deleted. No need to move memory. */
302 ZIPLIST_TAIL_OFFSET(zl
) = (first
.p
-zl
)-first
.prevrawlen
;
305 /* Resize and update length */
306 zl
= ziplistResize(zl
, ZIPLIST_BYTES(zl
)-totlen
+nextdiff
);
307 ZIPLIST_INCR_LENGTH(zl
,-deleted
);
312 /* Insert item at "p". */
313 static unsigned char *__ziplistInsert(unsigned char *zl
, unsigned char *p
, unsigned char *s
, unsigned int slen
) {
314 unsigned int curlen
= ZIPLIST_BYTES(zl
), reqlen
, prevlen
= 0;
315 unsigned int offset
, nextdiff
= 0;
317 unsigned char encoding
= ZIP_ENC_RAW
;
321 /* Find out prevlen for the entry that is inserted. */
322 if (p
[0] != ZIP_END
) {
324 prevlen
= entry
.prevrawlen
;
326 tail
= ZIPLIST_ENTRY_TAIL(zl
);
327 if (tail
[0] != ZIP_END
) {
328 prevlen
= zipRawEntryLength(tail
);
332 /* See if the entry can be encoded */
333 if (zipTryEncoding(s
,&value
,&encoding
)) {
334 reqlen
= zipEncodingSize(encoding
);
339 /* We need space for both the length of the previous entry and
340 * the length of the payload. */
341 reqlen
+= zipPrevEncodeLength(NULL
,prevlen
);
342 reqlen
+= zipEncodeLength(NULL
,encoding
,slen
);
344 /* When the insert position is not equal to the tail, we need to
345 * make sure that the next entry can hold this entry's length in
346 * its prevlen field. */
347 nextdiff
= (p
[0] != ZIP_END
) ? zipPrevLenByteDiff(p
,reqlen
) : 0;
349 /* Store offset because a realloc may change the address of zl. */
351 zl
= ziplistResize(zl
,curlen
+reqlen
+nextdiff
);
354 /* Apply memory move when necessary and update tail offset. */
355 if (p
[0] != ZIP_END
) {
356 /* Subtract one because of the ZIP_END bytes */
357 memmove(p
+reqlen
,p
-nextdiff
,curlen
-offset
-1+nextdiff
);
358 /* Encode this entry's raw length in the next entry. */
359 zipPrevEncodeLength(p
+reqlen
,reqlen
);
360 /* Update offset for tail */
361 ZIPLIST_TAIL_OFFSET(zl
) += reqlen
+nextdiff
;
363 /* This element will be the new tail. */
364 ZIPLIST_TAIL_OFFSET(zl
) = p
-zl
;
367 /* Write the entry */
368 p
+= zipPrevEncodeLength(p
,prevlen
);
369 p
+= zipEncodeLength(p
,encoding
,slen
);
370 if (encoding
!= ZIP_ENC_RAW
) {
371 zipSaveInteger(p
,value
,encoding
);
375 ZIPLIST_INCR_LENGTH(zl
,1);
379 unsigned char *ziplistPush(unsigned char *zl
, unsigned char *s
, unsigned int slen
, int where
) {
381 p
= (where
== ZIPLIST_HEAD
) ? ZIPLIST_ENTRY_HEAD(zl
) : ZIPLIST_ENTRY_END(zl
);
382 return __ziplistInsert(zl
,p
,s
,slen
);
385 unsigned char *ziplistPop(unsigned char *zl
, sds
*target
, int where
) {
389 if (target
) *target
= NULL
;
391 /* Get pointer to element to remove */
392 p
= (where
== ZIPLIST_HEAD
) ? ZIPLIST_ENTRY_HEAD(zl
) : ZIPLIST_ENTRY_TAIL(zl
);
393 if (*p
== ZIP_END
) return zl
;
397 if (entry
.encoding
== ZIP_ENC_RAW
) {
398 *target
= sdsnewlen(p
+entry
.headersize
,entry
.len
);
400 value
= zipLoadInteger(p
+entry
.headersize
,entry
.encoding
);
401 *target
= sdscatprintf(sdsempty(), "%lld", value
);
405 zl
= __ziplistDelete(zl
,p
,1);
409 /* Returns an offset to use for iterating with ziplistNext. When the given
410 * index is negative, the list is traversed back to front. When the list
411 * doesn't contain an element at the provided index, NULL is returned. */
412 unsigned char *ziplistIndex(unsigned char *zl
, int index
) {
417 p
= ZIPLIST_ENTRY_TAIL(zl
);
418 if (p
[0] != ZIP_END
) {
420 while (entry
.prevrawlen
> 0 && index
--) {
421 p
-= entry
.prevrawlen
;
426 p
= ZIPLIST_ENTRY_HEAD(zl
);
427 while (p
[0] != ZIP_END
&& index
--) {
428 p
+= zipRawEntryLength(p
);
431 return (p
[0] == ZIP_END
|| index
> 0) ? NULL
: p
;
434 /* Return pointer to next entry in ziplist. */
435 unsigned char *ziplistNext(unsigned char *zl
, unsigned char *p
) {
438 /* "p" could be equal to ZIP_END, caused by ziplistDelete,
439 * and we should return NULL. Otherwise, we should return NULL
440 * when the *next* element is ZIP_END (there is no next entry). */
441 if (p
[0] == ZIP_END
) {
444 p
= p
+zipRawEntryLength(p
);
445 return (p
[0] == ZIP_END
) ? NULL
: p
;
449 /* Return pointer to previous entry in ziplist. */
450 unsigned char *ziplistPrev(unsigned char *zl
, unsigned char *p
) {
453 /* Iterating backwards from ZIP_END should return the tail. When "p" is
454 * equal to the first element of the list, we're already at the head,
455 * and should return NULL. */
456 if (p
[0] == ZIP_END
) {
457 p
= ZIPLIST_ENTRY_TAIL(zl
);
458 return (p
[0] == ZIP_END
) ? NULL
: p
;
459 } else if (p
== ZIPLIST_ENTRY_HEAD(zl
)) {
463 return p
-entry
.prevrawlen
;
467 /* Get entry pointer to by 'p' and store in either 'e' or 'v' depending
468 * on the encoding of the entry. 'e' is always set to NULL to be able
469 * to find out whether the string pointer or the integer value was set.
470 * Return 0 if 'p' points to the end of the zipmap, 1 otherwise. */
471 unsigned int ziplistGet(unsigned char *p
, unsigned char **sstr
, unsigned int *slen
, long long *sval
) {
473 if (p
== NULL
|| p
[0] == ZIP_END
) return 0;
474 if (sstr
) *sstr
= NULL
;
477 if (entry
.encoding
== ZIP_ENC_RAW
) {
480 *sstr
= p
+entry
.headersize
;
484 *sval
= zipLoadInteger(p
+entry
.headersize
,entry
.encoding
);
490 /* Insert an entry at "p". */
491 unsigned char *ziplistInsert(unsigned char *zl
, unsigned char *p
, unsigned char *s
, unsigned int slen
) {
492 return __ziplistInsert(zl
,p
,s
,slen
);
495 /* Delete a single entry from the ziplist, pointed to by *p.
496 * Also update *p in place, to be able to iterate over the
497 * ziplist, while deleting entries. */
498 unsigned char *ziplistDelete(unsigned char *zl
, unsigned char **p
) {
499 unsigned int offset
= *p
-zl
;
500 zl
= __ziplistDelete(zl
,*p
,1);
502 /* Store pointer to current element in p, because ziplistDelete will
503 * do a realloc which might result in a different "zl"-pointer.
504 * When the delete direction is back to front, we might delete the last
505 * entry and end up with "p" pointing to ZIP_END, so check this. */
510 /* Delete a range of entries from the ziplist. */
511 unsigned char *ziplistDeleteRange(unsigned char *zl
, unsigned int index
, unsigned int num
) {
512 unsigned char *p
= ziplistIndex(zl
,index
);
513 return (p
== NULL
) ? zl
: __ziplistDelete(zl
,p
,num
);
516 /* Compare entry pointer to by 'p' with 'entry'. Return 1 if equal. */
517 unsigned int ziplistCompare(unsigned char *p
, unsigned char *sstr
, unsigned int slen
) {
519 unsigned char sencoding
;
520 long long zval
, sval
;
521 if (p
[0] == ZIP_END
) return 0;
524 if (entry
.encoding
== ZIP_ENC_RAW
) {
526 if (entry
.len
== slen
) {
527 return memcmp(p
+entry
.headersize
,sstr
,slen
) == 0;
532 /* Try to compare encoded values */
533 if (zipTryEncoding(sstr
,&sval
,&sencoding
)) {
534 if (entry
.encoding
== sencoding
) {
535 zval
= zipLoadInteger(p
+entry
.headersize
,entry
.encoding
);
543 /* Return length of ziplist. */
544 unsigned int ziplistLen(unsigned char *zl
) {
545 unsigned int len
= 0;
546 if (ZIPLIST_LENGTH(zl
) < UINT16_MAX
) {
547 len
= ZIPLIST_LENGTH(zl
);
549 unsigned char *p
= zl
+ZIPLIST_HEADER_SIZE
;
550 while (*p
!= ZIP_END
) {
551 p
+= zipRawEntryLength(p
);
555 /* Re-store length if small enough */
556 if (len
< UINT16_MAX
) ZIPLIST_LENGTH(zl
) = len
;
561 /* Return size in bytes of ziplist. */
562 unsigned int ziplistSize(unsigned char *zl
) {
563 return ZIPLIST_BYTES(zl
);
566 void ziplistRepr(unsigned char *zl
) {
570 printf("{total bytes %d} {length %u}\n",ZIPLIST_BYTES(zl
), ZIPLIST_LENGTH(zl
));
571 p
= ZIPLIST_ENTRY_HEAD(zl
);
572 while(*p
!= ZIP_END
) {
574 printf("{offset %ld, header %u, payload %u} ",p
-zl
,entry
.headersize
,entry
.len
);
575 p
+= entry
.headersize
;
576 if (entry
.encoding
== ZIP_ENC_RAW
) {
577 fwrite(p
,entry
.len
,1,stdout
);
579 printf("%lld", zipLoadInteger(p
,entry
.encoding
));
587 #ifdef ZIPLIST_TEST_MAIN
588 #include <sys/time.h>
590 unsigned char *createList() {
591 unsigned char *zl
= ziplistNew();
592 zl
= ziplistPush(zl
, (unsigned char*)"foo", 3, ZIPLIST_TAIL
);
593 zl
= ziplistPush(zl
, (unsigned char*)"quux", 4, ZIPLIST_TAIL
);
594 zl
= ziplistPush(zl
, (unsigned char*)"hello", 5, ZIPLIST_HEAD
);
595 zl
= ziplistPush(zl
, (unsigned char*)"1024", 4, ZIPLIST_TAIL
);
599 unsigned char *createIntList() {
600 unsigned char *zl
= ziplistNew();
604 zl
= ziplistPush(zl
, (unsigned char*)buf
, strlen(buf
), ZIPLIST_TAIL
);
605 sprintf(buf
, "128000");
606 zl
= ziplistPush(zl
, (unsigned char*)buf
, strlen(buf
), ZIPLIST_TAIL
);
607 sprintf(buf
, "-100");
608 zl
= ziplistPush(zl
, (unsigned char*)buf
, strlen(buf
), ZIPLIST_HEAD
);
609 sprintf(buf
, "4294967296");
610 zl
= ziplistPush(zl
, (unsigned char*)buf
, strlen(buf
), ZIPLIST_HEAD
);
611 sprintf(buf
, "non integer");
612 zl
= ziplistPush(zl
, (unsigned char*)buf
, strlen(buf
), ZIPLIST_TAIL
);
613 sprintf(buf
, "much much longer non integer");
614 zl
= ziplistPush(zl
, (unsigned char*)buf
, strlen(buf
), ZIPLIST_TAIL
);
618 long long usec(void) {
620 gettimeofday(&tv
,NULL
);
621 return (((long long)tv
.tv_sec
)*1000000)+tv
.tv_usec
;
624 void stress(int pos
, int num
, int maxsize
, int dnum
) {
627 char posstr
[2][5] = { "HEAD", "TAIL" };
629 for (i
= 0; i
< maxsize
; i
+=dnum
) {
631 for (j
= 0; j
< i
; j
++) {
632 zl
= ziplistPush(zl
,(unsigned char*)"quux",4,ZIPLIST_TAIL
);
635 /* Do num times a push+pop from pos */
637 for (k
= 0; k
< num
; k
++) {
638 zl
= ziplistPush(zl
,(unsigned char*)"quux",4,pos
);
639 zl
= ziplistDeleteRange(zl
,0,1);
641 printf("List size: %8d, bytes: %8d, %dx push+pop (%s): %6lld usec\n",
642 i
,ZIPLIST_BYTES(zl
),num
,posstr
[pos
],usec()-start
);
647 int main(int argc
, char **argv
) {
648 unsigned char *zl
, *p
;
649 unsigned char *entry
;
654 zl
= createIntList();
660 zl
= ziplistPop(zl
, &s
, ZIPLIST_TAIL
);
661 printf("Pop tail: %s (length %ld)\n", s
, sdslen(s
));
664 zl
= ziplistPop(zl
, &s
, ZIPLIST_HEAD
);
665 printf("Pop head: %s (length %ld)\n", s
, sdslen(s
));
668 zl
= ziplistPop(zl
, &s
, ZIPLIST_TAIL
);
669 printf("Pop tail: %s (length %ld)\n", s
, sdslen(s
));
672 zl
= ziplistPop(zl
, &s
, ZIPLIST_TAIL
);
673 printf("Pop tail: %s (length %ld)\n", s
, sdslen(s
));
676 printf("Get element at index 3:\n");
679 p
= ziplistIndex(zl
, 3);
680 if (!ziplistGet(p
, &entry
, &elen
, &value
)) {
681 printf("ERROR: Could not access index 3\n");
685 fwrite(entry
,elen
,1,stdout
);
688 printf("%lld\n", value
);
693 printf("Get element at index 4 (out of range):\n");
696 p
= ziplistIndex(zl
, 4);
698 printf("No entry\n");
700 printf("ERROR: Out of range index should return NULL, returned offset: %ld\n", p
-zl
);
706 printf("Get element at index -1 (last element):\n");
709 p
= ziplistIndex(zl
, -1);
710 if (!ziplistGet(p
, &entry
, &elen
, &value
)) {
711 printf("ERROR: Could not access index -1\n");
715 fwrite(entry
,elen
,1,stdout
);
718 printf("%lld\n", value
);
723 printf("Get element at index -4 (first element):\n");
726 p
= ziplistIndex(zl
, -4);
727 if (!ziplistGet(p
, &entry
, &elen
, &value
)) {
728 printf("ERROR: Could not access index -4\n");
732 fwrite(entry
,elen
,1,stdout
);
735 printf("%lld\n", value
);
740 printf("Get element at index -5 (reverse out of range):\n");
743 p
= ziplistIndex(zl
, -5);
745 printf("No entry\n");
747 printf("ERROR: Out of range index should return NULL, returned offset: %ld\n", p
-zl
);
753 printf("Iterate list from 0 to end:\n");
756 p
= ziplistIndex(zl
, 0);
757 while (ziplistGet(p
, &entry
, &elen
, &value
)) {
760 fwrite(entry
,elen
,1,stdout
);
762 printf("%lld", value
);
764 p
= ziplistNext(zl
,p
);
770 printf("Iterate list from 1 to end:\n");
773 p
= ziplistIndex(zl
, 1);
774 while (ziplistGet(p
, &entry
, &elen
, &value
)) {
777 fwrite(entry
,elen
,1,stdout
);
779 printf("%lld", value
);
781 p
= ziplistNext(zl
,p
);
787 printf("Iterate list from 2 to end:\n");
790 p
= ziplistIndex(zl
, 2);
791 while (ziplistGet(p
, &entry
, &elen
, &value
)) {
794 fwrite(entry
,elen
,1,stdout
);
796 printf("%lld", value
);
798 p
= ziplistNext(zl
,p
);
804 printf("Iterate starting out of range:\n");
807 p
= ziplistIndex(zl
, 4);
808 if (!ziplistGet(p
, &entry
, &elen
, &value
)) {
809 printf("No entry\n");
816 printf("Iterate from back to front:\n");
819 p
= ziplistIndex(zl
, -1);
820 while (ziplistGet(p
, &entry
, &elen
, &value
)) {
823 fwrite(entry
,elen
,1,stdout
);
825 printf("%lld", value
);
827 p
= ziplistPrev(zl
,p
);
833 printf("Iterate from back to front, deleting all items:\n");
836 p
= ziplistIndex(zl
, -1);
837 while (ziplistGet(p
, &entry
, &elen
, &value
)) {
840 fwrite(entry
,elen
,1,stdout
);
842 printf("%lld", value
);
844 zl
= ziplistDelete(zl
,&p
);
845 p
= ziplistPrev(zl
,p
);
851 printf("Delete inclusive range 0,0:\n");
854 zl
= ziplistDeleteRange(zl
, 0, 1);
858 printf("Delete inclusive range 0,1:\n");
861 zl
= ziplistDeleteRange(zl
, 0, 2);
865 printf("Delete inclusive range 1,2:\n");
868 zl
= ziplistDeleteRange(zl
, 1, 2);
872 printf("Delete with start index out of range:\n");
875 zl
= ziplistDeleteRange(zl
, 5, 1);
879 printf("Delete with num overflow:\n");
882 zl
= ziplistDeleteRange(zl
, 1, 5);
886 printf("Delete foo while iterating:\n");
889 p
= ziplistIndex(zl
,0);
890 while (ziplistGet(p
,&entry
,&elen
,&value
)) {
891 if (entry
&& strncmp("foo",(char*)entry
,elen
) == 0) {
892 printf("Delete foo\n");
893 zl
= ziplistDelete(zl
,&p
);
897 fwrite(entry
,elen
,1,stdout
);
899 printf("%lld",value
);
901 p
= ziplistNext(zl
,p
);
909 printf("Create long list and check indices:\n");
914 for (i
= 0; i
< 1000; i
++) {
915 len
= sprintf(buf
,"%d",i
);
916 zl
= ziplistPush(zl
,(unsigned char*)buf
,len
,ZIPLIST_TAIL
);
918 for (i
= 0; i
< 1000; i
++) {
919 p
= ziplistIndex(zl
,i
);
920 assert(ziplistGet(p
,NULL
,NULL
,&value
));
923 p
= ziplistIndex(zl
,-i
-1);
924 assert(ziplistGet(p
,NULL
,NULL
,&value
));
925 assert(999-i
== value
);
927 printf("SUCCESS\n\n");
930 printf("Compare strings with ziplist entries:\n");
933 p
= ziplistIndex(zl
,0);
934 if (!ziplistCompare(p
,(unsigned char*)"hello",5)) {
935 printf("ERROR: not \"hello\"\n");
938 if (ziplistCompare(p
,(unsigned char*)"hella",5)) {
939 printf("ERROR: \"hella\"\n");
943 p
= ziplistIndex(zl
,3);
944 if (!ziplistCompare(p
,(unsigned char*)"1024",4)) {
945 printf("ERROR: not \"1024\"\n");
948 if (ziplistCompare(p
,(unsigned char*)"1025",4)) {
949 printf("ERROR: \"1025\"\n");
955 printf("Stress with variable ziplist size:\n");
957 stress(ZIPLIST_HEAD
,100000,16384,256);
958 stress(ZIPLIST_TAIL
,100000,16384,256);