]> git.saurik.com Git - redis.git/blob - ziplist.c
ec988bc83c2766b356fae6d030c26cfdbb2ab461
[redis.git] / ziplist.c
1 /* Memory layout of a ziplist, containing "foo", "bar", "quux":
2 * <zlbytes><zllen><len>"foo"<len>"bar"<len>"quux"
3 *
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.
7 *
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.
11 *
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.
15 */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <assert.h>
21 #include <limits.h>
22 #include "zmalloc.h"
23 #include "sds.h"
24 #include "ziplist.h"
25
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. */
31 #define ZIP_END 255
32 #define ZIP_BIGLEN 254
33
34 /* Entry encoding */
35 #define ZIP_ENC_RAW 0
36 #define ZIP_ENC_SHORT 1
37 #define ZIP_ENC_INT 2
38 #define ZIP_ENC_LLONG 3
39 #define ZIP_ENCODING(p) ((p)[0] >> 6)
40
41 /* Length encoding for raw entries */
42 #define ZIP_LEN_INLINE 0
43 #define ZIP_LEN_UINT16 1
44 #define ZIP_LEN_UINT32 2
45
46 /* Utility macros */
47 #define ZIPLIST_BYTES(zl) (*((unsigned int*)(zl)))
48 #define ZIPLIST_TAIL_OFFSET(zl) (*((zl)+sizeof(unsigned int)))
49 #define ZIPLIST_LENGTH(zl) (*((zl)+2*sizeof(unsigned int)))
50 #define ZIPLIST_HEADER_SIZE (2*sizeof(unsigned int)+1)
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)
54 #define ZIPLIST_INCR_LENGTH(zl,incr) { \
55 if (ZIPLIST_LENGTH(zl) < ZIP_BIGLEN) ZIPLIST_LENGTH(zl)+=incr; }
56
57 typedef struct zlentry {
58 unsigned int prevrawlensize, prevrawlen;
59 unsigned int lensize, len;
60 unsigned int headersize;
61 unsigned char encoding;
62 unsigned char *p;
63 } zlentry;
64
65 /* Return bytes needed to store integer encoded by 'encoding' */
66 static unsigned int zipEncodingSize(char encoding) {
67 if (encoding == ZIP_ENC_SHORT) {
68 return sizeof(short int);
69 } else if (encoding == ZIP_ENC_INT) {
70 return sizeof(int);
71 } else if (encoding == ZIP_ENC_LLONG) {
72 return sizeof(long long);
73 }
74 assert(NULL);
75 }
76
77 /* Decode the encoded length pointed by 'p'. If a pointer to 'lensize' is
78 * provided, it is set to the number of bytes required to encode the length. */
79 static unsigned int zipDecodeLength(unsigned char *p, unsigned int *lensize) {
80 unsigned char encoding = ZIP_ENCODING(p), lenenc;
81 unsigned int len;
82
83 if (encoding == ZIP_ENC_RAW) {
84 lenenc = (p[0] >> 4) & 0x3;
85 if (lenenc == ZIP_LEN_INLINE) {
86 len = p[0] & 0xf;
87 if (lensize) *lensize = 1;
88 } else if (lenenc == ZIP_LEN_UINT16) {
89 len = p[1] | (p[2] << 8);
90 if (lensize) *lensize = 3;
91 } else {
92 len = p[1] | (p[2] << 8) | (p[3] << 16) | (p[4] << 24);
93 if (lensize) *lensize = 5;
94 }
95 } else {
96 len = zipEncodingSize(encoding);
97 if (lensize) *lensize = 1;
98 }
99 return len;
100 }
101
102 /* Encode the length 'l' writing it in 'p'. If p is NULL it just returns
103 * the amount of bytes required to encode such a length. */
104 static unsigned int zipEncodeLength(unsigned char *p, char encoding, unsigned int rawlen) {
105 unsigned char len = 1, lenenc, buf[5];
106 if (encoding == ZIP_ENC_RAW) {
107 if (rawlen <= 0xf) {
108 if (!p) return len;
109 lenenc = ZIP_LEN_INLINE;
110 buf[0] = rawlen;
111 } else if (rawlen <= 0xffff) {
112 len += 2;
113 if (!p) return len;
114 lenenc = ZIP_LEN_UINT16;
115 buf[1] = (rawlen ) & 0xff;
116 buf[2] = (rawlen >> 8) & 0xff;
117 } else {
118 len += 4;
119 if (!p) return len;
120 lenenc = ZIP_LEN_UINT32;
121 buf[1] = (rawlen ) & 0xff;
122 buf[2] = (rawlen >> 8) & 0xff;
123 buf[3] = (rawlen >> 16) & 0xff;
124 buf[4] = (rawlen >> 24) & 0xff;
125 }
126 buf[0] = (lenenc << 4) | (buf[0] & 0xf);
127 }
128 if (!p) return len;
129
130 /* Apparently we need to store the length in 'p' */
131 buf[0] = (encoding << 6) | (buf[0] & 0x3f);
132 memcpy(p,buf,len);
133 return len;
134 }
135
136 /* Decode the length of the previous element stored at "p". */
137 static unsigned int zipPrevDecodeLength(unsigned char *p, unsigned int *lensize) {
138 unsigned int len = *p;
139 if (len < ZIP_BIGLEN) {
140 if (lensize) *lensize = 1;
141 } else {
142 if (lensize) *lensize = 1+sizeof(len);
143 memcpy(&len,p+1,sizeof(len));
144 }
145 return len;
146 }
147
148 /* Encode the length of the previous entry and write it to "p". Return the
149 * number of bytes needed to encode this length if "p" is NULL. */
150 static unsigned int zipPrevEncodeLength(unsigned char *p, unsigned int len) {
151 if (p == NULL) {
152 return (len < ZIP_BIGLEN) ? 1 : sizeof(len)+1;
153 } else {
154 if (len < ZIP_BIGLEN) {
155 p[0] = len;
156 return 1;
157 } else {
158 p[0] = ZIP_BIGLEN;
159 memcpy(p+1,&len,sizeof(len));
160 return 1+sizeof(len);
161 }
162 }
163 }
164
165 /* Return the difference in number of bytes needed to store the new length
166 * "len" on the entry pointed to by "p". */
167 static int zipPrevLenByteDiff(unsigned char *p, unsigned int len) {
168 unsigned int prevlensize;
169 zipPrevDecodeLength(p,&prevlensize);
170 return zipPrevEncodeLength(NULL,len)-prevlensize;
171 }
172
173 /* Check if string pointed to by 'entry' can be encoded as an integer.
174 * Stores the integer value in 'v' and its encoding in 'encoding'.
175 * Warning: this function requires a NULL-terminated string! */
176 static int zipTryEncoding(char *entry, long long *v, char *encoding) {
177 long long value;
178 char *eptr;
179
180 if (entry[0] == '-' || (entry[0] >= '0' && entry[0] <= '9')) {
181 value = strtoll(entry,&eptr,10);
182 if (eptr[0] != '\0') return 0;
183 if (value >= SHRT_MIN && value <= SHRT_MAX) {
184 *encoding = ZIP_ENC_SHORT;
185 } else if (value >= INT_MIN && value <= INT_MAX) {
186 *encoding = ZIP_ENC_INT;
187 } else {
188 *encoding = ZIP_ENC_LLONG;
189 }
190 *v = value;
191 return 1;
192 }
193 return 0;
194 }
195
196 /* Store integer 'value' at 'p', encoded as 'encoding' */
197 static void zipSaveInteger(unsigned char *p, long long value, char encoding) {
198 short int s;
199 int i;
200 long long l;
201 if (encoding == ZIP_ENC_SHORT) {
202 s = value;
203 memcpy(p,&s,sizeof(s));
204 } else if (encoding == ZIP_ENC_INT) {
205 i = value;
206 memcpy(p,&i,sizeof(i));
207 } else if (encoding == ZIP_ENC_LLONG) {
208 l = value;
209 memcpy(p,&l,sizeof(l));
210 } else {
211 assert(NULL);
212 }
213 }
214
215 /* Read integer encoded as 'encoding' from 'p' */
216 static long long zipLoadInteger(unsigned char *p, char encoding) {
217 short int s;
218 int i;
219 long long l, ret;
220 if (encoding == ZIP_ENC_SHORT) {
221 memcpy(&s,p,sizeof(s));
222 ret = s;
223 } else if (encoding == ZIP_ENC_INT) {
224 memcpy(&i,p,sizeof(i));
225 ret = i;
226 } else if (encoding == ZIP_ENC_LLONG) {
227 memcpy(&l,p,sizeof(l));
228 ret = l;
229 } else {
230 assert(NULL);
231 }
232 return ret;
233 }
234
235 /* Return a struct with all information about an entry. */
236 static zlentry zipEntry(unsigned char *p) {
237 zlentry e;
238 e.prevrawlen = zipPrevDecodeLength(p,&e.prevrawlensize);
239 e.len = zipDecodeLength(p+e.prevrawlensize,&e.lensize);
240 e.headersize = e.prevrawlensize+e.lensize;
241 e.encoding = ZIP_ENCODING(p+e.prevrawlensize);
242 e.p = p;
243 return e;
244 }
245
246 /* Return the total number of bytes used by the entry at "p". */
247 static unsigned int zipRawEntryLength(unsigned char *p) {
248 zlentry e = zipEntry(p);
249 return e.headersize + e.len;
250 }
251
252 /* Create a new empty ziplist. */
253 unsigned char *ziplistNew(void) {
254 unsigned int bytes = ZIPLIST_HEADER_SIZE+1;
255 unsigned char *zl = zmalloc(bytes);
256 ZIPLIST_BYTES(zl) = bytes;
257 ZIPLIST_TAIL_OFFSET(zl) = ZIPLIST_HEADER_SIZE;
258 ZIPLIST_LENGTH(zl) = 0;
259 zl[bytes-1] = ZIP_END;
260 return zl;
261 }
262
263 /* Resize the ziplist. */
264 static unsigned char *ziplistResize(unsigned char *zl, unsigned int len) {
265 zl = zrealloc(zl,len);
266 ZIPLIST_BYTES(zl) = len;
267 zl[len-1] = ZIP_END;
268 return zl;
269 }
270
271 /* Delete "num" entries, starting at "p". Returns pointer to the ziplist. */
272 static unsigned char *__ziplistDelete(unsigned char *zl, unsigned char *p, int num) {
273 unsigned int i, totlen, deleted = 0;
274 int nextdiff = 0;
275 zlentry first = zipEntry(p);
276 for (i = 0; p[0] != ZIP_END && i < num; i++) {
277 p += zipRawEntryLength(p);
278 deleted++;
279 }
280
281 totlen = p-first.p;
282 if (totlen > 0) {
283 if (p[0] != ZIP_END) {
284 /* Tricky: storing the prevlen in this entry might reduce or
285 * increase the number of bytes needed, compared to the current
286 * prevlen. Note that we can always store this length because
287 * it was previously stored by an entry that is being deleted. */
288 nextdiff = zipPrevLenByteDiff(p,first.prevrawlen);
289 zipPrevEncodeLength(p-nextdiff,first.prevrawlen);
290
291 /* Update offset for tail */
292 ZIPLIST_TAIL_OFFSET(zl) -= totlen+nextdiff;
293
294 /* Move tail to the front of the ziplist */
295 memmove(first.p,p-nextdiff,ZIPLIST_BYTES(zl)-(p-zl)-1+nextdiff);
296 } else {
297 /* The entire tail was deleted. No need to move memory. */
298 ZIPLIST_TAIL_OFFSET(zl) = (first.p-zl)-first.prevrawlen;
299 }
300
301 /* Resize and update length */
302 zl = ziplistResize(zl, ZIPLIST_BYTES(zl)-totlen+nextdiff);
303 ZIPLIST_INCR_LENGTH(zl,-deleted);
304 }
305 return zl;
306 }
307
308 /* Insert item at "p". */
309 static unsigned char *__ziplistInsert(unsigned char *zl, unsigned char *p, char *s, unsigned int slen) {
310 unsigned int curlen = ZIPLIST_BYTES(zl), reqlen, prevlen = 0;
311 unsigned int offset, nextdiff = 0;
312 unsigned char *tail;
313 char encoding = ZIP_ENC_RAW;
314 long long value;
315 zlentry entry;
316
317 /* Find out prevlen for the entry that is inserted. */
318 if (p[0] != ZIP_END) {
319 entry = zipEntry(p);
320 prevlen = entry.prevrawlen;
321 } else {
322 tail = ZIPLIST_ENTRY_TAIL(zl);
323 if (tail[0] != ZIP_END) {
324 prevlen = zipRawEntryLength(tail);
325 }
326 }
327
328 /* See if the entry can be encoded */
329 if (zipTryEncoding(s,&value,&encoding)) {
330 reqlen = zipEncodingSize(encoding);
331 } else {
332 reqlen = slen;
333 }
334
335 /* We need space for both the length of the previous entry and
336 * the length of the payload. */
337 reqlen += zipPrevEncodeLength(NULL,prevlen);
338 reqlen += zipEncodeLength(NULL,encoding,slen);
339
340 /* When the insert position is not equal to the tail, we need to
341 * make sure that the next entry can hold this entry's length in
342 * its prevlen field. */
343 nextdiff = p[0] != ZIP_END ? zipPrevLenByteDiff(p,reqlen) : 0;
344
345 /* Store offset because a realloc may change the address of zl. */
346 offset = p-zl;
347 zl = ziplistResize(zl,curlen+reqlen+nextdiff);
348 p = zl+offset;
349
350 /* Apply memory move when necessary and update tail offset. */
351 if (p[0] != ZIP_END) {
352 /* Subtract one because of the ZIP_END bytes */
353 memmove(p+reqlen,p-nextdiff,curlen-offset-1+nextdiff);
354 /* Encode this entry's raw length in the next entry. */
355 zipPrevEncodeLength(p+reqlen,reqlen);
356 /* Update offset for tail */
357 ZIPLIST_TAIL_OFFSET(zl) += reqlen+nextdiff;
358 } else {
359 /* This element will be the new tail. */
360 ZIPLIST_TAIL_OFFSET(zl) = p-zl;
361 }
362
363 /* Write the entry */
364 p += zipPrevEncodeLength(p,prevlen);
365 p += zipEncodeLength(p,encoding,slen);
366 if (encoding != ZIP_ENC_RAW) {
367 zipSaveInteger(p,value,encoding);
368 } else {
369 memcpy(p,s,slen);
370 }
371 ZIPLIST_INCR_LENGTH(zl,1);
372 return zl;
373 }
374
375 unsigned char *ziplistPush(unsigned char *zl, char *s, unsigned int slen, int where) {
376 unsigned char *p;
377 p = (where == ZIPLIST_HEAD) ? ZIPLIST_ENTRY_HEAD(zl) : ZIPLIST_ENTRY_END(zl);
378 return __ziplistInsert(zl,p,s,slen);
379 }
380
381 unsigned char *ziplistPop(unsigned char *zl, sds *target, int where) {
382 zlentry entry;
383 unsigned char *p;
384 long long value;
385 if (target) *target = NULL;
386
387 /* Get pointer to element to remove */
388 p = (where == ZIPLIST_HEAD) ? ZIPLIST_ENTRY_HEAD(zl) : ZIPLIST_ENTRY_TAIL(zl);
389 if (*p == ZIP_END) return zl;
390
391 entry = zipEntry(p);
392 if (target) {
393 if (entry.encoding == ZIP_ENC_RAW) {
394 *target = sdsnewlen(p+entry.headersize,entry.len);
395 } else {
396 value = zipLoadInteger(p+entry.headersize,entry.encoding);
397 *target = sdscatprintf(sdsempty(), "%lld", value);
398 }
399 }
400
401 zl = __ziplistDelete(zl,p,1);
402 return zl;
403 }
404
405 /* Returns an offset to use for iterating with ziplistNext. */
406 unsigned char *ziplistIndex(unsigned char *zl, unsigned int index) {
407 unsigned char *p = zl+ZIPLIST_HEADER_SIZE;
408 unsigned int i = 0;
409 for (; i < index; i++) {
410 if (*p == ZIP_END) break;
411 p += zipRawEntryLength(p);
412 }
413 return p;
414 }
415
416 /* Return pointer to next entry in ziplist. */
417 unsigned char *ziplistNext(unsigned char *p) {
418 return *p == ZIP_END ? p : p+zipRawEntryLength(p);
419 }
420
421 /* Get entry pointer to by 'p' and store in either 'e' or 'v' depending
422 * on the encoding of the entry. 'e' is always set to NULL to be able
423 * to find out whether the string pointer or the integer value was set.
424 * Return 0 if 'p' points to the end of the zipmap, 1 otherwise. */
425 unsigned int ziplistGet(unsigned char *p, char **sstr, unsigned int *slen, long long *sval) {
426 zlentry entry;
427 if (*p == ZIP_END) return 0;
428 if (sstr) *sstr = NULL;
429
430 entry = zipEntry(p);
431 if (entry.encoding == ZIP_ENC_RAW) {
432 if (sstr) {
433 *slen = entry.len;
434 *sstr = (char*)p+entry.headersize;
435 }
436 } else {
437 if (sval) {
438 *sval = zipLoadInteger(p+entry.headersize,entry.encoding);
439 }
440 }
441 return 1;
442 }
443
444 /* Delete a range of entries from the ziplist. */
445 unsigned char *ziplistDeleteRange(unsigned char *zl, unsigned int index, unsigned int num) {
446 unsigned char *p = ziplistIndex(zl,index);
447 return __ziplistDelete(zl,p,num);
448 }
449
450 /* Delete a single entry from the ziplist, pointed to by *p.
451 * Also update *p in place, to be able to iterate over the
452 * ziplist, while deleting entries. */
453 unsigned char *ziplistDelete(unsigned char *zl, unsigned char **p) {
454 unsigned int offset = *p-zl;
455 zl = __ziplistDelete(zl,*p,1);
456
457 /* Store pointer to current element in p, because ziplistDelete will
458 * do a realloc which might result in a different "zl"-pointer. */
459 *p = zl+offset;
460 return zl;
461 }
462
463 /* Compare entry pointer to by 'p' with 'entry'. Return 1 if equal. */
464 unsigned int ziplistCompare(unsigned char *p, char *sstr, unsigned int slen) {
465 zlentry entry;
466 char sencoding;
467 long long val, sval;
468 if (*p == ZIP_END) return 0;
469
470 entry = zipEntry(p);
471 if (entry.encoding == ZIP_ENC_RAW) {
472 /* Raw compare */
473 if (entry.len == slen) {
474 return memcmp(p+entry.headersize,sstr,slen) == 0;
475 } else {
476 return 0;
477 }
478 } else {
479 /* Try to compare encoded values */
480 if (zipTryEncoding(sstr,&sval,&sencoding)) {
481 if (entry.encoding == sencoding) {
482 val = zipLoadInteger(p+entry.headersize,entry.encoding);
483 return val == sval;
484 }
485 }
486 }
487 return 0;
488 }
489
490 /* Return length of ziplist. */
491 unsigned int ziplistLen(unsigned char *zl) {
492 unsigned int len = 0;
493 if (ZIPLIST_LENGTH(zl) < ZIP_BIGLEN) {
494 len = ZIPLIST_LENGTH(zl);
495 } else {
496 unsigned char *p = zl+ZIPLIST_HEADER_SIZE;
497 while (*p != ZIP_END) {
498 p += zipRawEntryLength(p);
499 len++;
500 }
501
502 /* Re-store length if small enough */
503 if (len < ZIP_BIGLEN) ZIPLIST_LENGTH(zl) = len;
504 }
505 return len;
506 }
507
508 /* Return size in bytes of ziplist. */
509 unsigned int ziplistSize(unsigned char *zl) {
510 return ZIPLIST_BYTES(zl);
511 }
512
513 void ziplistRepr(unsigned char *zl) {
514 unsigned char *p;
515 zlentry entry;
516
517 printf("{total bytes %d} {length %u}\n",ZIPLIST_BYTES(zl), ZIPLIST_LENGTH(zl));
518 p = ZIPLIST_ENTRY_HEAD(zl);
519 while(*p != ZIP_END) {
520 entry = zipEntry(p);
521 printf("{offset %ld, header %u, payload %u} ",p-zl,entry.headersize,entry.len);
522 p += entry.headersize;
523 if (entry.encoding == ZIP_ENC_RAW) {
524 fwrite(p,entry.len,1,stdout);
525 } else {
526 printf("%lld", zipLoadInteger(p,entry.encoding));
527 }
528 printf("\n");
529 p += entry.len;
530 }
531 printf("{end}\n\n");
532 }
533
534 #ifdef ZIPLIST_TEST_MAIN
535
536 unsigned char *createList() {
537 unsigned char *zl = ziplistNew();
538 zl = ziplistPush(zl, "foo", 3, ZIPLIST_TAIL);
539 zl = ziplistPush(zl, "quux", 4, ZIPLIST_TAIL);
540 zl = ziplistPush(zl, "hello", 5, ZIPLIST_HEAD);
541 zl = ziplistPush(zl, "1024", 4, ZIPLIST_TAIL);
542 return zl;
543 }
544
545 unsigned char *createIntList() {
546 unsigned char *zl = ziplistNew();
547 char buf[32];
548
549 sprintf(buf, "100");
550 zl = ziplistPush(zl, buf, strlen(buf), ZIPLIST_TAIL);
551 sprintf(buf, "128000");
552 zl = ziplistPush(zl, buf, strlen(buf), ZIPLIST_TAIL);
553 sprintf(buf, "-100");
554 zl = ziplistPush(zl, buf, strlen(buf), ZIPLIST_HEAD);
555 sprintf(buf, "4294967296");
556 zl = ziplistPush(zl, buf, strlen(buf), ZIPLIST_HEAD);
557 sprintf(buf, "non integer");
558 zl = ziplistPush(zl, buf, strlen(buf), ZIPLIST_TAIL);
559 sprintf(buf, "much much longer non integer");
560 zl = ziplistPush(zl, buf, strlen(buf), ZIPLIST_TAIL);
561 return zl;
562 }
563
564 int main(int argc, char **argv) {
565 unsigned char *zl, *p;
566 char *entry;
567 unsigned int elen;
568 long long value;
569 sds s;
570
571 zl = createIntList();
572 ziplistRepr(zl);
573
574 zl = createList();
575 ziplistRepr(zl);
576
577 zl = ziplistPop(zl, &s, ZIPLIST_TAIL);
578 printf("Pop tail: %s (length %ld)\n", s, sdslen(s));
579 ziplistRepr(zl);
580
581 zl = ziplistPop(zl, &s, ZIPLIST_HEAD);
582 printf("Pop head: %s (length %ld)\n", s, sdslen(s));
583 ziplistRepr(zl);
584
585 zl = ziplistPop(zl, &s, ZIPLIST_TAIL);
586 printf("Pop tail: %s (length %ld)\n", s, sdslen(s));
587 ziplistRepr(zl);
588
589 zl = ziplistPop(zl, &s, ZIPLIST_TAIL);
590 printf("Pop tail: %s (length %ld)\n", s, sdslen(s));
591 ziplistRepr(zl);
592
593 printf("Iterate list from 0 to end:\n");
594 {
595 zl = createList();
596 p = ziplistIndex(zl, 0);
597 while (ziplistGet(p, &entry, &elen, &value)) {
598 printf("Entry: ");
599 if (entry) {
600 fwrite(entry,elen,1,stdout);
601 } else {
602 printf("%lld", value);
603 }
604 p = ziplistNext(p);
605 printf("\n");
606 }
607 printf("\n");
608 }
609
610 printf("Iterate list from 1 to end:\n");
611 {
612 zl = createList();
613 p = ziplistIndex(zl, 1);
614 while (ziplistGet(p, &entry, &elen, &value)) {
615 printf("Entry: ");
616 if (entry) {
617 fwrite(entry,elen,1,stdout);
618 } else {
619 printf("%lld", value);
620 }
621 p = ziplistNext(p);
622 printf("\n");
623 }
624 printf("\n");
625 }
626
627 printf("Iterate list from 2 to end:\n");
628 {
629 zl = createList();
630 p = ziplistIndex(zl, 2);
631 while (ziplistGet(p, &entry, &elen, &value)) {
632 printf("Entry: ");
633 if (entry) {
634 fwrite(entry,elen,1,stdout);
635 } else {
636 printf("%lld", value);
637 }
638 p = ziplistNext(p);
639 printf("\n");
640 }
641 printf("\n");
642 }
643
644 printf("Iterate starting out of range:\n");
645 {
646 zl = createList();
647 p = ziplistIndex(zl, 4);
648 if (!ziplistGet(p, &entry, &elen, &value)) {
649 printf("No entry\n");
650 } else {
651 printf("ERROR\n");
652 }
653 printf("\n");
654 }
655
656 printf("Delete inclusive range 0,0:\n");
657 {
658 zl = createList();
659 zl = ziplistDeleteRange(zl, 0, 1);
660 ziplistRepr(zl);
661 }
662
663 printf("Delete inclusive range 0,1:\n");
664 {
665 zl = createList();
666 zl = ziplistDeleteRange(zl, 0, 2);
667 ziplistRepr(zl);
668 }
669
670 printf("Delete inclusive range 1,2:\n");
671 {
672 zl = createList();
673 zl = ziplistDeleteRange(zl, 1, 2);
674 ziplistRepr(zl);
675 }
676
677 printf("Delete with start index out of range:\n");
678 {
679 zl = createList();
680 zl = ziplistDeleteRange(zl, 5, 1);
681 ziplistRepr(zl);
682 }
683
684 printf("Delete with num overflow:\n");
685 {
686 zl = createList();
687 zl = ziplistDeleteRange(zl, 1, 5);
688 ziplistRepr(zl);
689 }
690
691 printf("Delete foo while iterating:\n");
692 {
693 zl = createList();
694 p = ziplistIndex(zl, 0);
695 while (ziplistGet(p, &entry, &elen, &value)) {
696 if (entry && strncmp("foo", entry, elen) == 0) {
697 printf("Delete foo\n");
698 zl = ziplistDelete(zl, &p);
699 } else {
700 printf("Entry: ");
701 if (entry) {
702 fwrite(entry,elen,1,stdout);
703 } else {
704 printf("%lld", value);
705 }
706 p = ziplistNext(p);
707 printf("\n");
708 }
709 }
710 printf("\n");
711 ziplistRepr(zl);
712 }
713
714 printf("Compare strings with ziplist entries:\n");
715 {
716 zl = createList();
717 p = ziplistIndex(zl, 0);
718 if (!ziplistCompare(p,"hello",5)) {
719 printf("ERROR: not \"hello\"\n");
720 return 1;
721 }
722 if (ziplistCompare(p,"hella",5)) {
723 printf("ERROR: \"hella\"\n");
724 return 1;
725 }
726
727 p = ziplistIndex(zl, 3);
728 if (!ziplistCompare(p,"1024",4)) {
729 printf("ERROR: not \"1024\"\n");
730 return 1;
731 }
732 if (ziplistCompare(p,"1025",4)) {
733 printf("ERROR: \"1025\"\n");
734 return 1;
735 }
736 printf("SUCCESS\n");
737 }
738
739 return 0;
740 }
741 #endif