summaryrefslogtreecommitdiff
path: root/src/main/kotlin/io/dico/parcels2/storage
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/kotlin/io/dico/parcels2/storage')
-rw-r--r--src/main/kotlin/io/dico/parcels2/storage/Backing.kt4
-rw-r--r--src/main/kotlin/io/dico/parcels2/storage/Storage.kt12
-rw-r--r--src/main/kotlin/io/dico/parcels2/storage/exposed/ExposedBacking.kt7
-rw-r--r--src/main/kotlin/io/dico/parcels2/storage/exposed/ExposedExtensions.kt14
-rw-r--r--src/main/kotlin/io/dico/parcels2/storage/migration/plotme/PlotmeMigration.kt28
-rw-r--r--src/main/kotlin/io/dico/parcels2/storage/migration/plotme/PlotmeTables.kt41
6 files changed, 59 insertions, 47 deletions
diff --git a/src/main/kotlin/io/dico/parcels2/storage/Backing.kt b/src/main/kotlin/io/dico/parcels2/storage/Backing.kt
index 2e0a8ee..12d5bc7 100644
--- a/src/main/kotlin/io/dico/parcels2/storage/Backing.kt
+++ b/src/main/kotlin/io/dico/parcels2/storage/Backing.kt
@@ -43,14 +43,14 @@ interface Backing {
fun transmitAllParcelData(channel: SendChannel<DataPair>)
- fun readParcelData(parcel: ParcelId): ParcelData?
+ fun readParcelData(parcel: ParcelId): ParcelDataHolder?
fun getOwnedParcels(user: PlayerProfile): List<ParcelId>
fun getNumParcels(user: PlayerProfile): Int = getOwnedParcels(user).size
- fun setParcelData(parcel: ParcelId, data: ParcelData?)
+ fun setParcelData(parcel: ParcelId, data: ParcelDataHolder?)
fun setParcelOwner(parcel: ParcelId, owner: PlayerProfile?)
diff --git a/src/main/kotlin/io/dico/parcels2/storage/Storage.kt b/src/main/kotlin/io/dico/parcels2/storage/Storage.kt
index abd9f41..d718f20 100644
--- a/src/main/kotlin/io/dico/parcels2/storage/Storage.kt
+++ b/src/main/kotlin/io/dico/parcels2/storage/Storage.kt
@@ -13,7 +13,7 @@ import org.joda.time.DateTime
import java.util.UUID
import kotlin.coroutines.CoroutineContext
-typealias DataPair = Pair<ParcelId, ParcelData?>
+typealias DataPair = Pair<ParcelId, ParcelDataHolder?>
typealias PrivilegePair<TAttach> = Pair<TAttach, PrivilegesHolder>
interface Storage {
@@ -33,7 +33,7 @@ interface Storage {
fun updatePlayerName(uuid: UUID, name: String): Job
- fun readParcelData(parcel: ParcelId): Deferred<ParcelData?>
+ fun readParcelData(parcel: ParcelId): Deferred<ParcelDataHolder?>
fun transmitParcelData(parcels: Sequence<ParcelId>): ReceiveChannel<DataPair>
@@ -44,7 +44,7 @@ interface Storage {
fun getNumParcels(user: PlayerProfile): Deferred<Int>
- fun setParcelData(parcel: ParcelId, data: ParcelData?): Job
+ fun setParcelData(parcel: ParcelId, data: ParcelDataHolder?): Job
fun setParcelOwner(parcel: ParcelId, owner: PlayerProfile?): Job
@@ -62,7 +62,7 @@ interface Storage {
fun setGlobalPrivilege(owner: PlayerProfile, player: PlayerProfile, privilege: Privilege): Job
- fun getChannelToUpdateParcelData(): SendChannel<Pair<ParcelId, ParcelData>>
+ fun getChannelToUpdateParcelData(): SendChannel<Pair<ParcelId, ParcelDataHolder>>
}
class BackedStorage internal constructor(val b: Backing) : Storage, CoroutineScope {
@@ -93,7 +93,7 @@ class BackedStorage internal constructor(val b: Backing) : Storage, CoroutineSco
override fun getNumParcels(user: PlayerProfile) = b.launchFuture { b.getNumParcels(user) }
- override fun setParcelData(parcel: ParcelId, data: ParcelData?) = b.launchJob { b.setParcelData(parcel, data) }
+ override fun setParcelData(parcel: ParcelId, data: ParcelDataHolder?) = b.launchJob { b.setParcelData(parcel, data) }
override fun setParcelOwner(parcel: ParcelId, owner: PlayerProfile?) = b.launchJob { b.setParcelOwner(parcel, owner) }
@@ -110,5 +110,5 @@ class BackedStorage internal constructor(val b: Backing) : Storage, CoroutineSco
override fun setGlobalPrivilege(owner: PlayerProfile, player: PlayerProfile, privilege: Privilege) = b.launchJob { b.setGlobalPrivilege(owner, player, privilege) }
- override fun getChannelToUpdateParcelData(): SendChannel<Pair<ParcelId, ParcelData>> = b.openChannelForWriting { b.setParcelData(it.first, it.second) }
+ override fun getChannelToUpdateParcelData(): SendChannel<Pair<ParcelId, ParcelDataHolder>> = b.openChannelForWriting { b.setParcelData(it.first, it.second) }
}
diff --git a/src/main/kotlin/io/dico/parcels2/storage/exposed/ExposedBacking.kt b/src/main/kotlin/io/dico/parcels2/storage/exposed/ExposedBacking.kt
index fa68338..e49be79 100644
--- a/src/main/kotlin/io/dico/parcels2/storage/exposed/ExposedBacking.kt
+++ b/src/main/kotlin/io/dico/parcels2/storage/exposed/ExposedBacking.kt
@@ -9,6 +9,7 @@ import io.dico.parcels2.storage.*
import io.dico.parcels2.util.math.clampMax
import io.dico.parcels2.util.ext.synchronized
import kotlinx.coroutines.*
+import kotlinx.coroutines.Job
import kotlinx.coroutines.channels.ArrayChannel
import kotlinx.coroutines.channels.LinkedListChannel
import kotlinx.coroutines.channels.ReceiveChannel
@@ -152,7 +153,7 @@ class ExposedBacking(private val dataSourceFactory: () -> DataSource, val poolSi
channel.close()
}
- override fun readParcelData(parcel: ParcelId): ParcelData? {
+ override fun readParcelData(parcel: ParcelId): ParcelDataHolder? {
val row = ParcelsT.getRow(parcel) ?: return null
return rowToParcelData(row)
}
@@ -165,7 +166,7 @@ class ExposedBacking(private val dataSourceFactory: () -> DataSource, val poolSi
.toList()
}
- override fun setParcelData(parcel: ParcelId, data: ParcelData?) {
+ override fun setParcelData(parcel: ParcelId, data: ParcelDataHolder?) {
if (data == null) {
transaction {
ParcelsT.getId(parcel)?.let { id ->
@@ -262,7 +263,7 @@ class ExposedBacking(private val dataSourceFactory: () -> DataSource, val poolSi
private fun rowToParcelData(row: ResultRow) = ParcelDataHolder().apply {
owner = row[ParcelsT.owner_id]?.let { ProfilesT.getItem(it) }
lastClaimTime = row[ParcelsT.claim_time]
- ownerSignOutdated = row[ParcelsT.sign_oudated]
+ isOwnerSignOutdated = row[ParcelsT.sign_oudated]
val id = row[ParcelsT.id]
ParcelOptionsT.select { ParcelOptionsT.parcel_id eq id }.firstOrNull()?.let { optrow ->
diff --git a/src/main/kotlin/io/dico/parcels2/storage/exposed/ExposedExtensions.kt b/src/main/kotlin/io/dico/parcels2/storage/exposed/ExposedExtensions.kt
index 6ebac6d..0245625 100644
--- a/src/main/kotlin/io/dico/parcels2/storage/exposed/ExposedExtensions.kt
+++ b/src/main/kotlin/io/dico/parcels2/storage/exposed/ExposedExtensions.kt
@@ -5,8 +5,7 @@ import org.jetbrains.exposed.sql.Function
import org.jetbrains.exposed.sql.statements.InsertStatement
import org.jetbrains.exposed.sql.transactions.TransactionManager
-class UpsertStatement<Key : Any>(table: Table, conflictColumn: Column<*>? = null, conflictIndex: Index? = null)
- : InsertStatement<Key>(table, false) {
+class UpsertStatement<Key : Any>(table: Table, conflictColumn: Column<*>? = null, conflictIndex: Index? = null) : InsertStatement<Key>(table, false) {
val indexName: String
val indexColumns: List<Column<*>>
@@ -66,9 +65,10 @@ class Abs<T : Int?>(val expr: Expression<T>) : Function<T>(IntegerColumnType())
override fun toSQL(queryBuilder: QueryBuilder): String = "ABS(${expr.toSQL(queryBuilder)})"
}
-fun <T : Comparable<T>> SqlExpressionBuilder.greater(col1: ExpressionWithColumnType<T>, col2: ExpressionWithColumnType<T>): Expression<T> {
- return case(col1)
- .When(col1.greater(col2), col1)
- .Else(col2)
-}
+fun <T : Comparable<T>> greaterOf(col1: ExpressionWithColumnType<T>, col2: ExpressionWithColumnType<T>): Expression<T> =
+ with(SqlExpressionBuilder) {
+ case(col1)
+ .When(col1.greater(col2), col1)
+ .Else(col2)
+ }
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 a79d315..831fe42 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
@@ -7,8 +7,10 @@ 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.exposed.greaterOf
import io.dico.parcels2.storage.migration.Migration
+import io.dico.parcels2.storage.migration.plotme.PlotmeTables.PlotmePlotPlayerMap
+import io.dico.parcels2.storage.migration.plotme.PlotmeTables.PlotmeTable
import io.dico.parcels2.storage.toUUID
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
@@ -24,6 +26,7 @@ class PlotmeMigration(val options: PlotmeMigrationOptions) : Migration {
private var database: Database? = null
private var isShutdown: Boolean = false
private val mlogger = LoggerFactory.getLogger("PlotMe Migrator")
+ private val tables = PlotmeTables(options.tableNamesUppercase)
val dispatcher = newFixedThreadPoolContext(1, "PlotMe Migration Thread")
private fun <T> transaction(statement: Transaction.() -> T) = org.jetbrains.exposed.sql.transactions.transaction(database!!, statement)
@@ -51,9 +54,9 @@ class PlotmeMigration(val options: PlotmeMigrationOptions) : Migration {
isShutdown = true
}
- suspend fun doWork(target: Storage) {
+ suspend fun doWork(target: Storage) = with (tables) {
val exit = transaction {
- (!PlotmePlotsT.exists()).also {
+ (!PlotmePlots.exists()).also {
if (it) mlogger.warn("Plotme tables don't appear to exist. Exiting.")
}
}
@@ -75,29 +78,32 @@ class PlotmeMigration(val options: PlotmeMigrationOptions) : Migration {
}
mlogger.info("Transmitting data from plotmeplots table")
+ var count = 0
transaction {
- PlotmePlotsT.selectAll()
- .orderBy(PlotmePlotsT.world_name)
- .orderBy(with(SqlExpressionBuilder) { greater(PlotmePlotsT.px.abs(), PlotmePlotsT.pz.abs()) })
+
+ PlotmePlots.selectAll()
+ .orderBy(PlotmePlots.world_name)
+ .orderBy(greaterOf(PlotmePlots.px.abs(), PlotmePlots.pz.abs()))
.forEach { row ->
- val parcel = getParcelId(PlotmePlotsT, row) ?: return@forEach
- val owner = PlayerProfile.safe(row[PlotmePlotsT.owner_uuid]?.toUUID(), row[PlotmePlotsT.owner_name])
+ val parcel = getParcelId(PlotmePlots, row) ?: return@forEach
+ val owner = PlayerProfile.safe(row[PlotmePlots.owner_uuid]?.toUUID(), row[PlotmePlots.owner_name])
target.setParcelOwner(parcel, owner)
target.setParcelOwnerSignOutdated(parcel, true)
+ ++count
}
}
mlogger.info("Transmitting data from plotmeallowed table")
transaction {
- PlotmeAllowedT.transmitPlotmeAddedTable(Privilege.CAN_BUILD)
+ PlotmeAllowed.transmitPlotmeAddedTable(Privilege.CAN_BUILD)
}
mlogger.info("Transmitting data from plotmedenied table")
transaction {
- PlotmeDeniedT.transmitPlotmeAddedTable(Privilege.BANNED)
+ PlotmeDenied.transmitPlotmeAddedTable(Privilege.BANNED)
}
- mlogger.warn("Data has been **transmitted**.")
+ mlogger.warn("Data has been **transmitted**. $count plots were migrated to the parcels database.")
mlogger.warn("Loading parcel data might take a while as enqueued transactions from this migration are completed.")
}
diff --git a/src/main/kotlin/io/dico/parcels2/storage/migration/plotme/PlotmeTables.kt b/src/main/kotlin/io/dico/parcels2/storage/migration/plotme/PlotmeTables.kt
index 8564ad3..dc788c8 100644
--- a/src/main/kotlin/io/dico/parcels2/storage/migration/plotme/PlotmeTables.kt
+++ b/src/main/kotlin/io/dico/parcels2/storage/migration/plotme/PlotmeTables.kt
@@ -2,25 +2,30 @@ package io.dico.parcels2.storage.migration.plotme
import org.jetbrains.exposed.sql.Table
-const val uppercase: Boolean = false
-@Suppress("ConstantConditionIf")
-fun String.toCorrectCase() = if (uppercase) this else toLowerCase()
-
-sealed class PlotmeTable(name: String) : Table(name) {
- val px = integer("idX").primaryKey()
- val pz = integer("idZ").primaryKey()
- val world_name = varchar("world", 32).primaryKey()
-}
+class PlotmeTables(val uppercase: Boolean) {
+ fun String.toCorrectCase() = if (uppercase) this else toLowerCase()
-object PlotmePlotsT : PlotmeTable("plotmePlots".toCorrectCase()) {
- val owner_name = varchar("owner", 32)
- val owner_uuid = blob("ownerid").nullable()
-}
+ val PlotmePlots = PlotmePlotsT()
+ val PlotmeAllowed = PlotmeAllowedT()
+ val PlotmeDenied = PlotmeDeniedT()
+
+ inner abstract class PlotmeTable(name: String) : Table(name) {
+ val px = integer("idX").primaryKey()
+ val pz = integer("idZ").primaryKey()
+ val world_name = varchar("world", 32).primaryKey()
+ }
+
+ inner abstract class PlotmePlotPlayerMap(name: String) : PlotmeTable(name) {
+ val player_name = varchar("player", 32)
+ val player_uuid = blob("playerid").nullable()
+ }
+
+ inner class PlotmePlotsT : PlotmeTable("plotmePlots".toCorrectCase()) {
+ val owner_name = varchar("owner", 32)
+ val owner_uuid = blob("ownerid").nullable()
+ }
-sealed class PlotmePlotPlayerMap(name: String) : PlotmeTable(name) {
- val player_name = varchar("player", 32)
- val player_uuid = blob("playerid").nullable()
+ inner class PlotmeAllowedT : PlotmePlotPlayerMap("plotmeAllowed".toCorrectCase())
+ inner class PlotmeDeniedT : PlotmePlotPlayerMap("plotmeDenied".toCorrectCase())
}
-object PlotmeAllowedT : PlotmePlotPlayerMap("plotmeAllowed".toCorrectCase())
-object PlotmeDeniedT : PlotmePlotPlayerMap("plotmeDenied".toCorrectCase())