|
@@ -1,3 +1,4 @@
|
|
|
|
+import io.vertx.core.CompositeFuture
|
|
import io.vertx.core.Future
|
|
import io.vertx.core.Future
|
|
import io.vertx.core.Vertx
|
|
import io.vertx.core.Vertx
|
|
import io.vertx.core.VertxOptions
|
|
import io.vertx.core.VertxOptions
|
|
@@ -9,6 +10,7 @@ import io.vertx.sqlclient.SqlClient
|
|
import io.vertx.sqlclient.Tuple
|
|
import io.vertx.sqlclient.Tuple
|
|
import java.util.Random
|
|
import java.util.Random
|
|
|
|
|
|
|
|
+
|
|
class PostgresDatabase : Database {
|
|
class PostgresDatabase : Database {
|
|
private val queryPool: SqlClient
|
|
private val queryPool: SqlClient
|
|
private val updatePool: SqlClient
|
|
private val updatePool: SqlClient
|
|
@@ -44,24 +46,17 @@ class PostgresDatabase : Database {
|
|
(1..count).map { queryPool.findWorld(random.world()) }
|
|
(1..count).map { queryPool.findWorld(random.world()) }
|
|
).toCompletionStage().toCompletableFuture().get().list<World>()
|
|
).toCompletionStage().toCompletableFuture().get().list<World>()
|
|
|
|
|
|
- override fun updateWorlds(count: Int): List<Pair<Int, Int>> {
|
|
|
|
- val updatedAndSorted = Future
|
|
|
|
- .all(
|
|
|
|
- (1..count)
|
|
|
|
- .map {
|
|
|
|
- queryPool.findWorld(random.world())
|
|
|
|
- .map { it.first to random.world() }
|
|
|
|
- }
|
|
|
|
- )
|
|
|
|
- .toCompletionStage().toCompletableFuture().get().list<World>()
|
|
|
|
- .sortedBy { it.first }
|
|
|
|
-
|
|
|
|
- updatePool.preparedQuery("UPDATE world SET randomnumber = $1 WHERE id = $2")
|
|
|
|
- .executeBatch(updatedAndSorted.map { Tuple.of(it.first, it.second) })
|
|
|
|
- .toCompletionStage().toCompletableFuture().get()
|
|
|
|
-
|
|
|
|
- return updatedAndSorted
|
|
|
|
- }
|
|
|
|
|
|
+ override fun updateWorlds(count: Int) =
|
|
|
|
+ Future.all((1..count)
|
|
|
|
+ .map { queryPool.findWorld(random.world()) })
|
|
|
|
+ .map<List<World>>(CompositeFuture::list)
|
|
|
|
+ .toCompletionStage()
|
|
|
|
+ .thenCompose { worlds ->
|
|
|
|
+ updatePool.preparedQuery("UPDATE world SET randomnumber = $1 WHERE id = $2")
|
|
|
|
+ .executeBatch((1..count).map { Tuple.of(random.world(), random.world()) })
|
|
|
|
+ .toCompletionStage()
|
|
|
|
+ .thenApply { worlds }
|
|
|
|
+ }.toCompletableFuture().get()
|
|
|
|
|
|
override fun fortunes() = queryPool.preparedQuery("SELECT id, message FROM fortune")
|
|
override fun fortunes() = queryPool.preparedQuery("SELECT id, message FROM fortune")
|
|
.execute()
|
|
.execute()
|