[MOD/WIP] Separate all DB operations

Somewhat untested, but I fail to see how it could fail, right?

Next up: Getting the basics of LMDB up and running.
This commit is contained in:
LDA 2024-08-08 09:53:51 +02:00
parent b87979e9a2
commit 59dbfae1ae
3 changed files with 127 additions and 121 deletions

View file

@ -292,15 +292,9 @@ DbCreate(Db * db, size_t nArgs,...)
bool bool
DbDelete(Db * db, size_t nArgs,...) DbDelete(Db * db, size_t nArgs,...)
{ {
(void) nArgs; va_list ap;
(void) db;
/* TODO */
/*va_list ap;
Array *args; Array *args;
char *file;
char *hash;
bool ret = true; bool ret = true;
DbRef *ref;
if (!db) if (!db)
{ {
@ -311,59 +305,10 @@ DbDelete(Db * db, size_t nArgs,...)
args = ArrayFromVarArgs(nArgs, ap); args = ArrayFromVarArgs(nArgs, ap);
va_end(ap); va_end(ap);
pthread_mutex_lock(&db->lock); ret = db->delete(db, args);
hash = DbHashKey(args);
file = DbFileName(db, args);
ref = HashMapGet(db->cache, hash);
if (ref)
{
HashMapDelete(db->cache, hash);
JsonFree(ref->json);
StringArrayFree(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;
}
if (!db->leastRecent)
{
db->leastRecent = db->mostRecent;
}
Free(ref);
}
Free(hash);
if (UtilLastModified(file))
{
ret = (remove(file) == 0);
}
pthread_mutex_unlock(&db->lock);
ArrayFree(args); ArrayFree(args);
Free(file); return ret;
return ret;*/
return false;
} }
DbRef * DbRef *
@ -403,13 +348,13 @@ DbUnlock(Db * db, DbRef * ref)
bool bool
DbExists(Db * db, size_t nArgs,...) DbExists(Db * db, size_t nArgs,...)
{ {
(void) nArgs; va_list ap;
(void) db;
return false;
/*va_list ap;
Array *args; Array *args;
char *file;
bool ret; bool ret;
if (!db || !nArgs || !db->exists)
{
return false;
}
va_start(ap, nArgs); va_start(ap, nArgs);
args = ArrayFromVarArgs(nArgs, ap); args = ArrayFromVarArgs(nArgs, ap);
@ -420,81 +365,32 @@ DbExists(Db * db, size_t nArgs,...)
return false; return false;
} }
pthread_mutex_lock(&db->lock); ret = db->exists(db, args);
file = DbFileName(db, args);
ret = (UtilLastModified(file) != 0);
pthread_mutex_unlock(&db->lock);
Free(file);
ArrayFree(args); ArrayFree(args);
return ret;*/ return ret;
} }
Array * Array *
DbList(Db * db, size_t nArgs,...) DbList(Db * db, size_t nArgs,...)
{ {
(void) db; Array *result;
(void) nArgs;
/* TODO */
/*Array *result;
Array *path; Array *path;
DIR *files;
struct dirent *file;
char *dir;
va_list ap; va_list ap;
if (!db || !nArgs) if (!db || !nArgs || !db->list)
{
return NULL;
}
result = ArrayCreate();
if (!result)
{ {
return NULL; return NULL;
} }
va_start(ap, nArgs); va_start(ap, nArgs);
path = ArrayFromVarArgs(nArgs, ap); path = ArrayFromVarArgs(nArgs, ap);
dir = DbDirName(db, path, 0); va_end(ap);
pthread_mutex_lock(&db->lock);
files = opendir(dir);
if (!files)
{
ArrayFree(path);
ArrayFree(result);
Free(dir);
pthread_mutex_unlock(&db->lock);
return NULL;
}
while ((file = readdir(files)))
{
size_t namlen = strlen(file->d_name);
if (namlen > 5)
{
int nameOffset = namlen - 5;
if (StrEquals(file->d_name + nameOffset, ".json"))
{
file->d_name[nameOffset] = '\0';
ArrayAdd(result, StrDuplicate(file->d_name));
}
}
}
closedir(files);
result = db->list(db, path);
ArrayFree(path); ArrayFree(path);
Free(dir); return result;
pthread_mutex_unlock(&db->lock);
return result;*/
return NULL;
} }
void void
@ -574,3 +470,12 @@ DbRefInit(Db *db, DbRef *ref)
} }
db->mostRecent = ref; db->mostRecent = ref;
} }
void
StringArrayAppend(Array *arr, char *str)
{
if (!arr || !str)
{
return;
}
ArrayAdd(arr, StrDuplicate(str));
}

View file

@ -252,6 +252,100 @@ FlatCreate(Db *d, Array *dir)
return ret; return ret;
} }
static bool
FlatDelete(Db *d, Array *dir)
{
bool ret = true;
char *file;
FlatDb *db = (FlatDb *) d;
if (!d || !dir)
{
return false;
}
pthread_mutex_lock(&d->lock);
file = DbFileName(db, dir);
/* TODO: Unlink the entry from the linkedlist */
if (UtilLastModified(file))
{
ret = remove(file) == 0;
}
Free(file);
pthread_mutex_lock(&d->lock);
return ret;
}
static Array *
FlatList(Db *d, Array *dir)
{
FlatDb *db = (FlatDb *) d;
struct dirent *file;
Array *ret;
DIR *files;
char *path;
if (!d || !dir)
{
return NULL;
}
pthread_mutex_lock(&d->lock);
path = DbDirName(db, dir, 0);
files = opendir(path);
if (!files)
{
Free(path);
pthread_mutex_unlock(&d->lock);
return NULL;
}
ret = ArrayCreate();
while ((file = readdir(files)))
{
size_t namlen = strlen(file->d_name);
if (namlen > 5)
{
int nameOffset = namlen - 5;
if (StrEquals(file->d_name + nameOffset, ".json"))
{
file->d_name[nameOffset] = '\0';
StringArrayAppend(ret, file->d_name);
}
}
}
closedir(files);
Free(path);
pthread_mutex_unlock(&d->lock);
return ret;
}
static bool
FlatExists(Db *d, Array *dir)
{
FlatDb *db = (FlatDb *) d;
char *path;
bool ret;
if (!d || !dir)
{
return NULL;
}
pthread_mutex_lock(&d->lock);
path = DbFileName(db, dir);
ret = UtilLastModified(path) != 0;
Free(path);
pthread_mutex_unlock(&d->lock);
return ret;
}
Db * Db *
DbOpen(char *dir, size_t cache) DbOpen(char *dir, size_t cache)
{ {
@ -268,6 +362,9 @@ DbOpen(char *dir, size_t cache)
db->base.lockFunc = FlatLock; db->base.lockFunc = FlatLock;
db->base.unlock = FlatUnlock; db->base.unlock = FlatUnlock;
db->base.create = FlatCreate; db->base.create = FlatCreate;
db->base.delete = FlatDelete;
db->base.exists = FlatExists;
db->base.list = FlatList;
db->base.close = NULL; db->base.close = NULL;
return (Db *) db; return (Db *) db;

View file

@ -64,11 +64,14 @@ struct Db
DbRef *mostRecent; DbRef *mostRecent;
DbRef *leastRecent; DbRef *leastRecent;
/* TODO: Functions for implementation-specific operations /* Functions for implementation-specific operations
* (opening a ref, closing a db, removing an entry, ...) */ * (opening a ref, closing a db, removing an entry, ...) */
DbRef * (*lockFunc)(Db *, Array *); DbRef * (*lockFunc)(Db *, Array *);
DbRef * (*create)(Db *, Array *); DbRef * (*create)(Db *, Array *);
Array * (*list)(Db *, Array *);
bool (*unlock)(Db *, DbRef *); bool (*unlock)(Db *, DbRef *);
bool (*delete)(Db *, Array *);
bool (*exists)(Db *, Array *);
void (*close)(Db *); void (*close)(Db *);
/* Implementation-specific constructs */ /* Implementation-specific constructs */
@ -94,5 +97,6 @@ struct DbRef
extern void DbInit(Db *); extern void DbInit(Db *);
extern void DbRefInit(Db *, DbRef *); extern void DbRefInit(Db *, DbRef *);
extern void StringArrayFree(Array *); extern void StringArrayFree(Array *);
extern void StringArrayAppend(Array *, char *);
#endif #endif