1
0
mirror of https://github.com/CovidBraceletPrj/CovidBracelet.git synced 2024-12-05 00:55: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 "bloom.h"
#include "ens/storage.h" #include "ens/storage.h"
int bloom_init() { bloom_filter_t* bloom_init(size_t size) {
// TODO lome: implement bloom_filter_t* bloom = k_calloc(1, sizeof(bloom_filter_t));
return 0; bloom->size = size;
bloom->data = k_malloc(size * sizeof(uint8_t));
return bloom;
} }
int bloom_add_record(record_t* record) { void bloom_delete(bloom_filter_t* bloom) {
// TODO lome: implement if (bloom) {
return 0; 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) { void bloom_add_record(bloom_filter_t* bloom, record_t* record) {
// TODO lome: implement bloom_hash_t* h = bloom->func;
return 0; 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" #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. * 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) // 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) // 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 #endif

View File

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

View File

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