-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake_tables.c
360 lines (315 loc) · 9.82 KB
/
make_tables.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <string.h>
#include <stdbool.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "record.h"
#include "user.h"
#include "message.h"
#include "city.h"
#include "state.h"
#include "timestamp.h"
#include "datestamp.h"
#include "file_count.h"
#define HASH_SIZE 1009
typedef struct timestamp_node {
timestamp_t timestamp;
struct timestamp_node *next;
} timestamp_node;
typedef struct datestamp_node {
datestamp_t datestamp;
struct datestamp_node *next;
} datestamp_node;
typedef struct city_node {
city_t city;
struct city_node *next;
} city_node;
typedef struct state_node {
state_t state;
struct state_node *next;
} state_node;
int main(int argc, char **argv)
{
//print usage if needed
if (argc != 2) {
fprintf(stderr, "Usage: %s totalRecordNumber\n", argv[0]);
exit(0);
}
//get total record number from argument
int totalRecordNumber = atoi(argv[1]);
// time the program
struct timeval sysTimeStart, sysTimeEnd;
gettimeofday(&sysTimeStart, NULL);
// set up directories;
mkdir("cities", 0777);
mkdir("datestamps", 0777);
mkdir("messages", 0777);
mkdir("states", 0777);
mkdir("timestamps", 0777);
mkdir("users", 0777);
mkdir("bplus", 0777);
// set up some counters, etc.
unsigned int recordCount = 0,
userCount = 0,
cityCount = 0,
stateCount = 0,
timestampCount = 0,
datestampCount = 0,
messageCount = 0,
i,j;
// SET UP HASH TABLES FOR CITIES, STATES, TIMESTAMPS, DATESTAMPS
// cities
city_node *cityHT[HASH_SIZE];
for (i = 0; i < HASH_SIZE; i++)
{
cityHT[i] = malloc(sizeof(city_node));
cityHT[i] = NULL;
}
// states
state_node *stateHT[HASH_SIZE];
for (i = 0; i < HASH_SIZE; i++)
{
stateHT[i] = malloc(sizeof(state_node));
stateHT[i] = NULL;
}
// timestamps
timestamp_node *timestampHT[HASH_SIZE];
for (i = 0; i < HASH_SIZE; i++)
{
timestampHT[i] = malloc(sizeof(timestamp_node));
timestampHT[i] = NULL;
}
// datestamps
datestamp_node *datestampHT[HASH_SIZE];
for (i = 0; i < HASH_SIZE; i++)
{
datestampHT[i] = malloc(sizeof(datestamp_node));
datestampHT[i] = NULL;
}
// LOOP OVER RECORD FILES
char filename[1024];
FILE *fp = NULL;
for (i = 0; i < totalRecordNumber; i++) {
//open the corresponding file
sprintf(filename, "record_%06d.dat", i);
fp = fopen(filename,"rb");
if (!fp) {
fprintf(stderr, "Cannot open %s\n", filename);
continue;
}
record_t *record = read_record(fp);
// split location into city and state, as best we can
char cityStr[TEXT_SHORT];
char stateStr[TEXT_SHORT];
// there's one record where the location is \0, which strtok breaks on
if (record->location[0] == '\0')
{
strncpy(cityStr, "", TEXT_SHORT);
strncpy(stateStr, "", TEXT_SHORT);
}
else
{
char loc[TEXT_SHORT];
strncpy(loc, record->location, TEXT_SHORT);
strncpy(cityStr, strtok(loc, ","), TEXT_SHORT);
strncpy(stateStr, strtok(NULL, ","), TEXT_SHORT);
}
// create state
state_t state;
strncpy(state.name, stateStr, TEXT_SHORT);
// get stateId from hash if we have it already
unsigned int stateHash = hash_state(&state) % HASH_SIZE;
state_node *s;
int stateId = -1;
for(s = stateHT[stateHash]; (s != NULL) && (stateId == -1); s = s->next)
{
if (compare_states(&state, &(s->state)) == 0)
{
stateId = s->state.stateId;
}
}
// assign stateId, add to hash table, and write file if we don't have it
if (stateId == -1)
{
state.stateId = stateCount;
stateId = stateCount;
write_state(stateCount, &state);
stateCount++;
s = malloc(sizeof(state_node));
s->state = state;
s->next = stateHT[stateHash];
stateHT[stateHash] = s;
}
// create city
city_t city;
city.stateId = stateId;
strncpy(city.name, cityStr, TEXT_SHORT);
// get cityId from hash if we have it already
unsigned int cityHash = hash_city(&city) % HASH_SIZE;
city_node *c;
int cityId = -1;
for(c = cityHT[cityHash]; (c != NULL) && (cityId == -1); c = c->next)
{
if (compare_cities(&city, &(c->city)) == 0)
{
cityId = c->city.cityId;
}
}
// assign cityId, add to hash table, and write file if we don't have it
if (cityId == -1)
{
city.cityId = cityCount;
cityId = cityCount;
write_city(cityCount, &city);
cityCount++;
c = malloc(sizeof(city_node));
c->city = city;
c->next = cityHT[cityHash];
cityHT[cityHash] = c;
}
// create and write user
user_t user;
user.userId = record->id;
user.cityId = cityId;
user.stateId = stateId;
strncpy(user.name, record->name, TEXT_SHORT);
write_user(userCount, &user);
userCount++;
// loop over messages
for(j = 0; j < record->message_num; j++) {
// create timestamp
timestamp_t timestamp;
timestamp.hour = record->messages[j].hour;
timestamp.minute = record->messages[j].minute;
// get timestampId from hash if we have it already
unsigned int timestampHash = hash_timestamp(×tamp) % HASH_SIZE;
timestamp_node *t;
int tsId = -1;
for(t = timestampHT[timestampHash]; (t != NULL) && (tsId == -1); t = t->next)
{
if (compare_timestamps(×tamp, &(t->timestamp)) == 0)
{
tsId = t->timestamp.timestampId;
}
}
// assign timestampId, add to hash table, and write file if we don't have it
if (tsId == -1)
{
timestamp.timestampId = timestampCount;
tsId = timestampCount;
write_timestamp(timestampCount, ×tamp);
timestampCount++;
t = malloc(sizeof(timestamp_node));
t->timestamp = timestamp;
t->next = timestampHT[timestampHash];
timestampHT[timestampHash] = t;
}
// create datestamp
datestamp_t datestamp;
datestamp.year = record->messages[j].year;
datestamp.month = record->messages[j].month;
datestamp.day = record->messages[j].day;
// get datestampId from hash if we have it already
unsigned int datestampHash = hash_datestamp(&datestamp) % HASH_SIZE;
datestamp_node *d;
int dsId = -1;
for(d = datestampHT[datestampHash]; (d != NULL) && (dsId == -1); d = d->next)
{
if (compare_datestamps(&datestamp, &(d->datestamp)) == 0)
{
dsId = d->datestamp.datestampId;
}
}
// assign datestampId, add to hash table, and write file if we don't have it
if (dsId == -1)
{
datestamp.datestampId = datestampCount;
dsId = datestampCount;
write_datestamp(datestampCount, &datestamp);
datestampCount++;
d = malloc(sizeof(datestamp_node));
d->datestamp = datestamp;
d->next = datestampHT[datestampHash];
datestampHT[datestampHash] = d;
}
// create and write message
message_t message;
strncpy(message.text, record->messages[j].text, TEXT_LONG);
message.userId = user.userId;
message.timestampId = tsId;
message.datestampId = dsId;
message.messageId = messageCount;
write_message(messageCount, &message);
messageCount++;
}
// free and close record
free_record(record);
fclose(fp);
}
// free city nodes
city_node *cNode;
for (i = 0; i < HASH_SIZE; i++)
{
cNode = cityHT[i];
while (cNode != NULL)
{
city_node* tmp = cNode;
cNode = cNode->next;
free (tmp);
}
}
// free state nodes
state_node *sNode;
for (i = 0; i < HASH_SIZE; i++)
{
sNode = stateHT[i];
while (sNode != NULL)
{
state_node* tmp = sNode;
sNode = sNode->next;
free (tmp);
}
}
// free timestamp nodes
timestamp_node *tNode;
for (i = 0; i < HASH_SIZE; i++)
{
tNode = timestampHT[i];
while (tNode != NULL)
{
timestamp_node* tmp = tNode;
tNode = tNode->next;
free (tmp);
}
}
// free datestamp nodes
datestamp_node *dNode;
for (i = 0; i < HASH_SIZE; i++)
{
dNode = datestampHT[i];
while (dNode != NULL)
{
datestamp_node* tmp = dNode;
dNode = dNode->next;
free (tmp);
}
}
// create, write, print file count information file
file_count_t fc;
fc.users = userCount;
fc.cities = cityCount;
fc.states = stateCount;
fc.messages = messageCount;
fc.timestamps = timestampCount;
fc.datestamps = datestampCount;
write_file_count(&fc);
print_file_count(&fc);
// end timing the program
gettimeofday(&sysTimeEnd, NULL);
float totaltime2 = (sysTimeEnd.tv_sec - sysTimeStart.tv_sec)
+ (sysTimeEnd.tv_usec - sysTimeStart.tv_usec) / 1000000.0f;
printf("Process time %f seconds\n", totaltime2);
return 0;
}