設定
まず公開モジュールを選び、DocumentStore を1つ、IndexStore を1つ選びます。R2D1 は4つのバックエンドの組み合わせをサポートしますが、collection API は1つです。
公開モジュール
ドキュメントでは Maven Central の公開 URL を前提にしません。以下の coordinate にアプリケーションの version を指定して追加してください。
| モジュール | Coordinate | 役割 |
|---|---|---|
| R2D1 | dev.nexcraft:r2d1 | 共通 API、R2 DocumentStore、D1 IndexStore |
| R2D1-FILESYSTEM | dev.nexcraft:r2d1-filesystem | Filesystem DocumentStore |
| R2D1-JDBC | dev.nexcraft:r2d1-jdbc | H2、HSQLDB、SQLite IndexStore |
| R2D1-MICRONAUT | dev.nexcraft:r2d1-micronaut | Micronaut 5 bean 統合 |
DocumentStore を選ぶ
権威ドキュメントの保存先は R2 または Filesystem です。R2 は非同期 AWS SDK client を使い、Filesystem はアプリケーションが提供する executor で blocking I/O を実行します。
IndexStore を選ぶ
フィルタとソートに使う document identifier と @Index projection は D1 または JDBC に保存します。D1 は Java HttpClient による REST transport、JDBC はアプリケーション所有の DataSource 境界を使います。
リソース所有権
R2D1 adapter は呼び出し側が提供した DataSource、HttpClient、S3AsyncClient、Executor を勝手に閉じません。adapter または execution が直接作成した resource だけを所有します。
JDBC driver、connection pool、database server、Cloudflare account、credential はアプリケーションと運用環境が所有します。
4つの組み合わせ
R2 + D1、R2 + JDBC、Filesystem + D1、Filesystem + JDBC を選べます。2つの store と DocumentCodec を PersistenceCollectionFactory に渡すと、共通 R2D1Collection が両方を調整します。
R2D1 database =
R2D1.builder()
.collectionFactory(
new PersistenceCollectionFactory(
documentStore, indexStore, documentCodec, indexStore::initialize))
.build();
R2D1Collection<User> users = database.collection(User.class);