From a4a3b5be42560fe60ad6bdabbede555d92a0030f Mon Sep 17 00:00:00 2001 From: Ziyang Hu Date: Thu, 3 Nov 2022 21:00:04 +0800 Subject: [PATCH] move java lib back --- Cargo.lock | 253 ++++++++++++++++++++++++++++++++++++-- Cargo.toml | 2 +- build-release-linux.sh | 4 + build-release-mac.sh | 2 + build-release-windows.ps1 | 10 +- cozo-lib-java/Cargo.toml | 25 ++++ cozo-lib-java/src/lib.rs | 94 ++++++++++++++ 7 files changed, 375 insertions(+), 15 deletions(-) create mode 100644 cozo-lib-java/Cargo.toml create mode 100644 cozo-lib-java/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 2c179936..91c81416 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,16 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "Inflector" +version = "0.11.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe438c63458706e03479442743baae6c88256498e6431708f6dfc520a26515d3" +dependencies = [ + "lazy_static", + "regex", +] + [[package]] name = "addr2line" version = "0.17.0" @@ -29,7 +39,7 @@ version = "0.7.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fcb51a0695d8f838b1ee009b3fbf66bda078cd64590202a864a8f3e8c4315c47" dependencies = [ - "getrandom", + "getrandom 0.2.8", "once_cell", "version_check", ] @@ -205,6 +215,12 @@ version = "1.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" +[[package]] +name = "bytes" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec8a7b6a70fde80372154c65702f00a0f56f3e1c36abbc6c440484be248856db" + [[package]] name = "casey" version = "0.3.3" @@ -223,6 +239,12 @@ dependencies = [ "jobserver", ] +[[package]] +name = "cesu8" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d43a04d8753f35258c91f8ec639f792891f748a1edbd759cf1dcea3382ad83c" + [[package]] name = "cfg-if" version = "0.1.10" @@ -290,7 +312,7 @@ dependencies = [ "clap_lex", "indexmap", "once_cell", - "strsim", + "strsim 0.10.0", "termcolor", "textwrap 0.16.0", ] @@ -327,6 +349,16 @@ dependencies = [ "unicode-width", ] +[[package]] +name = "combine" +version = "4.6.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "35ed6e9d84f0b51a7f52daf1c7d71dd136fd7a3f41a8462b8cdb8c78d920fad4" +dependencies = [ + "bytes", + "memchr", +] + [[package]] name = "core-foundation-sys" version = "0.8.3" @@ -359,7 +391,7 @@ dependencies = [ "pest", "pest_derive", "priority-queue", - "rand", + "rand 0.8.5", "rayon", "regex", "rmp", @@ -388,6 +420,17 @@ dependencies = [ "serde_json", ] +[[package]] +name = "cozo_java" +version = "0.1.1" +dependencies = [ + "cozo", + "lazy_static", + "miette", + "robusta_jni", + "serde_json", +] + [[package]] name = "cozorocks" version = "0.1.0" @@ -540,6 +583,41 @@ dependencies = [ "syn", ] +[[package]] +name = "darling" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d706e75d87e35569db781a9b5e2416cff1236a47ed380831f959382ccd5f858" +dependencies = [ + "darling_core", + "darling_macro", +] + +[[package]] +name = "darling_core" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0c960ae2da4de88a91b2d920c2a7233b400bc33cb28453a2987822d8392519b" +dependencies = [ + "fnv", + "ident_case", + "proc-macro2", + "quote", + "strsim 0.9.3", + "syn", +] + +[[package]] +name = "darling_macro" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b5a2f4ac4969822c62224815d069952656cadc7084fdca9751e6d959189b72" +dependencies = [ + "darling_core", + "quote", + "syn", +] + [[package]] name = "deflate" version = "1.0.0" @@ -579,6 +657,16 @@ dependencies = [ "termcolor", ] +[[package]] +name = "error-chain" +version = "0.12.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2d2f06b9cac1506ece98fe3231e3cc9c4410ec3d5b1f24ae1c8946f0742cdefc" +dependencies = [ + "backtrace", + "version_check", +] + [[package]] name = "fastrand" version = "1.8.0" @@ -600,6 +688,12 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + [[package]] name = "form_urlencoded" version = "1.1.0" @@ -625,6 +719,17 @@ dependencies = [ "version_check", ] +[[package]] +name = "getrandom" +version = "0.1.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8fc3cb4d91f53b50155bdcfd23f6a4c39ae1969c2ae85982b135750cccaf5fce" +dependencies = [ + "cfg-if 1.0.0", + "libc", + "wasi 0.9.0+wasi-snapshot-preview1", +] + [[package]] name = "getrandom" version = "0.2.8" @@ -717,6 +822,12 @@ dependencies = [ "cxx-build", ] +[[package]] +name = "ident_case" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" + [[package]] name = "idna" version = "0.3.0" @@ -773,6 +884,26 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4217ad341ebadf8d8e724e264f13e593e0648f5b3e94b3896a5df283be015ecc" +[[package]] +name = "jni" +version = "0.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "36bcc950632e48b86da402c5c077590583da5ac0d480103611d5374e7c967a3c" +dependencies = [ + "cesu8", + "combine", + "error-chain", + "jni-sys", + "log", + "walkdir", +] + +[[package]] +name = "jni-sys" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8eaf4bc02d17cbdd7ff4c7438cafcdf7fb9a4613313ad11b4f8fefe7d3fa0130" + [[package]] name = "jobserver" version = "0.1.25" @@ -936,7 +1067,7 @@ dependencies = [ "mime", "mime_guess", "quick-error", - "rand", + "rand 0.8.5", "safemem", "tempfile", "twoway", @@ -1154,7 +1285,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b1181c94580fa345f50f19d738aaa39c0ed30a600d95cb2d3e23f94266f14fbf" dependencies = [ "phf_shared", - "rand", + "rand 0.8.5", ] [[package]] @@ -1237,6 +1368,19 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "rand" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03" +dependencies = [ + "getrandom 0.1.16", + "libc", + "rand_chacha 0.2.2", + "rand_core 0.5.1", + "rand_hc", +] + [[package]] name = "rand" version = "0.8.5" @@ -1244,8 +1388,18 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" dependencies = [ "libc", - "rand_chacha", - "rand_core", + "rand_chacha 0.3.1", + "rand_core 0.6.4", +] + +[[package]] +name = "rand_chacha" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402" +dependencies = [ + "ppv-lite86", + "rand_core 0.5.1", ] [[package]] @@ -1255,7 +1409,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" dependencies = [ "ppv-lite86", - "rand_core", + "rand_core 0.6.4", +] + +[[package]] +name = "rand_core" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19" +dependencies = [ + "getrandom 0.1.16", ] [[package]] @@ -1264,7 +1427,16 @@ version = "0.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" dependencies = [ - "getrandom", + "getrandom 0.2.8", +] + +[[package]] +name = "rand_hc" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c" +dependencies = [ + "rand_core 0.5.1", ] [[package]] @@ -1385,6 +1557,33 @@ dependencies = [ "rmp", ] +[[package]] +name = "robusta-codegen" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f66a33c846c7b892c2fd20ff58ba34859e6d83e491d51ce94f678cc7157ad774" +dependencies = [ + "Inflector", + "darling", + "proc-macro-error", + "proc-macro2", + "quote", + "rand 0.7.3", + "syn", +] + +[[package]] +name = "robusta_jni" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a61f3ca304f439f93ae5f59f2fe65f4c2693b253c7656dc242c061996a4b01f8" +dependencies = [ + "jni", + "paste", + "robusta-codegen", + "static_assertions", +] + [[package]] name = "rouille" version = "3.6.1" @@ -1399,7 +1598,7 @@ dependencies = [ "multipart", "num_cpus", "percent-encoding", - "rand", + "rand 0.8.5", "serde", "serde_derive", "serde_json", @@ -1449,6 +1648,15 @@ version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ef703b7cb59335eae2eb93ceb664c0eb7ea6bf567079d843e09420219668e072" +[[package]] +name = "same-file" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" +dependencies = [ + "winapi-util", +] + [[package]] name = "scopeguard" version = "1.1.0" @@ -1577,6 +1785,12 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" +[[package]] +name = "strsim" +version = "0.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6446ced80d6c486436db5c078dde11a9f73d42b57fb273121e160b84f63d894c" + [[package]] name = "strsim" version = "0.10.0" @@ -1888,7 +2102,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "feb41e78f93363bb2df8b0e86a2ca30eed7806ea16ea0c790d757cf93f79be83" dependencies = [ "atomic", - "getrandom", + "getrandom 0.2.8", "serde", ] @@ -1898,6 +2112,23 @@ version = "0.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" +[[package]] +name = "walkdir" +version = "2.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "808cf2735cd4b6866113f648b791c6adc5714537bc222d9347bb203386ffda56" +dependencies = [ + "same-file", + "winapi", + "winapi-util", +] + +[[package]] +name = "wasi" +version = "0.9.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" + [[package]] name = "wasi" version = "0.10.0+wasi-snapshot-preview1" diff --git a/Cargo.toml b/Cargo.toml index c5b73e97..074cfc3c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -68,4 +68,4 @@ lto = true #debug = true [workspace] -members = ["cozorocks", "cozo-lib-c"] +members = ["cozorocks", "cozo-lib-c", "cozo-lib-java"] diff --git a/build-release-linux.sh b/build-release-linux.sh index 052c828d..ffc752d4 100755 --- a/build-release-linux.sh +++ b/build-release-linux.sh @@ -8,15 +8,19 @@ mkdir release cross build --target=aarch64-unknown-linux-gnu --release cross build --target=aarch64-unknown-linux-gnu --release --manifest-path=cozo-lib-c/Cargo.toml +cross build --target=aarch64-unknown-linux-gnu --release --manifest-path=cozo-lib-java/Cargo.toml cross build --target=x86_64-unknown-linux-gnu --release cross build --target=x86_64-unknown-linux-gnu --release --manifest-path=cozo-lib-c/Cargo.toml +cross build --target=x86_64-unknown-linux-gnu --release --manifest-path=cozo-lib-java/Cargo.toml cp target/aarch64-unknown-linux-gnu/release/cozoserver release/cozoserver-${VERSION}-linux-arm64 cp target/aarch64-unknown-linux-gnu/release/libcozo_c.a release/libcozo_c-${VERSION}-linux-arm64.a cp target/aarch64-unknown-linux-gnu/release/libcozo_c.so release/libcozo_c-${VERSION}-linux-arm64.so +cp target/aarch64-unknown-linux-gnu/release/libcozo_java.so release/libcozo_java-${VERSION}-linux-arm64.so cp target/x86_64-unknown-linux-gnu/release/cozoserver release/cozoserver-${VERSION}-linux-x86_64 cp target/x86_64-unknown-linux-gnu/release/libcozo_c.a release/libcozo_c-${VERSION}-linux-x86_64.a cp target/x86_64-unknown-linux-gnu/release/libcozo_c.so release/libcozo_c-${VERSION}-linux-x86_64.so +cp target/x86_64-unknown-linux-gnu/release/libcozo_java.so release/libcozo_java-${VERSION}-linux-x86_64.so aarch64-linux-gnu-strip release/cozoserver-${VERSION}-linux-arm64 strip release/cozoserver-${VERSION}-linux-x86_64 gzip release/* diff --git a/build-release-mac.sh b/build-release-mac.sh index e0204046..b229808f 100755 --- a/build-release-mac.sh +++ b/build-release-mac.sh @@ -10,10 +10,12 @@ mkdir release cargo build --release cargo build --release --manifest-path=cozo-lib-c/Cargo.toml +cargo build --release --manifest-path=cozo-lib-java/Cargo.toml cp target/release/cozoserver release/cozoserver-${VERSION}-mac-${ARCH} cp target/release/libcozo_c.a release/libcozo_c-${VERSION}-mac-${ARCH}.a cp target/release/libcozo_c.dylib release/libcozo_c-${VERSION}-mac-${ARCH}.dylib +cp target/release/libcozo_java.dylib release/libcozo_java-${VERSION}-mac-${ARCH}.dylib strip release/cozoserver-${VERSION}-mac-${ARCH} gzip release/* \ No newline at end of file diff --git a/build-release-windows.ps1 b/build-release-windows.ps1 index dfbcaf4c..9c8f1266 100644 --- a/build-release-windows.ps1 +++ b/build-release-windows.ps1 @@ -14,14 +14,18 @@ mkdir release cargo build --release cargo build --release --manifest-path=cozo-lib-c/Cargo.toml +cargo build --release --manifest-path=cozo-lib-java/Cargo.toml cp target/release/cozoserver.exe release/cozoserver-${COZO_VERSION}-windows-x86_64.exe -cp target/release/cozo_c.dll release/libcozo_c-${COZO_VERSION}-windows-x86_64.dll cp target/release/cozo_c.lib release/libcozo_c-${COZO_VERSION}-windows-x86_64.lib +cp target/release/cozo_c.dll release/libcozo_c-${COZO_VERSION}-windows-x86_64.dll +cp target/release/cozo_java.dll release/libcozo_java-${COZO_VERSION}-windows-x86_64.dll Compress-Archive -Path release/cozoserver-${COZO_VERSION}-windows-x86_64.exe -DestinationPath release/cozoserver-${COZO_VERSION}-windows-x86_64.zip -Compress-Archive -Path release/libcozo_c-${COZO_VERSION}-windows-x86_64.dll -DestinationPath release/libcozo_c-${COZO_VERSION}-windows-x86_64.dll.zip Compress-Archive -Path release/libcozo_c-${COZO_VERSION}-windows-x86_64.lib -DestinationPath release/libcozo_c-${COZO_VERSION}-windows-x86_64.lib.zip +Compress-Archive -Path release/libcozo_c-${COZO_VERSION}-windows-x86_64.dll -DestinationPath release/libcozo_c-${COZO_VERSION}-windows-x86_64.dll.zip +Compress-Archive -Path release/libcozo_c-${COZO_VERSION}-windows-x86_64.dll -DestinationPath release/libcozo_c-${COZO_VERSION}-windows-x86_64.dll.zip Remove-Item release/cozoserver-${COZO_VERSION}-windows-x86_64.exe +Remove-Item release/libcozo_c-${COZO_VERSION}-windows-x86_64.lib Remove-Item release/libcozo_c-${COZO_VERSION}-windows-x86_64.dll -Remove-Item release/libcozo_c-${COZO_VERSION}-windows-x86_64.lib \ No newline at end of file +Remove-Item release/libcozo_java-${COZO_VERSION}-windows-x86_64.dll \ No newline at end of file diff --git a/cozo-lib-java/Cargo.toml b/cozo-lib-java/Cargo.toml new file mode 100644 index 00000000..06693393 --- /dev/null +++ b/cozo-lib-java/Cargo.toml @@ -0,0 +1,25 @@ +[package] +name = "cozo_java" +version = "0.1.1" +edition = "2021" +license = "AGPL-3.0-or-later" +homepage = "https://github.com/cozodb/cozo" +repository = "https://github.com/cozodb/cozo" +documentation = "https://cozodb.github.io/current/manual" +description = "Java bindings for CozoDB" + +[lib] +crate-type = ["cdylib"] + +[features] +jemalloc = ["cozo/jemalloc"] +io-uring = ["cozo/io-uring"] + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +robusta_jni = "0.2.0" +cozo = { version = "0.1.2", path = ".." } +miette = { version = "=5.3.0", features = ["fancy"] } +serde_json = "1.0.81" +lazy_static = "1.4.0" diff --git a/cozo-lib-java/src/lib.rs b/cozo-lib-java/src/lib.rs new file mode 100644 index 00000000..46fbd8cb --- /dev/null +++ b/cozo-lib-java/src/lib.rs @@ -0,0 +1,94 @@ +/* + * Copyright 2022, The Cozo Project Authors. Licensed under MIT/Apache-2.0/BSD-3-Clause. + */ +use std::collections::BTreeMap; +use std::sync::atomic::AtomicI32; +use std::sync::Mutex; + +use lazy_static::lazy_static; +use robusta_jni::bridge; + +use cozo::Db; + +#[derive(Default)] +struct Handles { + current: AtomicI32, + dbs: Mutex>, +} + +lazy_static! { + static ref HANDLES: Handles = Handles::default(); +} + +#[bridge] +mod jni { + use std::collections::BTreeMap; + use std::sync::atomic::Ordering; + + use robusta_jni::convert::{IntoJavaValue, Signature, TryFromJavaValue, TryIntoJavaValue}; + use robusta_jni::jni::errors::Error as JniError; + use robusta_jni::jni::errors::Result as JniResult; + use robusta_jni::jni::objects::AutoLocal; + + use cozo::Db; + + use crate::HANDLES; + + #[derive(Signature, TryIntoJavaValue, IntoJavaValue, TryFromJavaValue)] + #[package(org.cozodb)] + pub struct CozoDb<'env: 'borrow, 'borrow> { + #[instance] + raw: AutoLocal<'env, 'borrow>, + } + + impl<'env: 'borrow, 'borrow> CozoDb<'env, 'borrow> { + pub extern "jni" fn openDb(path: String) -> JniResult { + match Db::new(path) { + Ok(db) => { + let id = HANDLES.current.fetch_add(1, Ordering::AcqRel); + let mut dbs = HANDLES.dbs.lock().unwrap(); + dbs.insert(id, db); + Ok(id) + } + Err(err) => Err(JniError::from(format!("{:?}", err))), + } + } + pub extern "jni" fn closeDb(id: i32) -> JniResult { + let db = { + let mut dbs = HANDLES.dbs.lock().unwrap(); + dbs.remove(&id) + }; + Ok(db.is_some()) + } + pub extern "jni" fn runQuery( + id: i32, + script: String, + params_str: String, + ) -> JniResult { + let db = { + let db_ref = { + let dbs = HANDLES.dbs.lock().unwrap(); + dbs.get(&id).cloned() + }; + let db = db_ref.ok_or_else(|| JniError::from("database already closed"))?; + db + }; + let params_map: serde_json::Value = serde_json::from_str(¶ms_str) + .map_err(|_| JniError::from("the given params argument is not valid JSON"))?; + + let params_arg: BTreeMap<_, _> = match params_map { + serde_json::Value::Object(m) => m.into_iter().collect(), + _ => { + return Err(JniError::from( + "the given params argument is not a JSON map", + )) + } + }; + let result = db.run_script(&script, ¶ms_arg); + match result { + Ok(json) => Ok(json.to_string()), + Err(err) => Err(JniError::from(format!("{:?}", err))), + } + } + } +}