From 65e3e090bb006dea937fe519e94db6944a9d6821 Mon Sep 17 00:00:00 2001 From: trivernis Date: Tue, 11 Aug 2020 09:54:25 +0200 Subject: [PATCH] Fix insertion of pending chunks using too many parameters - Fixes #91 --- .../chunkmaster/lib/database/PendingChunks.kt | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/net/trivernis/chunkmaster/lib/database/PendingChunks.kt b/src/main/kotlin/net/trivernis/chunkmaster/lib/database/PendingChunks.kt index 9b79dfd..9c61671 100644 --- a/src/main/kotlin/net/trivernis/chunkmaster/lib/database/PendingChunks.kt +++ b/src/main/kotlin/net/trivernis/chunkmaster/lib/database/PendingChunks.kt @@ -2,6 +2,7 @@ package net.trivernis.chunkmaster.lib.database import net.trivernis.chunkmaster.lib.generation.ChunkCoordinates import java.util.concurrent.CompletableFuture +import kotlin.math.ceil class PendingChunks(private val sqliteManager: SqliteManager) { /** @@ -30,10 +31,25 @@ class PendingChunks(private val sqliteManager: SqliteManager) { return completableFuture } + fun addPendingChunks(taskId: Int, pendingChunks: List): CompletableFuture { + val futures = ArrayList>() + val statementCount = ceil(pendingChunks.size.toDouble() / 100.0).toInt() + + for (i in 0 until statementCount) { + futures.add(insertPendingChunks(taskId, pendingChunks.subList(i * 100, ((i * 100) + 100).coerceAtMost(pendingChunks.size)))) + } + + if (futures.size > 0) { + return CompletableFuture.allOf(*futures.toTypedArray()) + } else { + return CompletableFuture.supplyAsync { null } + } + } + /** * Adds pending chunks for a taskid */ - fun addPendingChunks(taskId: Int, pendingChunks: List): CompletableFuture { + private fun insertPendingChunks(taskId: Int, pendingChunks: List): CompletableFuture { val completableFuture = CompletableFuture() if (pendingChunks.isEmpty()) { completableFuture.complete(null)