2022-10-24 16:44:29 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2022 Jordan Bancino <@jordan:bancino.net>
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person
|
|
|
|
* obtaining a copy of this software and associated documentation files
|
|
|
|
* (the "Software"), to deal in the Software without restriction,
|
|
|
|
* including without limitation the rights to use, copy, modify, merge,
|
|
|
|
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
|
|
* and to permit persons to whom the Software is furnished to do so,
|
|
|
|
* subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
* included in all copies or portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
|
|
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
|
|
|
*/
|
|
|
|
#include <Db.h>
|
|
|
|
|
|
|
|
#include <Memory.h>
|
2022-11-06 00:47:17 +00:00
|
|
|
#include <Json.h>
|
2022-11-15 18:20:05 +00:00
|
|
|
#include <Util.h>
|
2022-10-24 16:44:29 +00:00
|
|
|
|
|
|
|
#include <pthread.h>
|
|
|
|
|
|
|
|
struct Db
|
|
|
|
{
|
2022-11-15 18:20:05 +00:00
|
|
|
char *dir;
|
2022-10-24 16:44:29 +00:00
|
|
|
pthread_mutex_t lock;
|
2022-10-31 12:12:42 +00:00
|
|
|
|
2022-11-18 20:42:08 +00:00
|
|
|
size_t cacheSize;
|
|
|
|
size_t maxCache;
|
2022-10-31 12:12:42 +00:00
|
|
|
HashMap *cache;
|
2022-11-18 20:42:08 +00:00
|
|
|
|
|
|
|
DbRef *mostRecent;
|
|
|
|
DbRef *leastRecent;
|
2022-10-24 16:44:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct DbRef
|
|
|
|
{
|
2022-10-31 12:12:42 +00:00
|
|
|
HashMap *json;
|
2022-10-24 16:44:29 +00:00
|
|
|
pthread_mutex_t lock;
|
2022-10-31 12:12:42 +00:00
|
|
|
|
|
|
|
unsigned long ts;
|
2022-11-15 18:20:05 +00:00
|
|
|
size_t size;
|
|
|
|
|
|
|
|
char *prefix;
|
|
|
|
char *key;
|
2022-11-18 20:42:08 +00:00
|
|
|
|
|
|
|
DbRef *prev;
|
|
|
|
DbRef *next;
|
2022-10-24 16:44:29 +00:00
|
|
|
};
|
|
|
|
|
2022-11-12 18:26:31 +00:00
|
|
|
static ssize_t DbComputeSize(HashMap *);
|
|
|
|
|
|
|
|
static ssize_t
|
|
|
|
DbComputeSizeOfValue(JsonValue * val)
|
|
|
|
{
|
|
|
|
MemoryInfo *a;
|
|
|
|
ssize_t total = 0;
|
|
|
|
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
union
|
|
|
|
{
|
|
|
|
char *str;
|
|
|
|
Array *arr;
|
|
|
|
} u;
|
|
|
|
|
|
|
|
if (!val)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
a = MemoryInfoGet(val);
|
|
|
|
if (a)
|
|
|
|
{
|
|
|
|
total += MemoryInfoGetSize(a);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (JsonValueType(val))
|
|
|
|
{
|
|
|
|
case JSON_OBJECT:
|
|
|
|
total += DbComputeSize(JsonValueAsObject(val));
|
|
|
|
break;
|
|
|
|
case JSON_ARRAY:
|
|
|
|
u.arr = JsonValueAsArray(val);
|
|
|
|
a = MemoryInfoGet(u.arr);
|
|
|
|
|
|
|
|
if (a)
|
|
|
|
{
|
|
|
|
total += MemoryInfoGetSize(a);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < ArraySize(u.arr); i++)
|
|
|
|
{
|
|
|
|
total += DbComputeSizeOfValue(ArrayGet(u.arr, i));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case JSON_STRING:
|
|
|
|
u.str = JsonValueAsString(val);
|
|
|
|
a = MemoryInfoGet(u.str);
|
|
|
|
if (a)
|
|
|
|
{
|
|
|
|
total += MemoryInfoGetSize(a);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case JSON_NULL:
|
|
|
|
case JSON_INTEGER:
|
|
|
|
case JSON_FLOAT:
|
|
|
|
case JSON_BOOLEAN:
|
|
|
|
default:
|
|
|
|
/* These don't use any extra heap space */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return total;
|
|
|
|
}
|
|
|
|
|
2022-11-06 00:47:17 +00:00
|
|
|
static ssize_t
|
2022-11-08 01:05:28 +00:00
|
|
|
DbComputeSize(HashMap * json)
|
2022-11-06 00:47:17 +00:00
|
|
|
{
|
2022-11-08 01:05:28 +00:00
|
|
|
char *key;
|
|
|
|
JsonValue *val;
|
|
|
|
MemoryInfo *a;
|
|
|
|
size_t total;
|
|
|
|
|
|
|
|
if (!json)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
total = 0;
|
|
|
|
|
|
|
|
a = MemoryInfoGet(json);
|
|
|
|
if (a)
|
|
|
|
{
|
|
|
|
total += MemoryInfoGetSize(a);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (HashMapIterate(json, &key, (void **) &val))
|
|
|
|
{
|
|
|
|
a = MemoryInfoGet(key);
|
|
|
|
if (a)
|
|
|
|
{
|
|
|
|
total += MemoryInfoGetSize(a);
|
|
|
|
}
|
|
|
|
|
2022-11-12 18:26:31 +00:00
|
|
|
total += DbComputeSizeOfValue(val);
|
2022-11-08 01:05:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return total;
|
2022-11-06 00:47:17 +00:00
|
|
|
}
|
|
|
|
|
2022-11-15 18:20:05 +00:00
|
|
|
static char *
|
|
|
|
DbHashKey(char *prefix, char *key)
|
|
|
|
{
|
|
|
|
return UtilStringConcat(prefix, key);
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
DbFileName(Db * db, char *prefix, char *key)
|
|
|
|
{
|
|
|
|
char *tmp = UtilStringConcat(prefix, "/");
|
|
|
|
char *tmp2 = UtilStringConcat(tmp, key);
|
|
|
|
char *tmp3 = UtilStringConcat(tmp2, ".json");
|
|
|
|
|
|
|
|
char *tmp4 = UtilStringConcat(db->dir, "/");
|
|
|
|
char *tmp5 = UtilStringConcat(tmp4, tmp3);
|
|
|
|
|
|
|
|
Free(tmp);
|
|
|
|
Free(tmp2);
|
|
|
|
Free(tmp3);
|
|
|
|
Free(tmp4);
|
|
|
|
|
|
|
|
return tmp5;
|
|
|
|
}
|
|
|
|
|
2022-11-18 20:42:08 +00:00
|
|
|
static void
|
|
|
|
DbCacheEvict(Db * db)
|
|
|
|
{
|
|
|
|
DbRef *ref = db->leastRecent;
|
|
|
|
|
|
|
|
while (ref && db->cacheSize > db->maxCache)
|
|
|
|
{
|
|
|
|
char *hash = DbHashKey(ref->prefix, ref->key);
|
|
|
|
|
|
|
|
JsonFree(ref->json);
|
|
|
|
pthread_mutex_destroy(&ref->lock);
|
|
|
|
|
|
|
|
HashMapDelete(db->cache, hash);
|
|
|
|
Free(hash);
|
|
|
|
Free(ref->prefix);
|
|
|
|
Free(ref->key);
|
|
|
|
|
|
|
|
db->cacheSize -= ref->size;
|
|
|
|
db->leastRecent = ref->next;
|
|
|
|
db->leastRecent->prev = NULL;
|
|
|
|
|
|
|
|
Free(ref);
|
|
|
|
|
|
|
|
ref = db->leastRecent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-24 16:44:29 +00:00
|
|
|
Db *
|
2022-11-15 18:20:05 +00:00
|
|
|
DbOpen(char *dir, size_t cache)
|
2022-10-24 16:44:29 +00:00
|
|
|
{
|
|
|
|
Db *db;
|
|
|
|
|
|
|
|
if (!dir)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
db = Malloc(sizeof(Db));
|
|
|
|
if (!db)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
db->dir = dir;
|
2022-10-31 12:12:42 +00:00
|
|
|
db->maxCache = cache;
|
2022-10-24 16:44:29 +00:00
|
|
|
|
|
|
|
pthread_mutex_init(&db->lock, NULL);
|
|
|
|
|
2022-10-31 12:12:42 +00:00
|
|
|
if (db->maxCache)
|
2022-10-24 16:44:29 +00:00
|
|
|
{
|
2022-10-31 12:12:42 +00:00
|
|
|
db->cache = HashMapCreate();
|
|
|
|
if (!db->cache)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
2022-10-24 16:44:29 +00:00
|
|
|
}
|
2022-11-15 18:20:05 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
db->cache = NULL;
|
|
|
|
}
|
2022-10-24 16:44:29 +00:00
|
|
|
|
2022-11-18 20:42:08 +00:00
|
|
|
db->mostRecent = NULL;
|
|
|
|
db->leastRecent = NULL;
|
|
|
|
|
2022-10-24 16:44:29 +00:00
|
|
|
return db;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DbClose(Db * db)
|
|
|
|
{
|
2022-11-15 18:20:05 +00:00
|
|
|
char *key;
|
|
|
|
DbRef *val;
|
|
|
|
|
2022-10-24 16:44:29 +00:00
|
|
|
if (!db)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-10-31 12:12:42 +00:00
|
|
|
pthread_mutex_destroy(&db->lock);
|
2022-11-15 18:20:05 +00:00
|
|
|
|
|
|
|
if (db->cache)
|
|
|
|
{
|
|
|
|
while (HashMapIterate(db->cache, &key, (void **) &val))
|
|
|
|
{
|
|
|
|
Free(key);
|
|
|
|
JsonFree(val->json);
|
|
|
|
Free(val->prefix);
|
|
|
|
Free(val->key);
|
|
|
|
pthread_mutex_destroy(&val->lock);
|
|
|
|
Free(val);
|
|
|
|
}
|
|
|
|
HashMapFree(db->cache);
|
|
|
|
}
|
2022-10-31 12:12:42 +00:00
|
|
|
|
2022-10-24 16:44:29 +00:00
|
|
|
Free(db);
|
|
|
|
}
|
|
|
|
|
|
|
|
DbRef *
|
|
|
|
DbCreate(Db * db, char *prefix, char *key)
|
|
|
|
{
|
2022-11-15 18:20:05 +00:00
|
|
|
FILE *fp;
|
|
|
|
char *file;
|
|
|
|
|
2022-10-31 12:12:42 +00:00
|
|
|
if (!db || !prefix || !key)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-11-15 18:20:05 +00:00
|
|
|
file = DbFileName(db, prefix, key);
|
|
|
|
|
2022-11-17 22:57:29 +00:00
|
|
|
if (UtilLastModified(file) || UtilMkdir(prefix, 0750) < 0)
|
2022-11-15 18:20:05 +00:00
|
|
|
{
|
2022-11-17 22:57:29 +00:00
|
|
|
Free(file);
|
2022-11-15 18:20:05 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-11-17 22:57:29 +00:00
|
|
|
fp = fopen(file, "w");
|
|
|
|
Free(file);
|
|
|
|
if (!fp)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(fp, "{}");
|
|
|
|
fflush(fp);
|
|
|
|
fclose(fp);
|
|
|
|
|
2022-11-15 18:20:05 +00:00
|
|
|
return DbLock(db, prefix, key);
|
2022-10-24 16:44:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DbRef *
|
|
|
|
DbLock(Db * db, char *prefix, char *key)
|
|
|
|
{
|
2022-11-15 18:20:05 +00:00
|
|
|
char *file;
|
|
|
|
char *hash;
|
|
|
|
DbRef *ref;
|
|
|
|
|
2022-10-31 12:12:42 +00:00
|
|
|
if (!db || !prefix || !key)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-11-15 18:20:05 +00:00
|
|
|
ref = NULL;
|
|
|
|
hash = NULL;
|
|
|
|
|
2022-10-31 12:12:42 +00:00
|
|
|
pthread_mutex_lock(&db->lock);
|
|
|
|
|
2022-11-15 18:20:05 +00:00
|
|
|
/* Check if the item is in the cache */
|
|
|
|
if (db->cache)
|
|
|
|
{
|
|
|
|
hash = DbHashKey(prefix, key);
|
|
|
|
ref = HashMapGet(db->cache, hash);
|
|
|
|
}
|
|
|
|
|
|
|
|
file = DbFileName(db, prefix, key);
|
|
|
|
|
|
|
|
if (ref) /* In cache */
|
|
|
|
{
|
|
|
|
unsigned long diskTs = UtilLastModified(file);
|
|
|
|
|
|
|
|
if (diskTs > ref->ts)
|
|
|
|
{
|
|
|
|
/* File was modified on disk since it was cached */
|
|
|
|
FILE *fp = fopen(file, "r");
|
|
|
|
HashMap *json;
|
|
|
|
|
|
|
|
if (!fp)
|
|
|
|
{
|
|
|
|
ref = NULL;
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
|
|
|
json = JsonDecode(fp);
|
|
|
|
fclose(fp);
|
|
|
|
|
|
|
|
if (!json)
|
|
|
|
{
|
|
|
|
ref = NULL;
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonFree(ref->json);
|
|
|
|
ref->json = json;
|
|
|
|
ref->ts = diskTs;
|
|
|
|
ref->size = DbComputeSize(ref->json);
|
2022-11-18 20:42:08 +00:00
|
|
|
|
|
|
|
if (db->cache)
|
|
|
|
{
|
|
|
|
/* Float this ref to mostRecent */
|
|
|
|
if (ref->next)
|
|
|
|
{
|
|
|
|
ref->next->prev = ref->prev;
|
|
|
|
ref->prev->next = ref->next;
|
|
|
|
|
|
|
|
if (!ref->prev)
|
|
|
|
{
|
|
|
|
db->leastRecent = ref->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
ref->prev = db->mostRecent;
|
|
|
|
ref->next = NULL;
|
|
|
|
db->mostRecent = ref;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The file on disk may be larger than what we have in
|
|
|
|
* memory, which may require items in cache to be
|
|
|
|
* evicted. */
|
|
|
|
DbCacheEvict(db);
|
|
|
|
}
|
2022-11-15 18:20:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Not in cache; load from disk */
|
|
|
|
FILE *fp = fopen(file, "r");
|
|
|
|
|
|
|
|
if (!fp)
|
|
|
|
{
|
|
|
|
ref = NULL;
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
|
|
|
ref = Malloc(sizeof(DbRef));
|
|
|
|
if (!ref)
|
|
|
|
{
|
|
|
|
fclose(fp);
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
|
|
|
ref->json = JsonDecode(fp);
|
|
|
|
fclose(fp);
|
|
|
|
|
|
|
|
if (!ref->json)
|
|
|
|
{
|
|
|
|
Free(ref);
|
|
|
|
ref = NULL;
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
|
|
|
pthread_mutex_init(&ref->lock, NULL);
|
|
|
|
ref->ts = UtilServerTs();
|
|
|
|
ref->size = DbComputeSize(ref->json);
|
2022-11-18 19:36:15 +00:00
|
|
|
ref->prefix = UtilStringDuplicate(prefix);
|
|
|
|
ref->key = UtilStringDuplicate(key);
|
2022-11-15 18:20:05 +00:00
|
|
|
|
|
|
|
/* If cache is enabled, cache this ref */
|
|
|
|
if (db->cache)
|
|
|
|
{
|
|
|
|
HashMapSet(db->cache, hash, ref);
|
|
|
|
db->cacheSize += ref->size;
|
|
|
|
|
2022-11-18 20:42:08 +00:00
|
|
|
ref->next = NULL;
|
|
|
|
ref->prev = db->mostRecent;
|
|
|
|
db->mostRecent = ref;
|
|
|
|
|
|
|
|
/* Adding this item to the cache may case it to grow too
|
|
|
|
* large, requiring some items to be evicted */
|
|
|
|
DbCacheEvict(db);
|
2022-11-15 18:20:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pthread_mutex_lock(&ref->lock);
|
|
|
|
|
|
|
|
finish:
|
2022-11-17 23:51:45 +00:00
|
|
|
pthread_mutex_unlock(&db->lock);
|
|
|
|
|
2022-11-15 18:20:05 +00:00
|
|
|
Free(file);
|
|
|
|
Free(hash);
|
2022-11-18 19:36:15 +00:00
|
|
|
|
2022-11-15 18:20:05 +00:00
|
|
|
return ref;
|
2022-10-24 16:44:29 +00:00
|
|
|
}
|
|
|
|
|
2022-11-17 22:57:29 +00:00
|
|
|
int
|
2022-10-24 16:44:29 +00:00
|
|
|
DbUnlock(Db * db, DbRef * ref)
|
|
|
|
{
|
2022-11-15 18:20:05 +00:00
|
|
|
FILE *fp;
|
|
|
|
char *file;
|
|
|
|
|
2022-10-31 12:12:42 +00:00
|
|
|
if (!db || !ref)
|
|
|
|
{
|
2022-11-17 22:57:29 +00:00
|
|
|
return 0;
|
2022-10-31 12:12:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pthread_mutex_lock(&db->lock);
|
|
|
|
|
2022-11-15 18:20:05 +00:00
|
|
|
file = DbFileName(db, ref->prefix, ref->key);
|
|
|
|
fp = fopen(file, "w");
|
2022-11-18 19:36:15 +00:00
|
|
|
Free(file);
|
|
|
|
|
2022-11-15 18:20:05 +00:00
|
|
|
if (!fp)
|
2022-11-17 22:57:29 +00:00
|
|
|
{
|
2022-11-18 19:36:15 +00:00
|
|
|
pthread_mutex_unlock(&db->lock);
|
2022-11-17 22:57:29 +00:00
|
|
|
return 0;
|
2022-11-15 18:20:05 +00:00
|
|
|
}
|
2022-10-31 12:12:42 +00:00
|
|
|
|
2022-11-15 18:20:05 +00:00
|
|
|
JsonEncode(ref->json, fp);
|
|
|
|
fflush(fp);
|
|
|
|
fclose(fp);
|
|
|
|
|
|
|
|
pthread_mutex_unlock(&ref->lock);
|
|
|
|
|
|
|
|
/* No cache, free it now */
|
|
|
|
if (!db->cache)
|
|
|
|
{
|
|
|
|
JsonFree(ref->json);
|
|
|
|
Free(ref->prefix);
|
|
|
|
Free(ref->key);
|
|
|
|
pthread_mutex_destroy(&ref->lock);
|
|
|
|
Free(ref);
|
|
|
|
}
|
2022-11-18 19:36:15 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* This ref should be in the cache, just update it's size */
|
|
|
|
db->cacheSize -= ref->size;
|
|
|
|
ref->size = DbComputeSize(ref->json);
|
|
|
|
db->cacheSize += ref->size;
|
2022-11-18 20:42:08 +00:00
|
|
|
|
|
|
|
/* If this ref has grown significantly since we last computed
|
|
|
|
* its size, it may have filled the cache and require some
|
|
|
|
* items to be evicted. */
|
|
|
|
DbCacheEvict(db);
|
2022-11-18 19:36:15 +00:00
|
|
|
}
|
2022-11-17 22:57:29 +00:00
|
|
|
|
2022-11-18 19:36:15 +00:00
|
|
|
pthread_mutex_unlock(&db->lock);
|
2022-11-17 22:57:29 +00:00
|
|
|
return 1;
|
2022-10-24 16:44:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
HashMap *
|
|
|
|
DbJson(DbRef * ref)
|
|
|
|
{
|
2022-10-31 12:12:42 +00:00
|
|
|
return ref ? ref->json : NULL;
|
2022-10-24 16:44:29 +00:00
|
|
|
}
|