]> git.saurik.com Git - redis.git/blob - ziplist.c
check if the list encoding needs to be changed on LPUSHX, RPUSHX, LINSERT
[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 <stdint.h>
21 #include <assert.h>
22 #include <limits.h>
23 #include "zmalloc.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_INT16 1
37 #define ZIP_ENC_INT32 2
38 #define ZIP_ENC_INT64 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) (*((uint32_t*)(zl)))
48 #define ZIPLIST_TAIL_OFFSET(zl) (*((uint32_t*)((zl)+sizeof(uint32_t))))
49 #define ZIPLIST_LENGTH(zl) (*((uint16_t*)((zl)+sizeof(uint32_t)*2)))
50 #define ZIPLIST_HEADER_SIZE (sizeof(uint32_t)*2+sizeof(uint16_t))
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
55 /* We know a positive increment can only be 1 because entries can only be
56 * pushed one at a time. */
57 #define ZIPLIST_INCR_LENGTH(zl,incr) { \
58 if (ZIPLIST_LENGTH(zl) < UINT16_MAX) ZIPLIST_LENGTH(zl)+=incr; }
59
60 typedef struct zlentry {
61 unsigned int prevrawlensize, prevrawlen;
62 unsigned int lensize, len;
63 unsigned int headersize;
64 unsigned char encoding;
65 unsigned char *p;
66 } zlentry;
67
68 /* Return bytes needed to store integer encoded by 'encoding' */
69 static unsigned int zipEncodingSize(unsigned char encoding) {
70 if (encoding == ZIP_ENC_INT16) {
71 return sizeof(int16_t);
72 } else if (encoding == ZIP_ENC_INT32) {
73 return sizeof(int32_t);
74 } else if (encoding == ZIP_ENC_INT64) {
75 return sizeof(int64_t);
76 }
77 assert(NULL);
78 }
79
80 /* Decode the encoded length pointed by 'p'. If a pointer to 'lensize' is
81 * provided, it is set to the number of bytes required to encode the length. */
82 static unsigned int zipDecodeLength(unsigned char *p, unsigned int *lensize) {
83 unsigned char encoding = ZIP_ENCODING(p), lenenc;
84 unsigned int len;
85
86 if (encoding == ZIP_ENC_RAW) {
87 lenenc = (p[0] >> 4) & 0x3;
88 if (lenenc == ZIP_LEN_INLINE) {
89 len = p[0] & 0xf;
90 if (lensize) *lensize = 1;
91 } else if (lenenc == ZIP_LEN_UINT16) {
92 len = p[1] | (p[2] << 8);
93 if (lensize) *lensize = 3;
94 } else {
95 len = p[1] | (p[2] << 8) | (p[3] << 16) | (p[4] << 24);
96 if (lensize) *lensize = 5;
97 }
98 } else {
99 len = zipEncodingSize(encoding);
100 if (lensize) *lensize = 1;
101 }
102 return len;
103 }
104
105 /* Encode the length 'l' writing it in 'p'. If p is NULL it just returns
106 * the amount of bytes required to encode such a length. */
107 static unsigned int zipEncodeLength(unsigned char *p, char encoding, unsigned int rawlen) {
108 unsigned char len = 1, lenenc, buf[5];
109 if (encoding == ZIP_ENC_RAW) {
110 if (rawlen <= 0xf) {
111 if (!p) return len;
112 lenenc = ZIP_LEN_INLINE;
113 buf[0] = rawlen;
114 } else if (rawlen <= 0xffff) {
115 len += 2;
116 if (!p) return len;
117 lenenc = ZIP_LEN_UINT16;
118 buf[1] = (rawlen ) & 0xff;
119 buf[2] = (rawlen >> 8) & 0xff;
120 } else {
121 len += 4;
122 if (!p) return len;
123 lenenc = ZIP_LEN_UINT32;
124 buf[1] = (rawlen ) & 0xff;
125 buf[2] = (rawlen >> 8) & 0xff;
126 buf[3] = (rawlen >> 16) & 0xff;
127 buf[4] = (rawlen >> 24) & 0xff;
128 }
129 buf[0] = (lenenc << 4) | (buf[0] & 0xf);
130 }
131 if (!p) return len;
132
133 /* Apparently we need to store the length in 'p' */
134 buf[0] = (encoding << 6) | (buf[0] & 0x3f);
135 memcpy(p,buf,len);
136 return len;
137 }
138
139 /* Decode the length of the previous element stored at "p". */
140 static unsigned int zipPrevDecodeLength(unsigned char *p, unsigned int *lensize) {
141 unsigned int len = *p;
142 if (len < ZIP_BIGLEN) {
143 if (lensize) *lensize = 1;
144 } else {
145 if (lensize) *lensize = 1+sizeof(len);
146 memcpy(&len,p+1,sizeof(len));
147 }
148 return len;
149 }
150
151 /* Encode the length of the previous entry and write it to "p". Return the
152 * number of bytes needed to encode this length if "p" is NULL. */
153 static unsigned int zipPrevEncodeLength(unsigned char *p, unsigned int len) {
154 if (p == NULL) {
155 return (len < ZIP_BIGLEN) ? 1 : sizeof(len)+1;
156 } else {
157 if (len < ZIP_BIGLEN) {
158 p[0] = len;
159 return 1;
160 } else {
161 p[0] = ZIP_BIGLEN;
162 memcpy(p+1,&len,sizeof(len));
163 return 1+sizeof(len);
164 }
165 }
166 }
167
168 /* Return the difference in number of bytes needed to store the new length
169 * "len" on the entry pointed to by "p". */
170 static int zipPrevLenByteDiff(unsigned char *p, unsigned int len) {
171 unsigned int prevlensize;
172 zipPrevDecodeLength(p,&prevlensize);
173 return zipPrevEncodeLength(NULL,len)-prevlensize;
174 }
175
176 /* Check if string pointed to by 'entry' can be encoded as an integer.
177 * Stores the integer value in 'v' and its encoding in 'encoding'.
178 * Warning: this function requires a NULL-terminated string! */
179 static int zipTryEncoding(unsigned char *entry, long long *v, unsigned char *encoding) {
180 long long value;
181 char *eptr;
182
183 if (entry[0] == '-' || (entry[0] >= '0' && entry[0] <= '9')) {
184 value = strtoll((char*)entry,&eptr,10);
185 if (eptr[0] != '\0') return 0;
186 if (value >= INT16_MIN && value <= INT16_MAX) {
187 *encoding = ZIP_ENC_INT16;
188 } else if (value >= INT32_MIN && value <= INT32_MAX) {
189 *encoding = ZIP_ENC_INT32;
190 } else {
191 *encoding = ZIP_ENC_INT64;
192 }
193 *v = value;
194 return 1;
195 }
196 return 0;
197 }
198
199 /* Store integer 'value' at 'p', encoded as 'encoding' */
200 static void zipSaveInteger(unsigned char *p, int64_t value, unsigned char encoding) {
201 int16_t i16;
202 int32_t i32;
203 int64_t i64;
204 if (encoding == ZIP_ENC_INT16) {
205 i16 = value;
206 memcpy(p,&i16,sizeof(i16));
207 } else if (encoding == ZIP_ENC_INT32) {
208 i32 = value;
209 memcpy(p,&i32,sizeof(i32));
210 } else if (encoding == ZIP_ENC_INT64) {
211 i64 = value;
212 memcpy(p,&i64,sizeof(i64));
213 } else {
214 assert(NULL);
215 }
216 }
217
218 /* Read integer encoded as 'encoding' from 'p' */
219 static int64_t zipLoadInteger(unsigned char *p, unsigned char encoding) {
220 int16_t i16;
221 int32_t i32;
222 int64_t i64, ret;
223 if (encoding == ZIP_ENC_INT16) {
224 memcpy(&i16,p,sizeof(i16));
225 ret = i16;
226 } else if (encoding == ZIP_ENC_INT32) {
227 memcpy(&i32,p,sizeof(i32));
228 ret = i32;
229 } else if (encoding == ZIP_ENC_INT64) {
230 memcpy(&i64,p,sizeof(i64));
231 ret = i64;
232 } else {
233 assert(NULL);
234 }
235 return ret;
236 }
237
238 /* Return a struct with all information about an entry. */
239 static zlentry zipEntry(unsigned char *p) {
240 zlentry e;
241 e.prevrawlen = zipPrevDecodeLength(p,&e.prevrawlensize);
242 e.len = zipDecodeLength(p+e.prevrawlensize,&e.lensize);
243 e.headersize = e.prevrawlensize+e.lensize;
244 e.encoding = ZIP_ENCODING(p+e.prevrawlensize);
245 e.p = p;
246 return e;
247 }
248
249 /* Return the total number of bytes used by the entry at "p". */
250 static unsigned int zipRawEntryLength(unsigned char *p) {
251 zlentry e = zipEntry(p);
252 return e.headersize + e.len;
253 }
254
255 /* Create a new empty ziplist. */
256 unsigned char *ziplistNew(void) {
257 unsigned int bytes = ZIPLIST_HEADER_SIZE+1;
258 unsigned char *zl = zmalloc(bytes);
259 ZIPLIST_BYTES(zl) = bytes;
260 ZIPLIST_TAIL_OFFSET(zl) = ZIPLIST_HEADER_SIZE;
261 ZIPLIST_LENGTH(zl) = 0;
262 zl[bytes-1] = ZIP_END;
263 return zl;
264 }
265
266 /* Resize the ziplist. */
267 static unsigned char *ziplistResize(unsigned char *zl, unsigned int len) {
268 zl = zrealloc(zl,len);
269 ZIPLIST_BYTES(zl) = len;
270 zl[len-1] = ZIP_END;
271 return zl;
272 }
273
274 /* Delete "num" entries, starting at "p". Returns pointer to the ziplist. */
275 static unsigned char *__ziplistDelete(unsigned char *zl, unsigned char *p, unsigned int num) {
276 unsigned int i, totlen, deleted = 0;
277 int nextdiff = 0;
278 zlentry first = zipEntry(p);
279 for (i = 0; p[0] != ZIP_END && i < num; i++) {
280 p += zipRawEntryLength(p);
281 deleted++;
282 }
283
284 totlen = p-first.p;
285 if (totlen > 0) {
286 if (p[0] != ZIP_END) {
287 /* Tricky: storing the prevlen in this entry might reduce or
288 * increase the number of bytes needed, compared to the current
289 * prevlen. Note that we can always store this length because
290 * it was previously stored by an entry that is being deleted. */
291 nextdiff = zipPrevLenByteDiff(p,first.prevrawlen);
292 zipPrevEncodeLength(p-nextdiff,first.prevrawlen);
293
294 /* Update offset for tail */
295 ZIPLIST_TAIL_OFFSET(zl) -= totlen+nextdiff;
296
297 /* Move tail to the front of the ziplist */
298 memmove(first.p,p-nextdiff,ZIPLIST_BYTES(zl)-(p-zl)-1+nextdiff);
299 } else {
300 /* The entire tail was deleted. No need to move memory. */
301 ZIPLIST_TAIL_OFFSET(zl) = (first.p-zl)-first.prevrawlen;
302 }
303
304 /* Resize and update length */
305 zl = ziplistResize(zl, ZIPLIST_BYTES(zl)-totlen+nextdiff);
306 ZIPLIST_INCR_LENGTH(zl,-deleted);
307 }
308 return zl;
309 }
310
311 /* Insert item at "p". */
312 static unsigned char *__ziplistInsert(unsigned char *zl, unsigned char *p, unsigned char *s, unsigned int slen) {
313 unsigned int curlen = ZIPLIST_BYTES(zl), reqlen, prevlen = 0;
314 unsigned int offset, nextdiff = 0;
315 unsigned char *tail;
316 unsigned char encoding = ZIP_ENC_RAW;
317 long long value;
318 zlentry entry;
319
320 /* Find out prevlen for the entry that is inserted. */
321 if (p[0] != ZIP_END) {
322 entry = zipEntry(p);
323 prevlen = entry.prevrawlen;
324 } else {
325 tail = ZIPLIST_ENTRY_TAIL(zl);
326 if (tail[0] != ZIP_END) {
327 prevlen = zipRawEntryLength(tail);
328 }
329 }
330
331 /* See if the entry can be encoded */
332 if (zipTryEncoding(s,&value,&encoding)) {
333 reqlen = zipEncodingSize(encoding);
334 } else {
335 reqlen = slen;
336 }
337
338 /* We need space for both the length of the previous entry and
339 * the length of the payload. */
340 reqlen += zipPrevEncodeLength(NULL,prevlen);
341 reqlen += zipEncodeLength(NULL,encoding,slen);
342
343 /* When the insert position is not equal to the tail, we need to
344 * make sure that the next entry can hold this entry's length in
345 * its prevlen field. */
346 nextdiff = (p[0] != ZIP_END) ? zipPrevLenByteDiff(p,reqlen) : 0;
347
348 /* Store offset because a realloc may change the address of zl. */
349 offset = p-zl;
350 zl = ziplistResize(zl,curlen+reqlen+nextdiff);
351 p = zl+offset;
352
353 /* Apply memory move when necessary and update tail offset. */
354 if (p[0] != ZIP_END) {
355 /* Subtract one because of the ZIP_END bytes */
356 memmove(p+reqlen,p-nextdiff,curlen-offset-1+nextdiff);
357 /* Encode this entry's raw length in the next entry. */
358 zipPrevEncodeLength(p+reqlen,reqlen);
359 /* Update offset for tail */
360 ZIPLIST_TAIL_OFFSET(zl) += reqlen+nextdiff;
361 } else {
362 /* This element will be the new tail. */
363 ZIPLIST_TAIL_OFFSET(zl) = p-zl;
364 }
365
366 /* Write the entry */
367 p += zipPrevEncodeLength(p,prevlen);
368 p += zipEncodeLength(p,encoding,slen);
369 if (encoding != ZIP_ENC_RAW) {
370 zipSaveInteger(p,value,encoding);
371 } else {
372 memcpy(p,s,slen);
373 }
374 ZIPLIST_INCR_LENGTH(zl,1);
375 return zl;
376 }
377
378 unsigned char *ziplistPush(unsigned char *zl, unsigned char *s, unsigned int slen, int where) {
379 unsigned char *p;
380 p = (where == ZIPLIST_HEAD) ? ZIPLIST_ENTRY_HEAD(zl) : ZIPLIST_ENTRY_END(zl);
381 return __ziplistInsert(zl,p,s,slen);
382 }
383
384 /* Returns an offset to use for iterating with ziplistNext. When the given
385 * index is negative, the list is traversed back to front. When the list
386 * doesn't contain an element at the provided index, NULL is returned. */
387 unsigned char *ziplistIndex(unsigned char *zl, int index) {
388 unsigned char *p;
389 zlentry entry;
390 if (index < 0) {
391 index = (-index)-1;
392 p = ZIPLIST_ENTRY_TAIL(zl);
393 if (p[0] != ZIP_END) {
394 entry = zipEntry(p);
395 while (entry.prevrawlen > 0 && index--) {
396 p -= entry.prevrawlen;
397 entry = zipEntry(p);
398 }
399 }
400 } else {
401 p = ZIPLIST_ENTRY_HEAD(zl);
402 while (p[0] != ZIP_END && index--) {
403 p += zipRawEntryLength(p);
404 }
405 }
406 return (p[0] == ZIP_END || index > 0) ? NULL : p;
407 }
408
409 /* Return pointer to next entry in ziplist. */
410 unsigned char *ziplistNext(unsigned char *zl, unsigned char *p) {
411 ((void) zl);
412
413 /* "p" could be equal to ZIP_END, caused by ziplistDelete,
414 * and we should return NULL. Otherwise, we should return NULL
415 * when the *next* element is ZIP_END (there is no next entry). */
416 if (p[0] == ZIP_END) {
417 return NULL;
418 } else {
419 p = p+zipRawEntryLength(p);
420 return (p[0] == ZIP_END) ? NULL : p;
421 }
422 }
423
424 /* Return pointer to previous entry in ziplist. */
425 unsigned char *ziplistPrev(unsigned char *zl, unsigned char *p) {
426 zlentry entry;
427
428 /* Iterating backwards from ZIP_END should return the tail. When "p" is
429 * equal to the first element of the list, we're already at the head,
430 * and should return NULL. */
431 if (p[0] == ZIP_END) {
432 p = ZIPLIST_ENTRY_TAIL(zl);
433 return (p[0] == ZIP_END) ? NULL : p;
434 } else if (p == ZIPLIST_ENTRY_HEAD(zl)) {
435 return NULL;
436 } else {
437 entry = zipEntry(p);
438 return p-entry.prevrawlen;
439 }
440 }
441
442 /* Get entry pointer to by 'p' and store in either 'e' or 'v' depending
443 * on the encoding of the entry. 'e' is always set to NULL to be able
444 * to find out whether the string pointer or the integer value was set.
445 * Return 0 if 'p' points to the end of the zipmap, 1 otherwise. */
446 unsigned int ziplistGet(unsigned char *p, unsigned char **sstr, unsigned int *slen, long long *sval) {
447 zlentry entry;
448 if (p == NULL || p[0] == ZIP_END) return 0;
449 if (sstr) *sstr = NULL;
450
451 entry = zipEntry(p);
452 if (entry.encoding == ZIP_ENC_RAW) {
453 if (sstr) {
454 *slen = entry.len;
455 *sstr = p+entry.headersize;
456 }
457 } else {
458 if (sval) {
459 *sval = zipLoadInteger(p+entry.headersize,entry.encoding);
460 }
461 }
462 return 1;
463 }
464
465 /* Insert an entry at "p". */
466 unsigned char *ziplistInsert(unsigned char *zl, unsigned char *p, unsigned char *s, unsigned int slen) {
467 return __ziplistInsert(zl,p,s,slen);
468 }
469
470 /* Delete a single entry from the ziplist, pointed to by *p.
471 * Also update *p in place, to be able to iterate over the
472 * ziplist, while deleting entries. */
473 unsigned char *ziplistDelete(unsigned char *zl, unsigned char **p) {
474 unsigned int offset = *p-zl;
475 zl = __ziplistDelete(zl,*p,1);
476
477 /* Store pointer to current element in p, because ziplistDelete will
478 * do a realloc which might result in a different "zl"-pointer.
479 * When the delete direction is back to front, we might delete the last
480 * entry and end up with "p" pointing to ZIP_END, so check this. */
481 *p = zl+offset;
482 return zl;
483 }
484
485 /* Delete a range of entries from the ziplist. */
486 unsigned char *ziplistDeleteRange(unsigned char *zl, unsigned int index, unsigned int num) {
487 unsigned char *p = ziplistIndex(zl,index);
488 return (p == NULL) ? zl : __ziplistDelete(zl,p,num);
489 }
490
491 /* Compare entry pointer to by 'p' with 'entry'. Return 1 if equal. */
492 unsigned int ziplistCompare(unsigned char *p, unsigned char *sstr, unsigned int slen) {
493 zlentry entry;
494 unsigned char sencoding;
495 long long zval, sval;
496 if (p[0] == ZIP_END) return 0;
497
498 entry = zipEntry(p);
499 if (entry.encoding == ZIP_ENC_RAW) {
500 /* Raw compare */
501 if (entry.len == slen) {
502 return memcmp(p+entry.headersize,sstr,slen) == 0;
503 } else {
504 return 0;
505 }
506 } else {
507 /* Try to compare encoded values */
508 if (zipTryEncoding(sstr,&sval,&sencoding)) {
509 if (entry.encoding == sencoding) {
510 zval = zipLoadInteger(p+entry.headersize,entry.encoding);
511 return zval == sval;
512 }
513 }
514 }
515 return 0;
516 }
517
518 /* Return length of ziplist. */
519 unsigned int ziplistLen(unsigned char *zl) {
520 unsigned int len = 0;
521 if (ZIPLIST_LENGTH(zl) < UINT16_MAX) {
522 len = ZIPLIST_LENGTH(zl);
523 } else {
524 unsigned char *p = zl+ZIPLIST_HEADER_SIZE;
525 while (*p != ZIP_END) {
526 p += zipRawEntryLength(p);
527 len++;
528 }
529
530 /* Re-store length if small enough */
531 if (len < UINT16_MAX) ZIPLIST_LENGTH(zl) = len;
532 }
533 return len;
534 }
535
536 /* Return size in bytes of ziplist. */
537 unsigned int ziplistSize(unsigned char *zl) {
538 return ZIPLIST_BYTES(zl);
539 }
540
541 void ziplistRepr(unsigned char *zl) {
542 unsigned char *p;
543 zlentry entry;
544
545 printf("{total bytes %d} {length %u}\n",ZIPLIST_BYTES(zl), ZIPLIST_LENGTH(zl));
546 p = ZIPLIST_ENTRY_HEAD(zl);
547 while(*p != ZIP_END) {
548 entry = zipEntry(p);
549 printf("{offset %ld, header %u, payload %u} ",p-zl,entry.headersize,entry.len);
550 p += entry.headersize;
551 if (entry.encoding == ZIP_ENC_RAW) {
552 fwrite(p,entry.len,1,stdout);
553 } else {
554 printf("%lld", zipLoadInteger(p,entry.encoding));
555 }
556 printf("\n");
557 p += entry.len;
558 }
559 printf("{end}\n\n");
560 }
561
562 #ifdef ZIPLIST_TEST_MAIN
563 #include <sys/time.h>
564
565 unsigned char *createList() {
566 unsigned char *zl = ziplistNew();
567 zl = ziplistPush(zl, (unsigned char*)"foo", 3, ZIPLIST_TAIL);
568 zl = ziplistPush(zl, (unsigned char*)"quux", 4, ZIPLIST_TAIL);
569 zl = ziplistPush(zl, (unsigned char*)"hello", 5, ZIPLIST_HEAD);
570 zl = ziplistPush(zl, (unsigned char*)"1024", 4, ZIPLIST_TAIL);
571 return zl;
572 }
573
574 unsigned char *createIntList() {
575 unsigned char *zl = ziplistNew();
576 char buf[32];
577
578 sprintf(buf, "100");
579 zl = ziplistPush(zl, (unsigned char*)buf, strlen(buf), ZIPLIST_TAIL);
580 sprintf(buf, "128000");
581 zl = ziplistPush(zl, (unsigned char*)buf, strlen(buf), ZIPLIST_TAIL);
582 sprintf(buf, "-100");
583 zl = ziplistPush(zl, (unsigned char*)buf, strlen(buf), ZIPLIST_HEAD);
584 sprintf(buf, "4294967296");
585 zl = ziplistPush(zl, (unsigned char*)buf, strlen(buf), ZIPLIST_HEAD);
586 sprintf(buf, "non integer");
587 zl = ziplistPush(zl, (unsigned char*)buf, strlen(buf), ZIPLIST_TAIL);
588 sprintf(buf, "much much longer non integer");
589 zl = ziplistPush(zl, (unsigned char*)buf, strlen(buf), ZIPLIST_TAIL);
590 return zl;
591 }
592
593 long long usec(void) {
594 struct timeval tv;
595 gettimeofday(&tv,NULL);
596 return (((long long)tv.tv_sec)*1000000)+tv.tv_usec;
597 }
598
599 void stress(int pos, int num, int maxsize, int dnum) {
600 int i,j,k;
601 unsigned char *zl;
602 char posstr[2][5] = { "HEAD", "TAIL" };
603 long long start;
604 for (i = 0; i < maxsize; i+=dnum) {
605 zl = ziplistNew();
606 for (j = 0; j < i; j++) {
607 zl = ziplistPush(zl,(unsigned char*)"quux",4,ZIPLIST_TAIL);
608 }
609
610 /* Do num times a push+pop from pos */
611 start = usec();
612 for (k = 0; k < num; k++) {
613 zl = ziplistPush(zl,(unsigned char*)"quux",4,pos);
614 zl = ziplistDeleteRange(zl,0,1);
615 }
616 printf("List size: %8d, bytes: %8d, %dx push+pop (%s): %6lld usec\n",
617 i,ZIPLIST_BYTES(zl),num,posstr[pos],usec()-start);
618 zfree(zl);
619 }
620 }
621
622 void pop(unsigned char *zl, int where) {
623 unsigned char *p, *vstr;
624 unsigned int vlen;
625 long long vlong;
626
627 p = ziplistIndex(zl,where == ZIPLIST_HEAD ? 0 : -1);
628 if (ziplistGet(p,&vstr,&vlen,&vlong)) {
629 if (where == ZIPLIST_HEAD)
630 printf("Pop head: ");
631 else
632 printf("Pop tail: ");
633
634 if (vstr)
635 fwrite(vstr,vlen,1,stdout);
636 else
637 printf("%lld", vlong);
638
639 printf("\n");
640 ziplistDeleteRange(zl,-1,1);
641 } else {
642 printf("ERROR: Could not pop\n");
643 exit(1);
644 }
645 }
646
647 int main(int argc, char **argv) {
648 unsigned char *zl, *p;
649 unsigned char *entry;
650 unsigned int elen;
651 long long value;
652
653 zl = createIntList();
654 ziplistRepr(zl);
655
656 zl = createList();
657 ziplistRepr(zl);
658
659 pop(zl,ZIPLIST_TAIL);
660 ziplistRepr(zl);
661
662 pop(zl,ZIPLIST_HEAD);
663 ziplistRepr(zl);
664
665 pop(zl,ZIPLIST_TAIL);
666 ziplistRepr(zl);
667
668 pop(zl,ZIPLIST_TAIL);
669 ziplistRepr(zl);
670
671 printf("Get element at index 3:\n");
672 {
673 zl = createList();
674 p = ziplistIndex(zl, 3);
675 if (!ziplistGet(p, &entry, &elen, &value)) {
676 printf("ERROR: Could not access index 3\n");
677 return 1;
678 }
679 if (entry) {
680 fwrite(entry,elen,1,stdout);
681 printf("\n");
682 } else {
683 printf("%lld\n", value);
684 }
685 printf("\n");
686 }
687
688 printf("Get element at index 4 (out of range):\n");
689 {
690 zl = createList();
691 p = ziplistIndex(zl, 4);
692 if (p == NULL) {
693 printf("No entry\n");
694 } else {
695 printf("ERROR: Out of range index should return NULL, returned offset: %ld\n", p-zl);
696 return 1;
697 }
698 printf("\n");
699 }
700
701 printf("Get element at index -1 (last element):\n");
702 {
703 zl = createList();
704 p = ziplistIndex(zl, -1);
705 if (!ziplistGet(p, &entry, &elen, &value)) {
706 printf("ERROR: Could not access index -1\n");
707 return 1;
708 }
709 if (entry) {
710 fwrite(entry,elen,1,stdout);
711 printf("\n");
712 } else {
713 printf("%lld\n", value);
714 }
715 printf("\n");
716 }
717
718 printf("Get element at index -4 (first element):\n");
719 {
720 zl = createList();
721 p = ziplistIndex(zl, -4);
722 if (!ziplistGet(p, &entry, &elen, &value)) {
723 printf("ERROR: Could not access index -4\n");
724 return 1;
725 }
726 if (entry) {
727 fwrite(entry,elen,1,stdout);
728 printf("\n");
729 } else {
730 printf("%lld\n", value);
731 }
732 printf("\n");
733 }
734
735 printf("Get element at index -5 (reverse out of range):\n");
736 {
737 zl = createList();
738 p = ziplistIndex(zl, -5);
739 if (p == NULL) {
740 printf("No entry\n");
741 } else {
742 printf("ERROR: Out of range index should return NULL, returned offset: %ld\n", p-zl);
743 return 1;
744 }
745 printf("\n");
746 }
747
748 printf("Iterate list from 0 to end:\n");
749 {
750 zl = createList();
751 p = ziplistIndex(zl, 0);
752 while (ziplistGet(p, &entry, &elen, &value)) {
753 printf("Entry: ");
754 if (entry) {
755 fwrite(entry,elen,1,stdout);
756 } else {
757 printf("%lld", value);
758 }
759 p = ziplistNext(zl,p);
760 printf("\n");
761 }
762 printf("\n");
763 }
764
765 printf("Iterate list from 1 to end:\n");
766 {
767 zl = createList();
768 p = ziplistIndex(zl, 1);
769 while (ziplistGet(p, &entry, &elen, &value)) {
770 printf("Entry: ");
771 if (entry) {
772 fwrite(entry,elen,1,stdout);
773 } else {
774 printf("%lld", value);
775 }
776 p = ziplistNext(zl,p);
777 printf("\n");
778 }
779 printf("\n");
780 }
781
782 printf("Iterate list from 2 to end:\n");
783 {
784 zl = createList();
785 p = ziplistIndex(zl, 2);
786 while (ziplistGet(p, &entry, &elen, &value)) {
787 printf("Entry: ");
788 if (entry) {
789 fwrite(entry,elen,1,stdout);
790 } else {
791 printf("%lld", value);
792 }
793 p = ziplistNext(zl,p);
794 printf("\n");
795 }
796 printf("\n");
797 }
798
799 printf("Iterate starting out of range:\n");
800 {
801 zl = createList();
802 p = ziplistIndex(zl, 4);
803 if (!ziplistGet(p, &entry, &elen, &value)) {
804 printf("No entry\n");
805 } else {
806 printf("ERROR\n");
807 }
808 printf("\n");
809 }
810
811 printf("Iterate from back to front:\n");
812 {
813 zl = createList();
814 p = ziplistIndex(zl, -1);
815 while (ziplistGet(p, &entry, &elen, &value)) {
816 printf("Entry: ");
817 if (entry) {
818 fwrite(entry,elen,1,stdout);
819 } else {
820 printf("%lld", value);
821 }
822 p = ziplistPrev(zl,p);
823 printf("\n");
824 }
825 printf("\n");
826 }
827
828 printf("Iterate from back to front, deleting all items:\n");
829 {
830 zl = createList();
831 p = ziplistIndex(zl, -1);
832 while (ziplistGet(p, &entry, &elen, &value)) {
833 printf("Entry: ");
834 if (entry) {
835 fwrite(entry,elen,1,stdout);
836 } else {
837 printf("%lld", value);
838 }
839 zl = ziplistDelete(zl,&p);
840 p = ziplistPrev(zl,p);
841 printf("\n");
842 }
843 printf("\n");
844 }
845
846 printf("Delete inclusive range 0,0:\n");
847 {
848 zl = createList();
849 zl = ziplistDeleteRange(zl, 0, 1);
850 ziplistRepr(zl);
851 }
852
853 printf("Delete inclusive range 0,1:\n");
854 {
855 zl = createList();
856 zl = ziplistDeleteRange(zl, 0, 2);
857 ziplistRepr(zl);
858 }
859
860 printf("Delete inclusive range 1,2:\n");
861 {
862 zl = createList();
863 zl = ziplistDeleteRange(zl, 1, 2);
864 ziplistRepr(zl);
865 }
866
867 printf("Delete with start index out of range:\n");
868 {
869 zl = createList();
870 zl = ziplistDeleteRange(zl, 5, 1);
871 ziplistRepr(zl);
872 }
873
874 printf("Delete with num overflow:\n");
875 {
876 zl = createList();
877 zl = ziplistDeleteRange(zl, 1, 5);
878 ziplistRepr(zl);
879 }
880
881 printf("Delete foo while iterating:\n");
882 {
883 zl = createList();
884 p = ziplistIndex(zl,0);
885 while (ziplistGet(p,&entry,&elen,&value)) {
886 if (entry && strncmp("foo",(char*)entry,elen) == 0) {
887 printf("Delete foo\n");
888 zl = ziplistDelete(zl,&p);
889 } else {
890 printf("Entry: ");
891 if (entry) {
892 fwrite(entry,elen,1,stdout);
893 } else {
894 printf("%lld",value);
895 }
896 p = ziplistNext(zl,p);
897 printf("\n");
898 }
899 }
900 printf("\n");
901 ziplistRepr(zl);
902 }
903
904 printf("Create long list and check indices:\n");
905 {
906 zl = ziplistNew();
907 char buf[32];
908 int i,len;
909 for (i = 0; i < 1000; i++) {
910 len = sprintf(buf,"%d",i);
911 zl = ziplistPush(zl,(unsigned char*)buf,len,ZIPLIST_TAIL);
912 }
913 for (i = 0; i < 1000; i++) {
914 p = ziplistIndex(zl,i);
915 assert(ziplistGet(p,NULL,NULL,&value));
916 assert(i == value);
917
918 p = ziplistIndex(zl,-i-1);
919 assert(ziplistGet(p,NULL,NULL,&value));
920 assert(999-i == value);
921 }
922 printf("SUCCESS\n\n");
923 }
924
925 printf("Compare strings with ziplist entries:\n");
926 {
927 zl = createList();
928 p = ziplistIndex(zl,0);
929 if (!ziplistCompare(p,(unsigned char*)"hello",5)) {
930 printf("ERROR: not \"hello\"\n");
931 return 1;
932 }
933 if (ziplistCompare(p,(unsigned char*)"hella",5)) {
934 printf("ERROR: \"hella\"\n");
935 return 1;
936 }
937
938 p = ziplistIndex(zl,3);
939 if (!ziplistCompare(p,(unsigned char*)"1024",4)) {
940 printf("ERROR: not \"1024\"\n");
941 return 1;
942 }
943 if (ziplistCompare(p,(unsigned char*)"1025",4)) {
944 printf("ERROR: \"1025\"\n");
945 return 1;
946 }
947 printf("SUCCESS\n");
948 }
949
950 printf("Stress with variable ziplist size:\n");
951 {
952 stress(ZIPLIST_HEAD,100000,16384,256);
953 stress(ZIPLIST_TAIL,100000,16384,256);
954 }
955
956 return 0;
957 }
958
959 #endif