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