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