Use a CTE to reduce duplication in CacheExporter

Signed-off-by: Graham <gpe@openrs2.org>
Graham 4 years ago
parent 9834dccfdd
commit 273f7eddf8
  1. 26
      archive/src/main/kotlin/org/openrs2/archive/cache/CacheExporter.kt

@ -18,29 +18,27 @@ public class CacheExporter @Inject constructor(
database.execute { connection -> database.execute { connection ->
connection.prepareStatement( connection.prepareStatement(
""" """
SELECT 255::uint1, e.archive_id::INTEGER, c.data, NULL WITH t AS (
FROM master_index_entries e SELECT e.archive_id, c.data, g.container_id
JOIN master_indexes m ON m.container_id = e.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 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 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 JOIN indexes i ON i.container_id = g.container_id AND i.version = e.version
WHERE m.container_id = ? WHERE m.container_id = ?
)
SELECT 255::uint1, t.archive_id::INTEGER, t.data, NULL
FROM t
UNION ALL UNION ALL
SELECT e.archive_id, ie.group_id, c.data, g.truncated_version SELECT t.archive_id, ig.group_id, c.data, g.truncated_version
FROM master_index_entries e FROM t
JOIN master_indexes m ON m.container_id = e.container_id JOIN index_groups ig ON ig.container_id = t.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 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 ic ON ic.id = ig.container_id AND ic.crc32 = e.crc32 JOIN containers c ON c.id = g.container_id AND c.crc32 = ig.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 = ?
""".trimIndent() """.trimIndent()
).use { stmt -> ).use { stmt ->
stmt.fetchSize = BATCH_SIZE stmt.fetchSize = BATCH_SIZE
stmt.setLong(1, id) stmt.setLong(1, id)
stmt.setLong(2, id)
stmt.executeQuery().use { rows -> stmt.executeQuery().use { rows ->
alloc.buffer(2, 2).use { versionBuf -> alloc.buffer(2, 2).use { versionBuf ->

Loading…
Cancel
Save