Add bloom filter
[fio.git] / lib / bloom.c
CommitLineData
652ae149
JA
1#include <stdlib.h>
2#include <inttypes.h>
3
4#include "bloom.h"
5#include "../hash.h"
6
7struct bloom {
8 uint64_t nentries;
9
10 uint32_t *map;
11};
12
13#define BITS_PER_INDEX (sizeof(uint32_t) * 8)
14#define BITS_INDEX_MASK (BITS_PER_INDEX - 1)
15
16static unsigned int jhash_init[] = { 0, 0x12db635, 0x2a4a53 };
17#define N_HASHES 3
18
19struct bloom *bloom_new(uint64_t entries)
20{
21 struct bloom *b;
22 size_t no_uints;
23
24 b = malloc(sizeof(*b));
25 b->nentries = entries;
26 no_uints = (entries + BITS_PER_INDEX - 1) / BITS_PER_INDEX;
27 b->map = calloc(no_uints, sizeof(uint32_t));
28 if (!b->map) {
29 free(b);
30 return NULL;
31 }
32
33 return b;
34}
35
36void bloom_free(struct bloom *b)
37{
38 free(b->map);
39 free(b);
40}
41
42static int __bloom_check(struct bloom *b, uint32_t *data, unsigned int nwords,
43 int set)
44{
45 uint32_t hashes[N_HASHES];
46 int i, was_set;
47
48 for (i = 0; i < N_HASHES; i++)
49 hashes[i] = jhash(data, nwords, jhash_init[i]) % b->nentries;
50
51 was_set = 0;
52 for (i = 0; i < N_HASHES; i++) {
53 const unsigned int index = hashes[i] / BITS_PER_INDEX;
54 const unsigned int bit = hashes[i] & BITS_INDEX_MASK;
55
56 if (b->map[index] & (1U << bit))
57 was_set++;
58 if (set)
59 b->map[index] |= 1U << bit;
60 }
61
62 return was_set == N_HASHES;
63}
64
65int bloom_check(struct bloom *b, uint32_t *data, unsigned int nwords)
66{
67 return __bloom_check(b, data, nwords, 0);
68}
69
70int bloom_set(struct bloom *b, uint32_t *data, unsigned int nwords)
71{
72 return __bloom_check(b, data, nwords, 1);
73}