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