summaryrefslogtreecommitdiff
path: root/bloom.h
diff options
context:
space:
mode:
Diffstat (limited to 'bloom.h')
-rw-r--r--bloom.h76
1 files changed, 76 insertions, 0 deletions
diff --git a/bloom.h b/bloom.h
new file mode 100644
index 0000000..3b95f9b
--- /dev/null
+++ b/bloom.h
@@ -0,0 +1,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 *name);
+
+bool bloom_mapped_lookup(const bloomfilter_mapped *, const void *, const size_t);
+#endif /* BLOOM_H */