zoukankan      html  css  js  c++  java
  • 基于C语言的ssdb笔记 ----hashmap的简单实例

    ssdb支持 zset, map/hash, list, kv 数据结构,同redis差不多。下面是关于ssdb hsahmap的使用笔记

    1.ssdb hashmap的命令

    1.hset name key value

    设置hashmap中指定key的值

    2.hget name key

    获取hashmap中指定key的值

    3.hdel name key

    删除hashmap中指定的key

    4.hincr name key [num]

    使hashmap中key对应的值增加num

    5.hexists name key

    判断指定的key是否存在于hashmap中

    6.hsize name

    返回hashmap中元素的个数

    7.hlist name_start name_end limit

    列出名字处于区间 (name_start, name_end] 的 hashmap.

    8.hrlist name_start name_end limit

    逆序

    9.hkeys name key_start key_end

    列出 hashmap 中处于区间 (key_start, key_end] 的 key 列表.

    10.hgetall name

    返回整个 hashmap.

    11.hscan name key_start key_end limit

    列出 hashmap 中处于区间 (key_start, key_end] 的 key-value 列表.

    12.hrscan name key_start key_end limit

    像 hscan, 逆序.

    13.hclear name

    删除 hashmap 中的所有 key.

    14.multi_hset name key1 value1 key2 value2 ...
    批量设置 hashmap 中的 key-value.

    15.multi_hget name key1 key2 ...
    批量获取 hashmap 中多个 key 对应的权重值.

    16.multi_hdel name key1 key2 ...

    指删除 hashmap 中的 key.

    2.ssdb使用hashmap 存储获取的代码

      这里因为ssdb是完全兼容redis的,所以完全可以用redis的库的接口函数。因为这样的特性,使得使用redis的项目很容易的能迁移到ssdb,作者的原博客也有完整的redis迁移到ssdb的完整教程。

    ssdb的连接

    redisContext *conn = redisConnect(host,port);
    host:连接ssdb的主机ip
    port:连接端口

    返回值 redisContext结构体 定义如下
    /* Context for a connection to Redis */
     2 typedef struct redisContext {
     3     int err; /* Error flags, 0 when there is no error */
     4     char errstr[128]; /* String representation of error when applicable */
     5     int fd; 
     6     int flags;
     7     char *obuf; /* Write buffer */
     8     redisReader *reader; /* Protocol reader */
     9 
    10     enum redisConnectionType connection_type;
    11     struct timeval *timeout;
    12 
    13     struct {
    14         char *host;
    15         char *source_addr;
    16         int port;
    17     } tcp;
    18 
    19     struct {
    20         char *path;
    21     } unix_sock;
    22 
    23 } redisContext;

    ssdb的命令执行
    redisReply* reply = redisCommand(conn,cmd)
    conn:redis的连接句柄
    cmd:执行的命令
    返回值:redisReply结构体 结构体如下:
    1 /* This is the reply object returned by redisCommand() */
    2 typedef struct redisReply {
    3     int type; /* REDIS_REPLY_* */
    4     long long integer; /* The integer when type is REDIS_REPLY_INTEGER */
    5     size_t len; /* Length of string */
    6     char *str; /* Used for both REDIS_REPLY_ERROR and REDIS_REPLY_STRING */
    7     size_t elements; /* number of elements, for REDIS_REPLY_ARRAY */
    8     struct redisReply **element; /* elements vector for REDIS_REPLY_ARRAY */
    9 } redisReply;

    其中type元素的类型可用来判断放回结果的状态:
    REDIS_REPLY_STRING 1   //返回字符串,查看str,len字段
    REDIS_REPLY_ARRAY 2    //返回一个数组,查看elements的值(数组个数),通过element[index]的方式访问数组元素,每个数组元素是一个redisReply对象的指针
    REDIS_REPLY_INTEGER 3  //返回整数,从integer字段获取值
    REDIS_REPLY_NIL 4      //没有数据返回
    REDIS_REPLY_STATUS 5   //表示状态,内容通过str字段查看,字符串长度是len字段
    REDIS_REPLY_ERROR 6    //表示出错,查看出错信息,如上的str,len字段

    redisReply的释放
    freeReplyObject(reply); 
    用来释放执行命令后所占用的内存

    ssdb连接的释放
    redisFree(conn);

    完整代码如下:
    
    
     1 redisContext*  ssdb_init(const char* host,uint16_t port) {
     2      redisContext *conn = redisConnect(host,port);
     3      if(conn->err) {
     4         printf("connection error:%s
    ",conn->errstr);
     5         redisFree(conn);
     6         return NULL;  
     7     }
     8     return conn;    
     9 }
    10 
    11 void ssdb_finit(redisContext *conn) { 
    12      if (conn)
    13          redisFree(conn);
    14 }
    15 
    16 redisReply* ssdb_command(redisContext *conn,char *cmd) {
    17      redisReply* reply = NULL;
    18      if(NULL != conn && cmd != NULL) {
    19          reply = redisCommand(conn,cmd);
    20      } else {
    21          printf("redis err
    ");
    22          return NULL;
    23      }
    24      return reply;
    25 }
    26 
    27 int main(){
    28      char buf[1024] = {0};
    29      char name[1024] = {0};
    30      char key[1024] = {0};
    31      char value[1024] = {0};
    32      redisReply* reply = NULL;
    33      redisContext *ssdb_conn = ssdb_init("127.0.0.1",8888);
    34      if(ssdb_conn == NULL){
    35          printf("ssdb conn err...
    ");
    36          return 0;
    37      }
    38      memcpy(name,"mymap",5);
    39      memcpy(key,"name",4);
    40      memcpy(value,"wangwu",6);
    41      snprintf(buf,sizeof(buf),"hset %s %s %s",name,key,value);
    42      reply = ssdb_command(ssdb_conn,buf);
    43      if(reply != NULL){
    44          if(reply->type == REDIS_REPLY_ERROR)
    45              printf("commd err:%s
    ",reply->str);
    46          freeReplyObject(reply);
    47      }
    48  
    49      //根据指定key获取value值
    50      memset(buf,0,sizeof(buf));
    51     memcpy(buf,"hget mymap name",sizeof(buf));         
    52      reply = ssdb_command(ssdb_conn,buf);
    53      if(reply == NULL)
    54          return 0;
    55      if(reply->type == REDIS_REPLY_ERROR){
    56          printf("commd err:%s
    ",reply->str);
    57          freeReplyObject(reply);
    58          return -1;
    59      }
    60      printf("the value this is:%s
    ",reply->str);
    61      freeReplyObject(reply);
    62 
    63      //获取mymap里所有的key-value值
    64      memset(buf,0,sizeof(buf));
    65      memcpy(buf,"hgetall mymap",sizeof(buf));
    66      reply = ssdb_command(ssdb_conn,buf);
    67      if(reply == NULL)
    68          return 0;
    69      int i = 0;
         /*此处对于获取的hashmap来说这个返回的数组每个key-value是占有两个元素的第一个元素为key 后一个元素为前一个元素的值*/
    70 for(i = 0;i < reply->elements;i+=2){ 71 printf("key:%s values:%s ",reply->element[i]->str,reply->element[i + 1]->str); 72 } 73 74 freeReplyObject(reply); 75 return 0; 76 }

    执行效果图如下:

  • 相关阅读:
    后台点赞 接口
    三表联查
    后台投票 接口
    MSXML insertBefore(IXMLDOMNode *newChild, VARIANT refChild) 传参
    WTL中菜单栏及工具栏项状态改变应注意的地方
    使用WTL的消息反射封装CEdit实现监听控件文本改变事件
    修改字体
    CEdit实现文本换行
    VC中获取窗口控件相对客户区的坐标
    关闭HTC手机充电时屏幕一直亮着绿色电池的办法
  • 原文地址:https://www.cnblogs.com/piaoyang/p/9267551.html
Copyright © 2011-2022 走看看