From 187b089946726a45769891115aef2408bff0a740 Mon Sep 17 00:00:00 2001 From: carbotaniuman <41451839+carbotaniuman@users.noreply.github.com> Date: Wed, 17 Feb 2021 15:57:47 -0600 Subject: [PATCH] Reduce DB load --- src/main/kotlin/mdnet/cache/ImageStorage.kt | 32 +++++++++++---------- src/main/kotlin/mdnet/cache/metadata.kt | 10 ++----- 2 files changed, 20 insertions(+), 22 deletions(-) diff --git a/src/main/kotlin/mdnet/cache/ImageStorage.kt b/src/main/kotlin/mdnet/cache/ImageStorage.kt index 20891f4..7e0ba1d 100644 --- a/src/main/kotlin/mdnet/cache/ImageStorage.kt +++ b/src/main/kotlin/mdnet/cache/ImageStorage.kt @@ -130,28 +130,30 @@ class ImageStorage( LOGGER.info { "Cache at $size out of $maxSize bytes" } // we need to prune the cache now if (size > maxSize * 0.95) { - val toClear = size - (maxSize * 0.9).toLong() + var toClear = size - (maxSize * 0.9).toLong() LOGGER.info { "Evicting at least $toClear bytes from cache" } - val list = database.useConnection { conn -> - conn.prepareStatement(IMAGES_TO_PRUNE).apply { - setLong(1, toClear) - }.use { stmt -> - stmt.executeQuery().let { - val ret = ArrayList() + while (toClear > 0) { + val list = database.useConnection { conn -> + conn.prepareStatement(IMAGES_TO_PRUNE).use { stmt -> + stmt.executeQuery().let { + val ret = ArrayList>() - while (it.next()) { - ret.add(it.getString(1)) + while (it.next()) { + ret.add(it.getString(1) to it.getInt(2)) + } + + ret } - - ret } } - } - for (id in list) { - LOGGER.info { "Evicting images $id from cache" } - deleteImage(id) + val count = list.fold(0) { sum, (id, num) -> + deleteImage(id) + sum + num + } + LOGGER.info { "Evicting $count bytes from cache this loop" } + toClear -= count } } } diff --git a/src/main/kotlin/mdnet/cache/metadata.kt b/src/main/kotlin/mdnet/cache/metadata.kt index 2ccdbc4..44bbd37 100644 --- a/src/main/kotlin/mdnet/cache/metadata.kt +++ b/src/main/kotlin/mdnet/cache/metadata.kt @@ -34,16 +34,12 @@ create table if not exists Images( accessed timestamp not null default CURRENT_TIMESTAMP, disk_size integer as ((size + 4095) / 4096 * 4096) ); -create index if not exists Images_lastAccessed_idx on Images(accessed, disk_size, id); +drop index if exists Images_lastAccessed_idx; +create index if not exists Images_accessed on Images(accessed); """ const val SIZE_TAKEN_SQL = "select sum(disk_size) from Images" const val IMAGES_TO_PRUNE = """ -select id from ( - select id, sum(disk_size) - OVER (order by accessed rows unbounded preceding exclude current row) - as RunningTotal from Images -) as X -WHERE coalesce(X.RunningTotal, 0) <= ?; +select id, disk_size from Images order by accessed asc limit 1000 """