From eb1f731b9d9a180dd16590b2e52c55187febe737 Mon Sep 17 00:00:00 2001 From: Fabian Schlenz Date: Mon, 5 Mar 2018 06:39:30 +0100 Subject: [PATCH] DB_Update_9 now uses chunks of messages to (massively) reduce the amount of heap space needed. --- .../telegram_backup/DatabaseUpdates.kt | 33 ++++++++++++------- 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/src/main/kotlin/de/fabianonline/telegram_backup/DatabaseUpdates.kt b/src/main/kotlin/de/fabianonline/telegram_backup/DatabaseUpdates.kt index 583b4a9..4ddfe59 100644 --- a/src/main/kotlin/de/fabianonline/telegram_backup/DatabaseUpdates.kt +++ b/src/main/kotlin/de/fabianonline/telegram_backup/DatabaseUpdates.kt @@ -381,21 +381,30 @@ internal class DB_Update_9(conn: Connection, db: Database) : DatabaseUpdate(conn override fun _doUpdate() { val logger = LoggerFactory.getLogger(DB_Update_9::class.java) println(" Updating supergroup channel message data (this might take some time)...") - val rs = stmt.executeQuery("SELECT id, data, source_id FROM messages WHERE source_type='channel' and sender_id IS NULL and api_layer=53") - val messages = TLVector() - val messages_to_delete = mutableListOf() + val count = db.queryInt("SELECT COUNT(*) FROM messages WHERE source_type='channel' and sender_id IS NULL and api_layer=53") + logger.debug("Found $count candidates for conversion") + val limit = 5000 + var offset = 0 var i = 0 - while (rs.next()) { - i++ - val msg = Database.bytesToTLMessage(rs.getBytes(2)) - if (msg!!.getFromId() != null) { - messages.add(msg) - messages_to_delete.add(rs.getInt(1)) + while (offset + 1 < count) { + logger.debug("Querying with limit $limit and offset $offset") + val rs = stmt.executeQuery("SELECT id, data, source_id FROM messages WHERE source_type='channel' and sender_id IS NULL and api_layer=53 LIMIT ${limit} OFFSET ${offset}") + val messages = TLVector() + val messages_to_delete = mutableListOf() + while (rs.next()) { + val msg = Database.bytesToTLMessage(rs.getBytes(2)) + if (msg!!.getFromId() != null) { + i++ + messages.add(msg) + messages_to_delete.add(rs.getInt(1)) + } } + db.saveMessages(messages, api_layer=53, source_type=MessageSource.SUPERGROUP) + execute("DELETE FROM messages WHERE id IN (" + messages_to_delete.joinToString() + ")") + + offset += limit } - logger.info("Converted ${messages.size} of ${i} messages.") - db.saveMessages(messages, api_layer=53, source_type=MessageSource.SUPERGROUP) - execute("DELETE FROM messages WHERE id IN (" + messages_to_delete.joinToString() + ")") + logger.info("Converted ${i} of ${count} messages.") println(" Cleaning up the database (this might also take some time, sorry)...") execute("VACUUM") }