]>
Commit | Line | Data |
---|---|---|
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 | |
dcb9cf4e PN |
26 | /* Important note: the ZIP_END value is used to depict the end of the |
27 | * ziplist structure. When a pointer contains an entry, the first couple | |
28 | * of bytes contain the encoded length of the previous entry. This length | |
29 | * is encoded as ZIP_ENC_RAW length, so the first two bits will contain 00 | |
30 | * and the byte will therefore never have a value of 255. */ | |
37fff074 | 31 | #define ZIP_END 255 |
aa549962 | 32 | #define ZIP_BIGLEN 254 |
37fff074 PN |
33 | |
34 | /* Entry encoding */ | |
35 | #define ZIP_ENC_RAW 0 | |
36 | #define ZIP_ENC_SHORT 1 | |
37 | #define ZIP_ENC_INT 2 | |
38 | #define ZIP_ENC_LLONG 3 | |
39 | #define ZIP_ENCODING(p) ((p)[0] >> 6) | |
40 | ||
41 | /* Length encoding for raw entries */ | |
42 | #define ZIP_LEN_INLINE 0 | |
43 | #define ZIP_LEN_UINT16 1 | |
44 | #define ZIP_LEN_UINT32 2 | |
45 | ||
46 | /* Utility macros */ | |
11ac6ff6 | 47 | #define ZIPLIST_BYTES(zl) (*((unsigned int*)(zl))) |
dcb9cf4e PN |
48 | #define ZIPLIST_TAIL_OFFSET(zl) (*((zl)+sizeof(unsigned int))) |
49 | #define ZIPLIST_LENGTH(zl) (*((zl)+2*sizeof(unsigned int))) | |
50 | #define ZIPLIST_HEADER_SIZE (2*sizeof(unsigned int)+1) | |
1ce81fa5 PN |
51 | #define ZIPLIST_ENTRY_HEAD(zl) ((zl)+ZIPLIST_HEADER_SIZE) |
52 | #define ZIPLIST_ENTRY_TAIL(zl) ((zl)+ZIPLIST_TAIL_OFFSET(zl)) | |
53 | #define ZIPLIST_ENTRY_END(zl) ((zl)+ZIPLIST_BYTES(zl)-1) | |
f6eb1747 | 54 | #define ZIPLIST_INCR_LENGTH(zl,incr) { \ |
aa549962 | 55 | if (ZIPLIST_LENGTH(zl) < ZIP_BIGLEN) ZIPLIST_LENGTH(zl)+=incr; } |
11ac6ff6 | 56 | |
a5456b2c PN |
57 | typedef struct zlentry { |
58 | unsigned int prevrawlensize, prevrawlen; | |
59 | unsigned int lensize, len; | |
60 | unsigned int headersize; | |
61 | unsigned char encoding; | |
0c0d0564 | 62 | unsigned char *p; |
a5456b2c PN |
63 | } zlentry; |
64 | ||
37fff074 PN |
65 | /* Return bytes needed to store integer encoded by 'encoding' */ |
66 | static unsigned int zipEncodingSize(char encoding) { | |
67 | if (encoding == ZIP_ENC_SHORT) { | |
68 | return sizeof(short int); | |
69 | } else if (encoding == ZIP_ENC_INT) { | |
70 | return sizeof(int); | |
71 | } else if (encoding == ZIP_ENC_LLONG) { | |
72 | return sizeof(long long); | |
73 | } | |
74 | assert(NULL); | |
75 | } | |
76 | ||
77 | /* Decode the encoded length pointed by 'p'. If a pointer to 'lensize' is | |
78 | * provided, it is set to the number of bytes required to encode the length. */ | |
79 | static unsigned int zipDecodeLength(unsigned char *p, unsigned int *lensize) { | |
80 | unsigned char encoding = ZIP_ENCODING(p), lenenc; | |
81 | unsigned int len; | |
82 | ||
83 | if (encoding == ZIP_ENC_RAW) { | |
84 | lenenc = (p[0] >> 4) & 0x3; | |
85 | if (lenenc == ZIP_LEN_INLINE) { | |
86 | len = p[0] & 0xf; | |
87 | if (lensize) *lensize = 1; | |
88 | } else if (lenenc == ZIP_LEN_UINT16) { | |
89 | len = p[1] | (p[2] << 8); | |
90 | if (lensize) *lensize = 3; | |
91 | } else { | |
92 | len = p[1] | (p[2] << 8) | (p[3] << 16) | (p[4] << 24); | |
93 | if (lensize) *lensize = 5; | |
94 | } | |
95 | } else { | |
96 | len = zipEncodingSize(encoding); | |
97 | if (lensize) *lensize = 1; | |
98 | } | |
99 | return len; | |
100 | } | |
101 | ||
102 | /* Encode the length 'l' writing it in 'p'. If p is NULL it just returns | |
103 | * the amount of bytes required to encode such a length. */ | |
104 | static unsigned int zipEncodeLength(unsigned char *p, char encoding, unsigned int rawlen) { | |
105 | unsigned char len = 1, lenenc, buf[5]; | |
106 | if (encoding == ZIP_ENC_RAW) { | |
107 | if (rawlen <= 0xf) { | |
108 | if (!p) return len; | |
109 | lenenc = ZIP_LEN_INLINE; | |
110 | buf[0] = rawlen; | |
111 | } else if (rawlen <= 0xffff) { | |
112 | len += 2; | |
113 | if (!p) return len; | |
114 | lenenc = ZIP_LEN_UINT16; | |
115 | buf[1] = (rawlen ) & 0xff; | |
116 | buf[2] = (rawlen >> 8) & 0xff; | |
117 | } else { | |
118 | len += 4; | |
119 | if (!p) return len; | |
120 | lenenc = ZIP_LEN_UINT32; | |
121 | buf[1] = (rawlen ) & 0xff; | |
122 | buf[2] = (rawlen >> 8) & 0xff; | |
123 | buf[3] = (rawlen >> 16) & 0xff; | |
124 | buf[4] = (rawlen >> 24) & 0xff; | |
125 | } | |
126 | buf[0] = (lenenc << 4) | (buf[0] & 0xf); | |
127 | } | |
128 | if (!p) return len; | |
129 | ||
130 | /* Apparently we need to store the length in 'p' */ | |
131 | buf[0] = (encoding << 6) | (buf[0] & 0x3f); | |
132 | memcpy(p,buf,len); | |
133 | return len; | |
134 | } | |
135 | ||
7b1f85c0 PN |
136 | /* Decode the length of the previous element stored at "p". */ |
137 | static unsigned int zipPrevDecodeLength(unsigned char *p, unsigned int *lensize) { | |
138 | unsigned int len = *p; | |
139 | if (len < ZIP_BIGLEN) { | |
140 | if (lensize) *lensize = 1; | |
141 | } else { | |
142 | if (lensize) *lensize = 1+sizeof(len); | |
143 | memcpy(&len,p+1,sizeof(len)); | |
144 | } | |
145 | return len; | |
146 | } | |
147 | ||
148 | /* Encode the length of the previous entry and write it to "p". Return the | |
149 | * number of bytes needed to encode this length if "p" is NULL. */ | |
150 | static unsigned int zipPrevEncodeLength(unsigned char *p, unsigned int len) { | |
151 | if (p == NULL) { | |
152 | return (len < ZIP_BIGLEN) ? 1 : sizeof(len)+1; | |
153 | } else { | |
154 | if (len < ZIP_BIGLEN) { | |
155 | p[0] = len; | |
156 | return 1; | |
157 | } else { | |
158 | p[0] = ZIP_BIGLEN; | |
159 | memcpy(p+1,&len,sizeof(len)); | |
160 | return 1+sizeof(len); | |
161 | } | |
162 | } | |
163 | } | |
164 | ||
dcb9cf4e PN |
165 | /* Return the difference in number of bytes needed to store the new length |
166 | * "len" on the entry pointed to by "p". */ | |
167 | static int zipPrevLenByteDiff(unsigned char *p, unsigned int len) { | |
168 | unsigned int prevlensize; | |
7b1f85c0 PN |
169 | zipPrevDecodeLength(p,&prevlensize); |
170 | return zipPrevEncodeLength(NULL,len)-prevlensize; | |
dcb9cf4e PN |
171 | } |
172 | ||
37fff074 PN |
173 | /* Check if string pointed to by 'entry' can be encoded as an integer. |
174 | * Stores the integer value in 'v' and its encoding in 'encoding'. | |
175 | * Warning: this function requires a NULL-terminated string! */ | |
a24ba809 | 176 | static int zipTryEncoding(char *entry, long long *v, char *encoding) { |
37fff074 PN |
177 | long long value; |
178 | char *eptr; | |
179 | ||
180 | if (entry[0] == '-' || (entry[0] >= '0' && entry[0] <= '9')) { | |
a24ba809 | 181 | value = strtoll(entry,&eptr,10); |
37fff074 PN |
182 | if (eptr[0] != '\0') return 0; |
183 | if (value >= SHRT_MIN && value <= SHRT_MAX) { | |
184 | *encoding = ZIP_ENC_SHORT; | |
185 | } else if (value >= INT_MIN && value <= INT_MAX) { | |
186 | *encoding = ZIP_ENC_INT; | |
187 | } else { | |
188 | *encoding = ZIP_ENC_LLONG; | |
189 | } | |
190 | *v = value; | |
191 | return 1; | |
192 | } | |
193 | return 0; | |
194 | } | |
195 | ||
196 | /* Store integer 'value' at 'p', encoded as 'encoding' */ | |
197 | static void zipSaveInteger(unsigned char *p, long long value, char encoding) { | |
198 | short int s; | |
199 | int i; | |
200 | long long l; | |
201 | if (encoding == ZIP_ENC_SHORT) { | |
202 | s = value; | |
203 | memcpy(p,&s,sizeof(s)); | |
204 | } else if (encoding == ZIP_ENC_INT) { | |
205 | i = value; | |
206 | memcpy(p,&i,sizeof(i)); | |
207 | } else if (encoding == ZIP_ENC_LLONG) { | |
208 | l = value; | |
209 | memcpy(p,&l,sizeof(l)); | |
210 | } else { | |
211 | assert(NULL); | |
212 | } | |
213 | } | |
214 | ||
215 | /* Read integer encoded as 'encoding' from 'p' */ | |
216 | static long long zipLoadInteger(unsigned char *p, char encoding) { | |
217 | short int s; | |
218 | int i; | |
219 | long long l, ret; | |
220 | if (encoding == ZIP_ENC_SHORT) { | |
221 | memcpy(&s,p,sizeof(s)); | |
222 | ret = s; | |
223 | } else if (encoding == ZIP_ENC_INT) { | |
224 | memcpy(&i,p,sizeof(i)); | |
225 | ret = i; | |
226 | } else if (encoding == ZIP_ENC_LLONG) { | |
227 | memcpy(&l,p,sizeof(l)); | |
228 | ret = l; | |
229 | } else { | |
230 | assert(NULL); | |
231 | } | |
232 | return ret; | |
233 | } | |
234 | ||
a5456b2c PN |
235 | /* Return a struct with all information about an entry. */ |
236 | static zlentry zipEntry(unsigned char *p) { | |
237 | zlentry e; | |
7b1f85c0 | 238 | e.prevrawlen = zipPrevDecodeLength(p,&e.prevrawlensize); |
a5456b2c PN |
239 | e.len = zipDecodeLength(p+e.prevrawlensize,&e.lensize); |
240 | e.headersize = e.prevrawlensize+e.lensize; | |
241 | e.encoding = ZIP_ENCODING(p+e.prevrawlensize); | |
0c0d0564 | 242 | e.p = p; |
a5456b2c PN |
243 | return e; |
244 | } | |
245 | ||
bb57b965 | 246 | /* Return the total number of bytes used by the entry at "p". */ |
37fff074 | 247 | static unsigned int zipRawEntryLength(unsigned char *p) { |
bb57b965 PN |
248 | zlentry e = zipEntry(p); |
249 | return e.headersize + e.len; | |
37fff074 PN |
250 | } |
251 | ||
11ac6ff6 PN |
252 | /* Create a new empty ziplist. */ |
253 | unsigned char *ziplistNew(void) { | |
254 | unsigned int bytes = ZIPLIST_HEADER_SIZE+1; | |
255 | unsigned char *zl = zmalloc(bytes); | |
256 | ZIPLIST_BYTES(zl) = bytes; | |
dcb9cf4e | 257 | ZIPLIST_TAIL_OFFSET(zl) = ZIPLIST_HEADER_SIZE; |
11ac6ff6 PN |
258 | ZIPLIST_LENGTH(zl) = 0; |
259 | zl[bytes-1] = ZIP_END; | |
260 | return zl; | |
261 | } | |
262 | ||
37fff074 | 263 | /* Resize the ziplist. */ |
11ac6ff6 | 264 | static unsigned char *ziplistResize(unsigned char *zl, unsigned int len) { |
37fff074 | 265 | zl = zrealloc(zl,len); |
11ac6ff6 PN |
266 | ZIPLIST_BYTES(zl) = len; |
267 | zl[len-1] = ZIP_END; | |
268 | return zl; | |
269 | } | |
270 | ||
0c0d0564 PN |
271 | /* Delete "num" entries, starting at "p". Returns pointer to the ziplist. */ |
272 | static unsigned char *__ziplistDelete(unsigned char *zl, unsigned char *p, int num) { | |
273 | unsigned int i, totlen, deleted = 0; | |
274 | int nextdiff = 0; | |
275 | zlentry first = zipEntry(p); | |
276 | for (i = 0; p[0] != ZIP_END && i < num; i++) { | |
277 | p += zipRawEntryLength(p); | |
278 | deleted++; | |
279 | } | |
280 | ||
281 | totlen = p-first.p; | |
282 | if (totlen > 0) { | |
283 | if (p[0] != ZIP_END) { | |
284 | /* Tricky: storing the prevlen in this entry might reduce or | |
285 | * increase the number of bytes needed, compared to the current | |
286 | * prevlen. Note that we can always store this length because | |
287 | * it was previously stored by an entry that is being deleted. */ | |
288 | nextdiff = zipPrevLenByteDiff(p,first.prevrawlen); | |
7b1f85c0 | 289 | zipPrevEncodeLength(p-nextdiff,first.prevrawlen); |
0c0d0564 PN |
290 | |
291 | /* Update offset for tail */ | |
292 | ZIPLIST_TAIL_OFFSET(zl) -= totlen+nextdiff; | |
293 | ||
294 | /* Move tail to the front of the ziplist */ | |
295 | memmove(first.p,p-nextdiff,ZIPLIST_BYTES(zl)-(p-zl)-1+nextdiff); | |
296 | } else { | |
297 | /* The entire tail was deleted. No need to move memory. */ | |
298 | ZIPLIST_TAIL_OFFSET(zl) = (first.p-zl)-first.prevrawlen; | |
299 | } | |
300 | ||
301 | /* Resize and update length */ | |
302 | zl = ziplistResize(zl, ZIPLIST_BYTES(zl)-totlen+nextdiff); | |
303 | ZIPLIST_INCR_LENGTH(zl,-deleted); | |
304 | } | |
305 | return zl; | |
11ac6ff6 PN |
306 | } |
307 | ||
6435c767 | 308 | /* Insert item at "p". */ |
a24ba809 | 309 | static unsigned char *__ziplistInsert(unsigned char *zl, unsigned char *p, char *s, unsigned int slen) { |
6435c767 PN |
310 | unsigned int curlen = ZIPLIST_BYTES(zl), reqlen, prevlen = 0; |
311 | unsigned int offset, nextdiff = 0; | |
312 | unsigned char *tail; | |
29b14d5f PN |
313 | char encoding = ZIP_ENC_RAW; |
314 | long long value; | |
6435c767 | 315 | zlentry entry; |
11ac6ff6 | 316 | |
6435c767 PN |
317 | /* Find out prevlen for the entry that is inserted. */ |
318 | if (p[0] != ZIP_END) { | |
319 | entry = zipEntry(p); | |
320 | prevlen = entry.prevrawlen; | |
dcb9cf4e | 321 | } else { |
1ce81fa5 | 322 | tail = ZIPLIST_ENTRY_TAIL(zl); |
6435c767 PN |
323 | if (tail[0] != ZIP_END) { |
324 | prevlen = zipRawEntryLength(tail); | |
325 | } | |
dcb9cf4e PN |
326 | } |
327 | ||
29b14d5f | 328 | /* See if the entry can be encoded */ |
6435c767 | 329 | if (zipTryEncoding(s,&value,&encoding)) { |
29b14d5f PN |
330 | reqlen = zipEncodingSize(encoding); |
331 | } else { | |
6435c767 | 332 | reqlen = slen; |
29b14d5f | 333 | } |
dcb9cf4e PN |
334 | |
335 | /* We need space for both the length of the previous entry and | |
336 | * the length of the payload. */ | |
7b1f85c0 | 337 | reqlen += zipPrevEncodeLength(NULL,prevlen); |
6435c767 PN |
338 | reqlen += zipEncodeLength(NULL,encoding,slen); |
339 | ||
340 | /* When the insert position is not equal to the tail, we need to | |
341 | * make sure that the next entry can hold this entry's length in | |
342 | * its prevlen field. */ | |
177a0a0b | 343 | nextdiff = (p[0] != ZIP_END) ? zipPrevLenByteDiff(p,reqlen) : 0; |
6435c767 PN |
344 | |
345 | /* Store offset because a realloc may change the address of zl. */ | |
346 | offset = p-zl; | |
347 | zl = ziplistResize(zl,curlen+reqlen+nextdiff); | |
348 | p = zl+offset; | |
349 | ||
350 | /* Apply memory move when necessary and update tail offset. */ | |
351 | if (p[0] != ZIP_END) { | |
352 | /* Subtract one because of the ZIP_END bytes */ | |
353 | memmove(p+reqlen,p-nextdiff,curlen-offset-1+nextdiff); | |
354 | /* Encode this entry's raw length in the next entry. */ | |
7b1f85c0 | 355 | zipPrevEncodeLength(p+reqlen,reqlen); |
6435c767 PN |
356 | /* Update offset for tail */ |
357 | ZIPLIST_TAIL_OFFSET(zl) += reqlen+nextdiff; | |
11ac6ff6 | 358 | } else { |
6435c767 PN |
359 | /* This element will be the new tail. */ |
360 | ZIPLIST_TAIL_OFFSET(zl) = p-zl; | |
dcb9cf4e PN |
361 | } |
362 | ||
11ac6ff6 | 363 | /* Write the entry */ |
7b1f85c0 | 364 | p += zipPrevEncodeLength(p,prevlen); |
6435c767 | 365 | p += zipEncodeLength(p,encoding,slen); |
29b14d5f PN |
366 | if (encoding != ZIP_ENC_RAW) { |
367 | zipSaveInteger(p,value,encoding); | |
368 | } else { | |
6435c767 | 369 | memcpy(p,s,slen); |
29b14d5f | 370 | } |
f6eb1747 | 371 | ZIPLIST_INCR_LENGTH(zl,1); |
11ac6ff6 PN |
372 | return zl; |
373 | } | |
374 | ||
a24ba809 | 375 | unsigned char *ziplistPush(unsigned char *zl, char *s, unsigned int slen, int where) { |
6435c767 | 376 | unsigned char *p; |
1ce81fa5 | 377 | p = (where == ZIPLIST_HEAD) ? ZIPLIST_ENTRY_HEAD(zl) : ZIPLIST_ENTRY_END(zl); |
6435c767 PN |
378 | return __ziplistInsert(zl,p,s,slen); |
379 | } | |
380 | ||
29b14d5f | 381 | unsigned char *ziplistPop(unsigned char *zl, sds *target, int where) { |
a5456b2c | 382 | zlentry entry; |
11ac6ff6 | 383 | unsigned char *p; |
29b14d5f PN |
384 | long long value; |
385 | if (target) *target = NULL; | |
11ac6ff6 PN |
386 | |
387 | /* Get pointer to element to remove */ | |
1ce81fa5 | 388 | p = (where == ZIPLIST_HEAD) ? ZIPLIST_ENTRY_HEAD(zl) : ZIPLIST_ENTRY_TAIL(zl); |
11ac6ff6 | 389 | if (*p == ZIP_END) return zl; |
dcb9cf4e | 390 | |
a5456b2c | 391 | entry = zipEntry(p); |
29b14d5f | 392 | if (target) { |
a5456b2c PN |
393 | if (entry.encoding == ZIP_ENC_RAW) { |
394 | *target = sdsnewlen(p+entry.headersize,entry.len); | |
29b14d5f | 395 | } else { |
a5456b2c | 396 | value = zipLoadInteger(p+entry.headersize,entry.encoding); |
29b14d5f PN |
397 | *target = sdscatprintf(sdsempty(), "%lld", value); |
398 | } | |
399 | } | |
11ac6ff6 | 400 | |
0c0d0564 | 401 | zl = __ziplistDelete(zl,p,1); |
11ac6ff6 PN |
402 | return zl; |
403 | } | |
404 | ||
c03206fd PN |
405 | /* Returns an offset to use for iterating with ziplistNext. When the given |
406 | * index is negative, the list is traversed back to front. When the list | |
407 | * doesn't contain an element at the provided index, NULL is returned. */ | |
408 | unsigned char *ziplistIndex(unsigned char *zl, int index) { | |
409 | unsigned char *p; | |
410 | zlentry entry; | |
411 | if (index < 0) { | |
412 | index = (-index)-1; | |
413 | p = ZIPLIST_ENTRY_TAIL(zl); | |
414 | if (p[0] != ZIP_END) { | |
415 | entry = zipEntry(p); | |
416 | while (entry.prevrawlen > 0 && index--) { | |
417 | p -= entry.prevrawlen; | |
418 | entry = zipEntry(p); | |
419 | } | |
420 | } | |
421 | } else { | |
422 | p = ZIPLIST_ENTRY_HEAD(zl); | |
423 | while (p[0] != ZIP_END && index--) { | |
424 | p += zipRawEntryLength(p); | |
425 | } | |
08253bf4 | 426 | } |
177a0a0b | 427 | return (p[0] == ZIP_END || index > 0) ? NULL : p; |
08253bf4 PN |
428 | } |
429 | ||
75d8978e PN |
430 | /* Return pointer to next entry in ziplist. */ |
431 | unsigned char *ziplistNext(unsigned char *p) { | |
177a0a0b | 432 | return (p[0] == ZIP_END) ? NULL : p+zipRawEntryLength(p); |
75d8978e PN |
433 | } |
434 | ||
033fb554 PN |
435 | /* Return pointer to previous entry in ziplist. */ |
436 | unsigned char *ziplistPrev(unsigned char *p) { | |
437 | zlentry entry = zipEntry(p); | |
438 | return (entry.prevrawlen == 0) ? NULL : p-entry.prevrawlen; | |
439 | } | |
440 | ||
75d8978e PN |
441 | /* Get entry pointer to by 'p' and store in either 'e' or 'v' depending |
442 | * on the encoding of the entry. 'e' is always set to NULL to be able | |
443 | * to find out whether the string pointer or the integer value was set. | |
444 | * Return 0 if 'p' points to the end of the zipmap, 1 otherwise. */ | |
a24ba809 | 445 | unsigned int ziplistGet(unsigned char *p, char **sstr, unsigned int *slen, long long *sval) { |
a5456b2c | 446 | zlentry entry; |
c03206fd | 447 | if (p == NULL || p[0] == ZIP_END) return 0; |
03e52931 | 448 | if (sstr) *sstr = NULL; |
dcb9cf4e | 449 | |
a5456b2c PN |
450 | entry = zipEntry(p); |
451 | if (entry.encoding == ZIP_ENC_RAW) { | |
03e52931 PN |
452 | if (sstr) { |
453 | *slen = entry.len; | |
a24ba809 | 454 | *sstr = (char*)p+entry.headersize; |
75d8978e PN |
455 | } |
456 | } else { | |
03e52931 PN |
457 | if (sval) { |
458 | *sval = zipLoadInteger(p+entry.headersize,entry.encoding); | |
75d8978e | 459 | } |
08253bf4 | 460 | } |
75d8978e | 461 | return 1; |
08253bf4 PN |
462 | } |
463 | ||
033fb554 PN |
464 | /* Insert an entry at "p". */ |
465 | unsigned char *ziplistInsert(unsigned char *zl, unsigned char *p, char *s, unsigned int slen) { | |
466 | return __ziplistInsert(zl,p,s,slen); | |
779deb60 PN |
467 | } |
468 | ||
0f10458c PN |
469 | /* Delete a single entry from the ziplist, pointed to by *p. |
470 | * Also update *p in place, to be able to iterate over the | |
471 | * ziplist, while deleting entries. */ | |
0f3dfa87 | 472 | unsigned char *ziplistDelete(unsigned char *zl, unsigned char **p, int direction) { |
0c0d0564 PN |
473 | unsigned int offset = *p-zl; |
474 | zl = __ziplistDelete(zl,*p,1); | |
0f10458c | 475 | |
0c0d0564 | 476 | /* Store pointer to current element in p, because ziplistDelete will |
0f3dfa87 PN |
477 | * do a realloc which might result in a different "zl"-pointer. |
478 | * When the delete direction is back to front, we might delete the last | |
479 | * entry and end up with "p" pointing to ZIP_END, so check this. */ | |
480 | if (*(zl+offset) == ZIP_END && direction == ZIPLIST_HEAD) { | |
481 | *p = ZIPLIST_ENTRY_TAIL(zl); | |
482 | } else { | |
483 | *p = zl+offset; | |
484 | } | |
0f10458c PN |
485 | return zl; |
486 | } | |
487 | ||
033fb554 PN |
488 | /* Delete a range of entries from the ziplist. */ |
489 | unsigned char *ziplistDeleteRange(unsigned char *zl, unsigned int index, unsigned int num) { | |
490 | unsigned char *p = ziplistIndex(zl,index); | |
491 | return (p == NULL) ? zl : __ziplistDelete(zl,p,num); | |
492 | } | |
493 | ||
c09c2c3b | 494 | /* Compare entry pointer to by 'p' with 'entry'. Return 1 if equal. */ |
a24ba809 | 495 | unsigned int ziplistCompare(unsigned char *p, char *sstr, unsigned int slen) { |
a5456b2c | 496 | zlentry entry; |
a24ba809 | 497 | char sencoding; |
a5456b2c | 498 | long long val, sval; |
177a0a0b | 499 | if (p[0] == ZIP_END) return 0; |
c09c2c3b | 500 | |
a5456b2c PN |
501 | entry = zipEntry(p); |
502 | if (entry.encoding == ZIP_ENC_RAW) { | |
c09c2c3b | 503 | /* Raw compare */ |
a5456b2c | 504 | if (entry.len == slen) { |
03e52931 | 505 | return memcmp(p+entry.headersize,sstr,slen) == 0; |
c09c2c3b PN |
506 | } else { |
507 | return 0; | |
508 | } | |
c4aace90 | 509 | } else { |
d593c488 | 510 | /* Try to compare encoded values */ |
03e52931 | 511 | if (zipTryEncoding(sstr,&sval,&sencoding)) { |
d593c488 PN |
512 | if (entry.encoding == sencoding) { |
513 | val = zipLoadInteger(p+entry.headersize,entry.encoding); | |
514 | return val == sval; | |
515 | } | |
c4aace90 | 516 | } |
c09c2c3b | 517 | } |
c4aace90 | 518 | return 0; |
c09c2c3b PN |
519 | } |
520 | ||
6205b463 PN |
521 | /* Return length of ziplist. */ |
522 | unsigned int ziplistLen(unsigned char *zl) { | |
523 | unsigned int len = 0; | |
524 | if (ZIPLIST_LENGTH(zl) < ZIP_BIGLEN) { | |
525 | len = ZIPLIST_LENGTH(zl); | |
526 | } else { | |
527 | unsigned char *p = zl+ZIPLIST_HEADER_SIZE; | |
528 | while (*p != ZIP_END) { | |
529 | p += zipRawEntryLength(p); | |
530 | len++; | |
531 | } | |
532 | ||
533 | /* Re-store length if small enough */ | |
534 | if (len < ZIP_BIGLEN) ZIPLIST_LENGTH(zl) = len; | |
535 | } | |
536 | return len; | |
537 | } | |
538 | ||
4812cf28 PN |
539 | /* Return size in bytes of ziplist. */ |
540 | unsigned int ziplistSize(unsigned char *zl) { | |
541 | return ZIPLIST_BYTES(zl); | |
542 | } | |
543 | ||
11ac6ff6 | 544 | void ziplistRepr(unsigned char *zl) { |
c8d9e7f4 PN |
545 | unsigned char *p; |
546 | zlentry entry; | |
11ac6ff6 | 547 | |
29b14d5f | 548 | printf("{total bytes %d} {length %u}\n",ZIPLIST_BYTES(zl), ZIPLIST_LENGTH(zl)); |
1ce81fa5 | 549 | p = ZIPLIST_ENTRY_HEAD(zl); |
11ac6ff6 | 550 | while(*p != ZIP_END) { |
c8d9e7f4 PN |
551 | entry = zipEntry(p); |
552 | printf("{offset %ld, header %u, payload %u} ",p-zl,entry.headersize,entry.len); | |
553 | p += entry.headersize; | |
554 | if (entry.encoding == ZIP_ENC_RAW) { | |
555 | fwrite(p,entry.len,1,stdout); | |
29b14d5f | 556 | } else { |
c8d9e7f4 | 557 | printf("%lld", zipLoadInteger(p,entry.encoding)); |
29b14d5f | 558 | } |
11ac6ff6 | 559 | printf("\n"); |
c8d9e7f4 | 560 | p += entry.len; |
11ac6ff6 PN |
561 | } |
562 | printf("{end}\n\n"); | |
563 | } | |
564 | ||
565 | #ifdef ZIPLIST_TEST_MAIN | |
11ac6ff6 | 566 | |
08253bf4 PN |
567 | unsigned char *createList() { |
568 | unsigned char *zl = ziplistNew(); | |
a24ba809 PN |
569 | zl = ziplistPush(zl, "foo", 3, ZIPLIST_TAIL); |
570 | zl = ziplistPush(zl, "quux", 4, ZIPLIST_TAIL); | |
571 | zl = ziplistPush(zl, "hello", 5, ZIPLIST_HEAD); | |
572 | zl = ziplistPush(zl, "1024", 4, ZIPLIST_TAIL); | |
08253bf4 PN |
573 | return zl; |
574 | } | |
575 | ||
29b14d5f PN |
576 | unsigned char *createIntList() { |
577 | unsigned char *zl = ziplistNew(); | |
578 | char buf[32]; | |
579 | ||
580 | sprintf(buf, "100"); | |
581 | zl = ziplistPush(zl, buf, strlen(buf), ZIPLIST_TAIL); | |
582 | sprintf(buf, "128000"); | |
583 | zl = ziplistPush(zl, buf, strlen(buf), ZIPLIST_TAIL); | |
584 | sprintf(buf, "-100"); | |
585 | zl = ziplistPush(zl, buf, strlen(buf), ZIPLIST_HEAD); | |
586 | sprintf(buf, "4294967296"); | |
587 | zl = ziplistPush(zl, buf, strlen(buf), ZIPLIST_HEAD); | |
588 | sprintf(buf, "non integer"); | |
589 | zl = ziplistPush(zl, buf, strlen(buf), ZIPLIST_TAIL); | |
590 | sprintf(buf, "much much longer non integer"); | |
591 | zl = ziplistPush(zl, buf, strlen(buf), ZIPLIST_TAIL); | |
592 | return zl; | |
593 | } | |
594 | ||
08253bf4 | 595 | int main(int argc, char **argv) { |
a24ba809 PN |
596 | unsigned char *zl, *p; |
597 | char *entry; | |
335d16bc | 598 | unsigned int elen; |
75d8978e | 599 | long long value; |
08253bf4 PN |
600 | sds s; |
601 | ||
29b14d5f PN |
602 | zl = createIntList(); |
603 | ziplistRepr(zl); | |
604 | ||
08253bf4 | 605 | zl = createList(); |
11ac6ff6 PN |
606 | ziplistRepr(zl); |
607 | ||
608 | zl = ziplistPop(zl, &s, ZIPLIST_TAIL); | |
609 | printf("Pop tail: %s (length %ld)\n", s, sdslen(s)); | |
610 | ziplistRepr(zl); | |
611 | ||
612 | zl = ziplistPop(zl, &s, ZIPLIST_HEAD); | |
613 | printf("Pop head: %s (length %ld)\n", s, sdslen(s)); | |
614 | ziplistRepr(zl); | |
615 | ||
dcb9cf4e PN |
616 | zl = ziplistPop(zl, &s, ZIPLIST_TAIL); |
617 | printf("Pop tail: %s (length %ld)\n", s, sdslen(s)); | |
618 | ziplistRepr(zl); | |
619 | ||
620 | zl = ziplistPop(zl, &s, ZIPLIST_TAIL); | |
621 | printf("Pop tail: %s (length %ld)\n", s, sdslen(s)); | |
622 | ziplistRepr(zl); | |
623 | ||
c03206fd PN |
624 | printf("Get element at index 3:\n"); |
625 | { | |
626 | zl = createList(); | |
627 | p = ziplistIndex(zl, 3); | |
628 | if (!ziplistGet(p, &entry, &elen, &value)) { | |
629 | printf("ERROR: Could not access index 3\n"); | |
630 | return 1; | |
631 | } | |
632 | if (entry) { | |
633 | fwrite(entry,elen,1,stdout); | |
634 | printf("\n"); | |
635 | } else { | |
636 | printf("%lld\n", value); | |
637 | } | |
638 | printf("\n"); | |
639 | } | |
640 | ||
641 | printf("Get element at index 4 (out of range):\n"); | |
642 | { | |
643 | zl = createList(); | |
644 | p = ziplistIndex(zl, 4); | |
645 | if (p == NULL) { | |
646 | printf("No entry\n"); | |
647 | } else { | |
648 | printf("ERROR: Out of range index should return NULL, returned offset: %ld\n", p-zl); | |
649 | return 1; | |
650 | } | |
651 | printf("\n"); | |
652 | } | |
653 | ||
654 | printf("Get element at index -1 (last element):\n"); | |
655 | { | |
656 | zl = createList(); | |
657 | p = ziplistIndex(zl, -1); | |
658 | if (!ziplistGet(p, &entry, &elen, &value)) { | |
659 | printf("ERROR: Could not access index -1\n"); | |
660 | return 1; | |
661 | } | |
662 | if (entry) { | |
663 | fwrite(entry,elen,1,stdout); | |
664 | printf("\n"); | |
665 | } else { | |
666 | printf("%lld\n", value); | |
667 | } | |
668 | printf("\n"); | |
669 | } | |
670 | ||
671 | printf("Get element at index -4 (first element):\n"); | |
672 | { | |
673 | zl = createList(); | |
674 | p = ziplistIndex(zl, -4); | |
675 | if (!ziplistGet(p, &entry, &elen, &value)) { | |
676 | printf("ERROR: Could not access index -4\n"); | |
677 | return 1; | |
678 | } | |
679 | if (entry) { | |
680 | fwrite(entry,elen,1,stdout); | |
681 | printf("\n"); | |
682 | } else { | |
683 | printf("%lld\n", value); | |
684 | } | |
685 | printf("\n"); | |
686 | } | |
687 | ||
688 | printf("Get element at index -5 (reverse out of range):\n"); | |
689 | { | |
690 | zl = createList(); | |
691 | p = ziplistIndex(zl, -5); | |
692 | if (p == NULL) { | |
693 | printf("No entry\n"); | |
694 | } else { | |
695 | printf("ERROR: Out of range index should return NULL, returned offset: %ld\n", p-zl); | |
696 | return 1; | |
697 | } | |
698 | printf("\n"); | |
699 | } | |
700 | ||
08253bf4 PN |
701 | printf("Iterate list from 0 to end:\n"); |
702 | { | |
703 | zl = createList(); | |
704 | p = ziplistIndex(zl, 0); | |
75d8978e | 705 | while (ziplistGet(p, &entry, &elen, &value)) { |
335d16bc | 706 | printf("Entry: "); |
75d8978e PN |
707 | if (entry) { |
708 | fwrite(entry,elen,1,stdout); | |
709 | } else { | |
710 | printf("%lld", value); | |
711 | } | |
712 | p = ziplistNext(p); | |
713 | printf("\n"); | |
08253bf4 PN |
714 | } |
715 | printf("\n"); | |
716 | } | |
717 | ||
718 | printf("Iterate list from 1 to end:\n"); | |
719 | { | |
720 | zl = createList(); | |
721 | p = ziplistIndex(zl, 1); | |
75d8978e | 722 | while (ziplistGet(p, &entry, &elen, &value)) { |
335d16bc | 723 | printf("Entry: "); |
75d8978e PN |
724 | if (entry) { |
725 | fwrite(entry,elen,1,stdout); | |
726 | } else { | |
727 | printf("%lld", value); | |
728 | } | |
729 | p = ziplistNext(p); | |
730 | printf("\n"); | |
08253bf4 PN |
731 | } |
732 | printf("\n"); | |
733 | } | |
734 | ||
735 | printf("Iterate list from 2 to end:\n"); | |
736 | { | |
737 | zl = createList(); | |
738 | p = ziplistIndex(zl, 2); | |
75d8978e | 739 | while (ziplistGet(p, &entry, &elen, &value)) { |
335d16bc | 740 | printf("Entry: "); |
75d8978e PN |
741 | if (entry) { |
742 | fwrite(entry,elen,1,stdout); | |
743 | } else { | |
744 | printf("%lld", value); | |
745 | } | |
746 | p = ziplistNext(p); | |
747 | printf("\n"); | |
08253bf4 PN |
748 | } |
749 | printf("\n"); | |
750 | } | |
751 | ||
752 | printf("Iterate starting out of range:\n"); | |
753 | { | |
754 | zl = createList(); | |
75d8978e PN |
755 | p = ziplistIndex(zl, 4); |
756 | if (!ziplistGet(p, &entry, &elen, &value)) { | |
08253bf4 PN |
757 | printf("No entry\n"); |
758 | } else { | |
759 | printf("ERROR\n"); | |
760 | } | |
779deb60 PN |
761 | printf("\n"); |
762 | } | |
763 | ||
0f3dfa87 PN |
764 | printf("Iterate from back to front:\n"); |
765 | { | |
766 | zl = createList(); | |
767 | p = ziplistIndex(zl, -1); | |
768 | while (ziplistGet(p, &entry, &elen, &value)) { | |
769 | printf("Entry: "); | |
770 | if (entry) { | |
771 | fwrite(entry,elen,1,stdout); | |
772 | } else { | |
773 | printf("%lld", value); | |
774 | } | |
775 | p = ziplistPrev(p); | |
776 | printf("\n"); | |
777 | } | |
778 | printf("\n"); | |
779 | } | |
780 | ||
781 | printf("Iterate from back to front, deleting all items:\n"); | |
782 | { | |
783 | zl = createList(); | |
784 | p = ziplistIndex(zl, -1); | |
785 | while (ziplistGet(p, &entry, &elen, &value)) { | |
786 | printf("Entry: "); | |
787 | if (entry) { | |
788 | fwrite(entry,elen,1,stdout); | |
789 | } else { | |
790 | printf("%lld", value); | |
791 | } | |
792 | zl = ziplistDelete(zl, &p, ZIPLIST_HEAD); | |
793 | printf("\n"); | |
794 | } | |
795 | printf("\n"); | |
796 | } | |
797 | ||
779deb60 PN |
798 | printf("Delete inclusive range 0,0:\n"); |
799 | { | |
800 | zl = createList(); | |
ba5b4bde | 801 | zl = ziplistDeleteRange(zl, 0, 1); |
779deb60 PN |
802 | ziplistRepr(zl); |
803 | } | |
804 | ||
805 | printf("Delete inclusive range 0,1:\n"); | |
806 | { | |
807 | zl = createList(); | |
ba5b4bde | 808 | zl = ziplistDeleteRange(zl, 0, 2); |
779deb60 PN |
809 | ziplistRepr(zl); |
810 | } | |
811 | ||
812 | printf("Delete inclusive range 1,2:\n"); | |
813 | { | |
814 | zl = createList(); | |
ba5b4bde | 815 | zl = ziplistDeleteRange(zl, 1, 2); |
779deb60 PN |
816 | ziplistRepr(zl); |
817 | } | |
818 | ||
819 | printf("Delete with start index out of range:\n"); | |
820 | { | |
821 | zl = createList(); | |
ba5b4bde | 822 | zl = ziplistDeleteRange(zl, 5, 1); |
779deb60 PN |
823 | ziplistRepr(zl); |
824 | } | |
825 | ||
826 | printf("Delete with num overflow:\n"); | |
827 | { | |
828 | zl = createList(); | |
ba5b4bde | 829 | zl = ziplistDeleteRange(zl, 1, 5); |
779deb60 | 830 | ziplistRepr(zl); |
08253bf4 PN |
831 | } |
832 | ||
0f10458c PN |
833 | printf("Delete foo while iterating:\n"); |
834 | { | |
835 | zl = createList(); | |
836 | p = ziplistIndex(zl, 0); | |
75d8978e PN |
837 | while (ziplistGet(p, &entry, &elen, &value)) { |
838 | if (entry && strncmp("foo", entry, elen) == 0) { | |
0f10458c | 839 | printf("Delete foo\n"); |
0f3dfa87 | 840 | zl = ziplistDelete(zl, &p, ZIPLIST_TAIL); |
0f10458c PN |
841 | } else { |
842 | printf("Entry: "); | |
75d8978e PN |
843 | if (entry) { |
844 | fwrite(entry,elen,1,stdout); | |
845 | } else { | |
846 | printf("%lld", value); | |
847 | } | |
848 | p = ziplistNext(p); | |
849 | printf("\n"); | |
0f10458c PN |
850 | } |
851 | } | |
852 | printf("\n"); | |
853 | ziplistRepr(zl); | |
c09c2c3b PN |
854 | } |
855 | ||
856 | printf("Compare strings with ziplist entries:\n"); | |
857 | { | |
858 | zl = createList(); | |
859 | p = ziplistIndex(zl, 0); | |
860 | if (!ziplistCompare(p,"hello",5)) { | |
dcb9cf4e | 861 | printf("ERROR: not \"hello\"\n"); |
a24ba809 | 862 | return 1; |
c09c2c3b PN |
863 | } |
864 | if (ziplistCompare(p,"hella",5)) { | |
dcb9cf4e | 865 | printf("ERROR: \"hella\"\n"); |
a24ba809 | 866 | return 1; |
c09c2c3b PN |
867 | } |
868 | ||
869 | p = ziplistIndex(zl, 3); | |
870 | if (!ziplistCompare(p,"1024",4)) { | |
dcb9cf4e | 871 | printf("ERROR: not \"1024\"\n"); |
a24ba809 | 872 | return 1; |
c09c2c3b PN |
873 | } |
874 | if (ziplistCompare(p,"1025",4)) { | |
dcb9cf4e | 875 | printf("ERROR: \"1025\"\n"); |
a24ba809 | 876 | return 1; |
c09c2c3b PN |
877 | } |
878 | printf("SUCCESS\n"); | |
0f10458c PN |
879 | } |
880 | ||
11ac6ff6 PN |
881 | return 0; |
882 | } | |
883 | #endif |