diff options
| author | Soikk | 2022-08-08 21:33:19 +0200 |
|---|---|---|
| committer | Soikk | 2022-08-08 21:33:19 +0200 |
| commit | 449ade1bcfb1a50793f8f9b5a359e76275de4d99 (patch) | |
| tree | 18ddd521a00300d753d2cef685300ba2fa21480d /src | |
| parent | 5dd0d7700fa98197cdadaa2369d789e092cc0f49 (diff) | |
| download | soikk-DB-449ade1bcfb1a50793f8f9b5a359e76275de4d99.tar.xz soikk-DB-449ade1bcfb1a50793f8f9b5a359e76275de4d99.tar.zst | |
Standarized function names. Added addTagFiles. Updated DOC and fixes bugs
Diffstat (limited to 'src')
| -rw-r--r-- | src/database.c | 138 | ||||
| -rw-r--r-- | src/db.db | bin | 715 -> 0 bytes | |||
| -rw-r--r-- | src/main.c | 26 | ||||
| -rw-r--r-- | src/main.exe | bin | 73988 -> 0 bytes | |||
| -rw-r--r-- | src/storage.c | 131 |
5 files changed, 159 insertions, 136 deletions
diff --git a/src/database.c b/src/database.c index 5b6249b..f930539 100644 --- a/src/database.c +++ b/src/database.c @@ -14,28 +14,17 @@ database *newDatabase(char *name){ return db; } -static void increaseCount(ctable *ct, uint64_t i){ - ct->table[i]++; -} - -static void decreaseCount(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 = nodeSearch(db->hfiles, h); - + uint64_t i = searchNode(db->hfiles, h); if(i == -1){ - ltableAdd(db->lfiles, file); - ctableAdd(db->cfiles, 0); + insertLtable(db->lfiles, file); + insertCtable(db->cfiles, 0); i = db->lfiles->size-1; db->hfiles = insertNode(db->hfiles, h, i); } - increaseCount(db->cfiles, i); - return i; } @@ -43,34 +32,42 @@ 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 = nodeSearch(db->htags, h); - + uint64_t i = searchNode(db->htags, h); if(i == -1){ - ltableAdd(db->ltags, tag); - ctableAdd(db->ctags, 0); + insertLtable(db->ltags, tag); + insertCtable(db->ctags, 0); i = db->ltags->size-1; db->htags = insertNode(db->htags, h, i); } - increaseCount(db->ctags, i); - return i; } +static void increaseCount(ctable *ct, uint64_t i){ + ct->table[i]++; +} + +static void decreaseCount(ctable *ct, uint64_t i){ + ct->table[i]--; +} + static int addRelation(database *db, relation r){ - if(mtableSearch(db->map, r) != -1){ - return -1; + if(searchMtable(db->map, r) == UINTMAX_MAX){ + insertMtable(db->map, r); + return 0; } - mtableAdd(db->map, r); - - return 0; + return -1; } int addFileTag(database *db, char *file, char *tag){ uint64_t fi = addFile(db, file), ti = addTag(db, tag); - addRelation(db, (relation){.file = fi, .tag = ti}); - - return 0; + int r = addRelation(db, (relation){.file = fi, .tag = ti}); + if(r == 0){ + increaseCount(db->cfiles, fi); + increaseCount(db->ctags, ti); + return 0; + } + return -1; } int addFileTags(database *db, char *file, int ntags, ...){ @@ -81,14 +78,56 @@ int addFileTags(database *db, char *file, int ntags, ...){ addFileTag(db, file, tag); } va_end(tags); + return 0; +} +int addTagFiles(database *db, char *tag, int nfiles, ...){ + va_list files; + va_start(files, nfiles); + for(uint64_t i = 0; i < nfiles; ++i){ + char *file = va_arg(files, char*); + addFileTag(db, file, tag); + } + va_end(files); return 0; } +// When removing the file from the ltable and ctable we change the indexes of the tags in front +// of it. Thus, we must change their indexes on the avl tree and mapping table. To do this we +// simply get all tags with an index higher than the tag we removed and substract one from it, +// since when removing a tag from the tables all we did was shift down all the tags in front of +// it one position +static void decreaseHigherIndexNode(node *n, uint64_t i){ + if(n == NULL){ + return; + } + if(n->i > i){ + n->i--; + } + decreaseHigherIndexNode(n->left, i); + decreaseHigherIndexNode(n->right, i); +} + +static void decreaseHigherFileIndexMap(mtable *mt, uint64_t i){ + for(uint64_t j = 0; j < mt->size; ++j){ + if(mt->table[j].file > i){ + mt->table[j].file--; + } + } +} + +static void decreaseHigherTagIndexMap(mtable *mt, uint64_t i){ + for(uint64_t j = 0; j < mt->size; ++j){ + if(mt->table[j].tag > i){ + mt->table[j].tag--; + } + } +} + int removeFile(database *db, char *file){ uint32_t l; file = normalizeStrLimit(file, &l, MAXPATH-1); - uint64_t i = ltableSearch(db->lfiles, file); + uint64_t i = searchLtable(db->lfiles, file); if(i == -1){ return -1; } @@ -98,17 +137,20 @@ int removeFile(database *db, char *file){ decreaseCount(db->ctags, r[j]); } uint64_t h = crc64(0, file, l); - ltableRemove(db->lfiles, file); - ctableRemove(db->cfiles, i); - deleteNode(db->hfiles, h); - mtableRemoveFile(db->map, i); + removeLtable(db->lfiles, file); + removeCtable(db->cfiles, i); + removeNode(db->hfiles, h); + removeFileMtable(db->map, i); + + decreaseHigherIndexNode(db->hfiles, i); + decreaseHigherFileIndexMap(db->map, i); return 0; } int removeTag(database *db, char *tag){ uint32_t l; tag = normalizeStrLimit(tag, &l, MAXPATH-1); - uint64_t i = ltableSearch(db->ltags, tag); + uint64_t i = searchLtable(db->ltags, tag); if(i == -1){ return -1; } @@ -118,20 +160,21 @@ int removeTag(database *db, char *tag){ decreaseCount(db->cfiles, r[j]); } uint64_t h = crc64(0, tag, l); - ltableRemove(db->ltags, tag); - ctableRemove(db->ctags, i); - deleteNode(db->htags, h); - mtableRemoveTag(db->map, i); + removeLtable(db->ltags, tag); + removeCtable(db->ctags, i); + removeNode(db->htags, h); + removeTagMtable(db->map, i); + + decreaseHigherIndexNode(db->htags, i); + decreaseHigherTagIndexMap(db->map, i); return 0; } -// Stores in r a list with the indexes of the first n tags that this file has -// If n is 0 or lower, it returns all of them. Stores in rl the length of r 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 = nodeSearch(db->hfiles, h); + uint64_t fi = searchNode(db->hfiles, h); if(fi == -1){ return -1; } @@ -151,17 +194,14 @@ int searchFile(database *db, char *file, uint64_t n, uint64_t **r, uint64_t *rl) (*r)[c++] = db->map->table[i].tag; } } - return 0; } -// Stores in r a list with the indexes of the first n files that have this tag -// If n is 0 or lower, it returns all of them. Stores in rl the length of r 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 = nodeSearch(db->htags, h); + uint64_t ti = searchNode(db->htags, h); if(ti == -1){ return -1; } @@ -181,7 +221,6 @@ int searchTag(database *db, char *tag, uint64_t n, uint64_t **r, uint64_t *rl){ (*r)[c++] = db->map->table[i].file; } } - return 0; } @@ -202,7 +241,6 @@ int storeDatabase(database *db, const char *path){ char end[3] = "END"; fwrite(end, sizeof(char), 3, fp); - fclose(fp); return 0; } @@ -255,11 +293,11 @@ void debugDatabase(database *db){ 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->cfiles->table[i]); + printf("\t\t+[%" PRIu64 "] %s (%" PRIu64 ")\n", i, 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->ctags->table[i]); + printf("\t\t+[%" PRIu64 "] %s (%" PRIu64 ")\n", i, db->ltags->table[i], db->ctags->table[i]); } printf("\t-hfiles: %d\n", db->lfiles->size); debugAVLtree(db->hfiles); @@ -267,7 +305,7 @@ void debugDatabase(database *db){ 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("\t\t+[%" PRIu64 "] %" PRIu64 ":%" PRIu64 "\n", i, db->map->table[i].file, db->map->table[i].tag); } printf("\n"); } diff --git a/src/db.db b/src/db.db Binary files differdeleted file mode 100644 index 1c12210..0000000 --- a/src/db.db +++ /dev/null @@ -4,21 +4,21 @@ int main(){ + inputBuffer *in = newInputBuffer(); - /*database *db = newDatabase("miDB"); - - - addFileTag(db, "vaca.png", "naturaleza"); + database *db = newDatabase("miDB"); + + + addFileTag(db, "vaca.png", "naturalezas"); addFileTags(db, "terry-davis.jpg", 3, "holyC", "programmer", "very cool"); addFileTag(db, "vaca.png", "lovely"); addFileTags(db, "vaca.png", 3, "nature", "animal", "very cool"); - - loadDatabase(db, "db.db"); + addFileTag(db, "terry-davis.jpg", "based"); + + storeDatabase(db, "db.db"); + + printDatabase(db); - printDatabase(db);*/ - - database *db = loadDatabase("db.db"); - debugDatabase(db); uint64_t *l, i; @@ -30,6 +30,12 @@ int main(){ } + addTagFiles(db, "elemento", 2, "vaca.png", "terry-davis.jpg"); + + printDatabase(db); + + debugDatabase(db); + diff --git a/src/main.exe b/src/main.exe Binary files differdeleted file mode 100644 index 9ac3ba5..0000000 --- a/src/main.exe +++ /dev/null diff --git a/src/storage.c b/src/storage.c index 3e45b2e..73d8131 100644 --- a/src/storage.c +++ b/src/storage.c @@ -5,13 +5,12 @@ ltable *newLtable(uint64_t size){ ltable *lt = malloc(sizeof(ltable)); - size = (((uint64_t)size) < 0) ? 0 : size; lt->size = size; lt->table = malloc(size*sizeof(char*)); return lt; } -int ltableAdd(ltable *lt, char *str){ +int insertLtable(ltable *lt, char *str){ uint32_t ls; str = normalizeStrLimit(str, &ls, MAXPATH-1); @@ -37,19 +36,19 @@ int ltableAdd(ltable *lt, char *str){ return 0; } -int ltableRemove(ltable *lt, char *str){ - uint64_t i = ltableSearch(lt, str); +int removeLtable(ltable *lt, char *str){ + uint64_t i = searchLtable(lt, str); if(i == -1){ return -1; } lt->size--; - for(uint64_t j = i; j < lt->size-1; ++j){ + for(uint64_t j = i; j < lt->size; ++j){ lt->table[j] = lt->table[j+1]; } return 0; } -uint64_t ltableSearch(ltable *lt, char *str){ +uint64_t searchLtable(ltable *lt, char *str){ uint32_t l; str = normalizeStrLimit(str, &l, MAXPATH-1); @@ -58,7 +57,7 @@ uint64_t ltableSearch(ltable *lt, char *str){ return i; } } - return -1; + return UINTMAX_MAX; } int storeLtable(const ltable *lt, FILE *fp){ @@ -102,13 +101,12 @@ ltable *loadLtable(FILE *fp){ ctable *newCtable(uint64_t size){ ctable *ct = malloc(sizeof(ctable)); - size = (((uint64_t)size) < 0) ? 0 : size; ct->size = size; - ct->table = malloc(size*sizeof(uint64_t)); + ct->table = calloc(size, sizeof(uint64_t)); return ct; } -int ctableAdd(ctable *ct, uint64_t n){ +int insertCtable(ctable *ct, uint64_t n){ uint64_t *nct = malloc((ct->size+1)*sizeof(uint64_t)); for(uint64_t i = 0; i < ct->size; ++i){ if(n == ct->table[i]){ @@ -126,24 +124,24 @@ int ctableAdd(ctable *ct, uint64_t n){ return 0; } -int ctableRemove(ctable *ct, uint64_t i){ - if(i >= ct->size){ +int removeCtable(ctable *ct, uint64_t i){ + if(i >= ct->size || i < 0){ return -1; } ct->size--; - for(uint64_t j = i; j < ct->size-1; ++j){ + for(uint64_t j = i; j < ct->size; ++j){ ct->table[j] = ct->table[j+1]; } return 0; } -uint64_t ctableSearch(ctable *ct, uint64_t n){ +uint64_t searchCtable(ctable *ct, uint64_t n){ for(uint64_t i = 0; i < ct->size; ++i){ if(n == ct->table[i]){ return i; } } - return -1; + return UINTMAX_MAX; } int storeCtable(const ctable *ct, FILE *fp){ @@ -182,13 +180,12 @@ ctable *loadCtable(FILE *fp){ mtable *newMtable(uint64_t size){ mtable *mt = malloc(sizeof(mtable)); - size = (((uint64_t)size) < 0) ? 0 : size; mt->size = size; mt->table = malloc(size*sizeof(relation)); return mt; } -int mtableAdd(mtable *mt, relation r){ +int insertMtable(mtable *mt, relation r){ relation *nmt = malloc((mt->size+1)*sizeof(relation)); for(uint64_t i = 0; i < mt->size; ++i){ if(r.file == mt->table[i].file && r.tag == mt->table[i].tag){ @@ -205,19 +202,19 @@ int mtableAdd(mtable *mt, relation r){ return 0; } -int mtableRemove(mtable *mt, relation r){ - uint64_t i = mtableSearch(mt, r); +int removeMtable(mtable *mt, relation r){ + uint64_t i = searchMtable(mt, r); if(i == -1){ return -1; } mt->size--; - for(uint64_t j = i; j < mt->size-1; ++j){ + for(uint64_t j = i; j < mt->size; ++j){ mt->table[j] = mt->table[j+1]; } return 0; } -int mtableRemoveFile(mtable *mt, uint64_t file){ +int removeFileMtable(mtable *mt, uint64_t file){ relation *nmt = malloc(mt->size*sizeof(relation)); uint64_t ni = 0; for(uint64_t i = 0; i < mt->size; ++i){ @@ -234,7 +231,7 @@ int mtableRemoveFile(mtable *mt, uint64_t file){ return 0; } -int mtableRemoveTag(mtable *mt, uint64_t tag){ +int removeTagMtable(mtable *mt, uint64_t tag){ relation *nmt = malloc(mt->size*sizeof(relation)); uint64_t ni = 0; for(uint64_t i = 0; i < mt->size; ++i){ @@ -251,31 +248,13 @@ int mtableRemoveTag(mtable *mt, uint64_t tag){ return 0; } -uint64_t mtableSearch(mtable *mt, relation r){ +uint64_t searchMtable(mtable *mt, relation r){ for(uint64_t i = 0; i < mt->size; ++i){ if(r.file == mt->table[i].file && r.tag == mt->table[i].tag){ return i; } } - return -1; -} - -uint64_t mtableSearchFile(mtable *mt, uint64_t file){ - for(uint64_t i = 0; i < mt->size; ++i){ - if(file == mt->table[i].file){ - return i; - } - } - return -1; -} - -uint64_t mtableSearchTag(mtable *mt, uint64_t tag){ - for(uint64_t i = 0; i < mt->size; ++i){ - if(tag == mt->table[i].tag){ - return i; - } - } - return -1; + return UINTMAX_MAX; } int storeMtable(const mtable *mt, FILE *fp){ @@ -314,32 +293,6 @@ mtable *loadMtable(FILE *fp){ // AVL TREE -static inline uint64_t max(uint64_t a, uint64_t b){ - return ((a > b) ? a : b); -} - -static uint64_t height(node *n){ - if(n != NULL){ - return 1 + max(height(n->left), height(n->right)); - } - return 0; -} - -static int64_t balance(node *n){ - if(n != NULL){ - return height(n->left) - height(n->right); - } - return 0; -} - -static node *lowestNode(node *n){ - node *t = n; - while(t->left != NULL){ - t = t->left; - } - return t; -} - node *newNode(uint64_t h, uint64_t i){ node *n = malloc(sizeof(node)); n->h = h; @@ -367,6 +320,24 @@ static node *rotateNodeLeft(node *r){ return nr; } +static inline uint64_t max(uint64_t a, uint64_t b){ + return ((a > b) ? a : b); +} + +static uint64_t height(node *n){ + if(n != NULL){ + return 1 + max(height(n->left), height(n->right)); + } + return 0; +} + +static int64_t balance(node *n){ + if(n != NULL){ + return height(n->left) - height(n->right); + } + return 0; +} + node *insertNode(node *r, uint64_t h, uint64_t i){ if(r == NULL){ return newNode(h, i); @@ -396,13 +367,21 @@ node *insertNode(node *r, uint64_t h, uint64_t i){ return r; } -node *deleteNode(node *r, uint64_t h){ +static node *lowestNode(node *n){ + node *t = n; + while(t->left != NULL){ + t = t->left; + } + return t; +} + +node *removeNode(node *r, uint64_t h){ if(r == NULL){ return r; }else if(r->h > h){ - r->left = deleteNode(r->left, h); + r->left = removeNode(r->left, h); }else if(r->h < h){ - r->right = deleteNode(r->right, h); + r->right = removeNode(r->right, h); }else{ if(r->left == NULL || r->right == NULL){ node *t = (r->left) ? r->left : r->right; @@ -417,7 +396,7 @@ node *deleteNode(node *r, uint64_t h){ node *t = lowestNode(r->right); r->h = t->h; r->i = t->i; - r->right = deleteNode(r->right, t->h); + r->right = removeNode(r->right, t->h); } } if(r == NULL){ @@ -443,15 +422,15 @@ node *deleteNode(node *r, uint64_t h){ } // Searches for h, returns i -uint64_t nodeSearch(node *n, uint64_t h){ +uint64_t searchNode(node *n, uint64_t h){ if(n == NULL){ - return -1; + return UINTMAX_MAX; }else if(h == n->h){ return n->i; }else if(h < n->h){ - return nodeSearch(n->left, h); + return searchNode(n->left, h); }else if(h > n->h){ - return nodeSearch(n->right, h); + return searchNode(n->right, h); } } |
