1
0
mirror of https://github.com/fabianonline/telegram_backup.git synced 2024-11-22 16:56:16 +00:00

Added limit and offset parameters to Database#getMessagesWithMedia in order to prevent high memory usage in downloadMedia.

This commit is contained in:
Fabian Schlenz 2018-03-15 20:49:15 +01:00
parent 968ee831f0
commit f4d563226c
2 changed files with 43 additions and 31 deletions

View File

@ -98,10 +98,14 @@ class Database private constructor(var client: TelegramClient) {
} }
fun getMessagesWithMedia(): LinkedList<TLMessage?> { fun getMessagesWithMediaCount() = queryInt("SELECT COUNT(*) FROM messages WHERE has_media=1")
fun getMessagesWithMedia(limit: Int = 0, offset: Int = 0): LinkedList<TLMessage?> {
try { try {
val list = LinkedList<TLMessage?>() val list = LinkedList<TLMessage?>()
val rs = stmt!!.executeQuery("SELECT data FROM messages WHERE has_media=1") var query = "SELECT data FROM messages WHERE has_media=1 ORDER BY id"
if (limit > 0) query += " LIMIT ${limit} OFFSET ${offset}"
val rs = stmt!!.executeQuery(query)
while (rs.next()) { while (rs.next()) {
list.add(bytesToTLMessage(rs.getBytes(1))) list.add(bytesToTLMessage(rs.getBytes(1)))
} }

View File

@ -310,9 +310,17 @@ class DownloadManager(internal var client: TelegramClient?, p: DownloadProgressI
downloadMessages(ids, null, source_type=MessageSource.NORMAL) downloadMessages(ids, null, source_type=MessageSource.NORMAL)
} }
val messages = this.db!!.getMessagesWithMedia() var offset = 0
val limit = 1000
val message_count = this.db!!.getMessagesWithMediaCount()
prog!!.onMediaDownloadStart(message_count)
while (true) {
logger.debug("Querying messages with media, limit={}, offset={}", limit, offset)
val messages = this.db!!.getMessagesWithMedia(limit=limit, offset=offset)
if (messages.size == 0) break
offset += limit
logger.debug("Database returned {} messages with media", messages.size) logger.debug("Database returned {} messages with media", messages.size)
prog!!.onMediaDownloadStart(messages.size)
for (msg in messages) { for (msg in messages) {
if (msg == null) continue if (msg == null) continue
val m = FileManagerFactory.getFileManager(msg, user!!, client!!) val m = FileManagerFactory.getFileManager(msg, user!!, client!!)
@ -340,7 +348,7 @@ class DownloadManager(internal var client: TelegramClient?, p: DownloadProgressI
// do nothing - skip this file // do nothing - skip this file
prog!!.onMediaSkipped() prog!!.onMediaSkipped()
} }
}
} }
} }
prog!!.onMediaDownloadFinished() prog!!.onMediaDownloadFinished()