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