]> git.saurik.com Git - redis.git/blame - ziplist.c
modify compare function to check if the encoding is equal before comparing
[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. */
387unsigned int ziplistGet(unsigned char *p, unsigned char **e, unsigned int *elen, long long *v) {
a5456b2c 388 zlentry entry;
75d8978e
PN
389 if (*p == ZIP_END) return 0;
390 if (e) *e = NULL;
dcb9cf4e 391
a5456b2c
PN
392 entry = zipEntry(p);
393 if (entry.encoding == ZIP_ENC_RAW) {
75d8978e 394 if (e) {
a5456b2c
PN
395 *elen = entry.len;
396 *e = p+entry.headersize;
75d8978e
PN
397 }
398 } else {
399 if (v) {
a5456b2c 400 *v = 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. */
a5456b2c
PN
449unsigned int ziplistCompare(unsigned char *p, unsigned char *s, unsigned int slen) {
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
PN
458 if (entry.len == slen) {
459 return memcmp(p+entry.headersize,s,slen) == 0;
c09c2c3b
PN
460 } else {
461 return 0;
462 }
c4aace90 463 } else {
d593c488 464 /* Try to compare encoded values */
a5456b2c 465 if (zipTryEncoding(s,&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) {
29b14d5f 499 unsigned char *p, encoding;
dcb9cf4e 500 unsigned int prevrawlensize, prevrawlen, lensize, len;
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) {
dcb9cf4e
PN
505 prevrawlen = zipDecodeLength(p,&prevrawlensize);
506 len = zipDecodeLength(p+prevrawlensize,&lensize);
507 printf("{offset %ld, header %u, payload %u} ",p-zl,prevrawlensize+lensize,len);
508 encoding = ZIP_ENCODING(p+prevrawlensize);
509 p += prevrawlensize+lensize;
29b14d5f 510 if (encoding == ZIP_ENC_RAW) {
dcb9cf4e 511 fwrite(p,len,1,stdout);
29b14d5f
PN
512 } else {
513 printf("%lld", zipLoadInteger(p,encoding));
514 }
11ac6ff6 515 printf("\n");
dcb9cf4e 516 p += len;
11ac6ff6
PN
517 }
518 printf("{end}\n\n");
519}
520
521#ifdef ZIPLIST_TEST_MAIN
11ac6ff6 522
08253bf4
PN
523unsigned char *createList() {
524 unsigned char *zl = ziplistNew();
11ac6ff6 525 zl = ziplistPush(zl, (unsigned char*)"foo", 3, ZIPLIST_TAIL);
11ac6ff6 526 zl = ziplistPush(zl, (unsigned char*)"quux", 4, ZIPLIST_TAIL);
11ac6ff6 527 zl = ziplistPush(zl, (unsigned char*)"hello", 5, ZIPLIST_HEAD);
75d8978e 528 zl = ziplistPush(zl, (unsigned char*)"1024", 4, ZIPLIST_TAIL);
08253bf4
PN
529 return zl;
530}
531
29b14d5f
PN
532unsigned char *createIntList() {
533 unsigned char *zl = ziplistNew();
534 char buf[32];
535
536 sprintf(buf, "100");
537 zl = ziplistPush(zl, buf, strlen(buf), ZIPLIST_TAIL);
538 sprintf(buf, "128000");
539 zl = ziplistPush(zl, buf, strlen(buf), ZIPLIST_TAIL);
540 sprintf(buf, "-100");
541 zl = ziplistPush(zl, buf, strlen(buf), ZIPLIST_HEAD);
542 sprintf(buf, "4294967296");
543 zl = ziplistPush(zl, buf, strlen(buf), ZIPLIST_HEAD);
544 sprintf(buf, "non integer");
545 zl = ziplistPush(zl, buf, strlen(buf), ZIPLIST_TAIL);
546 sprintf(buf, "much much longer non integer");
547 zl = ziplistPush(zl, buf, strlen(buf), ZIPLIST_TAIL);
548 return zl;
549}
550
08253bf4 551int main(int argc, char **argv) {
0f10458c 552 unsigned char *zl, *p, *q, *entry;
335d16bc 553 unsigned int elen;
75d8978e 554 long long value;
08253bf4
PN
555 sds s;
556
29b14d5f
PN
557 zl = createIntList();
558 ziplistRepr(zl);
559
08253bf4 560 zl = createList();
11ac6ff6
PN
561 ziplistRepr(zl);
562
563 zl = ziplistPop(zl, &s, ZIPLIST_TAIL);
564 printf("Pop tail: %s (length %ld)\n", s, sdslen(s));
565 ziplistRepr(zl);
566
567 zl = ziplistPop(zl, &s, ZIPLIST_HEAD);
568 printf("Pop head: %s (length %ld)\n", s, sdslen(s));
569 ziplistRepr(zl);
570
dcb9cf4e
PN
571 zl = ziplistPop(zl, &s, ZIPLIST_TAIL);
572 printf("Pop tail: %s (length %ld)\n", s, sdslen(s));
573 ziplistRepr(zl);
574
575 zl = ziplistPop(zl, &s, ZIPLIST_TAIL);
576 printf("Pop tail: %s (length %ld)\n", s, sdslen(s));
577 ziplistRepr(zl);
578
08253bf4
PN
579 printf("Iterate list from 0 to end:\n");
580 {
581 zl = createList();
582 p = ziplistIndex(zl, 0);
75d8978e 583 while (ziplistGet(p, &entry, &elen, &value)) {
335d16bc 584 printf("Entry: ");
75d8978e
PN
585 if (entry) {
586 fwrite(entry,elen,1,stdout);
587 } else {
588 printf("%lld", value);
589 }
590 p = ziplistNext(p);
591 printf("\n");
08253bf4
PN
592 }
593 printf("\n");
594 }
595
596 printf("Iterate list from 1 to end:\n");
597 {
598 zl = createList();
599 p = ziplistIndex(zl, 1);
75d8978e 600 while (ziplistGet(p, &entry, &elen, &value)) {
335d16bc 601 printf("Entry: ");
75d8978e
PN
602 if (entry) {
603 fwrite(entry,elen,1,stdout);
604 } else {
605 printf("%lld", value);
606 }
607 p = ziplistNext(p);
608 printf("\n");
08253bf4
PN
609 }
610 printf("\n");
611 }
612
613 printf("Iterate list from 2 to end:\n");
614 {
615 zl = createList();
616 p = ziplistIndex(zl, 2);
75d8978e 617 while (ziplistGet(p, &entry, &elen, &value)) {
335d16bc 618 printf("Entry: ");
75d8978e
PN
619 if (entry) {
620 fwrite(entry,elen,1,stdout);
621 } else {
622 printf("%lld", value);
623 }
624 p = ziplistNext(p);
625 printf("\n");
08253bf4
PN
626 }
627 printf("\n");
628 }
629
630 printf("Iterate starting out of range:\n");
631 {
632 zl = createList();
75d8978e
PN
633 p = ziplistIndex(zl, 4);
634 if (!ziplistGet(p, &entry, &elen, &value)) {
08253bf4
PN
635 printf("No entry\n");
636 } else {
637 printf("ERROR\n");
638 }
779deb60
PN
639 printf("\n");
640 }
641
642 printf("Delete inclusive range 0,0:\n");
643 {
644 zl = createList();
ba5b4bde 645 zl = ziplistDeleteRange(zl, 0, 1);
779deb60
PN
646 ziplistRepr(zl);
647 }
648
649 printf("Delete inclusive range 0,1:\n");
650 {
651 zl = createList();
ba5b4bde 652 zl = ziplistDeleteRange(zl, 0, 2);
779deb60
PN
653 ziplistRepr(zl);
654 }
655
656 printf("Delete inclusive range 1,2:\n");
657 {
658 zl = createList();
ba5b4bde 659 zl = ziplistDeleteRange(zl, 1, 2);
779deb60
PN
660 ziplistRepr(zl);
661 }
662
663 printf("Delete with start index out of range:\n");
664 {
665 zl = createList();
ba5b4bde 666 zl = ziplistDeleteRange(zl, 5, 1);
779deb60
PN
667 ziplistRepr(zl);
668 }
669
670 printf("Delete with num overflow:\n");
671 {
672 zl = createList();
ba5b4bde 673 zl = ziplistDeleteRange(zl, 1, 5);
779deb60 674 ziplistRepr(zl);
08253bf4
PN
675 }
676
0f10458c
PN
677 printf("Delete foo while iterating:\n");
678 {
679 zl = createList();
680 p = ziplistIndex(zl, 0);
75d8978e
PN
681 while (ziplistGet(p, &entry, &elen, &value)) {
682 if (entry && strncmp("foo", entry, elen) == 0) {
0f10458c 683 printf("Delete foo\n");
75d8978e 684 zl = ziplistDelete(zl, &p);
0f10458c
PN
685 } else {
686 printf("Entry: ");
75d8978e
PN
687 if (entry) {
688 fwrite(entry,elen,1,stdout);
689 } else {
690 printf("%lld", value);
691 }
692 p = ziplistNext(p);
693 printf("\n");
0f10458c
PN
694 }
695 }
696 printf("\n");
697 ziplistRepr(zl);
c09c2c3b
PN
698 }
699
700 printf("Compare strings with ziplist entries:\n");
701 {
702 zl = createList();
703 p = ziplistIndex(zl, 0);
704 if (!ziplistCompare(p,"hello",5)) {
dcb9cf4e 705 printf("ERROR: not \"hello\"\n");
c09c2c3b
PN
706 return;
707 }
708 if (ziplistCompare(p,"hella",5)) {
dcb9cf4e 709 printf("ERROR: \"hella\"\n");
c09c2c3b
PN
710 return;
711 }
712
713 p = ziplistIndex(zl, 3);
714 if (!ziplistCompare(p,"1024",4)) {
dcb9cf4e 715 printf("ERROR: not \"1024\"\n");
c09c2c3b
PN
716 return;
717 }
718 if (ziplistCompare(p,"1025",4)) {
dcb9cf4e 719 printf("ERROR: \"1025\"\n");
c09c2c3b
PN
720 return;
721 }
722 printf("SUCCESS\n");
0f10458c
PN
723 }
724
11ac6ff6
PN
725 return 0;
726}
727#endif