-
Notifications
You must be signed in to change notification settings - Fork 0
/
FBF.cpp
513 lines (476 loc) · 14.2 KB
/
FBF.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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
/*
* FBF.cpp
*
* Created on: Nov 20, 2014
* Author: rajatsub
*/
#include "FBF.h"
/**
* Overloaded Constructor
*/
FBF::FBF(unsigned long number, unsigned long long int tableSize, unsigned int numOfHashes, unsigned long refreshTime, double appProvidedFPR, bool enableDynamicResizing) {
//trace.funcEntry("FBF::FBF");
parameters.projected_element_count = PROJECTED_ELEMENT_COUNT;
parameters.false_positive_probability = FALSE_POSITIVE_PROBABILITY;
parameters.random_seed = RANDOM_SEED;
if ( !parameters ) {
cout<<" ERROR :: Invalid parameters"<<endl;
}
parameters.compute_optimal_parameters(tableSize, numOfHashes);
fbf.clear();
for ( int counter = 0; counter < number; counter++ ) {
fbf.emplace_back(bloom_filter(parameters));
fbf[counter].clear();
}
cout<<" INFO :: "<<fbf.size()<<" constituent bloom filters initialized in the FBF"<<endl;
future = FUTURE;
present = future + 1;
pastStart = future + 2;
numberOfBFs = fbf.size();
pastEnd = numberOfBFs - 1;
refreshRate = refreshTime;
maxTolerableFPR = appProvidedFPR;
this->enableDynamicResizing = enableDynamicResizing;
stopRefreshFBFThread = false;
stopdynamicResizingThread = false;
thresholdFraction = UNSAFE_THRESHOLD_FRACTION;
safeThresholdFraction = SAFE_THRESHOLD_FRACTION;
multiplicativeBloomFilterIncrement = MULTIPLICATIVE_BLOOM_FILTER_INCREMENT;
//additiveBloomFilterIncrement = ADDITIVE_BLOOM_FILTER_INCREMENT;
minRefreshRate = MIN_REFRESH_RATE;
additiveRefreshRateDecrement = ADDITIVE_REFRESH_RATE_DECREMENT;
minimumNumberOfBFs = MINIMUM_NUMBER_OF_BFS;
additiveBloomFilterDecrement = ADDITIVE_BLOOM_FILTER_DECREMENT;
additiveRefreshRateIncrement = ADDITIVE_REFRESH_RATE_INCREMENT;
cout<<" INFO :: FBFs initialized"<<endl;
/*
* Start the refresh FBF and dynamic resizing threads
*/
startThreads();
//trace.funcExit("FBF::FBF");
}
/**
* Destructor
*/
FBF::~FBF() {
//trace.funcEntry("FBF::~FBF");
stopRefreshFBFThread = true;
stopdynamicResizingThread = true;
refreshFBFThread.join();
if ( enableDynamicResizing ) {
dynamicResizingThread.join();
}
//trace.funcExit("FBF::~FBF");
}
/**
* FUNCTION NAME: insert
*
* DESCRIPTION: Insert element into the FBF
*/
void FBF::insert(unsigned long long int element) {
//trace.funcEntry("FBF::insert");
lock_guard<std::mutex> guard(fbfMutex);
fbf[present].insert(element);
fbf[future].insert(element);
//trace.funcExit("FBF::insert");
}
/**
* FUNCTION NAME: membershipCheck
*
* DESCRIPTION: Membership check function of the FBF
*/
bool FBF::membershipCheck(unsigned long long int element) {
//trace.funcEntry("FBF::membershipCheck");
bool found = false;
unsigned long lastPast = getPastEnd();
if ( fbf[future].contains(element) && fbf[present].contains(element) ) {
found = true;
}
else if ( fbf[present].contains(element) && fbf[pastStart].contains(element) ) {
found = true;
}
else if ( lastPast > pastStart ) {
for ( int j = pastStart; j <= (lastPast - 1); j++ ) {
if ( (fbf[j].contains(element) && fbf[j+1].contains(element)) ) {
found = true;
break;
}
}
}
else if ( fbf[lastPast].contains(element) ) {
found = true;
}
//trace.funcExit("FBF::membershipCheck");
return found;
}
/**
* FUNCTION NAME: startThreads
*
* DESCRIPTION: This function starts the FBF refresh as well as the dynamic resizing
* threads
*/
void FBF::startThreads() {
//trace.funcEntry("FBF::startThreads");
refreshFBFThread = thread(&FBF::refreshFunc, this);
if ( enableDynamicResizing ) {
dynamicResizingThread = thread(&FBF::dynamicResizingFunc, this);
}
//trace.funcExit("FBF::startThreads");
}
/**
* FUNCTION NAME: stopThreads
*
* DESCRIPTION: This function stops the FBF refresh as well as the dyanmic resizing threads
*/
void FBF::stopThreads() {
//trace.funcEntry("FBF::stopThreads");
stopRefreshFBFThread = true;
stopdynamicResizingThread = true;
//trace.funcExit("FBF::stopThreads");
}
/**
* FUNCTION NAME: refreshFunc
*
* DESCRIPTION: Thread function of refreshFBFThread
*/
void FBF::refreshFunc() {
//trace.funcEntry("FBF::refreshFunc");
cout<<endl<<" INFO :: Starting the refresh FBF thread"<<endl;
while (!stopRefreshFBFThread) {
refresh();
this_thread::sleep_for(chrono::seconds(getRefreshRate()));
}
//trace.funcExit("FBF::refreshFunc");
}
/**
* FUNCTION NAME: refresh
*
* DESCRIPTION: This function refreshes the FBF
*/
void FBF::refresh() {
//trace.funcEntry("FBF::refresh");
lock_guard<std::mutex> guard(fbfMutex);
fbf.insert(fbf.begin(), bloom_filter(parameters));
fbf.pop_back();
cout<<" INFO :: Refreshed FBF"<<endl;
//trace.funcExit("FBF::refresh");
}
/**
* FUNCTION NAME: dynamicResizingFunc
*
* DESCRIPTION: This function controls the dynamic resizing functionality
*/
void FBF::dynamicResizingFunc() {
//trace.funcEntry("FBF::dynamicResizingFunc");
cout<<endl<<" INFO :: Starting dynamic resizing thread"<<endl;
while (!stopdynamicResizingThread) {
checkFalsePositiveRate();
this_thread::sleep_for(chrono::seconds(DYNAMIC_RESIZING_CHECK_FREQUENCY_SECONDS));
}
//trace.funcExit("FBF::dynamicResizingFunc");
}
/**
* FUNCTION NAME: checkFalsePositiveRate
*
* DESCRIPTION: This function checks the current false positive rate of the FBF
* and decides whether to trigger dynamic resizing
*/
void FBF::checkFalsePositiveRate() {
//trace.funcEntry("FBF::checkFalsePositiveRate");
double currentFPR = checkEffectiveFPR();
if ( currentFPR > thresholdFraction * maxTolerableFPR ) {
scaleUp();
}
else if ( currentFPR <= safeThresholdFraction * maxTolerableFPR ) {
scaleDown();
}
//trace.funcExit("FBF::checkFalsePositiveRate");
}
/**
* FUNCTION NAME: scaleUp
*
* DESCRIPTION: This function expands the FBF and decreases the refresh rate
*/
void FBF::scaleUp() {
//trace.funcEntry("FBF::scaleUp");
fbfScaleUp();
numberOfBFs = fbf.size();
pastEnd = numberOfBFs - 1;
if ( getRefreshRate() > minRefreshRate ) {
setRefreshRate(getRefreshRate() - additiveRefreshRateDecrement);
}
cout<<endl<<"new rr: "<<getRefreshRate()<<endl;
cout<<endl<<"scale up"<<endl;
//trace.funcExit("FBF::scaleUp");
}
/**
* FUNCTION: fbfScaleUp
*
* DESCRIPTION: This function expands the FBF
*/
void FBF::fbfScaleUp() {
size_t currentFBFsize = fbf.size();
lock_guard<std::mutex> guard(fbfMutex);
for ( int counter = currentFBFsize; counter < currentFBFsize * multiplicativeBloomFilterIncrement; counter++ ) {
fbf.emplace_back(bloom_filter(parameters));
}
}
/**
* FUNCTION NAME: scaleDown
*
* DESCRIPTION: THis function shrinks the FBF and increases the refresh rate
*/
void FBF::scaleDown() {
//trace.funcEntry("FBF::scaleDown");
size_t currentFBFsize = fbf.size();
if ( currentFBFsize > (minimumNumberOfBFs + additiveBloomFilterDecrement) ) {
fbfScaleDown();
}
numberOfBFs = fbf.size();
pastEnd = numberOfBFs - 1;
setRefreshRate(getRefreshRate() + additiveRefreshRateIncrement);
//trace.funcExit("FBF::scaleDown");
}
/**
* FUNCTION NAME: fbfScaleDown
*
* DESCRIPTION: This function shrinks the FBF
*/
void FBF::fbfScaleDown() {
lock_guard<std::mutex> guard(fbfMutex);
for ( int counter = 0; counter < additiveBloomFilterDecrement; counter++ ) {
fbf.pop_back();
}
}
/**
* FUNCTION NAME: getRefreshRate
*
* DESCRIPTION: This function returns the refresh rate of the FBF
*/
unsigned long FBF::getRefreshRate() {
//trace.funcEntry("FBF::getRefreshRate");
//trace.funcExit("FBF::getRefreshRate", (int)this->refreshRate);
lock_guard<std::mutex> guard(rrMutex);
return this->refreshRate;
}
/**
* FUNCTION NAME: setRefreshRate
*
* DESCRIPTION: This function sets the refrehs rate of the FBF
*/
bool FBF::setRefreshRate(unsigned long newRR) {
//trace.funcEntry("FBF::setRefreshRate");
lock_guard<std::mutex> guard(rrMutex);
this->refreshRate = newRR;
//trace.funcExit("FBF::setRefreshRate");
return true;
}
/**
* FUNCTION NAME: getPastEnd
*
* DESCRIPTION: THis function returns the index of the Past end bloom filter
*/
unsigned long FBF::getPastEnd() {
//trace.funcEntry("FBF::getPastEnd");
numberOfBFs = fbf.size();
pastEnd = numberOfBFs - 1;
//trace.funcExit("FBF::getPastEnd", (int)this->pastEnd);
return this->pastEnd;
}
/**
* FUNCTION NAME: checkSmartFBF_FPR
*
* DESCRIPTION: This function checks the False Positives (FPs) and the
* False Positive Rate (FPR) of the FBF using SMART RULES
*/
double FBF::checkSmartFBF_FPR(unsigned long long int number, testType test) {
//trace.funcEntry("FBF::checkSmartFBF_FPR");
unsigned long long int smartFP = 0;
double smartFPR = 0.0;
long long int i = -1;
unsigned long long int counter = 0;
if ( test == falsePositiveNumber ) {
unsigned long long int tries = 0;
while ( counter != number ) {
if ( membershipCheck(i) ) {
smartFP++;
counter++;
}
i--;
tries++;
}
smartFPR = (double) smartFP/tries;
}
else if ( test == numberOfTrials ) {
while ( counter != number ) {
if ( membershipCheck(i) ) {
smartFP++;
}
i--;
counter++;
}
smartFPR = (double) smartFP/counter;
if ( 0 == smartFP ) {
smartFPR = checkSmartFBF_FPR(FALSE_POSITIVES_CHECK, falsePositiveNumber);
}
}
cout<<" RESULT :: Smart FPR = "<<smartFPR<<endl;
//trace.funcExit("FBF::checkSmartFBF_FPR", (int)smartFPR);
return smartFPR;
}
/**
* FUNCTION NAME: checkDumbFBF_FPR
*
* DESCRIPTION: This function checks the False Positives (FPs) and the
* False Positive Rate (FPR) of the FBF using NAIVE RULES
*/
double FBF::checkDumbFBF_FPR(unsigned long long int number, testType test) {
//trace.funcEntry("FBF::checkDumbFBF_FPR");
unsigned long long int dumbFP = 0;
double dumbFPR = 0.0;
long long int i = -1;
unsigned long long int counter = 0;
int j = 0;
unsigned long lastPast = getPastEnd();
if ( test == falsePositiveNumber ) {
unsigned long long int tries = 0;
while ( counter != number ) {
for ( j = future; j <= lastPast; j++ ) {
if ( fbf[j].contains(i) ) {
dumbFP++;
counter++;
break;
}
}
i--;
tries++;
}
dumbFPR = (double) dumbFP/tries;
}
else if ( test == numberOfTrials ) {
while ( counter != number ) {
for ( j = future; j <= lastPast; j++ ) {
if ( fbf[j].contains(i) ) {
dumbFP++;
break;
}
}
i--;
counter++;
}
dumbFPR = (double) dumbFP/counter;
if ( 0 == dumbFP ) {
dumbFPR = checkDumbFBF_FPR(FALSE_POSITIVES_CHECK, falsePositiveNumber);
}
}
cout<<" RESULT :: DUMB FPR = " <<dumbFPR <<endl;
//trace.funcExit("FBF::checkDumbFBF_FPR", (int)dumbFPR);
return dumbFPR;
}
/**
* FUNCTION NAME: checkEffectiveFPR
*
* DESCRIPTION: This function returns the False Positive Rate based
* on the conditional probability rules
*/
double FBF::checkEffectiveFPR() {
//trace.funcEntry("FBF::checkEffectiveFPR");
unsigned int counter = 0;
unsigned int temp = 0;
double effectiveFPR = 0.0;
unsigned long lastPast = getPastEnd();
// Debug
/*
cout<<endl<<" INFO :: Individual FPR here: "<<endl;
cout<<" INFO :: Future BF FPR: "<<fbf[future].effective_fpp()<<endl;
for ( int i = present; i <= lastPast; i++ ) {
cout<<" INFO :: " <<i<<"BF FPR: "<<fbf[i].effective_modified_fpp()<<endl;
}
*/
//effectiveFPR = ( fbf[future].effective_fpp() * fbf[present].effective_modified_fpp());
effectiveFPR = ( 1 - fbf[future].effective_fpp());
for ( counter = present; counter <= (lastPast - 1); counter++ ) {
temp = counter + 1;
//effectiveFPR += (fbf[counter].effective_modified_fpp() * fbf[temp].effective_modified_fpp());
//effectiveFPR *= (1 - (fbf[counter].effective_modified_fpp() * fbf[temp].effective_modified_fpp()));
effectiveFPR *= (1 - (fbf[counter].effective_fpp() * fbf[temp].effective_fpp()));
}
//effectiveFPR += (fbf[lastPast].effective_modified_fpp());
//effectiveFPR *= (1 - (fbf[lastPast].effective_modified_fpp()));
effectiveFPR *= (1 - (fbf[lastPast].effective_fpp()));
effectiveFPR = 1 - effectiveFPR;
cout<<" RESULT :: Effective FPR: " <<effectiveFPR <<endl;
//trace.funcExit("FBF::checkEffectiveFPR", (int)effectiveFPR);
return effectiveFPR;
}
/**
* FUNCTION NAME: fbfAnalysis
*
* DESCRIPTION: This function performs the FBF analysis
*/
double FBF::fbfAnalysis() {
//trace.funcEntry("FBF::fbfAnalysis");
vector<int> n;
for ( int i = 0 ; i < fbf.size(); i++ ) {
n.emplace_back(populateNi(i));
cout<<"Elements in : "<<i<<" filter: "<<n[i]<<endl;
}
double fbfFPR = 0.0;
fbfFPR = ( 1 - fbf[future].effective_n_given_fpp(n[future], fbf[future].getSaltSize()));
fbfFPR *= 1 - fbf[present].effective_n_given_fpp(n[present], (int)(fbf[present].getSaltSize()));
fbfFPR *= (1 - fbf[present].effective_n_given_fpp(n[present], (int)(fbf[present].getSaltSize())))*(1 - fbf[present].effective_n_given_fpp(n[present], (int)(fbf[present].getSaltSize() - Hi(future))) * fbf[pastStart].effective_n_given_fpp(n[pastStart], (int)Hi(future)));
fbfFPR *= 1 - fbf[pastStart].effective_n_given_fpp(n[pastStart], (int)(fbf[pastStart].getSaltSize() - Hi(present)));
double finalFP = 1 - fbfFPR;
cout<<" INFO :: fbf Analysis FPR: " <<finalFP<<endl;
//trace.funcExit("FBF::fbfAnalysis", (int)finalFP);
return finalFP;
}
/**
* FUNCTION NAME: populateNi
*
* DESCRIPTION: This function populates the number of elements inserted into each bloom filter
*/
int FBF::populateNi(int indexOfBloomFilter) {
//trace.funcEntry("FBF::populateNi");
if ( -1 == indexOfBloomFilter ) {
//trace.funcExit("FBF::populateNi", 0);
return 0;
}
else {
//trace.funcExit("FBF::populateNi", SUCCESS);
return fbf[indexOfBloomFilter].getInsertedElementCount() - populateNi(indexOfBloomFilter - 1);
}
}
/**
* FUNCTION NAME: Hi
*
* DESCRIPTION: This function is a helper function
*/
double FBF::Hi(int bloomFilterIndex) {
//trace.funcEntry("FBF::Hi");
if ( -1 == bloomFilterIndex ) {
//trace.funcExit("FBF::Hi");
return 0;
}
else {
//trace.funcExit("FBF::Hi");
//return expectedValue(populateNi(bloomFilterIndex), fbf[present].getSaltSize() - Hi(bloomFilterIndex-1));
// 12/02/14
return expectedValue(populateNi(bloomFilterIndex), fbf[present].getSaltSize() - 1);
}
}
/**
* FUNCTION NAME: expectedValue
*
* DESCRIPTION: Helper function
*/
double FBF::expectedValue(int p, int l) {
//trace.funcEntry("FBF::expectedValue");
double baseProb = std::exp(-1.0 * fbf[present].getSaltSize() * p /fbf[present].size());
double sum_e = 0.0;
for ( int i = 0; i <= l; i++ ) {
sum_e += i * std::pow((1 - baseProb), i) * std::pow((baseProb), (l-i));
}
//trace.funcExit("FBF::expectedValue", (int)sum_e);
return sum_e;
}