一、简介
zipmap是一个为优化Map<String, String>数据结构大小的结构。
- 查找时间复杂度O(n)
- 内存结构紧凑,访问效率高
- 适用少量元素的hash, 一旦元素个数达到一定限制就会转化为正在的hash结构
- 因无总长度字段,所以在插入、删除等需要扩容、缩容的操作中都至少需要遍历一次整个zipmap,以获取总长度
- 插入新kv都是追加方式
二、结构图

三、实现
3.1 宏定义
#define ZIPMAP_BIGLEN 254
#define ZIPMAP_END 255
/* The following defines the max value for the <free> field described in the
* comments above, that is, the max number of trailing bytes in a value. */
#define ZIPMAP_VALUE_MAX_FREE 4
/* The following macro returns the number of bytes needed to encode the length
* for the integer value _l, that is, 1 byte for lengths < ZIPMAP_BIGLEN and
* 5 bytes for all the other lengths. */
#define ZIPMAP_LEN_BYTES(_l) (((_l) < ZIPMAP_BIGLEN) ? 1 : sizeof(unsigned int)+1)
3.2 创建空的zipmap
unsigned char *zipmapNew(void) {
unsigned char *zm = zmalloc(2);
zm[0] = 0; /* Length */
zm[1] = ZIPMAP_END;
return zm;
}

3.3 例1. 插入“name”:“zhangsan”
zipmapSet(zm,(unsigned char*) "name",4, (unsigned char*) "zhangsan",8,NULL);
3.3.1 计算需要的总长度
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;
}
本例中长度都少于254, 所以总长度reqlen = 4 + 8 + 3 = 15
3.3.2 判断key是否存在
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) {
...
}
if (totlen != NULL) *totlen = (unsigned int)(p-zm)+1;
return k;
}
p = zipmapLookupRaw(zm,key,klen,&zmlen);
本例中zm是空的,所以key不存在, zmlen表示整个zipmap的大小, zmlen=2
3.3.3 扩容
zm = zipmapResize(zm, zmlen+reqlen);
static inline unsigned char *zipmapResize(unsigned char *zm, unsigned int len) {
zm = zrealloc(zm, len);
zm[len-1] = ZIPMAP_END;
return zm;
}

3.3.4 定位到结尾(追加方式)
p = zm+zmlen-1;
p = zm + 2 - 1 = zm + 1

3.3.5 计算插入后的zipmap总长度
zmlen = zmlen+reqlen;
zmlen = 2 + 15 = 17
3.3.6 更新元素个数
if (zm[0] < ZIPMAP_BIGLEN) zm[0]++;
当前length=0, 小于254, 所以直接加一

3.3.7 计算空闲字节数
empty = freelen-reqlen;
其中freelen=reqlen为初始值, 所以本例中empty=0
3.3.8 获取free值
本例中,empty=0, 所以vempty = empty = 0
3.3.9 编码key的len
p += zipmapEncodeLength(p,klen);
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);
}
}
}
当前key的长度4,小于254, 所以只需1字节

3.3.10 写入key
memcpy(p,key,klen);
p += klen;

3.3.11 编码value的len
p += zipmapEncodeLength(p,vlen);
value的长度8,只需要一字节

3.3.12 写入空闲free值
*p++ = vempty;

3.3.13 写入value
memcpy(p,val,vlen);

3.4 例2. 插入"age":“18”
zipmapSet(zm,(unsigned char*) "age",3, (unsigned char*) "18",2,NULL);
3.4.1 计算插入需要的总长度
reqlen = zipmapRequiredLength(klen,vlen);
reqlen = 3 + 2 + 3 = 8
3.4.2 查找key元素是否存在
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;
/* Match or skip the key */
l = zipmapDecodeLength(p);
llen = zipmapEncodeLength(NULL,l);
if (key != NULL && k == NULL && l == klen && !memcmp(p+llen,key,l)) {
/* Only return when the user doesn't care
* for the total length of the zipmap. */
if (totlen != NULL) {
k = p;
} else {
return p;
}
}
p += llen+l;
/* Skip the value as well */
l = zipmapDecodeLength(p);
p += zipmapEncodeLength(NULL,l);
free = p[0];
p += l+1+free; /* +1 to skip the free byte */
}
if (totlen != NULL) *totlen = (unsigned int)(p-zm)+1;
return k;
}
//解析长度编码,获取字符串长度
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;
}

- p[0] != ZIPMAP_END
- 获取长度字段,因为小于254, 所以只需要一个字节,读取
l=4 - 编码l,需要多少字节,用于后续跳过长度编码字节,4< 254, 所以
llen=1 - 判断查询的key是否存在,并且k==NULL(
只需要第一个key的位置), 并且长度相同,然后再比较内容,如果都相同,则找到,如果totlen未空,则不需要计算总长度,直接返回,否则继续 - 偏移到value

- 获取value的长度, 8< 254, 所以1字节,直接读取,
l=8 - 根据value长度,对长度进行编码,只需一字节,
llen=1,p = p + 1

- 获取free值,
free=0 - 跳过free 和value,
p += l+1+free = 8 + 1 + 0 = 9

- 进入循环重复步骤
- 这里刚好结束,所以循环结束,如果totlen非空,则填入整个zipmap总长度,
totlen=17 - 返回查找结果
3.4.3 扩容
本例中,key不存在,所以直接扩容17+8=25
zm = zipmapResize(zm, zmlen+reqlen);

3.4.4 定位到结尾位置(追加插入)
p = zm+zmlen-1;

3.4.5 计算新的zipmap总长度
这个只是后续使用,整个zipmap结构中没有存储此信息。
zmlen = zmlen+reqlen;
zmlen = 17 + 8 = 25
3.4.6 更新元素个数
if (zm[0] < ZIPMAP_BIGLEN) zm[0]++;

3.4.7 计算空闲字节
empty = freelen-reqlen;
当前freelen= reqlen, 所以empty=0, 所以vempty = empty = 0
3.4.8 写入长度编码
p += zipmapEncodeLength(p,klen);

3.4.9 写入key
memcpy(p,key,klen);
p += klen;

3.4.10 写入value的长度编码
p += zipmapEncodeLength(p,vlen);

3.4.11 写入free值
*p++ = vempty;

3.4.12 写入value
memcpy(p,val,vlen);

3.5 例3. 修改“name”, “name”:“lisi”
zipmapSet(zm,(unsigned char*) "name",4, (unsigned char*) "lisi",4,NULL);
3.5.1 计算插入元素需要的总长度
reqlen = zipmapRequiredLength(klen,vlen);
reqlen = 4 + 4 + 3 = 11
3.5.2 查找key元素是否存在
p = zipmapLookupRaw(zm,key,klen,&zmlen);

zmlen = 25
本例中,key存在,所以后续步骤有一些不同
3.5.3 获取整个需要修改的kv总长度
freelen = zipmapRawEntryLength(p);
static unsigned int zipmapRawEntryLength(unsigned char *p) {
unsigned int l = zipmapRawKeyLength(p);
return l + zipmapRawValueLength(p+l);
}
static unsigned int zipmapRawKeyLength(unsigned char *p) {
unsigned int l = zipmapDecodeLength(p);
return zipmapEncodeLength(NULL,l) + l;
}
/* Return the total amount used by a value
* (encoded length + single byte free count + payload) */
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;
}
freelen = 15
3.5.4 判断是否需要扩容
if (freelen < reqlen) {
//扩容
...
}
本例中原始长度freelen= 15 > 修改后的长度reqlen=11, 所以不需要扩容
3.5.5 计算空闲值
empty = freelen-reqlen;
empty = 15 - 11 = 4
3.5.6 判断是否需要缩容
if (empty >= ZIPMAP_VALUE_MAX_FREE) {
/* First, move the tail <empty> bytes to the front,
* then resize the zipmap to be <empty> bytes smaller. */
offset = p-zm;
memmove(p+reqlen, p+freelen, zmlen-(offset+freelen+1));
zmlen -= empty;
zm = zipmapResize(zm, zmlen);
p = zm+offset;
vempty = 0;
}
- 判断empty空闲字节是否>= 4, 是则需要缩容
本例中刚好4,所以需要缩容
-
计算偏移量,后续重新定位到修改位置
offse = p - zm = 1 -
移动数据
memmove(p+11, p+15, 25 - (1+15+1)) -> memmove(p+11, p+15, 8)

-
计算修改后的总大小
zmlen -= empty; zmlen=25 - 4 = 21 -
缩容
zm = zipmapResize(zm, zmlen); -> zipmapResize(zm, 21)

-
定位修改位置
p = zm+offset; p = zm + 1

-
设置free为0
vempty = 0;
3.5.7 写入key的长度编码
p += zipmapEncodeLength(p,klen);

3.5.8 写入key
memcpy(p,key,klen);
p += klen;

3.5.9 写入value的长度编码
p += zipmapEncodeLength(p,vlen);

3.5.10 写入free值
*p++ = vempty;

3.5.11 写入value
memcpy(p,val,vlen);

3.6 判断key是否存在
int zipmapExists(unsigned char *zm, unsigned char *key, unsigned int klen) {
return zipmapLookupRaw(zm,key,klen,NULL) != NULL;
}
3.7 整个zipmap的kv对个数
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++;
/* Re-store length if small enough */
if (len < ZIPMAP_BIGLEN) zm[0] = len;
}
return len;
}
- 当个数小于254时,直接返回
- 否则需要遍历整个zipmap
- 当计算后,个数小于254时,重新更新个数(场景,当一直插入,个数超过254时,第一个字节就时0xFE, 以后每次求个数都需要遍历,随着删除操作,个数低于254时,后续就不用遍历了)
3.8 通过key获取value
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;
}
- 首先根据key进行查找,未找到,则返回0
- 找到,则跳过key,赋值长度vlen, 以及value, 返回1
比如:查找“age”
-
查找

-
跳过key

-
获取长度
*vlen = 2 -
获取值

note:这里是直接指向zipmap地址,所以value不要调用free
3.9 根据key删除对应的kv
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);
/* Decrease zipmap length */
if (zm[0] < ZIPMAP_BIGLEN) zm[0]--;
if (deleted) *deleted = 1;
} else {
if (deleted) *deleted = 0;
}
return zm;
}
- 首先查询需要删除的key的位置
- 如果未查询到,则直接返回
- 计算需要删除的kv总大小
- 缩容
- 更新个数
3.9.1 例4. 删除key为"name"
3.9.2 查询key
unsigned char *p = zipmapLookupRaw(zm,key,klen,&zmlen);

zmlen = 21
3.9.3 计算将删除kv的总大小
freelen = zipmapRawEntryLength(p);
freelen = 11
3.9.4 移动数据
memmove(p, p+freelen, zmlen-((p-zm)+freelen+1));
memmove(p, p + 11, 21-(1+11+1)) -> memmove(p, p+11, 8)

3.9.5 缩容
zm = zipmapResize(zm, zmlen-freelen);
zm = zipmapResize(zm, 21-11) -> zipmapResize(zm, 10)

3.9.6 更新个数
if (zm[0] < ZIPMAP_BIGLEN) zm[0]--;

3.10 例5. 修改"age"值从18修改3

可以看到free为1,有1个空闲位置,本次未缩容,当下次改成两位数据时,就不必再次扩容。
ZipMap是一种内存优化的Map实现,主要用于存储少量元素。它具有O(n)的查找时间复杂度,内存结构紧凑,适用于小规模数据。当元素数量达到一定阈值时,会转换为常规的哈希结构。插入和删除操作可能导致遍历整个结构以获取长度,插入采用追加方式。此外,ZipMap在扩容和缩容时需要考虑空间利用率,例如通过移动尾部数据来释放空间。
1248

被折叠的 条评论
为什么被折叠?



