|
| 1 | +{{#title Kotlin SDK for Atomic Data}} |
| 2 | + |
| 3 | +# Kotlin SDK |
| 4 | + |
| 5 | +Local-first [Atomic Data](atomic-data-overview.md) for the JVM. The package |
| 6 | +wraps [`atomic_lib`](rust-lib.md) through |
| 7 | +[UniFFI](https://mozilla.github.io/uniffi-rs/) — the same Rust store the |
| 8 | +browser (WASM), Flutter app, and [Python SDK](python.md) use. Reads and |
| 9 | +writes go to a local [redb](https://github.com/cberner/redb) file. Edits are |
| 10 | +signed Loro commits. A server is optional. |
| 11 | + |
| 12 | +Source: [`ffi/`](https://github.com/atomicdata-dev/atomic-server/tree/develop/ffi) |
| 13 | +(crate `atomic-ffi`, Kotlin package `dev.atomicdata`). |
| 14 | + |
| 15 | +Python stays on PyO3. This UniFFI surface is what Swift and the Android |
| 16 | +Binder host will share later. |
| 17 | + |
| 18 | +## Install |
| 19 | + |
| 20 | +From a checkout of this repo (needs a Rust toolchain and JDK 21): |
| 21 | + |
| 22 | +```bash |
| 23 | +cd ffi |
| 24 | +cargo build |
| 25 | +./generate-kotlin.sh |
| 26 | +cd kotlin && ./gradlew test |
| 27 | +``` |
| 28 | + |
| 29 | +Point `jna.library.path` at `ffi/target/debug` so the JVM can load |
| 30 | +`libatomic_ffi`. |
| 31 | + |
| 32 | +## Quick start |
| 33 | + |
| 34 | +```kotlin |
| 35 | +import dev.atomicdata.Store |
| 36 | +import dev.atomicdata.Urls |
| 37 | + |
| 38 | +val store = Store.open("./my-atomic-data") |
| 39 | +val setup = store.setup("Ada") |
| 40 | + |
| 41 | +val note = store.create( |
| 42 | + Urls.PLAIN_TEXT, |
| 43 | + "Hello", |
| 44 | + null, |
| 45 | + mapOf("description" to "A locally stored note"), |
| 46 | +) |
| 47 | +note.set(Urls.DESCRIPTION, "Edited offline") |
| 48 | +note.save() |
| 49 | + |
| 50 | +val got = store.get(note.subject()) |
| 51 | +println("${got?.get("name")} ${got?.get("description")}") |
| 52 | + |
| 53 | +for (child in store.query(setup.driveSubject, null, null, null, null, 0u)) { |
| 54 | + println("${child.subject()} ${child.name()}") |
| 55 | +} |
| 56 | + |
| 57 | +store.flush() |
| 58 | +``` |
| 59 | + |
| 60 | +`setup.agentSecret` is the only way to sign writes after you reopen the |
| 61 | +store. Keep it. |
| 62 | + |
| 63 | +```kotlin |
| 64 | +val store = Store.open("./my-atomic-data") |
| 65 | +store.loadAgent(secret) |
| 66 | +``` |
| 67 | + |
| 68 | +`Store.inMemory()` is the same API without a directory. |
| 69 | + |
| 70 | +`Resource.destroyResource()` deletes the resource. It is not named |
| 71 | +`destroy()` because UniFFI already uses that for FFI handle teardown. |
| 72 | + |
| 73 | +## P2P sync (Iroh) |
| 74 | + |
| 75 | +```kotlin |
| 76 | +val node = store.startPeer() // did:ad:node:… |
| 77 | +store.announce(null) // optional pkarr publish |
| 78 | +// On the other device, same agent secret (or a grant), then: |
| 79 | +other.syncWith(node, null) |
| 80 | +``` |
| 81 | + |
| 82 | +After the first sync, connected peers get live Loro updates on `.save()`. |
| 83 | +`store.waitFor(subject, timeoutSecs)` blocks until a local or peer change |
| 84 | +lands. |
| 85 | + |
| 86 | +Two Iroh nodes cannot share one JVM — `startPeer` is process-global, same |
| 87 | +as Flutter and Python. Use two processes (or two devices). |
| 88 | + |
| 89 | +## What this is not (yet) |
| 90 | + |
| 91 | +WebSocket-to-server `SyncSession` is not wrapped. Blobs and history are not |
| 92 | +wrapped. An Android AAR (`cargo-ndk`) and Binder host are later layers. |
| 93 | +Iroh P2P is. |
0 commit comments