]>
git.saurik.com Git - redis.git/blob - ziplist.c
4b9d0fadcd15f9a1277e66ffb615ec29dde321fb
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.
26 /* Important note: the ZIP_END value is used to depict the end of the
27 * ziplist structure. When a pointer contains an entry, the first couple
28 * of bytes contain the encoded length of the previous entry. This length
29 * is encoded as ZIP_ENC_RAW length, so the first two bits will contain 00
30 * and the byte will therefore never have a value of 255. */
32 #define ZIP_BIGLEN 254
36 #define ZIP_ENC_INT16 1
37 #define ZIP_ENC_INT32 2
38 #define ZIP_ENC_INT64 3
39 #define ZIP_ENCODING(p) ((p)[0] >> 6)
41 /* Length encoding for raw entries */
42 #define ZIP_LEN_INLINE 0
43 #define ZIP_LEN_UINT16 1
44 #define ZIP_LEN_UINT32 2
47 #define ZIPLIST_BYTES(zl) (*((uint32_t*)(zl)))
48 #define ZIPLIST_TAIL_OFFSET(zl) (*((uint32_t*)((zl)+sizeof(uint32_t))))
49 #define ZIPLIST_LENGTH(zl) (*((uint16_t*)((zl)+sizeof(uint32_t)*2)))
50 #define ZIPLIST_HEADER_SIZE (sizeof(uint32_t)*2+sizeof(uint16_t))
51 #define ZIPLIST_ENTRY_HEAD(zl) ((zl)+ZIPLIST_HEADER_SIZE)
52 #define ZIPLIST_ENTRY_TAIL(zl) ((zl)+ZIPLIST_TAIL_OFFSET(zl))
53 #define ZIPLIST_ENTRY_END(zl) ((zl)+ZIPLIST_BYTES(zl)-1)
55 /* We know a positive increment can only be 1 because entries can only be
56 * pushed one at a time. */
57 #define ZIPLIST_INCR_LENGTH(zl,incr) { \
58 if (ZIPLIST_LENGTH(zl) < UINT16_MAX) ZIPLIST_LENGTH(zl)+=incr; }
60 typedef struct zlentry
{
61 unsigned int prevrawlensize
, prevrawlen
;
62 unsigned int lensize
, len
;
63 unsigned int headersize
;
64 unsigned char encoding
;
68 /* Return bytes needed to store integer encoded by 'encoding' */
69 static unsigned int zipEncodingSize(unsigned char encoding
) {
70 if (encoding
== ZIP_ENC_INT16
) {
71 return sizeof(int16_t);
72 } else if (encoding
== ZIP_ENC_INT32
) {
73 return sizeof(int32_t);
74 } else if (encoding
== ZIP_ENC_INT64
) {
75 return sizeof(int64_t);
80 /* Decode the encoded length pointed by 'p'. If a pointer to 'lensize' is
81 * provided, it is set to the number of bytes required to encode the length. */
82 static unsigned int zipDecodeLength(unsigned char *p
, unsigned int *lensize
) {
83 unsigned char encoding
= ZIP_ENCODING(p
), lenenc
;
86 if (encoding
== ZIP_ENC_RAW
) {
87 lenenc
= (p
[0] >> 4) & 0x3;
88 if (lenenc
== ZIP_LEN_INLINE
) {
90 if (lensize
) *lensize
= 1;
91 } else if (lenenc
== ZIP_LEN_UINT16
) {
92 len
= p
[1] | (p
[2] << 8);
93 if (lensize
) *lensize
= 3;
95 len
= p
[1] | (p
[2] << 8) | (p
[3] << 16) | (p
[4] << 24);
96 if (lensize
) *lensize
= 5;
99 len
= zipEncodingSize(encoding
);
100 if (lensize
) *lensize
= 1;
105 /* Encode the length 'l' writing it in 'p'. If p is NULL it just returns
106 * the amount of bytes required to encode such a length. */
107 static unsigned int zipEncodeLength(unsigned char *p
, char encoding
, unsigned int rawlen
) {
108 unsigned char len
= 1, lenenc
, buf
[5];
109 if (encoding
== ZIP_ENC_RAW
) {
112 lenenc
= ZIP_LEN_INLINE
;
114 } else if (rawlen
<= 0xffff) {
117 lenenc
= ZIP_LEN_UINT16
;
118 buf
[1] = (rawlen
) & 0xff;
119 buf
[2] = (rawlen
>> 8) & 0xff;
123 lenenc
= ZIP_LEN_UINT32
;
124 buf
[1] = (rawlen
) & 0xff;
125 buf
[2] = (rawlen
>> 8) & 0xff;
126 buf
[3] = (rawlen
>> 16) & 0xff;
127 buf
[4] = (rawlen
>> 24) & 0xff;
129 buf
[0] = (lenenc
<< 4) | (buf
[0] & 0xf);
133 /* Apparently we need to store the length in 'p' */
134 buf
[0] = (encoding
<< 6) | (buf
[0] & 0x3f);
139 /* Decode the length of the previous element stored at "p". */
140 static unsigned int zipPrevDecodeLength(unsigned char *p
, unsigned int *lensize
) {
141 unsigned int len
= *p
;
142 if (len
< ZIP_BIGLEN
) {
143 if (lensize
) *lensize
= 1;
145 if (lensize
) *lensize
= 1+sizeof(len
);
146 memcpy(&len
,p
+1,sizeof(len
));
151 /* Encode the length of the previous entry and write it to "p". Return the
152 * number of bytes needed to encode this length if "p" is NULL. */
153 static unsigned int zipPrevEncodeLength(unsigned char *p
, unsigned int len
) {
155 return (len
< ZIP_BIGLEN
) ? 1 : sizeof(len
)+1;
157 if (len
< ZIP_BIGLEN
) {
162 memcpy(p
+1,&len
,sizeof(len
));
163 return 1+sizeof(len
);
168 /* Return the difference in number of bytes needed to store the new length
169 * "len" on the entry pointed to by "p". */
170 static int zipPrevLenByteDiff(unsigned char *p
, unsigned int len
) {
171 unsigned int prevlensize
;
172 zipPrevDecodeLength(p
,&prevlensize
);
173 return zipPrevEncodeLength(NULL
,len
)-prevlensize
;
176 /* Check if string pointed to by 'entry' can be encoded as an integer.
177 * Stores the integer value in 'v' and its encoding in 'encoding'.
178 * Warning: this function requires a NULL-terminated string! */
179 static int zipTryEncoding(unsigned char *entry
, long long *v
, unsigned char *encoding
) {
183 if (entry
[0] == '-' || (entry
[0] >= '0' && entry
[0] <= '9')) {
184 value
= strtoll((char*)entry
,&eptr
,10);
185 if (eptr
[0] != '\0') return 0;
186 if (value
>= INT16_MIN
&& value
<= INT16_MAX
) {
187 *encoding
= ZIP_ENC_INT16
;
188 } else if (value
>= INT32_MIN
&& value
<= INT32_MAX
) {
189 *encoding
= ZIP_ENC_INT32
;
191 *encoding
= ZIP_ENC_INT64
;
199 /* Store integer 'value' at 'p', encoded as 'encoding' */
200 static void zipSaveInteger(unsigned char *p
, int64_t value
, unsigned char encoding
) {
204 if (encoding
== ZIP_ENC_INT16
) {
206 memcpy(p
,&i16
,sizeof(i16
));
207 } else if (encoding
== ZIP_ENC_INT32
) {
209 memcpy(p
,&i32
,sizeof(i32
));
210 } else if (encoding
== ZIP_ENC_INT64
) {
212 memcpy(p
,&i64
,sizeof(i64
));
218 /* Read integer encoded as 'encoding' from 'p' */
219 static int64_t zipLoadInteger(unsigned char *p
, unsigned char encoding
) {
223 if (encoding
== ZIP_ENC_INT16
) {
224 memcpy(&i16
,p
,sizeof(i16
));
226 } else if (encoding
== ZIP_ENC_INT32
) {
227 memcpy(&i32
,p
,sizeof(i32
));
229 } else if (encoding
== ZIP_ENC_INT64
) {
230 memcpy(&i64
,p
,sizeof(i64
));
238 /* Return a struct with all information about an entry. */
239 static zlentry
zipEntry(unsigned char *p
) {
241 e
.prevrawlen
= zipPrevDecodeLength(p
,&e
.prevrawlensize
);
242 e
.len
= zipDecodeLength(p
+e
.prevrawlensize
,&e
.lensize
);
243 e
.headersize
= e
.prevrawlensize
+e
.lensize
;
244 e
.encoding
= ZIP_ENCODING(p
+e
.prevrawlensize
);
249 /* Return the total number of bytes used by the entry at "p". */
250 static unsigned int zipRawEntryLength(unsigned char *p
) {
251 zlentry e
= zipEntry(p
);
252 return e
.headersize
+ e
.len
;
255 /* Create a new empty ziplist. */
256 unsigned char *ziplistNew(void) {
257 unsigned int bytes
= ZIPLIST_HEADER_SIZE
+1;
258 unsigned char *zl
= zmalloc(bytes
);
259 ZIPLIST_BYTES(zl
) = bytes
;
260 ZIPLIST_TAIL_OFFSET(zl
) = ZIPLIST_HEADER_SIZE
;
261 ZIPLIST_LENGTH(zl
) = 0;
262 zl
[bytes
-1] = ZIP_END
;
266 /* Resize the ziplist. */
267 static unsigned char *ziplistResize(unsigned char *zl
, unsigned int len
) {
268 zl
= zrealloc(zl
,len
);
269 ZIPLIST_BYTES(zl
) = len
;
274 /* Delete "num" entries, starting at "p". Returns pointer to the ziplist. */
275 static unsigned char *__ziplistDelete(unsigned char *zl
, unsigned char *p
, unsigned int num
) {
276 unsigned int i
, totlen
, deleted
= 0;
278 zlentry first
= zipEntry(p
);
279 for (i
= 0; p
[0] != ZIP_END
&& i
< num
; i
++) {
280 p
+= zipRawEntryLength(p
);
286 if (p
[0] != ZIP_END
) {
287 /* Tricky: storing the prevlen in this entry might reduce or
288 * increase the number of bytes needed, compared to the current
289 * prevlen. Note that we can always store this length because
290 * it was previously stored by an entry that is being deleted. */
291 nextdiff
= zipPrevLenByteDiff(p
,first
.prevrawlen
);
292 zipPrevEncodeLength(p
-nextdiff
,first
.prevrawlen
);
294 /* Update offset for tail */
295 ZIPLIST_TAIL_OFFSET(zl
) -= totlen
+nextdiff
;
297 /* Move tail to the front of the ziplist */
298 memmove(first
.p
,p
-nextdiff
,ZIPLIST_BYTES(zl
)-(p
-zl
)-1+nextdiff
);
300 /* The entire tail was deleted. No need to move memory. */
301 ZIPLIST_TAIL_OFFSET(zl
) = (first
.p
-zl
)-first
.prevrawlen
;
304 /* Resize and update length */
305 zl
= ziplistResize(zl
, ZIPLIST_BYTES(zl
)-totlen
+nextdiff
);
306 ZIPLIST_INCR_LENGTH(zl
,-deleted
);
311 /* Insert item at "p". */
312 static unsigned char *__ziplistInsert(unsigned char *zl
, unsigned char *p
, unsigned char *s
, unsigned int slen
) {
313 unsigned int curlen
= ZIPLIST_BYTES(zl
), reqlen
, prevlen
= 0;
314 unsigned int offset
, nextdiff
= 0;
316 unsigned char encoding
= ZIP_ENC_RAW
;
320 /* Find out prevlen for the entry that is inserted. */
321 if (p
[0] != ZIP_END
) {
323 prevlen
= entry
.prevrawlen
;
325 tail
= ZIPLIST_ENTRY_TAIL(zl
);
326 if (tail
[0] != ZIP_END
) {
327 prevlen
= zipRawEntryLength(tail
);
331 /* See if the entry can be encoded */
332 if (zipTryEncoding(s
,&value
,&encoding
)) {
333 reqlen
= zipEncodingSize(encoding
);
338 /* We need space for both the length of the previous entry and
339 * the length of the payload. */
340 reqlen
+= zipPrevEncodeLength(NULL
,prevlen
);
341 reqlen
+= zipEncodeLength(NULL
,encoding
,slen
);
343 /* When the insert position is not equal to the tail, we need to
344 * make sure that the next entry can hold this entry's length in
345 * its prevlen field. */
346 nextdiff
= (p
[0] != ZIP_END
) ? zipPrevLenByteDiff(p
,reqlen
) : 0;
348 /* Store offset because a realloc may change the address of zl. */
350 zl
= ziplistResize(zl
,curlen
+reqlen
+nextdiff
);
353 /* Apply memory move when necessary and update tail offset. */
354 if (p
[0] != ZIP_END
) {
355 /* Subtract one because of the ZIP_END bytes */
356 memmove(p
+reqlen
,p
-nextdiff
,curlen
-offset
-1+nextdiff
);
357 /* Encode this entry's raw length in the next entry. */
358 zipPrevEncodeLength(p
+reqlen
,reqlen
);
359 /* Update offset for tail */
360 ZIPLIST_TAIL_OFFSET(zl
) += reqlen
+nextdiff
;
362 /* This element will be the new tail. */
363 ZIPLIST_TAIL_OFFSET(zl
) = p
-zl
;
366 /* Write the entry */
367 p
+= zipPrevEncodeLength(p
,prevlen
);
368 p
+= zipEncodeLength(p
,encoding
,slen
);
369 if (encoding
!= ZIP_ENC_RAW
) {
370 zipSaveInteger(p
,value
,encoding
);
374 ZIPLIST_INCR_LENGTH(zl
,1);
378 unsigned char *ziplistPush(unsigned char *zl
, unsigned char *s
, unsigned int slen
, int where
) {
380 p
= (where
== ZIPLIST_HEAD
) ? ZIPLIST_ENTRY_HEAD(zl
) : ZIPLIST_ENTRY_END(zl
);
381 return __ziplistInsert(zl
,p
,s
,slen
);
384 /* Returns an offset to use for iterating with ziplistNext. When the given
385 * index is negative, the list is traversed back to front. When the list
386 * doesn't contain an element at the provided index, NULL is returned. */
387 unsigned char *ziplistIndex(unsigned char *zl
, int index
) {
392 p
= ZIPLIST_ENTRY_TAIL(zl
);
393 if (p
[0] != ZIP_END
) {
395 while (entry
.prevrawlen
> 0 && index
--) {
396 p
-= entry
.prevrawlen
;
401 p
= ZIPLIST_ENTRY_HEAD(zl
);
402 while (p
[0] != ZIP_END
&& index
--) {
403 p
+= zipRawEntryLength(p
);
406 return (p
[0] == ZIP_END
|| index
> 0) ? NULL
: p
;
409 /* Return pointer to next entry in ziplist. */
410 unsigned char *ziplistNext(unsigned char *zl
, unsigned char *p
) {
413 /* "p" could be equal to ZIP_END, caused by ziplistDelete,
414 * and we should return NULL. Otherwise, we should return NULL
415 * when the *next* element is ZIP_END (there is no next entry). */
416 if (p
[0] == ZIP_END
) {
419 p
= p
+zipRawEntryLength(p
);
420 return (p
[0] == ZIP_END
) ? NULL
: p
;
424 /* Return pointer to previous entry in ziplist. */
425 unsigned char *ziplistPrev(unsigned char *zl
, unsigned char *p
) {
428 /* Iterating backwards from ZIP_END should return the tail. When "p" is
429 * equal to the first element of the list, we're already at the head,
430 * and should return NULL. */
431 if (p
[0] == ZIP_END
) {
432 p
= ZIPLIST_ENTRY_TAIL(zl
);
433 return (p
[0] == ZIP_END
) ? NULL
: p
;
434 } else if (p
== ZIPLIST_ENTRY_HEAD(zl
)) {
438 return p
-entry
.prevrawlen
;
442 /* Get entry pointer to by 'p' and store in either 'e' or 'v' depending
443 * on the encoding of the entry. 'e' is always set to NULL to be able
444 * to find out whether the string pointer or the integer value was set.
445 * Return 0 if 'p' points to the end of the zipmap, 1 otherwise. */
446 unsigned int ziplistGet(unsigned char *p
, unsigned char **sstr
, unsigned int *slen
, long long *sval
) {
448 if (p
== NULL
|| p
[0] == ZIP_END
) return 0;
449 if (sstr
) *sstr
= NULL
;
452 if (entry
.encoding
== ZIP_ENC_RAW
) {
455 *sstr
= p
+entry
.headersize
;
459 *sval
= zipLoadInteger(p
+entry
.headersize
,entry
.encoding
);
465 /* Insert an entry at "p". */
466 unsigned char *ziplistInsert(unsigned char *zl
, unsigned char *p
, unsigned char *s
, unsigned int slen
) {
467 return __ziplistInsert(zl
,p
,s
,slen
);
470 /* Delete a single entry from the ziplist, pointed to by *p.
471 * Also update *p in place, to be able to iterate over the
472 * ziplist, while deleting entries. */
473 unsigned char *ziplistDelete(unsigned char *zl
, unsigned char **p
) {
474 unsigned int offset
= *p
-zl
;
475 zl
= __ziplistDelete(zl
,*p
,1);
477 /* Store pointer to current element in p, because ziplistDelete will
478 * do a realloc which might result in a different "zl"-pointer.
479 * When the delete direction is back to front, we might delete the last
480 * entry and end up with "p" pointing to ZIP_END, so check this. */
485 /* Delete a range of entries from the ziplist. */
486 unsigned char *ziplistDeleteRange(unsigned char *zl
, unsigned int index
, unsigned int num
) {
487 unsigned char *p
= ziplistIndex(zl
,index
);
488 return (p
== NULL
) ? zl
: __ziplistDelete(zl
,p
,num
);
491 /* Compare entry pointer to by 'p' with 'entry'. Return 1 if equal. */
492 unsigned int ziplistCompare(unsigned char *p
, unsigned char *sstr
, unsigned int slen
) {
494 unsigned char sencoding
;
495 long long zval
, sval
;
496 if (p
[0] == ZIP_END
) return 0;
499 if (entry
.encoding
== ZIP_ENC_RAW
) {
501 if (entry
.len
== slen
) {
502 return memcmp(p
+entry
.headersize
,sstr
,slen
) == 0;
507 /* Try to compare encoded values */
508 if (zipTryEncoding(sstr
,&sval
,&sencoding
)) {
509 if (entry
.encoding
== sencoding
) {
510 zval
= zipLoadInteger(p
+entry
.headersize
,entry
.encoding
);
518 /* Return length of ziplist. */
519 unsigned int ziplistLen(unsigned char *zl
) {
520 unsigned int len
= 0;
521 if (ZIPLIST_LENGTH(zl
) < UINT16_MAX
) {
522 len
= ZIPLIST_LENGTH(zl
);
524 unsigned char *p
= zl
+ZIPLIST_HEADER_SIZE
;
525 while (*p
!= ZIP_END
) {
526 p
+= zipRawEntryLength(p
);
530 /* Re-store length if small enough */
531 if (len
< UINT16_MAX
) ZIPLIST_LENGTH(zl
) = len
;
536 /* Return size in bytes of ziplist. */
537 unsigned int ziplistSize(unsigned char *zl
) {
538 return ZIPLIST_BYTES(zl
);
541 void ziplistRepr(unsigned char *zl
) {
545 printf("{total bytes %d} {length %u}\n",ZIPLIST_BYTES(zl
), ZIPLIST_LENGTH(zl
));
546 p
= ZIPLIST_ENTRY_HEAD(zl
);
547 while(*p
!= ZIP_END
) {
549 printf("{offset %ld, header %u, payload %u} ",p
-zl
,entry
.headersize
,entry
.len
);
550 p
+= entry
.headersize
;
551 if (entry
.encoding
== ZIP_ENC_RAW
) {
552 fwrite(p
,entry
.len
,1,stdout
);
554 printf("%lld", zipLoadInteger(p
,entry
.encoding
));
562 #ifdef ZIPLIST_TEST_MAIN
563 #include <sys/time.h>
565 unsigned char *createList() {
566 unsigned char *zl
= ziplistNew();
567 zl
= ziplistPush(zl
, (unsigned char*)"foo", 3, ZIPLIST_TAIL
);
568 zl
= ziplistPush(zl
, (unsigned char*)"quux", 4, ZIPLIST_TAIL
);
569 zl
= ziplistPush(zl
, (unsigned char*)"hello", 5, ZIPLIST_HEAD
);
570 zl
= ziplistPush(zl
, (unsigned char*)"1024", 4, ZIPLIST_TAIL
);
574 unsigned char *createIntList() {
575 unsigned char *zl
= ziplistNew();
579 zl
= ziplistPush(zl
, (unsigned char*)buf
, strlen(buf
), ZIPLIST_TAIL
);
580 sprintf(buf
, "128000");
581 zl
= ziplistPush(zl
, (unsigned char*)buf
, strlen(buf
), ZIPLIST_TAIL
);
582 sprintf(buf
, "-100");
583 zl
= ziplistPush(zl
, (unsigned char*)buf
, strlen(buf
), ZIPLIST_HEAD
);
584 sprintf(buf
, "4294967296");
585 zl
= ziplistPush(zl
, (unsigned char*)buf
, strlen(buf
), ZIPLIST_HEAD
);
586 sprintf(buf
, "non integer");
587 zl
= ziplistPush(zl
, (unsigned char*)buf
, strlen(buf
), ZIPLIST_TAIL
);
588 sprintf(buf
, "much much longer non integer");
589 zl
= ziplistPush(zl
, (unsigned char*)buf
, strlen(buf
), ZIPLIST_TAIL
);
593 long long usec(void) {
595 gettimeofday(&tv
,NULL
);
596 return (((long long)tv
.tv_sec
)*1000000)+tv
.tv_usec
;
599 void stress(int pos
, int num
, int maxsize
, int dnum
) {
602 char posstr
[2][5] = { "HEAD", "TAIL" };
604 for (i
= 0; i
< maxsize
; i
+=dnum
) {
606 for (j
= 0; j
< i
; j
++) {
607 zl
= ziplistPush(zl
,(unsigned char*)"quux",4,ZIPLIST_TAIL
);
610 /* Do num times a push+pop from pos */
612 for (k
= 0; k
< num
; k
++) {
613 zl
= ziplistPush(zl
,(unsigned char*)"quux",4,pos
);
614 zl
= ziplistDeleteRange(zl
,0,1);
616 printf("List size: %8d, bytes: %8d, %dx push+pop (%s): %6lld usec\n",
617 i
,ZIPLIST_BYTES(zl
),num
,posstr
[pos
],usec()-start
);
622 void pop(unsigned char *zl
, int where
) {
623 unsigned char *p
, *vstr
;
627 p
= ziplistIndex(zl
,where
== ZIPLIST_HEAD
? 0 : -1);
628 if (ziplistGet(p
,&vstr
,&vlen
,&vlong
)) {
629 if (where
== ZIPLIST_HEAD
)
630 printf("Pop head: ");
632 printf("Pop tail: ");
635 fwrite(vstr
,vlen
,1,stdout
);
637 printf("%lld", vlong
);
640 ziplistDeleteRange(zl
,-1,1);
642 printf("ERROR: Could not pop\n");
647 int main(int argc
, char **argv
) {
648 unsigned char *zl
, *p
;
649 unsigned char *entry
;
653 zl
= createIntList();
659 pop(zl
,ZIPLIST_TAIL
);
662 pop(zl
,ZIPLIST_HEAD
);
665 pop(zl
,ZIPLIST_TAIL
);
668 pop(zl
,ZIPLIST_TAIL
);
671 printf("Get element at index 3:\n");
674 p
= ziplistIndex(zl
, 3);
675 if (!ziplistGet(p
, &entry
, &elen
, &value
)) {
676 printf("ERROR: Could not access index 3\n");
680 fwrite(entry
,elen
,1,stdout
);
683 printf("%lld\n", value
);
688 printf("Get element at index 4 (out of range):\n");
691 p
= ziplistIndex(zl
, 4);
693 printf("No entry\n");
695 printf("ERROR: Out of range index should return NULL, returned offset: %ld\n", p
-zl
);
701 printf("Get element at index -1 (last element):\n");
704 p
= ziplistIndex(zl
, -1);
705 if (!ziplistGet(p
, &entry
, &elen
, &value
)) {
706 printf("ERROR: Could not access index -1\n");
710 fwrite(entry
,elen
,1,stdout
);
713 printf("%lld\n", value
);
718 printf("Get element at index -4 (first element):\n");
721 p
= ziplistIndex(zl
, -4);
722 if (!ziplistGet(p
, &entry
, &elen
, &value
)) {
723 printf("ERROR: Could not access index -4\n");
727 fwrite(entry
,elen
,1,stdout
);
730 printf("%lld\n", value
);
735 printf("Get element at index -5 (reverse out of range):\n");
738 p
= ziplistIndex(zl
, -5);
740 printf("No entry\n");
742 printf("ERROR: Out of range index should return NULL, returned offset: %ld\n", p
-zl
);
748 printf("Iterate list from 0 to end:\n");
751 p
= ziplistIndex(zl
, 0);
752 while (ziplistGet(p
, &entry
, &elen
, &value
)) {
755 fwrite(entry
,elen
,1,stdout
);
757 printf("%lld", value
);
759 p
= ziplistNext(zl
,p
);
765 printf("Iterate list from 1 to end:\n");
768 p
= ziplistIndex(zl
, 1);
769 while (ziplistGet(p
, &entry
, &elen
, &value
)) {
772 fwrite(entry
,elen
,1,stdout
);
774 printf("%lld", value
);
776 p
= ziplistNext(zl
,p
);
782 printf("Iterate list from 2 to end:\n");
785 p
= ziplistIndex(zl
, 2);
786 while (ziplistGet(p
, &entry
, &elen
, &value
)) {
789 fwrite(entry
,elen
,1,stdout
);
791 printf("%lld", value
);
793 p
= ziplistNext(zl
,p
);
799 printf("Iterate starting out of range:\n");
802 p
= ziplistIndex(zl
, 4);
803 if (!ziplistGet(p
, &entry
, &elen
, &value
)) {
804 printf("No entry\n");
811 printf("Iterate from back to front:\n");
814 p
= ziplistIndex(zl
, -1);
815 while (ziplistGet(p
, &entry
, &elen
, &value
)) {
818 fwrite(entry
,elen
,1,stdout
);
820 printf("%lld", value
);
822 p
= ziplistPrev(zl
,p
);
828 printf("Iterate from back to front, deleting all items:\n");
831 p
= ziplistIndex(zl
, -1);
832 while (ziplistGet(p
, &entry
, &elen
, &value
)) {
835 fwrite(entry
,elen
,1,stdout
);
837 printf("%lld", value
);
839 zl
= ziplistDelete(zl
,&p
);
840 p
= ziplistPrev(zl
,p
);
846 printf("Delete inclusive range 0,0:\n");
849 zl
= ziplistDeleteRange(zl
, 0, 1);
853 printf("Delete inclusive range 0,1:\n");
856 zl
= ziplistDeleteRange(zl
, 0, 2);
860 printf("Delete inclusive range 1,2:\n");
863 zl
= ziplistDeleteRange(zl
, 1, 2);
867 printf("Delete with start index out of range:\n");
870 zl
= ziplistDeleteRange(zl
, 5, 1);
874 printf("Delete with num overflow:\n");
877 zl
= ziplistDeleteRange(zl
, 1, 5);
881 printf("Delete foo while iterating:\n");
884 p
= ziplistIndex(zl
,0);
885 while (ziplistGet(p
,&entry
,&elen
,&value
)) {
886 if (entry
&& strncmp("foo",(char*)entry
,elen
) == 0) {
887 printf("Delete foo\n");
888 zl
= ziplistDelete(zl
,&p
);
892 fwrite(entry
,elen
,1,stdout
);
894 printf("%lld",value
);
896 p
= ziplistNext(zl
,p
);
904 printf("Create long list and check indices:\n");
909 for (i
= 0; i
< 1000; i
++) {
910 len
= sprintf(buf
,"%d",i
);
911 zl
= ziplistPush(zl
,(unsigned char*)buf
,len
,ZIPLIST_TAIL
);
913 for (i
= 0; i
< 1000; i
++) {
914 p
= ziplistIndex(zl
,i
);
915 assert(ziplistGet(p
,NULL
,NULL
,&value
));
918 p
= ziplistIndex(zl
,-i
-1);
919 assert(ziplistGet(p
,NULL
,NULL
,&value
));
920 assert(999-i
== value
);
922 printf("SUCCESS\n\n");
925 printf("Compare strings with ziplist entries:\n");
928 p
= ziplistIndex(zl
,0);
929 if (!ziplistCompare(p
,(unsigned char*)"hello",5)) {
930 printf("ERROR: not \"hello\"\n");
933 if (ziplistCompare(p
,(unsigned char*)"hella",5)) {
934 printf("ERROR: \"hella\"\n");
938 p
= ziplistIndex(zl
,3);
939 if (!ziplistCompare(p
,(unsigned char*)"1024",4)) {
940 printf("ERROR: not \"1024\"\n");
943 if (ziplistCompare(p
,(unsigned char*)"1025",4)) {
944 printf("ERROR: \"1025\"\n");
950 printf("Stress with variable ziplist size:\n");
952 stress(ZIPLIST_HEAD
,100000,16384,256);
953 stress(ZIPLIST_TAIL
,100000,16384,256);