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:
parent
f7587911d2
commit
7575ab853f
48
src/bloom.c
48
src/bloom.c
@ -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;
|
||||
}
|
19
src/bloom.h
19
src/bloom.h
@ -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
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user