-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash.c
94 lines (89 loc) · 2.59 KB
/
hash.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include "hash.h"
static inline unsigned int get_hash_key(char *str) {
unsigned int code = 5381, i;
for (i = 0; i < str[i]; i++) code = (code * 33 + str[i]);
return code;
}
int get_hash(struct hash *mc, char *str) {
int table, idx;
struct hashEntry *he;
unsigned int hash_key = get_hash_key(str);
// for (table = 0; table <= 1; table++) {
for (table = 0; table <= 0; table++) {
idx = hash_key & mc->ht[table].sizemask;
chLog(LOG_NOTICE, "[hash] hash_key: %d, idx: %d, msg: %s", hash_key, idx,
str);
he = mc->ht[table].table[idx];
// 만약 해당 슬롯에 이미 다른 entry가 있을 경우 error return
while (he) {
if (!strcmp(he->key, str)) return -1;
he = he->next;
}
// if (!dictIsRehashing(d)) break;
}
return idx;
}
int hashAdd(struct hash *mc, char *key, struct kvObject *val) {
int index;
struct hashEntry *entry;
index = get_hash(mc, key);
if (index == -1) return -1;
entry = chmalloc(sizeof(*entry));
entry->next = mc->ht[0].table[index];
mc->ht[0].table[index] = entry;
mc->ht[0].used++;
entry->key = key;
entry->val = val;
chLog(LOG_DEBUG, "[hash] Add - hash_key: %d, idx: %d, msg: %s", key, index,
val->ptr->buf);
return 0;
}
struct hashEntry *entryFind(struct hash *mc, struct msg *key) {
int table, idx;
struct hashEntry *he;
unsigned int hash_key = get_hash_key(key->buf);
// for (table = 0; table <= 1; table++) {
for (table = 0; table <= 0; table++) {
idx = hash_key & mc->ht[table].sizemask;
he = mc->ht[table].table[idx];
// 해당 슬롯에서 entry 찾기. 없으면 NULL
while (he) {
if (!strcmp(he->key, key->buf)) {
chLog(LOG_DEBUG, "[hash] Find - hash_key: %d, idx: %d, msg: %s",
hash_key, idx, he->key);
return he;
}
he = he->next;
}
// if (!dictIsRehashing(d)) break;
}
return NULL;
}
int hashDelete(struct hash *mc, struct msg *key) {
int idx;
if (mc->ht[0].used > 0) {
struct hashEntry *he, *prevHe;
unsigned int hash_key = get_hash_key(key->buf);
idx = hash_key & mc->ht[0].sizemask;
he = mc->ht[0].table[idx];
prevHe = NULL;
while (he) {
if (!strcmp(he->key, key->buf)) {
chLog(LOG_NOTICE, "[hash] Delete - hash_key: %d, idx: %d, msg: %s",
hash_key, idx, he->key);
if (prevHe)
prevHe->next = he->next;
else
mc->ht[0].table[idx] = he->next;
chfree(he->key);
chfree(he->val);
chfree(he);
mc->ht[0].used--;
return 0;
}
prevHe = he;
he = he->next;
}
}
return -1;
}