forked from spatialbloomfilter/libSBF-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-app.cpp
372 lines (310 loc) · 10.5 KB
/
test-app.cpp
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
361
362
363
364
365
366
367
368
369
370
371
372
/*
Spatial Bloom Filter C++ Library (libSBF-cpp)
Copyright (C) 2017 Luca Calderoni, Dario Maio,
University of Bologna
Copyright (C) 2017 Paolo Palmieri,
Cranfield University
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <sbflib.h>
#include <ctime>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <math.h>
#include <stdlib.h>
//This simple program allows to build a SBF over our test datasets and
//to perform some tests upon it.
//Specifically, the filter is tested with its own elements and with a larger set
//of elements which don't belong to the filter at all.
int main() {
std::ifstream myfile;
std::string line, a, member, path;
std::ofstream rate_file;
int len, line_count, area, area_check, n, narea, nver;
int well_recognised, false_positives, iser;
int* area_iser;
int* area_fp;
char* element;
sbf::SBF* myFilter = NULL;
/* ****************************** SETTINGS ****************************** */
//csv file delimiter
std::string delimiter = ",";
int perform_verification = 0;
int print_mode = 0;
//desired false positives probability (upper bound)
double max_fpp = 0.001;
//select the construction dataset to be used
std::string construction_dataset;
//select the verification dataset
std::string verification_dataset;
//specify the hash salt data file
time_t timer;
struct tm * timeinfo;
char buffer[80];
time(&timer);
timeinfo = localtime(&timer);
strftime(buffer, sizeof(buffer), "%d-%m-%Y-%I_%M_%S", timeinfo);
std::string buf(buffer);
std::string hash_salt("SBFHashSalt" + buf + ".txt");
//the exponent that determines filter size (in cells)
int bit_mapping;
//the number of hash executions for each mapped element
int hn;
//hash function to be used
int hf = 4;
/* **************************** END SETTINGS **************************** */
//prints licence information
printf("Spatial Bloom Filters\nCopyright (C) 2017 Luca Calderoni, Dario Maio (University of Bologna), Paolo Palmieri (Cranfield University)\nThis program comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome\nto redistribute it under certain conditions.\nSee the attached files 'COPYING' and 'COPYING.LESSER' for details.\n\n");
/* ***************************** USER INPUT ***************************** */
std::string input;
//asks for construction dataset file (mandatory)
std::cout << "Enter the name of the construction dataset (like area-element-unif.csv)..." << std::endl;
std::cin >> construction_dataset;
std::cin.ignore();
//asks for hash type (optional)
while (true) {
std::cout << "Enter the type of hash function to use:" << std::endl;
std::cout << "1 (SHA1), 4 (MD4), 5(MD5) (press ENTER for default)..." << std::endl;
getline(std::cin, input);
if (input.empty()) break;
else {
std::istringstream istr(input);
istr >> hf;
break;
}
std::cout << "Invalid number, please try again" << std::endl;
}
//asks for hash salt data file (optional)
std::cout << "Enter the name of the hash salt data file (like SBFHashSalt.txt)" << std::endl;
std::cout << "(press ENTER for default)..." << std::endl;
getline(std::cin, input);
if (input.empty()) {
//nothing to do
}
else {
std::istringstream istr(input);
istr >> hash_salt;
}
//asks for verification dataset file (optional)
std::cout << "Enter the name of the verification dataset (like non-elements.csv)" << std::endl;
std::cout << "(press ENTER to ignore)..." << std::endl;
getline(std::cin, input);
if (input.empty()) {
//nothing to do
}
else {
perform_verification = 1;
std::istringstream istr(input);
istr >> verification_dataset;
}
//asks for print mode (optional)
while (true) {
std::cout << "Enter the print mode to use:" << std::endl;
std::cout << "1 (prints filter information to the standard output)" << std::endl;
std::cout << "2 (prints filter information and cells values to the standard output)" << std::endl;
std::cout << "3 (save filter statistics and meta data to disk)" << std::endl;
std::cout << "4 (save both filter and related meta data to disk)" << std::endl;
std::cout << "(press ENTER to ignore)..." << std::endl;
getline(std::cin, input);
if (input.empty()) break;
else {
std::istringstream istr(input);
istr >> print_mode;
if (print_mode != 1 && print_mode != 2 && print_mode != 3 && print_mode != 4) print_mode = 0;
break;
}
std::cout << "Invalid number, please try again" << std::endl;
}
/* *************************** END USER INPUT *************************** */
//determines the number of elements and the number of areas depending on the
//chosen dataset
myfile.open(construction_dataset.c_str());
if (myfile.is_open()) {
line_count = 0;
while (getline(myfile, line)) {
++line_count;
a = line.substr(0, line.find(delimiter));
narea = atoi(a.c_str());
}
n = line_count;
myfile.close();
}
else {
printf("Unable to open file %s", construction_dataset.c_str());
exit(0);
}
//determines the optimal bit_mapping and hash number
int cells = (int)ceil((double)((double)-n*log(max_fpp)) / pow(log(2), 2));
bit_mapping = (int)ceil(log2(cells));
hn = (int)ceil((double)(cells / n)*log(2));
//constructs the filter and fills it with elements contained in the
//input dataset
try {
//filter construction
myFilter = new sbf::SBF(bit_mapping, hf, hn, narea, hash_salt);
}
catch (const std::invalid_argument& ia)
{
std::cerr << ia.what() << std::endl;
}
myfile.open(construction_dataset.c_str());
if (myfile.is_open()) {
//elements insertion
for (int i = 0; i < n; ++i)
{
//reads one line
getline(myfile, line);
a = line.substr(0, line.find(delimiter));
area = atoi(a.c_str());
member = line.substr(line.find(delimiter) + 1);
len = member.length();
element = new char[len];
memcpy(element, member.c_str(), len);
myFilter->Insert(element, len, area);
}
myfile.close();
}
else {
printf("Unable to open file %s", construction_dataset.c_str());
exit(0);
}
//calculates filter's probabilistic properties
myFilter->SetAPrioriAreaFpp();
myFilter->SetAreaFpp();
myFilter->SetAPrioriAreaIsep();
myFilter->SetAreaIsep();
myFilter->SetExpectedAreaCells();
//prints filter to the standard output or saves it to disk
if (print_mode == 1) myFilter->PrintFilter(0);
if (print_mode == 2) myFilter->PrintFilter(1);
if (print_mode == 3) myFilter->SaveToDisk("stats" + buf + ".csv", 1);
if (print_mode == 4) {
myFilter->SaveToDisk("filter" + buf + ".csv", 0);
myFilter->SaveToDisk("stats" + buf + ".csv", 1);
}
//operates a self check upon the filter (i.e. runs the Check method for each
//of the already mapped elements)
well_recognised = 0, iser = 0;
area_iser = new int[narea+1];
for (int a = 0; a < narea + 1; a++) {
area_iser[a] = 0;
}
myfile.open(construction_dataset.c_str());
if (myfile.is_open()) {
printf("Self-check:\n");
for (int i = 0; i < n; i++)
{
//reads one line
getline(myfile, line);
a = line.substr(0, line.find(delimiter));
area = atoi(a.c_str());
member = line.substr(line.find(delimiter) + 1);
len = member.length();
element = new char[len];
memcpy(element, member.c_str(), len);
area_check = myFilter->Check(element, len);
if (area == area_check) well_recognised++;
else {
iser++;
area_iser[area]++;
}
}
printf("Elements assigned to the correct set: %d\n", well_recognised);
printf("Inter-set errors: %d\n", iser);
printf("Inter-set errors rate: %.5f\n", (float)iser / (float)n);
if (print_mode == 3 || print_mode == 4) {
path = "ise" + buf + ".csv";
rate_file.open(path.c_str());
rate_file.setf(std::ios_base::fixed, std::ios_base::floatfield);
rate_file.precision(5);
// area-related parameters:
// area,inter-set errors,inter-set error rate
rate_file << "area" << ";" << "errors" << ";" << "rate" << std::endl;
for (int j = 1; j < narea + 1; j++) {
rate_file << j << ";" << area_iser[j] << ";" << (float)area_iser[j] / (float)myFilter->GetAreaMembers(j) << std::endl;
}
rate_file.close();
}
myfile.close();
}
else {
printf("Unable to open file %s", construction_dataset.c_str());
exit(0);
}
if (perform_verification) {
//determines the number of elements depending on the verification dataset
myfile.open(verification_dataset.c_str());
if (myfile.is_open()) {
line_count = 0;
while (getline(myfile, line)) {
++line_count;
}
nver = line_count;
myfile.close();
}
else {
printf("Unable to open file %s", verification_dataset.c_str());
exit(0);
}
//operates a verification using non members dataset
well_recognised = 0, false_positives = 0;
area_fp = new int[narea + 1];
for (int a = 0; a < narea + 1; a++) {
area_fp[a] = 0;
}
myfile.open(verification_dataset.c_str());
if (myfile.is_open()) {
printf("\nVerification (non-elements):\n");
for (int i = 0; i < nver; i++)
{
//reads one line
getline(myfile, line);
len = line.length();
element = new char[len];
memcpy(element, line.c_str(), len);
area = myFilter->Check(element, len);
if (area == 0) well_recognised++;
else
{
false_positives++;
area_fp[area]++;
}
}
printf("True negatives: %d\n", well_recognised);
printf("False positives: %d\n", false_positives);
printf("False positives rate: %.5f\n", (float)false_positives / (float)nver);
if (print_mode == 3 || print_mode == 4) {
path = "fp" + buf + ".csv";
rate_file.open(path.c_str());
rate_file.setf(std::ios_base::fixed, std::ios_base::floatfield);
rate_file.precision(5);
// area-related parameters:
// area,false positives,false positives rate
rate_file << "area" << ";" << "false positives" << ";" << "rate" << std::endl;
for (int j = 1; j < narea + 1; j++) {
rate_file << j << ";" << area_fp[j] << ";" << (float)area_fp[j] / (float)nver << std::endl;
}
rate_file.close();
}
myfile.close();
}
else {
printf("Unable to open file %s", verification_dataset.c_str());
exit(0);
}
}
printf("Press any key to continue\n");
getline(std::cin, input);
return 0;
}