diff --git a/LICENSE b/LICENSE index cf6947b..dbbb95c 100644 --- a/LICENSE +++ b/LICENSE @@ -186,7 +186,7 @@ same "printed page" as the copyright notice for easier identification within third-party archives. - Copyright 2022 Tatsuya Kawano + Copyright 2022 - 2024 Tatsuya Kawano Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. diff --git a/README.md b/README.md index b08fb3d..481d46d 100644 --- a/README.md +++ b/README.md @@ -21,8 +21,8 @@ The driver consists of two parts: `native` (Rust) functions. 2. A Rust library that wraps Moka cache and implements the functions called by the Java class. - - This library uses [jni crate][jni-crate], which provides a safe wrapper around - the JNI API. + - This library uses [`jni` crate][jni-crate], which provides a safe wrapper + around the JNI API. The Rust library is compiled into a dynamic library that is loaded into the Java VM at runtime. @@ -33,7 +33,7 @@ at runtime. ## Prerequisites - Java JDK to build the Caffeine Simulator and the Java part of the driver. -- Rust stable toolchain (1.51 or newer) to build Moka and the Rust part of the driver. +- Rust stable toolchain (1.75 or newer) to build Moka and the Rust part of the driver. ## Building the Driver @@ -57,7 +57,7 @@ $ cargo build --release Clone Caffeine's repository, and checkout a specific Git revision: ```console -$ REVISION=6800aa6573361e440c77d58b22e54c16d0ce2505 +$ REVISION=4ba734a6cf2f7243c77d2ad8ea9d941f6e36175c $ cd $SIM $ git clone https://github.com/ben-manes/caffeine.git @@ -98,38 +98,65 @@ Edit `application.conf` and add the following line in the `policies` section: ```properties policies = [ - # ... - product.Moka, + opt.Clairvoyant, + ... + linked.Lru, + ... + sketch.WindowTinyLfu, + ... + product.Moka, # <--- Add this line. + ] + + admission = [ + Always, + TinyLfu, ] ``` Build and run the Caffeine Simulator: ```console -## Replace `/path/to/trace/S3.lis` with the real path to the trace file: -$ TRACE=arc:/path/to/trace/S3.lis - -## The path to the directory containing the dynamic library: +## The path to the directory containing the dynamic library. $ DRV_LIB=$SIM/caffeine-sim-drivers/moka-driver-rs/target/release +## The path to the directory containing the ARC trace files. +## Replace `/path/to/...` with the real path. +$ ARC_DIR=/path/to/arc-trace-directory + +## The path to the directory containing the Corda trace files. +$ CORDA_DIR=$SIM/caffeine/simulator/src/main/resources/com/github/benmanes/caffeine/cache/simulator/parser/corda/ + $ cd $SIM/caffeine + +## Run the simulator against the ARC S3 trace file. $ ./gradlew simulator:simulate -q \ - -Dcaffeine.simulator.files.paths.0=$TRACE \ - --maximumSize=100_000,200_000,300_000,400_000,500_000,600_000,700_000,800_000 \ - --jvmArgs="-XX:+UseParallelGC,-Xmx8g,-Djava.library.path=$DRV_LIB" \ - --theme=light + -Dcaffeine.simulator.files.paths.0=arc:$ARC_DIR/S3.lis \ + --maximumSize=100_000,200_000,300_000,400_000,500_000,600_000,700_000,800_000 \ + -PjvmArgs="-XX:+UseParallelGC,-Xmx8g,-Djava.library.path=$DRV_LIB" \ + --theme=light + +$ mv $SIM/caffeine/simulator/build/reports/simulate{,-arc-s3} + +## Run the simulator against the Corda vault service large trace file. +$ ./gradlew simulator:simulate -q \ + -Dcaffeine.simulator.files.paths.0=corda:$CORDA_DIR/trace_vaultservice_large.gz \ + --maximumSize=200_000,400_000,600_000,800_000,1_000_000,1_200_000,1_400_000,1_600_000 \ + -PjvmArgs="-XX:+UseParallelGC,-Xmx8g,-Djava.library.path=$DRV_LIB" \ + --theme=light + +$ mv $SIM/caffeine/simulator/build/reports/simulate{,-corda-large} ``` ## Modifying the Driver If you want to modify the driver, e.g., to drive your own cache implementation, check -out the driver's codes and the "Getting Started" section of the jni crate's +out the driver's codes and the "Getting Started" section of the `jni` crate's documentation: - Driver's source code: - Java part: [MokaPolicy.java](./moka-driver-java/MokaPolicy.java) - Rust part: [src/lib.rs](./moka-driver-rs/src/lib.rs) -- jni crate: [Getting Started][jni-crate-getting-started] +- `jni` crate: [Getting Started][jni-crate-getting-started] [jni-crate-getting-started]: https://docs.rs/jni/latest/jni/index.html#getting-started diff --git a/moka-driver-java/MokaPolicy.java b/moka-driver-java/MokaPolicy.java index e761a39..7900074 100644 --- a/moka-driver-java/MokaPolicy.java +++ b/moka-driver-java/MokaPolicy.java @@ -2,6 +2,8 @@ import static com.github.benmanes.caffeine.cache.simulator.policy.Policy.Characteristic.WEIGHTED; +import java.util.Collections; +import java.util.HashSet; import java.util.Set; import com.github.benmanes.caffeine.cache.simulator.BasicSettings; @@ -13,38 +15,55 @@ @PolicySpec(name = "product.Moka", characteristics = WEIGHTED) public final class MokaPolicy implements Policy { + private final long cachePointer; private final PolicyStats policyStats; static { System.loadLibrary("moka"); } - public MokaPolicy(Config config, Set characteristics) { - policyStats = new PolicyStats(name()); + public MokaPolicy(Config config, String evictionPolicy) { + policyStats = new PolicyStats(name() + " (%s)", evictionPolicy); BasicSettings settings = new BasicSettings(config); long maximumSize = settings.maximumSize(); - boolean isWeighted = characteristics.contains(WEIGHTED); - initCache(maximumSize, isWeighted); + boolean isWeighted = false; + cachePointer = initCache(maximumSize, isWeighted, evictionPolicy); + } + + // public MokaPolicy(Config config, Set characteristics) { + // policyStats = new PolicyStats(name()); + // BasicSettings settings = new BasicSettings(config); + // long maximumSize = settings.maximumSize(); + // boolean isWeighted = characteristics.contains(WEIGHTED); + // String evictionPolicy = "TinyLFU"; + // cachePointer = initCache(maximumSize, isWeighted, evictionPolicy); + // } + + public static Set policies(Config config) { + HashSet policies = new HashSet<>(); + policies.add(new MokaPolicy(config, "TinyLFU")); + policies.add(new MokaPolicy(config, "LRU")); + return Collections.unmodifiableSet(policies); } @Override public void record(AccessEvent event) { - int value = getFromCacheIfPresent(event.key()); + int value = getFromCacheIfPresent(cachePointer, event.key()); if (value == -1) { - putToCache(event.key(), event.weight()); + putToCache(cachePointer, event.key(), event.weight()); policyStats.recordWeightedMiss(event.weight()); } else { policyStats.recordWeightedHit(event.weight()); if (event.weight() != value) { - putToCache(event.key(), event.weight()); + putToCache(cachePointer, event.key(), event.weight()); } } } - // @Override - // public void finished() { - // - // } + @Override + public void finished() { + dropCache(cachePointer); + } @Override public PolicyStats stats() { @@ -56,30 +75,43 @@ public PolicyStats stats() { * --------------------------------------------------------------------------- */ /** - * Creates the shared singleton instance of the Moka cache with given - * parameters. Currently, moka::sync::Cache is used. + * Creates an instance of the Moka cache with given parameters and returns + * the pointer to the instance as a long value. Currently, + * moka::sync::Cache is used. * * @param maximumSize * @param isWeighted + * @param evictionPolicy The eviction policy to use. TinyLFU or + * LRU. + * @return The pointer to the instance of the Moka cache. */ - private static native void initCache(long maximumSize, boolean isWeighted); + private static native long initCache(long maximumSize, boolean isWeighted, String evictionPolicy); /** * Returns the value (which is the weight in int) of the given key if * exists. Otherwise returns -1. * + * @param cachePointer * @param key * @return The weight of the key if exists. Otherwise -1. */ - private static native int getFromCacheIfPresent(long key); + private static native int getFromCacheIfPresent(long cachePointer, long key); /** * Stores the value (which is the weight in int) for the given key. * Updates the value if already exists. * + * @param cachePointer * @param key * @param weight */ - private static native void putToCache(long key, int weight); + private static native void putToCache(long cachePointer, long key, int weight); + + /** + * Drop the cache. + * + * @param cachePointer + */ + private static native void dropCache(long cachePointer); } diff --git a/moka-driver-java/registry-patch.diff b/moka-driver-java/registry-patch.diff index 609d755..68f4e0d 100644 --- a/moka-driver-java/registry-patch.diff +++ b/moka-driver-java/registry-patch.diff @@ -1,21 +1,20 @@ diff --git a/simulator/src/main/java/com/github/benmanes/caffeine/cache/simulator/policy/Registry.java b/simulator/src/main/java/com/github/benmanes/caffeine/cache/simulator/policy/Registry.java -index cf4f6b36..2882418e 100644 +index 28207f6a..7a66cb81 100644 --- a/simulator/src/main/java/com/github/benmanes/caffeine/cache/simulator/policy/Registry.java +++ b/simulator/src/main/java/com/github/benmanes/caffeine/cache/simulator/policy/Registry.java -@@ -79,6 +79,8 @@ import com.google.auto.value.AutoValue; +@@ -78,6 +78,7 @@ import com.github.benmanes.caffeine.cache.simulator.policy.two_queue.TwoQueuePol + import com.google.auto.value.AutoValue; import com.google.common.collect.ImmutableSet; import com.typesafe.config.Config; - +import io.crates.moka.cache.simulator.policy.product.MokaPolicy; -+ + /** * The registry of caching policies. - * -@@ -229,6 +231,7 @@ public final class Registry { +@@ -230,6 +231,7 @@ public final class Registry { registerMany(CoherencePolicy.class, CoherencePolicy::policies); registerMany(HazelcastPolicy.class, HazelcastPolicy::policies); registerMany(ExpiringMapPolicy.class, ExpiringMapPolicy::policies); -+ register(MokaPolicy.class, MokaPolicy::new); ++ registerMany(MokaPolicy.class, MokaPolicy::policies); } @AutoValue diff --git a/moka-driver-rs/.vscode/settings.json b/moka-driver-rs/.vscode/settings.json new file mode 100644 index 0000000..1cbe555 --- /dev/null +++ b/moka-driver-rs/.vscode/settings.json @@ -0,0 +1,7 @@ +{ + "cSpell.words": [ + "jlong", + "Moka", + "tinylfu" + ] +} diff --git a/moka-driver-rs/Cargo.lock b/moka-driver-rs/Cargo.lock index 4a465a9..35b30a6 100644 --- a/moka-driver-rs/Cargo.lock +++ b/moka-driver-rs/Cargo.lock @@ -4,9 +4,9 @@ version = 3 [[package]] name = "autocfg" -version = "1.1.0" +version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" +checksum = "f1fdabc7756949593fe60f30ec81974b613357de856987752631dea1e3394c80" [[package]] name = "bitflags" @@ -15,53 +15,22 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" [[package]] -name = "bumpalo" -version = "3.11.1" +name = "bitflags" +version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "572f695136211188308f16ad2ca5c851a712c464060ae6974944458eb83880ba" +checksum = "cf4b9d6a944f767f8e5e0db018570623c85f3d925ac718db4e06d0187adb21c1" [[package]] -name = "bytecount" -version = "0.6.3" +name = "bumpalo" +version = "3.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2c676a478f63e9fa2dd5368a42f28bba0d6c560b775f38583c8bbaa7fcd67c9c" +checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c" [[package]] name = "bytes" -version = "1.2.1" +version = "1.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ec8a7b6a70fde80372154c65702f00a0f56f3e1c36abbc6c440484be248856db" - -[[package]] -name = "camino" -version = "1.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "88ad0e1e3e88dd237a156ab9f571021b8a158caa0ae44b1968a241efb5144c1e" -dependencies = [ - "serde", -] - -[[package]] -name = "cargo-platform" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cbdb825da8a5df079a43676dbe042702f1707b1109f713a01420fbb4cc71fa27" -dependencies = [ - "serde", -] - -[[package]] -name = "cargo_metadata" -version = "0.14.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4acbb09d9ee8e23699b9634375c72795d095bf268439da88562cf9b501f181fa" -dependencies = [ - "camino", - "cargo-platform", - "semver", - "serde", - "serde_json", -] +checksum = "514de17de45fdb8dc022b1a7975556c53c86f9f0aa5f534b98977b171857c2c9" [[package]] name = "cesu8" @@ -77,9 +46,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "combine" -version = "4.6.6" +version = "4.6.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "35ed6e9d84f0b51a7f52daf1c7d71dd136fd7a3f41a8462b8cdb8c78d920fad4" +checksum = "ba5a308b75df32fe02788e748662718f03fde005016435c444eea572398219fd" dependencies = [ "bytes", "memchr", @@ -87,107 +56,53 @@ dependencies = [ [[package]] name = "crossbeam-channel" -version = "0.5.6" +version = "0.5.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c2dd04ddaf88237dc3b8d8f9a3c1004b506b54b3313403944054d23c0870c521" +checksum = "ab3db02a9c5b5121e1e42fbdb1aeb65f5e02624cc58c43f2884c6ccac0b82f95" dependencies = [ - "cfg-if", "crossbeam-utils", ] [[package]] name = "crossbeam-epoch" -version = "0.9.11" +version = "0.9.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f916dfc5d356b0ed9dae65f1db9fc9770aa2851d2662b988ccf4fe3516e86348" +checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e" dependencies = [ - "autocfg", - "cfg-if", "crossbeam-utils", - "memoffset", - "scopeguard", ] [[package]] name = "crossbeam-utils" -version = "0.8.12" +version = "0.8.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "edbafec5fa1f196ca66527c1b12c2ec4745ca14b50f1ad8f9f6f720b55d11fac" -dependencies = [ - "cfg-if", -] - -[[package]] -name = "error-chain" -version = "0.12.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2d2f06b9cac1506ece98fe3231e3cc9c4410ec3d5b1f24ae1c8946f0742cdefc" -dependencies = [ - "version_check", -] - -[[package]] -name = "fastrand" -version = "1.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a7a407cfaa3385c4ae6b23e84623d48c2798d06e3e6a1878f7f59f17b3f86499" -dependencies = [ - "instant", -] +checksum = "248e3bacc7dc6baa3b21e405ee045c3047101a49145e7e9eca583ab4c2ca5345" [[package]] name = "getrandom" -version = "0.2.8" +version = "0.2.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c05aeb6a22b8f62540c194aac980f2115af067bfe15a0734d7277a768d396b31" +checksum = "94b22e06ecb0110981051723910cbf0b5f5e09a2062dd7663334ee79a9d1286c" dependencies = [ "cfg-if", "libc", - "wasi 0.11.0+wasi-snapshot-preview1", -] - -[[package]] -name = "glob" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574" - -[[package]] -name = "hermit-abi" -version = "0.1.19" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" -dependencies = [ - "libc", + "wasi", ] -[[package]] -name = "instant" -version = "0.1.12" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a5bbe824c507c5da5956355e86a746d82e0e1464f65d862cc5e71da70e94b2c" -dependencies = [ - "cfg-if", -] - -[[package]] -name = "itoa" -version = "1.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4217ad341ebadf8d8e724e264f13e593e0648f5b3e94b3896a5df283be015ecc" - [[package]] name = "jni" -version = "0.20.0" +version = "0.21.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "039022cdf4d7b1cf548d31f60ae783138e5fd42013f6271049d7df7afadef96c" +checksum = "1a87aa2bb7d2af34197c04845522473242e1aa17c12f4935d5856491a7fb8c97" dependencies = [ "cesu8", + "cfg-if", "combine", "jni-sys", "log", "thiserror", "walkdir", + "windows-sys", ] [[package]] @@ -198,24 +113,24 @@ checksum = "8eaf4bc02d17cbdd7ff4c7438cafcdf7fb9a4613313ad11b4f8fefe7d3fa0130" [[package]] name = "js-sys" -version = "0.3.60" +version = "0.3.69" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "49409df3e3bf0856b916e2ceaca09ee28e6871cf7d9ce97a692cacfdb2a25a47" +checksum = "29c15563dc2726973df627357ce0c9ddddbea194836909d655df6a75d2cf296d" dependencies = [ "wasm-bindgen", ] [[package]] name = "libc" -version = "0.2.137" +version = "0.2.153" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fc7fcc620a3bff7cdd7a365be3376c97191aeaccc2a603e600951e452615bf89" +checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd" [[package]] name = "lock_api" -version = "0.4.9" +version = "0.4.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "435011366fe56583b16cf956f9df0095b405b82d76425bc8981c0e22e60ec4df" +checksum = "3c168f8615b12bc01f9c17e2eb0cc07dcae1940121185446edc3744920e8ef45" dependencies = [ "autocfg", "scopeguard", @@ -223,53 +138,29 @@ dependencies = [ [[package]] name = "log" -version = "0.4.17" +version = "0.4.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e" -dependencies = [ - "cfg-if", -] - -[[package]] -name = "mach" -version = "0.3.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b823e83b2affd8f40a9ee8c29dbc56404c1e34cd2710921f2801e2cf29527afa" -dependencies = [ - "libc", -] +checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c" [[package]] name = "memchr" -version = "2.5.0" +version = "2.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" - -[[package]] -name = "memoffset" -version = "0.6.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5aa361d4faea93603064a027415f07bd8e1d5c88c9fbf68bf56a285428fd79ce" -dependencies = [ - "autocfg", -] +checksum = "6c8640c5d730cb13ebd907d8d04b52f55ac9a2eec55b440c8892f40d56c76c1d" [[package]] name = "moka" -version = "0.9.6" +version = "0.12.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7b49a05f67020456541f4f29cbaa812016a266a86ec76f96d3873d459c68fe5e" +checksum = "9e0d88686dc561d743b40de8269b26eaf0dc58781bde087b0984646602021d08" dependencies = [ "crossbeam-channel", "crossbeam-epoch", "crossbeam-utils", - "num_cpus", "once_cell", "parking_lot", "quanta", "rustc_version", - "scheduled-thread-pool", - "skeptic", "smallvec", "tagptr", "thiserror", @@ -279,29 +170,18 @@ dependencies = [ [[package]] name = "moka-driver" -version = "0.1.0" +version = "0.2.0" dependencies = [ "jni", "moka", - "once_cell", "xxhash-rust", ] -[[package]] -name = "num_cpus" -version = "1.14.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f6058e64324c71e02bc2b150e4f3bc8286db6c83092132ffa3f6b1eab0f9def5" -dependencies = [ - "hermit-abi", - "libc", -] - [[package]] name = "once_cell" -version = "1.16.0" +version = "1.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "86f0b0d4bf799edbc74508c1e8bf170ff5f41238e5f8225603ca7caaae2b7860" +checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" [[package]] name = "parking_lot" @@ -315,87 +195,66 @@ dependencies = [ [[package]] name = "parking_lot_core" -version = "0.9.4" +version = "0.9.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4dc9e0dc2adc1c69d09143aff38d3d30c5c3f0df0dad82e6d25547af174ebec0" +checksum = "4c42a9226546d68acdd9c0a280d17ce19bfe27a46bf68784e4066115788d008e" dependencies = [ "cfg-if", "libc", "redox_syscall", "smallvec", - "windows-sys", + "windows-targets 0.48.5", ] [[package]] name = "proc-macro2" -version = "1.0.47" +version = "1.0.81" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5ea3d908b0e36316caf9e9e2c4625cdde190a7e6f440d794667ed17a1855e725" +checksum = "3d1597b0c024618f09a9c3b8655b7e430397a36d23fdafec26d6965e9eec3eba" dependencies = [ "unicode-ident", ] -[[package]] -name = "pulldown-cmark" -version = "0.9.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2d9cc634bc78768157b5cbfe988ffcd1dcba95cd2b2f03a88316c08c6d00ed63" -dependencies = [ - "bitflags", - "memchr", - "unicase", -] - [[package]] name = "quanta" -version = "0.10.1" +version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b7e31331286705f455e56cca62e0e717158474ff02b7936c1fa596d983f4ae27" +checksum = "8e5167a477619228a0b284fac2674e3c388cba90631d7b7de620e6f1fcd08da5" dependencies = [ "crossbeam-utils", "libc", - "mach", "once_cell", "raw-cpuid", - "wasi 0.10.2+wasi-snapshot-preview1", + "wasi", "web-sys", "winapi", ] [[package]] name = "quote" -version = "1.0.21" +version = "1.0.36" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bbe448f377a7d6961e30f5955f9b8d106c3f5e449d493ee1b125c1d43c2b5179" +checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7" dependencies = [ "proc-macro2", ] [[package]] name = "raw-cpuid" -version = "10.6.0" +version = "11.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a6823ea29436221176fe662da99998ad3b4db2c7f31e7b6f5fe43adccd6320bb" +checksum = "9d86a7c4638d42c44551f4791a20e687dbb4c3de1f33c43dd71e355cd429def1" dependencies = [ - "bitflags", + "bitflags 2.5.0", ] [[package]] name = "redox_syscall" -version = "0.2.16" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fb5a58c1855b4b6819d59012155603f0b22ad30cad752600aadfcb695265519a" -dependencies = [ - "bitflags", -] - -[[package]] -name = "remove_dir_all" -version = "0.5.3" +version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3acd125665422973a33ac9d3dd2df85edad0f4ae9b00dafb1a05e43a9f5ef8e7" +checksum = "4722d768eff46b75989dd134e5c353f0d6296e5aaa3132e776cbdb56be7731aa" dependencies = [ - "winapi", + "bitflags 1.3.2", ] [[package]] @@ -407,12 +266,6 @@ dependencies = [ "semver", ] -[[package]] -name = "ryu" -version = "1.0.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4501abdff3ae82a1c1b477a17252eb69cee9e66eb915c1abaa4f44d873df9f09" - [[package]] name = "same-file" version = "1.0.6" @@ -422,87 +275,29 @@ dependencies = [ "winapi-util", ] -[[package]] -name = "scheduled-thread-pool" -version = "0.2.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "977a7519bff143a44f842fd07e80ad1329295bd71686457f18e496736f4bf9bf" -dependencies = [ - "parking_lot", -] - [[package]] name = "scopeguard" -version = "1.1.0" +version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" +checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" [[package]] name = "semver" -version = "1.0.14" +version = "1.0.22" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e25dfac463d778e353db5be2449d1cce89bd6fd23c9f1ea21310ce6e5a1b29c4" -dependencies = [ - "serde", -] - -[[package]] -name = "serde" -version = "1.0.147" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d193d69bae983fc11a79df82342761dfbf28a99fc8d203dca4c3c1b590948965" -dependencies = [ - "serde_derive", -] - -[[package]] -name = "serde_derive" -version = "1.0.147" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4f1d362ca8fc9c3e3a7484440752472d68a6caa98f1ab81d99b5dfe517cec852" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "serde_json" -version = "1.0.87" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6ce777b7b150d76b9cf60d28b55f5847135a003f7d7350c6be7a773508ce7d45" -dependencies = [ - "itoa", - "ryu", - "serde", -] - -[[package]] -name = "skeptic" -version = "0.13.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "16d23b015676c90a0f01c197bfdc786c20342c73a0afdda9025adb0bc42940a8" -dependencies = [ - "bytecount", - "cargo_metadata", - "error-chain", - "glob", - "pulldown-cmark", - "tempfile", - "walkdir", -] +checksum = "92d43fe69e652f3df9bdc2b85b2854a0825b86e4fb76bc44d945137d053639ca" [[package]] name = "smallvec" -version = "1.10.0" +version = "1.13.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a507befe795404456341dfab10cef66ead4c041f62b8b11bbb92bffe5d0953e0" +checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67" [[package]] name = "syn" -version = "1.0.103" +version = "2.0.60" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a864042229133ada95abf3b54fdc62ef5ccabe9515b64717bcb9a1919e59445d" +checksum = "909518bc7b1c9b779f1bbf07f2929d35af9f0f37e47c6e9ef7f9dddc1e1821f3" dependencies = [ "proc-macro2", "quote", @@ -515,34 +310,20 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7b2093cf4c8eb1e67749a6762251bc9cd836b6fc171623bd0a9d324d37af2417" -[[package]] -name = "tempfile" -version = "3.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5cdb1ef4eaeeaddc8fbd371e5017057064af0911902ef36b39801f67cc6d79e4" -dependencies = [ - "cfg-if", - "fastrand", - "libc", - "redox_syscall", - "remove_dir_all", - "winapi", -] - [[package]] name = "thiserror" -version = "1.0.37" +version = "1.0.59" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "10deb33631e3c9018b9baf9dcbbc4f737320d2b576bac10f6aefa048fa407e3e" +checksum = "f0126ad08bff79f29fc3ae6a55cc72352056dfff61e3ff8bb7129476d44b23aa" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.37" +version = "1.0.59" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "982d17546b47146b28f7c22e3d08465f6b8903d0ea13c1660d9d84a6e7adcdbb" +checksum = "d1cd413b5d558b4c5bf3680e324a6fa5014e7b7c067a51e69dbdf47eb7148b66" dependencies = [ "proc-macro2", "quote", @@ -551,57 +332,35 @@ dependencies = [ [[package]] name = "triomphe" -version = "0.1.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f1ee9bd9239c339d714d657fac840c6d2a4f9c45f4f9ec7b0975113458be78db" - -[[package]] -name = "unicase" -version = "2.6.0" +version = "0.1.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "50f37be617794602aabbeee0be4f259dc1778fabe05e2d67ee8f79326d5cb4f6" -dependencies = [ - "version_check", -] +checksum = "859eb650cfee7434994602c3a68b25d77ad9e68c8a6cd491616ef86661382eb3" [[package]] name = "unicode-ident" -version = "1.0.5" +version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6ceab39d59e4c9499d4e5a8ee0e2735b891bb7308ac83dfb4e80cad195c9f6f3" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" [[package]] name = "uuid" -version = "1.2.1" +version = "1.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "feb41e78f93363bb2df8b0e86a2ca30eed7806ea16ea0c790d757cf93f79be83" +checksum = "a183cf7feeba97b4dd1c0d46788634f6221d87fa961b305bed08c851829efcc0" dependencies = [ "getrandom", ] -[[package]] -name = "version_check" -version = "0.9.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" - [[package]] name = "walkdir" -version = "2.3.2" +version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "808cf2735cd4b6866113f648b791c6adc5714537bc222d9347bb203386ffda56" +checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b" dependencies = [ "same-file", - "winapi", "winapi-util", ] -[[package]] -name = "wasi" -version = "0.10.2+wasi-snapshot-preview1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6" - [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" @@ -610,9 +369,9 @@ checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" [[package]] name = "wasm-bindgen" -version = "0.2.83" +version = "0.2.92" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eaf9f5aceeec8be17c128b2e93e031fb8a4d469bb9c4ae2d7dc1888b26887268" +checksum = "4be2531df63900aeb2bca0daaaddec08491ee64ceecbee5076636a3b026795a8" dependencies = [ "cfg-if", "wasm-bindgen-macro", @@ -620,9 +379,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-backend" -version = "0.2.83" +version = "0.2.92" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4c8ffb332579b0557b52d268b91feab8df3615f265d5270fec2a8c95b17c1142" +checksum = "614d787b966d3989fa7bb98a654e369c762374fd3213d212cfc0251257e747da" dependencies = [ "bumpalo", "log", @@ -635,9 +394,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro" -version = "0.2.83" +version = "0.2.92" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "052be0f94026e6cbc75cdefc9bae13fd6052cdcaf532fa6c45e7ae33a1e6c810" +checksum = "a1f8823de937b71b9460c0c34e25f3da88250760bec0ebac694b49997550d726" dependencies = [ "quote", "wasm-bindgen-macro-support", @@ -645,9 +404,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro-support" -version = "0.2.83" +version = "0.2.92" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "07bc0c051dc5f23e307b13285f9d75df86bfdf816c5721e573dec1f9b8aa193c" +checksum = "e94f17b526d0a461a191c78ea52bbce64071ed5c04c9ffe424dcb38f74171bb7" dependencies = [ "proc-macro2", "quote", @@ -658,15 +417,15 @@ dependencies = [ [[package]] name = "wasm-bindgen-shared" -version = "0.2.83" +version = "0.2.92" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1c38c045535d93ec4f0b4defec448e4291638ee608530863b1e2ba115d4fff7f" +checksum = "af190c94f2773fdb3729c55b007a722abb5384da03bc0986df4c289bf5567e96" [[package]] name = "web-sys" -version = "0.3.60" +version = "0.3.69" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bcda906d8be16e728fd5adc5b729afad4e444e106ab28cd1c7256e54fa61510f" +checksum = "77afa9a11836342370f4817622a2f0f418b134426d91a82dfb48f532d2ec13ef" dependencies = [ "js-sys", "wasm-bindgen", @@ -690,9 +449,9 @@ checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" [[package]] name = "winapi-util" -version = "0.1.5" +version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" +checksum = "f29e6f9198ba0d26b4c9f07dbe6f9ed633e1f3d5b8b414090084349e46a52596" dependencies = [ "winapi", ] @@ -705,63 +464,129 @@ checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" [[package]] name = "windows-sys" -version = "0.42.0" +version = "0.45.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75283be5efb2831d37ea142365f009c02ec203cd29a3ebecbc093d52315b66d0" +dependencies = [ + "windows-targets 0.42.2", +] + +[[package]] +name = "windows-targets" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e5180c00cd44c9b1c88adb3693291f1cd93605ded80c250a75d472756b4d071" +dependencies = [ + "windows_aarch64_gnullvm 0.42.2", + "windows_aarch64_msvc 0.42.2", + "windows_i686_gnu 0.42.2", + "windows_i686_msvc 0.42.2", + "windows_x86_64_gnu 0.42.2", + "windows_x86_64_gnullvm 0.42.2", + "windows_x86_64_msvc 0.42.2", +] + +[[package]] +name = "windows-targets" +version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5a3e1820f08b8513f676f7ab6c1f99ff312fb97b553d30ff4dd86f9f15728aa7" +checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" dependencies = [ - "windows_aarch64_gnullvm", - "windows_aarch64_msvc", - "windows_i686_gnu", - "windows_i686_msvc", - "windows_x86_64_gnu", - "windows_x86_64_gnullvm", - "windows_x86_64_msvc", + "windows_aarch64_gnullvm 0.48.5", + "windows_aarch64_msvc 0.48.5", + "windows_i686_gnu 0.48.5", + "windows_i686_msvc 0.48.5", + "windows_x86_64_gnu 0.48.5", + "windows_x86_64_gnullvm 0.48.5", + "windows_x86_64_msvc 0.48.5", ] [[package]] name = "windows_aarch64_gnullvm" -version = "0.42.0" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "597a5118570b68bc08d8d59125332c54f1ba9d9adeedeef5b99b02ba2b0698f8" + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "41d2aa71f6f0cbe00ae5167d90ef3cfe66527d6f613ca78ac8024c3ccab9a19e" +checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" [[package]] name = "windows_aarch64_msvc" -version = "0.42.0" +version = "0.42.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dd0f252f5a35cac83d6311b2e795981f5ee6e67eb1f9a7f64eb4500fbc4dcdb4" +checksum = "e08e8864a60f06ef0d0ff4ba04124db8b0fb3be5776a5cd47641e942e58c4d43" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" + +[[package]] +name = "windows_i686_gnu" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c61d927d8da41da96a81f029489353e68739737d3beca43145c8afec9a31a84f" [[package]] name = "windows_i686_gnu" -version = "0.42.0" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" + +[[package]] +name = "windows_i686_msvc" +version = "0.42.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fbeae19f6716841636c28d695375df17562ca208b2b7d0dc47635a50ae6c5de7" +checksum = "44d840b6ec649f480a41c8d80f9c65108b92d89345dd94027bfe06ac444d1060" [[package]] name = "windows_i686_msvc" -version = "0.42.0" +version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "84c12f65daa39dd2babe6e442988fc329d6243fdce47d7d2d155b8d874862246" +checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" [[package]] name = "windows_x86_64_gnu" -version = "0.42.0" +version = "0.42.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bf7b1b21b5362cbc318f686150e5bcea75ecedc74dd157d874d754a2ca44b0ed" +checksum = "8de912b8b8feb55c064867cf047dda097f92d51efad5b491dfb98f6bbb70cb36" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26d41b46a36d453748aedef1486d5c7a85db22e56aff34643984ea85514e94a3" [[package]] name = "windows_x86_64_gnullvm" -version = "0.42.0" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.42.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "09d525d2ba30eeb3297665bd434a54297e4170c7f1a44cad4ef58095b4cd2028" +checksum = "9aec5da331524158c6d1a4ac0ab1541149c0b9505fde06423b02f5ef0106b9f0" [[package]] name = "windows_x86_64_msvc" -version = "0.42.0" +version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f40009d85759725a34da6d89a94e63d7bdc50a862acf0dbc7c8e488f1edcb6f5" +checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" [[package]] name = "xxhash-rust" -version = "0.8.6" +version = "0.8.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "735a71d46c4d68d71d4b24d03fdc2b98e38cea81730595801db779c04fe80d70" +checksum = "927da81e25be1e1a2901d59b81b37dd2efd1fc9c9345a55007f09bf5a2d3ee03" diff --git a/moka-driver-rs/Cargo.toml b/moka-driver-rs/Cargo.toml index a763d8d..1349c27 100644 --- a/moka-driver-rs/Cargo.toml +++ b/moka-driver-rs/Cargo.toml @@ -1,7 +1,8 @@ [package] name = "moka-driver" -version = "0.1.0" +version = "0.2.0" edition = "2021" +rust-version = "1.75.0" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html @@ -10,8 +11,6 @@ name = "moka" crate_type = ["cdylib"] [dependencies] -jni = "0.20.0" -moka = "0.9.6" -# moka = { git = "https://github.com/moka-rs/moka", branch = "master" } -once_cell = "1.9.0" -xxhash-rust = { version = "0.8.5", features = ["xxh3"] } +jni = "0.21.1" +moka = { version = "0.12.7", features = ["sync"] } +xxhash-rust = { version = "0.8.10", features = ["xxh3"] } diff --git a/moka-driver-rs/src/lib.rs b/moka-driver-rs/src/lib.rs index 4e878f9..83629d0 100644 --- a/moka-driver-rs/src/lib.rs +++ b/moka-driver-rs/src/lib.rs @@ -1,14 +1,15 @@ use jni::{ - objects::JClass, + objects::{JClass, JString}, sys::{jboolean, jint, jlong}, JNIEnv, }; -use moka::sync::Cache; -use once_cell::sync::OnceCell; +use moka::{policy::EvictionPolicy, sync::Cache}; use std::hash::BuildHasher; const HASH_SEED_KEY: u64 = 982922761776577566; +type CacheTy = Cache; + #[derive(Clone, Default)] pub(crate) struct DefaultHasher; @@ -26,44 +27,73 @@ impl BuildHasher for DefaultHasher { } } -static CACHE: OnceCell> = OnceCell::new(); - -fn shared_cache() -> &'static Cache { - CACHE.get().expect("The cache is not initialized") -} - #[no_mangle] -pub extern "system" fn Java_io_crates_moka_cache_simulator_policy_product_MokaPolicy_initCache( - _env: JNIEnv, - _class: JClass, +pub extern "system" fn Java_io_crates_moka_cache_simulator_policy_product_MokaPolicy_initCache< + 'local, +>( + mut env: JNIEnv<'local>, + _class: JClass<'local>, maximum_capacity: jlong, is_weighted: jboolean, -) { + eviction_policy: JString<'local>, +) -> jlong { let mut builder = Cache::builder().max_capacity(maximum_capacity as u64); + if is_weighted == 0 { builder = builder.initial_capacity(maximum_capacity as usize); } else { builder = builder.weigher(|_k, v| *v as u32); } - let cache = builder.build_with_hasher(DefaultHasher::default()); - let _ = CACHE.set(cache); + + let ep: String = env + .get_string(&eviction_policy) + .expect("Could not get a Java String") + .into(); + let eviction_policy = match ep.to_ascii_lowercase().as_str() { + "tinylfu" => EvictionPolicy::tiny_lfu(), + "lru" => EvictionPolicy::lru(), + _ => panic!("Unknown eviction policy: {}", ep), + }; + builder = builder.eviction_policy(eviction_policy); + + let cache: CacheTy = builder.build_with_hasher(DefaultHasher); + Box::into_raw(Box::new(cache)) as jlong } #[no_mangle] -pub extern "system" fn Java_io_crates_moka_cache_simulator_policy_product_MokaPolicy_getFromCacheIfPresent( - _env: JNIEnv, - _class: JClass, +pub extern "system" fn Java_io_crates_moka_cache_simulator_policy_product_MokaPolicy_getFromCacheIfPresent< + 'local, +>( + _env: JNIEnv<'local>, + _class: JClass<'local>, + cache_ptr: jlong, key: jlong, ) -> jint { - shared_cache().get(&key).unwrap_or(-1) + let cache = unsafe { &mut *(cache_ptr as *mut CacheTy) }; + cache.get(&key).unwrap_or(-1) } #[no_mangle] -pub extern "system" fn Java_io_crates_moka_cache_simulator_policy_product_MokaPolicy_putToCache( - _env: JNIEnv, - _class: JClass, +pub extern "system" fn Java_io_crates_moka_cache_simulator_policy_product_MokaPolicy_putToCache< + 'local, +>( + _env: JNIEnv<'local>, + _class: JClass<'local>, + cache_ptr: jlong, key: jlong, value: jint, ) { - shared_cache().insert(key as i64, value as i32); + let cache = unsafe { &mut *(cache_ptr as *mut CacheTy) }; + cache.insert(key, value); +} + +#[no_mangle] +pub extern "system" fn Java_io_crates_moka_cache_simulator_policy_product_MokaPolicy_dropCache< + 'local, +>( + _env: JNIEnv<'local>, + _class: JClass<'local>, + cache_ptr: jlong, +) { + let _boxed_cache = unsafe { Box::from_raw(cache_ptr as *mut CacheTy) }; }