]>
git.saurik.com Git - redis.git/blob - ziplist.c
fe2bfaf6ccebafa8f0d218dc9e107562a5fc2701
1 /* Memory layout of a ziplist, containing "foo", "bar", "quux":
2 * <zlbytes><zllen><len>"foo"<len>"bar"<len>"quux"
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.
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.
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.
30 #define ZIP_ENC_SHORT 1
32 #define ZIP_ENC_LLONG 3
33 #define ZIP_ENCODING(p) ((p)[0] >> 6)
35 /* Length encoding for raw entries */
36 #define ZIP_LEN_INLINE 0
37 #define ZIP_LEN_UINT16 1
38 #define ZIP_LEN_UINT32 2
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)
44 #define ZIPLIST_INCR_LENGTH(zl,incr) { \
45 if (ZIPLIST_LENGTH(zl) < (ZIP_END-1)) ZIPLIST_LENGTH(zl)+=incr; }
47 /* Return bytes needed to store integer encoded by 'encoding' */
48 static unsigned int zipEncodingSize(char encoding
) {
49 if (encoding
== ZIP_ENC_SHORT
) {
50 return sizeof(short int);
51 } else if (encoding
== ZIP_ENC_INT
) {
53 } else if (encoding
== ZIP_ENC_LLONG
) {
54 return sizeof(long long);
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. */
61 static unsigned int zipDecodeLength(unsigned char *p
, unsigned int *lensize
) {
62 unsigned char encoding
= ZIP_ENCODING(p
), lenenc
;
65 if (encoding
== ZIP_ENC_RAW
) {
66 lenenc
= (p
[0] >> 4) & 0x3;
67 if (lenenc
== ZIP_LEN_INLINE
) {
69 if (lensize
) *lensize
= 1;
70 } else if (lenenc
== ZIP_LEN_UINT16
) {
71 len
= p
[1] | (p
[2] << 8);
72 if (lensize
) *lensize
= 3;
74 len
= p
[1] | (p
[2] << 8) | (p
[3] << 16) | (p
[4] << 24);
75 if (lensize
) *lensize
= 5;
78 len
= zipEncodingSize(encoding
);
79 if (lensize
) *lensize
= 1;
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. */
86 static 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
) {
91 lenenc
= ZIP_LEN_INLINE
;
93 } else if (rawlen
<= 0xffff) {
96 lenenc
= ZIP_LEN_UINT16
;
97 buf
[1] = (rawlen
) & 0xff;
98 buf
[2] = (rawlen
>> 8) & 0xff;
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;
108 buf
[0] = (lenenc
<< 4) | (buf
[0] & 0xf);
112 /* Apparently we need to store the length in 'p' */
113 buf
[0] = (encoding
<< 6) | (buf
[0] & 0x3f);
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! */
121 static int zipTryEncoding(unsigned char *entry
, long long *v
, char *encoding
) {
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
;
133 *encoding
= ZIP_ENC_LLONG
;
141 /* Store integer 'value' at 'p', encoded as 'encoding' */
142 static void zipSaveInteger(unsigned char *p
, long long value
, char encoding
) {
146 if (encoding
== ZIP_ENC_SHORT
) {
148 memcpy(p
,&s
,sizeof(s
));
149 } else if (encoding
== ZIP_ENC_INT
) {
151 memcpy(p
,&i
,sizeof(i
));
152 } else if (encoding
== ZIP_ENC_LLONG
) {
154 memcpy(p
,&l
,sizeof(l
));
160 /* Read integer encoded as 'encoding' from 'p' */
161 static long long zipLoadInteger(unsigned char *p
, char encoding
) {
165 if (encoding
== ZIP_ENC_SHORT
) {
166 memcpy(&s
,p
,sizeof(s
));
168 } else if (encoding
== ZIP_ENC_INT
) {
169 memcpy(&i
,p
,sizeof(i
));
171 } else if (encoding
== ZIP_ENC_LLONG
) {
172 memcpy(&l
,p
,sizeof(l
));
180 /* Return the total amount used by an entry (encoded length + payload). */
181 static unsigned int zipRawEntryLength(unsigned char *p
) {
182 unsigned int lensize
, len
;
183 len
= zipDecodeLength(p
, &lensize
);
184 return lensize
+ len
;
187 /* Create a new empty ziplist. */
188 unsigned 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
;
197 /* Resize the ziplist. */
198 static unsigned char *ziplistResize(unsigned char *zl
, unsigned int len
) {
199 zl
= zrealloc(zl
,len
);
200 ZIPLIST_BYTES(zl
) = len
;
205 static unsigned char *ziplistHead(unsigned char *zl
) {
206 return zl
+ZIPLIST_HEADER_SIZE
;
209 static unsigned char *ziplistTail(unsigned char *zl
) {
210 unsigned char *p
, *q
;
211 p
= q
= ziplistHead(zl
);
212 while (*p
!= ZIP_END
) {
214 p
+= zipRawEntryLength(p
);
219 unsigned char *ziplistPush(unsigned char *zl
, unsigned char *entry
, unsigned int elen
, int where
) {
220 unsigned int curlen
= ZIPLIST_BYTES(zl
), reqlen
;
222 char encoding
= ZIP_ENC_RAW
;
225 /* See if the entry can be encoded */
226 if (zipTryEncoding(entry
,&value
,&encoding
)) {
227 reqlen
= zipEncodingSize(encoding
);
231 reqlen
+= zipEncodeLength(NULL
,encoding
,elen
);
233 /* Resize the ziplist and move if needed */
234 zl
= ziplistResize(zl
,curlen
+reqlen
);
235 if (where
== ZIPLIST_HEAD
) {
236 p
= zl
+ZIPLIST_HEADER_SIZE
;
238 /* Subtract one because of the ZIP_END bytes */
239 memmove(p
+reqlen
,p
,curlen
-ZIPLIST_HEADER_SIZE
-1);
245 /* Write the entry */
246 p
+= zipEncodeLength(p
,encoding
,elen
);
247 if (encoding
!= ZIP_ENC_RAW
) {
248 zipSaveInteger(p
,value
,encoding
);
250 memcpy(p
,entry
,elen
);
252 ZIPLIST_INCR_LENGTH(zl
,1);
256 unsigned char *ziplistPop(unsigned char *zl
, sds
*target
, int where
) {
257 unsigned int curlen
= ZIPLIST_BYTES(zl
), rawlen
;
258 unsigned int len
, lensize
;
261 if (target
) *target
= NULL
;
263 /* Get pointer to element to remove */
264 p
= (where
== ZIPLIST_HEAD
) ? ziplistHead(zl
) : ziplistTail(zl
);
265 if (*p
== ZIP_END
) return zl
;
266 len
= zipDecodeLength(p
,&lensize
);
268 if (ZIP_ENCODING(p
) == ZIP_ENC_RAW
) {
269 *target
= sdsnewlen(p
+lensize
,len
);
271 value
= zipLoadInteger(p
+lensize
,ZIP_ENCODING(p
));
272 *target
= sdscatprintf(sdsempty(), "%lld", value
);
276 /* Move list to front when popping from the head */
277 rawlen
= lensize
+len
;
278 if (where
== ZIPLIST_HEAD
) {
279 memmove(p
,p
+rawlen
,curlen
-ZIPLIST_HEADER_SIZE
-len
);
282 /* Resize and update length */
283 zl
= ziplistResize(zl
,curlen
-rawlen
);
284 ZIPLIST_INCR_LENGTH(zl
,-1);
288 /* Returns an offset to use for iterating with ziplistNext. */
289 unsigned char *ziplistIndex(unsigned char *zl
, unsigned int index
) {
290 unsigned char *p
= zl
+ZIPLIST_HEADER_SIZE
;
292 for (; i
< index
; i
++) {
293 if (*p
== ZIP_END
) break;
294 p
+= zipRawEntryLength(p
);
299 /* Return pointer to next entry in ziplist. */
300 unsigned char *ziplistNext(unsigned char *p
) {
301 return *p
== ZIP_END
? p
: p
+zipRawEntryLength(p
);
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. */
308 unsigned 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;
312 len
= zipDecodeLength(p
,&lensize
);
313 if (ZIP_ENCODING(p
) == ZIP_ENC_RAW
) {
320 *v
= zipLoadInteger(p
+lensize
,ZIP_ENCODING(p
));
326 /* Delete a range of entries from the ziplist. */
327 unsigned char *ziplistDeleteRange(unsigned char *zl
, unsigned int index
, unsigned int num
) {
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
);
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);
340 /* Resize and update length */
341 zl
= ziplistResize(zl
, ZIPLIST_BYTES(zl
)-totlen
);
342 ZIPLIST_INCR_LENGTH(zl
,-deleted
);
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. */
350 unsigned 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;
355 /* Move current tail to the new tail when there *is* a tail */
356 if (tail
> 0) memmove(*p
,*p
+len
,tail
);
358 /* Resize and update length */
359 zl
= ziplistResize(zl
, ZIPLIST_BYTES(zl
)-len
);
360 ZIPLIST_INCR_LENGTH(zl
,-1);
362 /* Store new pointer to current element in p.
363 * This needs to be done because zl can change on realloc. */
368 void ziplistRepr(unsigned char *zl
) {
369 unsigned char *p
, encoding
;
370 unsigned int l
, lsize
;
373 printf("{total bytes %d} {length %u}\n",ZIPLIST_BYTES(zl
), ZIPLIST_LENGTH(zl
));
375 while(*p
!= ZIP_END
) {
376 l
= zipDecodeLength(p
,&lsize
);
377 printf("{header %u, payload %u} ",lsize
,l
);
378 encoding
= ZIP_ENCODING(p
);
380 if (encoding
== ZIP_ENC_RAW
) {
381 fwrite(p
,l
,1,stdout
);
383 printf("%lld", zipLoadInteger(p
,encoding
));
391 #ifdef ZIPLIST_TEST_MAIN
393 unsigned char *createList() {
394 unsigned char *zl
= ziplistNew();
395 zl
= ziplistPush(zl
, (unsigned char*)"foo", 3, ZIPLIST_TAIL
);
396 zl
= ziplistPush(zl
, (unsigned char*)"quux", 4, ZIPLIST_TAIL
);
397 zl
= ziplistPush(zl
, (unsigned char*)"hello", 5, ZIPLIST_HEAD
);
398 zl
= ziplistPush(zl
, (unsigned char*)"1024", 4, ZIPLIST_TAIL
);
402 unsigned char *createIntList() {
403 unsigned char *zl
= ziplistNew();
407 zl
= ziplistPush(zl
, buf
, strlen(buf
), ZIPLIST_TAIL
);
408 sprintf(buf
, "128000");
409 zl
= ziplistPush(zl
, buf
, strlen(buf
), ZIPLIST_TAIL
);
410 sprintf(buf
, "-100");
411 zl
= ziplistPush(zl
, buf
, strlen(buf
), ZIPLIST_HEAD
);
412 sprintf(buf
, "4294967296");
413 zl
= ziplistPush(zl
, buf
, strlen(buf
), ZIPLIST_HEAD
);
414 sprintf(buf
, "non integer");
415 zl
= ziplistPush(zl
, buf
, strlen(buf
), ZIPLIST_TAIL
);
416 sprintf(buf
, "much much longer non integer");
417 zl
= ziplistPush(zl
, buf
, strlen(buf
), ZIPLIST_TAIL
);
421 int main(int argc
, char **argv
) {
422 unsigned char *zl
, *p
, *q
, *entry
;
427 zl
= createIntList();
433 zl
= ziplistPop(zl
, &s
, ZIPLIST_TAIL
);
434 printf("Pop tail: %s (length %ld)\n", s
, sdslen(s
));
437 zl
= ziplistPop(zl
, &s
, ZIPLIST_HEAD
);
438 printf("Pop head: %s (length %ld)\n", s
, sdslen(s
));
441 printf("Iterate list from 0 to end:\n");
444 p
= ziplistIndex(zl
, 0);
445 while (ziplistGet(p
, &entry
, &elen
, &value
)) {
448 fwrite(entry
,elen
,1,stdout
);
450 printf("%lld", value
);
458 printf("Iterate list from 1 to end:\n");
461 p
= ziplistIndex(zl
, 1);
462 while (ziplistGet(p
, &entry
, &elen
, &value
)) {
465 fwrite(entry
,elen
,1,stdout
);
467 printf("%lld", value
);
475 printf("Iterate list from 2 to end:\n");
478 p
= ziplistIndex(zl
, 2);
479 while (ziplistGet(p
, &entry
, &elen
, &value
)) {
482 fwrite(entry
,elen
,1,stdout
);
484 printf("%lld", value
);
492 printf("Iterate starting out of range:\n");
495 p
= ziplistIndex(zl
, 4);
496 if (!ziplistGet(p
, &entry
, &elen
, &value
)) {
497 printf("No entry\n");
504 printf("Delete inclusive range 0,0:\n");
507 zl
= ziplistDeleteRange(zl
, 0, 1);
511 printf("Delete inclusive range 0,1:\n");
514 zl
= ziplistDeleteRange(zl
, 0, 2);
518 printf("Delete inclusive range 1,2:\n");
521 zl
= ziplistDeleteRange(zl
, 1, 2);
525 printf("Delete with start index out of range:\n");
528 zl
= ziplistDeleteRange(zl
, 5, 1);
532 printf("Delete with num overflow:\n");
535 zl
= ziplistDeleteRange(zl
, 1, 5);
539 printf("Delete foo while iterating:\n");
542 p
= ziplistIndex(zl
, 0);
543 while (ziplistGet(p
, &entry
, &elen
, &value
)) {
544 if (entry
&& strncmp("foo", entry
, elen
) == 0) {
545 printf("Delete foo\n");
546 zl
= ziplistDelete(zl
, &p
);
550 fwrite(entry
,elen
,1,stdout
);
552 printf("%lld", value
);