-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.c
280 lines (240 loc) · 7.4 KB
/
index.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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*============================================================================
# Author: Wade Leng
# E-mail: wade.hit@gmail.com
# Last modified: 2012-02-04 16:59
# Filename: index.c
# Description: this is index
============================================================================*/
#include <stdio.h>
#include <errno.h>
#include "index.h"
#include "layout.h"
#include "type.h"
#include "log.h"
typedef struct IDX_NODE
{
IDX_VALUE_INFO value_node;
PTR_KW left_id, right_id;
HASH hash_2, hash_3;
}IDX_NODE;
extern FILE* log_file;
static PTR_KW* ht_table = NULL;
static IDX_NODE* idx_nodes = NULL;
static PTR_KW* free_idx_nodes_stack = NULL;
static PTR_KW* free_idx_nodes_top;
static HASH hash_func_1(const char* key, int key_size);
static HASH hash_func_2(const char* key, int key_size);
static HASH hash_func_3(const char* key, int key_size);
static int _is_hash_same(HASH hash_x2, HASH hash_x3, HASH hash_y2, HASH hash_y3);
static int _get_free_idx_node();
static void _put_free_idx_node(int node_id);
int idx_init(const char* image, INIT_TYPE init_type)
{
int i;
PTR_KW j;
ht_table = (PTR_KW*)(image + IMAGE_HT_TABLE);
idx_nodes = (IDX_NODE*)(image + IMAGE_IDX_NODES);
free_idx_nodes_stack = (PTR_KW*)(image + IMAGE_FREE_IDX_NODES);
free_idx_nodes_top = (PTR_KW*) (image + IMAGE_FREE_IDX_NODES_HORIZON);
if (init_type == INIT_TYPE_CREATE)
{
for (i = 0; i < IDX_HT_TABLE_SIZE; i++)
ht_table[i] = IDX_NODE_NULL;
for (j = 0; j < IDX_NODES_MAX; j++)
free_idx_nodes_stack[j] = j;
*free_idx_nodes_top = IDX_NODES_MAX - 1;
}
else if (init_type == INIT_TYPE_LOAD)
{
for (i = 0; i < IDX_NODES_MAX; i++)
idx_nodes[i].value_node.buf_ptr = BUF_PTR_NULL;
}
log_err(__FILE__, __LINE__, log_file, "INDEX INIT SUCCESS.");
return 0;
}
int idx_insert(const char* key, int key_size, IDX_VALUE_INFO** insert_node_ptr)
{
int ht_table_id, node_id, new_node_id, cmp;
HASH cur_hash_2, cur_hash_3;
PTR_KW* pre_node_ptr;
cur_hash_2 = hash_func_2(key, key_size);
cur_hash_3 = hash_func_3(key, key_size);
ht_table_id = hash_func_1(key, key_size) % IDX_HT_TABLE_SIZE;
pre_node_ptr = &ht_table[ht_table_id];
node_id = ht_table[ht_table_id];
while (node_id != IDX_NODE_NULL)
{
cmp = _is_hash_same(idx_nodes[node_id].hash_2, idx_nodes[node_id].hash_3, cur_hash_2, cur_hash_3);
if (cmp == 0)
{
log_err(__FILE__, __LINE__, log_file, "idx_insert---key already exist.");
return -1;
} else if (cmp < 0)
{
pre_node_ptr = &(idx_nodes[node_id].left_id);
node_id = idx_nodes[node_id].left_id;
} else
{
pre_node_ptr = &(idx_nodes[node_id].right_id);
node_id = idx_nodes[node_id].right_id;
}
}
new_node_id = _get_free_idx_node();
if (new_node_id == -1)
{
log_err(__FILE__, __LINE__, log_file, "idx_insert---_get_free_idx_node fail.");
return -1;
}
idx_nodes[new_node_id].hash_2 = cur_hash_2;
idx_nodes[new_node_id].hash_3 = cur_hash_3;
idx_nodes[new_node_id].left_id = idx_nodes[new_node_id].right_id = IDX_NODE_NULL;
*pre_node_ptr = new_node_id;
*insert_node_ptr = (IDX_VALUE_INFO*) &(idx_nodes[new_node_id].value_node);
return 0;
}
int idx_search(const char* key, int key_size, IDX_VALUE_INFO** search_node)
{
HASH cur_hash_2, cur_hash_3;
int ht_table_id, node_id, cmp;
(*search_node)->buf_ptr = BUF_PTR_NULL;
(*search_node)->disk_offset = DISK_OFFSET_NULL;
(*search_node)->value_size = -1;
cur_hash_2 = hash_func_2(key, key_size);
cur_hash_3 = hash_func_3(key, key_size);
ht_table_id = hash_func_1(key, key_size) % IDX_HT_TABLE_SIZE;
node_id = ht_table[ht_table_id];
while(node_id != IDX_NODE_NULL)
{
cmp = _is_hash_same(idx_nodes[node_id].hash_2, idx_nodes[node_id].hash_3, cur_hash_2, cur_hash_3);
if (cmp == 0) {
(*search_node)->value_size = idx_nodes[node_id].value_node.value_size;
(*search_node)->buf_ptr = idx_nodes[node_id].value_node.buf_ptr;
(*search_node)->disk_offset = idx_nodes[node_id].value_node.disk_offset;
return 0;
} else if (cmp < 0) {
node_id = idx_nodes[node_id].left_id;
} else {
node_id = idx_nodes[node_id].right_id;
}
}
log_err(__FILE__, __LINE__, log_file, "idx_search---key not found.");
return -1;
}
int idx_delete(const char* key, int key_size, IDX_VALUE_INFO* delete_node)
{
int ht_table_id, node_id, cmp, nearest_id;
HASH cur_hash_2, cur_hash_3;
PTR_KW* pre_node_ptr;
PTR_KW* nearest_pre_node_ptr;
cur_hash_2 = hash_func_2(key, key_size);
cur_hash_3 = hash_func_3(key, key_size);
ht_table_id = hash_func_1(key, key_size) % IDX_HT_TABLE_SIZE;
pre_node_ptr = &ht_table[ht_table_id];
node_id = ht_table[ht_table_id];
while ( 1 )
{
if (node_id == IDX_NODE_NULL)
{
log_err(__FILE__, __LINE__, log_file, "idx_delete---key not found.");
return -1;
}
cmp = _is_hash_same(idx_nodes[node_id].hash_2, idx_nodes[node_id].hash_3, cur_hash_2, cur_hash_3);
if (cmp == 0) {
break;
} else if (cmp < 0) {
pre_node_ptr = &idx_nodes[node_id].left_id;
node_id = idx_nodes[node_id].left_id;
} else {
pre_node_ptr = &idx_nodes[node_id].right_id;
node_id = idx_nodes[node_id].right_id;
}
}
delete_node->buf_ptr = idx_nodes[node_id].value_node.buf_ptr;
delete_node->disk_offset = idx_nodes[node_id].value_node.disk_offset;
delete_node->value_size = idx_nodes[node_id].value_node.value_size;
if (idx_nodes[node_id].left_id == IDX_NODE_NULL && idx_nodes[node_id].right_id == IDX_NODE_NULL) {
*pre_node_ptr = IDX_NODE_NULL;
} else if (idx_nodes[node_id].left_id != IDX_NODE_NULL && idx_nodes[node_id].right_id != IDX_NODE_NULL) {
nearest_pre_node_ptr = &idx_nodes[node_id].right_id;
nearest_id = idx_nodes[node_id].right_id;
while (idx_nodes[nearest_id].left_id != IDX_NODE_NULL) {
nearest_pre_node_ptr = &idx_nodes[nearest_id].left_id;
nearest_id = idx_nodes[nearest_id].left_id;
}
*nearest_pre_node_ptr = idx_nodes[nearest_id].right_id;
idx_nodes[nearest_id].left_id = idx_nodes[node_id].left_id;
idx_nodes[nearest_id].right_id = idx_nodes[node_id].right_id;
*pre_node_ptr = nearest_id;
} else if (idx_nodes[node_id].left_id != IDX_NODE_NULL) {
*pre_node_ptr = idx_nodes[node_id].left_id;
} else {
*pre_node_ptr = idx_nodes[node_id].right_id;
}
_put_free_idx_node(node_id);
return 0;
}
int idx_exit()
{
if (log_file)
log_err(__FILE__, __LINE__, log_file, "INDEX EXIT.");
return 0;
}
static HASH hash_func_1(const char* key, int key_size)
{
unsigned int hash = 5381;
int i =0;
while (i < key_size) {
hash += (hash << 5) + *(key+i);
i++;
}
return (hash & 0x7FFFFFFF);
}
static HASH hash_func_2(const char* key, int key_size)
{
int hash = 0, x = 0, i = 0;
while (i < key_size) {
hash = (hash << 4) + *(key+i);
if ((x = hash & 0xF0000000L) != 0) {
hash ^= (x >> 24);
hash &= ~x;
}
i++;
}
return hash;
}
static HASH hash_func_3(const char* key, int key_size)
{
int hash = 0, i;
for (i=0; i < key_size; i++) {
if ((i & 1) == 0) {
hash ^= ((hash << 7) ^ (*(key+i)) ^ (hash >> 3));
} else {
hash ^= (~((hash << 11) ^ (*(key+i)) ^ (hash >> 5)));
}
}
return hash;
}
static int _is_hash_same(HASH hash_x2, HASH hash_x3, HASH hash_y2, HASH hash_y3)
{
if (hash_y2 == hash_x2)
{
if (hash_y3 == hash_x3)
return 0;
if (hash_y3 < hash_x3)
return -1;
else return 1;
}
if (hash_y2 < hash_x2)
return -1;
return 1;
}
static int _get_free_idx_node()
{
if (*free_idx_nodes_top < 0)
return -1;
return free_idx_nodes_stack[(*free_idx_nodes_top)--];
}
static void _put_free_idx_node(int node_id)
{
free_idx_nodes_stack[++(*free_idx_nodes_top)] = node_id;
}