]> git.saurik.com Git - redis.git/blame - src/ziplist.c
Merge pull request #456 from pietern/unstable-mkopts
[redis.git] / src / ziplist.c
CommitLineData
c4705381
PN
1/* The ziplist is a specially encoded dually linked list that is designed
2 * to be very memory efficient. It stores both strings and integer values,
3 * where integers are encoded as actual integers instead of a series of
4 * characters. It allows push and pop operations on either side of the list
5 * in O(1) time. However, because every operation requires a reallocation of
6 * the memory used by the ziplist, the actual complexity is related to the
7 * amount of memory used by the ziplist.
11ac6ff6 8 *
c4705381 9 * ----------------------------------------------------------------------------
11ac6ff6 10 *
c4705381
PN
11 * ZIPLIST OVERALL LAYOUT:
12 * The general layout of the ziplist is as follows:
13 * <zlbytes><zltail><zllen><entry><entry><zlend>
11ac6ff6 14 *
c4705381
PN
15 * <zlbytes> is an unsigned integer to hold the number of bytes that the
16 * ziplist occupies. This value needs to be stored to be able to resize the
17 * entire structure without the need to traverse it first.
18 *
19 * <zltail> is the offset to the last entry in the list. This allows a pop
20 * operation on the far side of the list without the need for full traversal.
21 *
22 * <zllen> is the number of entries.When this value is larger than 2**16-2,
23 * we need to traverse the entire list to know how many items it holds.
24 *
25 * <zlend> is a single byte special value, equal to 255, which indicates the
26 * end of the list.
27 *
28 * ZIPLIST ENTRIES:
29 * Every entry in the ziplist is prefixed by a header that contains two pieces
30 * of information. First, the length of the previous entry is stored to be
31 * able to traverse the list from back to front. Second, the encoding with an
32 * optional string length of the entry itself is stored.
33 *
34 * The length of the previous entry is encoded in the following way:
35 * If this length is smaller than 254 bytes, it will only consume a single
36 * byte that takes the length as value. When the length is greater than or
37 * equal to 254, it will consume 5 bytes. The first byte is set to 254 to
38 * indicate a larger value is following. The remaining 4 bytes take the
39 * length of the previous entry as value.
40 *
41 * The other header field of the entry itself depends on the contents of the
42 * entry. When the entry is a string, the first 2 bits of this header will hold
43 * the type of encoding used to store the length of the string, followed by the
44 * actual length of the string. When the entry is an integer the first 2 bits
45 * are both set to 1. The following 2 bits are used to specify what kind of
46 * integer will be stored after this header. An overview of the different
47 * types and encodings is as follows:
48 *
49 * |00pppppp| - 1 byte
50 * String value with length less than or equal to 63 bytes (6 bits).
51 * |01pppppp|qqqqqqqq| - 2 bytes
52 * String value with length less than or equal to 16383 bytes (14 bits).
53 * |10______|qqqqqqqq|rrrrrrrr|ssssssss|tttttttt| - 5 bytes
54 * String value with length greater than or equal to 16384 bytes.
55 * |1100____| - 1 byte
56 * Integer encoded as int16_t (2 bytes).
57 * |1101____| - 1 byte
58 * Integer encoded as int32_t (4 bytes).
59 * |1110____| - 1 byte
60 * Integer encoded as int64_t (8 bytes).
11ac6ff6
PN
61 */
62
63#include <stdio.h>
29b14d5f 64#include <stdlib.h>
11ac6ff6 65#include <string.h>
e1f93d4b 66#include <stdint.h>
11ac6ff6 67#include <assert.h>
29b14d5f 68#include <limits.h>
11ac6ff6 69#include "zmalloc.h"
edf23aff 70#include "util.h"
11ac6ff6 71#include "ziplist.h"
7a3e3720 72#include "endianconv.h"
11ac6ff6 73
37fff074 74#define ZIP_END 255
aa549962 75#define ZIP_BIGLEN 254
37fff074 76
c4705381 77/* Different encoding/length possibilities */
fe458402
PN
78#define ZIP_STR_MASK (0xc0)
79#define ZIP_INT_MASK (0x30)
c4705381
PN
80#define ZIP_STR_06B (0 << 6)
81#define ZIP_STR_14B (1 << 6)
82#define ZIP_STR_32B (2 << 6)
83#define ZIP_INT_16B (0xc0 | 0<<4)
84#define ZIP_INT_32B (0xc0 | 1<<4)
85#define ZIP_INT_64B (0xc0 | 2<<4)
37fff074 86
fe458402
PN
87/* Macro to determine type */
88#define ZIP_IS_STR(enc) (((enc) & ZIP_STR_MASK) < ZIP_STR_MASK)
37fff074
PN
89
90/* Utility macros */
e1f93d4b
PN
91#define ZIPLIST_BYTES(zl) (*((uint32_t*)(zl)))
92#define ZIPLIST_TAIL_OFFSET(zl) (*((uint32_t*)((zl)+sizeof(uint32_t))))
93#define ZIPLIST_LENGTH(zl) (*((uint16_t*)((zl)+sizeof(uint32_t)*2)))
94#define ZIPLIST_HEADER_SIZE (sizeof(uint32_t)*2+sizeof(uint16_t))
95#define ZIPLIST_ENTRY_HEAD(zl) ((zl)+ZIPLIST_HEADER_SIZE)
8e0ef249 96#define ZIPLIST_ENTRY_TAIL(zl) ((zl)+intrev32ifbe(ZIPLIST_TAIL_OFFSET(zl)))
97#define ZIPLIST_ENTRY_END(zl) ((zl)+intrev32ifbe(ZIPLIST_BYTES(zl))-1)
e1f93d4b
PN
98
99/* We know a positive increment can only be 1 because entries can only be
100 * pushed one at a time. */
f6eb1747 101#define ZIPLIST_INCR_LENGTH(zl,incr) { \
56538477 102 if (ZIPLIST_LENGTH(zl) < UINT16_MAX) \
3fa19b7d 103 ZIPLIST_LENGTH(zl) = intrev16ifbe(intrev16ifbe(ZIPLIST_LENGTH(zl))+incr); \
56538477 104}
11ac6ff6 105
a5456b2c
PN
106typedef struct zlentry {
107 unsigned int prevrawlensize, prevrawlen;
108 unsigned int lensize, len;
109 unsigned int headersize;
110 unsigned char encoding;
0c0d0564 111 unsigned char *p;
a5456b2c
PN
112} zlentry;
113
fe458402
PN
114#define ZIP_ENTRY_ENCODING(ptr, encoding) do { \
115 (encoding) = (ptr[0]) & (ZIP_STR_MASK | ZIP_INT_MASK); \
116 if (((encoding) & ZIP_STR_MASK) < ZIP_STR_MASK) { \
117 /* String encoding: 2 MSBs */ \
118 (encoding) &= ZIP_STR_MASK; \
119 } \
120} while(0)
c4705381 121
37fff074 122/* Return bytes needed to store integer encoded by 'encoding' */
c4705381
PN
123static unsigned int zipIntSize(unsigned char encoding) {
124 switch(encoding) {
125 case ZIP_INT_16B: return sizeof(int16_t);
126 case ZIP_INT_32B: return sizeof(int32_t);
127 case ZIP_INT_64B: return sizeof(int64_t);
37fff074
PN
128 }
129 assert(NULL);
8ce39260 130 return 0;
37fff074
PN
131}
132
37fff074
PN
133/* Encode the length 'l' writing it in 'p'. If p is NULL it just returns
134 * the amount of bytes required to encode such a length. */
c4705381
PN
135static unsigned int zipEncodeLength(unsigned char *p, unsigned char encoding, unsigned int rawlen) {
136 unsigned char len = 1, buf[5];
137
138 if (ZIP_IS_STR(encoding)) {
139 /* Although encoding is given it may not be set for strings,
140 * so we determine it here using the raw length. */
141 if (rawlen <= 0x3f) {
37fff074 142 if (!p) return len;
c4705381
PN
143 buf[0] = ZIP_STR_06B | rawlen;
144 } else if (rawlen <= 0x3fff) {
145 len += 1;
37fff074 146 if (!p) return len;
c4705381
PN
147 buf[0] = ZIP_STR_14B | ((rawlen >> 8) & 0x3f);
148 buf[1] = rawlen & 0xff;
37fff074
PN
149 } else {
150 len += 4;
151 if (!p) return len;
c4705381
PN
152 buf[0] = ZIP_STR_32B;
153 buf[1] = (rawlen >> 24) & 0xff;
154 buf[2] = (rawlen >> 16) & 0xff;
155 buf[3] = (rawlen >> 8) & 0xff;
156 buf[4] = rawlen & 0xff;
37fff074 157 }
c4705381
PN
158 } else {
159 /* Implies integer encoding, so length is always 1. */
160 if (!p) return len;
161 buf[0] = encoding;
37fff074 162 }
37fff074 163
c4705381 164 /* Store this length at p */
37fff074
PN
165 memcpy(p,buf,len);
166 return len;
167}
168
fe458402
PN
169/* Decode the length encoded in 'ptr'. The 'encoding' variable will hold the
170 * entries encoding, the 'lensize' variable will hold the number of bytes
171 * required to encode the entries length, and the 'len' variable will hold the
172 * entries length. */
173#define ZIP_DECODE_LENGTH(ptr, encoding, lensize, len) do { \
174 ZIP_ENTRY_ENCODING((ptr), (encoding)); \
175 if ((encoding) < ZIP_STR_MASK) { \
176 if ((encoding) == ZIP_STR_06B) { \
177 (lensize) = 1; \
178 (len) = (ptr)[0] & 0x3f; \
179 } else if ((encoding) == ZIP_STR_14B) { \
180 (lensize) = 2; \
181 (len) = (((ptr)[0] & 0x3f) << 8) | (ptr)[1]; \
182 } else if (encoding == ZIP_STR_32B) { \
183 (lensize) = 5; \
184 (len) = ((ptr)[1] << 24) | \
185 ((ptr)[2] << 16) | \
186 ((ptr)[3] << 8) | \
187 ((ptr)[4]); \
188 } else { \
189 assert(NULL); \
190 } \
191 } else { \
192 (lensize) = 1; \
193 (len) = zipIntSize(encoding); \
194 } \
195} while(0);
7b1f85c0
PN
196
197/* Encode the length of the previous entry and write it to "p". Return the
198 * number of bytes needed to encode this length if "p" is NULL. */
199static unsigned int zipPrevEncodeLength(unsigned char *p, unsigned int len) {
200 if (p == NULL) {
201 return (len < ZIP_BIGLEN) ? 1 : sizeof(len)+1;
202 } else {
203 if (len < ZIP_BIGLEN) {
204 p[0] = len;
205 return 1;
206 } else {
207 p[0] = ZIP_BIGLEN;
208 memcpy(p+1,&len,sizeof(len));
f2204374 209 memrev32ifbe(p+1);
7b1f85c0
PN
210 return 1+sizeof(len);
211 }
212 }
213}
214
169d2ef1
PN
215/* Encode the length of the previous entry and write it to "p". This only
216 * uses the larger encoding (required in __ziplistCascadeUpdate). */
217static void zipPrevEncodeLengthForceLarge(unsigned char *p, unsigned int len) {
218 if (p == NULL) return;
219 p[0] = ZIP_BIGLEN;
220 memcpy(p+1,&len,sizeof(len));
f2204374 221 memrev32ifbe(p+1);
169d2ef1
PN
222}
223
fe458402
PN
224/* Decode the number of bytes required to store the length of the previous
225 * element, from the perspective of the entry pointed to by 'ptr'. */
226#define ZIP_DECODE_PREVLENSIZE(ptr, prevlensize) do { \
227 if ((ptr)[0] < ZIP_BIGLEN) { \
228 (prevlensize) = 1; \
229 } else { \
230 (prevlensize) = 5; \
231 } \
232} while(0);
233
234/* Decode the length of the previous element, from the perspective of the entry
235 * pointed to by 'ptr'. */
236#define ZIP_DECODE_PREVLEN(ptr, prevlensize, prevlen) do { \
237 ZIP_DECODE_PREVLENSIZE(ptr, prevlensize); \
238 if ((prevlensize) == 1) { \
239 (prevlen) = (ptr)[0]; \
240 } else if ((prevlensize) == 5) { \
241 assert(sizeof((prevlensize)) == 4); \
242 memcpy(&(prevlen), ((char*)(ptr)) + 1, 4); \
bd376d13 243 memrev32ifbe(&prevlen); \
fe458402
PN
244 } \
245} while(0);
246
247/* Return the difference in number of bytes needed to store the length of the
248 * previous element 'len', in the entry pointed to by 'p'. */
dcb9cf4e
PN
249static int zipPrevLenByteDiff(unsigned char *p, unsigned int len) {
250 unsigned int prevlensize;
fe458402
PN
251 ZIP_DECODE_PREVLENSIZE(p, prevlensize);
252 return zipPrevEncodeLength(NULL, len) - prevlensize;
253}
254
255/* Return the total number of bytes used by the entry pointed to by 'p'. */
256static unsigned int zipRawEntryLength(unsigned char *p) {
257 unsigned int prevlensize, encoding, lensize, len;
258 ZIP_DECODE_PREVLENSIZE(p, prevlensize);
259 ZIP_DECODE_LENGTH(p + prevlensize, encoding, lensize, len);
260 return prevlensize + lensize + len;
dcb9cf4e
PN
261}
262
37fff074 263/* Check if string pointed to by 'entry' can be encoded as an integer.
61712508 264 * Stores the integer value in 'v' and its encoding in 'encoding'. */
265static int zipTryEncoding(unsigned char *entry, unsigned int entrylen, long long *v, unsigned char *encoding) {
37fff074 266 long long value;
37fff074 267
61712508 268 if (entrylen >= 32 || entrylen == 0) return 0;
edf23aff 269 if (string2ll((char*)entry,entrylen,&value)) {
61712508 270 /* Great, the string can be encoded. Check what's the smallest
271 * of our encoding types that can hold this value. */
e1f93d4b 272 if (value >= INT16_MIN && value <= INT16_MAX) {
c4705381 273 *encoding = ZIP_INT_16B;
e1f93d4b 274 } else if (value >= INT32_MIN && value <= INT32_MAX) {
c4705381 275 *encoding = ZIP_INT_32B;
37fff074 276 } else {
c4705381 277 *encoding = ZIP_INT_64B;
37fff074
PN
278 }
279 *v = value;
280 return 1;
281 }
282 return 0;
283}
284
285/* Store integer 'value' at 'p', encoded as 'encoding' */
e1f93d4b
PN
286static void zipSaveInteger(unsigned char *p, int64_t value, unsigned char encoding) {
287 int16_t i16;
288 int32_t i32;
289 int64_t i64;
c4705381 290 if (encoding == ZIP_INT_16B) {
e1f93d4b
PN
291 i16 = value;
292 memcpy(p,&i16,sizeof(i16));
f2204374 293 memrev16ifbe(p);
c4705381 294 } else if (encoding == ZIP_INT_32B) {
e1f93d4b
PN
295 i32 = value;
296 memcpy(p,&i32,sizeof(i32));
f2204374 297 memrev32ifbe(p);
c4705381 298 } else if (encoding == ZIP_INT_64B) {
e1f93d4b
PN
299 i64 = value;
300 memcpy(p,&i64,sizeof(i64));
f2204374 301 memrev64ifbe(p);
37fff074
PN
302 } else {
303 assert(NULL);
304 }
305}
306
307/* Read integer encoded as 'encoding' from 'p' */
e1f93d4b
PN
308static int64_t zipLoadInteger(unsigned char *p, unsigned char encoding) {
309 int16_t i16;
310 int32_t i32;
8ce39260 311 int64_t i64, ret = 0;
c4705381 312 if (encoding == ZIP_INT_16B) {
e1f93d4b 313 memcpy(&i16,p,sizeof(i16));
f2204374 314 memrev16ifbe(&i16);
e1f93d4b 315 ret = i16;
c4705381 316 } else if (encoding == ZIP_INT_32B) {
e1f93d4b 317 memcpy(&i32,p,sizeof(i32));
66d1b021 318 memrev32ifbe(&i32);
e1f93d4b 319 ret = i32;
c4705381 320 } else if (encoding == ZIP_INT_64B) {
e1f93d4b 321 memcpy(&i64,p,sizeof(i64));
3fa19b7d 322 memrev64ifbe(&i64);
e1f93d4b 323 ret = i64;
37fff074
PN
324 } else {
325 assert(NULL);
326 }
327 return ret;
328}
329
a5456b2c
PN
330/* Return a struct with all information about an entry. */
331static zlentry zipEntry(unsigned char *p) {
332 zlentry e;
fe458402
PN
333
334 ZIP_DECODE_PREVLEN(p, e.prevrawlensize, e.prevrawlen);
335 ZIP_DECODE_LENGTH(p + e.prevrawlensize, e.encoding, e.lensize, e.len);
336 e.headersize = e.prevrawlensize + e.lensize;
0c0d0564 337 e.p = p;
a5456b2c
PN
338 return e;
339}
340
11ac6ff6
PN
341/* Create a new empty ziplist. */
342unsigned char *ziplistNew(void) {
343 unsigned int bytes = ZIPLIST_HEADER_SIZE+1;
344 unsigned char *zl = zmalloc(bytes);
56538477 345 ZIPLIST_BYTES(zl) = intrev32ifbe(bytes);
346 ZIPLIST_TAIL_OFFSET(zl) = intrev32ifbe(ZIPLIST_HEADER_SIZE);
11ac6ff6
PN
347 ZIPLIST_LENGTH(zl) = 0;
348 zl[bytes-1] = ZIP_END;
349 return zl;
350}
351
37fff074 352/* Resize the ziplist. */
11ac6ff6 353static unsigned char *ziplistResize(unsigned char *zl, unsigned int len) {
37fff074 354 zl = zrealloc(zl,len);
56538477 355 ZIPLIST_BYTES(zl) = intrev32ifbe(len);
11ac6ff6
PN
356 zl[len-1] = ZIP_END;
357 return zl;
358}
359
169d2ef1
PN
360/* When an entry is inserted, we need to set the prevlen field of the next
361 * entry to equal the length of the inserted entry. It can occur that this
362 * length cannot be encoded in 1 byte and the next entry needs to be grow
363 * a bit larger to hold the 5-byte encoded prevlen. This can be done for free,
364 * because this only happens when an entry is already being inserted (which
365 * causes a realloc and memmove). However, encoding the prevlen may require
366 * that this entry is grown as well. This effect may cascade throughout
367 * the ziplist when there are consecutive entries with a size close to
368 * ZIP_BIGLEN, so we need to check that the prevlen can be encoded in every
369 * consecutive entry.
370 *
371 * Note that this effect can also happen in reverse, where the bytes required
372 * to encode the prevlen field can shrink. This effect is deliberately ignored,
373 * because it can cause a "flapping" effect where a chain prevlen fields is
374 * first grown and then shrunk again after consecutive inserts. Rather, the
375 * field is allowed to stay larger than necessary, because a large prevlen
376 * field implies the ziplist is holding large entries anyway.
377 *
378 * The pointer "p" points to the first entry that does NOT need to be
379 * updated, i.e. consecutive fields MAY need an update. */
380static unsigned char *__ziplistCascadeUpdate(unsigned char *zl, unsigned char *p) {
56538477 381 size_t curlen = intrev32ifbe(ZIPLIST_BYTES(zl)), rawlen, rawlensize;
69298a05 382 size_t offset, noffset, extra;
169d2ef1
PN
383 unsigned char *np;
384 zlentry cur, next;
385
386 while (p[0] != ZIP_END) {
387 cur = zipEntry(p);
388 rawlen = cur.headersize + cur.len;
389 rawlensize = zipPrevEncodeLength(NULL,rawlen);
390
391 /* Abort if there is no next entry. */
392 if (p[rawlen] == ZIP_END) break;
393 next = zipEntry(p+rawlen);
394
395 /* Abort when "prevlen" has not changed. */
396 if (next.prevrawlen == rawlen) break;
397
398 if (next.prevrawlensize < rawlensize) {
399 /* The "prevlen" field of "next" needs more bytes to hold
400 * the raw length of "cur". */
401 offset = p-zl;
402 extra = rawlensize-next.prevrawlensize;
403 zl = ziplistResize(zl,curlen+extra);
169d2ef1
PN
404 p = zl+offset;
405
b7d3bf51 406 /* Current pointer and offset for next element. */
169d2ef1
PN
407 np = p+rawlen;
408 noffset = np-zl;
b7d3bf51
PN
409
410 /* Update tail offset when next element is not the tail element. */
3fa19b7d 411 if ((zl+intrev32ifbe(ZIPLIST_TAIL_OFFSET(zl))) != np) {
412 ZIPLIST_TAIL_OFFSET(zl) =
413 intrev32ifbe(intrev32ifbe(ZIPLIST_TAIL_OFFSET(zl))+extra);
414 }
b7d3bf51
PN
415
416 /* Move the tail to the back. */
169d2ef1
PN
417 memmove(np+rawlensize,
418 np+next.prevrawlensize,
419 curlen-noffset-next.prevrawlensize-1);
420 zipPrevEncodeLength(np,rawlen);
421
422 /* Advance the cursor */
423 p += rawlen;
306c6a02 424 curlen += extra;
169d2ef1
PN
425 } else {
426 if (next.prevrawlensize > rawlensize) {
427 /* This would result in shrinking, which we want to avoid.
428 * So, set "rawlen" in the available bytes. */
429 zipPrevEncodeLengthForceLarge(p+rawlen,rawlen);
430 } else {
431 zipPrevEncodeLength(p+rawlen,rawlen);
432 }
433
434 /* Stop here, as the raw length of "next" has not changed. */
435 break;
436 }
437 }
438 return zl;
439}
440
0c0d0564 441/* Delete "num" entries, starting at "p". Returns pointer to the ziplist. */
b6eb9703 442static unsigned char *__ziplistDelete(unsigned char *zl, unsigned char *p, unsigned int num) {
0c0d0564 443 unsigned int i, totlen, deleted = 0;
69298a05
PN
444 size_t offset;
445 int nextdiff = 0;
169d2ef1
PN
446 zlentry first, tail;
447
448 first = zipEntry(p);
0c0d0564
PN
449 for (i = 0; p[0] != ZIP_END && i < num; i++) {
450 p += zipRawEntryLength(p);
451 deleted++;
452 }
453
454 totlen = p-first.p;
455 if (totlen > 0) {
456 if (p[0] != ZIP_END) {
457 /* Tricky: storing the prevlen in this entry might reduce or
458 * increase the number of bytes needed, compared to the current
459 * prevlen. Note that we can always store this length because
460 * it was previously stored by an entry that is being deleted. */
461 nextdiff = zipPrevLenByteDiff(p,first.prevrawlen);
7b1f85c0 462 zipPrevEncodeLength(p-nextdiff,first.prevrawlen);
0c0d0564
PN
463
464 /* Update offset for tail */
3fa19b7d 465 ZIPLIST_TAIL_OFFSET(zl) =
466 intrev32ifbe(intrev32ifbe(ZIPLIST_TAIL_OFFSET(zl))-totlen);
169d2ef1
PN
467
468 /* When the tail contains more than one entry, we need to take
469 * "nextdiff" in account as well. Otherwise, a change in the
470 * size of prevlen doesn't have an effect on the *tail* offset. */
471 tail = zipEntry(p);
3fa19b7d 472 if (p[tail.headersize+tail.len] != ZIP_END) {
cab1105c 473 ZIPLIST_TAIL_OFFSET(zl) =
3fa19b7d 474 intrev32ifbe(intrev32ifbe(ZIPLIST_TAIL_OFFSET(zl))+nextdiff);
475 }
0c0d0564
PN
476
477 /* Move tail to the front of the ziplist */
56538477 478 memmove(first.p,p-nextdiff,
479 intrev32ifbe(ZIPLIST_BYTES(zl))-(p-zl)-1+nextdiff);
0c0d0564
PN
480 } else {
481 /* The entire tail was deleted. No need to move memory. */
56538477 482 ZIPLIST_TAIL_OFFSET(zl) =
483 intrev32ifbe((first.p-zl)-first.prevrawlen);
0c0d0564
PN
484 }
485
486 /* Resize and update length */
169d2ef1 487 offset = first.p-zl;
56538477 488 zl = ziplistResize(zl, intrev32ifbe(ZIPLIST_BYTES(zl))-totlen+nextdiff);
0c0d0564 489 ZIPLIST_INCR_LENGTH(zl,-deleted);
169d2ef1
PN
490 p = zl+offset;
491
492 /* When nextdiff != 0, the raw length of the next entry has changed, so
493 * we need to cascade the update throughout the ziplist */
494 if (nextdiff != 0)
495 zl = __ziplistCascadeUpdate(zl,p);
0c0d0564
PN
496 }
497 return zl;
11ac6ff6
PN
498}
499
6435c767 500/* Insert item at "p". */
b6eb9703 501static unsigned char *__ziplistInsert(unsigned char *zl, unsigned char *p, unsigned char *s, unsigned int slen) {
56538477 502 size_t curlen = intrev32ifbe(ZIPLIST_BYTES(zl)), reqlen, prevlen = 0;
69298a05
PN
503 size_t offset;
504 int nextdiff = 0;
c4705381 505 unsigned char encoding = 0;
f013f400 506 long long value = 123456789; /* initialized to avoid warning. Using a value
507 that is easy to see if for some reason
508 we use it uninitialized. */
169d2ef1 509 zlentry entry, tail;
11ac6ff6 510
6435c767
PN
511 /* Find out prevlen for the entry that is inserted. */
512 if (p[0] != ZIP_END) {
513 entry = zipEntry(p);
514 prevlen = entry.prevrawlen;
dcb9cf4e 515 } else {
169d2ef1
PN
516 unsigned char *ptail = ZIPLIST_ENTRY_TAIL(zl);
517 if (ptail[0] != ZIP_END) {
518 prevlen = zipRawEntryLength(ptail);
6435c767 519 }
dcb9cf4e
PN
520 }
521
29b14d5f 522 /* See if the entry can be encoded */
61712508 523 if (zipTryEncoding(s,slen,&value,&encoding)) {
c4705381
PN
524 /* 'encoding' is set to the appropriate integer encoding */
525 reqlen = zipIntSize(encoding);
29b14d5f 526 } else {
c4705381
PN
527 /* 'encoding' is untouched, however zipEncodeLength will use the
528 * string length to figure out how to encode it. */
6435c767 529 reqlen = slen;
29b14d5f 530 }
dcb9cf4e
PN
531 /* We need space for both the length of the previous entry and
532 * the length of the payload. */
7b1f85c0 533 reqlen += zipPrevEncodeLength(NULL,prevlen);
6435c767
PN
534 reqlen += zipEncodeLength(NULL,encoding,slen);
535
536 /* When the insert position is not equal to the tail, we need to
537 * make sure that the next entry can hold this entry's length in
538 * its prevlen field. */
177a0a0b 539 nextdiff = (p[0] != ZIP_END) ? zipPrevLenByteDiff(p,reqlen) : 0;
6435c767
PN
540
541 /* Store offset because a realloc may change the address of zl. */
542 offset = p-zl;
543 zl = ziplistResize(zl,curlen+reqlen+nextdiff);
544 p = zl+offset;
545
546 /* Apply memory move when necessary and update tail offset. */
547 if (p[0] != ZIP_END) {
548 /* Subtract one because of the ZIP_END bytes */
549 memmove(p+reqlen,p-nextdiff,curlen-offset-1+nextdiff);
169d2ef1 550
6435c767 551 /* Encode this entry's raw length in the next entry. */
7b1f85c0 552 zipPrevEncodeLength(p+reqlen,reqlen);
169d2ef1 553
6435c767 554 /* Update offset for tail */
3fa19b7d 555 ZIPLIST_TAIL_OFFSET(zl) =
556 intrev32ifbe(intrev32ifbe(ZIPLIST_TAIL_OFFSET(zl))+reqlen);
169d2ef1
PN
557
558 /* When the tail contains more than one entry, we need to take
559 * "nextdiff" in account as well. Otherwise, a change in the
560 * size of prevlen doesn't have an effect on the *tail* offset. */
561 tail = zipEntry(p+reqlen);
3fa19b7d 562 if (p[reqlen+tail.headersize+tail.len] != ZIP_END) {
563 ZIPLIST_TAIL_OFFSET(zl) =
564 intrev32ifbe(intrev32ifbe(ZIPLIST_TAIL_OFFSET(zl))+nextdiff);
565 }
11ac6ff6 566 } else {
6435c767 567 /* This element will be the new tail. */
56538477 568 ZIPLIST_TAIL_OFFSET(zl) = intrev32ifbe(p-zl);
dcb9cf4e
PN
569 }
570
169d2ef1
PN
571 /* When nextdiff != 0, the raw length of the next entry has changed, so
572 * we need to cascade the update throughout the ziplist */
573 if (nextdiff != 0) {
574 offset = p-zl;
575 zl = __ziplistCascadeUpdate(zl,p+reqlen);
576 p = zl+offset;
577 }
578
11ac6ff6 579 /* Write the entry */
7b1f85c0 580 p += zipPrevEncodeLength(p,prevlen);
6435c767 581 p += zipEncodeLength(p,encoding,slen);
c4705381 582 if (ZIP_IS_STR(encoding)) {
6435c767 583 memcpy(p,s,slen);
c4705381
PN
584 } else {
585 zipSaveInteger(p,value,encoding);
29b14d5f 586 }
f6eb1747 587 ZIPLIST_INCR_LENGTH(zl,1);
11ac6ff6
PN
588 return zl;
589}
590
b6eb9703 591unsigned char *ziplistPush(unsigned char *zl, unsigned char *s, unsigned int slen, int where) {
6435c767 592 unsigned char *p;
1ce81fa5 593 p = (where == ZIPLIST_HEAD) ? ZIPLIST_ENTRY_HEAD(zl) : ZIPLIST_ENTRY_END(zl);
6435c767
PN
594 return __ziplistInsert(zl,p,s,slen);
595}
596
c03206fd
PN
597/* Returns an offset to use for iterating with ziplistNext. When the given
598 * index is negative, the list is traversed back to front. When the list
599 * doesn't contain an element at the provided index, NULL is returned. */
600unsigned char *ziplistIndex(unsigned char *zl, int index) {
601 unsigned char *p;
602 zlentry entry;
603 if (index < 0) {
604 index = (-index)-1;
605 p = ZIPLIST_ENTRY_TAIL(zl);
606 if (p[0] != ZIP_END) {
607 entry = zipEntry(p);
608 while (entry.prevrawlen > 0 && index--) {
609 p -= entry.prevrawlen;
610 entry = zipEntry(p);
611 }
612 }
613 } else {
614 p = ZIPLIST_ENTRY_HEAD(zl);
615 while (p[0] != ZIP_END && index--) {
616 p += zipRawEntryLength(p);
617 }
08253bf4 618 }
177a0a0b 619 return (p[0] == ZIP_END || index > 0) ? NULL : p;
08253bf4
PN
620}
621
d51ebef5 622/* Return pointer to next entry in ziplist.
623 *
624 * zl is the pointer to the ziplist
625 * p is the pointer to the current element
626 *
627 * The element after 'p' is returned, otherwise NULL if we are at the end. */
8632fb30
PN
628unsigned char *ziplistNext(unsigned char *zl, unsigned char *p) {
629 ((void) zl);
d71b9865
PN
630
631 /* "p" could be equal to ZIP_END, caused by ziplistDelete,
632 * and we should return NULL. Otherwise, we should return NULL
633 * when the *next* element is ZIP_END (there is no next entry). */
634 if (p[0] == ZIP_END) {
635 return NULL;
d71b9865 636 }
fe458402
PN
637
638 p += zipRawEntryLength(p);
639 if (p[0] == ZIP_END) {
640 return NULL;
641 }
642
643 return p;
75d8978e
PN
644}
645
033fb554 646/* Return pointer to previous entry in ziplist. */
8632fb30
PN
647unsigned char *ziplistPrev(unsigned char *zl, unsigned char *p) {
648 zlentry entry;
649
650 /* Iterating backwards from ZIP_END should return the tail. When "p" is
651 * equal to the first element of the list, we're already at the head,
652 * and should return NULL. */
653 if (p[0] == ZIP_END) {
654 p = ZIPLIST_ENTRY_TAIL(zl);
655 return (p[0] == ZIP_END) ? NULL : p;
656 } else if (p == ZIPLIST_ENTRY_HEAD(zl)) {
657 return NULL;
658 } else {
659 entry = zipEntry(p);
169d2ef1 660 assert(entry.prevrawlen > 0);
8632fb30
PN
661 return p-entry.prevrawlen;
662 }
033fb554
PN
663}
664
75d8978e
PN
665/* Get entry pointer to by 'p' and store in either 'e' or 'v' depending
666 * on the encoding of the entry. 'e' is always set to NULL to be able
667 * to find out whether the string pointer or the integer value was set.
668 * Return 0 if 'p' points to the end of the zipmap, 1 otherwise. */
b6eb9703 669unsigned int ziplistGet(unsigned char *p, unsigned char **sstr, unsigned int *slen, long long *sval) {
a5456b2c 670 zlentry entry;
c03206fd 671 if (p == NULL || p[0] == ZIP_END) return 0;
03e52931 672 if (sstr) *sstr = NULL;
dcb9cf4e 673
a5456b2c 674 entry = zipEntry(p);
c4705381 675 if (ZIP_IS_STR(entry.encoding)) {
03e52931
PN
676 if (sstr) {
677 *slen = entry.len;
b6eb9703 678 *sstr = p+entry.headersize;
75d8978e
PN
679 }
680 } else {
03e52931
PN
681 if (sval) {
682 *sval = zipLoadInteger(p+entry.headersize,entry.encoding);
75d8978e 683 }
08253bf4 684 }
75d8978e 685 return 1;
08253bf4
PN
686}
687
033fb554 688/* Insert an entry at "p". */
b6eb9703 689unsigned char *ziplistInsert(unsigned char *zl, unsigned char *p, unsigned char *s, unsigned int slen) {
033fb554 690 return __ziplistInsert(zl,p,s,slen);
779deb60
PN
691}
692
0f10458c
PN
693/* Delete a single entry from the ziplist, pointed to by *p.
694 * Also update *p in place, to be able to iterate over the
695 * ziplist, while deleting entries. */
6a8e35ad 696unsigned char *ziplistDelete(unsigned char *zl, unsigned char **p) {
69298a05 697 size_t offset = *p-zl;
0c0d0564 698 zl = __ziplistDelete(zl,*p,1);
0f10458c 699
0c0d0564 700 /* Store pointer to current element in p, because ziplistDelete will
0f3dfa87
PN
701 * do a realloc which might result in a different "zl"-pointer.
702 * When the delete direction is back to front, we might delete the last
703 * entry and end up with "p" pointing to ZIP_END, so check this. */
6a8e35ad 704 *p = zl+offset;
0f10458c
PN
705 return zl;
706}
707
033fb554
PN
708/* Delete a range of entries from the ziplist. */
709unsigned char *ziplistDeleteRange(unsigned char *zl, unsigned int index, unsigned int num) {
710 unsigned char *p = ziplistIndex(zl,index);
711 return (p == NULL) ? zl : __ziplistDelete(zl,p,num);
712}
713
c09c2c3b 714/* Compare entry pointer to by 'p' with 'entry'. Return 1 if equal. */
b6eb9703 715unsigned int ziplistCompare(unsigned char *p, unsigned char *sstr, unsigned int slen) {
a5456b2c 716 zlentry entry;
b6eb9703
PN
717 unsigned char sencoding;
718 long long zval, sval;
177a0a0b 719 if (p[0] == ZIP_END) return 0;
c09c2c3b 720
a5456b2c 721 entry = zipEntry(p);
c4705381 722 if (ZIP_IS_STR(entry.encoding)) {
c09c2c3b 723 /* Raw compare */
a5456b2c 724 if (entry.len == slen) {
03e52931 725 return memcmp(p+entry.headersize,sstr,slen) == 0;
c09c2c3b
PN
726 } else {
727 return 0;
728 }
c4aace90 729 } else {
d593c488 730 /* Try to compare encoded values */
61712508 731 if (zipTryEncoding(sstr,slen,&sval,&sencoding)) {
d593c488 732 if (entry.encoding == sencoding) {
b6eb9703
PN
733 zval = zipLoadInteger(p+entry.headersize,entry.encoding);
734 return zval == sval;
d593c488 735 }
c4aace90 736 }
c09c2c3b 737 }
c4aace90 738 return 0;
c09c2c3b
PN
739}
740
fe458402
PN
741/* Find pointer to the entry equal to the specified entry. Skip 'skip' entries
742 * between every comparison. Returns NULL when the field could not be found. */
743unsigned char *ziplistFind(unsigned char *p, unsigned char *vstr, unsigned int vlen, unsigned int skip) {
744 int skipcnt = 0;
745 unsigned char vencoding = 0;
746 long long vll = 0;
747
748 while (p[0] != ZIP_END) {
749 unsigned int prevlensize, encoding, lensize, len;
750 unsigned char *q;
751
752 ZIP_DECODE_PREVLENSIZE(p, prevlensize);
753 ZIP_DECODE_LENGTH(p + prevlensize, encoding, lensize, len);
754 q = p + prevlensize + lensize;
755
756 if (skipcnt == 0) {
757 /* Compare current entry with specified entry */
758 if (ZIP_IS_STR(encoding)) {
759 if (len == vlen && memcmp(q, vstr, vlen) == 0) {
760 return p;
761 }
762 } else {
763 /* Find out if the specified entry can be encoded */
764 if (vencoding == 0) {
765 /* UINT_MAX when the entry CANNOT be encoded */
766 if (!zipTryEncoding(vstr, vlen, &vll, &vencoding)) {
767 vencoding = UCHAR_MAX;
768 }
769
770 /* Must be non-zero by now */
771 assert(vencoding);
772 }
773
774 /* Compare current entry with specified entry */
775 if (encoding == vencoding) {
776 long long ll = zipLoadInteger(q, encoding);
777 if (ll == vll) {
778 return p;
779 }
780 }
781 }
782
783 /* Reset skip count */
784 skipcnt = skip;
785 } else {
786 /* Skip entry */
787 skipcnt--;
788 }
789
790 /* Move to next entry */
791 p = q + len;
792 }
793
794 return NULL;
795}
796
6205b463
PN
797/* Return length of ziplist. */
798unsigned int ziplistLen(unsigned char *zl) {
799 unsigned int len = 0;
56538477 800 if (intrev16ifbe(ZIPLIST_LENGTH(zl)) < UINT16_MAX) {
801 len = intrev16ifbe(ZIPLIST_LENGTH(zl));
6205b463
PN
802 } else {
803 unsigned char *p = zl+ZIPLIST_HEADER_SIZE;
804 while (*p != ZIP_END) {
805 p += zipRawEntryLength(p);
806 len++;
807 }
808
809 /* Re-store length if small enough */
56538477 810 if (len < UINT16_MAX) ZIPLIST_LENGTH(zl) = intrev16ifbe(len);
6205b463
PN
811 }
812 return len;
813}
814
d4fb9f41 815/* Return ziplist blob size in bytes. */
816size_t ziplistBlobLen(unsigned char *zl) {
56538477 817 return intrev32ifbe(ZIPLIST_BYTES(zl));
4812cf28
PN
818}
819
11ac6ff6 820void ziplistRepr(unsigned char *zl) {
c8d9e7f4 821 unsigned char *p;
169d2ef1 822 int index = 0;
c8d9e7f4 823 zlentry entry;
11ac6ff6 824
169d2ef1
PN
825 printf(
826 "{total bytes %d} "
827 "{length %u}\n"
828 "{tail offset %u}\n",
56538477 829 intrev32ifbe(ZIPLIST_BYTES(zl)),
830 intrev16ifbe(ZIPLIST_LENGTH(zl)),
831 intrev32ifbe(ZIPLIST_TAIL_OFFSET(zl)));
1ce81fa5 832 p = ZIPLIST_ENTRY_HEAD(zl);
11ac6ff6 833 while(*p != ZIP_END) {
c8d9e7f4 834 entry = zipEntry(p);
169d2ef1
PN
835 printf(
836 "{"
837 "addr 0x%08lx, "
838 "index %2d, "
839 "offset %5ld, "
840 "rl: %5u, "
841 "hs %2u, "
842 "pl: %5u, "
843 "pls: %2u, "
844 "payload %5u"
845 "} ",
10c12171 846 (long unsigned)p,
169d2ef1 847 index,
10c12171 848 (unsigned long) (p-zl),
169d2ef1
PN
849 entry.headersize+entry.len,
850 entry.headersize,
851 entry.prevrawlen,
852 entry.prevrawlensize,
853 entry.len);
c8d9e7f4 854 p += entry.headersize;
c4705381 855 if (ZIP_IS_STR(entry.encoding)) {
169d2ef1 856 if (entry.len > 40) {
10c12171 857 if (fwrite(p,40,1,stdout) == 0) perror("fwrite");
169d2ef1
PN
858 printf("...");
859 } else {
10c12171 860 if (entry.len &&
861 fwrite(p,entry.len,1,stdout) == 0) perror("fwrite");
169d2ef1 862 }
29b14d5f 863 } else {
3688d7f3 864 printf("%lld", (long long) zipLoadInteger(p,entry.encoding));
29b14d5f 865 }
11ac6ff6 866 printf("\n");
c8d9e7f4 867 p += entry.len;
169d2ef1 868 index++;
11ac6ff6
PN
869 }
870 printf("{end}\n\n");
871}
872
873#ifdef ZIPLIST_TEST_MAIN
ffc15852 874#include <sys/time.h>
306c6a02
PN
875#include "adlist.h"
876#include "sds.h"
877
878#define debug(f, ...) { if (DEBUG) printf(f, __VA_ARGS__); }
11ac6ff6 879
08253bf4
PN
880unsigned char *createList() {
881 unsigned char *zl = ziplistNew();
b84186ff
PN
882 zl = ziplistPush(zl, (unsigned char*)"foo", 3, ZIPLIST_TAIL);
883 zl = ziplistPush(zl, (unsigned char*)"quux", 4, ZIPLIST_TAIL);
884 zl = ziplistPush(zl, (unsigned char*)"hello", 5, ZIPLIST_HEAD);
885 zl = ziplistPush(zl, (unsigned char*)"1024", 4, ZIPLIST_TAIL);
08253bf4
PN
886 return zl;
887}
888
29b14d5f
PN
889unsigned char *createIntList() {
890 unsigned char *zl = ziplistNew();
891 char buf[32];
892
893 sprintf(buf, "100");
b84186ff 894 zl = ziplistPush(zl, (unsigned char*)buf, strlen(buf), ZIPLIST_TAIL);
29b14d5f 895 sprintf(buf, "128000");
b84186ff 896 zl = ziplistPush(zl, (unsigned char*)buf, strlen(buf), ZIPLIST_TAIL);
29b14d5f 897 sprintf(buf, "-100");
b84186ff 898 zl = ziplistPush(zl, (unsigned char*)buf, strlen(buf), ZIPLIST_HEAD);
29b14d5f 899 sprintf(buf, "4294967296");
b84186ff 900 zl = ziplistPush(zl, (unsigned char*)buf, strlen(buf), ZIPLIST_HEAD);
29b14d5f 901 sprintf(buf, "non integer");
b84186ff 902 zl = ziplistPush(zl, (unsigned char*)buf, strlen(buf), ZIPLIST_TAIL);
29b14d5f 903 sprintf(buf, "much much longer non integer");
b84186ff 904 zl = ziplistPush(zl, (unsigned char*)buf, strlen(buf), ZIPLIST_TAIL);
29b14d5f
PN
905 return zl;
906}
907
ffc15852
PN
908long long usec(void) {
909 struct timeval tv;
910 gettimeofday(&tv,NULL);
911 return (((long long)tv.tv_sec)*1000000)+tv.tv_usec;
912}
913
914void stress(int pos, int num, int maxsize, int dnum) {
915 int i,j,k;
916 unsigned char *zl;
917 char posstr[2][5] = { "HEAD", "TAIL" };
918 long long start;
919 for (i = 0; i < maxsize; i+=dnum) {
920 zl = ziplistNew();
921 for (j = 0; j < i; j++) {
922 zl = ziplistPush(zl,(unsigned char*)"quux",4,ZIPLIST_TAIL);
923 }
924
925 /* Do num times a push+pop from pos */
926 start = usec();
927 for (k = 0; k < num; k++) {
928 zl = ziplistPush(zl,(unsigned char*)"quux",4,pos);
929 zl = ziplistDeleteRange(zl,0,1);
930 }
931 printf("List size: %8d, bytes: %8d, %dx push+pop (%s): %6lld usec\n",
56538477 932 i,intrev32ifbe(ZIPLIST_BYTES(zl)),num,posstr[pos],usec()-start);
ffc15852
PN
933 zfree(zl);
934 }
935}
936
306974f5
PN
937void pop(unsigned char *zl, int where) {
938 unsigned char *p, *vstr;
939 unsigned int vlen;
940 long long vlong;
941
942 p = ziplistIndex(zl,where == ZIPLIST_HEAD ? 0 : -1);
943 if (ziplistGet(p,&vstr,&vlen,&vlong)) {
944 if (where == ZIPLIST_HEAD)
945 printf("Pop head: ");
946 else
947 printf("Pop tail: ");
948
949 if (vstr)
10c12171 950 if (vlen && fwrite(vstr,vlen,1,stdout) == 0) perror("fwrite");
306974f5
PN
951 else
952 printf("%lld", vlong);
953
954 printf("\n");
955 ziplistDeleteRange(zl,-1,1);
956 } else {
957 printf("ERROR: Could not pop\n");
958 exit(1);
959 }
960}
961
b7d3bf51 962int randstring(char *target, unsigned int min, unsigned int max) {
306c6a02
PN
963 int p, len = min+rand()%(max-min+1);
964 int minval, maxval;
965 switch(rand() % 3) {
966 case 0:
967 minval = 0;
968 maxval = 255;
969 break;
970 case 1:
971 minval = 48;
972 maxval = 122;
973 break;
974 case 2:
975 minval = 48;
976 maxval = 52;
977 break;
978 default:
979 assert(NULL);
980 }
981
982 while(p < len)
983 target[p++] = minval+rand()%(maxval-minval+1);
b7d3bf51 984 return len;
306c6a02
PN
985}
986
08253bf4 987int main(int argc, char **argv) {
a24ba809 988 unsigned char *zl, *p;
b84186ff 989 unsigned char *entry;
335d16bc 990 unsigned int elen;
75d8978e 991 long long value;
08253bf4 992
84403fe7
PN
993 /* If an argument is given, use it as the random seed. */
994 if (argc == 2)
995 srand(atoi(argv[1]));
996
29b14d5f
PN
997 zl = createIntList();
998 ziplistRepr(zl);
999
08253bf4 1000 zl = createList();
11ac6ff6
PN
1001 ziplistRepr(zl);
1002
306974f5 1003 pop(zl,ZIPLIST_TAIL);
11ac6ff6
PN
1004 ziplistRepr(zl);
1005
306974f5 1006 pop(zl,ZIPLIST_HEAD);
11ac6ff6
PN
1007 ziplistRepr(zl);
1008
306974f5 1009 pop(zl,ZIPLIST_TAIL);
dcb9cf4e
PN
1010 ziplistRepr(zl);
1011
306974f5 1012 pop(zl,ZIPLIST_TAIL);
dcb9cf4e
PN
1013 ziplistRepr(zl);
1014
c03206fd
PN
1015 printf("Get element at index 3:\n");
1016 {
1017 zl = createList();
1018 p = ziplistIndex(zl, 3);
1019 if (!ziplistGet(p, &entry, &elen, &value)) {
1020 printf("ERROR: Could not access index 3\n");
1021 return 1;
1022 }
1023 if (entry) {
10c12171 1024 if (elen && fwrite(entry,elen,1,stdout) == 0) perror("fwrite");
c03206fd
PN
1025 printf("\n");
1026 } else {
1027 printf("%lld\n", value);
1028 }
1029 printf("\n");
1030 }
1031
1032 printf("Get element at index 4 (out of range):\n");
1033 {
1034 zl = createList();
1035 p = ziplistIndex(zl, 4);
1036 if (p == NULL) {
1037 printf("No entry\n");
1038 } else {
1039 printf("ERROR: Out of range index should return NULL, returned offset: %ld\n", p-zl);
1040 return 1;
1041 }
1042 printf("\n");
1043 }
1044
1045 printf("Get element at index -1 (last element):\n");
1046 {
1047 zl = createList();
1048 p = ziplistIndex(zl, -1);
1049 if (!ziplistGet(p, &entry, &elen, &value)) {
1050 printf("ERROR: Could not access index -1\n");
1051 return 1;
1052 }
1053 if (entry) {
10c12171 1054 if (elen && fwrite(entry,elen,1,stdout) == 0) perror("fwrite");
c03206fd
PN
1055 printf("\n");
1056 } else {
1057 printf("%lld\n", value);
1058 }
1059 printf("\n");
1060 }
1061
1062 printf("Get element at index -4 (first element):\n");
1063 {
1064 zl = createList();
1065 p = ziplistIndex(zl, -4);
1066 if (!ziplistGet(p, &entry, &elen, &value)) {
1067 printf("ERROR: Could not access index -4\n");
1068 return 1;
1069 }
1070 if (entry) {
10c12171 1071 if (elen && fwrite(entry,elen,1,stdout) == 0) perror("fwrite");
c03206fd
PN
1072 printf("\n");
1073 } else {
1074 printf("%lld\n", value);
1075 }
1076 printf("\n");
1077 }
1078
1079 printf("Get element at index -5 (reverse out of range):\n");
1080 {
1081 zl = createList();
1082 p = ziplistIndex(zl, -5);
1083 if (p == NULL) {
1084 printf("No entry\n");
1085 } else {
1086 printf("ERROR: Out of range index should return NULL, returned offset: %ld\n", p-zl);
1087 return 1;
1088 }
1089 printf("\n");
1090 }
1091
08253bf4
PN
1092 printf("Iterate list from 0 to end:\n");
1093 {
1094 zl = createList();
1095 p = ziplistIndex(zl, 0);
75d8978e 1096 while (ziplistGet(p, &entry, &elen, &value)) {
335d16bc 1097 printf("Entry: ");
75d8978e 1098 if (entry) {
10c12171 1099 if (elen && fwrite(entry,elen,1,stdout) == 0) perror("fwrite");
75d8978e
PN
1100 } else {
1101 printf("%lld", value);
1102 }
8632fb30 1103 p = ziplistNext(zl,p);
75d8978e 1104 printf("\n");
08253bf4
PN
1105 }
1106 printf("\n");
1107 }
1108
1109 printf("Iterate list from 1 to end:\n");
1110 {
1111 zl = createList();
1112 p = ziplistIndex(zl, 1);
75d8978e 1113 while (ziplistGet(p, &entry, &elen, &value)) {
335d16bc 1114 printf("Entry: ");
75d8978e 1115 if (entry) {
10c12171 1116 if (elen && fwrite(entry,elen,1,stdout) == 0) perror("fwrite");
75d8978e
PN
1117 } else {
1118 printf("%lld", value);
1119 }
8632fb30 1120 p = ziplistNext(zl,p);
75d8978e 1121 printf("\n");
08253bf4
PN
1122 }
1123 printf("\n");
1124 }
1125
1126 printf("Iterate list from 2 to end:\n");
1127 {
1128 zl = createList();
1129 p = ziplistIndex(zl, 2);
75d8978e 1130 while (ziplistGet(p, &entry, &elen, &value)) {
335d16bc 1131 printf("Entry: ");
75d8978e 1132 if (entry) {
10c12171 1133 if (elen && fwrite(entry,elen,1,stdout) == 0) perror("fwrite");
75d8978e
PN
1134 } else {
1135 printf("%lld", value);
1136 }
8632fb30 1137 p = ziplistNext(zl,p);
75d8978e 1138 printf("\n");
08253bf4
PN
1139 }
1140 printf("\n");
1141 }
1142
1143 printf("Iterate starting out of range:\n");
1144 {
1145 zl = createList();
75d8978e
PN
1146 p = ziplistIndex(zl, 4);
1147 if (!ziplistGet(p, &entry, &elen, &value)) {
08253bf4
PN
1148 printf("No entry\n");
1149 } else {
1150 printf("ERROR\n");
1151 }
779deb60
PN
1152 printf("\n");
1153 }
1154
0f3dfa87
PN
1155 printf("Iterate from back to front:\n");
1156 {
1157 zl = createList();
1158 p = ziplistIndex(zl, -1);
1159 while (ziplistGet(p, &entry, &elen, &value)) {
1160 printf("Entry: ");
1161 if (entry) {
10c12171 1162 if (elen && fwrite(entry,elen,1,stdout) == 0) perror("fwrite");
0f3dfa87
PN
1163 } else {
1164 printf("%lld", value);
1165 }
8632fb30 1166 p = ziplistPrev(zl,p);
0f3dfa87
PN
1167 printf("\n");
1168 }
1169 printf("\n");
1170 }
1171
1172 printf("Iterate from back to front, deleting all items:\n");
1173 {
1174 zl = createList();
1175 p = ziplistIndex(zl, -1);
1176 while (ziplistGet(p, &entry, &elen, &value)) {
1177 printf("Entry: ");
1178 if (entry) {
10c12171 1179 if (elen && fwrite(entry,elen,1,stdout) == 0) perror("fwrite");
0f3dfa87
PN
1180 } else {
1181 printf("%lld", value);
1182 }
8632fb30
PN
1183 zl = ziplistDelete(zl,&p);
1184 p = ziplistPrev(zl,p);
0f3dfa87
PN
1185 printf("\n");
1186 }
1187 printf("\n");
1188 }
1189
779deb60
PN
1190 printf("Delete inclusive range 0,0:\n");
1191 {
1192 zl = createList();
ba5b4bde 1193 zl = ziplistDeleteRange(zl, 0, 1);
779deb60
PN
1194 ziplistRepr(zl);
1195 }
1196
1197 printf("Delete inclusive range 0,1:\n");
1198 {
1199 zl = createList();
ba5b4bde 1200 zl = ziplistDeleteRange(zl, 0, 2);
779deb60
PN
1201 ziplistRepr(zl);
1202 }
1203
1204 printf("Delete inclusive range 1,2:\n");
1205 {
1206 zl = createList();
ba5b4bde 1207 zl = ziplistDeleteRange(zl, 1, 2);
779deb60
PN
1208 ziplistRepr(zl);
1209 }
1210
1211 printf("Delete with start index out of range:\n");
1212 {
1213 zl = createList();
ba5b4bde 1214 zl = ziplistDeleteRange(zl, 5, 1);
779deb60
PN
1215 ziplistRepr(zl);
1216 }
1217
1218 printf("Delete with num overflow:\n");
1219 {
1220 zl = createList();
ba5b4bde 1221 zl = ziplistDeleteRange(zl, 1, 5);
779deb60 1222 ziplistRepr(zl);
08253bf4
PN
1223 }
1224
0f10458c
PN
1225 printf("Delete foo while iterating:\n");
1226 {
1227 zl = createList();
b84186ff
PN
1228 p = ziplistIndex(zl,0);
1229 while (ziplistGet(p,&entry,&elen,&value)) {
1230 if (entry && strncmp("foo",(char*)entry,elen) == 0) {
0f10458c 1231 printf("Delete foo\n");
b84186ff 1232 zl = ziplistDelete(zl,&p);
0f10458c
PN
1233 } else {
1234 printf("Entry: ");
75d8978e 1235 if (entry) {
10c12171 1236 if (elen && fwrite(entry,elen,1,stdout) == 0)
1237 perror("fwrite");
75d8978e 1238 } else {
b84186ff 1239 printf("%lld",value);
75d8978e 1240 }
b84186ff 1241 p = ziplistNext(zl,p);
75d8978e 1242 printf("\n");
0f10458c
PN
1243 }
1244 }
1245 printf("\n");
1246 ziplistRepr(zl);
c09c2c3b
PN
1247 }
1248
b0d605c1
PN
1249 printf("Regression test for >255 byte strings:\n");
1250 {
1251 char v1[257],v2[257];
1252 memset(v1,'x',256);
1253 memset(v2,'y',256);
1254 zl = ziplistNew();
1255 zl = ziplistPush(zl,(unsigned char*)v1,strlen(v1),ZIPLIST_TAIL);
1256 zl = ziplistPush(zl,(unsigned char*)v2,strlen(v2),ZIPLIST_TAIL);
1257
1258 /* Pop values again and compare their value. */
1259 p = ziplistIndex(zl,0);
1260 assert(ziplistGet(p,&entry,&elen,&value));
306c6a02 1261 assert(strncmp(v1,(char*)entry,elen) == 0);
b0d605c1
PN
1262 p = ziplistIndex(zl,1);
1263 assert(ziplistGet(p,&entry,&elen,&value));
306c6a02 1264 assert(strncmp(v2,(char*)entry,elen) == 0);
b0d605c1
PN
1265 printf("SUCCESS\n\n");
1266 }
1267
dbaa41c6
PN
1268 printf("Create long list and check indices:\n");
1269 {
1270 zl = ziplistNew();
1271 char buf[32];
1272 int i,len;
1273 for (i = 0; i < 1000; i++) {
1274 len = sprintf(buf,"%d",i);
b84186ff 1275 zl = ziplistPush(zl,(unsigned char*)buf,len,ZIPLIST_TAIL);
dbaa41c6
PN
1276 }
1277 for (i = 0; i < 1000; i++) {
1278 p = ziplistIndex(zl,i);
1279 assert(ziplistGet(p,NULL,NULL,&value));
1280 assert(i == value);
1281
1282 p = ziplistIndex(zl,-i-1);
1283 assert(ziplistGet(p,NULL,NULL,&value));
1284 assert(999-i == value);
1285 }
1286 printf("SUCCESS\n\n");
1287 }
1288
c09c2c3b
PN
1289 printf("Compare strings with ziplist entries:\n");
1290 {
1291 zl = createList();
b84186ff
PN
1292 p = ziplistIndex(zl,0);
1293 if (!ziplistCompare(p,(unsigned char*)"hello",5)) {
dcb9cf4e 1294 printf("ERROR: not \"hello\"\n");
a24ba809 1295 return 1;
c09c2c3b 1296 }
b84186ff 1297 if (ziplistCompare(p,(unsigned char*)"hella",5)) {
dcb9cf4e 1298 printf("ERROR: \"hella\"\n");
a24ba809 1299 return 1;
c09c2c3b
PN
1300 }
1301
b84186ff
PN
1302 p = ziplistIndex(zl,3);
1303 if (!ziplistCompare(p,(unsigned char*)"1024",4)) {
dcb9cf4e 1304 printf("ERROR: not \"1024\"\n");
a24ba809 1305 return 1;
c09c2c3b 1306 }
b84186ff 1307 if (ziplistCompare(p,(unsigned char*)"1025",4)) {
dcb9cf4e 1308 printf("ERROR: \"1025\"\n");
a24ba809 1309 return 1;
c09c2c3b 1310 }
169d2ef1
PN
1311 printf("SUCCESS\n\n");
1312 }
1313
1314 printf("Stress with random payloads of different encoding:\n");
1315 {
306c6a02 1316 int i,j,len,where;
169d2ef1 1317 unsigned char *p;
306c6a02 1318 char buf[1024];
b7d3bf51 1319 int buflen;
306c6a02
PN
1320 list *ref;
1321 listNode *refnode;
1322
1323 /* Hold temp vars from ziplist */
1324 unsigned char *sstr;
1325 unsigned int slen;
1326 long long sval;
1327
306c6a02
PN
1328 for (i = 0; i < 20000; i++) {
1329 zl = ziplistNew();
1330 ref = listCreate();
1331 listSetFreeMethod(ref,sdsfree);
1332 len = rand() % 256;
1333
1334 /* Create lists */
1335 for (j = 0; j < len; j++) {
1336 where = (rand() & 1) ? ZIPLIST_HEAD : ZIPLIST_TAIL;
b7d3bf51
PN
1337 if (rand() % 2) {
1338 buflen = randstring(buf,1,sizeof(buf)-1);
1339 } else {
1340 switch(rand() % 3) {
1341 case 0:
1342 buflen = sprintf(buf,"%lld",(0LL + rand()) >> 20);
1343 break;
1344 case 1:
1345 buflen = sprintf(buf,"%lld",(0LL + rand()));
1346 break;
1347 case 2:
1348 buflen = sprintf(buf,"%lld",(0LL + rand()) << 20);
1349 break;
1350 default:
1351 assert(NULL);
1352 }
306c6a02
PN
1353 }
1354
1355 /* Add to ziplist */
b7d3bf51 1356 zl = ziplistPush(zl, (unsigned char*)buf, buflen, where);
169d2ef1 1357
306c6a02
PN
1358 /* Add to reference list */
1359 if (where == ZIPLIST_HEAD) {
b7d3bf51 1360 listAddNodeHead(ref,sdsnewlen(buf, buflen));
306c6a02 1361 } else if (where == ZIPLIST_TAIL) {
b7d3bf51 1362 listAddNodeTail(ref,sdsnewlen(buf, buflen));
306c6a02
PN
1363 } else {
1364 assert(NULL);
1365 }
169d2ef1
PN
1366 }
1367
306c6a02
PN
1368 assert(listLength(ref) == ziplistLen(zl));
1369 for (j = 0; j < len; j++) {
1370 /* Naive way to get elements, but similar to the stresser
1371 * executed from the Tcl test suite. */
1372 p = ziplistIndex(zl,j);
1373 refnode = listIndex(ref,j);
1374
1375 assert(ziplistGet(p,&sstr,&slen,&sval));
1376 if (sstr == NULL) {
b7d3bf51 1377 buflen = sprintf(buf,"%lld",sval);
306c6a02 1378 } else {
b7d3bf51
PN
1379 buflen = slen;
1380 memcpy(buf,sstr,buflen);
1381 buf[buflen] = '\0';
306c6a02 1382 }
b7d3bf51 1383 assert(memcmp(buf,listNodeValue(refnode),buflen) == 0);
306c6a02
PN
1384 }
1385 zfree(zl);
1386 listRelease(ref);
169d2ef1
PN
1387 }
1388 printf("SUCCESS\n\n");
0f10458c
PN
1389 }
1390
ffc15852
PN
1391 printf("Stress with variable ziplist size:\n");
1392 {
1393 stress(ZIPLIST_HEAD,100000,16384,256);
1394 stress(ZIPLIST_TAIL,100000,16384,256);
1395 }
1396
11ac6ff6
PN
1397 return 0;
1398}
ffc15852 1399
11ac6ff6 1400#endif