mirror of
https://github.com/SchildiChat/SchildiChat-android.git
synced 2025-01-17 13:23:57 +03:00
Chunk key import to avoid ram allocation peak
This commit is contained in:
parent
0e44e32d2a
commit
1635c9730a
1 changed files with 22 additions and 11 deletions
|
@ -75,13 +75,9 @@ class CryptoLogger : Logger {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private class CryptoProgressListener(listener: ProgressListener?) : RustProgressListener {
|
private class CryptoProgressListener(private val listener: ProgressListener?) : RustProgressListener {
|
||||||
private val inner: ProgressListener? = listener
|
|
||||||
|
|
||||||
override fun onProgress(progress: Int, total: Int) {
|
override fun onProgress(progress: Int, total: Int) {
|
||||||
if (this.inner != null) {
|
listener?.onProgress(progress, total)
|
||||||
this.inner.onProgress(progress, total)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -524,14 +520,29 @@ internal class OlmMachine(
|
||||||
listener: ProgressListener?
|
listener: ProgressListener?
|
||||||
): ImportRoomKeysResult =
|
): ImportRoomKeysResult =
|
||||||
withContext(Dispatchers.IO) {
|
withContext(Dispatchers.IO) {
|
||||||
|
|
||||||
val adapter = MoshiProvider.providesMoshi().adapter(List::class.java)
|
val adapter = MoshiProvider.providesMoshi().adapter(List::class.java)
|
||||||
val encodedKeys = adapter.toJson(keys)
|
|
||||||
|
|
||||||
val rustListener = CryptoProgressListener(listener)
|
// If the key backup is too big we take the risk of causing OOM
|
||||||
|
// so let's chunk to avoid it
|
||||||
|
var totalImported = 0L
|
||||||
|
var accTotal = 0L
|
||||||
|
keys.chunked(500)
|
||||||
|
.forEach { keysSlice ->
|
||||||
|
val encodedKeys = adapter.toJson(keysSlice)
|
||||||
|
val rustListener = object : RustProgressListener {
|
||||||
|
override fun onProgress(progress: Int, total: Int) {
|
||||||
|
val accProgress = (accTotal + progress).toInt()
|
||||||
|
listener?.onProgress(accProgress, keys.size)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
val result = inner.importDecryptedKeys(encodedKeys, rustListener)
|
inner.importDecryptedKeys(encodedKeys, rustListener).let {
|
||||||
|
totalImported += it.imported
|
||||||
ImportRoomKeysResult(result.total.toInt(), result.imported.toInt())
|
accTotal += it.total
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ImportRoomKeysResult(totalImported.toInt(), accTotal.toInt())
|
||||||
}
|
}
|
||||||
|
|
||||||
@Throws(CryptoStoreException::class)
|
@Throws(CryptoStoreException::class)
|
||||||
|
|
Loading…
Add table
Reference in a new issue