forked from markfasheh/duperemove
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbloom.c
More file actions
145 lines (111 loc) · 2.88 KB
/
Copy pathbloom.c
File metadata and controls
145 lines (111 loc) · 2.88 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
/*
* Copyright (c) 2012, Jyri J. Virkki
* All rights reserved.
*
* This file is under BSD license. See LICENSE file.
*/
/*
* Refer to bloom.h for documentation on the public interfaces.
*/
#include <fcntl.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <inttypes.h>
#include <unistd.h>
#include "bloom.h"
#include "csum.h"
#include "debug.h"
static int bloom_check_add(struct bloom *bloom,
void *buffer, int len, int add)
{
if (bloom->ready == 0) {
fprintf(stderr, "bloom is not initialized!\n");
return -1;
}
int hits = 0;
unsigned int digest[DIGEST_LEN_MAX / sizeof(unsigned int)];
BUILD_BUG_ON(DIGEST_LEN_MAX % sizeof(unsigned int));
checksum_block(buffer, len, (unsigned char *)digest);
register unsigned int a = digest[0];
register unsigned int b = digest[1];
register unsigned int x;
register unsigned int i;
register unsigned int byte;
register unsigned int mask;
register unsigned char c;
/*
* We are using another way to reduce hashing
* See http://www.eecs.harvard.edu/~kirsch/pubs/bbbf/esa06.pdf
*/
for (i = 0; i < bloom->hashes; i++) {
x = (a + i*b) % bloom->bits;
byte = x >> 3;
c = bloom->bf[byte];
mask = 1 << (x % 8);
if (c & mask) {
hits++;
} else {
if (add)
bloom->bf[byte] = c | mask;
}
}
/* element already in (or collision) */
if (hits == bloom->hashes)
return 1;
return 0;
}
/*
* See
* http://en.wikipedia.org/wiki/Bloom_filter#Optimal_number_of_hash_functions
*/
int bloom_init(struct bloom *bloom, int entries, double error)
{
bloom->ready = 0;
if (entries < 1 || error == 0)
return 1;
bloom->entries = entries;
bloom->error = error;
double num = log(bloom->error);
double denom = 0.480453013918201; /* ln(2)^2 */
bloom->bpe = -(num / denom);
double dentries = (double)entries;
bloom->bits = (uint64_t)(dentries * bloom->bpe);
if (bloom->bits % 8)
bloom->bytes = (bloom->bits / 8) + 1;
else
bloom->bytes = bloom->bits / 8;
bloom->hashes = (int)ceil(0.693147180559945 * bloom->bpe); /* ln(2) */
bloom->bf = calloc(bloom->bytes, sizeof(unsigned char));
if (bloom->bf == NULL)
return 1;
bloom->ready = 1;
return 0;
}
int bloom_check(struct bloom *bloom, void *buffer, int len)
{
return bloom_check_add(bloom, buffer, len, 0);
}
int bloom_add(struct bloom *bloom, void *buffer, int len)
{
return bloom_check_add(bloom, buffer, len, 1);
}
void bloom_print(struct bloom *bloom)
{
printf("bloom at %p\n", (void *)bloom);
printf(" ->entries = %d\n", bloom->entries);
printf(" ->error = %f\n", bloom->error);
printf(" ->bits = %" PRIu64 "\n", bloom->bits);
printf(" ->bits per elem = %f\n", bloom->bpe);
printf(" ->bytes = %" PRIu64 "\n", bloom->bytes);
printf(" ->hash functions = %d\n", bloom->hashes);
}
void bloom_free(struct bloom *bloom)
{
if (bloom->ready)
free(bloom->bf);
bloom->ready = 0;
}