diff --git a/src/main/kotlin/de/darkatra/vrising/discord/migration/DatabaseMigrationService.kt b/src/main/kotlin/de/darkatra/vrising/discord/migration/DatabaseMigrationService.kt index fabc8909..82ef1352 100644 --- a/src/main/kotlin/de/darkatra/vrising/discord/migration/DatabaseMigrationService.kt +++ b/src/main/kotlin/de/darkatra/vrising/discord/migration/DatabaseMigrationService.kt @@ -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"]) diff --git a/src/main/kotlin/de/darkatra/vrising/discord/persistence/ServerRepository.kt b/src/main/kotlin/de/darkatra/vrising/discord/persistence/ServerRepository.kt index 68664ad4..e7472d22 100644 --- a/src/main/kotlin/de/darkatra/vrising/discord/persistence/ServerRepository.kt +++ b/src/main/kotlin/de/darkatra/vrising/discord/persistence/ServerRepository.kt @@ -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 @@ -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.") } @@ -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() + ) } } } diff --git a/src/main/kotlin/de/darkatra/vrising/discord/persistence/model/Server.kt b/src/main/kotlin/de/darkatra/vrising/discord/persistence/model/Server.kt index 1cce07e2..2fcd5d3d 100644 --- a/src/main/kotlin/de/darkatra/vrising/discord/persistence/model/Server.kt +++ b/src/main/kotlin/de/darkatra/vrising/discord/persistence/model/Server.kt @@ -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, @@ -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() { diff --git a/src/main/kotlin/de/darkatra/vrising/discord/persistence/model/Version.kt b/src/main/kotlin/de/darkatra/vrising/discord/persistence/model/Version.kt new file mode 100644 index 00000000..8e66954e --- /dev/null +++ b/src/main/kotlin/de/darkatra/vrising/discord/persistence/model/Version.kt @@ -0,0 +1,8 @@ +package de.darkatra.vrising.discord.persistence.model + +import java.time.Instant + +data class Version( + val revision: Long, + val updated: Instant +) diff --git a/src/main/kotlin/de/darkatra/vrising/discord/persistence/model/converter/ServerEntityConverter.kt b/src/main/kotlin/de/darkatra/vrising/discord/persistence/model/converter/ServerEntityConverter.kt index 70634246..c14a58f7 100644 --- a/src/main/kotlin/de/darkatra/vrising/discord/persistence/model/converter/ServerEntityConverter.kt +++ b/src/main/kotlin/de/darkatra/vrising/discord/persistence/model/converter/ServerEntityConverter.kt @@ -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 { @@ -17,7 +19,10 @@ class ServerEntityConverter : EntityConverter { @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, @@ -49,7 +54,8 @@ class ServerEntityConverter : EntityConverter { @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) diff --git a/src/test/kotlin/de/darkatra/vrising/discord/migration/DatabaseMigrationServiceTest.kt b/src/test/kotlin/de/darkatra/vrising/discord/migration/DatabaseMigrationServiceTest.kt index 102ca097..e2dce583 100644 --- a/src/test/kotlin/de/darkatra/vrising/discord/migration/DatabaseMigrationServiceTest.kt +++ b/src/test/kotlin/de/darkatra/vrising/discord/migration/DatabaseMigrationServiceTest.kt @@ -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 { @@ -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"]) diff --git a/src/test/kotlin/de/darkatra/vrising/discord/persistence/ServerRepositoryTest.kt b/src/test/kotlin/de/darkatra/vrising/discord/persistence/ServerRepositoryTest.kt index 21e1fe73..55e83ad9 100644 --- a/src/test/kotlin/de/darkatra/vrising/discord/persistence/ServerRepositoryTest.kt +++ b/src/test/kotlin/de/darkatra/vrising/discord/persistence/ServerRepositoryTest.kt @@ -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 @@ -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 { @@ -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) @@ -101,10 +96,8 @@ class ServerRepositoryTest { hostname = "test-2" } - println("Update 1") serverRepository.updateServer(update1) - println("Update 2") val e = assertThrows { serverRepository.updateServer(update2) } diff --git a/src/test/kotlin/de/darkatra/vrising/discord/persistence/model/ServerTestUtils.kt b/src/test/kotlin/de/darkatra/vrising/discord/persistence/model/ServerTestUtils.kt index e43ce1c6..46889e6e 100644 --- a/src/test/kotlin/de/darkatra/vrising/discord/persistence/model/ServerTestUtils.kt +++ b/src/test/kotlin/de/darkatra/vrising/discord/persistence/model/ServerTestUtils.kt @@ -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, diff --git a/src/test/kotlin/de/darkatra/vrising/discord/persistence/model/converter/EntityConverterTest.kt b/src/test/kotlin/de/darkatra/vrising/discord/persistence/model/converter/EntityConverterTest.kt index 95c55dc8..e6e97ba2 100644 --- a/src/test/kotlin/de/darkatra/vrising/discord/persistence/model/converter/EntityConverterTest.kt +++ b/src/test/kotlin/de/darkatra/vrising/discord/persistence/model/converter/EntityConverterTest.kt @@ -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 @@ -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,