-
Notifications
You must be signed in to change notification settings - Fork 2
/
bloom.c
181 lines (137 loc) · 3.19 KB
/
bloom.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
#include <stdarg.h>
#include <stdio.h>
#include <stdint.h>
#include <math.h>
#include "bloom.h"
#include "murmur.h"
#include "bloom_io.h"
#define BLOOMIN_VERSION "0.0.1"
#define SALT_CONSTANT 0x97c29b3a
/*
#define ERROR_RATE .01
#define SIZE 10000000
*/
BLOOM *bloom_init(uint32_t size, double error_rate)
{
BLOOM *bloom;
if ((bloom = (BLOOM *)malloc(sizeof(BLOOM))) == NULL) {
fprintf(stderr, "Error, could not alloc a new bloom filter\n");
return NULL;
}
bloom->filter = NULL;
bloom->size = size;
bloom->num_funcs = (uint32_t) ceil(log(1 / error_rate) / log(2));
bloom->hashes = calloc(bloom->num_funcs, sizeof(uint32_t));
return bloom;
}
BLOOM *bloom_load(uint32_t size, double error_rate)
{
BLOOM *bloom = bloom_init(size, error_rate);
if (bloom_read(bloom) == 0)
{
fprintf(stderr, "Error, could not alloc a new bloom filter\n");
return NULL;
}
return bloom;
}
BLOOM *bloom_create(uint32_t size, double error_rate)
{
BLOOM *bloom;
if ((bloom = (BLOOM *)malloc(sizeof(BLOOM))) == NULL) {
fprintf(stderr, "Error, could not alloc a new bloom filter\n");
return NULL;
}
if ((bloom->filter = (uint32_t *)calloc(size, sizeof(uint32_t))) == NULL){
return NULL;
}
bloom->num_funcs = (uint32_t) ceil(log(1 / error_rate) / log(2));
bloom->hashes = calloc(bloom->num_funcs, sizeof(uint32_t));
bloom->size = size;
return bloom;
}
BLOOM *bloom_create_from_file(uint32_t size, double error_rate)
{
BLOOM *bloom = bloom_load(size, error_rate);
return bloom;
}
static void hash_func(BLOOM *bloom, const char *key, uint32_t key_len, uint32_t *hashes)
{
int i;
uint32_t checksum[4];
MurmurHash3_x64_128(key, key_len, SALT_CONSTANT, checksum);
uint32_t h1 = checksum[0];
uint32_t h2 = checksum[1];
for (i = 0; i < bloom->num_funcs; i++) {
hashes[i] = (h1 + i * h2) % bloom->size;
}
}
int bloom_insert(BLOOM *bloom, const char *s, uint32_t len)
{
uint32_t i;
uint32_t idx;
uint32_t *hashes = bloom->hashes;
hash_func(bloom, s, len, hashes);
for (i = 0; i < bloom->num_funcs; ++i)
{
idx = hashes[i];
bloom->filter[idx]++;
}
return 1;
}
int bloom_check(BLOOM *bloom, const char *s, uint32_t len)
{
uint32_t i;
uint32_t idx;
uint32_t *hashes = bloom->hashes;
hash_func(bloom, s, len, hashes);
for (i = 0; i < bloom->num_funcs; ++i)
{
idx = hashes[i];
if (bloom->filter[idx] <= 0)
{
return 0;
}
}
return 1;
}
int bloom_delete(BLOOM *bloom, const char *s, uint32_t len)
{
uint32_t i;
uint32_t idx;
uint32_t *hashes = bloom->hashes;
hash_func(bloom, s, len, hashes);
if (!bloom_check(bloom,s,len))
{
return 0;
}
for (i = 0; i < bloom->num_funcs; ++i)
{
idx = hashes[i];
bloom->filter[idx]--;
if (bloom->filter[idx] <= 0)
{
bloom->filter[idx] = 0;
}
}
return 1;
}
int bloom_destroy(BLOOM *bloom)
{
if (bloom != NULL)
free(bloom->filter);
free(bloom->hashes);
free((void *)bloom);
return 0;
}
/*
int main()
{
BLOOM *bloom = bloom_create_from_file(SIZE,ERROR_RATE);
bloom_check(bloom, "newval", strlen("newval"));
bloom_insert(bloom, "newval", strlen("newval"));
bloom_write(bloom);
BLOOM *bloom2 = bloom_load(SIZE,ERROR_RATE);
bloom_check(bloom2, "newval", strlen("newval"));
return 0;
}
*/