static unsigned int dict_node_hash(const void *key) { const struct gossip_node *node = key; if (node == NULL) return 0; return dictGenHashFunction((unsigned char*)node->dc.data, node->dc.len) + dictGenHashFunction((unsigned char*)node->rack.data, node->rack.len) + node->token.mag[0]; }
static uint64_t intHashCallBack(const void *key) { const unsigned int *i = (const unsigned int *)key; return dictGenHashFunction(i, sizeof(unsigned int)); }
unsigned int dict_string_hash(const void *key) { const struct string *s = key; //return dictGenHashFunction((unsigned char*)key, dn_strlen((char*)key)); if (s == NULL) return 0; return dictGenHashFunction(s->data, s->len); }
/* Functions managing dictionary of callbacks for pub/sub. */ static unsigned int callbackHash(const void *key) { robj *o = (robj *) key; if (o->encoding == UG_ENCODING_RAW) { return dictGenHashFunction(o->ptr, (int)sdslen((sds)o->ptr)); } else { if (o->encoding == UG_ENCODING_INT) { char buf[32]; int len; len = ll2string(buf, 32, (long)o->ptr); return dictGenHashFunction((unsigned char *)buf, len); } else { unsigned int hash; o = getDecodedObject(o); hash = dictGenHashFunction(o->ptr, (int)sdslen((sds)o->ptr)); decrRefCount(o); return hash; } } }
JSObject* HashTable::get(char* key, int keyLength) { unsigned int hash = dictGenHashFunction(key, keyLength); int index = hash % max_size; if (elements[index] != NULL) { HashEntry* brother = elements[index]; do { if (strcmp(brother->key, key) == 0) { return brother->value; } brother = brother->next; } while (brother != NULL); } return NULL; }
JSObject* HashTable::set(char* key, int keyLength, JSObject* value) { HashEntry* element = new HashEntry(); //get from HashEntry pool//to do unsigned int hash = dictGenHashFunction(key, keyLength); int index = hash % max_size; if (elements[index] != NULL) { HashEntry* brother = elements[index]; do { if (strcmp(brother->key, key) == 0) { //free the old value "brother->value" //todo JSObject* oldObject = brother->value; brother->value = value; return oldObject; //replace entry } brother = brother->next; } while (brother != NULL); } element->key = key; element->value = value; element->hash = hash; if (elements[index] == NULL) { elements[index] = element; } else { int level = 1; HashEntry* brother = elements[index]; while (brother->next != NULL) { level++; brother = brother->next; } brother->next = element; element->level = level; } this->length++; if (this->length > this->threshold) { this->resize(); //asynchronous//todo } return NULL; //new entry }
JSObject* HashTable::del(char* key, int keyLength) { unsigned int hash = dictGenHashFunction(key, keyLength); int index = hash % max_size; if (elements[index] != NULL) { HashEntry** brother = elements + index; do { if (strcmp((*brother)->key, key) == 0) { HashEntry* old_element = *brother; //free the old element, put them into a HashEntry pool to reuse them. //todo (*brother) = (*brother)->next; return old_element->value; } brother = &((*brother)->next); } while ((*brother) != NULL); } return NULL; }
/* Functions managing dictionary of callbacks for pub/sub. */ static unsigned int callbackHash(const void *key) { return dictGenHashFunction((unsigned char*)key,sdslen((char*)key)); }
unsigned int dictHash(const void *key) { return dictGenHashFunction((unsigned char*)key, strlen((char*)key)); }
static unsigned int _dictStringCopyHTHashFunction(const void *key) { return dictGenHashFunction(key, strlen(key)); }
//该实现中缺少一些指针的检查,所以使用之前要确保指针的正确性 unsigned int strHash(const void *key) { char *str = (char *)key; return dictGenHashFunction((const unsigned char *)str, strlen(str)); }
static unsigned int dict_dstr_hash(const void *key) { return dictGenHashFunction((unsigned char*)key, (int)dstr_len((dstr)key)); }
static unsigned int hash_func(const void * key) { return dictGenHashFunction(key, strlen(key)); }
// dict's callbacks static unsigned int hash_cb(const void *key) { return dictGenHashFunction((unsigned char *)key, strlen((char *)key)); }
// table from connfd to muxConn static unsigned int intKeyHash(const void *key) { int t = (int)key; return dictGenHashFunction(&t, sizeof(t)); }
static uint64_t sdsHashCallBack(const void *key) { return dictGenHashFunction((unsigned char *)key, sdslen((char *)key)); }
unsigned int dictLookupHashFunction(const void *key) { lookupKey *lk = (lookupKey*)key; unsigned int ret = dictGenHashFunction(lk->name, strlen(lk->name)); return ret ^ (lk->type); }
unsigned int dictStringHash(const void *key) { return dictGenHashFunction(key, strlen(key)); }
/* * murmurhash2. */ unsigned int myHash(const void *key) { unsigned int ret = dictGenHashFunction(key, strlen((char*)key)); //printf("hash key = %u\n", ret); return ret; }
unsigned int dictObjHash(const void *key) { const robj *o = key; return dictGenHashFunction(o->ptr, sdslen((sds)o->ptr)); }