D1 IndexStore
D1IndexStore is the built-in IndexStore implementation for Cloudflare D1. It stores document
identifiers and indexed field values as a rebuildable projection. Complete document content remains
in the configured DocumentStore.
Dependency and construction
The D1 adapter is part of dev.nexcraft:r2d1:
implementation("dev.nexcraft:r2d1:<version>")
Configure the account, database, and API token explicitly:
import dev.nexcraft.r2d1.d1.D1Config;
import dev.nexcraft.r2d1.d1.D1IndexStore;
D1Config config = new D1Config(accountId, databaseId, apiToken);
D1IndexStore indexes = new D1IndexStore(config);
The values are placeholders. Use secret configuration for the API token. D1Config.toString()
redacts the account, database, and token values.
REST transport
The public constructor uses Cloudflare D1’s REST API through Java’s reusable asynchronous
HttpClient. It does not use a Cloudflare Worker transport, create an executor, create a scheduler,
or provide automatic retries.
If the application owns an HttpClient, pass it to the two-argument constructor. Closing the store
does not close that caller-owned client.
Initialize a collection
initialize(Class<?>) inspects the document annotations, creates or validates the managed collection
schema, and returns a CompletionStage. PersistenceCollectionFactory supplies this initializer to
the collection facade:
R2D1 database =
R2D1.builder()
.collectionFactory(
new PersistenceCollectionFactory(
documentStore, indexes, documentCodec, indexes::initialize))
.build();
The collection is not used until initialization completes. Concurrent first initialization calls
for the same collection share one stage, and a failed initialization is not retried automatically
by the same D1IndexStore instance.
Query projection
The D1 v1 adapter supports indexed String, Long, Double, and Boolean values. Queries use
explicitly indexed fields and the public comparison model. D1 never downloads R2 objects to filter
them in application memory.
clear(String) removes only derived rows. Tables, columns, and managed indexes remain available for
the replacement rows written by rebuildIndex().
Read Querying for the public operators and Consistency and Recovery for partial failures and recovery.
