]> git.saurik.com Git - redis.git/blame - ziplist.c
check if the list encoding needs to be changed on LPUSHX, RPUSHX, LINSERT
[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 23#include "zmalloc.h"
11ac6ff6 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
e1f93d4b
PN
36#define ZIP_ENC_INT16 1
37#define ZIP_ENC_INT32 2
38#define ZIP_ENC_INT64 3
37fff074
PN
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 */
e1f93d4b
PN
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. */
f6eb1747 57#define ZIPLIST_INCR_LENGTH(zl,incr) { \
e1f93d4b 58 if (ZIPLIST_LENGTH(zl) < UINT16_MAX) ZIPLIST_LENGTH(zl)+=incr; }
11ac6ff6 59
a5456b2c
PN
60typedef struct zlentry {
61 unsigned int prevrawlensize, prevrawlen;
62 unsigned int lensize, len;
63 unsigned int headersize;
64 unsigned char encoding;
0c0d0564 65 unsigned char *p;
a5456b2c
PN
66} zlentry;
67
37fff074 68/* Return bytes needed to store integer encoded by 'encoding' */
b6eb9703 69static unsigned int zipEncodingSize(unsigned char encoding) {
e1f93d4b
PN
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);
37fff074
PN
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. */
82static 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. */
107static 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
7b1f85c0
PN
139/* Decode the length of the previous element stored at "p". */
140static 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. */
153static 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
dcb9cf4e
PN
168/* Return the difference in number of bytes needed to store the new length
169 * "len" on the entry pointed to by "p". */
170static int zipPrevLenByteDiff(unsigned char *p, unsigned int len) {
171 unsigned int prevlensize;
7b1f85c0
PN
172 zipPrevDecodeLength(p,&prevlensize);
173 return zipPrevEncodeLength(NULL,len)-prevlensize;
dcb9cf4e
PN
174}
175
37fff074
PN
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! */
b6eb9703 179static int zipTryEncoding(unsigned char *entry, long long *v, unsigned char *encoding) {
37fff074
PN
180 long long value;
181 char *eptr;
182
183 if (entry[0] == '-' || (entry[0] >= '0' && entry[0] <= '9')) {
b6eb9703 184 value = strtoll((char*)entry,&eptr,10);
37fff074 185 if (eptr[0] != '\0') return 0;
e1f93d4b
PN
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;
37fff074 190 } else {
e1f93d4b 191 *encoding = ZIP_ENC_INT64;
37fff074
PN
192 }
193 *v = value;
194 return 1;
195 }
196 return 0;
197}
198
199/* Store integer 'value' at 'p', encoded as 'encoding' */
e1f93d4b
PN
200static 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));
37fff074
PN
213 } else {
214 assert(NULL);
215 }
216}
217
218/* Read integer encoded as 'encoding' from 'p' */
e1f93d4b
PN
219static 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;
37fff074
PN
232 } else {
233 assert(NULL);
234 }
235 return ret;
236}
237
a5456b2c
PN
238/* Return a struct with all information about an entry. */
239static zlentry zipEntry(unsigned char *p) {
240 zlentry e;
7b1f85c0 241 e.prevrawlen = zipPrevDecodeLength(p,&e.prevrawlensize);
a5456b2c
PN
242 e.len = zipDecodeLength(p+e.prevrawlensize,&e.lensize);
243 e.headersize = e.prevrawlensize+e.lensize;
244 e.encoding = ZIP_ENCODING(p+e.prevrawlensize);
0c0d0564 245 e.p = p;
a5456b2c
PN
246 return e;
247}
248
bb57b965 249/* Return the total number of bytes used by the entry at "p". */
37fff074 250static unsigned int zipRawEntryLength(unsigned char *p) {
bb57b965
PN
251 zlentry e = zipEntry(p);
252 return e.headersize + e.len;
37fff074
PN
253}
254
11ac6ff6
PN
255/* Create a new empty ziplist. */
256unsigned char *ziplistNew(void) {
257 unsigned int bytes = ZIPLIST_HEADER_SIZE+1;
258 unsigned char *zl = zmalloc(bytes);
259 ZIPLIST_BYTES(zl) = bytes;
dcb9cf4e 260 ZIPLIST_TAIL_OFFSET(zl) = ZIPLIST_HEADER_SIZE;
11ac6ff6
PN
261 ZIPLIST_LENGTH(zl) = 0;
262 zl[bytes-1] = ZIP_END;
263 return zl;
264}
265
37fff074 266/* Resize the ziplist. */
11ac6ff6 267static unsigned char *ziplistResize(unsigned char *zl, unsigned int len) {
37fff074 268 zl = zrealloc(zl,len);
11ac6ff6
PN
269 ZIPLIST_BYTES(zl) = len;
270 zl[len-1] = ZIP_END;
271 return zl;
272}
273
0c0d0564 274/* Delete "num" entries, starting at "p". Returns pointer to the ziplist. */
b6eb9703 275static unsigned char *__ziplistDelete(unsigned char *zl, unsigned char *p, unsigned int num) {
0c0d0564
PN
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);
7b1f85c0 292 zipPrevEncodeLength(p-nextdiff,first.prevrawlen);
0c0d0564
PN
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;
11ac6ff6
PN
309}
310
6435c767 311/* Insert item at "p". */
b6eb9703 312static unsigned char *__ziplistInsert(unsigned char *zl, unsigned char *p, unsigned char *s, unsigned int slen) {
6435c767
PN
313 unsigned int curlen = ZIPLIST_BYTES(zl), reqlen, prevlen = 0;
314 unsigned int offset, nextdiff = 0;
315 unsigned char *tail;
b6eb9703 316 unsigned char encoding = ZIP_ENC_RAW;
29b14d5f 317 long long value;
6435c767 318 zlentry entry;
11ac6ff6 319
6435c767
PN
320 /* Find out prevlen for the entry that is inserted. */
321 if (p[0] != ZIP_END) {
322 entry = zipEntry(p);
323 prevlen = entry.prevrawlen;
dcb9cf4e 324 } else {
1ce81fa5 325 tail = ZIPLIST_ENTRY_TAIL(zl);
6435c767
PN
326 if (tail[0] != ZIP_END) {
327 prevlen = zipRawEntryLength(tail);
328 }
dcb9cf4e
PN
329 }
330
29b14d5f 331 /* See if the entry can be encoded */
6435c767 332 if (zipTryEncoding(s,&value,&encoding)) {
29b14d5f
PN
333 reqlen = zipEncodingSize(encoding);
334 } else {
6435c767 335 reqlen = slen;
29b14d5f 336 }
dcb9cf4e
PN
337
338 /* We need space for both the length of the previous entry and
339 * the length of the payload. */
7b1f85c0 340 reqlen += zipPrevEncodeLength(NULL,prevlen);
6435c767
PN
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. */
177a0a0b 346 nextdiff = (p[0] != ZIP_END) ? zipPrevLenByteDiff(p,reqlen) : 0;
6435c767
PN
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. */
7b1f85c0 358 zipPrevEncodeLength(p+reqlen,reqlen);
6435c767
PN
359 /* Update offset for tail */
360 ZIPLIST_TAIL_OFFSET(zl) += reqlen+nextdiff;
11ac6ff6 361 } else {
6435c767
PN
362 /* This element will be the new tail. */
363 ZIPLIST_TAIL_OFFSET(zl) = p-zl;
dcb9cf4e
PN
364 }
365
11ac6ff6 366 /* Write the entry */
7b1f85c0 367 p += zipPrevEncodeLength(p,prevlen);
6435c767 368 p += zipEncodeLength(p,encoding,slen);
29b14d5f
PN
369 if (encoding != ZIP_ENC_RAW) {
370 zipSaveInteger(p,value,encoding);
371 } else {
6435c767 372 memcpy(p,s,slen);
29b14d5f 373 }
f6eb1747 374 ZIPLIST_INCR_LENGTH(zl,1);
11ac6ff6
PN
375 return zl;
376}
377
b6eb9703 378unsigned char *ziplistPush(unsigned char *zl, unsigned char *s, unsigned int slen, int where) {
6435c767 379 unsigned char *p;
1ce81fa5 380 p = (where == ZIPLIST_HEAD) ? ZIPLIST_ENTRY_HEAD(zl) : ZIPLIST_ENTRY_END(zl);
6435c767
PN
381 return __ziplistInsert(zl,p,s,slen);
382}
383
c03206fd
PN
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. */
387unsigned 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 }
08253bf4 405 }
177a0a0b 406 return (p[0] == ZIP_END || index > 0) ? NULL : p;
08253bf4
PN
407}
408
75d8978e 409/* Return pointer to next entry in ziplist. */
8632fb30
PN
410unsigned char *ziplistNext(unsigned char *zl, unsigned char *p) {
411 ((void) zl);
d71b9865
PN
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 }
75d8978e
PN
422}
423
033fb554 424/* Return pointer to previous entry in ziplist. */
8632fb30
PN
425unsigned 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 }
033fb554
PN
440}
441
75d8978e
PN
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. */
b6eb9703 446unsigned int ziplistGet(unsigned char *p, unsigned char **sstr, unsigned int *slen, long long *sval) {
a5456b2c 447 zlentry entry;
c03206fd 448 if (p == NULL || p[0] == ZIP_END) return 0;
03e52931 449 if (sstr) *sstr = NULL;
dcb9cf4e 450
a5456b2c
PN
451 entry = zipEntry(p);
452 if (entry.encoding == ZIP_ENC_RAW) {
03e52931
PN
453 if (sstr) {
454 *slen = entry.len;
b6eb9703 455 *sstr = p+entry.headersize;
75d8978e
PN
456 }
457 } else {
03e52931
PN
458 if (sval) {
459 *sval = zipLoadInteger(p+entry.headersize,entry.encoding);
75d8978e 460 }
08253bf4 461 }
75d8978e 462 return 1;
08253bf4
PN
463}
464
033fb554 465/* Insert an entry at "p". */
b6eb9703 466unsigned char *ziplistInsert(unsigned char *zl, unsigned char *p, unsigned char *s, unsigned int slen) {
033fb554 467 return __ziplistInsert(zl,p,s,slen);
779deb60
PN
468}
469
0f10458c
PN
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. */
6a8e35ad 473unsigned char *ziplistDelete(unsigned char *zl, unsigned char **p) {
0c0d0564
PN
474 unsigned int offset = *p-zl;
475 zl = __ziplistDelete(zl,*p,1);
0f10458c 476
0c0d0564 477 /* Store pointer to current element in p, because ziplistDelete will
0f3dfa87
PN
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. */
6a8e35ad 481 *p = zl+offset;
0f10458c
PN
482 return zl;
483}
484
033fb554
PN
485/* Delete a range of entries from the ziplist. */
486unsigned 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
c09c2c3b 491/* Compare entry pointer to by 'p' with 'entry'. Return 1 if equal. */
b6eb9703 492unsigned int ziplistCompare(unsigned char *p, unsigned char *sstr, unsigned int slen) {
a5456b2c 493 zlentry entry;
b6eb9703
PN
494 unsigned char sencoding;
495 long long zval, sval;
177a0a0b 496 if (p[0] == ZIP_END) return 0;
c09c2c3b 497
a5456b2c
PN
498 entry = zipEntry(p);
499 if (entry.encoding == ZIP_ENC_RAW) {
c09c2c3b 500 /* Raw compare */
a5456b2c 501 if (entry.len == slen) {
03e52931 502 return memcmp(p+entry.headersize,sstr,slen) == 0;
c09c2c3b
PN
503 } else {
504 return 0;
505 }
c4aace90 506 } else {
d593c488 507 /* Try to compare encoded values */
03e52931 508 if (zipTryEncoding(sstr,&sval,&sencoding)) {
d593c488 509 if (entry.encoding == sencoding) {
b6eb9703
PN
510 zval = zipLoadInteger(p+entry.headersize,entry.encoding);
511 return zval == sval;
d593c488 512 }
c4aace90 513 }
c09c2c3b 514 }
c4aace90 515 return 0;
c09c2c3b
PN
516}
517
6205b463
PN
518/* Return length of ziplist. */
519unsigned int ziplistLen(unsigned char *zl) {
520 unsigned int len = 0;
e1f93d4b 521 if (ZIPLIST_LENGTH(zl) < UINT16_MAX) {
6205b463
PN
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 */
e1f93d4b 531 if (len < UINT16_MAX) ZIPLIST_LENGTH(zl) = len;
6205b463
PN
532 }
533 return len;
534}
535
4812cf28
PN
536/* Return size in bytes of ziplist. */
537unsigned int ziplistSize(unsigned char *zl) {
538 return ZIPLIST_BYTES(zl);
539}
540
11ac6ff6 541void ziplistRepr(unsigned char *zl) {
c8d9e7f4
PN
542 unsigned char *p;
543 zlentry entry;
11ac6ff6 544
29b14d5f 545 printf("{total bytes %d} {length %u}\n",ZIPLIST_BYTES(zl), ZIPLIST_LENGTH(zl));
1ce81fa5 546 p = ZIPLIST_ENTRY_HEAD(zl);
11ac6ff6 547 while(*p != ZIP_END) {
c8d9e7f4
PN
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);
29b14d5f 553 } else {
c8d9e7f4 554 printf("%lld", zipLoadInteger(p,entry.encoding));
29b14d5f 555 }
11ac6ff6 556 printf("\n");
c8d9e7f4 557 p += entry.len;
11ac6ff6
PN
558 }
559 printf("{end}\n\n");
560}
561
562#ifdef ZIPLIST_TEST_MAIN
ffc15852 563#include <sys/time.h>
11ac6ff6 564
08253bf4
PN
565unsigned char *createList() {
566 unsigned char *zl = ziplistNew();
b84186ff
PN
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);
08253bf4
PN
571 return zl;
572}
573
29b14d5f
PN
574unsigned char *createIntList() {
575 unsigned char *zl = ziplistNew();
576 char buf[32];
577
578 sprintf(buf, "100");
b84186ff 579 zl = ziplistPush(zl, (unsigned char*)buf, strlen(buf), ZIPLIST_TAIL);
29b14d5f 580 sprintf(buf, "128000");
b84186ff 581 zl = ziplistPush(zl, (unsigned char*)buf, strlen(buf), ZIPLIST_TAIL);
29b14d5f 582 sprintf(buf, "-100");
b84186ff 583 zl = ziplistPush(zl, (unsigned char*)buf, strlen(buf), ZIPLIST_HEAD);
29b14d5f 584 sprintf(buf, "4294967296");
b84186ff 585 zl = ziplistPush(zl, (unsigned char*)buf, strlen(buf), ZIPLIST_HEAD);
29b14d5f 586 sprintf(buf, "non integer");
b84186ff 587 zl = ziplistPush(zl, (unsigned char*)buf, strlen(buf), ZIPLIST_TAIL);
29b14d5f 588 sprintf(buf, "much much longer non integer");
b84186ff 589 zl = ziplistPush(zl, (unsigned char*)buf, strlen(buf), ZIPLIST_TAIL);
29b14d5f
PN
590 return zl;
591}
592
ffc15852
PN
593long long usec(void) {
594 struct timeval tv;
595 gettimeofday(&tv,NULL);
596 return (((long long)tv.tv_sec)*1000000)+tv.tv_usec;
597}
598
599void 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
306974f5
PN
622void 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
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 652
29b14d5f
PN
653 zl = createIntList();
654 ziplistRepr(zl);
655
08253bf4 656 zl = createList();
11ac6ff6
PN
657 ziplistRepr(zl);
658
306974f5 659 pop(zl,ZIPLIST_TAIL);
11ac6ff6
PN
660 ziplistRepr(zl);
661
306974f5 662 pop(zl,ZIPLIST_HEAD);
11ac6ff6
PN
663 ziplistRepr(zl);
664
306974f5 665 pop(zl,ZIPLIST_TAIL);
dcb9cf4e
PN
666 ziplistRepr(zl);
667
306974f5 668 pop(zl,ZIPLIST_TAIL);
dcb9cf4e
PN
669 ziplistRepr(zl);
670
c03206fd
PN
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
08253bf4
PN
748 printf("Iterate list from 0 to end:\n");
749 {
750 zl = createList();
751 p = ziplistIndex(zl, 0);
75d8978e 752 while (ziplistGet(p, &entry, &elen, &value)) {
335d16bc 753 printf("Entry: ");
75d8978e
PN
754 if (entry) {
755 fwrite(entry,elen,1,stdout);
756 } else {
757 printf("%lld", value);
758 }
8632fb30 759 p = ziplistNext(zl,p);
75d8978e 760 printf("\n");
08253bf4
PN
761 }
762 printf("\n");
763 }
764
765 printf("Iterate list from 1 to end:\n");
766 {
767 zl = createList();
768 p = ziplistIndex(zl, 1);
75d8978e 769 while (ziplistGet(p, &entry, &elen, &value)) {
335d16bc 770 printf("Entry: ");
75d8978e
PN
771 if (entry) {
772 fwrite(entry,elen,1,stdout);
773 } else {
774 printf("%lld", value);
775 }
8632fb30 776 p = ziplistNext(zl,p);
75d8978e 777 printf("\n");
08253bf4
PN
778 }
779 printf("\n");
780 }
781
782 printf("Iterate list from 2 to end:\n");
783 {
784 zl = createList();
785 p = ziplistIndex(zl, 2);
75d8978e 786 while (ziplistGet(p, &entry, &elen, &value)) {
335d16bc 787 printf("Entry: ");
75d8978e
PN
788 if (entry) {
789 fwrite(entry,elen,1,stdout);
790 } else {
791 printf("%lld", value);
792 }
8632fb30 793 p = ziplistNext(zl,p);
75d8978e 794 printf("\n");
08253bf4
PN
795 }
796 printf("\n");
797 }
798
799 printf("Iterate starting out of range:\n");
800 {
801 zl = createList();
75d8978e
PN
802 p = ziplistIndex(zl, 4);
803 if (!ziplistGet(p, &entry, &elen, &value)) {
08253bf4
PN
804 printf("No entry\n");
805 } else {
806 printf("ERROR\n");
807 }
779deb60
PN
808 printf("\n");
809 }
810
0f3dfa87
PN
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 }
8632fb30 822 p = ziplistPrev(zl,p);
0f3dfa87
PN
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 }
8632fb30
PN
839 zl = ziplistDelete(zl,&p);
840 p = ziplistPrev(zl,p);
0f3dfa87
PN
841 printf("\n");
842 }
843 printf("\n");
844 }
845
779deb60
PN
846 printf("Delete inclusive range 0,0:\n");
847 {
848 zl = createList();
ba5b4bde 849 zl = ziplistDeleteRange(zl, 0, 1);
779deb60
PN
850 ziplistRepr(zl);
851 }
852
853 printf("Delete inclusive range 0,1:\n");
854 {
855 zl = createList();
ba5b4bde 856 zl = ziplistDeleteRange(zl, 0, 2);
779deb60
PN
857 ziplistRepr(zl);
858 }
859
860 printf("Delete inclusive range 1,2:\n");
861 {
862 zl = createList();
ba5b4bde 863 zl = ziplistDeleteRange(zl, 1, 2);
779deb60
PN
864 ziplistRepr(zl);
865 }
866
867 printf("Delete with start index out of range:\n");
868 {
869 zl = createList();
ba5b4bde 870 zl = ziplistDeleteRange(zl, 5, 1);
779deb60
PN
871 ziplistRepr(zl);
872 }
873
874 printf("Delete with num overflow:\n");
875 {
876 zl = createList();
ba5b4bde 877 zl = ziplistDeleteRange(zl, 1, 5);
779deb60 878 ziplistRepr(zl);
08253bf4
PN
879 }
880
0f10458c
PN
881 printf("Delete foo while iterating:\n");
882 {
883 zl = createList();
b84186ff
PN
884 p = ziplistIndex(zl,0);
885 while (ziplistGet(p,&entry,&elen,&value)) {
886 if (entry && strncmp("foo",(char*)entry,elen) == 0) {
0f10458c 887 printf("Delete foo\n");
b84186ff 888 zl = ziplistDelete(zl,&p);
0f10458c
PN
889 } else {
890 printf("Entry: ");
75d8978e
PN
891 if (entry) {
892 fwrite(entry,elen,1,stdout);
893 } else {
b84186ff 894 printf("%lld",value);
75d8978e 895 }
b84186ff 896 p = ziplistNext(zl,p);
75d8978e 897 printf("\n");
0f10458c
PN
898 }
899 }
900 printf("\n");
901 ziplistRepr(zl);
c09c2c3b
PN
902 }
903
dbaa41c6
PN
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);
b84186ff 911 zl = ziplistPush(zl,(unsigned char*)buf,len,ZIPLIST_TAIL);
dbaa41c6
PN
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
c09c2c3b
PN
925 printf("Compare strings with ziplist entries:\n");
926 {
927 zl = createList();
b84186ff
PN
928 p = ziplistIndex(zl,0);
929 if (!ziplistCompare(p,(unsigned char*)"hello",5)) {
dcb9cf4e 930 printf("ERROR: not \"hello\"\n");
a24ba809 931 return 1;
c09c2c3b 932 }
b84186ff 933 if (ziplistCompare(p,(unsigned char*)"hella",5)) {
dcb9cf4e 934 printf("ERROR: \"hella\"\n");
a24ba809 935 return 1;
c09c2c3b
PN
936 }
937
b84186ff
PN
938 p = ziplistIndex(zl,3);
939 if (!ziplistCompare(p,(unsigned char*)"1024",4)) {
dcb9cf4e 940 printf("ERROR: not \"1024\"\n");
a24ba809 941 return 1;
c09c2c3b 942 }
b84186ff 943 if (ziplistCompare(p,(unsigned char*)"1025",4)) {
dcb9cf4e 944 printf("ERROR: \"1025\"\n");
a24ba809 945 return 1;
c09c2c3b
PN
946 }
947 printf("SUCCESS\n");
0f10458c
PN
948 }
949
ffc15852
PN
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
11ac6ff6
PN
956 return 0;
957}
ffc15852 958
11ac6ff6 959#endif