-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_example.c
115 lines (92 loc) · 2.45 KB
/
main_example.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
//
// Created by theofilos on 6/12/2016.
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "hash.h"
#include "../BF/linux/BF.h"
void create_Index(char *fileName, char *attrName, char attrType, int buckets) {
int attrLength = strlen(attrName);
assert(!HT_CreateIndex(fileName, attrType, attrName, attrLength, buckets));
}
HT_info *open_Index(char *fileName) {
HT_info *info;
assert((info = HT_OpenIndex(fileName)) != NULL);
return info;
}
void close_Index(HT_info *info) {
assert(!HT_CloseIndex(info));
}
void insert_Entries(HT_info *info) {
FILE *stream;
char *line = NULL;
size_t len = 0;
ssize_t read;
stream = stdin;
Record record;
while ((read = getline(&line, &len, stream)) != -1) {
line[read - 2] = 0;
char *pch;
pch = strtok(line, ",");
record.id = atoi(pch);
pch = strtok(NULL, ",");
pch++;
pch[strlen(pch) - 1] = 0;
strncpy(record.name, pch, sizeof(record.name));
pch = strtok(NULL, ",");
pch++;
pch[strlen(pch) - 1] = 0;
strncpy(record.surname, pch, sizeof(record.surname));
pch = strtok(NULL, ",");
pch++;
pch[strlen(pch) - 1] = 0;
strncpy(record.city, pch, sizeof(record.city));
assert(!HT_InsertEntry(*info, record));
}
free(line);
}
void get_AllEntries(HT_info *info, void *value) {
assert(HT_GetAllEntries(*info, value) != -1);
}
#define fileName "HT_hashFile"
int main(int argc, char **argv) {
BF_Init();
HT_info *info;
Record record;
// make the record
record.id = 1;
strcpy(record.name, "john");
strcpy(record.surname, "Manios");
strcpy(record.city, "Mpournazi");
//printRecord(&record);
// -- create index
char attrName[20];
int buckets = 256;
strcpy(attrName, "city");
char attrType = 'c';
// strcpy(attrName, "id");
// char attrType = 'i';
create_Index(fileName, attrName, attrType, buckets);
// -- open index
info = open_Index(fileName);
// -- insert entries
insert_Entries(info);
// -- get all entries
char value[20];
strcpy(value, "Keratsini");
get_AllEntries(info, value);
// int value = 11903588;
// get_AllEntries(info, &value);
// -- close index
close_Index(info);
/*
// clean up
free(info->attrName);
free(info);
info = NULL;
*/
HashStatistics(fileName);
return 0;
}