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
|
#ifndef BLOOM_H
#define BLOOM_H
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#define BLOOM_MAX_NAME_LENGTH 255
#define BLOOM_MAX_HASHCOUNT 1024
typedef enum {
BF_SUCCESS = 0,
BF_OUTOFMEMORY,
BF_FOPEN,
BF_FREAD,
BF_FWRITE,
BF_FSTAT,
BF_INVALIDFILE,
// ERRORCOUNT is used as a counter. do not add anything below this line.
BF_ERRORCOUNT
} bloom_error_t;
typedef struct {
size_t size; /**< Size of the Bloom filter in bits */
size_t hashcount; /**< Number of hashes performed per element */
size_t bitmap_size; /**< Size of the bitmap in bytes */
size_t expected; /**< Expected capacity of the filter */
float accuracy; /**< Desired margin of error */
char name[BLOOM_MAX_NAME_LENGTH + 1];
uint8_t *bitmap; /**< Pointer to the bitmap of the filter */
} bloomfilter;
#define BLOOM_FILE_VERSION 1
typedef struct {
uint8_t magic[8];
uint32_t version;
uint8_t name[BLOOM_MAX_NAME_LENGTH + 1];
uint64_t size;
uint64_t hashcount;
uint64_t bitmap_size;
uint64_t expected;
float accuracy;
} bloomfilter_file;
typedef struct {
const bloomfilter_file *header;
const uint8_t *bitmap;
void *mapping;
size_t mapping_size;
} bloomfilter_mapped;
/* function declarations
*/
bloom_error_t bloom_init(bloomfilter *, const size_t, const double);
void bloom_destroy(bloomfilter *);
bool bloom_set_name(bloomfilter *, const char *);
const char *bloom_strerror(const bloom_error_t);
bloom_error_t bloom_save(const bloomfilter *, const char *);
size_t bloom_saturation_count(const bloomfilter *);
float bloom_saturation(const bloomfilter *);
void bloom_add(bloomfilter *, const void *, const size_t);
bloom_error_t bloom_map_fd(bloomfilter_mapped *, int);
void bloom_unmap(bloomfilter_mapped *);
bool bloom_valid_name(const char *, size_t);
bool bloom_mapped_lookup(const bloomfilter_mapped *, const void *, const size_t);
#endif /* BLOOM_H */
|