1*572c4311Sfengbojiang /* String -> String Map data structure optimized for size.
2*572c4311Sfengbojiang * This file implements a data structure mapping strings to other strings
3*572c4311Sfengbojiang * implementing an O(n) lookup data structure designed to be very memory
4*572c4311Sfengbojiang * efficient.
5*572c4311Sfengbojiang *
6*572c4311Sfengbojiang * The Redis Hash type uses this data structure for hashes composed of a small
7*572c4311Sfengbojiang * number of elements, to switch to a hash table once a given number of
8*572c4311Sfengbojiang * elements is reached.
9*572c4311Sfengbojiang *
10*572c4311Sfengbojiang * Given that many times Redis Hashes are used to represent objects composed
11*572c4311Sfengbojiang * of few fields, this is a very big win in terms of used memory.
12*572c4311Sfengbojiang *
13*572c4311Sfengbojiang * --------------------------------------------------------------------------
14*572c4311Sfengbojiang *
15*572c4311Sfengbojiang * Copyright (c) 2009-2010, Salvatore Sanfilippo <antirez at gmail dot com>
16*572c4311Sfengbojiang * All rights reserved.
17*572c4311Sfengbojiang *
18*572c4311Sfengbojiang * Redistribution and use in source and binary forms, with or without
19*572c4311Sfengbojiang * modification, are permitted provided that the following conditions are met:
20*572c4311Sfengbojiang *
21*572c4311Sfengbojiang * * Redistributions of source code must retain the above copyright notice,
22*572c4311Sfengbojiang * this list of conditions and the following disclaimer.
23*572c4311Sfengbojiang * * Redistributions in binary form must reproduce the above copyright
24*572c4311Sfengbojiang * notice, this list of conditions and the following disclaimer in the
25*572c4311Sfengbojiang * documentation and/or other materials provided with the distribution.
26*572c4311Sfengbojiang * * Neither the name of Redis nor the names of its contributors may be used
27*572c4311Sfengbojiang * to endorse or promote products derived from this software without
28*572c4311Sfengbojiang * specific prior written permission.
29*572c4311Sfengbojiang *
30*572c4311Sfengbojiang * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
31*572c4311Sfengbojiang * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32*572c4311Sfengbojiang * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33*572c4311Sfengbojiang * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
34*572c4311Sfengbojiang * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
35*572c4311Sfengbojiang * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
36*572c4311Sfengbojiang * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
37*572c4311Sfengbojiang * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
38*572c4311Sfengbojiang * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
39*572c4311Sfengbojiang * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
40*572c4311Sfengbojiang * POSSIBILITY OF SUCH DAMAGE.
41*572c4311Sfengbojiang */
42*572c4311Sfengbojiang
43*572c4311Sfengbojiang /* Memory layout of a zipmap, for the map "foo" => "bar", "hello" => "world":
44*572c4311Sfengbojiang *
45*572c4311Sfengbojiang * <zmlen><len>"foo"<len><free>"bar"<len>"hello"<len><free>"world"
46*572c4311Sfengbojiang *
47*572c4311Sfengbojiang * <zmlen> is 1 byte length that holds the current size of the zipmap.
48*572c4311Sfengbojiang * When the zipmap length is greater than or equal to 254, this value
49*572c4311Sfengbojiang * is not used and the zipmap needs to be traversed to find out the length.
50*572c4311Sfengbojiang *
51*572c4311Sfengbojiang * <len> is the length of the following string (key or value).
52*572c4311Sfengbojiang * <len> lengths are encoded in a single value or in a 5 bytes value.
53*572c4311Sfengbojiang * If the first byte value (as an unsigned 8 bit value) is between 0 and
54*572c4311Sfengbojiang * 253, it's a single-byte length. If it is 254 then a four bytes unsigned
55*572c4311Sfengbojiang * integer follows (in the host byte ordering). A value of 255 is used to
56*572c4311Sfengbojiang * signal the end of the hash.
57*572c4311Sfengbojiang *
58*572c4311Sfengbojiang * <free> is the number of free unused bytes after the string, resulting
59*572c4311Sfengbojiang * from modification of values associated to a key. For instance if "foo"
60*572c4311Sfengbojiang * is set to "bar", and later "foo" will be set to "hi", it will have a
61*572c4311Sfengbojiang * free byte to use if the value will enlarge again later, or even in
62*572c4311Sfengbojiang * order to add a key/value pair if it fits.
63*572c4311Sfengbojiang *
64*572c4311Sfengbojiang * <free> is always an unsigned 8 bit number, because if after an
65*572c4311Sfengbojiang * update operation there are more than a few free bytes, the zipmap will be
66*572c4311Sfengbojiang * reallocated to make sure it is as small as possible.
67*572c4311Sfengbojiang *
68*572c4311Sfengbojiang * The most compact representation of the above two elements hash is actually:
69*572c4311Sfengbojiang *
70*572c4311Sfengbojiang * "\x02\x03foo\x03\x00bar\x05hello\x05\x00world\xff"
71*572c4311Sfengbojiang *
72*572c4311Sfengbojiang * Note that because keys and values are prefixed length "objects",
73*572c4311Sfengbojiang * the lookup will take O(N) where N is the number of elements
74*572c4311Sfengbojiang * in the zipmap and *not* the number of bytes needed to represent the zipmap.
75*572c4311Sfengbojiang * This lowers the constant times considerably.
76*572c4311Sfengbojiang */
77*572c4311Sfengbojiang
78*572c4311Sfengbojiang #include <stdio.h>
79*572c4311Sfengbojiang #include <string.h>
80*572c4311Sfengbojiang #include "zmalloc.h"
81*572c4311Sfengbojiang #include "endianconv.h"
82*572c4311Sfengbojiang
83*572c4311Sfengbojiang #define ZIPMAP_BIGLEN 254
84*572c4311Sfengbojiang #define ZIPMAP_END 255
85*572c4311Sfengbojiang
86*572c4311Sfengbojiang /* The following defines the max value for the <free> field described in the
87*572c4311Sfengbojiang * comments above, that is, the max number of trailing bytes in a value. */
88*572c4311Sfengbojiang #define ZIPMAP_VALUE_MAX_FREE 4
89*572c4311Sfengbojiang
90*572c4311Sfengbojiang /* The following macro returns the number of bytes needed to encode the length
91*572c4311Sfengbojiang * for the integer value _l, that is, 1 byte for lengths < ZIPMAP_BIGLEN and
92*572c4311Sfengbojiang * 5 bytes for all the other lengths. */
93*572c4311Sfengbojiang #define ZIPMAP_LEN_BYTES(_l) (((_l) < ZIPMAP_BIGLEN) ? 1 : sizeof(unsigned int)+1)
94*572c4311Sfengbojiang
95*572c4311Sfengbojiang /* Create a new empty zipmap. */
zipmapNew(void)96*572c4311Sfengbojiang unsigned char *zipmapNew(void) {
97*572c4311Sfengbojiang unsigned char *zm = zmalloc(2);
98*572c4311Sfengbojiang
99*572c4311Sfengbojiang zm[0] = 0; /* Length */
100*572c4311Sfengbojiang zm[1] = ZIPMAP_END;
101*572c4311Sfengbojiang return zm;
102*572c4311Sfengbojiang }
103*572c4311Sfengbojiang
104*572c4311Sfengbojiang /* Decode the encoded length pointed by 'p' */
zipmapDecodeLength(unsigned char * p)105*572c4311Sfengbojiang static unsigned int zipmapDecodeLength(unsigned char *p) {
106*572c4311Sfengbojiang unsigned int len = *p;
107*572c4311Sfengbojiang
108*572c4311Sfengbojiang if (len < ZIPMAP_BIGLEN) return len;
109*572c4311Sfengbojiang memcpy(&len,p+1,sizeof(unsigned int));
110*572c4311Sfengbojiang memrev32ifbe(&len);
111*572c4311Sfengbojiang return len;
112*572c4311Sfengbojiang }
113*572c4311Sfengbojiang
114*572c4311Sfengbojiang /* Encode the length 'l' writing it in 'p'. If p is NULL it just returns
115*572c4311Sfengbojiang * the amount of bytes required to encode such a length. */
zipmapEncodeLength(unsigned char * p,unsigned int len)116*572c4311Sfengbojiang static unsigned int zipmapEncodeLength(unsigned char *p, unsigned int len) {
117*572c4311Sfengbojiang if (p == NULL) {
118*572c4311Sfengbojiang return ZIPMAP_LEN_BYTES(len);
119*572c4311Sfengbojiang } else {
120*572c4311Sfengbojiang if (len < ZIPMAP_BIGLEN) {
121*572c4311Sfengbojiang p[0] = len;
122*572c4311Sfengbojiang return 1;
123*572c4311Sfengbojiang } else {
124*572c4311Sfengbojiang p[0] = ZIPMAP_BIGLEN;
125*572c4311Sfengbojiang memcpy(p+1,&len,sizeof(len));
126*572c4311Sfengbojiang memrev32ifbe(p+1);
127*572c4311Sfengbojiang return 1+sizeof(len);
128*572c4311Sfengbojiang }
129*572c4311Sfengbojiang }
130*572c4311Sfengbojiang }
131*572c4311Sfengbojiang
132*572c4311Sfengbojiang /* Search for a matching key, returning a pointer to the entry inside the
133*572c4311Sfengbojiang * zipmap. Returns NULL if the key is not found.
134*572c4311Sfengbojiang *
135*572c4311Sfengbojiang * If NULL is returned, and totlen is not NULL, it is set to the entire
136*572c4311Sfengbojiang * size of the zimap, so that the calling function will be able to
137*572c4311Sfengbojiang * reallocate the original zipmap to make room for more entries. */
zipmapLookupRaw(unsigned char * zm,unsigned char * key,unsigned int klen,unsigned int * totlen)138*572c4311Sfengbojiang static unsigned char *zipmapLookupRaw(unsigned char *zm, unsigned char *key, unsigned int klen, unsigned int *totlen) {
139*572c4311Sfengbojiang unsigned char *p = zm+1, *k = NULL;
140*572c4311Sfengbojiang unsigned int l,llen;
141*572c4311Sfengbojiang
142*572c4311Sfengbojiang while(*p != ZIPMAP_END) {
143*572c4311Sfengbojiang unsigned char free;
144*572c4311Sfengbojiang
145*572c4311Sfengbojiang /* Match or skip the key */
146*572c4311Sfengbojiang l = zipmapDecodeLength(p);
147*572c4311Sfengbojiang llen = zipmapEncodeLength(NULL,l);
148*572c4311Sfengbojiang if (key != NULL && k == NULL && l == klen && !memcmp(p+llen,key,l)) {
149*572c4311Sfengbojiang /* Only return when the user doesn't care
150*572c4311Sfengbojiang * for the total length of the zipmap. */
151*572c4311Sfengbojiang if (totlen != NULL) {
152*572c4311Sfengbojiang k = p;
153*572c4311Sfengbojiang } else {
154*572c4311Sfengbojiang return p;
155*572c4311Sfengbojiang }
156*572c4311Sfengbojiang }
157*572c4311Sfengbojiang p += llen+l;
158*572c4311Sfengbojiang /* Skip the value as well */
159*572c4311Sfengbojiang l = zipmapDecodeLength(p);
160*572c4311Sfengbojiang p += zipmapEncodeLength(NULL,l);
161*572c4311Sfengbojiang free = p[0];
162*572c4311Sfengbojiang p += l+1+free; /* +1 to skip the free byte */
163*572c4311Sfengbojiang }
164*572c4311Sfengbojiang if (totlen != NULL) *totlen = (unsigned int)(p-zm)+1;
165*572c4311Sfengbojiang return k;
166*572c4311Sfengbojiang }
167*572c4311Sfengbojiang
zipmapRequiredLength(unsigned int klen,unsigned int vlen)168*572c4311Sfengbojiang static unsigned long zipmapRequiredLength(unsigned int klen, unsigned int vlen) {
169*572c4311Sfengbojiang unsigned int l;
170*572c4311Sfengbojiang
171*572c4311Sfengbojiang l = klen+vlen+3;
172*572c4311Sfengbojiang if (klen >= ZIPMAP_BIGLEN) l += 4;
173*572c4311Sfengbojiang if (vlen >= ZIPMAP_BIGLEN) l += 4;
174*572c4311Sfengbojiang return l;
175*572c4311Sfengbojiang }
176*572c4311Sfengbojiang
177*572c4311Sfengbojiang /* Return the total amount used by a key (encoded length + payload) */
zipmapRawKeyLength(unsigned char * p)178*572c4311Sfengbojiang static unsigned int zipmapRawKeyLength(unsigned char *p) {
179*572c4311Sfengbojiang unsigned int l = zipmapDecodeLength(p);
180*572c4311Sfengbojiang return zipmapEncodeLength(NULL,l) + l;
181*572c4311Sfengbojiang }
182*572c4311Sfengbojiang
183*572c4311Sfengbojiang /* Return the total amount used by a value
184*572c4311Sfengbojiang * (encoded length + single byte free count + payload) */
zipmapRawValueLength(unsigned char * p)185*572c4311Sfengbojiang static unsigned int zipmapRawValueLength(unsigned char *p) {
186*572c4311Sfengbojiang unsigned int l = zipmapDecodeLength(p);
187*572c4311Sfengbojiang unsigned int used;
188*572c4311Sfengbojiang
189*572c4311Sfengbojiang used = zipmapEncodeLength(NULL,l);
190*572c4311Sfengbojiang used += p[used] + 1 + l;
191*572c4311Sfengbojiang return used;
192*572c4311Sfengbojiang }
193*572c4311Sfengbojiang
194*572c4311Sfengbojiang /* If 'p' points to a key, this function returns the total amount of
195*572c4311Sfengbojiang * bytes used to store this entry (entry = key + associated value + trailing
196*572c4311Sfengbojiang * free space if any). */
zipmapRawEntryLength(unsigned char * p)197*572c4311Sfengbojiang static unsigned int zipmapRawEntryLength(unsigned char *p) {
198*572c4311Sfengbojiang unsigned int l = zipmapRawKeyLength(p);
199*572c4311Sfengbojiang return l + zipmapRawValueLength(p+l);
200*572c4311Sfengbojiang }
201*572c4311Sfengbojiang
zipmapResize(unsigned char * zm,unsigned int len)202*572c4311Sfengbojiang static inline unsigned char *zipmapResize(unsigned char *zm, unsigned int len) {
203*572c4311Sfengbojiang zm = zrealloc(zm, len);
204*572c4311Sfengbojiang zm[len-1] = ZIPMAP_END;
205*572c4311Sfengbojiang return zm;
206*572c4311Sfengbojiang }
207*572c4311Sfengbojiang
208*572c4311Sfengbojiang /* Set key to value, creating the key if it does not already exist.
209*572c4311Sfengbojiang * If 'update' is not NULL, *update is set to 1 if the key was
210*572c4311Sfengbojiang * already preset, otherwise to 0. */
zipmapSet(unsigned char * zm,unsigned char * key,unsigned int klen,unsigned char * val,unsigned int vlen,int * update)211*572c4311Sfengbojiang unsigned char *zipmapSet(unsigned char *zm, unsigned char *key, unsigned int klen, unsigned char *val, unsigned int vlen, int *update) {
212*572c4311Sfengbojiang unsigned int zmlen, offset;
213*572c4311Sfengbojiang unsigned int freelen, reqlen = zipmapRequiredLength(klen,vlen);
214*572c4311Sfengbojiang unsigned int empty, vempty;
215*572c4311Sfengbojiang unsigned char *p;
216*572c4311Sfengbojiang
217*572c4311Sfengbojiang freelen = reqlen;
218*572c4311Sfengbojiang if (update) *update = 0;
219*572c4311Sfengbojiang p = zipmapLookupRaw(zm,key,klen,&zmlen);
220*572c4311Sfengbojiang if (p == NULL) {
221*572c4311Sfengbojiang /* Key not found: enlarge */
222*572c4311Sfengbojiang zm = zipmapResize(zm, zmlen+reqlen);
223*572c4311Sfengbojiang p = zm+zmlen-1;
224*572c4311Sfengbojiang zmlen = zmlen+reqlen;
225*572c4311Sfengbojiang
226*572c4311Sfengbojiang /* Increase zipmap length (this is an insert) */
227*572c4311Sfengbojiang if (zm[0] < ZIPMAP_BIGLEN) zm[0]++;
228*572c4311Sfengbojiang } else {
229*572c4311Sfengbojiang /* Key found. Is there enough space for the new value? */
230*572c4311Sfengbojiang /* Compute the total length: */
231*572c4311Sfengbojiang if (update) *update = 1;
232*572c4311Sfengbojiang freelen = zipmapRawEntryLength(p);
233*572c4311Sfengbojiang if (freelen < reqlen) {
234*572c4311Sfengbojiang /* Store the offset of this key within the current zipmap, so
235*572c4311Sfengbojiang * it can be resized. Then, move the tail backwards so this
236*572c4311Sfengbojiang * pair fits at the current position. */
237*572c4311Sfengbojiang offset = p-zm;
238*572c4311Sfengbojiang zm = zipmapResize(zm, zmlen-freelen+reqlen);
239*572c4311Sfengbojiang p = zm+offset;
240*572c4311Sfengbojiang
241*572c4311Sfengbojiang /* The +1 in the number of bytes to be moved is caused by the
242*572c4311Sfengbojiang * end-of-zipmap byte. Note: the *original* zmlen is used. */
243*572c4311Sfengbojiang memmove(p+reqlen, p+freelen, zmlen-(offset+freelen+1));
244*572c4311Sfengbojiang zmlen = zmlen-freelen+reqlen;
245*572c4311Sfengbojiang freelen = reqlen;
246*572c4311Sfengbojiang }
247*572c4311Sfengbojiang }
248*572c4311Sfengbojiang
249*572c4311Sfengbojiang /* We now have a suitable block where the key/value entry can
250*572c4311Sfengbojiang * be written. If there is too much free space, move the tail
251*572c4311Sfengbojiang * of the zipmap a few bytes to the front and shrink the zipmap,
252*572c4311Sfengbojiang * as we want zipmaps to be very space efficient. */
253*572c4311Sfengbojiang empty = freelen-reqlen;
254*572c4311Sfengbojiang if (empty >= ZIPMAP_VALUE_MAX_FREE) {
255*572c4311Sfengbojiang /* First, move the tail <empty> bytes to the front, then resize
256*572c4311Sfengbojiang * the zipmap to be <empty> bytes smaller. */
257*572c4311Sfengbojiang offset = p-zm;
258*572c4311Sfengbojiang memmove(p+reqlen, p+freelen, zmlen-(offset+freelen+1));
259*572c4311Sfengbojiang zmlen -= empty;
260*572c4311Sfengbojiang zm = zipmapResize(zm, zmlen);
261*572c4311Sfengbojiang p = zm+offset;
262*572c4311Sfengbojiang vempty = 0;
263*572c4311Sfengbojiang } else {
264*572c4311Sfengbojiang vempty = empty;
265*572c4311Sfengbojiang }
266*572c4311Sfengbojiang
267*572c4311Sfengbojiang /* Just write the key + value and we are done. */
268*572c4311Sfengbojiang /* Key: */
269*572c4311Sfengbojiang p += zipmapEncodeLength(p,klen);
270*572c4311Sfengbojiang memcpy(p,key,klen);
271*572c4311Sfengbojiang p += klen;
272*572c4311Sfengbojiang /* Value: */
273*572c4311Sfengbojiang p += zipmapEncodeLength(p,vlen);
274*572c4311Sfengbojiang *p++ = vempty;
275*572c4311Sfengbojiang memcpy(p,val,vlen);
276*572c4311Sfengbojiang return zm;
277*572c4311Sfengbojiang }
278*572c4311Sfengbojiang
279*572c4311Sfengbojiang /* Remove the specified key. If 'deleted' is not NULL the pointed integer is
280*572c4311Sfengbojiang * set to 0 if the key was not found, to 1 if it was found and deleted. */
zipmapDel(unsigned char * zm,unsigned char * key,unsigned int klen,int * deleted)281*572c4311Sfengbojiang unsigned char *zipmapDel(unsigned char *zm, unsigned char *key, unsigned int klen, int *deleted) {
282*572c4311Sfengbojiang unsigned int zmlen, freelen;
283*572c4311Sfengbojiang unsigned char *p = zipmapLookupRaw(zm,key,klen,&zmlen);
284*572c4311Sfengbojiang if (p) {
285*572c4311Sfengbojiang freelen = zipmapRawEntryLength(p);
286*572c4311Sfengbojiang memmove(p, p+freelen, zmlen-((p-zm)+freelen+1));
287*572c4311Sfengbojiang zm = zipmapResize(zm, zmlen-freelen);
288*572c4311Sfengbojiang
289*572c4311Sfengbojiang /* Decrease zipmap length */
290*572c4311Sfengbojiang if (zm[0] < ZIPMAP_BIGLEN) zm[0]--;
291*572c4311Sfengbojiang
292*572c4311Sfengbojiang if (deleted) *deleted = 1;
293*572c4311Sfengbojiang } else {
294*572c4311Sfengbojiang if (deleted) *deleted = 0;
295*572c4311Sfengbojiang }
296*572c4311Sfengbojiang return zm;
297*572c4311Sfengbojiang }
298*572c4311Sfengbojiang
299*572c4311Sfengbojiang /* Call before iterating through elements via zipmapNext() */
zipmapRewind(unsigned char * zm)300*572c4311Sfengbojiang unsigned char *zipmapRewind(unsigned char *zm) {
301*572c4311Sfengbojiang return zm+1;
302*572c4311Sfengbojiang }
303*572c4311Sfengbojiang
304*572c4311Sfengbojiang /* This function is used to iterate through all the zipmap elements.
305*572c4311Sfengbojiang * In the first call the first argument is the pointer to the zipmap + 1.
306*572c4311Sfengbojiang * In the next calls what zipmapNext returns is used as first argument.
307*572c4311Sfengbojiang * Example:
308*572c4311Sfengbojiang *
309*572c4311Sfengbojiang * unsigned char *i = zipmapRewind(my_zipmap);
310*572c4311Sfengbojiang * while((i = zipmapNext(i,&key,&klen,&value,&vlen)) != NULL) {
311*572c4311Sfengbojiang * printf("%d bytes key at $p\n", klen, key);
312*572c4311Sfengbojiang * printf("%d bytes value at $p\n", vlen, value);
313*572c4311Sfengbojiang * }
314*572c4311Sfengbojiang */
zipmapNext(unsigned char * zm,unsigned char ** key,unsigned int * klen,unsigned char ** value,unsigned int * vlen)315*572c4311Sfengbojiang unsigned char *zipmapNext(unsigned char *zm, unsigned char **key, unsigned int *klen, unsigned char **value, unsigned int *vlen) {
316*572c4311Sfengbojiang if (zm[0] == ZIPMAP_END) return NULL;
317*572c4311Sfengbojiang if (key) {
318*572c4311Sfengbojiang *key = zm;
319*572c4311Sfengbojiang *klen = zipmapDecodeLength(zm);
320*572c4311Sfengbojiang *key += ZIPMAP_LEN_BYTES(*klen);
321*572c4311Sfengbojiang }
322*572c4311Sfengbojiang zm += zipmapRawKeyLength(zm);
323*572c4311Sfengbojiang if (value) {
324*572c4311Sfengbojiang *value = zm+1;
325*572c4311Sfengbojiang *vlen = zipmapDecodeLength(zm);
326*572c4311Sfengbojiang *value += ZIPMAP_LEN_BYTES(*vlen);
327*572c4311Sfengbojiang }
328*572c4311Sfengbojiang zm += zipmapRawValueLength(zm);
329*572c4311Sfengbojiang return zm;
330*572c4311Sfengbojiang }
331*572c4311Sfengbojiang
332*572c4311Sfengbojiang /* Search a key and retrieve the pointer and len of the associated value.
333*572c4311Sfengbojiang * If the key is found the function returns 1, otherwise 0. */
zipmapGet(unsigned char * zm,unsigned char * key,unsigned int klen,unsigned char ** value,unsigned int * vlen)334*572c4311Sfengbojiang int zipmapGet(unsigned char *zm, unsigned char *key, unsigned int klen, unsigned char **value, unsigned int *vlen) {
335*572c4311Sfengbojiang unsigned char *p;
336*572c4311Sfengbojiang
337*572c4311Sfengbojiang if ((p = zipmapLookupRaw(zm,key,klen,NULL)) == NULL) return 0;
338*572c4311Sfengbojiang p += zipmapRawKeyLength(p);
339*572c4311Sfengbojiang *vlen = zipmapDecodeLength(p);
340*572c4311Sfengbojiang *value = p + ZIPMAP_LEN_BYTES(*vlen) + 1;
341*572c4311Sfengbojiang return 1;
342*572c4311Sfengbojiang }
343*572c4311Sfengbojiang
344*572c4311Sfengbojiang /* Return 1 if the key exists, otherwise 0 is returned. */
zipmapExists(unsigned char * zm,unsigned char * key,unsigned int klen)345*572c4311Sfengbojiang int zipmapExists(unsigned char *zm, unsigned char *key, unsigned int klen) {
346*572c4311Sfengbojiang return zipmapLookupRaw(zm,key,klen,NULL) != NULL;
347*572c4311Sfengbojiang }
348*572c4311Sfengbojiang
349*572c4311Sfengbojiang /* Return the number of entries inside a zipmap */
zipmapLen(unsigned char * zm)350*572c4311Sfengbojiang unsigned int zipmapLen(unsigned char *zm) {
351*572c4311Sfengbojiang unsigned int len = 0;
352*572c4311Sfengbojiang if (zm[0] < ZIPMAP_BIGLEN) {
353*572c4311Sfengbojiang len = zm[0];
354*572c4311Sfengbojiang } else {
355*572c4311Sfengbojiang unsigned char *p = zipmapRewind(zm);
356*572c4311Sfengbojiang while((p = zipmapNext(p,NULL,NULL,NULL,NULL)) != NULL) len++;
357*572c4311Sfengbojiang
358*572c4311Sfengbojiang /* Re-store length if small enough */
359*572c4311Sfengbojiang if (len < ZIPMAP_BIGLEN) zm[0] = len;
360*572c4311Sfengbojiang }
361*572c4311Sfengbojiang return len;
362*572c4311Sfengbojiang }
363*572c4311Sfengbojiang
364*572c4311Sfengbojiang /* Return the raw size in bytes of a zipmap, so that we can serialize
365*572c4311Sfengbojiang * the zipmap on disk (or everywhere is needed) just writing the returned
366*572c4311Sfengbojiang * amount of bytes of the C array starting at the zipmap pointer. */
zipmapBlobLen(unsigned char * zm)367*572c4311Sfengbojiang size_t zipmapBlobLen(unsigned char *zm) {
368*572c4311Sfengbojiang unsigned int totlen;
369*572c4311Sfengbojiang zipmapLookupRaw(zm,NULL,0,&totlen);
370*572c4311Sfengbojiang return totlen;
371*572c4311Sfengbojiang }
372*572c4311Sfengbojiang
373*572c4311Sfengbojiang #ifdef REDIS_TEST
zipmapRepr(unsigned char * p)374*572c4311Sfengbojiang static void zipmapRepr(unsigned char *p) {
375*572c4311Sfengbojiang unsigned int l;
376*572c4311Sfengbojiang
377*572c4311Sfengbojiang printf("{status %u}",*p++);
378*572c4311Sfengbojiang while(1) {
379*572c4311Sfengbojiang if (p[0] == ZIPMAP_END) {
380*572c4311Sfengbojiang printf("{end}");
381*572c4311Sfengbojiang break;
382*572c4311Sfengbojiang } else {
383*572c4311Sfengbojiang unsigned char e;
384*572c4311Sfengbojiang
385*572c4311Sfengbojiang l = zipmapDecodeLength(p);
386*572c4311Sfengbojiang printf("{key %u}",l);
387*572c4311Sfengbojiang p += zipmapEncodeLength(NULL,l);
388*572c4311Sfengbojiang if (l != 0 && fwrite(p,l,1,stdout) == 0) perror("fwrite");
389*572c4311Sfengbojiang p += l;
390*572c4311Sfengbojiang
391*572c4311Sfengbojiang l = zipmapDecodeLength(p);
392*572c4311Sfengbojiang printf("{value %u}",l);
393*572c4311Sfengbojiang p += zipmapEncodeLength(NULL,l);
394*572c4311Sfengbojiang e = *p++;
395*572c4311Sfengbojiang if (l != 0 && fwrite(p,l,1,stdout) == 0) perror("fwrite");
396*572c4311Sfengbojiang p += l+e;
397*572c4311Sfengbojiang if (e) {
398*572c4311Sfengbojiang printf("[");
399*572c4311Sfengbojiang while(e--) printf(".");
400*572c4311Sfengbojiang printf("]");
401*572c4311Sfengbojiang }
402*572c4311Sfengbojiang }
403*572c4311Sfengbojiang }
404*572c4311Sfengbojiang printf("\n");
405*572c4311Sfengbojiang }
406*572c4311Sfengbojiang
407*572c4311Sfengbojiang #define UNUSED(x) (void)(x)
zipmapTest(int argc,char * argv[])408*572c4311Sfengbojiang int zipmapTest(int argc, char *argv[]) {
409*572c4311Sfengbojiang unsigned char *zm;
410*572c4311Sfengbojiang
411*572c4311Sfengbojiang UNUSED(argc);
412*572c4311Sfengbojiang UNUSED(argv);
413*572c4311Sfengbojiang
414*572c4311Sfengbojiang zm = zipmapNew();
415*572c4311Sfengbojiang
416*572c4311Sfengbojiang zm = zipmapSet(zm,(unsigned char*) "name",4, (unsigned char*) "foo",3,NULL);
417*572c4311Sfengbojiang zm = zipmapSet(zm,(unsigned char*) "surname",7, (unsigned char*) "foo",3,NULL);
418*572c4311Sfengbojiang zm = zipmapSet(zm,(unsigned char*) "age",3, (unsigned char*) "foo",3,NULL);
419*572c4311Sfengbojiang zipmapRepr(zm);
420*572c4311Sfengbojiang
421*572c4311Sfengbojiang zm = zipmapSet(zm,(unsigned char*) "hello",5, (unsigned char*) "world!",6,NULL);
422*572c4311Sfengbojiang zm = zipmapSet(zm,(unsigned char*) "foo",3, (unsigned char*) "bar",3,NULL);
423*572c4311Sfengbojiang zm = zipmapSet(zm,(unsigned char*) "foo",3, (unsigned char*) "!",1,NULL);
424*572c4311Sfengbojiang zipmapRepr(zm);
425*572c4311Sfengbojiang zm = zipmapSet(zm,(unsigned char*) "foo",3, (unsigned char*) "12345",5,NULL);
426*572c4311Sfengbojiang zipmapRepr(zm);
427*572c4311Sfengbojiang zm = zipmapSet(zm,(unsigned char*) "new",3, (unsigned char*) "xx",2,NULL);
428*572c4311Sfengbojiang zm = zipmapSet(zm,(unsigned char*) "noval",5, (unsigned char*) "",0,NULL);
429*572c4311Sfengbojiang zipmapRepr(zm);
430*572c4311Sfengbojiang zm = zipmapDel(zm,(unsigned char*) "new",3,NULL);
431*572c4311Sfengbojiang zipmapRepr(zm);
432*572c4311Sfengbojiang
433*572c4311Sfengbojiang printf("\nLook up large key:\n");
434*572c4311Sfengbojiang {
435*572c4311Sfengbojiang unsigned char buf[512];
436*572c4311Sfengbojiang unsigned char *value;
437*572c4311Sfengbojiang unsigned int vlen, i;
438*572c4311Sfengbojiang for (i = 0; i < 512; i++) buf[i] = 'a';
439*572c4311Sfengbojiang
440*572c4311Sfengbojiang zm = zipmapSet(zm,buf,512,(unsigned char*) "long",4,NULL);
441*572c4311Sfengbojiang if (zipmapGet(zm,buf,512,&value,&vlen)) {
442*572c4311Sfengbojiang printf(" <long key> is associated to the %d bytes value: %.*s\n",
443*572c4311Sfengbojiang vlen, vlen, value);
444*572c4311Sfengbojiang }
445*572c4311Sfengbojiang }
446*572c4311Sfengbojiang
447*572c4311Sfengbojiang printf("\nPerform a direct lookup:\n");
448*572c4311Sfengbojiang {
449*572c4311Sfengbojiang unsigned char *value;
450*572c4311Sfengbojiang unsigned int vlen;
451*572c4311Sfengbojiang
452*572c4311Sfengbojiang if (zipmapGet(zm,(unsigned char*) "foo",3,&value,&vlen)) {
453*572c4311Sfengbojiang printf(" foo is associated to the %d bytes value: %.*s\n",
454*572c4311Sfengbojiang vlen, vlen, value);
455*572c4311Sfengbojiang }
456*572c4311Sfengbojiang }
457*572c4311Sfengbojiang printf("\nIterate through elements:\n");
458*572c4311Sfengbojiang {
459*572c4311Sfengbojiang unsigned char *i = zipmapRewind(zm);
460*572c4311Sfengbojiang unsigned char *key, *value;
461*572c4311Sfengbojiang unsigned int klen, vlen;
462*572c4311Sfengbojiang
463*572c4311Sfengbojiang while((i = zipmapNext(i,&key,&klen,&value,&vlen)) != NULL) {
464*572c4311Sfengbojiang printf(" %d:%.*s => %d:%.*s\n", klen, klen, key, vlen, vlen, value);
465*572c4311Sfengbojiang }
466*572c4311Sfengbojiang }
467*572c4311Sfengbojiang return 0;
468*572c4311Sfengbojiang }
469*572c4311Sfengbojiang #endif
470