From 17c9664ee9582c1ded0b6433b5a3dee8ae9482fe Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Mon, 29 Apr 2024 18:21:09 +0700 Subject: [PATCH] Don't write partial changes to universe chunks database --- .../kstarbound/server/StarboundServer.kt | 1 - .../kstarbound/server/world/ServerUniverse.kt | 22 +++++++------------ 2 files changed, 8 insertions(+), 15 deletions(-) diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/server/StarboundServer.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/server/StarboundServer.kt index e5eb86ae..70f1255c 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/server/StarboundServer.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/server/StarboundServer.kt @@ -341,7 +341,6 @@ sealed class StarboundServer(val root: File) : BlockableEventLoop("Server thread scheduleWithFixedDelay(Runnable { setMetadata("universe_clock", JsonPrimitive(universeClock.time)) database.commit() - universe.flush() }, declareInterval, declareInterval, TimeUnit.NANOSECONDS) scheduleAtFixedRate(Runnable { diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/server/world/ServerUniverse.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/server/world/ServerUniverse.kt index e1ea75c6..944e8d5a 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/server/world/ServerUniverse.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/server/world/ServerUniverse.kt @@ -505,7 +505,6 @@ class ServerUniverse(folder: File? = null) : Universe(), Closeable { carrier.execute { legacyDatabase?.close() - database.commit() databaseCleanable.clean() } @@ -520,12 +519,6 @@ class ServerUniverse(folder: File? = null) : Universe(), Closeable { } } - fun flush() { - if (!isMemory) { - database.commit() - } - } - private fun generateChunk(chunkPos: Vector2i): CompletableFuture { val random = random(staticRandom64(chunkPos.x, chunkPos.y, "ChunkIndexMix")) val region = chunkRegion(chunkPos) @@ -533,7 +526,7 @@ class ServerUniverse(folder: File? = null) : Universe(), Closeable { return CompletableFuture.supplyAsync(Supplier { val constellationCandidates = ArrayList() val systemPositions = ArrayList() - val systems = ArrayList>() + val systems = ArrayList>() for (x in region.mins.x until region.maxs.x) { for (y in region.mins.y until region.maxs.y) { @@ -543,10 +536,7 @@ class ServerUniverse(folder: File? = null) : Universe(), Closeable { val system = generateSystem(random, pos) ?: continue systemPositions.add(pos) - - systems.add(CompletableFuture.supplyAsync(Supplier { system.serialize() }, Starbound.EXECUTOR) - .thenAcceptAsync(Consumer { it.write(insertSystem) }, carrier)) - + systems.add(CompletableFuture.supplyAsync(Supplier { system.serialize() }, Starbound.EXECUTOR)) systemCache.put(Vector3i(system.x, system.y, system.z), CompletableFuture.completedFuture(system)) if ( @@ -562,8 +552,12 @@ class ServerUniverse(folder: File? = null) : Universe(), Closeable { val chunk = Chunk(chunkPos.x, chunkPos.y, ObjectOpenHashSet(systemPositions), ObjectOpenHashSet(generateConstellations(random, constellationCandidates))) val serialized = chunk.serialize() - CompletableFuture.allOf(*systems.toTypedArray()) - .thenApplyAsync(Function { serialized.write(insertChunk); database.commit(); chunk }, carrier) + CompletableFuture.allOf(*systems.toTypedArray()).thenApplyAsync(Function { + serialized.write(insertChunk) + systems.forEach { it.get().write(insertSystem) } + database.commit() + chunk + }, carrier) }, Starbound.EXECUTOR).thenCompose { it } }