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