diff --git a/src/database.c b/src/database.c index 6d2e335..7d4f2f5 100644 --- a/src/database.c +++ b/src/database.c @@ -16,6 +16,7 @@ #include "main.h" #include "database.h" +#include "bitmap.h" #define DB_SEEK(db, offset) \ errno = 0; \ @@ -122,7 +123,7 @@ int db_wr_rec(db_t *db, rec_t *rec) } int db_rd_blk(db_t *db, block_t *blk) -{ +{ ssize_t bytes = 0; assert(db != NULL); @@ -180,3 +181,34 @@ int db_wr_list(db_t *db, rec_t *list, size_t list_len) return processed; } + +int db_search(db_t *db, float tresh, match_t **matches) +{ + uint64_t found = 0; + block_t blk; + const int blk_size = 4096; + unsigned int i = 0; + unsigned char *p; + float diff = 0; + + *matches = NULL; + match = *matches; + memset(blk, 0x0, sizeof(block_t)); + blk.start = 1; + blk.records = blk_size; + + /* TODO: expand matches */ + + while (db_rd_blk(db, &blk) > 0) { + p = blk.data; + for (i = 0; i < blk.records; i++, p += REC_LEN) { + if (*p == '\0') continue; + diff = bitmap_compare(p + 2, sample.data + 2) / BITMAP_BITS; + if (diff > tresh) continue; + match->diff_map = diff; + } + FREE(blk.data); + } + + return found; +} diff --git a/src/database.h b/src/database.h index 8137306..433ce77 100644 --- a/src/database.h +++ b/src/database.h @@ -21,6 +21,11 @@ typedef struct { unsigned char data[REC_LEN]; } rec_t; +typedef struct { + uint64_t num; + float diff; +} match_t; + extern int db_open(db_t *db, const char *path); extern int db_close(db_t *db);