From 9834dccfdd3d920e8f6937517c7711fa1394e2fe Mon Sep 17 00:00:00 2001 From: Graham Date: Sun, 31 Jan 2021 21:18:16 +0000 Subject: [PATCH] Add indexes to the groups table This will reduce the impact of checksum/version collisions, as a collision would have to happen between two indexes of the same archive rather than any two indexes. Signed-off-by: Graham --- .../openrs2/archive/cache/CacheExporter.kt | 18 ++++++++++-------- .../openrs2/archive/cache/CacheImporter.kt | 19 +++++++++++++------ 2 files changed, 23 insertions(+), 14 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 a8b22ba037..c27123d805 100644 --- a/archive/src/main/kotlin/org/openrs2/archive/cache/CacheExporter.kt +++ b/archive/src/main/kotlin/org/openrs2/archive/cache/CacheExporter.kt @@ -21,18 +21,20 @@ public class CacheExporter @Inject constructor( 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 containers c ON c.crc32 = e.crc32 - JOIN indexes i ON i.container_id = c.id AND i.version = e.version + 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 = ? UNION ALL - SELECT e.archive_id, ig.group_id, c.data, g.truncated_version + 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 containers ic ON ic.crc32 = e.crc32 - JOIN indexes i ON i.container_id = ic.id AND i.version = e.version - JOIN index_groups ig ON ig.container_id = i.container_id - JOIN groups g ON g.archive_id = e.archive_id 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 + 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 = ? """.trimIndent() ).use { stmt -> diff --git a/archive/src/main/kotlin/org/openrs2/archive/cache/CacheImporter.kt b/archive/src/main/kotlin/org/openrs2/archive/cache/CacheImporter.kt index 39c2894afc..87b81d0ae6 100644 --- a/archive/src/main/kotlin/org/openrs2/archive/cache/CacheImporter.kt +++ b/archive/src/main/kotlin/org/openrs2/archive/cache/CacheImporter.kt @@ -47,11 +47,12 @@ public class CacheImporter @Inject constructor( ) : Container(data, false) public class Index( + archive: Int, public val index: Js5Index, data: ByteBuf, - ) : Container(data, false) + ) : Group(Js5Archive.ARCHIVESET, archive, data, index.version, false) - public class Group( + public open class Group( public val archive: Int, public val group: Int, data: ByteBuf, @@ -218,7 +219,7 @@ public class CacheImporter @Inject constructor( public suspend fun importIndexAndGetMissingGroups(archive: Int, index: Js5Index, buf: ByteBuf): List { return database.execute { connection -> prepare(connection) - addIndex(connection, Index(index, buf)) + addIndex(connection, Index(archive, index, buf)) connection.prepareStatement( """ @@ -367,7 +368,7 @@ public class CacheImporter @Inject constructor( } } - private fun addGroups(connection: Connection, groups: List) { + private fun addGroups(connection: Connection, groups: List): List { val containerIds = addContainers(connection, groups) connection.prepareStatement( @@ -387,18 +388,24 @@ public class CacheImporter @Inject constructor( stmt.executeBatch() } + + return containerIds + } + + private fun addGroup(connection: Connection, group: Group): Long { + return addGroups(connection, listOf(group)).single() } private fun readIndex(store: Store, archive: Int): Index { return store.read(Js5Archive.ARCHIVESET, archive).use { buf -> Js5Compression.uncompress(buf.slice()).use { uncompressed -> - Index(Js5Index.read(uncompressed), buf.retain()) + Index(archive, Js5Index.read(uncompressed), buf.retain()) } } } private fun addIndex(connection: Connection, index: Index) { - val containerId = addContainer(connection, index) + val containerId = addGroup(connection, index) val savepoint = connection.setSavepoint() connection.prepareStatement(