Skip to content

Commit 6762569

Browse files
committed
revert /update change
1 parent 19b4f5b commit 6762569

File tree

1 file changed

+19
-14
lines changed
  • frameworks/Kotlin/ktor/ktor-r2dbc/src/main/kotlin/org/jetbrains/ktor/benchmarks

1 file changed

+19
-14
lines changed

frameworks/Kotlin/ktor/ktor-r2dbc/src/main/kotlin/org/jetbrains/ktor/benchmarks/Hello.kt

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -134,14 +134,20 @@ fun Application.main() {
134134
worldsUpdated.add(world)
135135
}
136136

137-
// Execute updates sequentially
138-
worldsUpdated.forEach { world ->
139-
Mono.usingWhen(dbConnFactory.create(), { connection ->
140-
Mono.from(connection.createStatement(UPDATE_QUERY)
141-
.bind("$1", world.randomNumber)
142-
.bind("$2", world.id)
143-
.execute())
144-
}, Connection::close).awaitFirstOrNull()
137+
val worldsUpdated = buildList {
138+
worlds.collect { world ->
139+
world.randomNumber = random.nextInt(DB_ROWS) + 1
140+
add(world)
141+
142+
Mono.usingWhen(dbConnFactory.create(), { connection ->
143+
Mono.from(
144+
connection.createStatement(UPDATE_QUERY)
145+
.bind(0, world.randomNumber)
146+
.bind(1, world.id)
147+
.execute()
148+
).flatMap { Mono.from(it.rowsUpdated) }
149+
}, Connection::close).awaitFirstOrNull()
150+
}
145151
}
146152

147153
call.respondText(json.encodeToString(worldsUpdated), ContentType.Application.Json)
@@ -157,14 +163,13 @@ private fun getWorld(
157163
.execute())
158164
.flatMap { r ->
159165
Mono.from(r.map { row, _ ->
160-
val id = row.get("id", Int::class.java) ?: row.get(0, Int::class.java)
161-
val randomNumber = row.get("randomnumber", Int::class.java) ?: row.get(1, Int::class.java)
162-
163-
if (id == null || randomNumber == null) {
166+
val id = row.get(0, Int::class.java)
167+
val randomNumber = row.get(1, Int::class.java)
168+
if (id != null && randomNumber != null) {
169+
World(id, randomNumber)
170+
} else {
164171
throw IllegalStateException("Database returned null values for required fields")
165172
}
166-
167-
World(id, randomNumber)
168173
})
169174
}
170175
}, Connection::close)

0 commit comments

Comments
 (0)