zipmap.c  代码实现:

#include <stdio.h>
#include <string.h>
#include "zmalloc.h"
#include "endianconv.h"#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)/* Create a new empty zipmap. */
unsigned char *zipmapNew(void) {unsigned char *zm = zmalloc(2);zm[0] = 0; /* Length */zm[1] = ZIPMAP_END;return zm;
}/* Decode the encoded length pointed by 'p' */
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;
}/* Encode the length 'l' writing it in 'p'. If p is NULL it just returns* the amount of bytes required to encode such a length. */
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);}}
}/* Search for a matching key, returning a pointer to the entry inside the* zipmap. Returns NULL if the key is not found.** If NULL is returned, and totlen is not NULL, it is set to the entire* size of the zimap, so that the calling function will be able to* reallocate the original zipmap to make room for more entries. */
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 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;
}/* Return the total amount used by a key (encoded length + payload) */
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;
}/* If 'p' points to a key, this function returns the total amount of* bytes used to store this entry (entry = key + associated value + trailing* free space if any). */
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;
}/* Set key to value, creating the key if it does not already exist.* If 'update' is not NULL, *update is set to 1 if the key was* already preset, otherwise to 0. */
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) {/* Key not found: enlarge */zm = zipmapResize(zm, zmlen+reqlen);p = zm+zmlen-1;zmlen = zmlen+reqlen;/* Increase zipmap length (this is an insert) */if (zm[0] < ZIPMAP_BIGLEN) zm[0]++;} else {/* Key found. Is there enough space for the new value? *//* Compute the total length: */if (update) *update = 1;freelen = zipmapRawEntryLength(p);if (freelen < reqlen) {/* Store the offset of this key within the current zipmap, so* it can be resized. Then, move the tail backwards so this* pair fits at the current position. */offset = p-zm;zm = zipmapResize(zm, zmlen-freelen+reqlen);p = zm+offset;/* The +1 in the number of bytes to be moved is caused by the* end-of-zipmap byte. Note: the *original* zmlen is used. */memmove(p+reqlen, p+freelen, zmlen-(offset+freelen+1));zmlen = zmlen-freelen+reqlen;freelen = reqlen;}}/* We now have a suitable block where the key/value entry can* be written. If there is too much free space, move the tail* of the zipmap a few bytes to the front and shrink the zipmap,* as we want zipmaps to be very space efficient. */empty = freelen-reqlen;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;} else {vempty = empty;}/* Just write the key + value and we are done. *//* Key: */p += zipmapEncodeLength(p,klen);memcpy(p,key,klen);p += klen;/* Value: */p += zipmapEncodeLength(p,vlen);*p++ = vempty;memcpy(p,val,vlen);return zm;
}/* Remove the specified key. If 'deleted' is not NULL the pointed integer is* set to 0 if the key was not found, to 1 if it was found and deleted. */
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;
}/* Call before iterating through elements via zipmapNext() */
unsigned char *zipmapRewind(unsigned char *zm) {return zm+1;
}/* This function is used to iterate through all the zipmap elements.* In the first call the first argument is the pointer to the zipmap + 1.* In the next calls what zipmapNext returns is used as first argument.* Example:** unsigned char *i = zipmapRewind(my_zipmap);* while((i = zipmapNext(i,&key,&klen,&value,&vlen)) != NULL) {*     printf("%d bytes key at $p\n", klen, key);*     printf("%d bytes value at $p\n", vlen, value);* }*/
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;
}/* Search a key and retrieve the pointer and len of the associated value.* If the key is found the function returns 1, otherwise 0. */
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;
}/* Return 1 if the key exists, otherwise 0 is returned. */
int zipmapExists(unsigned char *zm, unsigned char *key, unsigned int klen) {return zipmapLookupRaw(zm,key,klen,NULL) != NULL;
}/* Return the number of entries inside a zipmap */
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;
}/* Return the raw size in bytes of a zipmap, so that we can serialize* the zipmap on disk (or everywhere is needed) just writing the returned* amount of bytes of the C array starting at the zipmap pointer. */
size_t zipmapBlobLen(unsigned char *zm) {unsigned int totlen;zipmapLookupRaw(zm,NULL,0,&totlen);return totlen;
}#ifdef ZIPMAP_TEST_MAIN
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");
}int main(void) {unsigned char *zm;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

reids 源码 zipmap.c 压缩map的实现相关推荐

  1. Linux服务器开发,Reids源码 主从同步与对象模型

    ──────────────────────────────────────────────────────────────── ┌------------┐ │▉▉♥♥♥♥♥♥♥♥ 99% │ ♥❤ ...

  2. linux内存源码分析 - 内存压缩(同步关系)

    本文为原创,转载请注明:http://www.cnblogs.com/tolimit/ 概述 最近在看内存回收,内存回收在进行同步的一些情况非常复杂,然后就想,不会内存压缩的页面迁移过程中的同步关系也 ...

  3. linux 源码编译upx 压缩软件

    UPX(the Ultimate Packer for eXecutables)是一个非常全面的可执行文件压缩软件,支持dos/exe.dos/com.dos/sys.djgpp2/coff. wat ...

  4. c++ map 获取key列表_好未来Golang源码系列一:Map实现原理分析

    分享老师:学而思网校 郭雨田 一.map的结构与设计原理 golang中map是一个kv对集合.底层使用hash table,用链表来解决冲突 ,出现冲突时,不是每一个key都申请一个结构通过链表串起 ...

  5. redis源码之字符串压缩

    redis关于字符串压缩的几个文件分别是:lzf.h,lzfP.h,lzf_c.c,lzf_d.c,下面看一个测试用例. #include <iostream> #include < ...

  6. 利用三星S3C6410源码实现同时压缩视频和图片

    前段时间实现了利用三星S3C6410一边压缩视频生成H264文件一边抓取并压缩生成jpg图片.核心步骤如下: 视频压缩和图片压缩利用同一个handle. /* Codec set *//* Get c ...

  7. redis 源码 ziplist.c 压缩list的实现

    ziplist.c代码实现: #include <stdio.h> #include <stdlib.h> #include <string.h> #include ...

  8. reids源码 t_hash.c 实现

    t_hash.c文件代码: #include "redis.h" #include <math.h>/*-------------------------------- ...

  9. 源码解读_Go Map源码解读之Map迭代

    点击上方蓝色"后端开发杂谈"关注我们, 专注于后端日常开发技术分享 map 迭代 本文主要是针对map迭代部分的源码分析, 可能篇幅有些过长,且全部是代码, 请耐心阅读. 源码位置 ...

最新文章

  1. 读盘写盘计算机里面的意义,什么叫计算机里的写盘
  2. PHP一个比较完善的树形结构代码
  3. oracle存储过程季度方法,Oracle存储过程、触发器实现获取时间段内周、月、季度的具体时间...
  4. 模拟实现string其中的一些知识点
  5. Delphi WebService 的编写、调试、发布(IIS)、调用
  6. 《iOS 6核心开发手册(第4版)》——1.13节秘诀:从滚动视图中拖动
  7. 颠覆三观,内存真能当SSD用了!!!
  8. java设置word页面为A3_word页面怎么设置为A3打印格式
  9. [数据可视化] 饼图(Pie Chart)
  10. MSDOS(MBR)、GPT、BIOS、UEFI
  11. BZOJ1577: [Usaco2009 Feb]庙会捷运Fair Shuttle 贪心+线段树
  12. Centos 7 根目录存储容量调整大小
  13. 王者荣耀服务器维护费用,王者荣耀服务器全线崩溃!事后只补偿100铭文!网友:卸载了...
  14. Java封装的四个关键字
  15. 浅谈FromHandle
  16. Keras.layers.core.dense()方法详解
  17. 1060显卡支持dx12吗_GTX1660和GTX1060哪个性价比高?GTX1060和GTX1660显卡区别对比
  18. 智能机器人技术综合实训课程说明
  19. 什么是最牛逼的代码?
  20. python 指纹_详解Python3之数据指纹MD5校验与对比

热门文章

  1. no module named 'social_core'
  2. Django CMS介绍(转载)
  3. fatal: unable to access 'xxxxxxxxxxxxx':The requested URL returned error: 403
  4. linux下面使用gparted进行格式化
  5. 基于numpy的多项式拟合预测人口数值
  6. 我心目中未来的计算机200字,我心目中未来的计算机.doc
  7. vuex的计算属性_Vuex详细介绍
  8. linux系统中扩展一个逻辑卷,Linux 创建及扩展逻辑卷
  9. bwlabel算法_bwlabel函数的c++实现
  10. VISTA中释放系统还原占用的硬盘空间