Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / apple / Hub / IPCRelayStore.swift
IPCRelayStore.swift74 lines · 3.8 KiBRaw
| 1 | // EverySync Hub — `RelayStore` shim over the envelope IPC. |
| 2 | // |
| 3 | // The XCFramework's `SyncDriver.drive(kind, store, remote, append, now)` |
| 4 | // needs a `RelayStore<T>` (`listLocal / upsertLocal / deleteLocal / |
| 5 | // loadBaseline / saveSnapshots / removeSnapshots / listLocalOk / parseRemote / |
| 6 | // serializeForRemote`). On Apple the envelopes live in the App-Group SQLite |
| 7 | // IPC store (shared with the Passwords/Notes shells), so this file documents |
| 8 | // the exact adapter the Mac wires in Kotlin (see |
| 9 | // `../XCFramework/IOS_ACTUALS_NOTES.md` for the Kotlin owner): |
| 10 | // |
| 11 | // - `listLocal(vaultId)` = `EnvelopeIPC.querySince(vaultId, table, since: -1)` |
| 12 | // mapped through `*Kind.fromJson(payloadJson)`. ANY throw (SQLite error, |
| 13 | // coordination failure) = unhealthy read: the shim reports |
| 14 | // `listLocalOk == false` (or throws `LocalReadException`) so the driver |
| 15 | // ABORTS — never folds failure as empty (invariant 1+2, verbatim). |
| 16 | // Precedence: the ROW's `deleted` column wins over a stale `payloadJson` |
| 17 | // flag. A shell-side delete (`deleteByVault`) tombstones the row and stamps |
| 18 | // `updatedAt` but leaves the old payload bytes in place until the merge |
| 19 | // writes the tombstone envelope — so the shim MUST treat |
| 20 | // `row.deleted == true` as deleted regardless of what the payload says. |
| 21 | // (Same reason `querySince` includes tombstones: absence without a |
| 22 | // tombstone resurrects — never deletes.) |
| 23 | // - `parseRemote` = `*Kind.fromJson`; null skips a foreign/corrupt entry. |
| 24 | // - `serializeForRemote` = `*Kind.toJson` (decrypted form — encryption |
| 25 | // happens in the transport's `appendRecords`, never here). |
| 26 | // - `upsertLocal`/`deleteLocal` = `upsertRaw` / per-table delete semantics |
| 27 | // (passwords hard-delete, everything else tombstones — Swift holds no |
| 28 | // opinion beyond applying driver output). |
| 29 | // - Baselines (`RelaySnapshot`: syncId/kind/contentHash/localUpdatedAt/ |
| 30 | // remoteModified/lastSyncedAt/deleted) persist in a hub-side sidecar |
| 31 | // SQLite table (NOT in the IPC tables — the IPC columns are exactly the |
| 32 | // five `SyncSink` columns, no more). |
| 33 | // |
| 34 | // Swift never implements this interface directly (generics over Kotlin types |
| 35 | // cross the boundary poorly); the live shim is Kotlin. This file exists so |
| 36 | // the mapping is specified once, in the repo, next to the sinks. |
| 37 | |
| 38 | import Foundation |
| 39 | import EverysyncCore |
| 40 | |
| 41 | /// Which IPC table backs each vault type in the shim. |
| 42 | public enum RelayStoreTables { |
| 43 | /// `passwords:<vault>` <-> `passwords` table (HARD delete). |
| 44 | public static let passwords = EnvelopeTable.passwords |
| 45 | /// `notes:<vault>` <-> `notes` table (SOFT delete). |
| 46 | public static let notes = EnvelopeTable.notes |
| 47 | /// `todos:<vault>` <-> `todos` table (SOFT delete). |
| 48 | /// `notes` + `createdAt` MUST be persisted (envelope + hash inputs). |
| 49 | public static let todos = EnvelopeTable.todos |
| 50 | /// Hub-internal contacts/calendar rows: same five columns; delete |
| 51 | /// semantics per the XCFramework entity kind (Swift holds no opinion). |
| 52 | public static let contacts = EnvelopeTable.contacts |
| 53 | public static let calendars = EnvelopeTable.calendars |
| 54 | } |
| 55 | |
| 56 | /// Baseline sidecar: `RelaySnapshot` rows persisted per (vault, kind). |
| 57 | /// Schema (hub-local SQLite, NOT the IPC store): |
| 58 | /// `baselines(syncId TEXT, kind TEXT, contentHash TEXT, localUpdatedAt |
| 59 | /// INTEGER, remoteModified INTEGER, lastSyncedAt INTEGER, deleted INTEGER, |
| 60 | /// PRIMARY KEY (syncId, kind))`. |
| 61 | public enum BaselineSidecar { |
| 62 | public static let tableDDL = """ |
| 63 | CREATE TABLE IF NOT EXISTS baselines ( |
| 64 | syncId TEXT NOT NULL, |
| 65 | kind TEXT NOT NULL, |
| 66 | contentHash TEXT NOT NULL, |
| 67 | localUpdatedAt INTEGER NOT NULL, |
| 68 | remoteModified INTEGER NOT NULL, |
| 69 | lastSyncedAt INTEGER NOT NULL, |
| 70 | deleted INTEGER NOT NULL DEFAULT 0, |
| 71 | PRIMARY KEY (syncId, kind) |
| 72 | ); |
| 73 | """ |
| 74 | } |