D1 IndexStore
D1IndexStore 是使用 Cloudflare D1 REST API 的 built-in IndexStore。它只保存 document identifier 和 indexed field 值组成的可重建 projection,完整 document content 仍在 DocumentStore 中。
依赖与创建
D1 adapter 包含在 dev.nexcraft:r2d1 中。请明确配置 account、database 和 API token。下面的值是 placeholder,token 应使用 secret configuration。
implementation("dev.nexcraft:r2d1:<version>")D1Config
D1Config.toString() 会隐藏 account、database 和 token。
D1Config config = new D1Config(accountId, databaseId, apiToken);
D1IndexStore indexes = new D1IndexStore(config);REST transport 边界
public constructor 通过 Java reusable asynchronous HttpClient 调用 Cloudflare D1 REST API。不要将它理解为支持 Cloudflare Worker transport。
adapter 不会创建 executor、scheduler 或 automatic retry。应用可以传入自己拥有的 HttpClient;关闭 store 不会关闭该 client。
Collection schema 初始化
initialize(Class<?>) 检查 document annotation,创建或验证 managed collection schema,然后返回 CompletionStage。将 indexes::initialize 传给 PersistenceCollectionFactory。
D1Config config = new D1Config(accountId, databaseId, apiToken);
D1IndexStore indexes = new D1IndexStore(config);
R2D1 database =
R2D1.builder()
.collectionFactory(
new PersistenceCollectionFactory(
documentStore, indexes, documentCodec, indexes::initialize))
.build();Query projection
D1 adapter 支持 indexed String、Long、Double、Boolean。D1 不会为了 query 下载 R2 object,再在 application memory 中过滤。
clear(String) 只删除 derived rows;table、column 和 managed index 会保留给 rebuildIndex() 写入 replacement rows。
