Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/feat/switch-to-sql' into feat/sw…
Browse files Browse the repository at this point in the history
…itch-to-sql
  • Loading branch information
phinner committed Nov 23, 2023
2 parents ee2c4e1 + beb1df2 commit 0e77c5e
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 21 deletions.
9 changes: 9 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,12 @@ services:
ports:
- "5672:5672"
- "15672:15672"

# The Mariadb database
# login is root:root
mariadb:
image: mariadb:latest
ports:
- "3306:3306"
environment:
MARIADB_ROOT_PASSWORD: root
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ import com.xpdustry.imperium.common.misc.exists
import com.xpdustry.imperium.common.snowflake.Snowflake
import com.xpdustry.imperium.common.snowflake.SnowflakeGenerator
import java.io.InputStream
import java.util.function.Supplier
import kotlin.math.roundToInt
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.asFlow
import kotlinx.coroutines.flow.map
import org.jetbrains.exposed.sql.ColumnSet
import org.jetbrains.exposed.sql.Join
import org.jetbrains.exposed.sql.ResultRow
import org.jetbrains.exposed.sql.SchemaUtils
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
Expand Down Expand Up @@ -65,7 +67,7 @@ interface MindustryMapManager {
author: String?,
width: Int,
height: Int,
stream: InputStream
stream: Supplier<InputStream>
): Snowflake

suspend fun updateMap(
Expand All @@ -74,7 +76,7 @@ interface MindustryMapManager {
author: String?,
width: Int,
height: Int,
stream: InputStream
stream: Supplier<InputStream>
): Boolean

suspend fun getMapInputStream(map: Snowflake): InputStream?
Expand Down Expand Up @@ -174,7 +176,7 @@ class SimpleMindustryMapManager(
author: String?,
width: Int,
height: Int,
stream: InputStream
stream: Supplier<InputStream>
): Snowflake =
provider.newSuspendTransaction {
val snowflake = generator.generate()
Expand All @@ -185,7 +187,7 @@ class SimpleMindustryMapManager(
it[MindustryMapTable.author] = author
it[MindustryMapTable.width] = width
it[MindustryMapTable.height] = height
it[file] = ExposedBlob(stream)
it[file] = ExposedBlob(stream.get().use(InputStream::readAllBytes))
}
snowflake
}
Expand All @@ -196,7 +198,7 @@ class SimpleMindustryMapManager(
author: String?,
width: Int,
height: Int,
stream: InputStream
stream: Supplier<InputStream>
): Boolean =
provider.newSuspendTransaction {
val rows =
Expand All @@ -205,17 +207,19 @@ class SimpleMindustryMapManager(
it[MindustryMapTable.author] = author
it[MindustryMapTable.width] = width
it[MindustryMapTable.height] = height
it[file] = ExposedBlob(stream)
it[file] = ExposedBlob(stream.get().use(InputStream::readAllBytes))
}
rows != 0
}

override suspend fun getMapInputStream(map: Snowflake): InputStream? =
MindustryMapTable.slice(MindustryMapTable.file)
.select { MindustryMapTable.id eq map }
.firstOrNull()
?.get(MindustryMapTable.file)
?.inputStream
provider.newSuspendTransaction {
MindustryMapTable.slice(MindustryMapTable.file)
.select { MindustryMapTable.id eq map }
.firstOrNull()
?.get(MindustryMapTable.file)
?.inputStream
}

override suspend fun searchMapByName(query: String): Flow<MindustryMap> =
provider.newSuspendTransaction {
Expand All @@ -242,7 +246,7 @@ class SimpleMindustryMapManager(
}

private fun ColumnSet.sliceWithoutFile() =
MindustryMapTable.slice(columns - MindustryMapTable.file)
slice((if (this is Join) table.columns else columns) - MindustryMapTable.file)

private suspend fun getMapGamemodes(map: Snowflake): Set<MindustryGamemode> =
provider.newSuspendTransaction {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,7 @@ class SimplePunishmentManager(
this[PunishmentTable.targetAddressMask])

val author =
Punishment.Author(
this[PunishmentTable.authorId].value, this[PunishmentTable.authorType])
Punishment.Author(this[PunishmentTable.authorId], this[PunishmentTable.authorType])

val pardon =
if (this[PunishmentTable.pardonTimestamp] == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@
package com.xpdustry.imperium.common.security

import com.xpdustry.imperium.common.snowflake.SnowflakeIdTable
import com.xpdustry.imperium.common.user.UserTable
import org.jetbrains.exposed.sql.ReferenceOption
import org.jetbrains.exposed.sql.javatime.duration
import org.jetbrains.exposed.sql.javatime.timestamp

object PunishmentTable : SnowflakeIdTable("punishment") {
val authorId = reference("author_id", UserTable, onDelete = ReferenceOption.SET_NULL)
val authorId = long("author_id")
val authorType = enumerationByName<Punishment.Author.Type>("author_type", 32)
val targetAddress = binary("target_address", 16)
val targetAddressMask = byte("target_address_mask")
Expand Down
3 changes: 2 additions & 1 deletion imperium-discord/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ tasks.shadowJar {
exclude(dependency("org.apache.logging.log4j:log4j-to-slf4j:.*"))
exclude(dependency("com.sksamuel.hoplite:hoplite-.*:.*"))
exclude(dependency("org.javacord:javacord-core:.*"))
exclude(dependency(libs.sqlite.get()))
exclude(dependency(libs.exposed.jdbc.get()))
exclude(dependency(libs.sqlite.get()))
exclude(dependency(libs.mariadb.get()))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ class MapCommand(instances: InstanceManager) : ImperiumApplication.Listener {
@ButtonCommand(MAP_UPLOAD_BUTTON, Rank.ADMIN)
private suspend fun onMapUpload(actor: InteractionSender.Button) {
val attachment = actor.message.attachments.first()
val meta = content.getMapMetadata(attachment.asInputStream()).getOrThrow()
val meta = attachment.asInputStream().use { content.getMapMetadata(it).getOrThrow() }

val map = maps.findMapByName(meta.name.stripMindustryColors())
val snowflake: Snowflake
Expand All @@ -151,7 +151,7 @@ class MapCommand(instances: InstanceManager) : ImperiumApplication.Listener {
author = meta.author?.stripMindustryColors(),
width = meta.width,
height = meta.height,
stream = attachment.asInputStream())
stream = attachment::asInputStream)
} else {
snowflake = map.snowflake
maps.updateMap(
Expand All @@ -160,7 +160,7 @@ class MapCommand(instances: InstanceManager) : ImperiumApplication.Listener {
author = meta.author?.stripMindustryColors(),
width = meta.width,
height = meta.height,
stream = attachment.asInputStream())
stream = attachment::asInputStream)
}

updateSubmissionEmbed(actor, Color.GREEN, "uploaded")
Expand Down
1 change: 1 addition & 0 deletions imperium-mindustry/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ tasks.shadowJar {
minimize {
exclude(dependency("com.sksamuel.hoplite:hoplite-.*:.*"))
exclude(dependency(libs.sqlite.get()))
exclude(dependency(libs.mariadb.get()))
}
}

Expand Down

0 comments on commit 0e77c5e

Please sign in to comment.