- サーバーサイドKotlinという選択肢とユーザベース
- Java 17をはじめよう Kotlinをはじめよう
- R2DBC公式
- JJUG ナイトセミナー 「Reactive Streams特集」に行ってきました #jjug
- R2DBCの2つのMySQLドライバー実装について
Last active
September 2, 2022 09:04
-
-
Save yyYank/cbcf8074b3b3bfa0fd68d9fc00cb6e73 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import kotlinx.coroutines.Dispatchers | |
import kotlinx.coroutines.async | |
import kotlinx.coroutines.runBlocking | |
import org.springframework.r2dbc.core.DatabaseClient | |
import org.springframework.stereotype.Repository | |
import org.springframework.stereotype.Service | |
import reactor.core.publisher.Flux | |
@Repository | |
class HogeRepository( | |
private val dbClient: DatabaseClient | |
) { | |
fun select(ids: List<String>): Flux<HogeEntity> { | |
return dbClient.sql( | |
""" | |
select * from hoge | |
where id in (:ids) | |
""".trimIndent() | |
) | |
.bind("ids", ids) | |
.map { row -> | |
HogeEntity( | |
id = checkNotNull(row.get("id", String::class.java)), | |
hoge = checkNotNull(row.get("hoge", java.lang.Integer::class.java)?.toInt()), | |
fuga = checkNotNull(row.get("fuga", String::class.java)) | |
) | |
} | |
.all() | |
} | |
} | |
data class HogeEntity(val id: String, val hoge: Int, val fuga: String) | |
data class Hoge() | |
@Service | |
class HogeService( | |
private val repository: hoge.infra.HogeRepository | |
) { | |
fun fetchHoge(ids: List<String>): List<Hoge> = runBlocking { | |
val hogeFlux = repository.select(ids) | |
val hogeList = hogeFlux.collectList() | |
.awaitSingle() | |
ids.map { id -> | |
async(context = Dispatchers.Default) { | |
// 非同期でものすごい処理 | |
Hoge() | |
} | |
}.map { it.await() } | |
} | |
} | |
fun callback(f: () -> Unit) {} | |
// ちょっとだけコールバックヘルともサヨナラできた気がした | |
fun hoge() { | |
callback { | |
callback { | |
callback { | |
callback { | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment