本质上就是char*
字符串
通过指定的格式排列的数据结构
基本格式
1 | <zmlen> // 当zmlen小于254时,记录的是键值对的个数,当等于254时,表示需要遍历整个zipmap才能指导具体的长度 |
1 |
相关文件
- zipmap.h
- zipmap.c
相关概念
void *memmove( void* dest, const void* src, size_t count );
memmove用于从src拷贝count个字节到dest,如果目标区域和源区域有重叠的话,memmove能够保证源串在被覆盖之前将重叠区域的字节拷贝到目标区域中。但复制后src内容会被更改。但是当目标区域与源区域没有重叠则和memcpy函数功能相同。
函数分析
unsigned char *zipmapNew(void);
功能
创建一个空的zipmap
源码
1 | /* Create a new empty zipmap. */ |
unsigned char *zipmapSet(unsigned char *zm, unsigned char *key, unsigned int klen, unsigned char *val, unsigned int vlen, int *update);
功能
insert/update(merge)功能,新增或者修改值
源码
1 | /* Set key to value, creating the key if it does not already exist. |
unsigned char *zipmapDel(unsigned char *zm, unsigned char *key, unsigned int klen, int *deleted);
功能
删除指定键值对
源码
1 | /* Remove the specified key. If 'deleted' is not NULL the pointed integer is |
unsigned char *zipmapRewind(unsigned char *zm);
功能
跳过zm开头的长度记录字节,遍历用的统一起始接口
源码
1 | /* Call before iterating through elements via zipmapNext() */ |
unsigned char *zipmapNext(unsigned char *zm, unsigned char **key, unsigned int *klen, unsigned char **value, unsigned int *vlen);
功能
遍历,获取下一个数据,无数据则返回空
源码
1 | /* This function is used to iterate through all the zipmap elements. |
int zipmapGet(unsigned char *zm, unsigned char *key, unsigned int klen, unsigned char **value, unsigned int *vlen);
功能
根据键,获取值,返回是否找到
源码
1 | /* Search a key and retrieve the pointer and len of the associated value. |
int zipmapExists(unsigned char *zm, unsigned char *key, unsigned int klen);
功能
判断键是否存在
源码
1 | /* Return 1 if the key exists, otherwise 0 is returned. */ |
unsigned int zipmapLen(unsigned char *zm);
功能
获取zipmap的长度
zm[0] < ZIPMAP_BIGLEN则取首地址值,否则遍历计算
源码
1 | /* Return the number of entries inside a zipmap */ |
size_t zipmapBlobLen(unsigned char *zm);
功能
获取zipmap所占的所有字节数(从头到结束)
源码
1 | /* Return the raw size in bytes of a zipmap, so that we can serialize |
static unsigned int zipmapDecodeLength(unsigned char *p);
功能
通用的获取长度值的方法
源码
1 | /* Decode the encoded length pointed by 'p' */ |
static unsigned int zipmapEncodeLength(unsigned char *p, unsigned int len);
功能
根据实际长度,获取到编码后占用的字节长度
源码
1 | /* Encode the length 'l' writing it in 'p'. If p is NULL it just returns |
static unsigned char *zipmapLookupRaw(unsigned char *zm, unsigned char *key, unsigned int klen, unsigned int *totlen);
功能
key存在,查找指定key所在的头指针
totlen存在,返回整个zm占用的字节长度
源码
1 | /* Search for a matching key, returning a pointer to the entry inside the |
static unsigned long zipmapRequiredLength(unsigned int klen, unsigned int vlen);
功能
根据键值长度,计算存储需要的数据量
源码
1 | static unsigned long zipmapRequiredLength(unsigned int klen, unsigned int vlen) { |
static unsigned int zipmapRawKeyLength(unsigned char *p);
功能
获取key的总长度(数据+长度记录)
源码
1 | /* Return the total amount used by a key (encoded length + payload) */ |
static unsigned int zipmapRawValueLength(unsigned char *p);
功能
获取value的总长度(数据+长度记录)
源码
1 | /* Return the total amount used by a value |
static unsigned int zipmapRawEntryLength(unsigned char *p);
功能
获取键值对的完整长度
源码
1 | /* If 'p' points to a key, this function returns the total amount of |
static inline unsigned char *zipmapResize(unsigned char *zm, unsigned int len);
功能
重新设置zm的内存大小(新长度小于就长度会截断)
源码
1 | static inline unsigned char *zipmapResize(unsigned char *zm, unsigned int len) { |