Remove tmp_groups table

Like the tmp_indexes table, there's no need for it - we can read the
data we need from the index_groups rows we've just inserted.

Signed-off-by: Graham <gpe@openrs2.org>
Graham 4 years ago
parent 692ca20344
commit e3865180ca
  1. 56
      archive/src/main/kotlin/org/openrs2/archive/cache/CacheImporter.kt

@ -254,36 +254,7 @@ public class CacheImporter @Inject constructor(
): List<Int> { ): List<Int> {
return database.execute { connection -> return database.execute { connection ->
prepare(connection) prepare(connection)
addIndex(connection, Index(archive, index, buf, uncompressed)) val id = addIndex(connection, Index(archive, index, buf, uncompressed))
connection.prepareStatement(
"""
CREATE TEMPORARY TABLE tmp_groups (
group_id INTEGER NOT NULL,
crc32 INTEGER NOT NULL,
version INTEGER NOT NULL
) ON COMMIT DROP
""".trimIndent()
).use { stmt ->
stmt.execute()
}
connection.prepareStatement(
"""
INSERT INTO tmp_groups (group_id, crc32, version)
VALUES (?, ?, ?)
""".trimIndent()
).use { stmt ->
for (entry in index) {
stmt.setInt(1, entry.id)
stmt.setInt(2, entry.checksum)
stmt.setInt(3, entry.version)
stmt.addBatch()
}
stmt.executeBatch()
}
/* /*
* In order to defend against (crc32, version) collisions, we only * In order to defend against (crc32, version) collisions, we only
@ -298,21 +269,22 @@ public class CacheImporter @Inject constructor(
*/ */
connection.prepareStatement( connection.prepareStatement(
""" """
SELECT t.group_id SELECT ig.group_id
FROM tmp_groups t FROM index_groups ig
LEFT JOIN master_index_valid_indexes i ON i.master_index_id = ? AND LEFT JOIN master_index_valid_indexes i ON i.master_index_id = ? AND
i.archive_id = ? i.archive_id = ?
LEFT JOIN index_groups ig ON ig.container_id = i.container_id AND ig.group_id = t.group_id AND LEFT JOIN index_groups ig2 ON ig2.container_id = i.container_id AND ig2.group_id = ig.group_id AND
ig.crc32 = t.crc32 AND ig.version = t.version ig2.crc32 = ig.crc32 AND ig2.version = ig.version
LEFT JOIN groups g ON g.archive_id = i.archive_id AND g.group_id = ig.group_id AND LEFT JOIN groups g ON g.archive_id = i.archive_id AND g.group_id = ig2.group_id AND
g.version = ig.version AND NOT g.version_truncated g.version = ig2.version AND NOT g.version_truncated
LEFT JOIN containers c ON c.id = g.container_id AND c.crc32 = ig.crc32 LEFT JOIN containers c ON c.id = g.container_id AND c.crc32 = ig2.crc32
WHERE g.container_id IS NULL WHERE ig.container_id = ? AND g.container_id IS NULL
ORDER BY t.group_id ASC ORDER BY ig.group_id ASC
""".trimIndent() """.trimIndent()
).use { stmt -> ).use { stmt ->
stmt.setObject(1, previousMasterIndexId, Types.INTEGER) stmt.setObject(1, previousMasterIndexId, Types.INTEGER)
stmt.setInt(2, archive) stmt.setInt(2, archive)
stmt.setLong(3, id)
stmt.executeQuery().use { rows -> stmt.executeQuery().use { rows ->
val groups = mutableListOf<Int>() val groups = mutableListOf<Int>()
@ -582,7 +554,7 @@ public class CacheImporter @Inject constructor(
} }
} }
private fun addIndex(connection: Connection, index: Index) { private fun addIndex(connection: Connection, index: Index): Long {
val containerId = addGroup(connection, index) val containerId = addGroup(connection, index)
val savepoint = connection.setSavepoint() val savepoint = connection.setSavepoint()
@ -607,7 +579,7 @@ public class CacheImporter @Inject constructor(
} catch (ex: SQLException) { } catch (ex: SQLException) {
if (ex.sqlState == PSQLState.UNIQUE_VIOLATION.state) { if (ex.sqlState == PSQLState.UNIQUE_VIOLATION.state) {
connection.rollback(savepoint) connection.rollback(savepoint)
return@addIndex return@addIndex containerId
} }
throw ex throw ex
} }
@ -679,6 +651,8 @@ public class CacheImporter @Inject constructor(
stmt.executeBatch() stmt.executeBatch()
} }
return containerId
} }
private fun prepare(connection: Connection) { private fun prepare(connection: Connection) {

Loading…
Cancel
Save