Quickstart for JVM
Python · Node · Go · Rust · Swift · Dart · JVM · .NET · C++ · Objective-C
This program shows two devices synchronizing through one remote. It resolves a conflict so both devices finish with the same row. See Concepts for the database and synchronization terms.
| Term | Meaning in this program |
|---|---|
| Local store | The database files on one device. |
| Remote | The shared copy of a database that devices synchronize with. |
| Revision | One published database state that does not change. |
| Head | The remote’s newest published revision. |
| Push | Send local writes to the remote. |
| Pull | Receive writes from the remote. |
| Pending | Local writes that the device has not confirmed as published. |
| Replay | Run local statements again on the remote revision. |
| Quarantine | Preserve a transaction that cannot replay safely, for the application to handle. |
The two databases represent a laptop and a phone. Both change the same row before receiving the other’s change. The remote uses a temporary directory that the program removes when it exits.
- Complete the installation.
- Save this program as
Quickstart.java. - Compile it against
potluckdb-jvm. - Run it with
--enable-native-access=ALL-UNNAMED.
The program prints two lines:
| Output | Meaning |
|---|---|
Use query for this statement. | The program passes a read statement to execute on purpose. The call fails with read_in_execute, and the program prints the error’s action(). |
quickstart: ok | The program completed all checks, including checking that both devices contain the same row. |
Without an accepted license key, a trial notice on standard error is expected. See license notices for the text. If the program fails or omits these lines, check the installation and any changes to the program.
// Two devices, one remote, one conflict, and its resolution.
import dev.potluckdb.Bindings;import dev.potluckdb.ConflictResolution;import dev.potluckdb.Database;import dev.potluckdb.OpenOptions;import dev.potluckdb.PotluckDB;import dev.potluckdb.PotluckDBException;import dev.potluckdb.Remote;import dev.potluckdb.Rows;import dev.potluckdb.SyncPolicy;import dev.potluckdb.SyncResult;import java.nio.file.Files;import java.nio.file.Path;import java.util.Comparator;import java.util.List;import java.util.concurrent.BlockingQueue;import java.util.concurrent.LinkedBlockingQueue;import java.util.concurrent.TimeUnit;
public final class Quickstart { /** Every sync attempt reports an outcome, UpToDate included; wait for the requested one. */ static <T extends SyncResult> T waitFor(BlockingQueue<SyncResult> events, Class<T> wanted) throws Exception { while (true) { SyncResult outcome = events.poll(10, TimeUnit.SECONDS); if (outcome == null) throw new IllegalStateException("no sync outcome within ten seconds"); if (outcome instanceof SyncResult.Failed failed) throw failed.error(); if (wanted.isInstance(outcome)) return wanted.cast(outcome); } }
/** One Pushed is one automatic cycle's work, not proof that everything written so far is published. */ static void waitUntilPublished(Database device, BlockingQueue<SyncResult> events) throws Exception { while (device.status().pendingWrites() > 0) { waitFor(events, SyncResult.Pushed.class); } }
static void expect(boolean condition, String message) { if (!condition) throw new IllegalStateException(message); }
public static void main(String[] args) throws Exception { Path scratch = Files.createTempDirectory("potluckdb-"); try (Remote remote = Remote.fromDirectory(scratch.resolve("remote").toString())) { // a bucket replaces this in production; see the installation page // The laptop synchronizes on its own: automatic is the default policy. // onSync delivers every outcome to the program; this program queues them. BlockingQueue<SyncResult> laptopEvents = new LinkedBlockingQueue<>(); OpenOptions laptopOptions = OpenOptions.builder().onSync(laptopEvents::add).build(); OpenOptions phoneOptions = OpenOptions.builder().syncPolicy(SyncPolicy.manual()).build(); try (Database laptop = PotluckDB.open(scratch.resolve("laptop").toString(), remote, laptopOptions)) { laptop.transaction(setup -> { // one transaction: both statements travel in one push setup.execute("CREATE TABLE note(id INTEGER PRIMARY KEY, body TEXT NOT NULL)"); setup.execute("INSERT INTO note(body) VALUES(?)", Bindings.positional("buy milk")); return null; }); waitUntilPublished(laptop, laptopEvents);
// The phone synchronizes only when instructed: a manual policy, and sync() // calls that return the outcome as a value. It starts from the revision the // laptop published, so it opens after that revision exists. try (Database phone = PotluckDB.open(scratch.resolve("phone").toString(), remote, phoneOptions)) { Rows rows = phone.query("SELECT id, body FROM note ORDER BY id"); expect(rows.columns().equals(List.of("id", "body")), "columns are reported with the rows"); expect("buy milk".equals(rows.get(0).get("body")) && Long.valueOf(1).equals(rows.get(0).get(0)), "a row is read by column name or position");
// Both devices modify the same row before either has seen the other's write. laptop.execute("UPDATE note SET body = ? WHERE id = ?", Bindings.positional("buy oat milk", 1)); phone.execute("UPDATE note SET body = ? WHERE id = ?", Bindings.positional("buy milk and eggs", 1)); waitUntilPublished(laptop, laptopEvents);
// The phone's push finds a remote head it has not seen: // a Conflict, returned rather than thrown. expect(phone.sync() instanceof SyncResult.Conflict, "the phone's push is refused with a conflict"); ConflictResolution resolution = phone.resolveConflict(); // adopt the laptop's revision, replay the phone's statement on it expect(resolution.quarantined().isEmpty(), "no statement was quarantined"); expect(phone.sync() instanceof SyncResult.Pushed, "the phone's push succeeds after resolution");
// The laptop pulls the resolved revision on its own. // Both devices hold the same row. waitFor(laptopEvents, SyncResult.Pulled.class); for (Database db : List.of(laptop, phone)) { Rows current = db.query("SELECT body FROM note WHERE id = ?", Bindings.positional(1)); expect("buy milk and eggs".equals(current.get(0).get("body")), "both devices hold the same row"); }
// query reads, execute writes. The wrong call is refused before execution. try { laptop.execute("SELECT count(*) FROM note"); expect(false, "execute refuses a read"); } catch (PotluckDBException error) { expect("read_in_execute".equals(error.code()), "the refusal names the code"); System.out.println(error.action()); // Use query for this statement. } } } } finally { try (var paths = Files.walk(scratch)) { paths.sorted(Comparator.reverseOrder()).forEach(path -> path.toFile().delete()); } } System.out.println("quickstart: ok"); }}What the program demonstrates
Section titled “What the program demonstrates”The remote uses a directory. No server runs. Remote.fromDirectory uses a directory that stands in for a bucket in a storage service. The installation page shows the constructor for a storage service. These constructors support S3, Google Cloud Storage, and Azure Blob Storage. The rest of the program stays the same.
The laptop synchronizes automatically. Automatic synchronization is the default policy, which decides when to synchronize. The laptop never calls sync(). Its onSync hook, a function that receives synchronization results, reports every attempt. A failed result carries the error.
The hook runs on the synchronization thread. Transfer the outcome to your own thread, as shown with a queue.
| Result | Meaning |
|---|---|
SyncResult.UpToDate | The attempt found no pending writes and no remote revision to pull. |
SyncResult.Pushed | The attempt published its pending writes as a remote revision. |
SyncResult.Pulled | The attempt adopted a remote revision. It can also report replayed and quarantined transactions. |
SyncResult.Conflict | The device’s push met a remote revision that the device had not seen. |
SyncResult.Failed | An automatic attempt failed and carries the error. |
An automatic result covers one attempt. SyncResult.Pushed confirms the writes that the attempt published. Writes made after the attempt began can still be pending. If an earlier attempt failed, an explicit sync() first publishes that attempt’s writes and leaves later writes pending. A program that must know its own writes are published reads status().pendingWrites(). waitUntilPublished does that here before the second device opens.
Rows include their column names. rows.columns() lists the names. You can read a row by column name or position.
The phone receives a conflict result. sync() returns SyncResult.Conflict when the remote has an unseen revision. This result does not throw an exception. resolveConflict() adopts the laptop’s revision and replays the phone’s statement. The phone’s text then appears on both devices. The resolution lists any transactions that cannot replay safely as quarantined.
query reads and execute writes. Each accepts exactly one statement of its own kind. Each refuses the other kind before execution, with PotluckDBException whose action() names the required call.
These calls promise different things. query returns rows and uses the connection’s maximumRows to bound the result. A different SQL engine can answer a read. execute changes data that a revision publishes and other devices receive.
This execution-intent policy separates reads from writes. SQLite preparation can have effects, including under EXPLAIN.
Neither call accepts SQL transaction control, which starts, commits, or rolls back a transaction.
Use transaction for transaction control, as the setup shows.
Reference
Section titled “Reference”Open and construct and Database describe every call. Options and policies describes the automatic policy and its parameters. The examples provide longer programs that the test suite runs.