Implement DbDelete()

This commit is contained in:
Jordan Bancino 2022-12-15 02:39:58 +00:00
parent abb4a9cf20
commit c8cd2cebb7
4 changed files with 81 additions and 2 deletions

View file

@ -11,7 +11,7 @@ Key:
Milestone: v0.1.1 Milestone: v0.1.1
----------------- -----------------
[ ] DbDelete() [x] DbDelete()
[ ] UtilRandomString() [ ] UtilRandomString()
[ ] Database version file [ ] Database version file
[ ] User registration [ ] User registration

View file

@ -1,4 +1,4 @@
.Dd $Mdocdate: December 14 2022 $ .Dd $Mdocdate: December 15 2022 $
.Dt TELODENDRIA-CHANGELOG 7 .Dt TELODENDRIA-CHANGELOG 7
.Os Telodendria Project .Os Telodendria Project
.Sh NAME .Sh NAME
@ -37,6 +37,12 @@ Changes:
.It .It
Improved HTTP request logging by removing unnecessary Improved HTTP request logging by removing unnecessary
log entries and making errors more specific. log entries and making errors more specific.
.It
Added a way to safely delete objects from the database.
While the database is for persistent storage, there may
be some ephemeral data that lives there only for a short
while, so it will be necessary to remove that data when
necessary.
.El .El
.Pp .Pp
Bug fixes: Bug fixes:

View file

@ -537,6 +537,76 @@ DbCreate(Db * db, size_t nArgs,...)
return DbLockFromArr(db, args); return DbLockFromArr(db, args);
} }
int
DbDelete(Db * db, size_t nArgs,...)
{
va_list ap;
Array *args;
char *file;
char *hash;
int ret = 1;
DbRef *ref;
if (!db)
{
return 0;
}
va_start(ap, nArgs);
args = ArrayFromVarArgs(nArgs, ap);
va_end(ap);
pthread_mutex_lock(&db->lock);
hash = DbHashKey(args);
file = DbFileName(db, args);
ref = HashMapGet(db->cache, hash);
if (ref)
{
pthread_mutex_lock(&ref->lock);
HashMapDelete(db->cache, hash);
JsonFree(ref->json);
ArrayFree(ref->name);
db->cacheSize -= ref->size;
if (ref->next)
{
ref->next->prev = ref->prev;
}
else
{
db->mostRecent = ref->prev;
}
if (ref->prev)
{
ref->prev->next = ref->next;
}
else
{
db->leastRecent = ref->next;
}
pthread_mutex_unlock(&ref->lock);
pthread_mutex_destroy(&ref->lock);
Free(ref);
}
Free(hash);
if (UtilLastModified(file))
{
ret = remove(file) == 0;
}
pthread_mutex_unlock(&db->lock);
return ret;
}
DbRef * DbRef *
DbLock(Db * db, size_t nArgs,...) DbLock(Db * db, size_t nArgs,...)
{ {

View file

@ -44,6 +44,9 @@ extern void
extern DbRef * extern DbRef *
DbCreate(Db *, size_t,...); DbCreate(Db *, size_t,...);
extern int
DbDelete(Db *, size_t,...);
extern DbRef * extern DbRef *
DbLock(Db *, size_t,...); DbLock(Db *, size_t,...);