-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBloomFilter.h
More file actions
186 lines (132 loc) · 4.68 KB
/
Copy pathBloomFilter.h
File metadata and controls
186 lines (132 loc) · 4.68 KB
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
#ifndef BLOOMFILTER_H
#define BLOOMFILTER_H
#include <cmath>
// #include <string.h>
// #include <openssl/md5.h>Go to your project folder and make sure there's a file at:
#include <fcntl.h>
#include <sys/stat.h>
// #include <cf/vec.h>
#include <cf/crypto.h>
// #include "../frontier/ReaderWriterLock.h"
#include <cf/threading/cfmutex.h>
#include <cf/threading/cfguard.h>
#include <utility>
#include <cassert>
#include <vector>
class Bloomfilter
{
public:
Bloomfilter()
{
this->num_hashes = 0;
}
Bloomfilter(bool construct) {
if (!construct) {
this->num_hashes = 0;
return;
}
// Determine the size of bits of our data vector, and resize.
// Use the formula: m = - (n * log(p)) / (log(2)^2)
const unsigned int optimized_size = (int)(- (NUM_OBJECTS * log(FALSE_POSITIVE_RATE)) / (log(2) * log(2)));
// Determine number of hash functions to use.
const unsigned int n = (int)( (optimized_size / NUM_OBJECTS) * log(2) );
this->num_hashes = n;
bits.resize( optimized_size, false );
}
~Bloomfilter() = default;
int buildBloomFilter( const char * path ) {
int fd = open(path, O_RDONLY );
if (fd == -1) {
std::cerr << "Error opening bloom filter";
return 1;
}
struct stat sb;
if (fstat(fd, &sb) == -1) {
perror("Error getting file size");
close(fd);
return 1;
}
int fsize = sb.st_size;
int pos = 0;
while (pos < bits.size() / 8 && pos < fsize) {
bool val = false;
read(fd, &val, 1);
bits.push_back(val);
pos += 1;
}
close(fd);
return 0;
}
int writeBloomFilter() {
// WithWriteLock wl(bloom_lock);
cf::Guard g(bloom_lock);
int fd = open("./log/frontier/bloomfilter.bin", O_TRUNC | O_RDWR );
if (fd == -1) {
std::cerr << "Error opening bloom filter";
return 1;
}
struct stat sb;
if (fstat(fd, &sb) == -1) {
perror("Error getting file size");
close(fd);
return 1;
}
int fsize = sb.st_size;
int pos = 0;
while (pos < bits.size() - 8) {
uint8_t byte_value = 0;
for (int i = 0; i < 8; ++i) {
if (bits[pos + i]) {
byte_value |= (1 << i); // Set the i-th bit if bool_array[i] is true
}
}
write(fd, &byte_value, 1);
pos += 8;
}
close(fd);
return 0;
}
inline void insert( const string& s)
{
// Hash the string into two unique hashes.
// const auto s_new = std::string(s.c_str());
// Use double hashing to get unique bit, and repeat for each hash function.
// WithWriteLock wl(bloom_lock);
cf::Guard g(bloom_lock);
if (s.size() == 0)
return;
const auto hashes = crypto.doubleHash(s);
for ( unsigned int i = 0; i < num_hashes; ++i )
{
const unsigned int index = ( (hashes.first + i * hashes.second) % bits.size() );
bits[index] = true;
}
}
inline bool contains( const string& s )
{
// Hash the string into two unqiue hashes.
// Use double hashing to get unique bit, and repeat for each hash function.
// If bit is false, we know for certain this unique string has not been inserted.
// If all bits were true, the string is likely inserted, but false positive is possible.
cf::Guard g(bloom_lock);
if (s.size() == 0)
return false;
const auto hashes = crypto.doubleHash(s);
for ( unsigned int i = 0; i < num_hashes; ++i )
{
const unsigned int index = ( (hashes.first + i * hashes.second) % bits.size() );
if ( !bits[index] ) return false;
}
return true;
}
private:
static constexpr size_t NUM_OBJECTS = 10000000; // 10 million
static constexpr double FALSE_POSITIVE_RATE = 0.005; // .5%
unsigned int num_hashes;
// unsigned int optimized_size;
std::vector<bool> bits;
Crypto crypto;
// ReaderWriterLock bloom_lock;
cf::Mutex bloom_lock;
};
#endif