-
Notifications
You must be signed in to change notification settings - Fork 0
/
exhash.c
executable file
·222 lines (161 loc) · 5.83 KB
/
exhash.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
#include <stdio.h>
#include <stdlib.h>
#include "BF.h"
#include "exhash.h"
int EH_CreateIndex(char* fileName, char* attrName, char attrType, int attrLength, int depth) {
EH_info info;
void* block;
int i, *index, counter;
if (BF_CreateFile(fileName) < 0)
return -1;
if ((info.fileDesc = BF_OpenFile(fileName)) < 0)
return -1;
info.attrLength = attrLength;
info.attrName = attrName;
info.attrType = attrType;
info.depth = depth;
// -----------------------------------------
// Store the EH struct for type of file
// recognition.
if (BF_AllocateBlock(info.fileDesc) < 0)
return -1;
if (BF_ReadBlock(info.fileDesc, 0, &block) < 0)
return -1;
memcpy(block, &info, sizeof(EH_info));
if (BF_WriteBlock(info.fileDesc, 0) < 0)
return -1;
// -----------------------------------------
// ftiakse block euretiriou
if (BF_AllocateBlock(info.fileDesc) < 0)
return -1;
// ftiakse upoloipa blocks
// stin arxi enas int me posses eggrafes,
// meta int me deixti ston kado uperxeilisis,
// mpenoun oi eggrafes
for (i = 0; i < calc_depth(info.depth); i++) {
if (BF_AllocateBlock(info.fileDesc) < 0)
return -1;
BF_ReadBlock(info.fileDesc, i + 2, &block);
counter = 0;
memcpy(block, &counter, sizeof(int));
counter = -1;
memcpy(block + sizeof(int), &counter, sizeof(int));
counter = info.depth;
memcpy(block + 2 * sizeof(int), &counter, sizeof(int));
BF_WriteBlock(info.fileDesc, i + 2);
}
// -----------------------------------------
// kataskeui euretiriou
if (BF_ReadBlock(info.fileDesc, 1, &block) < 0)
return -1;
index = malloc(calc_depth(info.depth) * sizeof(int));
for (i = 0; i < calc_depth(info.depth); i++)
index[i] = i + 2;
memcpy(block, index, calc_depth(info.depth) * sizeof(int));
if (BF_WriteBlock(info.fileDesc, 1) < 0)
return -1;
// -----------------------------------------
BF_CloseFile(info.fileDesc);
free(index);
return 0;
}
EH_info* EH_OpenIndex(char *fileName) {
EH_info* info;
void *block;
info = malloc(sizeof(EH_info));
BF_ReadBlock(BF_OpenFile(fileName), 0, &block);
memcpy(info, block, sizeof(EH_info));
return info;
}
int EH_CloseIndex(EH_info* header_info) {
if (BF_CloseFile(header_info->fileDesc) < 0)
return -1;
free(header_info);
return 0;
}
int EH_InsertEntry(EH_info* header_info, Record record) {
unsigned long hash_key;
void* block;
int N, counter, local_d, block_num, *index, *index2, i, tmp_key;
char string[20];
N = calc_depth(header_info->depth);
index = malloc(N * sizeof(int));
sprintf(string, "%d", record.id);
hash_key = hash(string);
//printf("%d \n", hash_key);
tmp_key = hash_key % N;
//fortose ton pinaka
BF_ReadBlock(header_info->fileDesc, 1, &block);
memcpy(index, block, N * sizeof(int));
//diavase to block
BF_ReadBlock(header_info->fileDesc, index[tmp_key], &block);
memcpy(&counter, block, sizeof(int));
memcpy(&local_d, block + 2 * sizeof(int), sizeof(int));
//printf("counter: %d \n", counter);
if (counter < BLOCK_SIZE / sizeof(Record)) {
memcpy(block + 3 * sizeof(int) + counter * sizeof(Record), &record, sizeof(Record));
BF_WriteBlock(header_info->fileDesc, index[tmp_key]);
}
else if (counter == BLOCK_SIZE && local_d < header_info->depth) {
printf("kati \n");
}
else if (counter == BLOCK_SIZE && local_d < header_info->depth) {
//diplasiase pinaka, auksise topiko vathos
//perna deiktes kainourgio pinaka
index2 = malloc(2 * N * sizeof(int));
local_d++;
(header_info->depth)++;
N = N * 2;
for (i = 0; i < 2 * N; i++)
if (i < N)
index2[i] = index[i];
else
index2[i] = -1;
tmp_key = hash_key % N;
//an dn uparxei block diomiourgise to
if (index2[tmp_key] == -1) {
BF_AllocateBlock(header_info->fileDesc);
BF_ReadBlock(header_info->fileDesc, BF_GetBlockCounter(header_info->fileDesc) - 1, &block);
counter = 0;
memcpy(block, &counter, sizeof(int));
counter = 1; //gia uperxeilisi
memcpy(block + sizeof(int), &counter, sizeof(int));
counter = header_info->depth;
memcpy(block + 2 * sizeof(int), &counter, sizeof(int));
BF_WriteBlock(header_info->fileDesc, BF_GetBlockCounter(header_info->fileDesc) - 1);
index2[tmp_key] = BF_GetBlockCounter(header_info->fileDesc) - 1;
}
BF_ReadBlock(header_info->fileDesc, index2[tmp_key], &block);
memcpy(block + 3 * sizeof(int), &record, sizeof(Record));
BF_WriteBlock(header_info->fileDesc, index[tmp_key]);
BF_ReadBlock(header_info->fileDesc, 1, &block);
memcpy(block, index2, N*sizeof(int));
BF_WriteBlock(header_info->fileDesc, 1);
}
return 0;
}
int EH_GetAllEntries(EH_info header_info, void *value) {
/* Add your code here */
return -1;
}
int HashStatistics(char* filename) {
/* Add your code here */
return -1;
}
//http://www.cse.yorku.ca/~oz/hash.html
unsigned long hash(char *str) {
unsigned long hash = 5381;
int c;
while ((c = *str++))
hash = ((hash << 5) + hash) + c;
return hash;
}
int calc_depth(int n) {
int i = 0;
int result = 1;
while (i < n) {
result = result * 2;
i++;
}
return result;
}