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