Use zstd to compress client context and system world data

This commit is contained in:
DBotThePony 2024-12-09 14:35:00 +07:00
parent 11e4efb2af
commit aeca7836cd
Signed by: DBot
GPG Key ID: DCC23B5715498507

View File

@ -25,10 +25,14 @@ import ru.dbotthepony.kstarbound.defs.world.WorldTemplate
import ru.dbotthepony.kstarbound.fromJson import ru.dbotthepony.kstarbound.fromJson
import ru.dbotthepony.kstarbound.json.readJsonElement import ru.dbotthepony.kstarbound.json.readJsonElement
import ru.dbotthepony.kstarbound.json.readJsonElementInflated import ru.dbotthepony.kstarbound.json.readJsonElementInflated
import ru.dbotthepony.kstarbound.json.readJsonElementZstd
import ru.dbotthepony.kstarbound.json.readJsonObject import ru.dbotthepony.kstarbound.json.readJsonObject
import ru.dbotthepony.kstarbound.json.readJsonObjectZstd
import ru.dbotthepony.kstarbound.json.writeJsonElement import ru.dbotthepony.kstarbound.json.writeJsonElement
import ru.dbotthepony.kstarbound.json.writeJsonElementDeflated import ru.dbotthepony.kstarbound.json.writeJsonElementDeflated
import ru.dbotthepony.kstarbound.json.writeJsonElementZstd
import ru.dbotthepony.kstarbound.json.writeJsonObject import ru.dbotthepony.kstarbound.json.writeJsonObject
import ru.dbotthepony.kstarbound.json.writeJsonObjectZstd
import ru.dbotthepony.kstarbound.math.vector.Vector2i import ru.dbotthepony.kstarbound.math.vector.Vector2i
import ru.dbotthepony.kstarbound.server.world.LegacyWorldStorage import ru.dbotthepony.kstarbound.server.world.LegacyWorldStorage
import ru.dbotthepony.kstarbound.server.world.NativeLocalWorldStorage import ru.dbotthepony.kstarbound.server.world.NativeLocalWorldStorage
@ -159,7 +163,7 @@ sealed class StarboundServer(val root: File) : BlockableEventLoop("Server thread
lookupClientContext.executeQuery().use { lookupClientContext.executeQuery().use {
if (it.next()) { if (it.next()) {
it.getBytes(1).readJsonObject() it.getBytes(1).readJsonObjectZstd()
} else { } else {
null null
} }
@ -168,9 +172,10 @@ sealed class StarboundServer(val root: File) : BlockableEventLoop("Server thread
} }
fun writeClientContext(uuid: UUID, context: JsonObject): CompletableFuture<*> { fun writeClientContext(uuid: UUID, context: JsonObject): CompletableFuture<*> {
val compressed = context.writeJsonObjectZstd()
return supplyAsync { return supplyAsync {
writeClientContext.setString(1, uuid.toStarboundString()) writeClientContext.setString(1, uuid.toStarboundString())
writeClientContext.setBytes(2, context.writeJsonObject()) writeClientContext.setBytes(2, compressed)
writeClientContext.execute() writeClientContext.execute()
} }
} }
@ -241,7 +246,7 @@ sealed class StarboundServer(val root: File) : BlockableEventLoop("Server thread
lookupSystemWorld.executeQuery().use { lookupSystemWorld.executeQuery().use {
if (it.next()) { if (it.next()) {
it.getBytes(1).readJsonElementInflated() it.getBytes(1).readJsonElementZstd()
} else { } else {
null null
} }
@ -250,11 +255,13 @@ sealed class StarboundServer(val root: File) : BlockableEventLoop("Server thread
} }
fun writeServerWorldData(pos: Vector3i, data: JsonElement) { fun writeServerWorldData(pos: Vector3i, data: JsonElement) {
val compressed = data.writeJsonElementZstd()
execute { execute {
writeSystemWorld.setInt(1, pos.x) writeSystemWorld.setInt(1, pos.x)
writeSystemWorld.setInt(2, pos.y) writeSystemWorld.setInt(2, pos.y)
writeSystemWorld.setInt(3, pos.z) writeSystemWorld.setInt(3, pos.z)
writeSystemWorld.setBytes(4, data.writeJsonElementDeflated()) writeSystemWorld.setBytes(4, compressed)
writeSystemWorld.execute() writeSystemWorld.execute()
} }