summaryrefslogtreecommitdiff
path: root/src/main/kotlin/io/dico/parcels2/storage/migration/plotme/PlotmeMigration.kt
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/kotlin/io/dico/parcels2/storage/migration/plotme/PlotmeMigration.kt')
-rw-r--r--src/main/kotlin/io/dico/parcels2/storage/migration/plotme/PlotmeMigration.kt115
1 files changed, 50 insertions, 65 deletions
diff --git a/src/main/kotlin/io/dico/parcels2/storage/migration/plotme/PlotmeMigration.kt b/src/main/kotlin/io/dico/parcels2/storage/migration/plotme/PlotmeMigration.kt
index 1f6e49c..f0c0cd8 100644
--- a/src/main/kotlin/io/dico/parcels2/storage/migration/plotme/PlotmeMigration.kt
+++ b/src/main/kotlin/io/dico/parcels2/storage/migration/plotme/PlotmeMigration.kt
@@ -6,45 +6,43 @@ import com.zaxxer.hikari.HikariDataSource
import io.dico.parcels2.*
import io.dico.parcels2.options.PlotmeMigrationOptions
import io.dico.parcels2.storage.Storage
+import io.dico.parcels2.storage.exposed.abs
+import io.dico.parcels2.storage.exposed.greater
import io.dico.parcels2.storage.migration.Migration
-import io.dico.parcels2.util.Vec2i
-import io.dico.parcels2.util.isValid
import io.dico.parcels2.util.toUUID
-import io.dico.parcels2.util.uuid
-import kotlinx.coroutines.experimental.*
-import org.bukkit.Bukkit
+import kotlinx.coroutines.experimental.Job
+import kotlinx.coroutines.experimental.launch
+import kotlinx.coroutines.experimental.newFixedThreadPoolContext
import org.jetbrains.exposed.sql.*
import org.slf4j.LoggerFactory
-import java.io.ByteArrayOutputStream
import java.sql.Blob
import java.util.UUID
-import java.util.concurrent.ConcurrentHashMap
import javax.sql.DataSource
-import kotlin.coroutines.experimental.coroutineContext
class PlotmeMigration(val options: PlotmeMigrationOptions) : Migration {
private var dataSource: DataSource? = null
private var database: Database? = null
private var isShutdown: Boolean = false
private val mlogger = LoggerFactory.getLogger("PlotMe Migrator")
+ val dispatcher = newFixedThreadPoolContext(1, "PlotMe Migration Thread")
private fun <T> transaction(statement: Transaction.() -> T) = org.jetbrains.exposed.sql.transactions.transaction(database!!, statement)
override fun migrateTo(storage: Storage): Job {
- return launch(context = storage.asyncDispatcher) {
+ return launch(dispatcher) {
init()
- transaction { launch(context = Unconfined, start = CoroutineStart.UNDISPATCHED) { doWork(storage) } }
+ doWork(storage)
shutdown()
}
}
- suspend fun init() {
- if (isShutdown) throw IllegalStateException()
+ fun init() {
+ if (isShutdown || database != null) throw IllegalStateException()
dataSource = options.storage.getDataSourceFactory()!!()
database = Database.connect(dataSource!!)
}
- suspend fun shutdown() {
+ fun shutdown() {
if (isShutdown) throw IllegalStateException()
dataSource?.let {
(it as? HikariDataSource)?.close()
@@ -53,74 +51,61 @@ class PlotmeMigration(val options: PlotmeMigrationOptions) : Migration {
isShutdown = true
}
- private val parcelsCache = hashMapOf<String, MutableMap<Vec2i, ParcelData>>()
-
- private fun getMap(worldName: String): MutableMap<Vec2i, ParcelData>? {
- val mapped = options.worldsFromTo[worldName] ?: return null
- return parcelsCache.computeIfAbsent(mapped) { mutableMapOf() }
- }
-
- private fun getData(worldName: String, position: Vec2i): ParcelData? {
- return getMap(worldName)?.computeIfAbsent(position) { ParcelDataHolder(addedMap = ConcurrentHashMap()) }
- }
-
- suspend fun doWork(target: Storage): Unit {
- if (!PlotmePlotsT.exists()) {
- mlogger.warn("Plotme tables don't appear to exist. Exiting.")
- return
+ suspend fun doWork(target: Storage) {
+ val exit = transaction {
+ (!PlotmePlotsT.exists()).also {
+ if (it) mlogger.warn("Plotme tables don't appear to exist. Exiting.")
+ }
}
+ if (exit) return
- parcelsCache.clear()
+ val worldCache = options.worldsFromTo.mapValues { ParcelWorldId(it.value) }
- iterPlotmeTable(PlotmePlotsT) { data, row ->
- // in practice, owner_uuid is not null for any plot currently. It will convert well.
- data.owner = ParcelOwner(row[owner_uuid]?.toUUID(), row[owner_name])
+ fun getParcelId(table: PlotmeTable, row: ResultRow): ParcelId? {
+ val world = worldCache[row[table.world_name]] ?: return null
+ return ParcelId(world, row[table.px], row[table.pz])
}
- launch(context = target.asyncDispatcher) {
- iterPlotmeTable(PlotmeAllowedT) { data, row ->
- val uuid = row[player_uuid]?.toUUID()
- ?: Bukkit.getOfflinePlayer(row[player_name]).takeIf { it.isValid }?.uuid
- ?: return@iterPlotmeTable
-
- data.setAddedStatus(uuid, AddedStatus.ALLOWED)
+ fun PlotmePlotPlayerMap.transmitPlotmeAddedTable(kind: AddedStatus) {
+ selectAll().forEach { row ->
+ val parcel = getParcelId(this, row) ?: return@forEach
+ val profile = StatusKey.safe(row[player_uuid]?.toUUID(), row[player_name]) ?: return@forEach
+ target.setParcelPlayerStatus(parcel, profile, kind)
}
}
- launch(context = target.asyncDispatcher) {
- iterPlotmeTable(PlotmeDeniedT) { data, row ->
- val uuid = row[player_uuid]?.toUUID()
- ?: Bukkit.getOfflinePlayer(row[player_name]).takeIf { it.isValid }?.uuid
- ?: return@iterPlotmeTable
-
- data.setAddedStatus(uuid, AddedStatus.BANNED)
- }
+ mlogger.info("Transmitting data from plotmeplots table")
+ transaction {
+ PlotmePlotsT.selectAll()
+ .orderBy(PlotmePlotsT.world_name)
+ .orderBy(with(SqlExpressionBuilder) { greater(PlotmePlotsT.px.abs(), PlotmePlotsT.pz.abs()) })
+ .forEach { row ->
+ val parcel = getParcelId(PlotmePlotsT, row) ?: return@forEach
+ val owner = PlayerProfile.safe(row[PlotmePlotsT.owner_uuid]?.toUUID(), row[PlotmePlotsT.owner_name])
+ target.setParcelOwner(parcel, owner)
+ }
}
- println(coroutineContext[Job]!!.children)
- coroutineContext[Job]!!.joinChildren()
+ mlogger.info("Transmitting data from plotmeallowed table")
+ transaction {
+ PlotmeAllowedT.transmitPlotmeAddedTable(AddedStatus.ALLOWED)
+ }
- for ((worldName, map) in parcelsCache) {
- val world = ParcelWorldId(worldName)
- for ((pos, data) in map) {
- val parcel = ParcelId(world, pos)
- target.setParcelData(parcel, data)
- }
+ mlogger.info("Transmitting data from plotmedenied table")
+ transaction {
+ PlotmeDeniedT.transmitPlotmeAddedTable(AddedStatus.BANNED)
}
+ mlogger.warn("Data has been **transmitted**.")
+ mlogger.warn("Loading parcel data might take a while as enqueued transactions from this migration are completed.")
}
- private fun Blob.toUUID(): UUID {
- val out = ByteArrayOutputStream(16)
- binaryStream.copyTo(out, bufferSize = 16)
- return out.toByteArray().toUUID()
+ private fun Blob.toUUID(): UUID? {
+ val ba = ByteArray(16)
+ val count = binaryStream.read(ba, 0, 16)
+ if (count < 16) return null
+ return ba.toUUID()
}
- private inline fun <T : PlotmeTable> iterPlotmeTable(table: T, block: T.(ParcelData, ResultRow) -> Unit) {
- table.selectAll().forEach { row ->
- val data = getData(row[table.world_name], Vec2i(row[table.px], row[table.pz])) ?: return@forEach
- table.block(data, row)
- }
- }
} \ No newline at end of file