1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
|
#include <stdio.h> #include <string.h> #include "zmalloc.h" #include "endianconv.h"
#define ZIPMAP_BIGLEN 254 #define ZIPMAP_END 255
#define ZIPMAP_VALUE_MAX_FREE 4
#define ZIPMAP_LEN_BYTES(_l) (((_l) < ZIPMAP_BIGLEN) ? 1 : sizeof(unsigned int)+1)
unsigned char *zipmapNew(void) { unsigned char *zm = zmalloc(2);
zm[0] = 0; zm[1] = ZIPMAP_END; return zm; }
static unsigned int zipmapDecodeLength(unsigned char *p) { unsigned int len = *p;
if (len < ZIPMAP_BIGLEN) return len; memcpy(&len,p+1,sizeof(unsigned int)); memrev32ifbe(&len); return len; }
static unsigned int zipmapEncodeLength(unsigned char *p, unsigned int len) { if (p == NULL) { return ZIPMAP_LEN_BYTES(len); } else { if (len < ZIPMAP_BIGLEN) { p[0] = len; return 1; } else { p[0] = ZIPMAP_BIGLEN; memcpy(p+1,&len,sizeof(len)); memrev32ifbe(p+1); return 1+sizeof(len); } } }
static unsigned char *zipmapLookupRaw(unsigned char *zm, unsigned char *key, unsigned int klen, unsigned int *totlen) { unsigned char *p = zm+1, *k = NULL; unsigned int l,llen;
while(*p != ZIPMAP_END) { unsigned char free;
l = zipmapDecodeLength(p); llen = zipmapEncodeLength(NULL,l); if (key != NULL && k == NULL && l == klen && !memcmp(p+llen,key,l)) {
if (totlen != NULL) { k = p; } else { return p; } } p += llen+l; l = zipmapDecodeLength(p); p += zipmapEncodeLength(NULL,l); free = p[0]; p += l+1+free; } if (totlen != NULL) *totlen = (unsigned int)(p-zm)+1; return k; }
static unsigned long zipmapRequiredLength(unsigned int klen, unsigned int vlen) { unsigned int l;
l = klen+vlen+3; if (klen >= ZIPMAP_BIGLEN) l += 4; if (vlen >= ZIPMAP_BIGLEN) l += 4; return l; }
static unsigned int zipmapRawKeyLength(unsigned char *p) { unsigned int l = zipmapDecodeLength(p); return zipmapEncodeLength(NULL,l) + l; }
static unsigned int zipmapRawValueLength(unsigned char *p) { unsigned int l = zipmapDecodeLength(p); unsigned int used;
used = zipmapEncodeLength(NULL,l); used += p[used] + 1 + l; return used; }
static unsigned int zipmapRawEntryLength(unsigned char *p) { unsigned int l = zipmapRawKeyLength(p); return l + zipmapRawValueLength(p+l); }
static inline unsigned char *zipmapResize(unsigned char *zm, unsigned int len) { zm = zrealloc(zm, len); zm[len-1] = ZIPMAP_END; return zm; }
unsigned char *zipmapSet(unsigned char *zm, unsigned char *key, unsigned int klen, unsigned char *val, unsigned int vlen, int *update) { unsigned int zmlen, offset; unsigned int freelen, reqlen = zipmapRequiredLength(klen,vlen); unsigned int empty, vempty; unsigned char *p;
freelen = reqlen; if (update) *update = 0; p = zipmapLookupRaw(zm,key,klen,&zmlen); if (p == NULL) { zm = zipmapResize(zm, zmlen+reqlen); p = zm+zmlen-1; zmlen = zmlen+reqlen;
if (zm[0] < ZIPMAP_BIGLEN) zm[0]++; } else { if (update) *update = 1; freelen = zipmapRawEntryLength(p); if (freelen < reqlen) {
offset = p-zm; zm = zipmapResize(zm, zmlen-freelen+reqlen); p = zm+offset;
memmove(p+reqlen, p+freelen, zmlen-(offset+freelen+1)); zmlen = zmlen-freelen+reqlen; freelen = reqlen; } }
empty = freelen-reqlen; if (empty >= ZIPMAP_VALUE_MAX_FREE) {
offset = p-zm; memmove(p+reqlen, p+freelen, zmlen-(offset+freelen+1)); zmlen -= empty; zm = zipmapResize(zm, zmlen); p = zm+offset; vempty = 0; } else { vempty = empty; }
p += zipmapEncodeLength(p,klen); memcpy(p,key,klen); p += klen; p += zipmapEncodeLength(p,vlen); *p++ = vempty; memcpy(p,val,vlen); return zm; }
unsigned char *zipmapDel(unsigned char *zm, unsigned char *key, unsigned int klen, int *deleted) { unsigned int zmlen, freelen; unsigned char *p = zipmapLookupRaw(zm,key,klen,&zmlen); if (p) { freelen = zipmapRawEntryLength(p); memmove(p, p+freelen, zmlen-((p-zm)+freelen+1)); zm = zipmapResize(zm, zmlen-freelen);
if (zm[0] < ZIPMAP_BIGLEN) zm[0]--;
if (deleted) *deleted = 1; } else { if (deleted) *deleted = 0; } return zm; }
unsigned char *zipmapRewind(unsigned char *zm) { return zm+1; }
unsigned char *zipmapNext(unsigned char *zm, unsigned char **key, unsigned int *klen, unsigned char **value, unsigned int *vlen) { if (zm[0] == ZIPMAP_END) return NULL; if (key) { *key = zm; *klen = zipmapDecodeLength(zm); *key += ZIPMAP_LEN_BYTES(*klen); } zm += zipmapRawKeyLength(zm); if (value) { *value = zm+1; *vlen = zipmapDecodeLength(zm); *value += ZIPMAP_LEN_BYTES(*vlen); } zm += zipmapRawValueLength(zm);
return zm; }
int zipmapGet(unsigned char *zm, unsigned char *key, unsigned int klen, unsigned char **value, unsigned int *vlen) { unsigned char *p;
if ((p = zipmapLookupRaw(zm,key,klen,NULL)) == NULL) return 0; p += zipmapRawKeyLength(p); *vlen = zipmapDecodeLength(p); *value = p + ZIPMAP_LEN_BYTES(*vlen) + 1; return 1; }
int zipmapExists(unsigned char *zm, unsigned char *key, unsigned int klen) { return zipmapLookupRaw(zm,key,klen,NULL) != NULL; }
unsigned int zipmapLen(unsigned char *zm) { unsigned int len = 0; if (zm[0] < ZIPMAP_BIGLEN) { len = zm[0]; } else { unsigned char *p = zipmapRewind(zm); while((p = zipmapNext(p,NULL,NULL,NULL,NULL)) != NULL) len++;
if (len < ZIPMAP_BIGLEN) zm[0] = len; } return len; }
size_t zipmapBlobLen(unsigned char *zm) { unsigned int totlen; zipmapLookupRaw(zm,NULL,0,&totlen); return totlen; }
#ifdef REDIS_TEST static void zipmapRepr(unsigned char *p) { unsigned int l;
printf("{status %u}",*p++); while(1) { if (p[0] == ZIPMAP_END) { printf("{end}"); break; } else { unsigned char e;
l = zipmapDecodeLength(p); printf("{key %u}",l); p += zipmapEncodeLength(NULL,l); if (l != 0 && fwrite(p,l,1,stdout) == 0) perror("fwrite"); p += l;
l = zipmapDecodeLength(p); printf("{value %u}",l); p += zipmapEncodeLength(NULL,l); e = *p++; if (l != 0 && fwrite(p,l,1,stdout) == 0) perror("fwrite"); p += l+e; if (e) { printf("["); while(e--) printf("."); printf("]"); } } } printf("\n"); }
#define UNUSED(x) (void)(x) int zipmapTest(int argc, char *argv[]) { unsigned char *zm;
UNUSED(argc); UNUSED(argv);
zm = zipmapNew();
zm = zipmapSet(zm,(unsigned char*) "name",4, (unsigned char*) "foo",3,NULL); zm = zipmapSet(zm,(unsigned char*) "surname",7, (unsigned char*) "foo",3,NULL); zm = zipmapSet(zm,(unsigned char*) "age",3, (unsigned char*) "foo",3,NULL); zipmapRepr(zm);
zm = zipmapSet(zm,(unsigned char*) "hello",5, (unsigned char*) "world!",6,NULL); zm = zipmapSet(zm,(unsigned char*) "foo",3, (unsigned char*) "bar",3,NULL); zm = zipmapSet(zm,(unsigned char*) "foo",3, (unsigned char*) "!",1,NULL); zipmapRepr(zm); zm = zipmapSet(zm,(unsigned char*) "foo",3, (unsigned char*) "12345",5,NULL); zipmapRepr(zm); zm = zipmapSet(zm,(unsigned char*) "new",3, (unsigned char*) "xx",2,NULL); zm = zipmapSet(zm,(unsigned char*) "noval",5, (unsigned char*) "",0,NULL); zipmapRepr(zm); zm = zipmapDel(zm,(unsigned char*) "new",3,NULL); zipmapRepr(zm);
printf("\nLook up large key:\n"); { unsigned char buf[512]; unsigned char *value; unsigned int vlen, i; for (i = 0; i < 512; i++) buf[i] = 'a';
zm = zipmapSet(zm,buf,512,(unsigned char*) "long",4,NULL); if (zipmapGet(zm,buf,512,&value,&vlen)) { printf(" <long key> is associated to the %d bytes value: %.*s\n", vlen, vlen, value); } }
printf("\nPerform a direct lookup:\n"); { unsigned char *value; unsigned int vlen;
if (zipmapGet(zm,(unsigned char*) "foo",3,&value,&vlen)) { printf(" foo is associated to the %d bytes value: %.*s\n", vlen, vlen, value); } } printf("\nIterate through elements:\n"); { unsigned char *i = zipmapRewind(zm); unsigned char *key, *value; unsigned int klen, vlen;
while((i = zipmapNext(i,&key,&klen,&value,&vlen)) != NULL) { printf(" %d:%.*s => %d:%.*s\n", klen, klen, key, vlen, vlen, value); } } return 0; } #endif
|