Remove DB_MIN_CACHE because that's dumb.

You should be able to totally disable the cache if you so please. This
should ensure Telodendria uses less memory on constrained systems.
This commit is contained in:
Jordan Bancino 2023-03-03 14:26:10 +00:00
parent 1770789333
commit 5d590df83d
5 changed files with 52 additions and 43 deletions

View file

@ -13,9 +13,9 @@ Milestone: v0.2.0
[ ] Abstract /email/requestToken and /msidsn/requestToken [ ] Abstract /email/requestToken and /msidsn/requestToken
[~] Db API [x] Db API
[x] If object is in cache, but doesn't exist on disk, delete from cache [x] If object is in cache, but doesn't exist on disk, delete from cache
[ ] Allow cache to be totally disabled (no MIN_CACHE_SIZE) [x] Allow cache to be totally disabled (no MIN_CACHE_SIZE)
[x] List keys under a path (DbList() using POSIX opendir()) [x] List keys under a path (DbList() using POSIX opendir())
[~] User login [~] User login
[x] User manipulation functions (so we don't use the DB directly) [x] User manipulation functions (so we don't use the DB directly)

View file

@ -328,7 +328,7 @@ DbOpen(char *dir, size_t cache)
{ {
Db *db; Db *db;
if (!dir || cache < DB_MIN_CACHE) if (!dir)
{ {
return NULL; return NULL;
} }
@ -344,14 +344,21 @@ DbOpen(char *dir, size_t cache)
pthread_mutex_init(&db->lock, NULL); pthread_mutex_init(&db->lock, NULL);
db->cache = HashMapCreate(); if (db->maxCache)
if (!db->cache)
{ {
return NULL; db->cache = HashMapCreate();
} if (!db->cache)
{
return NULL;
}
db->mostRecent = NULL; db->mostRecent = NULL;
db->leastRecent = NULL; db->leastRecent = NULL;
}
else
{
db->cache = NULL;
}
return db; return db;
} }
@ -537,8 +544,6 @@ DbLockFromArr(Db * db, Array * args)
pthread_mutex_init(&ref->lock, NULL); pthread_mutex_init(&ref->lock, NULL);
pthread_mutex_lock(&ref->lock); pthread_mutex_lock(&ref->lock);
ref->ts = UtilServerTs();
ref->size = DbComputeSize(ref->json);
for (i = 0; i < ArraySize(args); i++) for (i = 0; i < ArraySize(args); i++)
{ {
@ -546,16 +551,21 @@ DbLockFromArr(Db * db, Array * args)
} }
ref->name = name; ref->name = name;
HashMapSet(db->cache, hash, ref); if (db->cache)
db->cacheSize += ref->size; {
ref->ts = UtilServerTs();
ref->size = DbComputeSize(ref->json);
HashMapSet(db->cache, hash, ref);
db->cacheSize += ref->size;
ref->next = NULL; ref->next = NULL;
ref->prev = db->mostRecent; ref->prev = db->mostRecent;
db->mostRecent = ref; db->mostRecent = ref;
/* Adding this item to the cache may case it to grow too large, /* Adding this item to the cache may case it to grow too
* requiring some items to be evicted */ * large, requiring some items to be evicted */
DbCacheEvict(db); DbCacheEvict(db);
}
} }
finish: finish:
@ -747,16 +757,28 @@ DbUnlock(Db * db, DbRef * ref)
fflush(ref->fp); fflush(ref->fp);
fclose(ref->fp); fclose(ref->fp);
db->cacheSize -= ref->size; if (db->cache)
ref->size = DbComputeSize(ref->json); {
db->cacheSize += ref->size; db->cacheSize -= ref->size;
ref->size = DbComputeSize(ref->json);
db->cacheSize += ref->size;
pthread_mutex_unlock(&ref->lock); /* 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);
pthread_mutex_unlock(&ref->lock);
}
else
{
JsonFree(ref->json);
StringArrayFree(ref->name);
/* If this ref has grown significantly since we last computed its pthread_mutex_unlock(&ref->lock);
* size, it may have filled the cache and require some items to be pthread_mutex_destroy(&ref->lock);
* evicted. */
DbCacheEvict(db); Free(ref);
}
pthread_mutex_unlock(&db->lock); pthread_mutex_unlock(&db->lock);
return 1; return 1;

View file

@ -523,20 +523,11 @@ main(int argc, char **argv)
if (!tConfig->maxCache) if (!tConfig->maxCache)
{ {
Log(lc, LOG_WARNING, "Max-cache is set to zero."); Log(lc, LOG_WARNING, "Database caching is disabled.");
Log(lc, LOG_WARNING, Log(lc, LOG_WARNING,
"If this is not what you intended, check the config file"); "If this is not what you intended, check the config file");
Log(lc, LOG_WARNING, Log(lc, LOG_WARNING,
"and ensure that max-cache is a valid number of bytes."); "and ensure that maxCache is a valid number of bytes.");
}
if (tConfig->maxCache < DB_MIN_CACHE)
{
Log(lc, LOG_WARNING,
"Specified max cache size is less than the minimum of %d bytes.",
DB_MIN_CACHE);
Log(lc, LOG_WARNING, "Using a max-cache of %d bytes.", DB_MIN_CACHE);
tConfig->maxCache = DB_MIN_CACHE;
} }
matrixArgs.db = DbOpen(".", tConfig->maxCache); matrixArgs.db = DbOpen(".", tConfig->maxCache);

View file

@ -258,7 +258,7 @@ TelodendriaConfigParse(HashMap * config, LogConfig * lc)
CONFIG_OPTIONAL_INTEGER(tConfig->threads, "threads", 1); CONFIG_OPTIONAL_INTEGER(tConfig->threads, "threads", 1);
CONFIG_OPTIONAL_INTEGER(tConfig->maxConnections, "maxConnections", 32); CONFIG_OPTIONAL_INTEGER(tConfig->maxConnections, "maxConnections", 32);
CONFIG_OPTIONAL_INTEGER(tConfig->maxCache, "maxCache", DB_MIN_CACHE); CONFIG_OPTIONAL_INTEGER(tConfig->maxCache, "maxCache", 0);
CONFIG_REQUIRE("federation", JSON_BOOLEAN); CONFIG_REQUIRE("federation", JSON_BOOLEAN);
if (JsonValueAsBoolean(value)) if (JsonValueAsBoolean(value))

View file

@ -24,10 +24,6 @@
#ifndef TELODENDRIA_DB_H #ifndef TELODENDRIA_DB_H
#define TELODENDRIA_DB_H #define TELODENDRIA_DB_H
#ifndef DB_MIN_CACHE
#define DB_MIN_CACHE 1024
#endif
#include <stddef.h> #include <stddef.h>
#include <HashMap.h> #include <HashMap.h>