This commit is contained in:
Benoit Marty 2021-03-18 10:57:28 +01:00
parent f6032da788
commit 96b37a8206
4 changed files with 16 additions and 16 deletions

View file

@ -29,7 +29,7 @@ import org.matrix.android.sdk.internal.crypto.model.rest.RestKeyInfo
import org.matrix.android.sdk.internal.network.GlobalErrorReceiver
import org.matrix.android.sdk.internal.network.executeRequest
import org.matrix.android.sdk.internal.task.Task
import org.matrix.android.sdk.internal.util.getBetsChunkSize
import org.matrix.android.sdk.internal.util.computeBestChunkSize
import javax.inject.Inject
internal interface DownloadKeysForUsersTask : Task<DownloadKeysForUsersTask.Params, KeysQueryResponse> {
@ -47,7 +47,7 @@ internal class DefaultDownloadKeysForUsers @Inject constructor(
) : DownloadKeysForUsersTask {
override suspend fun execute(params: DownloadKeysForUsersTask.Params): KeysQueryResponse {
val bestChunkSize = getBetsChunkSize(params.userIds.size, LIMIT)
val bestChunkSize = computeBestChunkSize(params.userIds.size, LIMIT)
// Store server results in these mutable maps
val deviceKeys = mutableMapOf<String, Map<String, DeviceKeysWithUnsigned>>()

View file

@ -64,7 +64,7 @@ import org.matrix.android.sdk.internal.session.sync.model.LazyRoomSyncEphemeral
import org.matrix.android.sdk.internal.session.sync.model.RoomSync
import org.matrix.android.sdk.internal.session.sync.model.RoomSyncAccountData
import org.matrix.android.sdk.internal.session.sync.model.RoomsSyncResponse
import org.matrix.android.sdk.internal.util.getBetsChunkSize
import org.matrix.android.sdk.internal.util.computeBestChunkSize
import timber.log.Timber
import javax.inject.Inject
@ -140,12 +140,12 @@ internal class RoomSyncHandler @Inject constructor(private val readReceiptHandle
syncLocalTimeStampMillis: Long,
aggregator: SyncResponsePostTreatmentAggregator,
reporter: ProgressReporter?) {
val bestChunkSize = getBetsChunkSize(
val bestChunkSize = computeBestChunkSize(
listSize = handlingStrategy.data.keys.size,
limit = (initialSyncStrategy as? InitialSyncStrategy.Optimized)?.maxRoomsToInsert ?: Int.MAX_VALUE
)
if (bestChunkSize.shouldSplit()) {
if (bestChunkSize.shouldChunk()) {
reportSubtask(reporter, InitSyncStep.ImportingAccountJoinedRooms, bestChunkSize.numberOfChunks, 0.6f) {
Timber.d("INIT_SYNC ${handlingStrategy.data.keys.size} rooms to insert, split with $bestChunkSize")
// I cannot find a better way to chunk a map, so chunk the keys and then create new maps

View file

@ -22,10 +22,10 @@ internal data class BestChunkSize(
val numberOfChunks: Int,
val chunkSize: Int
) {
fun shouldSplit() = numberOfChunks > 1
fun shouldChunk() = numberOfChunks > 1
}
internal fun getBetsChunkSize(listSize: Int, limit: Int): BestChunkSize {
internal fun computeBestChunkSize(listSize: Int, limit: Int): BestChunkSize {
return if (listSize <= limit) {
BestChunkSize(
numberOfChunks = 1,

View file

@ -27,35 +27,35 @@ import org.matrix.android.sdk.MatrixTest
class MathUtilTest : MatrixTest {
@Test
fun testGetBestChunkSize0() = doTest(0, 100, 1, 0)
fun testComputeBestChunkSize0() = doTest(0, 100, 1, 0)
@Test
fun testGetBestChunkSize1to99() {
fun testComputeBestChunkSize1to99() {
for (i in 1..99) {
doTest(i, 100, 1, i)
}
}
@Test
fun testGetBestChunkSize100() = doTest(100, 100, 1, 100)
fun testComputeBestChunkSize100() = doTest(100, 100, 1, 100)
@Test
fun testGetBestChunkSize101() = doTest(101, 100, 2, 51)
fun testComputeBestChunkSize101() = doTest(101, 100, 2, 51)
@Test
fun testGetBestChunkSize199() = doTest(199, 100, 2, 100)
fun testComputeBestChunkSize199() = doTest(199, 100, 2, 100)
@Test
fun testGetBestChunkSize200() = doTest(200, 100, 2, 100)
fun testComputeBestChunkSize200() = doTest(200, 100, 2, 100)
@Test
fun testGetBestChunkSize201() = doTest(201, 100, 3, 67)
fun testComputeBestChunkSize201() = doTest(201, 100, 3, 67)
@Test
fun testGetBestChunkSize240() = doTest(240, 100, 3, 80)
fun testComputeBestChunkSize240() = doTest(240, 100, 3, 80)
private fun doTest(listSize: Int, limit: Int, expectedNumberOfChunks: Int, expectedChunkSize: Int) {
val result = getBetsChunkSize(listSize, limit)
val result = computeBestChunkSize(listSize, limit)
result.numberOfChunks shouldBeEqualTo expectedNumberOfChunks
result.chunkSize shouldBeEqualTo expectedChunkSize