diff --git a/Documentation/ExampleModel.png b/Documentation/ExampleModel.png new file mode 100644 index 0000000..eea1077 Binary files /dev/null and b/Documentation/ExampleModel.png differ diff --git a/Documentation/Plan.md b/Documentation/Plan.md index d447861..13c2f32 100644 --- a/Documentation/Plan.md +++ b/Documentation/Plan.md @@ -92,6 +92,7 @@ timers for `schedule`. | `LinuxBluetooth` | `Central`, `Peripheral`, `Service`, `Characteristic`, `Descriptor`, `Advertisement` over BlueZ | | `LinuxGraphics` | `Surface` over EGL and GLES 2.0, loaded at run time; the player's `GLSurface` view shows it (§7) | | `LinuxWebKit` | `WebContent` over WPE WebKit, loaded at run time; the player's `WebView` view shows it (§8) | +| `LinuxCoreModel` | `LinuxModelStore`: the model store over CoreModel-SQLite, through AppRuntime's `HostModelStore` (§9) | | `LinuxHostApp` | `main`: reads the app's keys, starts the host, and runs Cacao's SDL loop with one window showing the guest's first renderer | | `LinuxPackager`, `linux-app` | The packager (§11, [Packager.md](Packager.md)) | @@ -275,7 +276,7 @@ WebKit, and the player draws `WebView` nodes. It follows Perry's - Reading files beside a bundled page only: WebKit's file-URL rules apply. -## 9. Files and settings +## 9. App data: files, settings and model stores `LinuxFiles` is the `Files` contract (AppRuntime's `Files.md`): AppRuntime's `HostFiles`, which does it all over POSIX calls, with the app's directories @@ -311,6 +312,40 @@ it, rewritten atomically at each change. - **If it can't be opened**, the host logs why. Reads then find nothing, and writes are refused. +`LinuxModelStore`, in `LinuxCoreModel`, is the model store +(`Persistence.md`): the guest's CoreModel, through the `ModelStore` +contracts, to AppRuntime's `HostModelStore`, which turns them back into +CoreModel's requests for a backend. On Linux the backend is CoreModel's own +SQLite one, CoreModel-SQLite, with SQLite compiled in, so a device needs no +`libsqlite3`. + +- **A store** is `/Stores/.sqlite` in a container, and + `$XDG_DATA_HOME//Stores/.sqlite` otherwise: beside the app's + `Documents` and `Data`, out of reach of its `Files`. +- **The model** the store was made with is kept in it, in a table of its + own, so the model and the objects change together. An open with the same + model opens it. One that `canMigrate(from:to:)` allows (entities or + properties added or removed) is migrated by copying what both models + have into a new database, which then replaces the old one. Any other + fails with `migrationRequired`, or starts the store again with `.reset`. +- **One `SQLiteDatabase` per store**, whichever of the app's storages use + it, since it caches what it reads and must be its file's only writer. A + storage still open on a model that a later open migrated from fails with + `unavailable`: its file is gone. +- **A batch is one transaction**, in which each insert and delete is a + savepoint of its own: all of it, or none. +- **Requests answer as CoreModel's in-memory evaluation does**, which is + what defines them. Comparing the two found differences on both sides, + fixed in PureSwift/CoreModel-SQLite#14 and PureSwift/CoreModel#41, which + the package uses as branches until they're released. A predicate SQL + can't say is evaluated in memory, over the store's objects. +- **Indexes**: the app's, and one on each to-one relationship's column, + which its inverse to-many is read through. Those the app no longer + declares are dropped. +- **SQLite's errors** are the contract's: a full disk is `outOfSpace`, a + file that isn't a database `corrupted`, one that can't be opened or read + `unavailable`. + ## 10. Testing - `LinuxUI`: DarwinUI's tests, on Cacao (§5). @@ -322,6 +357,11 @@ it, rewritten atomically at each change. contract end to end in a container, answering on the main loop. - `LinuxHost`'s settings: the file's path from the environment, and settings read again at the next launch. +- `LinuxCoreModel`: where stores are; about 500 requests answered as + CoreModel's in-memory store answers them; every value there and back; + batches applied whole or not at all; a store kept between launches, + migrated, refused or emptied; indexes made and dropped; a file that isn't + a store; no directory. Runs on macOS and Linux. - `LinuxWebKit`: on Linux, in WPE: loading HTML and the events' order, scripts' JSON, navigations asked about and refused, back, bundle resources, a refused connection, remote loads without `network`, frames @@ -329,7 +369,9 @@ it, rewritten atomically at each change. on one thread, which WebKit takes for its main thread. - End to end: AppRuntime's `ExampleUI` built as a `linux-arm64` slice, linked into `linux-app-host` and run headless with SDL's `offscreen` - driver, its first frame saved with `CACAO_SCREENSHOT`. + driver, its first frame saved with `CACAO_SCREENSHOT`. `ExampleModel` + the same way, launched twice on one container, with to-dos written into + its SQLite database between the launches: the second shows them. - Real BlueZ needs hardware: manual runs on a Raspberry Pi or a laptop with `bluetoothd`. diff --git a/Documentation/PortMaster.md b/Documentation/PortMaster.md index 969cf76..12beaa7 100644 --- a/Documentation/PortMaster.md +++ b/Documentation/PortMaster.md @@ -50,7 +50,7 @@ host's options for a handheld: | `LINUXAPPRUNTIME_SCALE` | Pixels per point: 1 below 700 pixels tall, 1.5 below 1000, 2 above, so a 640 × 480 screen is 640 × 480 points and a 720 × 720 one 480 × 480 | | `LINUXAPPRUNTIME_CURSOR=1` | Draw a pointer: there's no mouse, and KMSDRM has no system cursor | | `LINUXAPPRUNTIME_FONTS` | `fonts/`, used when fontconfig isn't there | -| `LINUXAPPRUNTIME_CONTAINER` | `conf/`: the app's files and settings, where ports keep what's theirs | +| `LINUXAPPRUNTIME_CONTAINER` | `conf/`: the app's files, settings and model stores, where ports keep what's theirs | ## 3. Controls diff --git a/Package.swift b/Package.swift index 0d23048..ac7bbe9 100644 --- a/Package.swift +++ b/Package.swift @@ -72,6 +72,7 @@ let package = Package( .library(name: "LinuxBluetooth", targets: ["LinuxBluetooth"]), .library(name: "LinuxGraphics", targets: ["LinuxGraphics"]), .library(name: "LinuxWebKit", targets: ["LinuxWebKit"]), + .library(name: "LinuxCoreModel", targets: ["LinuxCoreModel"]), .library(name: "LinuxHostApp", targets: ["LinuxHostApp"]), .executable(name: "linux-app-host", targets: ["LinuxAppHost"]), .executable(name: "linux-app", targets: ["LinuxAppTool"]), @@ -85,6 +86,13 @@ let package = Package( .package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.5.0"), // SHA-256, for bundles' CodeResources; the D-Bus package uses it too. .package(url: "https://github.com/apple/swift-crypto.git", "3.0.0"..<"5.0.0"), + // Model stores: CoreModel's SQLite backend, and the SQLite it's + // over, which is compiled in on Linux. Both masters, until they're + // released: they answer requests as CoreModel's in-memory evaluation + // does (PureSwift/CoreModel-SQLite#14, PureSwift/CoreModel#41). + .package(url: "https://github.com/PureSwift/CoreModel-SQLite.git", branch: "master"), + .package(url: "https://github.com/PureSwift/CoreModel.git", branch: "master"), + .package(url: "https://github.com/PureSwift/SQLite.git", from: "0.2.2"), ], targets: [ @@ -138,13 +146,28 @@ let package = Package( swiftSettings: host ), + // MARK: - Model stores + + // The guest's CoreModel, through the ModelStore contracts, to + // CoreModel-SQLite. + .target( + name: "LinuxCoreModel", + dependencies: [ + "LinuxHost", runtime("DynamicCoreModel"), runtime("RuntimeCoreModel"), runtime("DynamicFiles"), runtime("DynamicRuntime"), + runtime("RuntimeABI"), runtime("RuntimeHost"), + .product(name: "CoreModelSQLite", package: "CoreModel-SQLite"), + .product(name: "SQLite", package: "SQLite"), + ], + swiftSettings: host + ), + // MARK: - App // libc functions app slices need that older glibc lacks. .target(name: "CLinuxHost"), .target( name: "LinuxHostApp", - dependencies: ["LinuxHost", "LinuxUI", "LinuxBluetooth", "LinuxGraphics", "LinuxWebKit", "CLinuxHost", runtime("CRuntime"), + dependencies: ["LinuxHost", "LinuxUI", "LinuxBluetooth", "LinuxGraphics", "LinuxWebKit", "LinuxCoreModel", "CLinuxHost", runtime("CRuntime"), .product(name: "CacaoSDL", package: "Cacao")], swiftSettings: host ), @@ -166,7 +189,7 @@ let package = Package( // doesn't build Cacao. .target( name: "LinuxPackager", - dependencies: ["LinuxHost", "LinuxBluetooth", "LinuxGraphics", "LinuxWebKit", runtime("DynamicUI"), + dependencies: ["LinuxHost", "LinuxBluetooth", "LinuxGraphics", "LinuxWebKit", "LinuxCoreModel", runtime("DynamicUI"), .product(name: "Crypto", package: "swift-crypto")] ), .executableTarget( @@ -184,6 +207,12 @@ let package = Package( ), .testTarget(name: "LinuxBluetoothTests", dependencies: ["LinuxBluetooth"], swiftSettings: host), .testTarget(name: "LinuxGraphicsTests", dependencies: ["LinuxGraphics", runtime("DynamicGraphics")], swiftSettings: host), + .testTarget( + name: "LinuxCoreModelTests", + dependencies: ["LinuxCoreModel", runtime("DynamicCoreModel"), runtime("RuntimeCoreModel"), + .product(name: "CoreModelSQLite", package: "CoreModel-SQLite")], + swiftSettings: host + ), .testTarget(name: "LinuxWebKitTests", dependencies: ["LinuxWebKit", runtime("DynamicWebKit")], swiftSettings: host), .testTarget(name: "LinuxPackagerTests", dependencies: ["LinuxPackager", "LinuxHostApp"]), ] diff --git a/README.md b/README.md index 82c9c86..44a7574 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,12 @@ in its `documents`: ![ExampleFiles on Linux](Documentation/ExampleFiles.png) +`ExampleModel` keeps to-do lists in a model store with CoreModel, kept in +SQLite. At its second launch it reads back what's there, here two to-dos +added to its database between launches: + +![ExampleModel on Linux](Documentation/ExampleModel.png) + ## Modules | Module | | @@ -50,6 +56,7 @@ in its `documents`: | `LinuxBluetooth` | `Central`, `Peripheral`, `Service`, `Characteristic`, `Descriptor` and `Advertisement` over BlueZ, through `wendylabsinc/dbus` | | `LinuxGraphics` | `Surface`: GLES 2.0 drawn in EGL pbuffers, loaded at run time, and read back into the player's `GLSurface` view ([plan](Documentation/Plan.md) §7) | | `LinuxWebKit` | `WebContent`: WPE WebKit pages, loaded at run time, drawn into shared memory and shown by the player's `WebView` view ([plan](Documentation/Plan.md) §8) | +| `LinuxCoreModel` | `LinuxModelStore`: the app's CoreModel stores in SQLite, through AppRuntime's `HostModelStore` and CoreModel-SQLite ([plan](Documentation/Plan.md) §9) | | `LinuxHostApp`, `linux-app-host` | The app's `main`: the host, and Cacao's window and loop | | `LinuxPackager`, `linux-app` | The packager: validation, the host build, wasm2c, the install tree, PortMaster ports | @@ -149,7 +156,7 @@ The bundle needs `linux-arm64` and `linux-armv7` slices (`guest.py bundle | Variable | | |---|---| | `LINUXAPPRUNTIME_DEBUG` | `1` logs the host's debug messages | -| `LINUXAPPRUNTIME_CONTAINER` | An absolute path that holds the app's files (`Documents`, `Data`, `Caches`, `tmp`) and settings (`Defaults`), in place of XDG's directories | +| `LINUXAPPRUNTIME_CONTAINER` | An absolute path that holds the app's files (`Documents`, `Data`, `Caches`, `tmp`) settings (`Defaults`) and model stores (`Stores`), in place of XDG's directories | | `CACAO_SCREENSHOT` | Saves the first frame to a PNG and quits | | `CACAO_SHOW_STATISTICS` | `1` shows Cacao's frame times | | `SDL_VIDEO_DRIVER` | `wayland`, `x11`, `kmsdrm`, or `offscreen` for no display | diff --git a/Sources/LinuxCoreModel/LinuxModelStore.swift b/Sources/LinuxCoreModel/LinuxModelStore.swift new file mode 100644 index 0000000..fa23060 --- /dev/null +++ b/Sources/LinuxCoreModel/LinuxModelStore.swift @@ -0,0 +1,53 @@ +import DynamicFiles +import DynamicRuntime +import Foundation +import LinuxHost +import RuntimeABI +import RuntimeCoreModel +import RuntimeHost + +/// The model store (AppRuntime's `Persistence.md`): the guest's CoreModel +/// through the `ModelStore` contracts to `RuntimeCoreModel`'s +/// `HostModelStore`, over CoreModel-SQLite (`Plan.md` §9). +/// +/// | `LINUXAPPRUNTIME_CONTAINER` set | Otherwise (XDG) | +/// |---|---| +/// | `/Stores/.sqlite` | `$XDG_DATA_HOME//Stores/.sqlite` | +/// +/// Beside the app's `Documents` and `Data` (`LinuxFiles`), but not in +/// them: the app's files can't reach its stores. +public enum LinuxModelStore: HostModule { + + public static var contracts: [ContractDescriptor] { + HostModelStoreModule.contracts + } + + /// Says where the app's stores are, then registers the contracts. + /// Without a directory, `open` fails with `unavailable`. + public static func register(in registrar: some ContractRegistrar) { + do { + let directory = try self.directory(identifier: LinuxBundle.info.identifier, environment: LinuxFiles.environment) + HostModelStore.configure(HostModelStore.Configuration( + provider: SQLiteModelStorageProvider(directory: directory), + deliver: deliver)) + HostLog.debug("model stores: \(directory)") + } catch { + HostLog.error("model stores: \(error)") + } + HostModelStoreModule.register(in: registrar) + } + + /// Answers go to the main loop, from the stores' tasks. + static let deliver: @Sendable (@escaping @Sendable () -> Void) -> Void = { answer in + MainLoop.shared.post(answer) + } + + /// The directory of the app `identifier`'s stores: beside its + /// documents. + public static func directory(identifier: String, environment: [String: String]) throws(LinuxFilesError) -> String { + let directories = try LinuxFiles.directories(identifier: identifier, resources: nil, environment: environment) + // `documents` is always there; it's `/Documents`. + let documents = directories[.documents]! + return String(documents.dropLast("/Documents".count)) + "/Stores" + } +} diff --git a/Sources/LinuxCoreModel/SQLiteModelStorage.swift b/Sources/LinuxCoreModel/SQLiteModelStorage.swift new file mode 100644 index 0000000..6901ede --- /dev/null +++ b/Sources/LinuxCoreModel/SQLiteModelStorage.swift @@ -0,0 +1,155 @@ +import CoreModel +import CoreModelSQLite +import DynamicCoreModel +import Foundation +import RuntimeCoreModel +import SQLite + +/// An open store: CoreModel-SQLite's `SQLiteDatabase`, with what +/// `HostModelStore` asks of a backend besides. +/// +/// - **Batches are atomic**: one transaction, in which each insert and +/// delete makes a savepoint of its own. +/// - **What SQL can't say is evaluated here**: a predicate CoreModel-SQLite +/// can't translate is answered by CoreModel's own evaluator, over the +/// store's objects, so it answers as the in-memory store does. +/// - **SQLite's errors** are the contract's: a full disk is `outOfSpace`, +/// a file that isn't a database `corrupted`. +/// +/// - **Once its store is migrated, emptied or destroyed**, it's closed: +/// every operation fails with `unavailable`, since its file is gone. +/// +/// `HostModelStore` gives it one operation at a time, so a batch's +/// transaction has the connection to itself. +public final class SQLiteModelStorage: AtomicModelStorage, @unchecked Sendable { + + public let database: SQLiteDatabase + + private let lock = NSLock() + + private var isClosed = false + + init(database: SQLiteDatabase) { + self.database = database + } + + /// Fails every operation from here on. + func close() { + lock.withLock { isClosed = true } + } + + public func fetch(_ entity: EntityName, for id: ObjectID) async throws -> ModelData? { + try await mapped { try await database.fetch(entity, for: id) } + } + + public func fetch(_ fetchRequest: FetchRequest) async throws -> [ModelData] { + try await mapped { + do { + return try await database.fetch(fetchRequest) + } catch SQLiteDatabaseError.invalidPredicate { + return try await evaluated(fetchRequest) + } + } + } + + public func fetchID(_ fetchRequest: FetchRequest) async throws -> [ObjectID] { + try await mapped { + do { + return try await database.fetchID(fetchRequest) + } catch SQLiteDatabaseError.invalidPredicate { + return try await evaluated(fetchRequest).map(\.id) + } + } + } + + public func count(_ fetchRequest: FetchRequest) async throws -> UInt { + try await mapped { + do { + return try await database.count(fetchRequest) + } catch SQLiteDatabaseError.invalidPredicate { + return UInt(try await evaluated(fetchRequest).count) + } + } + } + + public func insert(_ value: ModelData) async throws { + try await mapped { try await database.insert(value) } + } + + public func insert(_ values: [ModelData]) async throws { + try await mapped { try await database.insert(values) } + } + + public func delete(_ entity: EntityName, for id: ObjectID) async throws { + try await mapped { try await database.delete(entity, for: id) } + } + + public func delete(_ entity: EntityName, for ids: [ObjectID]) async throws { + try await mapped { try await database.delete(entity, for: ids) } + } + + public func register(function: DatabaseFunction) async throws { + try await mapped { try await database.register(function: function) } + } + + public func perform(_ changes: ModelChanges) async throws { + try await mapped { + try await database.execute("BEGIN") + do { + for change in changes.changes { + switch change { + case .insert(let value): + try await database.insert(value) + case .delete(let entity, let id): + try await database.delete(entity, for: id) + } + } + try await database.execute("COMMIT") + } catch { + try? await database.execute("ROLLBACK") + throw error + } + } + } + + /// The request answered by CoreModel's evaluator, over every object in + /// the store, since its key paths may go through relationships. + private func evaluated(_ request: FetchRequest) async throws -> [ModelData] { + var objects = [ModelData]() + for entity in await database.model.entities { + objects += try await database.fetch(FetchRequest(entity: entity.id)) + } + return request.evaluate(objects) + } + + private func mapped(_ body: () async throws -> T) async throws -> T { + guard !lock.withLock({ isClosed }) else { + throw ModelStoreError(.unavailable, message: "the store was migrated, emptied or destroyed since this storage opened it") + } + do { + return try await body() + } catch { + throw Self.failure(error) + } + } + + /// The contract's error for one of SQLite's; any other error as it is. + static func failure(_ error: any Error) -> any Error { + guard let error = error as? SQLiteError else { + return error + } + let message = error.message + // The primary result code (https://sqlite.org/rescode.html). + switch error.errorCode.rawValue & 0xff { + case 13: // SQLITE_FULL + return ModelStoreError(.outOfSpace, message: message) + case 11, 26: // SQLITE_CORRUPT, SQLITE_NOTADB + return ModelStoreError(.corrupted, message: message) + case 8, 10, 14: // SQLITE_READONLY, SQLITE_IOERR, SQLITE_CANTOPEN + // Also a storage whose file a migration replaced. + return ModelStoreError(.unavailable, message: message) + default: + return ModelStoreError(.unknown, message: message) + } + } +} diff --git a/Sources/LinuxCoreModel/SQLiteModelStorageProvider.swift b/Sources/LinuxCoreModel/SQLiteModelStorageProvider.swift new file mode 100644 index 0000000..c609141 --- /dev/null +++ b/Sources/LinuxCoreModel/SQLiteModelStorageProvider.swift @@ -0,0 +1,259 @@ +import CoreModel +import CoreModelSQLite +import DynamicCoreModel +import Foundation +import RuntimeCoreModel +import SQLite + +/// Model stores as SQLite databases in one directory: CoreModel-SQLite's +/// `SQLiteDatabase` behind `RuntimeCoreModel`'s `HostModelStore` +/// (`Plan.md` §9). +/// +/// The store `name` is `/.sqlite`. It keeps the model it +/// was made with in a table of its own, so the model and the objects +/// change together, and an open compares the two (AppRuntime's +/// `Persistence.md`, Migration): +/// +/// - the same model opens the store as it is; +/// - one that `InMemoryModelStorageProvider.canMigrate(from:to:)` allows +/// is migrated by copying what both models have into a new database, +/// which then replaces the old one; +/// - any other fails with `migrationRequired`, or with `.reset` starts the +/// store again, empty. +/// +/// A store open in several of the app's storages is one `SQLiteDatabase`, +/// since it caches what it reads and must be its file's only writer. +public actor SQLiteModelStorageProvider: ModelStorageProvider { + + public nonisolated let directory: String + + private var databases: [String: (model: Model, storage: SQLiteModelStorage)] = [:] + + public init(directory: String) { + self.directory = directory + } + + /// Where the store `name` is. + public nonisolated func path(_ name: String) -> String { + directory + "/" + name + ".sqlite" + } + + public func open(_ name: String, model: Model, indexes: [EntityName: [[PropertyKey]]], migration: ModelMigration) async throws -> any ModelStorage { + do { + let storage = try await self.storage(name, model: model, migration: migration) + try Self.updateIndexes(at: path(name), model: model, declared: indexes) + return storage + } catch { + throw SQLiteModelStorage.failure(error) + } + } + + public func destroy(_ name: String) async throws { + databases.removeValue(forKey: name)?.storage.close() + let path = path(name) + Self.remove(path) + Self.remove(Self.migrating(path)) + } + + // MARK: Opening + + private func storage(_ name: String, model: Model, migration: ModelMigration) async throws -> SQLiteModelStorage { + let model = Self.canonical(model) + if let open = databases[name], open.model == model { + return open.storage + } + let path = path(name) + do { + try FileManager.default.createDirectory(atPath: directory, withIntermediateDirectories: true) + } catch { + throw ModelStoreError(.unavailable, message: "can't make \(directory): \(error)") + } + let stored = FileManager.default.fileExists(atPath: path) ? try Self.storedModel(at: path) : nil + let storage: SQLiteModelStorage + if let stored, stored == model { + storage = try SQLiteModelStorage(database: SQLiteDatabase(path: path, model: model)) + } else if let stored, InMemoryModelStorageProvider.canMigrate(from: stored, to: model) { + try await migrate(name, from: stored, to: model) + storage = try SQLiteModelStorage(database: SQLiteDatabase(path: path, model: model)) + } else if stored != nil, migration == .fail { + // A refused open changes nothing. + throw ModelStoreError(.migrationRequired, message: "the store \(name) was made with another model") + } else { + // New, or emptied for a model it can't be migrated to. + databases.removeValue(forKey: name)?.storage.close() + Self.remove(path) + storage = try SQLiteModelStorage(database: Self.create(at: path, model: model)) + } + databases[name] = (model, storage) + return storage + } + + /// Copies what both models have into a new database, which replaces + /// the store at `name`'s path once it's complete. + private func migrate(_ name: String, from stored: Model, to model: Model) async throws { + let path = path(name) + let migrating = Self.migrating(path) + Self.remove(migrating) + do { + // An open store is its file's writer; otherwise it's opened + // only to be read. + let old = try databases[name]?.storage.database ?? SQLiteDatabase(path: path, model: stored) + try await Self.copy(from: old, to: migrating, model: model, stored: stored) + } catch { + Self.remove(migrating) + throw error + } + // Storages still open on the old model lose their file, and fail + // with `unavailable` from here on. + databases.removeValue(forKey: name)?.storage.close() + guard rename(migrating, path) == 0 else { + let failure = ModelStoreError(.unavailable, message: "can't replace \(path): \(String(cString: strerror(errno)))") + Self.remove(migrating) + throw failure + } + Self.removeJournals(path) + } + + /// Copies the objects of `old` that `model` has into a new database at + /// `path`, with only the properties `model` has. The database is closed + /// when this returns. + private static func copy(from old: SQLiteDatabase, to path: String, model: Model, stored: Model) async throws { + let new = try create(at: path, model: model) + try await new.execute("BEGIN") + do { + for entity in model.entities where stored[entity.id] != nil { + var objects = try await old.fetch(FetchRequest(entity: entity.id)) + for index in objects.indices { + objects[index].attributes = objects[index].attributes.filter { entity[attribute: $0.key] != nil } + objects[index].relationships = objects[index].relationships.filter { entity[relationship: $0.key] != nil } + } + try await new.insert(objects) + } + try await new.execute("COMMIT") + } catch { + try? await new.execute("ROLLBACK") + throw error + } + } + + /// A new database at `path` for `model`, which it keeps. + private static func create(at path: String, model: Model) throws -> SQLiteDatabase { + let connection = try Connection(path: path) + let json = String(decoding: try JSONEncoder.sorted.encode(model), as: UTF8.self) + try connection.transaction { + try connection.run("CREATE TABLE IF NOT EXISTS \(modelTable.quoted) (\("model".quoted) TEXT NOT NULL)") + try connection.run("DELETE FROM \(modelTable.quoted)") + try connection.run("INSERT INTO \(modelTable.quoted) (\("model".quoted)) VALUES (?)", [.text(json)]) + } + return try SQLiteDatabase(connection: connection, model: model) + } + + /// The model the store at `path` was made with; `nil` for a file that + /// has none, such as one left before its model was written. + static func storedModel(at path: String) throws -> Model? { + let connection = try Connection(path: path) + let tables = try connection.scalar("SELECT count(*) FROM sqlite_master WHERE type = 'table' AND name = ?", [.text(modelTable)]) + guard let count = tables?.integer, count > 0 else { + return nil + } + guard let json = try connection.scalar("SELECT \("model".quoted) FROM \(modelTable.quoted) LIMIT 1")?.string else { + return nil + } + do { + return canonical(try JSONDecoder().decode(Model.self, from: Data(json.utf8))) + } catch { + throw ModelStoreError(.corrupted, message: "the store's model doesn't decode: \(error)") + } + } + + // MARK: Indexes + + /// The table the store's model is kept in. Not an entity's: CoreModel's + /// entity names don't have a `.`. + static let modelTable = "appruntime.model" + + /// The prefix of the indexes this keeps. + static let indexPrefix = "appruntime.index." + + /// Makes the indexes the app declared, and one on each to-one + /// relationship's column, which a to-many is read through; drops those + /// it no longer wants. + static func updateIndexes(at path: String, model: Model, declared: [EntityName: [[PropertyKey]]]) throws { + var wanted: [String: String] = [:] + for entity in model.entities { + var lists = declared[entity.id] ?? [] + lists += entity.relationships.filter { $0.type == .toOne }.map { [$0.id] } + for keys in lists { + // Only columns can be indexed: attributes and to-ones. + let columns = keys.allSatisfy { key in + entity[attribute: key] != nil || entity[relationship: key]?.type == .toOne + } + guard columns, !keys.isEmpty else { continue } + let name = indexPrefix + entity.id.rawValue + "." + keys.map(\.rawValue).joined(separator: ",") + wanted[name] = "CREATE INDEX IF NOT EXISTS \(name.quoted) ON \(entity.id.rawValue.quoted) (\(keys.map(\.rawValue.quoted).joined(separator: ", ")))" + } + } + let connection = try Connection(path: path) + let list = try connection.scalar( + "SELECT group_concat(name, char(10)) FROM sqlite_master WHERE type = 'index' AND substr(name, 1, ?) = ?", + [.integer(Int64(indexPrefix.utf8.count)), .text(indexPrefix)]) + let existing = Set((list?.string ?? "").split(separator: "\n").map(String.init)) + try connection.transaction { + for name in existing where wanted[name] == nil { + try connection.run("DROP INDEX IF EXISTS \(name.quoted)") + } + for (name, sql) in wanted where !existing.contains(name) { + try connection.run(sql) + } + } + } + + // MARK: Files + + static func migrating(_ path: String) -> String { + path + ".migrating" + } + + /// Removes a database and whatever SQLite keeps beside it. + static func remove(_ path: String) { + unlink(path) + removeJournals(path) + } + + static func removeJournals(_ path: String) { + for suffix in ["-journal", "-wal", "-shm"] { + unlink(path + suffix) + } + } + + /// A model with its entities and properties in one order, so two that + /// say the same thing are equal. + static func canonical(_ model: Model) -> Model { + Model(entities: model.entities + .map { entity in + EntityDescription( + id: entity.id, + attributes: entity.attributes.sorted { $0.id.rawValue < $1.id.rawValue }, + relationships: entity.relationships.sorted { $0.id.rawValue < $1.id.rawValue } + ) + } + .sorted { $0.id.rawValue < $1.id.rawValue }) + } +} + +extension String { + + /// A quoted SQL identifier. + var quoted: String { + "\"" + replacingOccurrences(of: "\"", with: "\"\"") + "\"" + } +} + +extension JSONEncoder { + + static var sorted: JSONEncoder { + let encoder = JSONEncoder() + encoder.outputFormatting = [.sortedKeys] + return encoder + } +} diff --git a/Sources/LinuxHostApp/HostApp.swift b/Sources/LinuxHostApp/HostApp.swift index 68bf780..0888cc5 100644 --- a/Sources/LinuxHostApp/HostApp.swift +++ b/Sources/LinuxHostApp/HostApp.swift @@ -4,6 +4,7 @@ import CLinuxHost import CacaoSDL import Foundation import LinuxBluetooth +import LinuxCoreModel import LinuxGraphics import LinuxWebKit import LinuxUI @@ -26,9 +27,10 @@ public enum HostApp { /// Every module this host has: the SwiftUI player (DarwinUI, on Cacao), /// Bluetooth, GL surfaces and web pages, which the player shows, and - /// the app's files and settings. + /// the app's files, settings and model stores. public static let modules: [any HostModule.Type] = [ DarwinUI.self, LinuxBluetooth.self, LinuxGraphics.self, LinuxWebKit.self, LinuxFiles.self, LinuxDefaults.self, + LinuxModelStore.self, ] /// The window's title: the app's name. diff --git a/Sources/LinuxPackager/Validator.swift b/Sources/LinuxPackager/Validator.swift index 19c74b8..313d471 100644 --- a/Sources/LinuxPackager/Validator.swift +++ b/Sources/LinuxPackager/Validator.swift @@ -1,6 +1,7 @@ import DynamicRuntime import Foundation import LinuxBluetooth +import LinuxCoreModel import LinuxGraphics import LinuxWebKit import LinuxHost @@ -12,9 +13,10 @@ public enum HostContracts { /// The modules `linux-app-host` registers (`HostApp.modules`): the /// player's contracts, without building the player, Bluetooth, - /// graphics, web views, files and settings. + /// graphics, web views, files, settings and model stores. nonisolated(unsafe) public static let modules: [any HostModule.Type] = [ PlayerContracts.self, LinuxBluetooth.self, LinuxGraphics.self, LinuxWebKit.self, LinuxFiles.self, LinuxDefaults.self, + LinuxModelStore.self, ] public static var provided: [String: ContractDescriptor] { diff --git a/Tests/LinuxCoreModelTests/LinuxCoreModelTests.swift b/Tests/LinuxCoreModelTests/LinuxCoreModelTests.swift new file mode 100644 index 0000000..53d91a9 --- /dev/null +++ b/Tests/LinuxCoreModelTests/LinuxCoreModelTests.swift @@ -0,0 +1,425 @@ +import CoreModel +import CoreModelSQLite +import DynamicCoreModel +import Foundation +import RuntimeCoreModel +import SQLite +import Testing +@testable import LinuxCoreModel + +// MARK: The model + +/// Written by hand, as a guest writes them: CoreModel's macros are off +/// under Embedded Swift. +struct Person: Entity, Equatable { + + static var entityName: EntityName { "Person" } + + static var attributes: [CodingKeys: AttributeType] { + [ + .name: .string, .age: .int32, .rank: .int16, .visits: .int64, .ratio: .float, .score: .double, + .isMember: .bool, .joined: .date, .token: .uuid, .homepage: .url, .balance: .decimal, .photo: .data, + ] + } + + static var relationships: [CodingKeys: Relationship] { + [.team: Relationship(id: "team", type: .toOne, destinationEntity: "Team", inverseRelationship: "members")] + } + + enum CodingKeys: String, CodingKey { + case id, name, age, rank, visits, ratio, score, isMember, joined, token, homepage, balance, photo, team + } + + let id: String + var name: String + var age: Int32 + var rank: Int16 = 0 + var visits: Int64 = 0 + var ratio: Float = 0 + var score: Double = 0 + var isMember = false + var joined: Date? + var token: UUID? + var homepage: URL? + var balance: Decimal? + var photo: Data? + var team: String? + + init(id: String, name: String, age: Int32, score: Double = 0, isMember: Bool = false, team: String? = nil) { + self.id = id + self.name = name + self.age = age + self.score = score + self.isMember = isMember + self.team = team + } + + init(from model: ModelData) throws(ModelDataDecodingError) { + id = model.id.rawValue + name = try model.decode(String.self, forKey: CodingKeys.name) + age = try model.decode(Int32.self, forKey: CodingKeys.age) + rank = try model.decode(Int16.self, forKey: CodingKeys.rank) + visits = try model.decode(Int64.self, forKey: CodingKeys.visits) + ratio = try model.decode(Float.self, forKey: CodingKeys.ratio) + score = try model.decode(Double.self, forKey: CodingKeys.score) + isMember = try model.decode(Bool.self, forKey: CodingKeys.isMember) + joined = try model.decode(Date?.self, forKey: CodingKeys.joined) + token = try model.decode(UUID?.self, forKey: CodingKeys.token) + homepage = try model.decode(URL?.self, forKey: CodingKeys.homepage) + balance = try model.decode(Decimal?.self, forKey: CodingKeys.balance) + photo = try model.decode(Data?.self, forKey: CodingKeys.photo) + team = try model.decodeRelationship(String?.self, forKey: CodingKeys.team) + } + + func encode() -> ModelData { + var data = ModelData(entity: Self.entityName, id: ObjectID(rawValue: id)) + data.encode(name, forKey: CodingKeys.name) + data.encode(age, forKey: CodingKeys.age) + data.encode(rank, forKey: CodingKeys.rank) + data.encode(visits, forKey: CodingKeys.visits) + data.encode(ratio, forKey: CodingKeys.ratio) + data.encode(score, forKey: CodingKeys.score) + data.encode(isMember, forKey: CodingKeys.isMember) + data.encode(joined, forKey: CodingKeys.joined) + data.encode(token, forKey: CodingKeys.token) + data.encode(homepage, forKey: CodingKeys.homepage) + data.encode(balance, forKey: CodingKeys.balance) + data.encode(photo, forKey: CodingKeys.photo) + data.encodeRelationship(team, forKey: CodingKeys.team) + return data + } +} + +struct Team: Entity, Equatable { + + static var entityName: EntityName { "Team" } + + static var attributes: [CodingKeys: AttributeType] { [.name: .string] } + + static var relationships: [CodingKeys: Relationship] { + [.members: Relationship(id: "members", type: .toMany, destinationEntity: "Person", inverseRelationship: "team")] + } + + enum CodingKeys: String, CodingKey { + case id, name, members + } + + let id: String + var name: String + var members: [String] = [] + + init(id: String, name: String) { + self.id = id + self.name = name + } + + init(from model: ModelData) throws(ModelDataDecodingError) { + id = model.id.rawValue + name = try model.decode(String.self, forKey: CodingKeys.name) + members = try model.decodeRelationship([String].self, forKey: CodingKeys.members).sorted() + } + + func encode() -> ModelData { + var data = ModelData(entity: Self.entityName, id: ObjectID(rawValue: id)) + data.encode(name, forKey: CodingKeys.name) + return data + } +} + +/// The SQLite stores, each test in a directory of its own, against +/// CoreModel's in-memory store as the reference for what a request +/// returns. +@Suite struct LinuxCoreModelTests { + + static let model = Model(entities: [ + EntityDescription(entity: Person.self), + EntityDescription(entity: Team.self), + EntityDescription(id: "Note", attributes: [Attribute(id: "text", type: .string)], relationships: []), + ]) + + static let people: [Person] = { + var ada = Person(id: "ada", name: "Ada", age: 36, score: 9.5, isMember: true, team: "engine") + ada.rank = -3 + ada.visits = 1 << 40 + ada.ratio = 0.25 + ada.joined = Date(timeIntervalSince1970: 1_700_000_000.5) + ada.token = UUID(uuidString: "9E3C1A52-0C1B-4F0E-9D4B-6A1F2C3D4E5F") + ada.homepage = URL(string: "https://example.com/ada?x=1") + ada.balance = Decimal(string: "1234.5678") + ada.photo = Data([0, 1, 2, 255]) + return [ + ada, + Person(id: "grace", name: "Grace", age: 45, score: 8, isMember: true, team: "engine"), + Person(id: "alan", name: "alan", age: 41, score: 7.25, team: "theory"), + Person(id: "edsger", name: "Édsger", age: 17, score: 0), + Person(id: "linus", name: "Linus", age: 17, score: 3, isMember: true, team: "theory"), + ] + }() + + static var objects: [ModelData] { + [Team(id: "engine", name: "Engine").encode(), Team(id: "theory", name: "Theory").encode()] + people.map { $0.encode() } + } + + let directory = FileManager.default.temporaryDirectory.appendingPathComponent("linux-core-model-\(UUID())").path + + func provider() -> SQLiteModelStorageProvider { + SQLiteModelStorageProvider(directory: directory) + } + + func open(_ provider: SQLiteModelStorageProvider, _ model: Model = LinuxCoreModelTests.model, indexes: [EntityName: [[PropertyKey]]] = [:], + migration: ModelMigration = .fail) async throws -> SQLiteModelStorage { + try #require(try await provider.open("library", model: model, indexes: indexes, migration: migration) as? SQLiteModelStorage) + } + + /// What a failing call failed with; `nil` when it didn't. + func failure(_ body: () async throws -> Void) async -> ModelStoreError.Code? { + do { + try await body() + return nil + } catch { + return (error as? ModelStoreError)?.code + } + } + + // MARK: Where + + @Test func storesAreBesideTheAppsDocuments() throws { + #expect(try LinuxModelStore.directory(identifier: "org.example.App", environment: ["HOME": "/home/ada"]) + == "/home/ada/.local/share/org.example.App/Stores") + #expect(try LinuxModelStore.directory(identifier: "org.example.App", environment: ["HOME": "/home/ada", "XDG_DATA_HOME": "/data"]) + == "/data/org.example.App/Stores") + #expect(try LinuxModelStore.directory(identifier: "org.example.App", environment: ["LINUXAPPRUNTIME_CONTAINER": "/roms/ports/app/conf"]) + == "/roms/ports/app/conf/Stores") + #expect(throws: (any Error).self) { + try LinuxModelStore.directory(identifier: "../x", environment: ["HOME": "/home/ada"]) + } + #expect(provider().path("library") == directory + "/library.sqlite") + } + + // MARK: What a request returns + + @Test func requestsAnswerAsTheReference() async throws { + defer { try? FileManager.default.removeItem(atPath: directory) } + let store = try await open(provider()) + let reference = InMemoryModelStorage(model: Self.model) + try await store.insert(Self.objects) + try await reference.insert(Self.objects) + + typealias Predicate = FetchRequest.Predicate + let age: Predicate.Expression = .keyPath("age") + let predicates: [Predicate?] = [ + nil, + .value(true), + .value(false), + Person.CodingKeys.age >= Int32(18), + Person.CodingKeys.age < Int32(18), + Person.CodingKeys.age == Int32(17), + Person.CodingKeys.age != Int32(17), + Person.CodingKeys.score > 7.5, + Person.CodingKeys.isMember == true, + Person.CodingKeys.name == "alan", + .comparison(.init(left: .keyPath("name"), right: .attribute(.string("a")), type: .beginsWith)), + .comparison(.init(left: .keyPath("name"), right: .attribute(.string("a")), type: .beginsWith, options: [.caseInsensitive])), + .comparison(.init(left: .keyPath("name"), right: .attribute(.string("N")), type: .endsWith, options: [.caseInsensitive])), + .comparison(.init(left: .keyPath("name"), right: .attribute(.string("ra")), type: .contains)), + .comparison(.init(left: .keyPath("name"), right: .attribute(.string("*a*")), type: .like)), + .comparison(.init(left: .keyPath("name"), right: .attribute(.string("Ada Lovelace")), type: .in)), + .comparison(.init(left: .keyPath("joined"), right: .attribute(.null), type: .equalTo)), + .comparison(.init(left: .keyPath("joined"), right: .attribute(.date(Date(timeIntervalSince1970: 1_600_000_000))), type: .greaterThan)), + .comparison(.init(left: .keyPath("token"), right: .attribute(.uuid(UUID(uuidString: "9E3C1A52-0C1B-4F0E-9D4B-6A1F2C3D4E5F")!)), type: .equalTo)), + .comparison(.init(left: .keyPath("photo"), right: .attribute(.data(Data([0, 1, 2, 255]))), type: .equalTo)), + .comparison(.init(left: .keyPath("visits"), right: .attribute(.int64(1 << 40)), type: .equalTo)), + .comparison(.init(left: .keyPath("team"), right: .relationship(.toOne("engine")), type: .equalTo)), + .comparison(.init(left: .keyPath("team"), right: .relationship(.null), type: .equalTo)), + .comparison(.init(left: .keyPath("team.name"), right: .attribute(.string("Theory")), type: .equalTo)), + .comparison(.init(left: .arithmetic(.init(function: .add, left: age, right: .attribute(.int32(4)))), right: .attribute(.int32(45)), type: .equalTo)), + .comparison(.init(left: .arithmetic(.init(function: .multiply, left: .keyPath("score"), right: .attribute(.double(2)))), right: .attribute(.double(15)), type: .greaterThan)), + .compound(.and([Person.CodingKeys.age >= Int32(18), Person.CodingKeys.isMember == true])), + .compound(.or([Person.CodingKeys.age < Int32(18), Person.CodingKeys.name == "Ada"])), + .compound(.not(Person.CodingKeys.isMember == true)), + .compound(.and([])), + .compound(.or([])), + ] + let sorts: [[FetchRequest.SortDescriptor]] = [ + [], + [.init(property: "name")], + [.init(property: "age"), .init(property: "score", ascending: false)], + [.init(property: "joined", ascending: false)], + ] + for predicate in predicates { + for sort in sorts { + for (limit, offset) in [(0, 0), (2, 0), (2, 1), (0, 4)] { + let request = FetchRequest(entity: "Person", sortDescriptors: sort, predicate: predicate, fetchLimit: limit, fetchOffset: offset) + let expected = try await reference.fetch(request) + let found = try await store.fetch(request) + #expect(found.map(\.id) == expected.map(\.id), "\(String(describing: predicate)) \(sort.map { "\($0.term) \($0.ascending)" }) \(limit) \(offset): expected \(expected.map(\.id.rawValue))") + #expect(try await store.fetchID(request) == expected.map(\.id)) + #expect(try await store.count(request) == UInt(expected.count)) + } + } + } + // Something matched: neither answers nothing to everything. + #expect(try await store.count(FetchRequest(entity: "Person", predicate: Person.CodingKeys.age >= Int32(18))) == 3) + + // Through to-many relationships. + let members: FetchRequest.Predicate = .comparison(.init(left: .keyPath("members"), right: .relationship(.toOne("linus")), type: .contains)) + #expect(try await store.fetchID(FetchRequest(entity: "Team", predicate: members)) == ["theory"]) + let anyMember: FetchRequest.Predicate = .comparison(.init(left: .keyPath("members.age"), right: .attribute(.int32(40)), type: .greaterThan, modifier: .any)) + #expect(try await store.fetchID(FetchRequest(entity: "Team", predicate: anyMember)) + == (try await reference.fetchID(FetchRequest(entity: "Team", predicate: anyMember)))) + } + + @Test func everyValueComesBackAsItWent() async throws { + defer { try? FileManager.default.removeItem(atPath: directory) } + let store = try await open(provider()) + try await store.insert(Self.objects) + for person in Self.people { + let found = try #require(try await store.fetch(Person.entityName, for: ObjectID(rawValue: person.id))) + #expect(try Person(from: found) == person) + } + let engine = try #require(try await store.fetch("Team", for: "engine")) + #expect(try Team(from: engine).members == ["ada", "grace"]) + } + + // MARK: Writes + + @Test func aBatchIsAppliedWholeOrNotAtAll() async throws { + defer { try? FileManager.default.removeItem(atPath: directory) } + let store = try await open(provider()) + try await store.insert(Self.objects) + + var changes = ModelChanges() + changes.insert(Person(id: "barbara", name: "Barbara", age: 30).encode()) + changes.delete(Person.entityName, for: "ada") + // An insert merges: the properties a record has, and the rest left. + var renamed = ModelData(entity: Person.entityName, id: "grace") + renamed.encode("Grace Hopper", forKey: Person.CodingKeys.name) + changes.insert(renamed) + try await store.perform(changes) + #expect(Set(try await store.fetchID(FetchRequest(entity: "Person"))) == ["alan", "barbara", "edsger", "grace", "linus"]) + let grace = try Person(from: try #require(try await store.fetch(Person.entityName, for: "grace"))) + #expect(grace.name == "Grace Hopper" && grace.age == 45 && grace.team == "engine") + // The other side of a deleted object's relationship follows. + #expect(try Team(from: try #require(try await store.fetch("Team", for: "engine"))).members == ["grace"]) + + // The last change can't be applied: none of them is. + var failing = ModelChanges() + failing.delete(Person.entityName, for: "linus") + failing.insert(Note(id: "n1", text: "kept?")) + failing.insert(ModelData(entity: "Nothing", id: "x")) + await #expect(throws: (any Error).self) { try await store.perform(failing) } + #expect(try await store.fetch(Person.entityName, for: "linus") != nil) + #expect(try await store.count(FetchRequest(entity: "Note")) == 0) + // And the store goes on. + try await store.perform(ModelChanges()) + try await store.insert(Note(id: "n2", text: "after")) + #expect(try await store.count(FetchRequest(entity: "Note")) == 1) + } + + // MARK: Opening + + @Test func aStoreKeepsItsObjectsBetweenOpenings() async throws { + defer { try? FileManager.default.removeItem(atPath: directory) } + let first = provider() + let store = try await open(first) + #expect(try await open(first) === store) + try await store.insert(Self.objects) + + // Another launch. + let reopened = try await open(provider()) + #expect(reopened !== store) + #expect(try await reopened.count(FetchRequest(entity: "Person")) == 5) + #expect(try await reopened.fetchID(FetchRequest(entity: "Person", predicate: Person.CodingKeys.isMember == true)) == ["ada", "grace", "linus"]) + + // Destroyed, it's empty. + try await first.destroy("library") + #expect(!FileManager.default.fileExists(atPath: first.path("library"))) + #expect(try await open(provider()).count(FetchRequest(entity: "Person")) == 0) + } + + @Test func aChangedModelMigratesOrIsRefused() async throws { + defer { try? FileManager.default.removeItem(atPath: directory) } + let stores = provider() + let store = try await open(stores) + try await store.insert(Self.objects) + try await store.insert(Note(id: "n1", text: "a note")) + + // Notes are gone; a person has an email and no score; there are tags. + func entity(_ id: EntityName, _ edit: (inout EntityDescription) -> Void) -> EntityDescription { + var entity = Self.model[id]! + edit(&entity) + return entity + } + let person = entity("Person") { person in + person.attributes.removeAll { $0.id == "score" } + person.attributes.append(Attribute(id: "email", type: .string)) + } + let migrated = Model(entities: [ + person, Self.model["Team"]!, + EntityDescription(id: "Tag", attributes: [Attribute(id: "label", type: .string)], relationships: []), + ]) + // The same launch: the app opens its store again with a newer model. + let opened = try await open(stores, migrated) + #expect(try await opened.count(FetchRequest(entity: "Person")) == 5) + let ada = try #require(try await opened.fetch(Person.entityName, for: "ada")) + #expect(ada.attributes["name"] == .string("Ada")) + #expect(ada.attributes["score"] == nil) + #expect(ada.attributes["email"] == .null) + #expect(ada.relationships["team"] == .toOne("engine")) + #expect(try Team(from: try #require(try await opened.fetch("Team", for: "theory"))).members == ["alan", "linus"]) + try await opened.insert(ModelData(entity: "Tag", id: "t", attributes: ["label": .string("new")])) + // A storage still open on the old model has lost its file to the + // new one: it fails, and says so. + #expect(await failure { _ = try await store.count(FetchRequest(entity: "Note")) } == .unavailable) + #expect(!FileManager.default.fileExists(atPath: stores.path("library") + ".migrating")) + + // A name that's now a number can't be migrated: refused, and the + // store is as it was. + let renumbered = Model(entities: [ + entity("Person") { person in + person.attributes = person.attributes.map { $0.id == "name" ? Attribute(id: "name", type: .int64) : $0 } + }, + Self.model["Team"]!, + ]) + #expect(await failure { _ = try await open(provider(), renumbered) } == .migrationRequired) + #expect(try await open(provider(), migrated).count(FetchRequest(entity: "Tag")) == 1) + + // Or emptied, when the app says so. + let reset = try await open(provider(), renumbered, migration: .reset) + #expect(try await reset.count(FetchRequest(entity: "Person")) == 0) + #expect(try await open(provider(), renumbered).count(FetchRequest(entity: "Person")) == 0) + } + + @Test func indexesFollowTheApp() async throws { + defer { try? FileManager.default.removeItem(atPath: directory) } + func indexes(_ provider: SQLiteModelStorageProvider) throws -> [String] { + let connection = try Connection(path: provider.path("library")) + let list = try connection.scalar("SELECT group_concat(name, char(10)) FROM sqlite_master WHERE type = 'index' AND name LIKE 'appruntime.index.%'") + return (list?.string ?? "").split(separator: "\n").map(String.init).sorted() + } + let first = provider() + _ = try await open(first, indexes: ["Person": [["name"], ["age", "score"]]]) + #expect(try indexes(first) == ["appruntime.index.Person.age,score", "appruntime.index.Person.name", "appruntime.index.Person.team"]) + // One dropped; the relationship's stays. + let second = provider() + _ = try await open(second, indexes: ["Person": [["age", "score"]]]) + #expect(try indexes(second) == ["appruntime.index.Person.age,score", "appruntime.index.Person.team"]) + } + + @Test func aFileThatIsNotAStoreIsCorrupted() async throws { + defer { try? FileManager.default.removeItem(atPath: directory) } + let provider = provider() + try FileManager.default.createDirectory(atPath: directory, withIntermediateDirectories: true) + try Data(repeating: 0x41, count: 4096).write(to: URL(fileURLWithPath: provider.path("library"))) + #expect(await failure { _ = try await open(provider) } == .corrupted) + } + + @Test func withoutADirectoryThereIsNoStore() async throws { + let provider = SQLiteModelStorageProvider(directory: "/proc/no-stores-here") + #expect(await failure { _ = try await open(provider) } == .unavailable) + } +} + +func Note(id: ObjectID, text: String) -> ModelData { + ModelData(entity: "Note", id: id, attributes: ["text": .string(text)]) +}