diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 5922651..4d1b170 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -44,13 +44,15 @@ jobs: - name: Install JDK uses: actions/setup-java@v4.2.1 with: - distribution: 'temurin' - java-version: '22' + distribution: "temurin" + java-version: "22" + + - name: Set up Gradle + uses: gradle/gradle-build-action@v3 - name: Java tests - run: | - cd fs-storage - make test-linux + run: gradle test + working-directory: ./java windows: name: Test on Windows @@ -62,19 +64,24 @@ jobs: - name: Install Rust uses: dtolnay/rust-toolchain@stable + - name: Build Release + run: cargo build --verbose --release + - name: Run tests run: cargo test --workspace --verbose - name: Install JDK uses: actions/setup-java@v4.2.1 with: - distribution: 'temurin' - java-version: '22' + distribution: "temurin" + java-version: "22" + + - name: Set up Gradle + uses: gradle/gradle-build-action@v3 - name: Java tests - run: | - cd fs-storage - make test-windows + run: gradle test + working-directory: ./java mac-intel: name: Test on macOS Intel @@ -86,16 +93,21 @@ jobs: - name: Install Rust uses: dtolnay/rust-toolchain@stable + - name: Build Release + run: cargo build --verbose --release + - name: Run tests run: cargo test --workspace --verbose - name: Install JDK uses: actions/setup-java@v4.2.1 with: - distribution: 'temurin' - java-version: '22' + distribution: "temurin" + java-version: "22" + + - name: Set up Gradle + uses: gradle/gradle-build-action@v3 - name: Java tests - run: | - cd fs-storage - make test-mac + run: gradle test + working-directory: ./java diff --git a/README.md b/README.md index d2d7b7e..00b4c5d 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,7 @@ Development docs will come sometime. **The Concept of the Framework** The framework is supposed to help in solving the following problems: + 1. Management of user data, stored as simple files on the disk, as well as various kinds of metadata: tags, scores, arbitrary properties like movie title or description. Such a metadata is persisted to filesystem for easier sync, backup and migration by any 3rd-party tool. 2. Sync of both user data and metadata, across all user devices in P2P fashion. Cache syncing might be implemented later, too. 3. Version tracking for user data: not only text-files, but also images, videos etc. @@ -111,3 +112,7 @@ cargo flamegraph --bench index_build_benchmark -o index_build_benchmark.svg -- - > - Run as superuser to enable DTrace. This can be achieved by using `cargo flamegraph --root ...`. > > For further details, please refer to https://github.com/flamegraph-rs/flamegraph?tab=readme-ov-file#dtrace-on-macos + +## Bindings + +`arks` includes support for Java bindings using the [`jni-rs`](https://github.com/jni-rs/jni-rs) crate, which uses the Java Native Interface (JNI) to allow Rust functions to be called from Java. The Java bindings are implemented as a Gradle project, located in the `java/` directory. diff --git a/fs-storage/Makefile b/fs-storage/Makefile deleted file mode 100644 index a7d173e..0000000 --- a/fs-storage/Makefile +++ /dev/null @@ -1,52 +0,0 @@ -CARGO_CMD = cargo -JAR_FILE := junit.jar -URL := https://repo1.maven.org/maven2/org/junit/platform/junit-platform-console-standalone/1.9.3/junit-platform-console-standalone-1.9.3.jar - -.PHONY: test-linux -test-linux: build-rust download-test-lib java-test-linux - -.PHONY: test-mac -test-mac: build-rust download-test-lib java-test-mac - -.PHONY: test-windows -test-windows: build-rust download-test-lib java-test-windows - -.PHONY: build-rust -build-rust: - $(CARGO_CMD) build - -.PHONY: download-test-lib -download-test-lib: - cd tests && \ - if [ ! -f "$(JAR_FILE)" ]; then \ - curl -o "$(JAR_FILE)" "$(URL)"; \ - else \ - echo "$(JAR_FILE) already exists."; \ - fi - -.PHONY: java-test-linux -java-test-linux: - (cd ../target/debug && \ - export LD_LIBRARY_PATH=$$PWD && \ - cd ../../fs-storage/tests && \ - javac -d out FileStorage.java && \ - javac -d out -cp out:$(JAR_FILE) FileStorageTest.java && \ - RUST_BACKTRACE=1 java -jar $(JAR_FILE) --class-path out --scan-class-path) - -.PHONY: java-test-mac -java-test-mac: - (cd ../target/debug && \ - export DYLD_LIBRARY_PATH=$$PWD && \ - cd ../../fs-storage/tests && \ - javac -d out FileStorage.java && \ - javac -d out -cp out:$(JAR_FILE) FileStorageTest.java && \ - RUST_BACKTRACE=1 java -jar $(JAR_FILE) --class-path out --scan-class-path) - -.PHONY: java-test-windows -java-test-windows: - (cd ../target/debug && \ - export LIBRARY_PATH="$$PWD" && \ - cd ../../fs-storage/tests && \ - javac -d out FileStorage.java && \ - javac -d out -cp "out;$(JAR_FILE)" FileStorageTest.java && \ - RUST_BACKTRACE=1 java -Djava.library.path=$$LIBRARY_PATH -jar $(JAR_FILE) --class-path out --scan-class-path) \ No newline at end of file diff --git a/fs-storage/README.md b/fs-storage/README.md index 7351ded..cfd6856 100644 --- a/fs-storage/README.md +++ b/fs-storage/README.md @@ -44,15 +44,3 @@ key2: value2 ```bash cargo run --example cli read /tmp/z ``` - -## Java Wrapper -```java -javac -h . FileStorage.java -javac FileStorage.java -LD_LIBRARY_PATH=/target/debug && java FileStorage.java -``` - -## Steps to test Java Wrapper -```bash -cd tests && make test -``` \ No newline at end of file diff --git a/fs-storage/src/base_storage.rs b/fs-storage/src/base_storage.rs index 1b1bd70..8398125 100644 --- a/fs-storage/src/base_storage.rs +++ b/fs-storage/src/base_storage.rs @@ -6,7 +6,7 @@ use jnix::{FromJava, IntoJava}; #[derive(Debug, PartialEq, PartialOrd, Ord, Eq, Clone)] #[cfg_attr(feature = "jni-bindings", derive(FromJava, IntoJava))] -#[jnix(class_name = "SyncStatus")] +#[jnix(class_name = "dev/arkbuilders/core/FileStorage$SyncStatus")] /// Represents the synchronization status of the storage. pub enum SyncStatus { /// No synchronization needed. diff --git a/fs-storage/src/jni/file_storage.rs b/fs-storage/src/jni/file_storage.rs index 265e90a..6f1e8c1 100644 --- a/fs-storage/src/jni/file_storage.rs +++ b/fs-storage/src/jni/file_storage.rs @@ -28,7 +28,7 @@ impl FileStorage { } #[no_mangle] -pub extern "system" fn Java_FileStorage_create<'local>( +pub extern "system" fn Java_dev_arkbuilders_core_FileStorage_create<'local>( mut env: JNIEnv<'local>, _class: JClass, label: JString<'local>, @@ -53,7 +53,7 @@ pub extern "system" fn Java_FileStorage_create<'local>( } #[no_mangle] -pub extern "system" fn Java_FileStorage_set<'local>( +pub extern "system" fn Java_dev_arkbuilders_core_FileStorage_set<'local>( mut env: JNIEnv<'local>, _class: JClass, id: JString<'local>, @@ -67,7 +67,7 @@ pub extern "system" fn Java_FileStorage_set<'local>( } #[no_mangle] -pub extern "system" fn Java_FileStorage_remove<'local>( +pub extern "system" fn Java_dev_arkbuilders_core_FileStorage_remove<'local>( mut env: JNIEnv<'local>, _class: JClass, id: JString<'local>, @@ -86,7 +86,9 @@ pub extern "system" fn Java_FileStorage_remove<'local>( // type and returns it. #[no_mangle] #[allow(non_snake_case)] -pub extern "system" fn Java_FileStorage_syncStatus<'env>( +pub extern "system" fn Java_dev_arkbuilders_core_FileStorage_syncStatus< + 'env, +>( env: jnix::jni::JNIEnv<'env>, _this: jnix::jni::objects::JObject<'env>, file_storage_ptr: jnix::jni::sys::jlong, @@ -104,7 +106,7 @@ pub extern "system" fn Java_FileStorage_syncStatus<'env>( } #[no_mangle] -pub extern "system" fn Java_FileStorage_sync( +pub extern "system" fn Java_dev_arkbuilders_core_FileStorage_sync( mut env: JNIEnv<'_>, _class: JClass, file_storage_ptr: jlong, @@ -118,7 +120,7 @@ pub extern "system" fn Java_FileStorage_sync( } #[no_mangle] -pub extern "system" fn Java_FileStorage_readFS( +pub extern "system" fn Java_dev_arkbuilders_core_FileStorage_readFS( mut env: JNIEnv<'_>, _class: JClass, file_storage_ptr: jlong, @@ -171,7 +173,7 @@ pub extern "system" fn Java_FileStorage_readFS( } #[no_mangle] -pub extern "system" fn Java_FileStorage_writeFS( +pub extern "system" fn Java_dev_arkbuilders_core_FileStorage_writeFS( mut env: JNIEnv<'_>, _class: JClass, file_storage_ptr: jlong, @@ -187,7 +189,7 @@ pub extern "system" fn Java_FileStorage_writeFS( #[allow(clippy::suspicious_doc_comments)] ///! Safety: The FileStorage instance is dropped after this call #[no_mangle] -pub extern "system" fn Java_FileStorage_erase( +pub extern "system" fn Java_dev_arkbuilders_core_FileStorage_erase( mut env: JNIEnv<'_>, _class: JClass, file_storage_ptr: jlong, @@ -202,7 +204,7 @@ pub extern "system" fn Java_FileStorage_erase( } #[no_mangle] -pub extern "system" fn Java_FileStorage_merge( +pub extern "system" fn Java_dev_arkbuilders_core_FileStorage_merge( mut env: JNIEnv<'_>, _class: JClass, file_storage_ptr: jlong, diff --git a/fs-storage/tests/.gitignore b/fs-storage/tests/.gitignore deleted file mode 100644 index f23b948..0000000 --- a/fs-storage/tests/.gitignore +++ /dev/null @@ -1 +0,0 @@ -*.jar \ No newline at end of file diff --git a/fs-storage/tests/FileStorage.java b/fs-storage/tests/FileStorage.java deleted file mode 100644 index 132e679..0000000 --- a/fs-storage/tests/FileStorage.java +++ /dev/null @@ -1,61 +0,0 @@ -public class FileStorage { - private long fileStoragePtr; - - static { - System.loadLibrary("fs_storage"); - } - - private static native long create(String label, String path); - - private static native void set(String id, String value, long file_storage_ptr); - - private static native void remove(String id, long file_storage_ptr); - - private static native void sync(long file_storage_ptr); - - private static native SyncStatus syncStatus(long file_storage_ptr); - - private static native Object readFS(long file_storage_ptr); - - private static native void writeFS(long file_storage_ptr); - - private static native void erase(long file_storage_ptr); - - private static native void merge(long file_storage_ptr, long other_file_storage_ptr); - - public FileStorage(String label, String path) { - this.fileStoragePtr = create(label, path); - } - - public void set(String id, String value) { - set(id, value, this.fileStoragePtr); - } - - public void remove(String id) { - remove(id, this.fileStoragePtr); - } - - public void sync() { - sync(this.fileStoragePtr); - } - - public SyncStatus syncStatus() { - return syncStatus(this.fileStoragePtr); - } - - public Object readFS() { - return readFS(this.fileStoragePtr); - } - - public void writeFS() { - writeFS(this.fileStoragePtr); - } - - public void erase() { - erase(this.fileStoragePtr); - } - - public void merge(FileStorage other) { - merge(this.fileStoragePtr, other.fileStoragePtr); - } -} diff --git a/fs-storage/tests/SyncStatus.java b/fs-storage/tests/SyncStatus.java deleted file mode 100644 index 8f2cd62..0000000 --- a/fs-storage/tests/SyncStatus.java +++ /dev/null @@ -1,11 +0,0 @@ -/// Represents the synchronization status of the storage. -public enum SyncStatus { - /// No synchronization needed. - InSync, - /// In-memory key-value mapping is stale. - MappingStale, - /// External file system storage is stale. - StorageStale, - /// In-memory key-value mapping and external file system storage diverge. - Diverge, -} diff --git a/java/.gitattributes b/java/.gitattributes new file mode 100644 index 0000000..097f9f9 --- /dev/null +++ b/java/.gitattributes @@ -0,0 +1,9 @@ +# +# https://help.github.com/articles/dealing-with-line-endings/ +# +# Linux start script should use lf +/gradlew text eol=lf + +# These are Windows script files and should use crlf +*.bat text eol=crlf + diff --git a/java/.gitignore b/java/.gitignore new file mode 100644 index 0000000..1b6985c --- /dev/null +++ b/java/.gitignore @@ -0,0 +1,5 @@ +# Ignore Gradle project-specific cache directory +.gradle + +# Ignore Gradle build output directory +build diff --git a/java/README.md b/java/README.md new file mode 100644 index 0000000..44c5098 --- /dev/null +++ b/java/README.md @@ -0,0 +1,55 @@ +# `ark-java` + +This subdirectory contains the Java bindings for `ark` core Rust code. These bindings use [JNI](https://en.wikipedia.org/wiki/Java_Native_Interface) (Java Native Interface). + +The process involves generating dynamic libraries from Rust code, which are then loaded into the Java runtime. This is achieved by compiling the Rust code into shared libraries (`.so` on Linux, `.dylib` on macOS, and `.dll` on Windows), which are then linked and loaded at runtime using the [`System.loadLibrary`](https://docs.oracle.com/javase/8/docs/api/java/lang/Runtime.html#loadLibrary-java.lang.String-) method in Java. This method loads the specified native library, making its functions available for use in a Java environment. + +We use Gradle for dependency management. + +## Building the Bindings + +To build the bindings and set up the environment, follow these steps: + +### Prerequisites + +- Ensure you have the Rust toolchain installed. You can install it from [rustup](https://rustup.rs/). +- Ensure you have a JDK installed. You can download it from [AdoptOpenJDK](https://adoptopenjdk.net/). +- Ensure you have Gradle installed. You can install it from [Gradle](https://gradle.org/install/_) + +### Steps + +1. **Build the Rust Library:** Run the following command to compile the Rust code and generate the dynamic library: + +```sh +cargo build --release +``` + +This will compile the Rust code and generate the dynamic library in the `target/release` directory. + +2. **Build the Java Project:** To compile build the project, run: + +```sh +./gradlew build +``` + +This will compile the Java code and generate the JAR file in the `lib/build/libs` directory. This will also run the unit tests. + +## Documentation + +To generate the Javadoc documentation, run: + +```sh +./gradlew javadoc +``` + +The documentation will be generated in the `lib/build/docs/javadoc` directory. + +## Cleaning the Build + +To clean the build, run: + +```sh +./gradlew clean +``` + +This will clean the build and remove the generated files. diff --git a/java/gradle/libs.versions.toml b/java/gradle/libs.versions.toml new file mode 100644 index 0000000..8f20ed0 --- /dev/null +++ b/java/gradle/libs.versions.toml @@ -0,0 +1,12 @@ +# This file was generated by the Gradle 'init' task. +# https://docs.gradle.org/current/userguide/platforms.html#sub::toml-dependencies-format + +[versions] +commons-math3 = "3.6.1" +guava = "33.0.0-jre" +junit-jupiter = "5.10.2" + +[libraries] +commons-math3 = { module = "org.apache.commons:commons-math3", version.ref = "commons-math3" } +guava = { module = "com.google.guava:guava", version.ref = "guava" } +junit-jupiter = { module = "org.junit.jupiter:junit-jupiter", version.ref = "junit-jupiter" } diff --git a/java/gradle/wrapper/gradle-wrapper.jar b/java/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..e644113 Binary files /dev/null and b/java/gradle/wrapper/gradle-wrapper.jar differ diff --git a/java/gradle/wrapper/gradle-wrapper.properties b/java/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..a441313 --- /dev/null +++ b/java/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,7 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-8.8-bin.zip +networkTimeout=10000 +validateDistributionUrl=true +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/java/gradlew b/java/gradlew new file mode 100755 index 0000000..b740cf1 --- /dev/null +++ b/java/gradlew @@ -0,0 +1,249 @@ +#!/bin/sh + +# +# Copyright © 2015-2021 the original authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +############################################################################## +# +# Gradle start up script for POSIX generated by Gradle. +# +# Important for running: +# +# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is +# noncompliant, but you have some other compliant shell such as ksh or +# bash, then to run this script, type that shell name before the whole +# command line, like: +# +# ksh Gradle +# +# Busybox and similar reduced shells will NOT work, because this script +# requires all of these POSIX shell features: +# * functions; +# * expansions «$var», «${var}», «${var:-default}», «${var+SET}», +# «${var#prefix}», «${var%suffix}», and «$( cmd )»; +# * compound commands having a testable exit status, especially «case»; +# * various built-in commands including «command», «set», and «ulimit». +# +# Important for patching: +# +# (2) This script targets any POSIX shell, so it avoids extensions provided +# by Bash, Ksh, etc; in particular arrays are avoided. +# +# The "traditional" practice of packing multiple parameters into a +# space-separated string is a well documented source of bugs and security +# problems, so this is (mostly) avoided, by progressively accumulating +# options in "$@", and eventually passing that to Java. +# +# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, +# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; +# see the in-line comments for details. +# +# There are tweaks for specific operating systems such as AIX, CygWin, +# Darwin, MinGW, and NonStop. +# +# (3) This script is generated from the Groovy template +# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# within the Gradle project. +# +# You can find Gradle at https://github.com/gradle/gradle/. +# +############################################################################## + +# Attempt to set APP_HOME + +# Resolve links: $0 may be a link +app_path=$0 + +# Need this for daisy-chained symlinks. +while + APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path + [ -h "$app_path" ] +do + ls=$( ls -ld "$app_path" ) + link=${ls#*' -> '} + case $link in #( + /*) app_path=$link ;; #( + *) app_path=$APP_HOME$link ;; + esac +done + +# This is normally unused +# shellcheck disable=SC2034 +APP_BASE_NAME=${0##*/} +# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036) +APP_HOME=$( cd "${APP_HOME:-./}" > /dev/null && pwd -P ) || exit + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD=maximum + +warn () { + echo "$*" +} >&2 + +die () { + echo + echo "$*" + echo + exit 1 +} >&2 + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "$( uname )" in #( + CYGWIN* ) cygwin=true ;; #( + Darwin* ) darwin=true ;; #( + MSYS* | MINGW* ) msys=true ;; #( + NONSTOP* ) nonstop=true ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD=$JAVA_HOME/jre/sh/java + else + JAVACMD=$JAVA_HOME/bin/java + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD=java + if ! command -v java >/dev/null 2>&1 + then + die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +fi + +# Increase the maximum file descriptors if we can. +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac +fi + +# Collect all arguments for the java command, stacking in reverse order: +# * args from the command line +# * the main class name +# * -classpath +# * -D...appname settings +# * --module-path (only if needed) +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. + +# For Cygwin or MSYS, switch paths to Windows format before running java +if "$cygwin" || "$msys" ; then + APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) + CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) + + JAVACMD=$( cygpath --unix "$JAVACMD" ) + + # Now convert the arguments - kludge to limit ourselves to /bin/sh + for arg do + if + case $arg in #( + -*) false ;; # don't mess with options #( + /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath + [ -e "$t" ] ;; #( + *) false ;; + esac + then + arg=$( cygpath --path --ignore --mixed "$arg" ) + fi + # Roll the args list around exactly as many times as the number of + # args, so each arg winds up back in the position where it started, but + # possibly modified. + # + # NB: a `for` loop captures its iteration list before it begins, so + # changing the positional parameters here affects neither the number of + # iterations, nor the values presented in `arg`. + shift # remove old arg + set -- "$@" "$arg" # push replacement arg + done +fi + + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Collect all arguments for the java command: +# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments, +# and any embedded shellness will be escaped. +# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be +# treated as '${Hostname}' itself on the command line. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + org.gradle.wrapper.GradleWrapperMain \ + "$@" + +# Stop when "xargs" is not available. +if ! command -v xargs >/dev/null 2>&1 +then + die "xargs is not available" +fi + +# Use "xargs" to parse quoted args. +# +# With -n1 it outputs one arg per line, with the quotes and backslashes removed. +# +# In Bash we could simply go: +# +# readarray ARGS < <( xargs -n1 <<<"$var" ) && +# set -- "${ARGS[@]}" "$@" +# +# but POSIX shell has neither arrays nor command substitution, so instead we +# post-process each arg (as a line of input to sed) to backslash-escape any +# character that might be a shell metacharacter, then use eval to reverse +# that process (while maintaining the separation between arguments), and wrap +# the whole thing up as a single "set" statement. +# +# This will of course break if any of these variables contains a newline or +# an unmatched quote. +# + +eval "set -- $( + printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | + xargs -n1 | + sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | + tr '\n' ' ' + )" '"$@"' + +exec "$JAVACMD" "$@" diff --git a/java/gradlew.bat b/java/gradlew.bat new file mode 100644 index 0000000..25da30d --- /dev/null +++ b/java/gradlew.bat @@ -0,0 +1,92 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem + +@if "%DEBUG%"=="" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%"=="" set DIRNAME=. +@rem This is normally unused +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if %ERRORLEVEL% equ 0 goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* + +:end +@rem End local scope for the variables with windows NT shell +if %ERRORLEVEL% equ 0 goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +set EXIT_CODE=%ERRORLEVEL% +if %EXIT_CODE% equ 0 set EXIT_CODE=1 +if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% +exit /b %EXIT_CODE% + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/java/lib/build.gradle.kts b/java/lib/build.gradle.kts new file mode 100644 index 0000000..96541a6 --- /dev/null +++ b/java/lib/build.gradle.kts @@ -0,0 +1,42 @@ +plugins { + // Apply the java-library plugin for API and implementation separation. + `java-library` +} + +repositories { + // Use Maven Central for resolving dependencies. + mavenCentral() +} + +dependencies { + // Use JUnit Jupiter for testing. + testImplementation(libs.junit.jupiter) + + testRuntimeOnly("org.junit.platform:junit-platform-launcher") + + // This dependency is exported to consumers, that is to say found on their compile classpath. + api(libs.commons.math3) + + // This dependency is used internally, and not exposed to consumers on their own compile classpath. + implementation(libs.guava) +} + +// Apply a specific Java toolchain to ease working on different environments. +java { + toolchain { + languageVersion = JavaLanguageVersion.of(21) + } +} + +tasks.named("test") { + // Use JUnit Platform for unit tests. + useJUnitPlatform() + // Set the JVM argument for the java.library.path (To import rust compiled library) + val rustLibPath = projectDir.resolve("../../target/release").absolutePath + jvmArgs = listOf("-Djava.library.path=$rustLibPath") +} + +tasks.named("javadoc") { + options.encoding = "UTF-8" + options.memberLevel = JavadocMemberLevel.PUBLIC +} diff --git a/java/lib/src/main/java/dev/arkbuilders/core/FileStorage.java b/java/lib/src/main/java/dev/arkbuilders/core/FileStorage.java new file mode 100644 index 0000000..a405f2c --- /dev/null +++ b/java/lib/src/main/java/dev/arkbuilders/core/FileStorage.java @@ -0,0 +1,136 @@ +package dev.arkbuilders.core; + +/** + * Represents a file storage system that persists data to disk. + */ +public class FileStorage { + private long fileStoragePtr; + + static { + System.loadLibrary("fs_storage"); + } + + /** + * Represents the synchronization status between in-memory key-value mapping and + * external file system storage. + */ + public enum SyncStatus { + /** + * No synchronization needed + */ + InSync, + /** + * In-memory key-value mapping is stale + */ + MappingStale, + /** + * External file system storage is stale + */ + StorageStale, + /** + * In-memory key-value mapping and external file system storage diverge + */ + Diverge, + } + + private static native long create(String label, String path); + + private static native void set(String id, String value, long file_storage_ptr); + + private static native void remove(String id, long file_storage_ptr); + + private static native void sync(long file_storage_ptr); + + private static native SyncStatus syncStatus(long file_storage_ptr); + + private static native Object readFS(long file_storage_ptr); + + private static native void writeFS(long file_storage_ptr); + + private static native void erase(long file_storage_ptr); + + private static native void merge(long file_storage_ptr, long other_file_storage_ptr); + + /** + * Creates a new file storage system. + * + * @param label The label of the file storage system. + * @param path The path to the file storage system. + */ + public FileStorage(String label, String path) { + this.fileStoragePtr = create(label, path); + } + + /** + * Set a key-value pair in the internal mapping. + * + * @param id The key. + * @param value The value. + */ + public void set(String id, String value) { + set(id, value, this.fileStoragePtr); + } + + /** + * Remove an entry from the internal mapping given a key. + * + * @param id The key. + */ + public void remove(String id) { + remove(id, this.fileStoragePtr); + } + + /** + * Sync the in-memory storage with the storage on disk. + */ + public void sync() { + sync(this.fileStoragePtr); + } + + /** + * Compare the timestamp of the storage file with the timestamp of the in-memory + * storage and the last written to time to determine if either of the two + * requires syncing. + * + * @return The synchronization status. + */ + public SyncStatus syncStatus() { + return syncStatus(this.fileStoragePtr); + } + + /** + * Read the data from file + * + * @return The file storage system. + */ + public Object readFS() { + return readFS(this.fileStoragePtr); + } + + /** + * Write the data to file. + * + * Note: Update the modified timestamp in file metadata to avoid OS timing + * issues. + * See https://github.com/ARK-Builders/ark-rust/pull/63#issuecomment-2163882227 + */ + public void writeFS() { + writeFS(this.fileStoragePtr); + } + + /** + * Erase the file from disk + */ + public void erase() { + erase(this.fileStoragePtr); + } + + /** + * Merge the data from another storage instance into this storage instance + * + * @param other The other storage instance + */ + public void merge(FileStorage other) { + merge(this.fileStoragePtr, other.fileStoragePtr); + } +} diff --git a/fs-storage/tests/FileStorageTest.java b/java/lib/src/test/java/dev/arkbuilders/core/FileStorageTest.java similarity index 95% rename from fs-storage/tests/FileStorageTest.java rename to java/lib/src/test/java/dev/arkbuilders/core/FileStorageTest.java index f984474..49c69e7 100644 --- a/fs-storage/tests/FileStorageTest.java +++ b/java/lib/src/test/java/dev/arkbuilders/core/FileStorageTest.java @@ -1,3 +1,5 @@ +package dev.arkbuilders.core; + import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; @@ -52,11 +54,11 @@ public void testFileStorageNeedsSyncing() { Path storagePath = tempDir.resolve("test.txt"); FileStorage fileStorage = new FileStorage("test", storagePath.toString()); fileStorage.writeFS(); - assertEquals(SyncStatus.InSync, fileStorage.syncStatus()); + assertEquals(FileStorage.SyncStatus.InSync, fileStorage.syncStatus()); fileStorage.set("key1", "value1"); - assertEquals(SyncStatus.StorageStale, fileStorage.syncStatus()); + assertEquals(FileStorage.SyncStatus.StorageStale, fileStorage.syncStatus()); fileStorage.writeFS(); - assertEquals(SyncStatus.InSync, fileStorage.syncStatus()); + assertEquals(FileStorage.SyncStatus.InSync, fileStorage.syncStatus()); } @Test diff --git a/java/settings.gradle.kts b/java/settings.gradle.kts new file mode 100644 index 0000000..2a9d8c0 --- /dev/null +++ b/java/settings.gradle.kts @@ -0,0 +1,14 @@ +/* + * This file was generated by the Gradle 'init' task. + * + * The settings file is used to specify which projects to include in your build. + * For more detailed information on multi-project builds, please refer to https://docs.gradle.org/8.8/userguide/multi_project_builds.html in the Gradle documentation. + */ + +plugins { + // Apply the foojay-resolver plugin to allow automatic download of JDKs + id("org.gradle.toolchains.foojay-resolver-convention") version "0.8.0" +} + +rootProject.name = "ark" +include("lib")