]>
Commit | Line | Data |
---|---|---|
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 | /* 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. */ | |
31 | #define ZIP_END 255 | |
32 | #define ZIP_BIGLEN 254 | |
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 */ | |
47 | #define ZIPLIST_BYTES(zl) (*((unsigned int*)(zl))) | |
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) | |
51 | #define ZIPLIST_INCR_LENGTH(zl,incr) { \ | |
52 | if (ZIPLIST_LENGTH(zl) < ZIP_BIGLEN) ZIPLIST_LENGTH(zl)+=incr; } | |
53 | ||
54 | typedef struct zlentry { | |
55 | unsigned int prevrawlensize, prevrawlen; | |
56 | unsigned int lensize, len; | |
57 | unsigned int headersize; | |
58 | unsigned char encoding; | |
59 | } zlentry; | |
60 | ||
61 | /* Return bytes needed to store integer encoded by 'encoding' */ | |
62 | static unsigned int zipEncodingSize(char encoding) { | |
63 | if (encoding == ZIP_ENC_SHORT) { | |
64 | return sizeof(short int); | |
65 | } else if (encoding == ZIP_ENC_INT) { | |
66 | return sizeof(int); | |
67 | } else if (encoding == ZIP_ENC_LLONG) { | |
68 | return sizeof(long long); | |
69 | } | |
70 | assert(NULL); | |
71 | } | |
72 | ||
73 | /* Decode the encoded length pointed by 'p'. If a pointer to 'lensize' is | |
74 | * provided, it is set to the number of bytes required to encode the length. */ | |
75 | static unsigned int zipDecodeLength(unsigned char *p, unsigned int *lensize) { | |
76 | unsigned char encoding = ZIP_ENCODING(p), lenenc; | |
77 | unsigned int len; | |
78 | ||
79 | if (encoding == ZIP_ENC_RAW) { | |
80 | lenenc = (p[0] >> 4) & 0x3; | |
81 | if (lenenc == ZIP_LEN_INLINE) { | |
82 | len = p[0] & 0xf; | |
83 | if (lensize) *lensize = 1; | |
84 | } else if (lenenc == ZIP_LEN_UINT16) { | |
85 | len = p[1] | (p[2] << 8); | |
86 | if (lensize) *lensize = 3; | |
87 | } else { | |
88 | len = p[1] | (p[2] << 8) | (p[3] << 16) | (p[4] << 24); | |
89 | if (lensize) *lensize = 5; | |
90 | } | |
91 | } else { | |
92 | len = zipEncodingSize(encoding); | |
93 | if (lensize) *lensize = 1; | |
94 | } | |
95 | return len; | |
96 | } | |
97 | ||
98 | /* Encode the length 'l' writing it in 'p'. If p is NULL it just returns | |
99 | * the amount of bytes required to encode such a length. */ | |
100 | static unsigned int zipEncodeLength(unsigned char *p, char encoding, unsigned int rawlen) { | |
101 | unsigned char len = 1, lenenc, buf[5]; | |
102 | if (encoding == ZIP_ENC_RAW) { | |
103 | if (rawlen <= 0xf) { | |
104 | if (!p) return len; | |
105 | lenenc = ZIP_LEN_INLINE; | |
106 | buf[0] = rawlen; | |
107 | } else if (rawlen <= 0xffff) { | |
108 | len += 2; | |
109 | if (!p) return len; | |
110 | lenenc = ZIP_LEN_UINT16; | |
111 | buf[1] = (rawlen ) & 0xff; | |
112 | buf[2] = (rawlen >> 8) & 0xff; | |
113 | } else { | |
114 | len += 4; | |
115 | if (!p) return len; | |
116 | lenenc = ZIP_LEN_UINT32; | |
117 | buf[1] = (rawlen ) & 0xff; | |
118 | buf[2] = (rawlen >> 8) & 0xff; | |
119 | buf[3] = (rawlen >> 16) & 0xff; | |
120 | buf[4] = (rawlen >> 24) & 0xff; | |
121 | } | |
122 | buf[0] = (lenenc << 4) | (buf[0] & 0xf); | |
123 | } | |
124 | if (!p) return len; | |
125 | ||
126 | /* Apparently we need to store the length in 'p' */ | |
127 | buf[0] = (encoding << 6) | (buf[0] & 0x3f); | |
128 | memcpy(p,buf,len); | |
129 | return len; | |
130 | } | |
131 | ||
132 | /* Return the difference in number of bytes needed to store the new length | |
133 | * "len" on the entry pointed to by "p". */ | |
134 | static int zipPrevLenByteDiff(unsigned char *p, unsigned int len) { | |
135 | unsigned int prevlensize; | |
136 | zipDecodeLength(p,&prevlensize); | |
137 | return zipEncodeLength(NULL,ZIP_ENC_RAW,len)-prevlensize; | |
138 | } | |
139 | ||
140 | /* Check if string pointed to by 'entry' can be encoded as an integer. | |
141 | * Stores the integer value in 'v' and its encoding in 'encoding'. | |
142 | * Warning: this function requires a NULL-terminated string! */ | |
143 | static int zipTryEncoding(unsigned char *entry, long long *v, char *encoding) { | |
144 | long long value; | |
145 | char *eptr; | |
146 | ||
147 | if (entry[0] == '-' || (entry[0] >= '0' && entry[0] <= '9')) { | |
148 | value = strtoll((char*)entry,&eptr,10); | |
149 | if (eptr[0] != '\0') return 0; | |
150 | if (value >= SHRT_MIN && value <= SHRT_MAX) { | |
151 | *encoding = ZIP_ENC_SHORT; | |
152 | } else if (value >= INT_MIN && value <= INT_MAX) { | |
153 | *encoding = ZIP_ENC_INT; | |
154 | } else { | |
155 | *encoding = ZIP_ENC_LLONG; | |
156 | } | |
157 | *v = value; | |
158 | return 1; | |
159 | } | |
160 | return 0; | |
161 | } | |
162 | ||
163 | /* Store integer 'value' at 'p', encoded as 'encoding' */ | |
164 | static void zipSaveInteger(unsigned char *p, long long value, char encoding) { | |
165 | short int s; | |
166 | int i; | |
167 | long long l; | |
168 | if (encoding == ZIP_ENC_SHORT) { | |
169 | s = value; | |
170 | memcpy(p,&s,sizeof(s)); | |
171 | } else if (encoding == ZIP_ENC_INT) { | |
172 | i = value; | |
173 | memcpy(p,&i,sizeof(i)); | |
174 | } else if (encoding == ZIP_ENC_LLONG) { | |
175 | l = value; | |
176 | memcpy(p,&l,sizeof(l)); | |
177 | } else { | |
178 | assert(NULL); | |
179 | } | |
180 | } | |
181 | ||
182 | /* Read integer encoded as 'encoding' from 'p' */ | |
183 | static long long zipLoadInteger(unsigned char *p, char encoding) { | |
184 | short int s; | |
185 | int i; | |
186 | long long l, ret; | |
187 | if (encoding == ZIP_ENC_SHORT) { | |
188 | memcpy(&s,p,sizeof(s)); | |
189 | ret = s; | |
190 | } else if (encoding == ZIP_ENC_INT) { | |
191 | memcpy(&i,p,sizeof(i)); | |
192 | ret = i; | |
193 | } else if (encoding == ZIP_ENC_LLONG) { | |
194 | memcpy(&l,p,sizeof(l)); | |
195 | ret = l; | |
196 | } else { | |
197 | assert(NULL); | |
198 | } | |
199 | return ret; | |
200 | } | |
201 | ||
202 | /* Return a struct with all information about an entry. */ | |
203 | static zlentry zipEntry(unsigned char *p) { | |
204 | zlentry e; | |
205 | e.prevrawlen = zipDecodeLength(p,&e.prevrawlensize); | |
206 | e.len = zipDecodeLength(p+e.prevrawlensize,&e.lensize); | |
207 | e.headersize = e.prevrawlensize+e.lensize; | |
208 | e.encoding = ZIP_ENCODING(p+e.prevrawlensize); | |
209 | return e; | |
210 | } | |
211 | ||
212 | /* Return the total amount used by an entry (encoded length + payload). */ | |
213 | static unsigned int zipRawEntryLength(unsigned char *p) { | |
214 | unsigned int prevlensize, lensize, len; | |
215 | /* Byte-size of encoded length of previous entry */ | |
216 | zipDecodeLength(p,&prevlensize); | |
217 | /* Encoded length of this entry's payload */ | |
218 | len = zipDecodeLength(p+prevlensize, &lensize); | |
219 | return prevlensize+lensize+len; | |
220 | } | |
221 | ||
222 | /* Create a new empty ziplist. */ | |
223 | unsigned char *ziplistNew(void) { | |
224 | unsigned int bytes = ZIPLIST_HEADER_SIZE+1; | |
225 | unsigned char *zl = zmalloc(bytes); | |
226 | ZIPLIST_BYTES(zl) = bytes; | |
227 | ZIPLIST_TAIL_OFFSET(zl) = ZIPLIST_HEADER_SIZE; | |
228 | ZIPLIST_LENGTH(zl) = 0; | |
229 | zl[bytes-1] = ZIP_END; | |
230 | return zl; | |
231 | } | |
232 | ||
233 | /* Resize the ziplist. */ | |
234 | static unsigned char *ziplistResize(unsigned char *zl, unsigned int len) { | |
235 | zl = zrealloc(zl,len); | |
236 | ZIPLIST_BYTES(zl) = len; | |
237 | zl[len-1] = ZIP_END; | |
238 | return zl; | |
239 | } | |
240 | ||
241 | static unsigned char *ziplistHead(unsigned char *zl) { | |
242 | return zl+ZIPLIST_HEADER_SIZE; | |
243 | } | |
244 | ||
245 | static unsigned char *ziplistTail(unsigned char *zl) { | |
246 | unsigned char *p, *q; | |
247 | p = q = ziplistHead(zl); | |
248 | while (*p != ZIP_END) { | |
249 | q = p; | |
250 | p += zipRawEntryLength(p); | |
251 | } | |
252 | return q; | |
253 | } | |
254 | ||
255 | unsigned char *ziplistPush(unsigned char *zl, unsigned char *entry, unsigned int elen, int where) { | |
256 | unsigned int curlen = ZIPLIST_BYTES(zl), reqlen, prevlen; | |
257 | unsigned char *p, *curtail; | |
258 | char encoding = ZIP_ENC_RAW; | |
259 | long long value; | |
260 | ||
261 | /* We need to store the length of the current tail when the list | |
262 | * is non-empty and we push at the tail. */ | |
263 | curtail = zl+ZIPLIST_TAIL_OFFSET(zl); | |
264 | if (where == ZIPLIST_TAIL && curtail[0] != ZIP_END) { | |
265 | prevlen = zipRawEntryLength(curtail); | |
266 | } else { | |
267 | prevlen = 0; | |
268 | } | |
269 | ||
270 | /* See if the entry can be encoded */ | |
271 | if (zipTryEncoding(entry,&value,&encoding)) { | |
272 | reqlen = zipEncodingSize(encoding); | |
273 | } else { | |
274 | reqlen = elen; | |
275 | } | |
276 | ||
277 | /* We need space for both the length of the previous entry and | |
278 | * the length of the payload. */ | |
279 | reqlen += zipEncodeLength(NULL,ZIP_ENC_RAW,prevlen); | |
280 | reqlen += zipEncodeLength(NULL,encoding,elen); | |
281 | ||
282 | /* Resize the ziplist and move if needed */ | |
283 | zl = ziplistResize(zl,curlen+reqlen); | |
284 | if (where == ZIPLIST_HEAD) { | |
285 | p = zl+ZIPLIST_HEADER_SIZE; | |
286 | if (*p != ZIP_END) { | |
287 | /* Subtract one because of the ZIP_END bytes */ | |
288 | memmove(p+reqlen,p,curlen-ZIPLIST_HEADER_SIZE-1); | |
289 | } | |
290 | } else { | |
291 | p = zl+curlen-1; | |
292 | } | |
293 | ||
294 | /* Update tail offset if this is not the first element */ | |
295 | if (curtail[0] != ZIP_END) { | |
296 | if (where == ZIPLIST_HEAD) { | |
297 | ZIPLIST_TAIL_OFFSET(zl) += reqlen; | |
298 | } else { | |
299 | ZIPLIST_TAIL_OFFSET(zl) += prevlen; | |
300 | } | |
301 | } | |
302 | ||
303 | /* Write the entry */ | |
304 | p += zipEncodeLength(p,ZIP_ENC_RAW,prevlen); | |
305 | p += zipEncodeLength(p,encoding,elen); | |
306 | if (encoding != ZIP_ENC_RAW) { | |
307 | zipSaveInteger(p,value,encoding); | |
308 | } else { | |
309 | memcpy(p,entry,elen); | |
310 | } | |
311 | ZIPLIST_INCR_LENGTH(zl,1); | |
312 | return zl; | |
313 | } | |
314 | ||
315 | unsigned char *ziplistPop(unsigned char *zl, sds *target, int where) { | |
316 | unsigned int curlen = ZIPLIST_BYTES(zl), rawlen; | |
317 | zlentry entry; | |
318 | int nextdiff = 0; | |
319 | unsigned char *p; | |
320 | long long value; | |
321 | if (target) *target = NULL; | |
322 | ||
323 | /* Get pointer to element to remove */ | |
324 | p = (where == ZIPLIST_HEAD) ? ziplistHead(zl) : ziplistTail(zl); | |
325 | if (*p == ZIP_END) return zl; | |
326 | ||
327 | entry = zipEntry(p); | |
328 | rawlen = entry.headersize+entry.len; | |
329 | if (target) { | |
330 | if (entry.encoding == ZIP_ENC_RAW) { | |
331 | *target = sdsnewlen(p+entry.headersize,entry.len); | |
332 | } else { | |
333 | value = zipLoadInteger(p+entry.headersize,entry.encoding); | |
334 | *target = sdscatprintf(sdsempty(), "%lld", value); | |
335 | } | |
336 | } | |
337 | ||
338 | if (where == ZIPLIST_HEAD) { | |
339 | /* The next entry will now be the head of the list */ | |
340 | if (p[rawlen] != ZIP_END) { | |
341 | /* Tricky: storing the length of the previous entry in the next | |
342 | * entry (this previous length is always 0 when popping from the | |
343 | * head), might reduce the number of bytes needed. | |
344 | * | |
345 | * In this special case (new length is 0), we know that the | |
346 | * byte difference to store is always <= 0, which means that | |
347 | * we always have space to store it. */ | |
348 | nextdiff = zipPrevLenByteDiff(p+rawlen,0); | |
349 | zipEncodeLength(p+rawlen-nextdiff,ZIP_ENC_RAW,0); | |
350 | } | |
351 | /* Move list to the front */ | |
352 | memmove(p,p+rawlen-nextdiff,curlen-ZIPLIST_HEADER_SIZE-rawlen+nextdiff); | |
353 | ||
354 | /* Subtract the gained space from the tail offset */ | |
355 | ZIPLIST_TAIL_OFFSET(zl) -= rawlen+nextdiff; | |
356 | } else { | |
357 | /* Subtract the length of the previous element from the tail offset. */ | |
358 | ZIPLIST_TAIL_OFFSET(zl) -= entry.prevrawlen; | |
359 | } | |
360 | ||
361 | /* Resize and update length */ | |
362 | zl = ziplistResize(zl,curlen-rawlen+nextdiff); | |
363 | ZIPLIST_INCR_LENGTH(zl,-1); | |
364 | return zl; | |
365 | } | |
366 | ||
367 | /* Returns an offset to use for iterating with ziplistNext. */ | |
368 | unsigned char *ziplistIndex(unsigned char *zl, unsigned int index) { | |
369 | unsigned char *p = zl+ZIPLIST_HEADER_SIZE; | |
370 | unsigned int i = 0; | |
371 | for (; i < index; i++) { | |
372 | if (*p == ZIP_END) break; | |
373 | p += zipRawEntryLength(p); | |
374 | } | |
375 | return p; | |
376 | } | |
377 | ||
378 | /* Return pointer to next entry in ziplist. */ | |
379 | unsigned char *ziplistNext(unsigned char *p) { | |
380 | return *p == ZIP_END ? p : p+zipRawEntryLength(p); | |
381 | } | |
382 | ||
383 | /* Get entry pointer to by 'p' and store in either 'e' or 'v' depending | |
384 | * on the encoding of the entry. 'e' is always set to NULL to be able | |
385 | * to find out whether the string pointer or the integer value was set. | |
386 | * Return 0 if 'p' points to the end of the zipmap, 1 otherwise. */ | |
387 | unsigned int ziplistGet(unsigned char *p, unsigned char **sstr, unsigned int *slen, long long *sval) { | |
388 | zlentry entry; | |
389 | if (*p == ZIP_END) return 0; | |
390 | if (sstr) *sstr = NULL; | |
391 | ||
392 | entry = zipEntry(p); | |
393 | if (entry.encoding == ZIP_ENC_RAW) { | |
394 | if (sstr) { | |
395 | *slen = entry.len; | |
396 | *sstr = p+entry.headersize; | |
397 | } | |
398 | } else { | |
399 | if (sval) { | |
400 | *sval = zipLoadInteger(p+entry.headersize,entry.encoding); | |
401 | } | |
402 | } | |
403 | return 1; | |
404 | } | |
405 | ||
406 | /* Delete a range of entries from the ziplist. */ | |
407 | unsigned char *ziplistDeleteRange(unsigned char *zl, unsigned int index, unsigned int num) { | |
408 | unsigned char *p, *first = ziplistIndex(zl, index); | |
409 | unsigned int i, totlen, deleted = 0; | |
410 | for (p = first, i = 0; *p != ZIP_END && i < num; i++) { | |
411 | p += zipRawEntryLength(p); | |
412 | deleted++; | |
413 | } | |
414 | ||
415 | totlen = p-first; | |
416 | if (totlen > 0) { | |
417 | /* Move current tail to the new tail when there *is* a tail */ | |
418 | if (*p != ZIP_END) memmove(first,p,ZIPLIST_BYTES(zl)-(p-zl)-1); | |
419 | ||
420 | /* Resize and update length */ | |
421 | zl = ziplistResize(zl, ZIPLIST_BYTES(zl)-totlen); | |
422 | ZIPLIST_INCR_LENGTH(zl,-deleted); | |
423 | } | |
424 | return zl; | |
425 | } | |
426 | ||
427 | /* Delete a single entry from the ziplist, pointed to by *p. | |
428 | * Also update *p in place, to be able to iterate over the | |
429 | * ziplist, while deleting entries. */ | |
430 | unsigned char *ziplistDelete(unsigned char *zl, unsigned char **p) { | |
431 | unsigned int offset = *p-zl, tail, len; | |
432 | len = zipRawEntryLength(*p); | |
433 | tail = ZIPLIST_BYTES(zl)-offset-len-1; | |
434 | ||
435 | /* Move current tail to the new tail when there *is* a tail */ | |
436 | if (tail > 0) memmove(*p,*p+len,tail); | |
437 | ||
438 | /* Resize and update length */ | |
439 | zl = ziplistResize(zl, ZIPLIST_BYTES(zl)-len); | |
440 | ZIPLIST_INCR_LENGTH(zl,-1); | |
441 | ||
442 | /* Store new pointer to current element in p. | |
443 | * This needs to be done because zl can change on realloc. */ | |
444 | *p = zl+offset; | |
445 | return zl; | |
446 | } | |
447 | ||
448 | /* Compare entry pointer to by 'p' with 'entry'. Return 1 if equal. */ | |
449 | unsigned int ziplistCompare(unsigned char *p, unsigned char *sstr, unsigned int slen) { | |
450 | zlentry entry; | |
451 | unsigned char sencoding; | |
452 | long long val, sval; | |
453 | if (*p == ZIP_END) return 0; | |
454 | ||
455 | entry = zipEntry(p); | |
456 | if (entry.encoding == ZIP_ENC_RAW) { | |
457 | /* Raw compare */ | |
458 | if (entry.len == slen) { | |
459 | return memcmp(p+entry.headersize,sstr,slen) == 0; | |
460 | } else { | |
461 | return 0; | |
462 | } | |
463 | } else { | |
464 | /* Try to compare encoded values */ | |
465 | if (zipTryEncoding(sstr,&sval,&sencoding)) { | |
466 | if (entry.encoding == sencoding) { | |
467 | val = zipLoadInteger(p+entry.headersize,entry.encoding); | |
468 | return val == sval; | |
469 | } | |
470 | } | |
471 | } | |
472 | return 0; | |
473 | } | |
474 | ||
475 | /* Return length of ziplist. */ | |
476 | unsigned int ziplistLen(unsigned char *zl) { | |
477 | unsigned int len = 0; | |
478 | if (ZIPLIST_LENGTH(zl) < ZIP_BIGLEN) { | |
479 | len = ZIPLIST_LENGTH(zl); | |
480 | } else { | |
481 | unsigned char *p = zl+ZIPLIST_HEADER_SIZE; | |
482 | while (*p != ZIP_END) { | |
483 | p += zipRawEntryLength(p); | |
484 | len++; | |
485 | } | |
486 | ||
487 | /* Re-store length if small enough */ | |
488 | if (len < ZIP_BIGLEN) ZIPLIST_LENGTH(zl) = len; | |
489 | } | |
490 | return len; | |
491 | } | |
492 | ||
493 | /* Return size in bytes of ziplist. */ | |
494 | unsigned int ziplistSize(unsigned char *zl) { | |
495 | return ZIPLIST_BYTES(zl); | |
496 | } | |
497 | ||
498 | void ziplistRepr(unsigned char *zl) { | |
499 | unsigned char *p; | |
500 | zlentry entry; | |
501 | ||
502 | printf("{total bytes %d} {length %u}\n",ZIPLIST_BYTES(zl), ZIPLIST_LENGTH(zl)); | |
503 | p = ziplistHead(zl); | |
504 | while(*p != ZIP_END) { | |
505 | entry = zipEntry(p); | |
506 | printf("{offset %ld, header %u, payload %u} ",p-zl,entry.headersize,entry.len); | |
507 | p += entry.headersize; | |
508 | if (entry.encoding == ZIP_ENC_RAW) { | |
509 | fwrite(p,entry.len,1,stdout); | |
510 | } else { | |
511 | printf("%lld", zipLoadInteger(p,entry.encoding)); | |
512 | } | |
513 | printf("\n"); | |
514 | p += entry.len; | |
515 | } | |
516 | printf("{end}\n\n"); | |
517 | } | |
518 | ||
519 | #ifdef ZIPLIST_TEST_MAIN | |
520 | ||
521 | unsigned char *createList() { | |
522 | unsigned char *zl = ziplistNew(); | |
523 | zl = ziplistPush(zl, (unsigned char*)"foo", 3, ZIPLIST_TAIL); | |
524 | zl = ziplistPush(zl, (unsigned char*)"quux", 4, ZIPLIST_TAIL); | |
525 | zl = ziplistPush(zl, (unsigned char*)"hello", 5, ZIPLIST_HEAD); | |
526 | zl = ziplistPush(zl, (unsigned char*)"1024", 4, ZIPLIST_TAIL); | |
527 | return zl; | |
528 | } | |
529 | ||
530 | unsigned char *createIntList() { | |
531 | unsigned char *zl = ziplistNew(); | |
532 | char buf[32]; | |
533 | ||
534 | sprintf(buf, "100"); | |
535 | zl = ziplistPush(zl, buf, strlen(buf), ZIPLIST_TAIL); | |
536 | sprintf(buf, "128000"); | |
537 | zl = ziplistPush(zl, buf, strlen(buf), ZIPLIST_TAIL); | |
538 | sprintf(buf, "-100"); | |
539 | zl = ziplistPush(zl, buf, strlen(buf), ZIPLIST_HEAD); | |
540 | sprintf(buf, "4294967296"); | |
541 | zl = ziplistPush(zl, buf, strlen(buf), ZIPLIST_HEAD); | |
542 | sprintf(buf, "non integer"); | |
543 | zl = ziplistPush(zl, buf, strlen(buf), ZIPLIST_TAIL); | |
544 | sprintf(buf, "much much longer non integer"); | |
545 | zl = ziplistPush(zl, buf, strlen(buf), ZIPLIST_TAIL); | |
546 | return zl; | |
547 | } | |
548 | ||
549 | int main(int argc, char **argv) { | |
550 | unsigned char *zl, *p, *q, *entry; | |
551 | unsigned int elen; | |
552 | long long value; | |
553 | sds s; | |
554 | ||
555 | zl = createIntList(); | |
556 | ziplistRepr(zl); | |
557 | ||
558 | zl = createList(); | |
559 | ziplistRepr(zl); | |
560 | ||
561 | zl = ziplistPop(zl, &s, ZIPLIST_TAIL); | |
562 | printf("Pop tail: %s (length %ld)\n", s, sdslen(s)); | |
563 | ziplistRepr(zl); | |
564 | ||
565 | zl = ziplistPop(zl, &s, ZIPLIST_HEAD); | |
566 | printf("Pop head: %s (length %ld)\n", s, sdslen(s)); | |
567 | ziplistRepr(zl); | |
568 | ||
569 | zl = ziplistPop(zl, &s, ZIPLIST_TAIL); | |
570 | printf("Pop tail: %s (length %ld)\n", s, sdslen(s)); | |
571 | ziplistRepr(zl); | |
572 | ||
573 | zl = ziplistPop(zl, &s, ZIPLIST_TAIL); | |
574 | printf("Pop tail: %s (length %ld)\n", s, sdslen(s)); | |
575 | ziplistRepr(zl); | |
576 | ||
577 | printf("Iterate list from 0 to end:\n"); | |
578 | { | |
579 | zl = createList(); | |
580 | p = ziplistIndex(zl, 0); | |
581 | while (ziplistGet(p, &entry, &elen, &value)) { | |
582 | printf("Entry: "); | |
583 | if (entry) { | |
584 | fwrite(entry,elen,1,stdout); | |
585 | } else { | |
586 | printf("%lld", value); | |
587 | } | |
588 | p = ziplistNext(p); | |
589 | printf("\n"); | |
590 | } | |
591 | printf("\n"); | |
592 | } | |
593 | ||
594 | printf("Iterate list from 1 to end:\n"); | |
595 | { | |
596 | zl = createList(); | |
597 | p = ziplistIndex(zl, 1); | |
598 | while (ziplistGet(p, &entry, &elen, &value)) { | |
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 | printf("\n"); | |
609 | } | |
610 | ||
611 | printf("Iterate list from 2 to end:\n"); | |
612 | { | |
613 | zl = createList(); | |
614 | p = ziplistIndex(zl, 2); | |
615 | while (ziplistGet(p, &entry, &elen, &value)) { | |
616 | printf("Entry: "); | |
617 | if (entry) { | |
618 | fwrite(entry,elen,1,stdout); | |
619 | } else { | |
620 | printf("%lld", value); | |
621 | } | |
622 | p = ziplistNext(p); | |
623 | printf("\n"); | |
624 | } | |
625 | printf("\n"); | |
626 | } | |
627 | ||
628 | printf("Iterate starting out of range:\n"); | |
629 | { | |
630 | zl = createList(); | |
631 | p = ziplistIndex(zl, 4); | |
632 | if (!ziplistGet(p, &entry, &elen, &value)) { | |
633 | printf("No entry\n"); | |
634 | } else { | |
635 | printf("ERROR\n"); | |
636 | } | |
637 | printf("\n"); | |
638 | } | |
639 | ||
640 | printf("Delete inclusive range 0,0:\n"); | |
641 | { | |
642 | zl = createList(); | |
643 | zl = ziplistDeleteRange(zl, 0, 1); | |
644 | ziplistRepr(zl); | |
645 | } | |
646 | ||
647 | printf("Delete inclusive range 0,1:\n"); | |
648 | { | |
649 | zl = createList(); | |
650 | zl = ziplistDeleteRange(zl, 0, 2); | |
651 | ziplistRepr(zl); | |
652 | } | |
653 | ||
654 | printf("Delete inclusive range 1,2:\n"); | |
655 | { | |
656 | zl = createList(); | |
657 | zl = ziplistDeleteRange(zl, 1, 2); | |
658 | ziplistRepr(zl); | |
659 | } | |
660 | ||
661 | printf("Delete with start index out of range:\n"); | |
662 | { | |
663 | zl = createList(); | |
664 | zl = ziplistDeleteRange(zl, 5, 1); | |
665 | ziplistRepr(zl); | |
666 | } | |
667 | ||
668 | printf("Delete with num overflow:\n"); | |
669 | { | |
670 | zl = createList(); | |
671 | zl = ziplistDeleteRange(zl, 1, 5); | |
672 | ziplistRepr(zl); | |
673 | } | |
674 | ||
675 | printf("Delete foo while iterating:\n"); | |
676 | { | |
677 | zl = createList(); | |
678 | p = ziplistIndex(zl, 0); | |
679 | while (ziplistGet(p, &entry, &elen, &value)) { | |
680 | if (entry && strncmp("foo", entry, elen) == 0) { | |
681 | printf("Delete foo\n"); | |
682 | zl = ziplistDelete(zl, &p); | |
683 | } else { | |
684 | printf("Entry: "); | |
685 | if (entry) { | |
686 | fwrite(entry,elen,1,stdout); | |
687 | } else { | |
688 | printf("%lld", value); | |
689 | } | |
690 | p = ziplistNext(p); | |
691 | printf("\n"); | |
692 | } | |
693 | } | |
694 | printf("\n"); | |
695 | ziplistRepr(zl); | |
696 | } | |
697 | ||
698 | printf("Compare strings with ziplist entries:\n"); | |
699 | { | |
700 | zl = createList(); | |
701 | p = ziplistIndex(zl, 0); | |
702 | if (!ziplistCompare(p,"hello",5)) { | |
703 | printf("ERROR: not \"hello\"\n"); | |
704 | return; | |
705 | } | |
706 | if (ziplistCompare(p,"hella",5)) { | |
707 | printf("ERROR: \"hella\"\n"); | |
708 | return; | |
709 | } | |
710 | ||
711 | p = ziplistIndex(zl, 3); | |
712 | if (!ziplistCompare(p,"1024",4)) { | |
713 | printf("ERROR: not \"1024\"\n"); | |
714 | return; | |
715 | } | |
716 | if (ziplistCompare(p,"1025",4)) { | |
717 | printf("ERROR: \"1025\"\n"); | |
718 | return; | |
719 | } | |
720 | printf("SUCCESS\n"); | |
721 | } | |
722 | ||
723 | return 0; | |
724 | } | |
725 | #endif |