1
0
mirror of https://github.com/CovidBraceletPrj/CovidBracelet.git synced 2024-12-04 16:45:43 +01:00

Add first version of a bloom filter

This commit is contained in:
H1ghBre4k3r 2021-07-30 00:36:36 +02:00 committed by Patrick Rathje
parent f7587911d2
commit 7575ab853f
4 changed files with 57 additions and 14 deletions

View File

@ -1,17 +1,47 @@
#include "bloom.h"
#include "ens/storage.h"
int bloom_init() {
// TODO lome: implement
return 0;
bloom_filter_t* bloom_init(size_t size) {
bloom_filter_t* bloom = k_calloc(1, sizeof(bloom_filter_t));
bloom->size = size;
bloom->data = k_malloc(size * sizeof(uint8_t));
return bloom;
}
int bloom_add_record(record_t* record) {
// TODO lome: implement
return 0;
void bloom_delete(bloom_filter_t* bloom) {
if (bloom) {
while (bloom->func) {
bloom_hash_t* h;
h = bloom->func;
bloom->func = h->next;
k_free(h);
}
k_free(bloom->data);
k_free(bloom);
}
}
int bloom_probably_has_record(record_t* record) {
// TODO lome: implement
return 0;
void bloom_add_record(bloom_filter_t* bloom, record_t* record) {
bloom_hash_t* h = bloom->func;
uint8_t* data = bloom->data;
while (h) {
uint32_t hash = h->func(record);
hash %= bloom->size * 8;
data[hash / 8] |= 1 << (hash % 8);
h = h->next;
}
}
bool bloom_probably_has_record(bloom_filter_t* bloom, record_t* record) {
bloom_hash_t* h = bloom->func;
uint8_t* data = bloom->data;
while (h) {
uint32_t hash = h->func(record);
hash %= bloom->size * 8;
if ((data[hash / 8] & (1 << (hash % 8))) == 0) {
return false;
}
h = h->next;
}
return true;
}

View File

@ -3,15 +3,28 @@
#include "ens/storage.h"
typedef uint32_t (*hash_function)(const void* data);
typedef struct bloom_hash {
hash_function func;
struct bloom_hash* next;
} bloom_hash_t;
typedef struct bloom_filter {
bloom_hash_t* func;
uint8_t* data;
size_t size;
} bloom_filter_t;
/**
* Initialize the bloom filter on basis of the already registerred records.
*/
int bloom_init();
bloom_filter_t* bloom_init(size_t size);
// TODO lome: maybe only use RPI (should be sufficient)
int bloom_add_record(record_t* record);
void bloom_add_record(bloom_filter_t* bloom, record_t* record);
// TODO lome: maybe only use RPI (should be sufficient)
int bloom_probably_has_record(record_t* record);
bool bloom_probably_has_record(bloom_filter_t* bloom, record_t* record);
#endif

View File

@ -50,7 +50,7 @@ int register_record(record_t* record) {
if (rc) {
return rc;
}
rc = bloom_add_record(record);
// rc = bloom_add_record(record);
return rc;
}

View File

@ -50,7 +50,7 @@ void main(void) {
setup_test_data();
#endif
err = bloom_init();
err = bloom_init(1);
if (err) {
printk("init bloom failed (err %d)\n", err);
return;