]> git.saurik.com Git - redis.git/blame - ziplist.c
added header ziplist.h
[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
PN
19#include <string.h>
20#include <assert.h>
29b14d5f 21#include <limits.h>
11ac6ff6
PN
22#include "zmalloc.h"
23#include "sds.h"
24#include "ziplist.h"
11ac6ff6 25
37fff074
PN
26#define ZIP_END 255
27
28/* Entry encoding */
29#define ZIP_ENC_RAW 0
30#define ZIP_ENC_SHORT 1
31#define ZIP_ENC_INT 2
32#define ZIP_ENC_LLONG 3
33#define ZIP_ENCODING(p) ((p)[0] >> 6)
34
35/* Length encoding for raw entries */
36#define ZIP_LEN_INLINE 0
37#define ZIP_LEN_UINT16 1
38#define ZIP_LEN_UINT32 2
39
40/* Utility macros */
11ac6ff6
PN
41#define ZIPLIST_BYTES(zl) (*((unsigned int*)(zl)))
42#define ZIPLIST_LENGTH(zl) (*((zl)+sizeof(unsigned int)))
43#define ZIPLIST_HEADER_SIZE (sizeof(unsigned int)+1)
f6eb1747
PN
44#define ZIPLIST_INCR_LENGTH(zl,incr) { \
45 if (ZIPLIST_LENGTH(zl) < (ZIP_END-1)) ZIPLIST_LENGTH(zl)+=incr; }
11ac6ff6 46
37fff074
PN
47/* Return bytes needed to store integer encoded by 'encoding' */
48static unsigned int zipEncodingSize(char encoding) {
49 if (encoding == ZIP_ENC_SHORT) {
50 return sizeof(short int);
51 } else if (encoding == ZIP_ENC_INT) {
52 return sizeof(int);
53 } else if (encoding == ZIP_ENC_LLONG) {
54 return sizeof(long long);
55 }
56 assert(NULL);
57}
58
59/* Decode the encoded length pointed by 'p'. If a pointer to 'lensize' is
60 * provided, it is set to the number of bytes required to encode the length. */
61static unsigned int zipDecodeLength(unsigned char *p, unsigned int *lensize) {
62 unsigned char encoding = ZIP_ENCODING(p), lenenc;
63 unsigned int len;
64
65 if (encoding == ZIP_ENC_RAW) {
66 lenenc = (p[0] >> 4) & 0x3;
67 if (lenenc == ZIP_LEN_INLINE) {
68 len = p[0] & 0xf;
69 if (lensize) *lensize = 1;
70 } else if (lenenc == ZIP_LEN_UINT16) {
71 len = p[1] | (p[2] << 8);
72 if (lensize) *lensize = 3;
73 } else {
74 len = p[1] | (p[2] << 8) | (p[3] << 16) | (p[4] << 24);
75 if (lensize) *lensize = 5;
76 }
77 } else {
78 len = zipEncodingSize(encoding);
79 if (lensize) *lensize = 1;
80 }
81 return len;
82}
83
84/* Encode the length 'l' writing it in 'p'. If p is NULL it just returns
85 * the amount of bytes required to encode such a length. */
86static unsigned int zipEncodeLength(unsigned char *p, char encoding, unsigned int rawlen) {
87 unsigned char len = 1, lenenc, buf[5];
88 if (encoding == ZIP_ENC_RAW) {
89 if (rawlen <= 0xf) {
90 if (!p) return len;
91 lenenc = ZIP_LEN_INLINE;
92 buf[0] = rawlen;
93 } else if (rawlen <= 0xffff) {
94 len += 2;
95 if (!p) return len;
96 lenenc = ZIP_LEN_UINT16;
97 buf[1] = (rawlen ) & 0xff;
98 buf[2] = (rawlen >> 8) & 0xff;
99 } else {
100 len += 4;
101 if (!p) return len;
102 lenenc = ZIP_LEN_UINT32;
103 buf[1] = (rawlen ) & 0xff;
104 buf[2] = (rawlen >> 8) & 0xff;
105 buf[3] = (rawlen >> 16) & 0xff;
106 buf[4] = (rawlen >> 24) & 0xff;
107 }
108 buf[0] = (lenenc << 4) | (buf[0] & 0xf);
109 }
110 if (!p) return len;
111
112 /* Apparently we need to store the length in 'p' */
113 buf[0] = (encoding << 6) | (buf[0] & 0x3f);
114 memcpy(p,buf,len);
115 return len;
116}
117
118/* Check if string pointed to by 'entry' can be encoded as an integer.
119 * Stores the integer value in 'v' and its encoding in 'encoding'.
120 * Warning: this function requires a NULL-terminated string! */
121static int zipTryEncoding(unsigned char *entry, long long *v, char *encoding) {
122 long long value;
123 char *eptr;
124
125 if (entry[0] == '-' || (entry[0] >= '0' && entry[0] <= '9')) {
126 value = strtoll(entry,&eptr,10);
127 if (eptr[0] != '\0') return 0;
128 if (value >= SHRT_MIN && value <= SHRT_MAX) {
129 *encoding = ZIP_ENC_SHORT;
130 } else if (value >= INT_MIN && value <= INT_MAX) {
131 *encoding = ZIP_ENC_INT;
132 } else {
133 *encoding = ZIP_ENC_LLONG;
134 }
135 *v = value;
136 return 1;
137 }
138 return 0;
139}
140
141/* Store integer 'value' at 'p', encoded as 'encoding' */
142static void zipSaveInteger(unsigned char *p, long long value, char encoding) {
143 short int s;
144 int i;
145 long long l;
146 if (encoding == ZIP_ENC_SHORT) {
147 s = value;
148 memcpy(p,&s,sizeof(s));
149 } else if (encoding == ZIP_ENC_INT) {
150 i = value;
151 memcpy(p,&i,sizeof(i));
152 } else if (encoding == ZIP_ENC_LLONG) {
153 l = value;
154 memcpy(p,&l,sizeof(l));
155 } else {
156 assert(NULL);
157 }
158}
159
160/* Read integer encoded as 'encoding' from 'p' */
161static long long zipLoadInteger(unsigned char *p, char encoding) {
162 short int s;
163 int i;
164 long long l, ret;
165 if (encoding == ZIP_ENC_SHORT) {
166 memcpy(&s,p,sizeof(s));
167 ret = s;
168 } else if (encoding == ZIP_ENC_INT) {
169 memcpy(&i,p,sizeof(i));
170 ret = i;
171 } else if (encoding == ZIP_ENC_LLONG) {
172 memcpy(&l,p,sizeof(l));
173 ret = l;
174 } else {
175 assert(NULL);
176 }
177 return ret;
178}
179
180/* Return the total amount used by an entry (encoded length + payload). */
181static unsigned int zipRawEntryLength(unsigned char *p) {
182 unsigned int lensize, len;
183 len = zipDecodeLength(p, &lensize);
184 return lensize + len;
185}
186
11ac6ff6
PN
187/* Create a new empty ziplist. */
188unsigned char *ziplistNew(void) {
189 unsigned int bytes = ZIPLIST_HEADER_SIZE+1;
190 unsigned char *zl = zmalloc(bytes);
191 ZIPLIST_BYTES(zl) = bytes;
192 ZIPLIST_LENGTH(zl) = 0;
193 zl[bytes-1] = ZIP_END;
194 return zl;
195}
196
37fff074 197/* Resize the ziplist. */
11ac6ff6 198static unsigned char *ziplistResize(unsigned char *zl, unsigned int len) {
37fff074 199 zl = zrealloc(zl,len);
11ac6ff6
PN
200 ZIPLIST_BYTES(zl) = len;
201 zl[len-1] = ZIP_END;
202 return zl;
203}
204
205static unsigned char *ziplistHead(unsigned char *zl) {
206 return zl+ZIPLIST_HEADER_SIZE;
207}
208
209static unsigned char *ziplistTail(unsigned char *zl) {
210 unsigned char *p, *q;
211 p = q = ziplistHead(zl);
212 while (*p != ZIP_END) {
213 q = p;
214 p += zipRawEntryLength(p);
215 }
216 return q;
217}
218
219unsigned char *ziplistPush(unsigned char *zl, unsigned char *entry, unsigned int elen, int where) {
29b14d5f 220 unsigned int curlen = ZIPLIST_BYTES(zl), reqlen;
11ac6ff6 221 unsigned char *p;
29b14d5f
PN
222 char encoding = ZIP_ENC_RAW;
223 long long value;
11ac6ff6 224
29b14d5f
PN
225 /* See if the entry can be encoded */
226 if (zipTryEncoding(entry,&value,&encoding)) {
227 reqlen = zipEncodingSize(encoding);
228 } else {
229 reqlen = elen;
230 }
231 reqlen += zipEncodeLength(NULL,encoding,elen);
11ac6ff6 232
29b14d5f
PN
233 /* Resize the ziplist and move if needed */
234 zl = ziplistResize(zl,curlen+reqlen);
11ac6ff6
PN
235 if (where == ZIPLIST_HEAD) {
236 p = zl+ZIPLIST_HEADER_SIZE;
237 if (*p != ZIP_END) {
238 /* Subtract one because of the ZIP_END bytes */
239 memmove(p+reqlen,p,curlen-ZIPLIST_HEADER_SIZE-1);
240 }
241 } else {
242 p = zl+curlen-1;
243 }
244
11ac6ff6 245 /* Write the entry */
29b14d5f
PN
246 p += zipEncodeLength(p,encoding,elen);
247 if (encoding != ZIP_ENC_RAW) {
248 zipSaveInteger(p,value,encoding);
249 } else {
250 memcpy(p,entry,elen);
251 }
f6eb1747 252 ZIPLIST_INCR_LENGTH(zl,1);
11ac6ff6
PN
253 return zl;
254}
255
29b14d5f
PN
256unsigned char *ziplistPop(unsigned char *zl, sds *target, int where) {
257 unsigned int curlen = ZIPLIST_BYTES(zl), rawlen;
258 unsigned int len, lensize;
11ac6ff6 259 unsigned char *p;
29b14d5f
PN
260 long long value;
261 if (target) *target = NULL;
11ac6ff6
PN
262
263 /* Get pointer to element to remove */
264 p = (where == ZIPLIST_HEAD) ? ziplistHead(zl) : ziplistTail(zl);
265 if (*p == ZIP_END) return zl;
29b14d5f
PN
266 len = zipDecodeLength(p,&lensize);
267 if (target) {
268 if (ZIP_ENCODING(p) == ZIP_ENC_RAW) {
269 *target = sdsnewlen(p+lensize,len);
270 } else {
271 value = zipLoadInteger(p+lensize,ZIP_ENCODING(p));
272 *target = sdscatprintf(sdsempty(), "%lld", value);
273 }
274 }
11ac6ff6
PN
275
276 /* Move list to front when popping from the head */
29b14d5f 277 rawlen = lensize+len;
11ac6ff6 278 if (where == ZIPLIST_HEAD) {
29b14d5f 279 memmove(p,p+rawlen,curlen-ZIPLIST_HEADER_SIZE-len);
11ac6ff6
PN
280 }
281
282 /* Resize and update length */
29b14d5f 283 zl = ziplistResize(zl,curlen-rawlen);
f6eb1747 284 ZIPLIST_INCR_LENGTH(zl,-1);
11ac6ff6
PN
285 return zl;
286}
287
08253bf4
PN
288/* Returns an offset to use for iterating with ziplistNext. */
289unsigned char *ziplistIndex(unsigned char *zl, unsigned int index) {
290 unsigned char *p = zl+ZIPLIST_HEADER_SIZE;
291 unsigned int i = 0;
292 for (; i < index; i++) {
293 if (*p == ZIP_END) break;
294 p += zipRawEntryLength(p);
295 }
296 return p;
297}
298
75d8978e
PN
299/* Return pointer to next entry in ziplist. */
300unsigned char *ziplistNext(unsigned char *p) {
301 return *p == ZIP_END ? p : p+zipRawEntryLength(p);
302}
303
304/* Get entry pointer to by 'p' and store in either 'e' or 'v' depending
305 * on the encoding of the entry. 'e' is always set to NULL to be able
306 * to find out whether the string pointer or the integer value was set.
307 * Return 0 if 'p' points to the end of the zipmap, 1 otherwise. */
308unsigned int ziplistGet(unsigned char *p, unsigned char **e, unsigned int *elen, long long *v) {
309 unsigned int len, lensize;
310 if (*p == ZIP_END) return 0;
311 if (e) *e = NULL;
312 len = zipDecodeLength(p,&lensize);
313 if (ZIP_ENCODING(p) == ZIP_ENC_RAW) {
314 if (e) {
315 *elen = len;
316 *e = p+lensize;
317 }
318 } else {
319 if (v) {
320 *v = zipLoadInteger(p+lensize,ZIP_ENCODING(p));
321 }
08253bf4 322 }
75d8978e 323 return 1;
08253bf4
PN
324}
325
ba5b4bde
PN
326/* Delete a range of entries from the ziplist. */
327unsigned char *ziplistDeleteRange(unsigned char *zl, unsigned int index, unsigned int num) {
779deb60
PN
328 unsigned char *p, *first = ziplistIndex(zl, index);
329 unsigned int i, deleted = 0, totlen, newlen;
330 for (p = first, i = 0; *p != ZIP_END && i < num; i++) {
331 p += zipRawEntryLength(p);
332 deleted++;
333 }
334
335 totlen = p-first;
336 if (totlen > 0) {
337 /* Move current tail to the new tail when there *is* a tail */
338 if (*p != ZIP_END) memmove(first,p,ZIPLIST_BYTES(zl)-(p-zl)-1);
339
340 /* Resize and update length */
341 zl = ziplistResize(zl, ZIPLIST_BYTES(zl)-totlen);
f6eb1747 342 ZIPLIST_INCR_LENGTH(zl,-deleted);
779deb60
PN
343 }
344 return zl;
345}
346
0f10458c
PN
347/* Delete a single entry from the ziplist, pointed to by *p.
348 * Also update *p in place, to be able to iterate over the
349 * ziplist, while deleting entries. */
350unsigned char *ziplistDelete(unsigned char *zl, unsigned char **p) {
351 unsigned int offset = *p-zl, tail, len;
352 len = zipRawEntryLength(*p);
353 tail = ZIPLIST_BYTES(zl)-offset-len-1;
354
355 /* Move current tail to the new tail when there *is* a tail */
356 if (tail > 0) memmove(*p,*p+len,tail);
357
358 /* Resize and update length */
359 zl = ziplistResize(zl, ZIPLIST_BYTES(zl)-len);
f6eb1747 360 ZIPLIST_INCR_LENGTH(zl,-1);
0f10458c
PN
361
362 /* Store new pointer to current element in p.
363 * This needs to be done because zl can change on realloc. */
364 *p = zl+offset;
365 return zl;
366}
367
c09c2c3b
PN
368/* Compare entry pointer to by 'p' with 'entry'. Return 1 if equal. */
369unsigned int ziplistCompare(unsigned char *p, unsigned char *entry, unsigned int elen) {
370 unsigned int zlen, lensize;
371 char encoding;
372 long long zval, eval;
373 if (*p == ZIP_END) return 0;
374
375 zlen = zipDecodeLength(p,&lensize);
376 if (zipTryEncoding(entry,&eval,&encoding)) {
377 /* Do integer compare */
378 zval = zipLoadInteger(p+lensize,ZIP_ENCODING(p));
379 return zval == eval;
380 } else {
381 /* Raw compare */
382 if (zlen == elen) {
383 return memcmp(p+lensize,entry,elen) == 0;
384 } else {
385 return 0;
386 }
387 }
388}
389
11ac6ff6 390void ziplistRepr(unsigned char *zl) {
29b14d5f
PN
391 unsigned char *p, encoding;
392 unsigned int l, lsize;
393 long long value;
11ac6ff6 394
29b14d5f 395 printf("{total bytes %d} {length %u}\n",ZIPLIST_BYTES(zl), ZIPLIST_LENGTH(zl));
11ac6ff6
PN
396 p = ziplistHead(zl);
397 while(*p != ZIP_END) {
29b14d5f
PN
398 l = zipDecodeLength(p,&lsize);
399 printf("{header %u, payload %u} ",lsize,l);
400 encoding = ZIP_ENCODING(p);
401 p += lsize;
402 if (encoding == ZIP_ENC_RAW) {
403 fwrite(p,l,1,stdout);
404 } else {
405 printf("%lld", zipLoadInteger(p,encoding));
406 }
11ac6ff6
PN
407 printf("\n");
408 p += l;
409 }
410 printf("{end}\n\n");
411}
412
413#ifdef ZIPLIST_TEST_MAIN
11ac6ff6 414
08253bf4
PN
415unsigned char *createList() {
416 unsigned char *zl = ziplistNew();
11ac6ff6 417 zl = ziplistPush(zl, (unsigned char*)"foo", 3, ZIPLIST_TAIL);
11ac6ff6 418 zl = ziplistPush(zl, (unsigned char*)"quux", 4, ZIPLIST_TAIL);
11ac6ff6 419 zl = ziplistPush(zl, (unsigned char*)"hello", 5, ZIPLIST_HEAD);
75d8978e 420 zl = ziplistPush(zl, (unsigned char*)"1024", 4, ZIPLIST_TAIL);
08253bf4
PN
421 return zl;
422}
423
29b14d5f
PN
424unsigned char *createIntList() {
425 unsigned char *zl = ziplistNew();
426 char buf[32];
427
428 sprintf(buf, "100");
429 zl = ziplistPush(zl, buf, strlen(buf), ZIPLIST_TAIL);
430 sprintf(buf, "128000");
431 zl = ziplistPush(zl, buf, strlen(buf), ZIPLIST_TAIL);
432 sprintf(buf, "-100");
433 zl = ziplistPush(zl, buf, strlen(buf), ZIPLIST_HEAD);
434 sprintf(buf, "4294967296");
435 zl = ziplistPush(zl, buf, strlen(buf), ZIPLIST_HEAD);
436 sprintf(buf, "non integer");
437 zl = ziplistPush(zl, buf, strlen(buf), ZIPLIST_TAIL);
438 sprintf(buf, "much much longer non integer");
439 zl = ziplistPush(zl, buf, strlen(buf), ZIPLIST_TAIL);
440 return zl;
441}
442
08253bf4 443int main(int argc, char **argv) {
0f10458c 444 unsigned char *zl, *p, *q, *entry;
335d16bc 445 unsigned int elen;
75d8978e 446 long long value;
08253bf4
PN
447 sds s;
448
29b14d5f
PN
449 zl = createIntList();
450 ziplistRepr(zl);
451
08253bf4 452 zl = createList();
11ac6ff6
PN
453 ziplistRepr(zl);
454
455 zl = ziplistPop(zl, &s, ZIPLIST_TAIL);
456 printf("Pop tail: %s (length %ld)\n", s, sdslen(s));
457 ziplistRepr(zl);
458
459 zl = ziplistPop(zl, &s, ZIPLIST_HEAD);
460 printf("Pop head: %s (length %ld)\n", s, sdslen(s));
461 ziplistRepr(zl);
462
08253bf4
PN
463 printf("Iterate list from 0 to end:\n");
464 {
465 zl = createList();
466 p = ziplistIndex(zl, 0);
75d8978e 467 while (ziplistGet(p, &entry, &elen, &value)) {
335d16bc 468 printf("Entry: ");
75d8978e
PN
469 if (entry) {
470 fwrite(entry,elen,1,stdout);
471 } else {
472 printf("%lld", value);
473 }
474 p = ziplistNext(p);
475 printf("\n");
08253bf4
PN
476 }
477 printf("\n");
478 }
479
480 printf("Iterate list from 1 to end:\n");
481 {
482 zl = createList();
483 p = ziplistIndex(zl, 1);
75d8978e 484 while (ziplistGet(p, &entry, &elen, &value)) {
335d16bc 485 printf("Entry: ");
75d8978e
PN
486 if (entry) {
487 fwrite(entry,elen,1,stdout);
488 } else {
489 printf("%lld", value);
490 }
491 p = ziplistNext(p);
492 printf("\n");
08253bf4
PN
493 }
494 printf("\n");
495 }
496
497 printf("Iterate list from 2 to end:\n");
498 {
499 zl = createList();
500 p = ziplistIndex(zl, 2);
75d8978e 501 while (ziplistGet(p, &entry, &elen, &value)) {
335d16bc 502 printf("Entry: ");
75d8978e
PN
503 if (entry) {
504 fwrite(entry,elen,1,stdout);
505 } else {
506 printf("%lld", value);
507 }
508 p = ziplistNext(p);
509 printf("\n");
08253bf4
PN
510 }
511 printf("\n");
512 }
513
514 printf("Iterate starting out of range:\n");
515 {
516 zl = createList();
75d8978e
PN
517 p = ziplistIndex(zl, 4);
518 if (!ziplistGet(p, &entry, &elen, &value)) {
08253bf4
PN
519 printf("No entry\n");
520 } else {
521 printf("ERROR\n");
522 }
779deb60
PN
523 printf("\n");
524 }
525
526 printf("Delete inclusive range 0,0:\n");
527 {
528 zl = createList();
ba5b4bde 529 zl = ziplistDeleteRange(zl, 0, 1);
779deb60
PN
530 ziplistRepr(zl);
531 }
532
533 printf("Delete inclusive range 0,1:\n");
534 {
535 zl = createList();
ba5b4bde 536 zl = ziplistDeleteRange(zl, 0, 2);
779deb60
PN
537 ziplistRepr(zl);
538 }
539
540 printf("Delete inclusive range 1,2:\n");
541 {
542 zl = createList();
ba5b4bde 543 zl = ziplistDeleteRange(zl, 1, 2);
779deb60
PN
544 ziplistRepr(zl);
545 }
546
547 printf("Delete with start index out of range:\n");
548 {
549 zl = createList();
ba5b4bde 550 zl = ziplistDeleteRange(zl, 5, 1);
779deb60
PN
551 ziplistRepr(zl);
552 }
553
554 printf("Delete with num overflow:\n");
555 {
556 zl = createList();
ba5b4bde 557 zl = ziplistDeleteRange(zl, 1, 5);
779deb60 558 ziplistRepr(zl);
08253bf4
PN
559 }
560
0f10458c
PN
561 printf("Delete foo while iterating:\n");
562 {
563 zl = createList();
564 p = ziplistIndex(zl, 0);
75d8978e
PN
565 while (ziplistGet(p, &entry, &elen, &value)) {
566 if (entry && strncmp("foo", entry, elen) == 0) {
0f10458c 567 printf("Delete foo\n");
75d8978e 568 zl = ziplistDelete(zl, &p);
0f10458c
PN
569 } else {
570 printf("Entry: ");
75d8978e
PN
571 if (entry) {
572 fwrite(entry,elen,1,stdout);
573 } else {
574 printf("%lld", value);
575 }
576 p = ziplistNext(p);
577 printf("\n");
0f10458c
PN
578 }
579 }
580 printf("\n");
581 ziplistRepr(zl);
c09c2c3b
PN
582 }
583
584 printf("Compare strings with ziplist entries:\n");
585 {
586 zl = createList();
587 p = ziplistIndex(zl, 0);
588 if (!ziplistCompare(p,"hello",5)) {
589 printf("ERROR\n");
590 return;
591 }
592 if (ziplistCompare(p,"hella",5)) {
593 printf("ERROR\n");
594 return;
595 }
596
597 p = ziplistIndex(zl, 3);
598 if (!ziplistCompare(p,"1024",4)) {
599 printf("ERROR\n");
600 return;
601 }
602 if (ziplistCompare(p,"1025",4)) {
603 printf("ERROR\n");
604 return;
605 }
606 printf("SUCCESS\n");
0f10458c
PN
607 }
608
11ac6ff6
PN
609 return 0;
610}
611#endif