Skip to content

Commit

Permalink
chore: use object with revision to represent the version of a Server
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkAtra committed Jun 30, 2024
1 parent c2a3ed6 commit 4e16b2f
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ class DatabaseMigrationService(

val server = Document.createDocument().apply {
put("id", document["id"])
put("version", document["version"])
put("version_revision", 1L)
put("version_updated", Instant.ofEpochMilli(document["version"] as Long).toString())
put("discordServerId", document["discordServerId"])
put("hostname", document["hostname"])
put("queryPort", document["queryPort"])
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package de.darkatra.vrising.discord.persistence

import de.darkatra.vrising.discord.persistence.model.Server
import de.darkatra.vrising.discord.persistence.model.Version
import org.dizitart.kno2.filters.and
import org.dizitart.kno2.filters.eq
import org.dizitart.no2.Nitrite
Expand Down Expand Up @@ -34,9 +35,7 @@ class ServerRepository(
?: throw OutdatedServerException("Server with id '${server.id}' not found."))
.version!!

println("Server: $serverVersion")
println("Database: $databaseVersion")
if (serverVersion == null || databaseVersion > serverVersion) {
if (serverVersion == null || databaseVersion.revision > serverVersion.revision || databaseVersion.updated > serverVersion.updated) {
throw OutdatedServerException("Server with id '${server.id}' was already updated by another thread.")
}

Expand Down Expand Up @@ -99,7 +98,10 @@ class ServerRepository(

return server.apply {
@Suppress("DEPRECATION") // this is the internal usage the warning is referring to
version = Instant.now().toEpochMilli()
version = Version(
revision = (version?.revision ?: 0) + 1,
updated = Instant.now()
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ data class Server(
@Id
val id: String,
@Deprecated("This field is updated automatically by the ServerRepository, manually update with caution")
var version: Long? = null,

internal var version: Version? = null,
var discordServerId: String,

var hostname: String,
Expand Down Expand Up @@ -45,7 +44,7 @@ data class Server(

@Suppress("DEPRECATION") // this is the internal usage the warning is referring to
val lastUpdated: Instant
get() = Instant.ofEpochMilli(version!!)
get() = version!!.updated

override val status: Status
get() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package de.darkatra.vrising.discord.persistence.model

import java.time.Instant

data class Version(
val revision: Long,
val updated: Instant
)
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import de.darkatra.vrising.discord.persistence.model.PlayerActivityFeed
import de.darkatra.vrising.discord.persistence.model.PvpKillFeed
import de.darkatra.vrising.discord.persistence.model.Server
import de.darkatra.vrising.discord.persistence.model.StatusMonitor
import de.darkatra.vrising.discord.persistence.model.Version
import org.dizitart.no2.collection.Document
import org.dizitart.no2.common.mapper.EntityConverter
import org.dizitart.no2.common.mapper.NitriteMapper
import java.time.Instant

class ServerEntityConverter : EntityConverter<Server> {

Expand All @@ -17,7 +19,10 @@ class ServerEntityConverter : EntityConverter<Server> {
@Suppress("DEPRECATION")
return Server(
id = document.get(Server::id.name, String::class.java),
version = document.get(Server::version.name) as Long,
version = Version(
revision = document.get(Server::version.name + "_" + Version::revision.name) as Long,
updated = Instant.parse(document.get(Server::version.name + "_" + Version::updated.name, String::class.java)),
),
discordServerId = document.get(Server::discordServerId.name, String::class.java),
hostname = document.get(Server::hostname.name, String::class.java),
queryPort = document.get(Server::queryPort.name) as Int,
Expand Down Expand Up @@ -49,7 +54,8 @@ class ServerEntityConverter : EntityConverter<Server> {
@Suppress("DEPRECATION")
return Document.createDocument().apply {
put(Server::id.name, server.id)
put(Server::version.name, server.version)
put(Server::version.name + "_" + Version::revision.name, server.version!!.revision)
put(Server::version.name + "_" + Version::updated.name, server.version!!.updated.toString())
put(Server::discordServerId.name, server.discordServerId)
put(Server::hostname.name, server.hostname)
put(Server::queryPort.name, server.queryPort)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package de.darkatra.vrising.discord.migration
import de.darkatra.vrising.discord.DatabaseConfigurationTestUtils
import de.darkatra.vrising.discord.persistence.model.Server
import de.darkatra.vrising.discord.persistence.model.Status
import de.darkatra.vrising.discord.persistence.model.Version
import org.assertj.core.api.Assertions.assertThat
import org.dizitart.no2.collection.Document
import org.junit.jupiter.api.Disabled
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.condition.DisabledInNativeImage
import java.time.Instant

@DisabledInNativeImage
class DatabaseMigrationServiceTest {
Expand Down Expand Up @@ -195,7 +197,7 @@ class DatabaseMigrationServiceTest {
}
assertThat(server.id).isEqualTo(oldDocument["id"])
@Suppress("DEPRECATION")
assertThat(server.version).isEqualTo(oldDocument["version"])
assertThat(server.version).isEqualTo(Version(1, Instant.ofEpochMilli(oldDocument["version"] as Long)))
assertThat(server.discordServerId).isEqualTo(oldDocument["discordServerId"])
assertThat(server.hostname).isEqualTo(oldDocument["hostname"])
assertThat(server.queryPort).isEqualTo(oldDocument["queryPort"])
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package de.darkatra.vrising.discord.persistence

import ch.qos.logback.classic.Logger
import de.darkatra.vrising.discord.DatabaseConfigurationTestUtils
import de.darkatra.vrising.discord.persistence.model.ServerTestUtils
import org.assertj.core.api.Assertions.assertThat
Expand All @@ -9,7 +8,6 @@ import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import org.junit.jupiter.api.condition.DisabledInNativeImage
import org.slf4j.LoggerFactory

@DisabledInNativeImage
class ServerRepositoryTest {
Expand Down Expand Up @@ -88,9 +86,6 @@ class ServerRepositoryTest {
@Test
fun `should not update server status monitor with higher version`() {

val logger = LoggerFactory.getLogger("nitrite") as Logger
logger.level = ch.qos.logback.classic.Level.DEBUG

val server = ServerTestUtils.getServer()
serverRepository.addServer(server)

Expand All @@ -101,10 +96,8 @@ class ServerRepositoryTest {
hostname = "test-2"
}

println("Update 1")
serverRepository.updateServer(update1)

println("Update 2")
val e = assertThrows<OutdatedServerException> {
serverRepository.updateServer(update2)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ object ServerTestUtils {
fun getServer(): Server {
return Server(
id = ID,
version = Instant.now().toEpochMilli(),
version = Version(
revision = 1,
updated = Instant.now()
),
discordServerId = DISCORD_SERVER_ID,
hostname = HOST_NAME,
queryPort = QUERY_PORT,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import de.darkatra.vrising.discord.persistence.model.ServerTestUtils.ID
import de.darkatra.vrising.discord.persistence.model.ServerTestUtils.QUERY_PORT
import de.darkatra.vrising.discord.persistence.model.Status
import de.darkatra.vrising.discord.persistence.model.StatusMonitor
import de.darkatra.vrising.discord.persistence.model.Version
import org.assertj.core.api.Assertions.assertThat
import org.dizitart.no2.collection.Document
import org.dizitart.no2.common.mapper.SimpleNitriteMapper
Expand All @@ -34,7 +35,10 @@ class EntityConverterTest {

val originalServer = Server(
id = ID,
version = Instant.now().toEpochMilli(),
version = Version(
revision = 1,
updated = Instant.now()
),
discordServerId = DISCORD_SERVER_ID,
hostname = HOST_NAME,
queryPort = QUERY_PORT,
Expand Down

0 comments on commit 4e16b2f

Please sign in to comment.