SPB Git

Repository Public

SyncDatabase.swift 11.6 KB 259 lines swift

1import Foundation23/// Names of the synchronized collections (the server only ever sees these names and opaque ids).4enum SyncCollection {5    static let history = "history", bookmarks = "bookmarks", favorites = "favorites", tabs = "tabs", closed = "closed"6    static let reading = "reading", searches = "searches", prefs = "prefs"7    /// Added in 3.0: synced Canvas boards (opt-in per board) and Portal hand-offs.8    static let canvas = "canvas", handoff = "handoff"9    static let all = [history, bookmarks, favorites, tabs, closed, reading, searches, prefs, canvas, handoff]10}1112struct SyncRecord: Equatable, Sendable {13    var collection: String14    /// Natural key on the device: a URL, a query, a UUID or a device id. It is never sent in the clear.15    var key: String16    var json: Data17    var modifiedAt: Date18    var deleted: Bool19    var serverSeq: Int20    var dirty: Bool21    var remoteID: String?22}2324/// The local, plaintext mirror of everything that can be synchronized (`sync.sqlite`, on this device only).25/// It doubles as an offline queue: local changes are flagged `dirty` and wait here until the server accepts them.26/// Favorites, reading list, search history, preferences and other devices' tabs live only here;27/// history and bookmarks are bridged from the profile database.28final class SyncDatabase: Sendable {29    let db: SQLiteDatabase3031    static let migrations: [[String]] = [[32        """33        CREATE TABLE records (34            collection TEXT NOT NULL,35            key TEXT NOT NULL,36            json TEXT NOT NULL DEFAULT '',37            modified_at REAL NOT NULL,38            deleted INTEGER NOT NULL DEFAULT 0,39            server_seq INTEGER NOT NULL DEFAULT 0,40            dirty INTEGER NOT NULL DEFAULT 0,41            remote_id TEXT,42            PRIMARY KEY (collection, key)43        )44        """,45        "CREATE INDEX records_dirty ON records(dirty) WHERE dirty = 1",46        "CREATE INDEX records_remote ON records(collection, remote_id)",47        "CREATE TABLE meta (key TEXT PRIMARY KEY, value TEXT NOT NULL)",48    ]]4950    init(url: URL?) throws {51        if let url { try FileManager.default.createDirectory(at: url.deletingLastPathComponent(), withIntermediateDirectories: true) }52        db = try SQLiteDatabase(path: url?.path)53        try db.migrate(Self.migrations)54    }5556    // MARK: Local writes (always flagged for upload)5758    func put(_ collection: String, key: String, json: Data, modifiedAt: Date = Date()) {59        _ = try? db.execute("""60            INSERT INTO records(collection, key, json, modified_at, deleted, dirty) VALUES(?,?,?,?,0,1)61            ON CONFLICT(collection, key) DO UPDATE SET json = excluded.json, modified_at = excluded.modified_at, deleted = 0, dirty = 162            """, [.text(collection), .text(key), .text(String(decoding: json, as: UTF8.self)), .double(modifiedAt.timeIntervalSince1970)])63    }6465    func put<T: Encodable>(_ collection: String, key: String, value: T, modifiedAt: Date = Date()) {66        guard let data = try? Self.encoder.encode(value) else { return }67        put(collection, key: key, json: data, modifiedAt: modifiedAt)68    }6970    /// Marks a record deleted. The tombstone stays until the server has it, so the deletion reaches other devices.71    func remove(_ collection: String, key: String, at date: Date = Date()) {72        _ = try? db.execute("UPDATE records SET deleted = 1, json = '', modified_at = ?, dirty = 1 WHERE collection = ? AND key = ? AND deleted = 0",73                            [.double(date.timeIntervalSince1970), .text(collection), .text(key)])74        // A record the server never saw can simply disappear.75        _ = try? db.execute("DELETE FROM records WHERE collection = ? AND key = ? AND deleted = 1 AND server_seq = 0 AND remote_id IS NULL", [.text(collection), .text(key)])76    }7778    func removeAll(_ collection: String, at date: Date = Date()) {79        for record in records(collection) { remove(collection, key: record.key, at: date) }80    }8182    // MARK: Reads8384    func record(_ collection: String, key: String) -> SyncRecord? {85        rows("SELECT * FROM records WHERE collection = ? AND key = ?", [.text(collection), .text(key)]).first86    }8788    /// Live (not deleted) records of a collection, newest first.89    func records(_ collection: String) -> [SyncRecord] {90        rows("SELECT * FROM records WHERE collection = ? AND deleted = 0 ORDER BY modified_at DESC", [.text(collection)])91    }9293    func values<T: Decodable>(_ collection: String, as type: T.Type = T.self) -> [T] {94        records(collection).compactMap { try? Self.decoder.decode(T.self, from: $0.json) }95    }9697    func value<T: Decodable>(_ collection: String, key: String, as type: T.Type = T.self) -> T? {98        guard let record = record(collection, key: key), !record.deleted else { return nil }99        return try? Self.decoder.decode(T.self, from: record.json)100    }101102    func dirty(limit: Int) -> [SyncRecord] { rows("SELECT * FROM records WHERE dirty = 1 ORDER BY modified_at ASC LIMIT ?", [.int(Int64(limit))]) }103    var dirtyCount: Int { Int((try? db.query("SELECT COUNT(*) AS n FROM records WHERE dirty = 1").first?["n"]?.int).flatMap { $0 } ?? 0) }104    func count(_ collection: String) -> Int {105        Int((try? db.query("SELECT COUNT(*) AS n FROM records WHERE collection = ? AND deleted = 0", [.text(collection)]).first?["n"]?.int).flatMap { $0 } ?? 0)106    }107108    func record(_ collection: String, remoteID: String) -> SyncRecord? {109        rows("SELECT * FROM records WHERE collection = ? AND remote_id = ?", [.text(collection), .text(remoteID)]).first110    }111112    // MARK: Sync bookkeeping113114    /// The server accepted our version: remember its sequence number, and clear the flag unless the record changed again meanwhile.115    func markPushed(_ pushed: SyncRecord, remoteID: String, seq: Int) {116        _ = try? db.execute("""117            UPDATE records SET server_seq = ?, remote_id = ?, dirty = CASE WHEN modified_at = ? AND deleted = ? THEN 0 ELSE 1 END118            WHERE collection = ? AND key = ?119            """, [.int(Int64(seq)), .text(remoteID), .double(pushed.modifiedAt.timeIntervalSince1970), .int(pushed.deleted ? 1 : 0), .text(pushed.collection), .text(pushed.key)])120        _ = try? db.execute("DELETE FROM records WHERE collection = ? AND key = ? AND deleted = 1 AND dirty = 0", [.text(pushed.collection), .text(pushed.key)])121    }122123    /// Stores a version that came from the server (or the result of a merge, flagged `dirty` when it must be sent back).124    func applyRemote(_ collection: String, key: String, json: Data, modifiedAt: Date, seq: Int, remoteID: String, dirty: Bool = false) {125        _ = try? db.execute("""126            INSERT INTO records(collection, key, json, modified_at, deleted, server_seq, dirty, remote_id) VALUES(?,?,?,?,0,?,?,?)127            ON CONFLICT(collection, key) DO UPDATE SET json = excluded.json, modified_at = excluded.modified_at, deleted = 0,128                server_seq = excluded.server_seq, dirty = excluded.dirty, remote_id = excluded.remote_id129            """, [.text(collection), .text(key), .text(String(decoding: json, as: UTF8.self)), .double(modifiedAt.timeIntervalSince1970),130                  .int(Int64(seq)), .int(dirty ? 1 : 0), .text(remoteID)])131    }132133    func applyRemoteDeletion(_ collection: String, key: String) {134        _ = try? db.execute("DELETE FROM records WHERE collection = ? AND key = ?", [.text(collection), .text(key)])135    }136137    /// Our write lost the race but should still win: keep it queued on top of the server's current version.138    func rebase(_ collection: String, key: String, seq: Int, remoteID: String) {139        _ = try? db.execute("UPDATE records SET server_seq = ?, remote_id = ?, dirty = 1 WHERE collection = ? AND key = ?",140                            [.int(Int64(seq)), .text(remoteID), .text(collection), .text(key)])141    }142143    /// Forgets everything known about the server (new account, new key, server data erased): every live record is queued again.144    func resetServerState() {145        _ = try? db.execute("DELETE FROM records WHERE deleted = 1")146        _ = try? db.execute("UPDATE records SET server_seq = 0, remote_id = NULL, dirty = 1")147        _ = try? db.execute("DELETE FROM meta WHERE key IN ('cursor', 'keyCheck')")148    }149150    /// Signing out with "remove data from this device": other devices' tabs go, local data stays but is no longer tied to the server.151    func forgetRemoteOnlyData() {152        _ = try? db.execute("DELETE FROM records WHERE collection IN ('tabs', 'closed')")153        resetServerState()154    }155156    func eraseEverything() {157        _ = try? db.execute("DELETE FROM records")158        _ = try? db.execute("DELETE FROM meta")159    }160161    func meta(_ key: String) -> String? { (try? db.query("SELECT value FROM meta WHERE key = ?", [.text(key)]).first?["value"]?.string).flatMap { $0 } }162    func setMeta(_ key: String, _ value: String?) {163        if let value {164            _ = try? db.execute("INSERT INTO meta(key, value) VALUES(?,?) ON CONFLICT(key) DO UPDATE SET value = excluded.value", [.text(key), .text(value)])165        } else {166            _ = try? db.execute("DELETE FROM meta WHERE key = ?", [.text(key)])167        }168    }169170    var cursor: Int {171        get { meta("cursor").flatMap(Int.init) ?? 0 }172        set { setMeta("cursor", String(newValue)) }173    }174175    private func rows(_ sql: String, _ params: [SQLValue]) -> [SyncRecord] {176        ((try? db.query(sql, params)) ?? []).map { row in177            SyncRecord(collection: row["collection"]?.string ?? "", key: row["key"]?.string ?? "", json: Data((row["json"]?.string ?? "").utf8),178                       modifiedAt: Date(timeIntervalSince1970: row["modified_at"]?.double ?? 0), deleted: (row["deleted"]?.int ?? 0) != 0,179                       serverSeq: Int(row["server_seq"]?.int ?? 0), dirty: (row["dirty"]?.int ?? 0) != 0, remoteID: row["remote_id"]?.string)180        }181    }182183    static let encoder: JSONEncoder = {184        let encoder = JSONEncoder()185        encoder.dateEncodingStrategy = .millisecondsSince1970186        encoder.outputFormatting = [.sortedKeys]187        return encoder188    }()189190    static let decoder: JSONDecoder = {191        let decoder = JSONDecoder()192        decoder.dateDecodingStrategy = .millisecondsSince1970193        return decoder194    }()195}196197// MARK: - Record types198199struct HistorySyncItem: Codable, Equatable, Sendable {200    var url: String201    var title: String202    var lastVisit: Date203    var visitCount: Int204}205206struct BookmarkSyncItem: Codable, Equatable, Sendable {207    var id: String208    var parentID: String?209    var isFolder: Bool210    var title: String211    var url: String?212    var tags: [String]213    var position: Int214    var createdAt: Date215}216217struct FavoriteItem: Codable, Equatable, Identifiable, Sendable {218    var id: String = UUID().uuidString219    var title: String220    var url: String221    var position: Int = 0222    var isPinned = false223}224225struct ReadingItem: Codable, Equatable, Identifiable, Sendable {226    var id: String { url }227    var url: String228    var title: String229    var addedAt: Date = Date()230    var isRead = false231}232233struct SearchHistoryItem: Codable, Equatable, Identifiable, Sendable {234    var id: String { query.lowercased() }235    var query: String236    var lastUsed: Date = Date()237    var count = 1238}239240struct RemoteTab: Codable, Equatable, Hashable, Sendable {241    var title: String242    var url: String243}244245/// The open (or recently closed) tabs of one device, as that device last published them.246struct DeviceTabs: Codable, Equatable, Identifiable, Sendable {247    var id: String { deviceID }248    var deviceID: String249    var deviceName: String250    var platform: String251    var tabs: [RemoteTab]252    var updatedAt: Date = Date()253}254255/// Preferences that follow the user from device to device.256struct SyncedPreferences: Codable, Equatable, Sendable {257    var search = SearchPreferences()258}259
Files main
Menu

Theme