|
@@ -1,27 +1,28 @@
|
|
|
-package co.there4.hexagon
|
|
|
+package com.hexagonkt
|
|
|
|
|
|
-import co.there4.hexagon.settings.SettingsManager.settings
|
|
|
-import co.there4.hexagon.store.MongoIdRepository
|
|
|
-import co.there4.hexagon.store.mongoCollection
|
|
|
-import co.there4.hexagon.store.mongoDatabase
|
|
|
+import com.hexagonkt.helpers.systemSetting
|
|
|
+import com.hexagonkt.settings.SettingsManager.settings
|
|
|
+import com.hexagonkt.settings.SettingsManager.setting
|
|
|
+import com.hexagonkt.store.MongoIdRepository
|
|
|
+import com.hexagonkt.store.mongoCollection
|
|
|
+import com.hexagonkt.store.mongoDatabase
|
|
|
|
|
|
import com.zaxxer.hikari.HikariConfig
|
|
|
import com.zaxxer.hikari.HikariDataSource
|
|
|
|
|
|
-import java.lang.System.getenv
|
|
|
import java.sql.Connection
|
|
|
import java.sql.ResultSet.CONCUR_READ_ONLY
|
|
|
import java.sql.ResultSet.TYPE_FORWARD_ONLY
|
|
|
-import javax.sql.DataSource
|
|
|
+import kotlin.reflect.KClass
|
|
|
|
|
|
import kotlin.reflect.KProperty1
|
|
|
|
|
|
internal const val WORLD_ROWS = 10000
|
|
|
|
|
|
-private val DB_HOST = getenv("DBHOST") ?: "localhost"
|
|
|
-private val DB_NAME = settings["database"] as? String ?: "hello_world"
|
|
|
-private val WORLD_NAME: String = settings["worldCollection"] as? String ?: "world"
|
|
|
-private val FORTUNE_NAME: String = settings["fortuneCollection"] as? String ?: "fortune"
|
|
|
+private val DB_HOST = systemSetting("DBHOST", "localhost")
|
|
|
+private val DB_NAME = setting("database", "hello_world")
|
|
|
+private val WORLD_NAME: String = setting("worldCollection", "world")
|
|
|
+private val FORTUNE_NAME: String = setting("fortuneCollection", "fortune")
|
|
|
|
|
|
private val postgresqlUrl = "jdbc:postgresql://$DB_HOST/$DB_NAME?" +
|
|
|
"jdbcCompliantTruncation=false&" +
|
|
@@ -50,22 +51,25 @@ internal interface Store {
|
|
|
fun findAllFortunes(): List<Fortune>
|
|
|
fun findWorlds(count: Int): List<World>
|
|
|
fun replaceWorlds(count: Int): List<World>
|
|
|
+ fun close()
|
|
|
}
|
|
|
|
|
|
private class MongoDbStore : Store {
|
|
|
private val database = mongoDatabase("mongodb://$DB_HOST/$DB_NAME")
|
|
|
|
|
|
- private val worldRepository = repository(WORLD_NAME, World::_id)
|
|
|
- private val fortuneRepository = repository(FORTUNE_NAME, Fortune::_id)
|
|
|
+ private val worldRepository = repository(WORLD_NAME, World::class, World::_id)
|
|
|
+ private val fortuneRepository = repository(FORTUNE_NAME, Fortune::class, Fortune::_id)
|
|
|
|
|
|
// TODO Find out why it fails when creating index '_id' with background: true
|
|
|
- private inline fun <reified T : Any> repository(name: String, key: KProperty1<T, Int>) =
|
|
|
- MongoIdRepository(T::class, mongoCollection(name, database), key, indexOrder = null)
|
|
|
+ private fun <T : Any> repository(name: String, type: KClass<T>, key: KProperty1<T, Int>) =
|
|
|
+ MongoIdRepository(type, mongoCollection(name, database), key, indexOrder = null)
|
|
|
+
|
|
|
+ override fun close() { /* Not needed */ }
|
|
|
|
|
|
override fun findAllFortunes() = fortuneRepository.findObjects().toList()
|
|
|
|
|
|
override fun findWorlds(count: Int) =
|
|
|
- (1..count).map { worldRepository.find(randomWorld()) }.filterNotNull()
|
|
|
+ (1..count).mapNotNull { worldRepository.find(randomWorld()) }
|
|
|
|
|
|
override fun replaceWorlds(count: Int) = (1..count)
|
|
|
.map { worldRepository.find(randomWorld())?.copy(randomNumber = randomWorld()) }
|
|
@@ -82,17 +86,21 @@ private class SqlStore(jdbcUrl: String) : Store {
|
|
|
private val UPDATE_WORLD = "update world set randomNumber = ? where id = ?"
|
|
|
private val SELECT_ALL_FORTUNES = "select * from fortune"
|
|
|
|
|
|
- private val DATA_SOURCE: DataSource
|
|
|
+ private val DATA_SOURCE: HikariDataSource
|
|
|
|
|
|
init {
|
|
|
val config = HikariConfig()
|
|
|
config.jdbcUrl = jdbcUrl
|
|
|
- config.maximumPoolSize = settings["maximumPoolSize"] as? Int ?: 32
|
|
|
- config.username = settings["databaseUsername"] as? String ?: "benchmarkdbuser"
|
|
|
- config.password = settings["databasePassword"] as? String ?: "benchmarkdbpass"
|
|
|
+ config.maximumPoolSize = setting("maximumPoolSize", 16)
|
|
|
+ config.username = setting("databaseUsername", "benchmarkdbuser")
|
|
|
+ config.password = setting("databasePassword", "benchmarkdbpass")
|
|
|
DATA_SOURCE = HikariDataSource(config)
|
|
|
}
|
|
|
|
|
|
+ override fun close() {
|
|
|
+ DATA_SOURCE.close()
|
|
|
+ }
|
|
|
+
|
|
|
override fun findAllFortunes(): List<Fortune> {
|
|
|
var fortunes = listOf<Fortune>()
|
|
|
|