From 118faf7a5df1a802b8d13439d74df791d671d2fa Mon Sep 17 00:00:00 2001 From: Ziyang Hu Date: Mon, 28 Nov 2022 20:59:28 +0800 Subject: [PATCH] Always bundle Sqlite if required --- Cargo.lock | 1 + cozo-core/Cargo.toml | 3 ++- cozo-core/src/runtime/db.rs | 7 +++++-- cozo-lib-swift/Cargo.toml | 23 ++++++++++++++++++++++- cozo-lib-swift/README.md | 34 ++++++++++++++++++++++++++++++++++ cozo-lib-swift/build-rust.sh | 10 +++++----- 6 files changed, 69 insertions(+), 9 deletions(-) create mode 100644 cozo-lib-swift/README.md diff --git a/Cargo.lock b/Cargo.lock index 9cbda885..b004b09c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -531,6 +531,7 @@ dependencies = [ "smallvec", "smartstring", "sqlite", + "sqlite3-src", "thiserror", "tikv-client", "tikv-jemallocator-global", diff --git a/cozo-core/Cargo.toml b/cozo-core/Cargo.toml index 79e6ec5a..435fec7d 100644 --- a/cozo-core/Cargo.toml +++ b/cozo-core/Cargo.toml @@ -26,7 +26,7 @@ minimal = ["storage-sqlite"] ## also allows backup and restore with Sqlite data files. ## Sqlite is easy to compile, has very low resource requirements and reasonable performance, ## but does not support much concurrency. -storage-sqlite = ["dep:sqlite"] +storage-sqlite = ["dep:sqlite", "dep:sqlite3-src"] ## Enables the [RocksDB](http://rocksdb.org/) backend. ## RocksDB is hard (even impossible) to compile on some platforms, uses quite a lot of resources ## including background threads, but is very performant and supports a high level of concurrency. @@ -132,6 +132,7 @@ sled = { version = "0.34.7", optional = true } tikv-client = { version = "0.1.0", optional = true } tokio = { version = "1.21.2", optional = true } sqlite = { version = "0.30.1", optional = true } +sqlite3-src = { version = "0.4.0", optional = true, features = ["bundled"] } js-sys = { version = "0.3.60", optional = true } #redb = "0.9.0" #ouroboros = "0.15.5" diff --git a/cozo-core/src/runtime/db.rs b/cozo-core/src/runtime/db.rs index 4486b654..16287256 100644 --- a/cozo-core/src/runtime/db.rs +++ b/cozo-core/src/runtime/db.rs @@ -208,7 +208,9 @@ impl<'s, S: Storage<'s>> Db { } Ok(JsonValue::Object(ret)) } - /// Import relations + /// Import relations. The argument `data` accepts data in the shape of + /// what was returned by [Self::export_relations]. + /// The target stored relations must already exist in the database. pub fn import_relations(&'s self, data: &Map) -> Result<()> { #[derive(Debug, Diagnostic, Error)] #[error("cannot import data for relation '{0}': {1}")] @@ -418,7 +420,8 @@ impl<'s, S: Storage<'s>> Db { #[cfg(not(feature = "storage-sqlite"))] bail!("backup requires the 'storage-sqlite' feature to be enabled") } - /// Import data from relations in a backup file + /// Import data from relations in a backup file. + /// The target stored relations must already exist in the database. pub fn import_from_backup(&'s self, in_file: &str, relations: &[String]) -> Result<()> { #[cfg(not(feature = "storage-sqlite"))] bail!("backup requires the 'storage-sqlite' feature to be enabled"); diff --git a/cozo-lib-swift/Cargo.toml b/cozo-lib-swift/Cargo.toml index aafd2f69..04587662 100644 --- a/cozo-lib-swift/Cargo.toml +++ b/cozo-lib-swift/Cargo.toml @@ -15,13 +15,34 @@ crate-type = ["staticlib"] # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [features] +## Enables the `minimal`, `requests` and `graph-algo` features +compact = ["minimal", "requests", "graph-algo", "rayon"] +## Enables the `storage-sqlite` and `graph-algo` features +mobile = ["storage-sqlite", "graph-algo", "rayon"] +## Enables the `minimal`, `requests` and `graph-algo` features in single threaded mode +compact-single-threaded = ["minimal", "requests", "graph-algo"] +## Enables the `storage-sqlite` feature +minimal = ["storage-sqlite"] +## Enables the [Sqlite](https://www.sqlite.org/index.html) backend, also allows backup and restore with Sqlite data files. +storage-sqlite = ["cozo/storage-sqlite"] +## Enables the [RocksDB](http://rocksdb.org/) backend storage-rocksdb = ["cozo/storage-rocksdb"] +## Enables the graph algorithms +graph-algo = ["cozo/graph-algo"] +## Allows the utilities to make web requests to fetch data +requests = ["cozo/requests"] +## Uses jemalloc as the global allocator, can make a difference in performance jemalloc = ["cozo/jemalloc"] +## Enables io-uring option for the RocksDB storage io-uring = ["cozo/io-uring"] +## Allows threading and enables the use of the `rayon` library for parallelizing algorithms +rayon = ["cozo/rayon"] +## Disallows the use of threads +nothread = ["cozo/nothread"] [build-dependencies] swift-bridge-build = "0.1.41" [dependencies] -cozo = { version = "0.2.1", path = "../cozo-core", default-features = false, features = ["compact"] } +cozo = { version = "0.2.1", path = "../cozo-core", default-features = false } swift-bridge = "0.1.41" diff --git a/cozo-lib-swift/README.md b/cozo-lib-swift/README.md new file mode 100644 index 00000000..76bdb66b --- /dev/null +++ b/cozo-lib-swift/README.md @@ -0,0 +1,34 @@ +# Cozo for Swift on Apple + +Only the `storage-sqlite` engine is enabled for the Swift prebuilt binaries, as using +other storage engines on desktop or mobile does not make too much sense. If you disagree, +see the Building section below. + +## Using the library + +TODO + +## Building + +First, install the [Rust toolchain](https://rustup.rs). +Then run the [build script](build-rust.sh) in this directory. +It is recommended to also set the environment variable `CARGO_PROFILE_RELEASE_LTO=fat`: +this makes the building process much longer, but in turn the library runs a little bit faster. + +When everything goes well, you should find the compiled Swift package in a directory called +`CozoSwiftBridge`. + +If you want to use the RocksDB engine on Desktop, in the build script change the two lines +```bash +cargo build -p cozo-swift -F compact --target x86_64-apple-darwin --release +cargo build -p cozo-swift -F compact --target aarch64-apple-darwin --release +``` +to +```bash +cargo build -p cozo-swift -F compact -F storage-rocksdb --target x86_64-apple-darwin --release +cargo build -p cozo-swift -F compact -F storage-rocksdb --target aarch64-apple-darwin --release +``` + +Then you also need to link your executable with `libc++`: in XCode, click on your project +in the left drawer, then on the right go to `Build phases > Link Binary With Libraries`, +click the plus sign, search for `libc++`, then add `libc++.tbd` found under Apple SDKs. \ No newline at end of file diff --git a/cozo-lib-swift/build-rust.sh b/cozo-lib-swift/build-rust.sh index ff110fa2..91894c33 100755 --- a/cozo-lib-swift/build-rust.sh +++ b/cozo-lib-swift/build-rust.sh @@ -6,8 +6,8 @@ cd $THISDIR export SWIFT_BRIDGE_OUT_DIR="$(pwd)/generated" # Build the project for the desired platforms: -cargo build -p cozo-swift -F storage-rocksdb --target x86_64-apple-darwin --release -cargo build -p cozo-swift -F storage-rocksdb --target aarch64-apple-darwin --release +cargo build -p cozo-swift -F compact --target x86_64-apple-darwin --release +cargo build -p cozo-swift -F compact --target aarch64-apple-darwin --release mkdir -p ../target/universal-macos/release lipo \ @@ -15,9 +15,9 @@ lipo \ ../target/x86_64-apple-darwin/release/libcozo_swift.a -create -output \ ../target/universal-macos/release/libcozo_swift.a -cargo build -p cozo-swift --target aarch64-apple-ios --release -cargo build -p cozo-swift --target x86_64-apple-ios --release -cargo build -p cozo-swift --target aarch64-apple-ios-sim --release +cargo build -p cozo-swift -F compact --target aarch64-apple-ios --release +cargo build -p cozo-swift -F compact --target x86_64-apple-ios --release +cargo build -p cozo-swift -F compact --target aarch64-apple-ios-sim --release mkdir -p ../target/universal-ios/release