From 273f7eddf8d40cf369efbe648d595b4d38bf1c16 Mon Sep 17 00:00:00 2001 From: Graham Date: Sun, 31 Jan 2021 21:44:04 +0000 Subject: [PATCH] Use a CTE to reduce duplication in CacheExporter Signed-off-by: Graham --- .../openrs2/archive/cache/CacheExporter.kt | 34 +++++++++---------- 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/archive/src/main/kotlin/org/openrs2/archive/cache/CacheExporter.kt b/archive/src/main/kotlin/org/openrs2/archive/cache/CacheExporter.kt index c27123d8..37c013b2 100644 --- a/archive/src/main/kotlin/org/openrs2/archive/cache/CacheExporter.kt +++ b/archive/src/main/kotlin/org/openrs2/archive/cache/CacheExporter.kt @@ -18,29 +18,27 @@ public class CacheExporter @Inject constructor( database.execute { connection -> connection.prepareStatement( """ - SELECT 255::uint1, e.archive_id::INTEGER, c.data, NULL - FROM master_index_entries e - JOIN master_indexes m ON m.container_id = e.container_id - JOIN groups g ON g.archive_id = 255 AND g.group_id = e.archive_id::INTEGER AND g.truncated_version = e.version & 65535 - JOIN containers c ON c.id = g.container_id AND c.crc32 = e.crc32 - JOIN indexes i ON i.container_id = g.container_id AND i.version = e.version - WHERE m.container_id = ? + WITH t AS ( + SELECT e.archive_id, c.data, g.container_id + FROM master_indexes m + JOIN master_index_entries e ON e.container_id = m.container_id + JOIN groups g ON g.archive_id = 255 AND g.group_id = e.archive_id::INTEGER AND g.truncated_version = e.version & 65535 + JOIN containers c ON c.id = g.container_id AND c.crc32 = e.crc32 + JOIN indexes i ON i.container_id = g.container_id AND i.version = e.version + WHERE m.container_id = ? + ) + SELECT 255::uint1, t.archive_id::INTEGER, t.data, NULL + FROM t UNION ALL - SELECT e.archive_id, ie.group_id, c.data, g.truncated_version - FROM master_index_entries e - JOIN master_indexes m ON m.container_id = e.container_id - JOIN groups ig ON ig.archive_id = 255 AND ig.group_id = e.archive_id::INTEGER AND ig.truncated_version = e.version & 65535 - JOIN containers ic ON ic.id = ig.container_id AND ic.crc32 = e.crc32 - JOIN indexes i ON i.container_id = ig.container_id AND i.version = e.version - JOIN index_groups ie ON ie.container_id = ig.container_id - JOIN groups g ON g.archive_id = e.archive_id AND g.group_id = ie.group_id AND g.truncated_version = ie.version & 65535 - JOIN containers c ON c.id = g.container_id AND c.crc32 = ie.crc32 - WHERE m.container_id = ? + SELECT t.archive_id, ig.group_id, c.data, g.truncated_version + FROM t + JOIN index_groups ig ON ig.container_id = t.container_id + JOIN groups g ON g.archive_id = t.archive_id::INTEGER AND g.group_id = ig.group_id AND g.truncated_version = ig.version & 65535 + JOIN containers c ON c.id = g.container_id AND c.crc32 = ig.crc32 """.trimIndent() ).use { stmt -> stmt.fetchSize = BATCH_SIZE stmt.setLong(1, id) - stmt.setLong(2, id) stmt.executeQuery().use { rows -> alloc.buffer(2, 2).use { versionBuf ->