From 5d590df83d52b2b20f42f140493339caf67b0948 Mon Sep 17 00:00:00 2001 From: Jordan Bancino Date: Fri, 3 Mar 2023 14:26:10 +0000 Subject: [PATCH] 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. --- TODO.txt | 4 +-- src/Db.c | 72 +++++++++++++++++++++++++++-------------- src/Telodendria.c | 13 ++------ src/TelodendriaConfig.c | 2 +- src/include/Db.h | 4 --- 5 files changed, 52 insertions(+), 43 deletions(-) diff --git a/TODO.txt b/TODO.txt index 2eea4e6..409cb91 100644 --- a/TODO.txt +++ b/TODO.txt @@ -13,9 +13,9 @@ Milestone: v0.2.0 [ ] 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 - [ ] 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()) [~] User login [x] User manipulation functions (so we don't use the DB directly) diff --git a/src/Db.c b/src/Db.c index 6dde54a..7016d62 100644 --- a/src/Db.c +++ b/src/Db.c @@ -328,7 +328,7 @@ DbOpen(char *dir, size_t cache) { Db *db; - if (!dir || cache < DB_MIN_CACHE) + if (!dir) { return NULL; } @@ -344,14 +344,21 @@ DbOpen(char *dir, size_t cache) pthread_mutex_init(&db->lock, NULL); - db->cache = HashMapCreate(); - if (!db->cache) + if (db->maxCache) { - return NULL; - } + db->cache = HashMapCreate(); + if (!db->cache) + { + return NULL; + } - db->mostRecent = NULL; - db->leastRecent = NULL; + db->mostRecent = NULL; + db->leastRecent = NULL; + } + else + { + db->cache = NULL; + } return db; } @@ -537,8 +544,6 @@ DbLockFromArr(Db * db, Array * args) pthread_mutex_init(&ref->lock, NULL); pthread_mutex_lock(&ref->lock); - ref->ts = UtilServerTs(); - ref->size = DbComputeSize(ref->json); for (i = 0; i < ArraySize(args); i++) { @@ -546,16 +551,21 @@ DbLockFromArr(Db * db, Array * args) } ref->name = name; - HashMapSet(db->cache, hash, ref); - db->cacheSize += ref->size; + if (db->cache) + { + ref->ts = UtilServerTs(); + ref->size = DbComputeSize(ref->json); + HashMapSet(db->cache, hash, ref); + db->cacheSize += ref->size; - ref->next = NULL; - ref->prev = db->mostRecent; - db->mostRecent = ref; + 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); + /* Adding this item to the cache may case it to grow too + * large, requiring some items to be evicted */ + DbCacheEvict(db); + } } finish: @@ -747,16 +757,28 @@ DbUnlock(Db * db, DbRef * ref) fflush(ref->fp); fclose(ref->fp); - db->cacheSize -= ref->size; - ref->size = DbComputeSize(ref->json); - db->cacheSize += ref->size; + if (db->cache) + { + 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 - * size, it may have filled the cache and require some items to be - * evicted. */ - DbCacheEvict(db); + pthread_mutex_unlock(&ref->lock); + pthread_mutex_destroy(&ref->lock); + + Free(ref); + } pthread_mutex_unlock(&db->lock); return 1; diff --git a/src/Telodendria.c b/src/Telodendria.c index ddb84b1..b7e5cfd 100644 --- a/src/Telodendria.c +++ b/src/Telodendria.c @@ -523,20 +523,11 @@ main(int argc, char **argv) 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, "If this is not what you intended, check the config file"); Log(lc, LOG_WARNING, - "and ensure that max-cache 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; + "and ensure that maxCache is a valid number of bytes."); } matrixArgs.db = DbOpen(".", tConfig->maxCache); diff --git a/src/TelodendriaConfig.c b/src/TelodendriaConfig.c index d96194e..4eba79d 100644 --- a/src/TelodendriaConfig.c +++ b/src/TelodendriaConfig.c @@ -258,7 +258,7 @@ TelodendriaConfigParse(HashMap * config, LogConfig * lc) CONFIG_OPTIONAL_INTEGER(tConfig->threads, "threads", 1); 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); if (JsonValueAsBoolean(value)) diff --git a/src/include/Db.h b/src/include/Db.h index b77c74f..611b303 100644 --- a/src/include/Db.h +++ b/src/include/Db.h @@ -24,10 +24,6 @@ #ifndef TELODENDRIA_DB_H #define TELODENDRIA_DB_H -#ifndef DB_MIN_CACHE -#define DB_MIN_CACHE 1024 -#endif - #include #include