]> git.saurik.com Git - redis.git/blob - ziplist.c
add function to retrieve ziplist size in bytes
[redis.git] / ziplist.c
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>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <assert.h>
21 #include <limits.h>
22 #include "zmalloc.h"
23 #include "sds.h"
24 #include "ziplist.h"
25
26 #define ZIP_END 255
27 #define ZIP_BIGLEN 254
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 */
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)
45 #define ZIPLIST_INCR_LENGTH(zl,incr) { \
46 if (ZIPLIST_LENGTH(zl) < ZIP_BIGLEN) ZIPLIST_LENGTH(zl)+=incr; }
47
48 /* Return bytes needed to store integer encoded by 'encoding' */
49 static 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. */
62 static 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. */
87 static 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! */
122 static 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' */
143 static 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' */
162 static 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). */
182 static unsigned int zipRawEntryLength(unsigned char *p) {
183 unsigned int lensize, len;
184 len = zipDecodeLength(p, &lensize);
185 return lensize + len;
186 }
187
188 /* Create a new empty ziplist. */
189 unsigned 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
198 /* Resize the ziplist. */
199 static unsigned char *ziplistResize(unsigned char *zl, unsigned int len) {
200 zl = zrealloc(zl,len);
201 ZIPLIST_BYTES(zl) = len;
202 zl[len-1] = ZIP_END;
203 return zl;
204 }
205
206 static unsigned char *ziplistHead(unsigned char *zl) {
207 return zl+ZIPLIST_HEADER_SIZE;
208 }
209
210 static 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
220 unsigned char *ziplistPush(unsigned char *zl, unsigned char *entry, unsigned int elen, int where) {
221 unsigned int curlen = ZIPLIST_BYTES(zl), reqlen;
222 unsigned char *p;
223 char encoding = ZIP_ENC_RAW;
224 long long value;
225
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);
233
234 /* Resize the ziplist and move if needed */
235 zl = ziplistResize(zl,curlen+reqlen);
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
246 /* Write the entry */
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 }
253 ZIPLIST_INCR_LENGTH(zl,1);
254 return zl;
255 }
256
257 unsigned char *ziplistPop(unsigned char *zl, sds *target, int where) {
258 unsigned int curlen = ZIPLIST_BYTES(zl), rawlen;
259 unsigned int len, lensize;
260 unsigned char *p;
261 long long value;
262 if (target) *target = NULL;
263
264 /* Get pointer to element to remove */
265 p = (where == ZIPLIST_HEAD) ? ziplistHead(zl) : ziplistTail(zl);
266 if (*p == ZIP_END) return zl;
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 }
276
277 /* Move list to front when popping from the head */
278 rawlen = lensize+len;
279 if (where == ZIPLIST_HEAD) {
280 memmove(p,p+rawlen,curlen-ZIPLIST_HEADER_SIZE-len);
281 }
282
283 /* Resize and update length */
284 zl = ziplistResize(zl,curlen-rawlen);
285 ZIPLIST_INCR_LENGTH(zl,-1);
286 return zl;
287 }
288
289 /* Returns an offset to use for iterating with ziplistNext. */
290 unsigned 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
300 /* Return pointer to next entry in ziplist. */
301 unsigned 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. */
309 unsigned 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 }
323 }
324 return 1;
325 }
326
327 /* Delete a range of entries from the ziplist. */
328 unsigned char *ziplistDeleteRange(unsigned char *zl, unsigned int index, unsigned int num) {
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);
343 ZIPLIST_INCR_LENGTH(zl,-deleted);
344 }
345 return zl;
346 }
347
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. */
351 unsigned 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);
361 ZIPLIST_INCR_LENGTH(zl,-1);
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
369 /* Compare entry pointer to by 'p' with 'entry'. Return 1 if equal. */
370 unsigned 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 (ZIP_ENCODING(p) == ZIP_ENC_RAW) {
378 /* Raw compare */
379 if (zlen == elen) {
380 return memcmp(p+lensize,entry,elen) == 0;
381 } else {
382 return 0;
383 }
384 } else {
385 if (zipTryEncoding(entry,&eval,&encoding)) {
386 /* Do integer compare */
387 zval = zipLoadInteger(p+lensize,ZIP_ENCODING(p));
388 return zval == eval;
389 } else {
390 /* Ziplist entry is integer encoded, but given entry is not. */
391 }
392 }
393 return 0;
394 }
395
396 /* Return length of ziplist. */
397 unsigned int ziplistLen(unsigned char *zl) {
398 unsigned int len = 0;
399 if (ZIPLIST_LENGTH(zl) < ZIP_BIGLEN) {
400 len = ZIPLIST_LENGTH(zl);
401 } else {
402 unsigned char *p = zl+ZIPLIST_HEADER_SIZE;
403 while (*p != ZIP_END) {
404 p += zipRawEntryLength(p);
405 len++;
406 }
407
408 /* Re-store length if small enough */
409 if (len < ZIP_BIGLEN) ZIPLIST_LENGTH(zl) = len;
410 }
411 return len;
412 }
413
414 /* Return size in bytes of ziplist. */
415 unsigned int ziplistSize(unsigned char *zl) {
416 return ZIPLIST_BYTES(zl);
417 }
418
419 void ziplistRepr(unsigned char *zl) {
420 unsigned char *p, encoding;
421 unsigned int l, lsize;
422 long long value;
423
424 printf("{total bytes %d} {length %u}\n",ZIPLIST_BYTES(zl), ZIPLIST_LENGTH(zl));
425 p = ziplistHead(zl);
426 while(*p != ZIP_END) {
427 l = zipDecodeLength(p,&lsize);
428 printf("{header %u, payload %u} ",lsize,l);
429 encoding = ZIP_ENCODING(p);
430 p += lsize;
431 if (encoding == ZIP_ENC_RAW) {
432 fwrite(p,l,1,stdout);
433 } else {
434 printf("%lld", zipLoadInteger(p,encoding));
435 }
436 printf("\n");
437 p += l;
438 }
439 printf("{end}\n\n");
440 }
441
442 #ifdef ZIPLIST_TEST_MAIN
443
444 unsigned char *createList() {
445 unsigned char *zl = ziplistNew();
446 zl = ziplistPush(zl, (unsigned char*)"foo", 3, ZIPLIST_TAIL);
447 zl = ziplistPush(zl, (unsigned char*)"quux", 4, ZIPLIST_TAIL);
448 zl = ziplistPush(zl, (unsigned char*)"hello", 5, ZIPLIST_HEAD);
449 zl = ziplistPush(zl, (unsigned char*)"1024", 4, ZIPLIST_TAIL);
450 return zl;
451 }
452
453 unsigned char *createIntList() {
454 unsigned char *zl = ziplistNew();
455 char buf[32];
456
457 sprintf(buf, "100");
458 zl = ziplistPush(zl, buf, strlen(buf), ZIPLIST_TAIL);
459 sprintf(buf, "128000");
460 zl = ziplistPush(zl, buf, strlen(buf), ZIPLIST_TAIL);
461 sprintf(buf, "-100");
462 zl = ziplistPush(zl, buf, strlen(buf), ZIPLIST_HEAD);
463 sprintf(buf, "4294967296");
464 zl = ziplistPush(zl, buf, strlen(buf), ZIPLIST_HEAD);
465 sprintf(buf, "non integer");
466 zl = ziplistPush(zl, buf, strlen(buf), ZIPLIST_TAIL);
467 sprintf(buf, "much much longer non integer");
468 zl = ziplistPush(zl, buf, strlen(buf), ZIPLIST_TAIL);
469 return zl;
470 }
471
472 int main(int argc, char **argv) {
473 unsigned char *zl, *p, *q, *entry;
474 unsigned int elen;
475 long long value;
476 sds s;
477
478 zl = createIntList();
479 ziplistRepr(zl);
480
481 zl = createList();
482 ziplistRepr(zl);
483
484 zl = ziplistPop(zl, &s, ZIPLIST_TAIL);
485 printf("Pop tail: %s (length %ld)\n", s, sdslen(s));
486 ziplistRepr(zl);
487
488 zl = ziplistPop(zl, &s, ZIPLIST_HEAD);
489 printf("Pop head: %s (length %ld)\n", s, sdslen(s));
490 ziplistRepr(zl);
491
492 printf("Iterate list from 0 to end:\n");
493 {
494 zl = createList();
495 p = ziplistIndex(zl, 0);
496 while (ziplistGet(p, &entry, &elen, &value)) {
497 printf("Entry: ");
498 if (entry) {
499 fwrite(entry,elen,1,stdout);
500 } else {
501 printf("%lld", value);
502 }
503 p = ziplistNext(p);
504 printf("\n");
505 }
506 printf("\n");
507 }
508
509 printf("Iterate list from 1 to end:\n");
510 {
511 zl = createList();
512 p = ziplistIndex(zl, 1);
513 while (ziplistGet(p, &entry, &elen, &value)) {
514 printf("Entry: ");
515 if (entry) {
516 fwrite(entry,elen,1,stdout);
517 } else {
518 printf("%lld", value);
519 }
520 p = ziplistNext(p);
521 printf("\n");
522 }
523 printf("\n");
524 }
525
526 printf("Iterate list from 2 to end:\n");
527 {
528 zl = createList();
529 p = ziplistIndex(zl, 2);
530 while (ziplistGet(p, &entry, &elen, &value)) {
531 printf("Entry: ");
532 if (entry) {
533 fwrite(entry,elen,1,stdout);
534 } else {
535 printf("%lld", value);
536 }
537 p = ziplistNext(p);
538 printf("\n");
539 }
540 printf("\n");
541 }
542
543 printf("Iterate starting out of range:\n");
544 {
545 zl = createList();
546 p = ziplistIndex(zl, 4);
547 if (!ziplistGet(p, &entry, &elen, &value)) {
548 printf("No entry\n");
549 } else {
550 printf("ERROR\n");
551 }
552 printf("\n");
553 }
554
555 printf("Delete inclusive range 0,0:\n");
556 {
557 zl = createList();
558 zl = ziplistDeleteRange(zl, 0, 1);
559 ziplistRepr(zl);
560 }
561
562 printf("Delete inclusive range 0,1:\n");
563 {
564 zl = createList();
565 zl = ziplistDeleteRange(zl, 0, 2);
566 ziplistRepr(zl);
567 }
568
569 printf("Delete inclusive range 1,2:\n");
570 {
571 zl = createList();
572 zl = ziplistDeleteRange(zl, 1, 2);
573 ziplistRepr(zl);
574 }
575
576 printf("Delete with start index out of range:\n");
577 {
578 zl = createList();
579 zl = ziplistDeleteRange(zl, 5, 1);
580 ziplistRepr(zl);
581 }
582
583 printf("Delete with num overflow:\n");
584 {
585 zl = createList();
586 zl = ziplistDeleteRange(zl, 1, 5);
587 ziplistRepr(zl);
588 }
589
590 printf("Delete foo while iterating:\n");
591 {
592 zl = createList();
593 p = ziplistIndex(zl, 0);
594 while (ziplistGet(p, &entry, &elen, &value)) {
595 if (entry && strncmp("foo", entry, elen) == 0) {
596 printf("Delete foo\n");
597 zl = ziplistDelete(zl, &p);
598 } else {
599 printf("Entry: ");
600 if (entry) {
601 fwrite(entry,elen,1,stdout);
602 } else {
603 printf("%lld", value);
604 }
605 p = ziplistNext(p);
606 printf("\n");
607 }
608 }
609 printf("\n");
610 ziplistRepr(zl);
611 }
612
613 printf("Compare strings with ziplist entries:\n");
614 {
615 zl = createList();
616 p = ziplistIndex(zl, 0);
617 if (!ziplistCompare(p,"hello",5)) {
618 printf("ERROR\n");
619 return;
620 }
621 if (ziplistCompare(p,"hella",5)) {
622 printf("ERROR\n");
623 return;
624 }
625
626 p = ziplistIndex(zl, 3);
627 if (!ziplistCompare(p,"1024",4)) {
628 printf("ERROR\n");
629 return;
630 }
631 if (ziplistCompare(p,"1025",4)) {
632 printf("ERROR\n");
633 return;
634 }
635 printf("SUCCESS\n");
636 }
637
638 return 0;
639 }
640 #endif