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)