redis源碼分析之dict

#include <stdio.h>
#include <strings.h>
#include <stdlib.h>
#include "dict.h"
int dictSdsKeyCaseCompare(void *privdata, const void *key1, const void *key2)
{
    return strcasecmp(key1, key2) == 0;
}
unsigned int dictSdsCaseHash(const void *key)
{
  return dictGenCaseHashFunction((unsigned char *)key, strlen((char *)key));
}
void dictSdsDestructor(void *privdata, void *val)
{
  //free(val);
}

int main() {
  dictType commandTableDictType = {
    dictSdsCaseHash,       /* hash function */
    NULL,                  /* key dup */
    NULL,                  /* val dup */
    dictSdsKeyCaseCompare, /* key compare */
    dictSdsDestructor,     /* key destructor */
    NULL                   /* val destructor */
  };

  dict* d = dictCreate(&commandTableDictType, NULL);

  char* k = "hello";
  char* v = "world\n";
  dictAdd(d, k, v);

  dictAdd(d, (void*)k, v);
  dictAdd(d, "firstname", "zhou");
  dictAdd(d, "secondname", "jackson");

  printf("firstname=%s\n",dictFetchValue(d,"firstname"));

  dictEntry* entry = dictFind(d, "secondname");
  printf("secondname=%s\n",dictGetVal(entry));

  return 0;
}
相關文章
相關標籤/搜索