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