aboutsummaryrefslogtreecommitdiff
path: root/src/database.c
diff options
context:
space:
mode:
authorSoikk2022-08-06 21:46:45 +0200
committerSoikk2022-08-06 21:46:45 +0200
commitf9a7d9cd8a7e603c89d4cc5d98d7f694cc217d85 (patch)
tree647b02d6ebabbafed7604da36685b5d17e0c825c /src/database.c
parent0e95d2c61da7b7fbce76b0633313128b7440136e (diff)
downloadsoikk-DB-f9a7d9cd8a7e603c89d4cc5d98d7f694cc217d85.tar.xz
soikk-DB-f9a7d9cd8a7e603c89d4cc5d98d7f694cc217d85.tar.zst
Refactored database. Replaced htable with ctable (same thing different name). Introduced AVL tree for hashes.
Diffstat (limited to 'src/database.c')
-rw-r--r--src/database.c87
1 files changed, 43 insertions, 44 deletions
diff --git a/src/database.c b/src/database.c
index 5595240..a6a9dfa 100644
--- a/src/database.c
+++ b/src/database.c
@@ -6,10 +6,10 @@ database *newDatabase(char *name){
memcpy(db->name, name, len(name)+1);
db->lfiles = newLtable(0);
db->ltags = newLtable(0);
- db->hfiles = newHtable(0);
- db->htags = newHtable(0);
- db->fcount = newHtable(0);
- db->tcount = newHtable(0);
+ db->cfiles = newCtable(0);
+ db->ctags = newCtable(0);
+ db->hfiles = NULL;
+ db->htags = NULL;
db->map = newMtable(0);
return db;
}
@@ -27,10 +27,10 @@ database *loadDatabase(const char* path){
database *db = newDatabase(name);
db->lfiles = loadLtable(fp);
db->ltags = loadLtable(fp);
- db->hfiles = loadHtable(fp);
- db->htags = loadHtable(fp);
- db->fcount = loadHtable(fp);
- db->tcount = loadHtable(fp);
+ db->cfiles = loadCtable(fp);
+ db->ctags = loadCtable(fp);
+ db->hfiles = loadAVLTree(fp);
+ db->htags = loadAVLTree(fp);
db->map = loadMtable(fp);
char *end = calloc(3, sizeof(char));
@@ -51,10 +51,10 @@ int storeDatabase(database *db, const char *path){
storeLtable(db->lfiles, fp);
storeLtable(db->ltags, fp);
- storeHtable(db->hfiles, fp);
- storeHtable(db->htags, fp);
- storeHtable(db->fcount, fp);
- storeHtable(db->tcount, fp);
+ storeCtable(db->cfiles, fp);
+ storeCtable(db->ctags, fp);
+ storeAVLTree(db->hfiles, fp);
+ storeAVLTree(db->htags, fp);
storeMtable(db->map, fp);
char end[3] = "END";
@@ -64,24 +64,24 @@ int storeDatabase(database *db, const char *path){
return 0;
}
-static void increaseCount(htable *ht, uint64_t i){
- ht->table[i]++;
+static void increaseCount(ctable *ct, uint64_t i){
+ ct->table[i]++;
}
uint64_t addFile(database *db, char *file){
uint32_t l;
file = normalizeStrLimit(file, &l, MAXPATH-1);
uint64_t h = crc64(0, file, l);
- uint64_t i = htableSearch(db->hfiles, h);
+ uint64_t i = nodeSearch(db->hfiles, h);
if(i == -1){
ltableAdd(db->lfiles, file);
- htableAdd(db->hfiles, h);
- htableAdd(db->fcount, 0);
- i = db->hfiles->size-1;
+ ctableAdd(db->cfiles, 0);
+ i = db->lfiles->size-1;
+ db->hfiles = insertNode(db->hfiles, h, i);
}
- increaseCount(db->fcount, i);
-
+ increaseCount(db->cfiles, i);
+
return i;
}
@@ -89,16 +89,17 @@ uint64_t addTag(database *db, char *tag){
uint32_t l;
tag = normalizeStrLimit(tag, &l, MAXPATH-1);
uint64_t h = crc64(0, tag, l);
- uint64_t i = htableSearch(db->htags, h);
+ uint64_t i = nodeSearch(db->htags, h);
if(i == -1){
ltableAdd(db->ltags, tag);
- htableAdd(db->htags, h);
- htableAdd(db->tcount, 0);
- i = db->htags->size-1;
+ ctableAdd(db->ctags, 0);
+ i = db->ltags->size-1;
+ db->htags = insertNode(db->htags, h, i);
+
}
- increaseCount(db->tcount, i);
-
+ increaseCount(db->ctags, i);
+
return i;
}
@@ -136,7 +137,7 @@ int searchFile(database *db, char *file, uint64_t n, uint64_t **r, uint64_t *rl)
uint32_t l;
file = normalizeStrLimit(file, &l, MAXPATH-1);
uint64_t h = crc64(0, file, l);
- uint64_t fi = htableSearch(db->hfiles, h);
+ uint64_t fi = nodeSearch(db->hfiles, h);
if(fi == -1){
return -1;
}
@@ -166,7 +167,7 @@ int searchTag(database *db, char *tag, uint64_t n, uint64_t **r, uint64_t *rl){
uint32_t l;
tag = normalizeStrLimit(tag, &l, MAXPATH-1);
uint64_t h = crc64(0, tag, l);
- uint64_t ti = htableSearch(db->htags, h);
+ uint64_t ti = nodeSearch(db->htags, h);
if(ti == -1){
return -1;
}
@@ -197,34 +198,32 @@ void printDatabase(database *db){
printf("\n");
}
+void debugAVLtree(node *n){
+ if(n != NULL){
+ printf("\t\t+%" PRIu64 " -> %" PRIu64 "\n", n->h, n->i);
+ debugAVLtree(n->left);
+ debugAVLtree(n->right);
+ }
+}
+
void debugDatabase(database *db){
printf("\n");
printf("Name: %s\n", db->name);
printf("\t-lfiles: %d\n", db->lfiles->size);
for(uint64_t i = 0; i < db->lfiles->size; ++i){
- printf("\t\t+%s (%" PRIu64 ")\n", db->lfiles->table[i], db->fcount->table[i]);
+ printf("\t\t+%s (%" PRIu64 ")\n", db->lfiles->table[i], db->cfiles->table[i]);
}
printf("\t-ltags: %d\n", db->ltags->size);
for(uint64_t i = 0; i < db->ltags->size; ++i){
- printf("\t\t+%s (%" PRIu64 ")\n", db->ltags->table[i], db->tcount->table[i]);
- }
- printf("\t-hfiles: %d\n", db->hfiles->size);
- for(uint64_t i = 0; i < db->hfiles->size; ++i){
- printf("\t\t+%" PRIu64 "\n", db->hfiles->table[i]);
- }
- printf("\t-htags: %d\n", db->htags->size);
- for(uint64_t i = 0; i < db->htags->size; ++i){
- printf("\t\t+%" PRIu64 "\n", db->htags->table[i]);
+ printf("\t\t+%s (%" PRIu64 ")\n", db->ltags->table[i], db->ctags->table[i]);
}
+ printf("\t-hfiles: %d\n", height(db->hfiles));
+ debugAVLtree(db->hfiles);
+ printf("\t-htags: %d\n", height(db->htags));
+ debugAVLtree(db->htags);
printf("\t-map: %d\n", db->map->size);
for(uint64_t i = 0; i < db->map->size; ++i){
printf("\t\t+%" PRIu64 ":%" PRIu64 "\n", db->map->table[i].file, db->map->table[i].tag);
}
printf("\n");
}
-
-void reOrderDatabase(database *db){
-
-
-
-}