-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconn-hash-tabale.c
More file actions
174 lines (149 loc) · 4.86 KB
/
conn-hash-tabale.c
File metadata and controls
174 lines (149 loc) · 4.86 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <stdbool.h>
#include "conn-hash-tabale.h"
#define INITIAL_TABLE_SIZE 100
#define MAX_LOAD_FACTOR 0.75
typedef struct kvNode {
char *key;
int value;
struct kvNode *next;
} kvNode;
kvNode **hashTable;
int tableSize = INITIAL_TABLE_SIZE;
int numItems = 0;
pthread_mutex_t lock;
// Combined hash function with switch-case for selecting hash type
unsigned int hash(const char *key, int type) {
unsigned long int value = 0;
int i = 0;
switch (type) {
case 1: // First hash function
for (i = 0; key[i]; i++) {
value = value * 37 + key[i];
}
break;
case 2: // Second hash function
for (i = 0; key[i]; i++) {
value = value * 41 + key[i];
}
break;
case 3: // Third hash function
for (i = 0; key[i]; i++) {
value = value * 31 + key[i];
}
break;
default:
return 0; // Invalid hash type
}
return value % tableSize;
}
bool initializeTable() {
hashTable = malloc(sizeof(kvNode*) * tableSize);
if (!hashTable) return false;
for (int i = 0; i < tableSize; i++) {
hashTable[i] = NULL;
}
return pthread_mutex_init(&lock, NULL) == 0;
}
bool addConnection(const char *uuid, int sock) {
if ((double)numItems / tableSize > MAX_LOAD_FACTOR) {
// Placeholder for potential rehashing code if needed
// rehash();
}
pthread_mutex_lock(&lock);
unsigned int index;
kvNode *node;
for (int type = 1; type <= 3; type++) {
index = hash(uuid, type);
node = hashTable[index];
bool exists = false;
while (node) {
if (strcmp(node->key, uuid) == 0) {
exists = true;
break;
}
node = node->next;
}
if (!exists && !hashTable[index]) { // Check if slot is empty and key does not exist
kvNode *newNode = malloc(sizeof(kvNode));
if (!newNode) {
pthread_mutex_unlock(&lock);
return false;
}
newNode->key = strdup(uuid);
newNode->value = sock;
newNode->next = hashTable[index];
hashTable[index] = newNode;
numItems++;
pthread_mutex_unlock(&lock);
return true;
}
}
pthread_mutex_unlock(&lock);
return false; // All hash functions tried and no space found
}
bool findConnection(const char *uuid) {
pthread_mutex_lock(&lock);
for (int type = 1; type <= 3; type++) {
unsigned int index = hash(uuid, type);
kvNode *node = hashTable[index];
while (node) {
if (strcmp(node->key, uuid) == 0) {
pthread_mutex_unlock(&lock);
return true;
}
node = node->next;
}
}
pthread_mutex_unlock(&lock);
return false;
}
bool deleteConnection(const char *uuid) {
pthread_mutex_lock(&lock); // 加锁以保证线程安全
bool found = false;
for (int type = 1; type <= 3; type++) { // 尝试所有的哈希函数
unsigned int index = hash(uuid, type);
kvNode **node = &hashTable[index];
while (*node) {
if (strcmp((*node)->key, uuid) == 0) {
kvNode *temp = *node;
*node = temp->next; // 从链表中移除元素
free(temp->key); // 释放字符串内存
free(temp); // 释放节点内存
numItems--; // 更新存储的元素数量
found = true;
break; // 删除成功后退出循环
}
node = &(*node)->next; // 移动到下一个节点
}
if (found) {
break; // 如果已找到并删除了元素,则不需继续尝试其他哈希函数
}
}
pthread_mutex_unlock(&lock); // 解锁
return found; // 返回是否成功删除元素
}
bool changeConnection(const char *uuid, int newValue) {
pthread_mutex_lock(&lock); // 加锁以保证线程安全
bool found = false;
for (int type = 1; type <= 3; type++) { // 尝试所有的哈希函数
unsigned int index = hash(uuid, type);
kvNode *node = hashTable[index];
while (node) {
if (strcmp(node->key, uuid) == 0) {
node->value = newValue; // 更新节点的值
found = true;
break; // 更新成功后退出循环
}
node = node->next; // 移动到下一个节点
}
if (found) {
break; // 如果已找到并更新了元素,则不需继续尝试其他哈希函数
}
}
pthread_mutex_unlock(&lock); // 解锁
return found; // 返回是否成功更新元素
}