From 3ac24f6912db22138e3b156eda6c48fbc2b9447f Mon Sep 17 00:00:00 2001 From: Kenneth Endfinger Date: Mon, 10 Jan 2022 02:13:13 -0500 Subject: [PATCH] Gjallarhorn: Implement trimming at the changelog level, resulting in really fast renderings. --- .../commands/BlockChangeTimelapseCommand.kt | 20 +++++++++++++++++-- .../gjallarhorn/state/BlockLogTracker.kt | 13 +----------- .../gjallarhorn/state/BlockMapRenderPool.kt | 2 -- .../gjallarhorn/state/BlockMapTimelapse.kt | 12 +++++------ 4 files changed, 24 insertions(+), 23 deletions(-) diff --git a/tool-gjallarhorn/src/main/kotlin/cloud/kubelet/foundation/gjallarhorn/commands/BlockChangeTimelapseCommand.kt b/tool-gjallarhorn/src/main/kotlin/cloud/kubelet/foundation/gjallarhorn/commands/BlockChangeTimelapseCommand.kt index 40ad637..5c6c8cc 100644 --- a/tool-gjallarhorn/src/main/kotlin/cloud/kubelet/foundation/gjallarhorn/commands/BlockChangeTimelapseCommand.kt +++ b/tool-gjallarhorn/src/main/kotlin/cloud/kubelet/foundation/gjallarhorn/commands/BlockChangeTimelapseCommand.kt @@ -4,7 +4,9 @@ import cloud.kubelet.foundation.gjallarhorn.render.BlockDiversityRenderer import cloud.kubelet.foundation.gjallarhorn.render.BlockHeightMapRenderer import cloud.kubelet.foundation.gjallarhorn.render.BlockImageRenderer import cloud.kubelet.foundation.gjallarhorn.state.* +import cloud.kubelet.foundation.gjallarhorn.util.compose import cloud.kubelet.foundation.gjallarhorn.util.savePngFile +import cloud.kubelet.foundation.heimdall.view.BlockChangeView import com.github.ajalt.clikt.core.CliktCommand import com.github.ajalt.clikt.core.requireObject import com.github.ajalt.clikt.parameters.options.flag @@ -13,6 +15,9 @@ import com.github.ajalt.clikt.parameters.options.required import com.github.ajalt.clikt.parameters.types.enum import com.github.ajalt.clikt.parameters.types.int import org.jetbrains.exposed.sql.Database +import org.jetbrains.exposed.sql.SqlExpressionBuilder.greaterEq +import org.jetbrains.exposed.sql.SqlExpressionBuilder.lessEq +import org.jetbrains.exposed.sql.and import org.slf4j.LoggerFactory import java.awt.Color import java.awt.Font @@ -45,8 +50,19 @@ class BlockChangeTimelapseCommand : CliktCommand("Block Change Timelapse", name override fun run() { val threadPoolExecutor = ScheduledThreadPoolExecutor(16) - val changelog = BlockChangelog.query(db) - val timelapse = BlockMapTimelapse(maybeBuildTrim()) + + val trim = maybeBuildTrim() + val filter = compose( + combine = { a, b -> a and b }, + { trim?.first?.x != null } to { BlockChangeView.x greaterEq trim!!.first.x }, + { trim?.first?.z != null } to { BlockChangeView.z greaterEq trim!!.first.z }, + { trim?.second?.x != null } to { BlockChangeView.x lessEq trim!!.second.x }, + { trim?.second?.z != null } to { BlockChangeView.z lessEq trim!!.second.z } + ) + + val changelog = BlockChangelog.query(db, filter) + logger.info("Block Changelog: ${changelog.changes.size} changes") + val timelapse = BlockMapTimelapse() var slices = timelapse.calculateChangelogSlices(changelog, timelapseMode.interval, timelapseIntervalLimit) if (timelapseSpeedChangeThreshold != null && timelapseSpeedChangeMinimumIntervalSeconds != null) { diff --git a/tool-gjallarhorn/src/main/kotlin/cloud/kubelet/foundation/gjallarhorn/state/BlockLogTracker.kt b/tool-gjallarhorn/src/main/kotlin/cloud/kubelet/foundation/gjallarhorn/state/BlockLogTracker.kt index 5570d15..3475cb0 100644 --- a/tool-gjallarhorn/src/main/kotlin/cloud/kubelet/foundation/gjallarhorn/state/BlockLogTracker.kt +++ b/tool-gjallarhorn/src/main/kotlin/cloud/kubelet/foundation/gjallarhorn/state/BlockLogTracker.kt @@ -17,17 +17,6 @@ class BlockLogTracker(private val mode: BlockTrackMode = BlockTrackMode.RemoveOn } } - fun trimOutsideXAndZRange(min: BlockCoordinate, max: BlockCoordinate) { - val blockPositionsToRemove = blocks.keys.filter { - it.x < min.x || - it.z < min.z || - it.x > max.x || - it.z > max.z - }.toList() - - blockPositionsToRemove.forEach { blocks.remove(it) } - } - fun calculateZeroBlockOffset(): BlockCoordinate { val x = blocks.keys.minOf { it.x } val y = blocks.keys.minOf { it.y } @@ -48,7 +37,7 @@ class BlockLogTracker(private val mode: BlockTrackMode = BlockTrackMode.RemoveOn } fun isEmpty() = blocks.isEmpty() - fun isNotEmpty() = blocks.isNotEmpty() + fun isNotEmpty() = !isEmpty() fun buildBlockMap(offset: BlockCoordinate = BlockCoordinate.zero): BlockMap { val map = BlockMap() diff --git a/tool-gjallarhorn/src/main/kotlin/cloud/kubelet/foundation/gjallarhorn/state/BlockMapRenderPool.kt b/tool-gjallarhorn/src/main/kotlin/cloud/kubelet/foundation/gjallarhorn/state/BlockMapRenderPool.kt index f6867e8..532db10 100644 --- a/tool-gjallarhorn/src/main/kotlin/cloud/kubelet/foundation/gjallarhorn/state/BlockMapRenderPool.kt +++ b/tool-gjallarhorn/src/main/kotlin/cloud/kubelet/foundation/gjallarhorn/state/BlockMapRenderPool.kt @@ -60,7 +60,6 @@ class BlockMapRenderPool( val sliced = changelog.slice(slice) val tracker = BlockLogTracker(blockTrackMode) tracker.replay(sliced) - delegate.postProcessTracker(tracker) if (tracker.isNotEmpty()) { trackers[slice] = tracker } @@ -70,7 +69,6 @@ class BlockMapRenderPool( } interface RenderPoolDelegate { - fun postProcessTracker(tracker: BlockLogTracker) fun buildRenderJobs(pool: BlockMapRenderPool, trackers: MutableMap) } diff --git a/tool-gjallarhorn/src/main/kotlin/cloud/kubelet/foundation/gjallarhorn/state/BlockMapTimelapse.kt b/tool-gjallarhorn/src/main/kotlin/cloud/kubelet/foundation/gjallarhorn/state/BlockMapTimelapse.kt index 5e1bbd5..7a7f8aa 100644 --- a/tool-gjallarhorn/src/main/kotlin/cloud/kubelet/foundation/gjallarhorn/state/BlockMapTimelapse.kt +++ b/tool-gjallarhorn/src/main/kotlin/cloud/kubelet/foundation/gjallarhorn/state/BlockMapTimelapse.kt @@ -4,7 +4,7 @@ import java.time.Duration import java.time.Instant import java.util.stream.Stream -class BlockMapTimelapse(val trim: Pair? = null) : +class BlockMapTimelapse : BlockMapRenderPool.RenderPoolDelegate { fun calculateChangelogSlices( changelog: BlockChangelog, interval: Duration, limit: Int? = null @@ -46,6 +46,10 @@ class BlockMapTimelapse(val trim: Pair? = n pool: BlockMapRenderPool, trackers: MutableMap ) { + if (trackers.isEmpty()) { + return + } + val allBlockOffsets = trackers.map { it.value.calculateZeroBlockOffset() } val globalBlockOffset = BlockCoordinate.maxOf(allBlockOffsets) val allBlockMaxes = trackers.map { it.value.calculateMaxBlock() } @@ -60,10 +64,4 @@ class BlockMapTimelapse(val trim: Pair? = n } } } - - override fun postProcessTracker(tracker: BlockLogTracker) { - if (trim != null) { - tracker.trimOutsideXAndZRange(trim.first, trim.second) - } - } }