diff --git a/build.gradle b/build.gradle index 67113fa..3da94eb 100644 --- a/build.gradle +++ b/build.gradle @@ -1,9 +1,9 @@ plugins { id "idea" id "java-library" - id "me.champeau.jmh" version "latest.release" +// id "net.ltgt.errorprone" version "latest.release" + id("me.champeau.jmh") version "latest.release" id "com.vanniktech.maven.publish" version "latest.release" - id "net.ltgt.errorprone" version "latest.release" } allprojects { @@ -22,14 +22,14 @@ tasks.withType(JavaCompile).configureEach { // ? gradle.projectsEvaluated { options.release.set(17) options.deprecation = true - options.errorprone { - enabled = true // << '} + 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 -P "${APP_HOME:-./}" > /dev/null && printf '%s\n' "$PWD" ) || 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, 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/gradlew.bat b/gradlew.bat new file mode 100644 index 0000000..9d21a21 --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,94 @@ +@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 +@rem SPDX-License-Identifier: Apache-2.0 +@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/jitpack.yml b/jitpack.yml new file mode 100644 index 0000000..cc276a8 --- /dev/null +++ b/jitpack.yml @@ -0,0 +1,5 @@ +jdk: + - openjdk17 + +install: + - gradle clean build publishToMavenLocal --console=plain --continue --warning-mode all \ No newline at end of file diff --git a/settings.gradle b/settings.gradle index 33a39cc..a847a23 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1 +1,8 @@ -rootProject.name = 'fastutil-concurrent-wrapper' +pluginManagement { + repositories { + gradlePluginPortal() + mavenCentral() + } +} + +rootProject.name = 'fastutil-concurrent-wrapper' \ No newline at end of file diff --git a/src/jmh/java/com/trivago/kangaroo/AbstractBenchHelper.java b/src/jmh/java/com/trivago/kangaroo/AbstractBenchHelper.java index 0d807f7..cff591e 100644 --- a/src/jmh/java/com/trivago/kangaroo/AbstractBenchHelper.java +++ b/src/jmh/java/com/trivago/kangaroo/AbstractBenchHelper.java @@ -1,24 +1,24 @@ package com.trivago.kangaroo; -import com.trivago.fastutilconcurrentwrapper.LongLongMap; import com.trivago.fastutilconcurrentwrapper.PrimitiveMapBuilder; +import com.trivago.fastutilconcurrentwrapper.longkey.ConcurrentLongLongMap; import java.util.concurrent.ThreadLocalRandom; public abstract class AbstractBenchHelper extends AbstractCommonBenchHelper { protected static final int NUM_VALUES = 1_000_000; - protected LongLongMap map; + protected ConcurrentLongLongMap map; public void initAndLoadData(PrimitiveMapBuilder.MapMode mode) { if (mode == PrimitiveMapBuilder.MapMode.BUSY_WAITING){ - map = LongLongMap.newBuilder() + map = ConcurrentLongLongMap.newBuilder() .withBuckets(16) .withInitialCapacity(NUM_VALUES) .withMode(PrimitiveMapBuilder.MapMode.BUSY_WAITING) .withLoadFactor(0.8f) .build(); } else { - map = LongLongMap.newBuilder() + map = ConcurrentLongLongMap.newBuilder() .withBuckets(16) .withInitialCapacity(NUM_VALUES) .withLoadFactor(0.8f) diff --git a/src/jmh/java/com/trivago/kangaroo/AbstractCommonBenchHelper.java b/src/jmh/java/com/trivago/kangaroo/AbstractCommonBenchHelper.java index 091f500..af4fa41 100644 --- a/src/jmh/java/com/trivago/kangaroo/AbstractCommonBenchHelper.java +++ b/src/jmh/java/com/trivago/kangaroo/AbstractCommonBenchHelper.java @@ -8,7 +8,7 @@ import java.util.concurrent.TimeUnit; -abstract public class AbstractCommonBenchHelper { +public abstract class AbstractCommonBenchHelper { @Threads(4) @Benchmark @BenchmarkMode(Mode.Throughput) @@ -54,9 +54,9 @@ public void testRandomAllOpsAvgTime() { testAllOps(); } - abstract public void testGet(); + public abstract void testGet(); - abstract public void testPut(); + public abstract void testPut(); - abstract public void testAllOps(); -} + public abstract void testAllOps(); +} \ No newline at end of file diff --git a/src/jmh/java/com/trivago/kangaroo/FastutilWrapperBusyWaitingBenchmark.java b/src/jmh/java/com/trivago/kangaroo/FastutilWrapperBusyWaitingBenchmark.java index 8a83c00..963163f 100644 --- a/src/jmh/java/com/trivago/kangaroo/FastutilWrapperBusyWaitingBenchmark.java +++ b/src/jmh/java/com/trivago/kangaroo/FastutilWrapperBusyWaitingBenchmark.java @@ -12,9 +12,8 @@ @Warmup(iterations = 3, time = 1) @Measurement(iterations = 3, time = 2) public class FastutilWrapperBusyWaitingBenchmark extends AbstractBenchHelper { - @Setup(Level.Trial) public void loadData() { - super.initAndLoadData(PrimitiveMapBuilder.MapMode.BUSY_WAITING); + initAndLoadData(PrimitiveMapBuilder.MapMode.BUSY_WAITING); } } \ No newline at end of file diff --git a/src/jmh/java/com/trivago/kangaroo/FastutilWrapperDefaultBenchmark.java b/src/jmh/java/com/trivago/kangaroo/FastutilWrapperDefaultBenchmark.java index 5e05b62..d51d86f 100644 --- a/src/jmh/java/com/trivago/kangaroo/FastutilWrapperDefaultBenchmark.java +++ b/src/jmh/java/com/trivago/kangaroo/FastutilWrapperDefaultBenchmark.java @@ -12,7 +12,6 @@ @Warmup(iterations = 3, time = 1) @Measurement(iterations = 3, time = 2) public class FastutilWrapperDefaultBenchmark extends AbstractBenchHelper { - @Setup(Level.Trial) public void loadData() { initAndLoadData(PrimitiveMapBuilder.MapMode.BLOCKING); diff --git a/src/jmh/java/com/trivago/kangaroo/JavaUtilWrapperBenchmark.java b/src/jmh/java/com/trivago/kangaroo/JavaUtilWrapperBenchmark.java index 695a6b8..458788e 100644 --- a/src/jmh/java/com/trivago/kangaroo/JavaUtilWrapperBenchmark.java +++ b/src/jmh/java/com/trivago/kangaroo/JavaUtilWrapperBenchmark.java @@ -12,11 +12,13 @@ import java.util.Map; import java.util.concurrent.ThreadLocalRandom; +/** + JDK Collections.synchronizedMap(Long2LongOpenHashMap) +*/ @State(Scope.Benchmark) @Warmup(iterations = 3, time = 1) @Measurement(iterations = 3, time = 2) public class JavaUtilWrapperBenchmark extends AbstractCommonBenchHelper { - Map map; @Setup(Level.Trial) @@ -30,18 +32,21 @@ public void loadData() { map = Collections.synchronizedMap(m); } - public void testGet() { + @Override + public void testGet() { long key = ThreadLocalRandom.current().nextLong(); map.get(key); } - public void testPut() { + @Override + public void testPut() { long key = ThreadLocalRandom.current().nextLong(); long value = ThreadLocalRandom.current().nextLong(); map.put(key, value); } - public void testAllOps() { + @Override + public void testAllOps() { int op = ThreadLocalRandom.current().nextInt(3); long key = ThreadLocalRandom.current().nextLong(); switch (op) { @@ -57,4 +62,4 @@ public void testAllOps() { break; } } -} +} \ No newline at end of file diff --git a/src/jmh/java/com/trivago/kangaroo/long2long/AbstractLongLongBenchHelper.java b/src/jmh/java/com/trivago/kangaroo/long2long/AbstractLongLongBenchHelper.java new file mode 100644 index 0000000..cacaf5a --- /dev/null +++ b/src/jmh/java/com/trivago/kangaroo/long2long/AbstractLongLongBenchHelper.java @@ -0,0 +1,58 @@ +package com.trivago.kangaroo.long2long; + +import com.trivago.fastutilconcurrentwrapper.PrimitiveMapBuilder; +import com.trivago.fastutilconcurrentwrapper.longkey.ConcurrentLongLongMap; +import com.trivago.kangaroo.AbstractCommonBenchHelper; + +import java.util.concurrent.ThreadLocalRandom; + +public abstract class AbstractLongLongBenchHelper extends AbstractCommonBenchHelper { + protected static final int NUM_VALUES = 1_000_000; + protected ConcurrentLongLongMap map; + + public void initAndLoadData (PrimitiveMapBuilder.MapMode mode) { + map = ConcurrentLongLongMap.newBuilder() + .withBuckets(16) + .withInitialCapacity(NUM_VALUES) + .withMode(mode) + .withLoadFactor(0.8f) + .build(); + + for (int i = 0; i < NUM_VALUES; i++) { + long key = ThreadLocalRandom.current().nextLong(); + long value = ThreadLocalRandom.current().nextLong(); + map.put(key, value); + } + } + + @Override + public void testGet() { + long key = ThreadLocalRandom.current().nextLong(); + map.get(key); + } + + @Override + public void testPut() { + long key = ThreadLocalRandom.current().nextLong(); + long value = ThreadLocalRandom.current().nextLong(); + map.put(key, value); + } + + @Override + public void testAllOps() { + int op = ThreadLocalRandom.current().nextInt(3); + long key = ThreadLocalRandom.current().nextLong(); + switch (op) { + case 1: + long value = ThreadLocalRandom.current().nextLong(); + map.put(key, value); + break; + case 2: + map.remove(key); + break; + default: + map.get(key); + break; + } + } +} \ No newline at end of file diff --git a/src/jmh/java/com/trivago/kangaroo/long2long/FastutilWrapperBusyWaitingLongLongBenchmark.java b/src/jmh/java/com/trivago/kangaroo/long2long/FastutilWrapperBusyWaitingLongLongBenchmark.java new file mode 100644 index 0000000..e8733dc --- /dev/null +++ b/src/jmh/java/com/trivago/kangaroo/long2long/FastutilWrapperBusyWaitingLongLongBenchmark.java @@ -0,0 +1,19 @@ +package com.trivago.kangaroo.long2long; + +import com.trivago.fastutilconcurrentwrapper.PrimitiveMapBuilder; +import org.openjdk.jmh.annotations.Level; +import org.openjdk.jmh.annotations.Measurement; +import org.openjdk.jmh.annotations.Scope; +import org.openjdk.jmh.annotations.Setup; +import org.openjdk.jmh.annotations.State; +import org.openjdk.jmh.annotations.Warmup; + +@State(Scope.Benchmark) +@Warmup(iterations = 3, time = 1) +@Measurement(iterations = 3, time = 2) +public class FastutilWrapperBusyWaitingLongLongBenchmark extends AbstractLongLongBenchHelper { + @Setup(Level.Trial) + public void loadData() { + initAndLoadData(PrimitiveMapBuilder.MapMode.BUSY_WAITING); + } +} \ No newline at end of file diff --git a/src/jmh/java/com/trivago/kangaroo/long2long/FastutilWrapperDefaultLongLongBenchmark.java b/src/jmh/java/com/trivago/kangaroo/long2long/FastutilWrapperDefaultLongLongBenchmark.java new file mode 100644 index 0000000..f20f5ca --- /dev/null +++ b/src/jmh/java/com/trivago/kangaroo/long2long/FastutilWrapperDefaultLongLongBenchmark.java @@ -0,0 +1,19 @@ +package com.trivago.kangaroo.long2long; + +import com.trivago.fastutilconcurrentwrapper.PrimitiveMapBuilder; +import org.openjdk.jmh.annotations.Level; +import org.openjdk.jmh.annotations.Measurement; +import org.openjdk.jmh.annotations.Scope; +import org.openjdk.jmh.annotations.Setup; +import org.openjdk.jmh.annotations.State; +import org.openjdk.jmh.annotations.Warmup; + +@State(Scope.Benchmark) +@Warmup(iterations = 3, time = 1) +@Measurement(iterations = 3, time = 2) +public class FastutilWrapperDefaultLongLongBenchmark extends AbstractLongLongBenchHelper { + @Setup(Level.Trial) + public void loadData() { + initAndLoadData(PrimitiveMapBuilder.MapMode.BLOCKING); + } +} \ No newline at end of file diff --git a/src/jmh/java/com/trivago/kangaroo/long2long/JavaConcurrentLongLongBenchmark.java b/src/jmh/java/com/trivago/kangaroo/long2long/JavaConcurrentLongLongBenchmark.java new file mode 100644 index 0000000..16ca2e7 --- /dev/null +++ b/src/jmh/java/com/trivago/kangaroo/long2long/JavaConcurrentLongLongBenchmark.java @@ -0,0 +1,62 @@ +package com.trivago.kangaroo.long2long; + +import com.trivago.kangaroo.AbstractCommonBenchHelper; +import org.openjdk.jmh.annotations.Level; +import org.openjdk.jmh.annotations.Measurement; +import org.openjdk.jmh.annotations.Scope; +import org.openjdk.jmh.annotations.Setup; +import org.openjdk.jmh.annotations.State; +import org.openjdk.jmh.annotations.Warmup; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ThreadLocalRandom; + +@State(Scope.Benchmark) +@Warmup(iterations = 3, time = 1) +@Measurement(iterations = 3, time = 2) +public class JavaConcurrentLongLongBenchmark extends AbstractCommonBenchHelper { + + Map map; + + @Setup(Level.Trial) + public void loadData() { + map = new ConcurrentHashMap<>(AbstractLongLongBenchHelper.NUM_VALUES, 0.8f); + for (int i = 0; i < AbstractLongLongBenchHelper.NUM_VALUES; i++) { + long key = ThreadLocalRandom.current().nextLong(); + long value = ThreadLocalRandom.current().nextLong(); + map.put(key, value); + } + } + + @Override + public void testGet() { + long key = ThreadLocalRandom.current().nextLong(); + map.get(key); + } + + @Override + public void testPut() { + long key = ThreadLocalRandom.current().nextLong(); + long value = ThreadLocalRandom.current().nextLong(); + map.put(key, value); + } + + @Override + public void testAllOps() { + int op = ThreadLocalRandom.current().nextInt(3); + long key = ThreadLocalRandom.current().nextLong(); + switch (op) { + case 1: + long value = ThreadLocalRandom.current().nextLong(); + map.put(key, value); + break; + case 2: + map.remove(key); + break; + default: + map.get(key); + break; + } + } +} \ No newline at end of file diff --git a/src/jmh/java/com/trivago/kangaroo/long2long/JavaUtilWrapperLongLongBenchmark.java b/src/jmh/java/com/trivago/kangaroo/long2long/JavaUtilWrapperLongLongBenchmark.java new file mode 100644 index 0000000..885466c --- /dev/null +++ b/src/jmh/java/com/trivago/kangaroo/long2long/JavaUtilWrapperLongLongBenchmark.java @@ -0,0 +1,64 @@ +package com.trivago.kangaroo.long2long; + +import com.trivago.kangaroo.AbstractCommonBenchHelper; +import it.unimi.dsi.fastutil.longs.Long2LongOpenHashMap; +import org.openjdk.jmh.annotations.Level; +import org.openjdk.jmh.annotations.Measurement; +import org.openjdk.jmh.annotations.Scope; +import org.openjdk.jmh.annotations.Setup; +import org.openjdk.jmh.annotations.State; +import org.openjdk.jmh.annotations.Warmup; + +import java.util.Collections; +import java.util.Map; +import java.util.concurrent.ThreadLocalRandom; + +@State(Scope.Benchmark) +@Warmup(iterations = 3, time = 1) +@Measurement(iterations = 3, time = 2) +public class JavaUtilWrapperLongLongBenchmark extends AbstractCommonBenchHelper { + + Map map; + + @Setup(Level.Trial) + public void loadData() { + Long2LongOpenHashMap m = new Long2LongOpenHashMap(AbstractLongLongBenchHelper.NUM_VALUES, 0.8f); + for (int i = 0; i < AbstractLongLongBenchHelper.NUM_VALUES; i++) { + long key = ThreadLocalRandom.current().nextLong(); + long value = ThreadLocalRandom.current().nextLong(); + m.put(key, value); + } + map = Collections.synchronizedMap(m); + } + + @Override + public void testGet() { + long key = ThreadLocalRandom.current().nextLong(); + map.get(key); + } + + @Override + public void testPut() { + long key = ThreadLocalRandom.current().nextLong(); + long value = ThreadLocalRandom.current().nextLong(); + map.put(key, value); + } + + @Override + public void testAllOps() { + int op = ThreadLocalRandom.current().nextInt(3); + long key = ThreadLocalRandom.current().nextLong(); + switch (op) { + case 1: + long value = ThreadLocalRandom.current().nextLong(); + map.put(key, value); + break; + case 2: + map.remove(key); + break; + default: + map.get(key); + break; + } + } +} \ No newline at end of file diff --git a/src/jmh/java/com/trivago/kangaroo/object2long/AbstractObjectLongBenchHelper.java b/src/jmh/java/com/trivago/kangaroo/object2long/AbstractObjectLongBenchHelper.java new file mode 100644 index 0000000..ea02095 --- /dev/null +++ b/src/jmh/java/com/trivago/kangaroo/object2long/AbstractObjectLongBenchHelper.java @@ -0,0 +1,58 @@ +package com.trivago.kangaroo.object2long; + +import com.trivago.fastutilconcurrentwrapper.PrimitiveMapBuilder; +import com.trivago.fastutilconcurrentwrapper.objkey.ConcurrentObjectLongMap; +import com.trivago.kangaroo.AbstractCommonBenchHelper; + +import java.util.concurrent.ThreadLocalRandom; + +public abstract class AbstractObjectLongBenchHelper extends AbstractCommonBenchHelper { + protected static final int NUM_VALUES = 1_000_000; + protected ConcurrentObjectLongMap map; + + public void initAndLoadData (PrimitiveMapBuilder.MapMode mode) { + map = ConcurrentObjectLongMap.newBuilder() + .withBuckets(16) + .withInitialCapacity(NUM_VALUES) + .withMode(mode) + .withLoadFactor(0.8f) + .build(); + + for (int i = 0; i < NUM_VALUES; i++) { + TestObjectKey key = new TestObjectKey(); + long value = ThreadLocalRandom.current().nextLong(); + map.put(key, value); + } + } + + @Override + public void testGet() { + TestObjectKey key = new TestObjectKey(); + map.get(key); + } + + @Override + public void testPut() { + TestObjectKey key = new TestObjectKey(); + long value = ThreadLocalRandom.current().nextLong(); + map.put(key, value); + } + + @Override + public void testAllOps() { + int op = ThreadLocalRandom.current().nextInt(3); + TestObjectKey key = new TestObjectKey(); + switch (op) { + case 1: + long value = ThreadLocalRandom.current().nextLong(); + map.put(key, value); + break; + case 2: + map.remove(key); + break; + default: + map.get(key); + break; + } + } +} \ No newline at end of file diff --git a/src/jmh/java/com/trivago/kangaroo/object2long/FastutilWrapperBusyWaitingObjectLongBenchmark.java b/src/jmh/java/com/trivago/kangaroo/object2long/FastutilWrapperBusyWaitingObjectLongBenchmark.java new file mode 100644 index 0000000..47424c2 --- /dev/null +++ b/src/jmh/java/com/trivago/kangaroo/object2long/FastutilWrapperBusyWaitingObjectLongBenchmark.java @@ -0,0 +1,19 @@ +package com.trivago.kangaroo.object2long; + +import com.trivago.fastutilconcurrentwrapper.PrimitiveMapBuilder; +import org.openjdk.jmh.annotations.Level; +import org.openjdk.jmh.annotations.Measurement; +import org.openjdk.jmh.annotations.Scope; +import org.openjdk.jmh.annotations.Setup; +import org.openjdk.jmh.annotations.State; +import org.openjdk.jmh.annotations.Warmup; + +@State(Scope.Benchmark) +@Warmup(iterations = 3, time = 1) +@Measurement(iterations = 3, time = 2) +public class FastutilWrapperBusyWaitingObjectLongBenchmark extends AbstractObjectLongBenchHelper { + @Setup(Level.Trial) + public void loadData() { + initAndLoadData(PrimitiveMapBuilder.MapMode.BUSY_WAITING); + } +} \ No newline at end of file diff --git a/src/jmh/java/com/trivago/kangaroo/object2long/FastutilWrapperDefaultObjectLongBenchmark.java b/src/jmh/java/com/trivago/kangaroo/object2long/FastutilWrapperDefaultObjectLongBenchmark.java new file mode 100644 index 0000000..cec4cce --- /dev/null +++ b/src/jmh/java/com/trivago/kangaroo/object2long/FastutilWrapperDefaultObjectLongBenchmark.java @@ -0,0 +1,19 @@ +package com.trivago.kangaroo.object2long; + +import com.trivago.fastutilconcurrentwrapper.PrimitiveMapBuilder; +import org.openjdk.jmh.annotations.Level; +import org.openjdk.jmh.annotations.Measurement; +import org.openjdk.jmh.annotations.Scope; +import org.openjdk.jmh.annotations.Setup; +import org.openjdk.jmh.annotations.State; +import org.openjdk.jmh.annotations.Warmup; + +@State(Scope.Benchmark) +@Warmup(iterations = 3, time = 1) +@Measurement(iterations = 3, time = 2) +public class FastutilWrapperDefaultObjectLongBenchmark extends AbstractObjectLongBenchHelper { + @Setup(Level.Trial) + public void loadData() { + initAndLoadData(PrimitiveMapBuilder.MapMode.BLOCKING); + } +} \ No newline at end of file diff --git a/src/jmh/java/com/trivago/kangaroo/object2long/JavaConcurrentObjectLongBenchmark.java b/src/jmh/java/com/trivago/kangaroo/object2long/JavaConcurrentObjectLongBenchmark.java new file mode 100644 index 0000000..e790615 --- /dev/null +++ b/src/jmh/java/com/trivago/kangaroo/object2long/JavaConcurrentObjectLongBenchmark.java @@ -0,0 +1,61 @@ +package com.trivago.kangaroo.object2long; + +import com.trivago.kangaroo.AbstractCommonBenchHelper; +import org.openjdk.jmh.annotations.Level; +import org.openjdk.jmh.annotations.Measurement; +import org.openjdk.jmh.annotations.Scope; +import org.openjdk.jmh.annotations.Setup; +import org.openjdk.jmh.annotations.State; +import org.openjdk.jmh.annotations.Warmup; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ThreadLocalRandom; + +@State(Scope.Benchmark) +@Warmup(iterations = 3, time = 1) +@Measurement(iterations = 3, time = 2) +public class JavaConcurrentObjectLongBenchmark extends AbstractCommonBenchHelper { + + Map map; + + @Setup(Level.Trial) + public void loadData() { + map = new ConcurrentHashMap<>(AbstractObjectLongBenchHelper.NUM_VALUES, 0.8f); + for (int i = 0; i < AbstractObjectLongBenchHelper.NUM_VALUES; i++) { + TestObjectKey key = new TestObjectKey(); + long value = ThreadLocalRandom.current().nextLong(); + map.put(key, value); + } + } + + @Override + public void testGet() { + map.get(new TestObjectKey()); + } + + @Override + public void testPut() { + TestObjectKey key = new TestObjectKey(); + long value = ThreadLocalRandom.current().nextLong(); + map.put(key, value); + } + + @Override + public void testAllOps() { + int op = ThreadLocalRandom.current().nextInt(3); + TestObjectKey key = new TestObjectKey(); + switch (op) { + case 1: + long value = ThreadLocalRandom.current().nextLong(); + map.put(key, value); + break; + case 2: + map.remove(key); + break; + default: + map.get(key); + break; + } + } +} \ No newline at end of file diff --git a/src/jmh/java/com/trivago/kangaroo/object2long/JavaUtilWrapperObjectLongBenchmark.java b/src/jmh/java/com/trivago/kangaroo/object2long/JavaUtilWrapperObjectLongBenchmark.java new file mode 100644 index 0000000..d794de2 --- /dev/null +++ b/src/jmh/java/com/trivago/kangaroo/object2long/JavaUtilWrapperObjectLongBenchmark.java @@ -0,0 +1,63 @@ +package com.trivago.kangaroo.object2long; + +import com.trivago.kangaroo.AbstractCommonBenchHelper; +import it.unimi.dsi.fastutil.objects.Object2LongOpenHashMap; +import org.openjdk.jmh.annotations.Level; +import org.openjdk.jmh.annotations.Measurement; +import org.openjdk.jmh.annotations.Scope; +import org.openjdk.jmh.annotations.Setup; +import org.openjdk.jmh.annotations.State; +import org.openjdk.jmh.annotations.Warmup; + +import java.util.Collections; +import java.util.Map; +import java.util.concurrent.ThreadLocalRandom; + +@State(Scope.Benchmark) +@Warmup(iterations = 3, time = 1) +@Measurement(iterations = 3, time = 2) +public class JavaUtilWrapperObjectLongBenchmark extends AbstractCommonBenchHelper { + + Map map; + + @Setup(Level.Trial) + public void loadData() { + Object2LongOpenHashMap m = new Object2LongOpenHashMap<>(AbstractObjectLongBenchHelper.NUM_VALUES, 0.8f); + for (int i = 0; i < AbstractObjectLongBenchHelper.NUM_VALUES; i++) { + TestObjectKey key = new TestObjectKey(); + long value = ThreadLocalRandom.current().nextLong(); + m.put(key, value); + } + map = Collections.synchronizedMap(m); + } + + @Override + public void testGet() { + map.get(new TestObjectKey()); + } + + @Override + public void testPut() { + TestObjectKey key = new TestObjectKey(); + long value = ThreadLocalRandom.current().nextLong(); + map.put(key, value); + } + + @Override + public void testAllOps() { + int op = ThreadLocalRandom.current().nextInt(3); + TestObjectKey key = new TestObjectKey(); + switch (op) { + case 1: + long value = ThreadLocalRandom.current().nextLong(); + map.put(key, value); + break; + case 2: + map.remove(key); + break; + default: + map.get(key); + break; + } + } +} \ No newline at end of file diff --git a/src/jmh/java/com/trivago/kangaroo/object2long/TestObjectKey.java b/src/jmh/java/com/trivago/kangaroo/object2long/TestObjectKey.java new file mode 100644 index 0000000..2957802 --- /dev/null +++ b/src/jmh/java/com/trivago/kangaroo/object2long/TestObjectKey.java @@ -0,0 +1,13 @@ +package com.trivago.kangaroo.object2long; + +import java.util.concurrent.ThreadLocalRandom; + +public class TestObjectKey { + + private final int id = ThreadLocalRandom.current().nextInt(); + + @Override + public int hashCode() { + return Integer.hashCode(id); + } +} diff --git a/src/main/java/com/trivago/fastutilconcurrentwrapper/IntFloatMap.java b/src/main/java/com/trivago/fastutilconcurrentwrapper/IntFloatMap.java deleted file mode 100644 index c2ece40..0000000 --- a/src/main/java/com/trivago/fastutilconcurrentwrapper/IntFloatMap.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.trivago.fastutilconcurrentwrapper; - -import com.trivago.fastutilconcurrentwrapper.map.ConcurrentBusyWaitingIntFloatMap; -import com.trivago.fastutilconcurrentwrapper.map.ConcurrentIntFloatMap; -import it.unimi.dsi.fastutil.ints.Int2FloatFunction; - -import java.util.function.BiFunction; - -public interface IntFloatMap extends PrimitiveKeyMap { - boolean containsKey(int key); - - float get(int key); - - float put(int key, float value); - - float getDefaultValue(); - - float remove(int key); - - boolean remove(int key, float value); - - float computeIfAbsent(int key, Int2FloatFunction mappingFunction); - - float computeIfPresent(int key, BiFunction mappingFunction); - - static PrimitiveMapBuilder newBuilder () { - return new PrimitiveMapBuilder<>(){ - @Override - public IntFloatMap build () { - float def = defaultValue != null ? defaultValue : 0; - return switch (mapMode){ - case BUSY_WAITING -> new ConcurrentBusyWaitingIntFloatMap(buckets, initialCapacity, loadFactor, def); - case BLOCKING -> new ConcurrentIntFloatMap(buckets, initialCapacity, loadFactor, def); - }; - } - }; - } -} \ No newline at end of file diff --git a/src/main/java/com/trivago/fastutilconcurrentwrapper/IntIntMap.java b/src/main/java/com/trivago/fastutilconcurrentwrapper/IntIntMap.java deleted file mode 100644 index 33545ec..0000000 --- a/src/main/java/com/trivago/fastutilconcurrentwrapper/IntIntMap.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.trivago.fastutilconcurrentwrapper; - -import com.trivago.fastutilconcurrentwrapper.map.ConcurrentBusyWaitingIntIntMap; -import com.trivago.fastutilconcurrentwrapper.map.ConcurrentIntIntMap; -import it.unimi.dsi.fastutil.ints.Int2IntFunction; - -import java.util.function.BiFunction; - -public interface IntIntMap extends PrimitiveKeyMap { - boolean containsKey(int key); - - /** - * @param key int Map.key - * @return defaultValue if the key is not present - */ - int get(int key); - - int put(int key, int value); - - int getDefaultValue(); - - int remove(int key); - - boolean remove(int key, int value); - - int computeIfAbsent(int key, Int2IntFunction mappingFunction); - - int computeIfPresent(int key, BiFunction mappingFunction); - - static PrimitiveMapBuilder newBuilder () { - return new PrimitiveMapBuilder<>(){ - @Override - public IntIntMap build () { - int def = defaultValue != null ? defaultValue : 0; - return switch (mapMode){ - case BUSY_WAITING -> new ConcurrentBusyWaitingIntIntMap(buckets, initialCapacity, loadFactor, def); - case BLOCKING -> new ConcurrentIntIntMap(buckets, initialCapacity, loadFactor, def); - }; - } - }; - } -} \ No newline at end of file diff --git a/src/main/java/com/trivago/fastutilconcurrentwrapper/LongFloatMap.java b/src/main/java/com/trivago/fastutilconcurrentwrapper/LongFloatMap.java deleted file mode 100644 index 62ea880..0000000 --- a/src/main/java/com/trivago/fastutilconcurrentwrapper/LongFloatMap.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.trivago.fastutilconcurrentwrapper; - -import com.trivago.fastutilconcurrentwrapper.map.ConcurrentBusyWaitingLongFloatMap; -import com.trivago.fastutilconcurrentwrapper.map.ConcurrentLongFloatMap; -import it.unimi.dsi.fastutil.longs.Long2FloatFunction; - -import java.util.function.BiFunction; - -public interface LongFloatMap extends PrimitiveKeyMap { - boolean containsKey (long key); - - /** - * @param key key - * @return 0.0 if the key is not present - */ - float get(long key); - - float put(long key, float value); - - float getDefaultValue(); - - float remove(long key); - - /** - * Remove this key only if it has the given value. - * - * @param key - * @param value - * @return - */ - boolean remove(long key, float value); - - float computeIfAbsent(long key, Long2FloatFunction mappingFunction); - - float computeIfPresent(int key, BiFunction mappingFunction); - - static PrimitiveMapBuilder newBuilder () { - return new PrimitiveMapBuilder<>(){ - @Override - public LongFloatMap build () { - float def = defaultValue != null ? defaultValue : 0; - return switch (mapMode){ - case BUSY_WAITING -> new ConcurrentBusyWaitingLongFloatMap(buckets, initialCapacity, loadFactor, def); - case BLOCKING -> new ConcurrentLongFloatMap(buckets, initialCapacity, loadFactor, def); - }; - } - }; - } -} \ No newline at end of file diff --git a/src/main/java/com/trivago/fastutilconcurrentwrapper/LongIntMap.java b/src/main/java/com/trivago/fastutilconcurrentwrapper/LongIntMap.java deleted file mode 100644 index 533e174..0000000 --- a/src/main/java/com/trivago/fastutilconcurrentwrapper/LongIntMap.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.trivago.fastutilconcurrentwrapper; - -import com.trivago.fastutilconcurrentwrapper.map.ConcurrentBusyWaitingLongIntMap; -import com.trivago.fastutilconcurrentwrapper.map.ConcurrentLongIntMap; -import it.unimi.dsi.fastutil.longs.Long2IntFunction; - -import java.util.function.BiFunction; - -public interface LongIntMap extends PrimitiveKeyMap { - boolean containsKey (long key); - - /** - * @param key key to get - * @return configured LongIntMap.getDefaultValue(), if the key is not present - */ - int get(long key); - - int put(long key, int value); - - int getDefaultValue(); - - int remove(long key); - - boolean remove(long key, int value); - - int computeIfAbsent(long key, Long2IntFunction mappingFunction); - - int computeIfPresent(long key, BiFunction mappingFunction); - - static PrimitiveMapBuilder newBuilder () { - return new PrimitiveMapBuilder<>(){ - @Override - public LongIntMap build() { - int def = defaultValue != null ? defaultValue : 0; - return switch (mapMode){ - case BUSY_WAITING -> new ConcurrentBusyWaitingLongIntMap(buckets, initialCapacity, loadFactor, def); - case BLOCKING -> new ConcurrentLongIntMap(buckets, initialCapacity, loadFactor, def); - }; - } - }; - } -} \ No newline at end of file diff --git a/src/main/java/com/trivago/fastutilconcurrentwrapper/LongLongMap.java b/src/main/java/com/trivago/fastutilconcurrentwrapper/LongLongMap.java deleted file mode 100644 index 8cc0a26..0000000 --- a/src/main/java/com/trivago/fastutilconcurrentwrapper/LongLongMap.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.trivago.fastutilconcurrentwrapper; - -import com.trivago.fastutilconcurrentwrapper.map.ConcurrentBusyWaitingLongLongMap; -import com.trivago.fastutilconcurrentwrapper.map.ConcurrentLongLongMap; -import it.unimi.dsi.fastutil.longs.Long2LongFunction; - -import java.util.function.BiFunction; - -public interface LongLongMap extends PrimitiveKeyMap { - boolean containsKey (long key); - - /** - * @param key long map.key - * @return defaultValue if the key is not present - */ - long get(long key); - - long put(long key, long value); - - long getDefaultValue(); - - long remove(long key); - - boolean remove(long key, long value); - - long computeIfAbsent(long key, Long2LongFunction mappingFunction); - - long computeIfPresent(long key, BiFunction mappingFunction); - - static PrimitiveMapBuilder newBuilder () { - return new PrimitiveMapBuilder<>(){ - @Override - public LongLongMap build() { - long def = defaultValue != null ? defaultValue : 0; - return switch (mapMode){ - case BUSY_WAITING -> new ConcurrentBusyWaitingLongLongMap(buckets, initialCapacity, loadFactor, def); - case BLOCKING -> new ConcurrentLongLongMap(buckets, initialCapacity, loadFactor, def); - }; - } - }; - } -} \ No newline at end of file diff --git a/src/main/java/com/trivago/fastutilconcurrentwrapper/map/PrimitiveConcurrentMap.java b/src/main/java/com/trivago/fastutilconcurrentwrapper/PrimitiveConcurrentMap.java similarity index 81% rename from src/main/java/com/trivago/fastutilconcurrentwrapper/map/PrimitiveConcurrentMap.java rename to src/main/java/com/trivago/fastutilconcurrentwrapper/PrimitiveConcurrentMap.java index 266b349..5ce9579 100644 --- a/src/main/java/com/trivago/fastutilconcurrentwrapper/map/PrimitiveConcurrentMap.java +++ b/src/main/java/com/trivago/fastutilconcurrentwrapper/PrimitiveConcurrentMap.java @@ -1,6 +1,5 @@ -package com.trivago.fastutilconcurrentwrapper.map; +package com.trivago.fastutilconcurrentwrapper; -import com.trivago.fastutilconcurrentwrapper.PrimitiveKeyMap; import it.unimi.dsi.fastutil.Function; import it.unimi.dsi.fastutil.HashCommon; @@ -11,17 +10,20 @@ /** @see it.unimi.dsi.fastutil.Function @see com.google.common.util.concurrent.Striped + @see org.jctools.maps.NonBlockingHashMapLong */ public abstract class PrimitiveConcurrentMap implements PrimitiveKeyMap { protected final int numBuckets; protected final ReadWriteLock[] locks; - protected PrimitiveConcurrentMap(int numBuckets) { + protected PrimitiveConcurrentMap (int numBuckets) { + if (numBuckets < 1 || numBuckets > 100_000_000) + throw new IllegalArgumentException("numBuckets must be between 1 and 100_000_000, but: "+ numBuckets); this.numBuckets = numBuckets; this.locks = new ReadWriteLock[numBuckets]; for (int i = 0; i < numBuckets; i++) locks[i] = new ReentrantReadWriteLock(); - } + }//new /** Lock must be held! */ protected abstract Function mapAt (int index); @@ -79,7 +81,11 @@ protected int getBucket(int key) { return bucket(key, numBuckets);// Integer.hashCode(key) == key } - static int bucket (int hash, int bucketSize) { + protected int getBucket (Object key) { + return key != null ? bucket(key.hashCode(), numBuckets) : 0; + } + + public static int bucket (int hash, int bucketSize) { return Math.abs(HashCommon.mix(hash) % bucketSize); } } \ No newline at end of file diff --git a/src/main/java/com/trivago/fastutilconcurrentwrapper/map/ConcurrentBusyWaitingIntFloatMap.java b/src/main/java/com/trivago/fastutilconcurrentwrapper/intkey/ConcurrentBusyWaitingIntFloatMap.java similarity index 68% rename from src/main/java/com/trivago/fastutilconcurrentwrapper/map/ConcurrentBusyWaitingIntFloatMap.java rename to src/main/java/com/trivago/fastutilconcurrentwrapper/intkey/ConcurrentBusyWaitingIntFloatMap.java index 9d39c9b..e94aec3 100644 --- a/src/main/java/com/trivago/fastutilconcurrentwrapper/map/ConcurrentBusyWaitingIntFloatMap.java +++ b/src/main/java/com/trivago/fastutilconcurrentwrapper/intkey/ConcurrentBusyWaitingIntFloatMap.java @@ -1,37 +1,17 @@ -package com.trivago.fastutilconcurrentwrapper.map; +package com.trivago.fastutilconcurrentwrapper.intkey; -import com.trivago.fastutilconcurrentwrapper.IntFloatMap; -import it.unimi.dsi.fastutil.Function; import it.unimi.dsi.fastutil.ints.Int2FloatFunction; -import it.unimi.dsi.fastutil.ints.Int2FloatOpenHashMap; import java.util.concurrent.locks.Lock; import java.util.function.BiFunction; -public class ConcurrentBusyWaitingIntFloatMap extends PrimitiveConcurrentMap implements IntFloatMap { - private final Int2FloatOpenHashMap[] maps; - private final float defaultValue; - - public ConcurrentBusyWaitingIntFloatMap( - int numBuckets, - int initialCapacity, - float loadFactor, - float defaultValue - ){ - super(numBuckets); - this.maps = new Int2FloatOpenHashMap[numBuckets]; - this.defaultValue = defaultValue; - for (int i = 0; i < numBuckets; i++) - maps[i] = new Int2FloatOpenHashMap(initialCapacity, loadFactor); +public class ConcurrentBusyWaitingIntFloatMap extends ConcurrentIntFloatMap { + public ConcurrentBusyWaitingIntFloatMap (int numBuckets, int initialCapacity, float loadFactor, float defaultValue) { + super(numBuckets, initialCapacity, loadFactor, defaultValue); } @Override - protected Function mapAt (int index) { - return maps[index]; - } - - @Override - public boolean containsKey(int key) { + public boolean containsKey(int key) { int bucket = getBucket(key); Lock readLock = locks[bucket].readLock(); @@ -49,7 +29,7 @@ public boolean containsKey(int key) { } @Override - public float get(int key) { + public float get (int key) { int bucket = getBucket(key); Lock readLock = locks[bucket].readLock(); @@ -67,7 +47,7 @@ public float get(int key) { } @Override - public float put(int key, float value) { + public float put(int key, float value) { int bucket = getBucket(key); Lock writeLock = locks[bucket].writeLock(); @@ -84,10 +64,8 @@ public float put(int key, float value) { } } - @Override public float getDefaultValue (){ return defaultValue; } - @Override - public float remove(int key) { + public float remove(int key) { int bucket = getBucket(key); Lock writeLock = locks[bucket].writeLock(); @@ -105,7 +83,7 @@ public float remove(int key) { } @Override - public boolean remove(int key, float value) { + public boolean remove(int key, float value) { int bucket = getBucket(key); Lock writeLock = locks[bucket].writeLock(); @@ -123,7 +101,7 @@ public boolean remove(int key, float value) { } @Override - public float computeIfAbsent(int key, Int2FloatFunction mappingFunction) { + public float computeIfAbsent(int key, Int2FloatFunction mappingFunction) { int bucket = getBucket(key); Lock writeLock = locks[bucket].writeLock(); @@ -141,7 +119,7 @@ public float computeIfAbsent(int key, Int2FloatFunction mappingFunction) { } @Override - public float computeIfPresent(int key, BiFunction mappingFunction) { + public float computeIfPresent(int key, BiFunction mappingFunction) { int bucket = getBucket(key); Lock writeLock = locks[bucket].writeLock(); diff --git a/src/main/java/com/trivago/fastutilconcurrentwrapper/map/ConcurrentBusyWaitingIntIntMap.java b/src/main/java/com/trivago/fastutilconcurrentwrapper/intkey/ConcurrentBusyWaitingIntIntMap.java similarity index 77% rename from src/main/java/com/trivago/fastutilconcurrentwrapper/map/ConcurrentBusyWaitingIntIntMap.java rename to src/main/java/com/trivago/fastutilconcurrentwrapper/intkey/ConcurrentBusyWaitingIntIntMap.java index c97acd1..0e22bb2 100644 --- a/src/main/java/com/trivago/fastutilconcurrentwrapper/map/ConcurrentBusyWaitingIntIntMap.java +++ b/src/main/java/com/trivago/fastutilconcurrentwrapper/intkey/ConcurrentBusyWaitingIntIntMap.java @@ -1,33 +1,13 @@ -package com.trivago.fastutilconcurrentwrapper.map; +package com.trivago.fastutilconcurrentwrapper.intkey; -import com.trivago.fastutilconcurrentwrapper.IntIntMap; -import it.unimi.dsi.fastutil.Function; import it.unimi.dsi.fastutil.ints.Int2IntFunction; -import it.unimi.dsi.fastutil.ints.Int2IntOpenHashMap; import java.util.concurrent.locks.Lock; import java.util.function.BiFunction; -public class ConcurrentBusyWaitingIntIntMap extends PrimitiveConcurrentMap implements IntIntMap { - private final Int2IntOpenHashMap[] maps; - private final int defaultValue; - - public ConcurrentBusyWaitingIntIntMap( - int numBuckets, - int initialCapacity, - float loadFactor, - int defaultValue - ){ - super(numBuckets); - this.maps = new Int2IntOpenHashMap[numBuckets]; - this.defaultValue = defaultValue; - for (int i = 0; i < numBuckets; i++) - maps[i] = new Int2IntOpenHashMap(initialCapacity, loadFactor); - } - - @Override - protected Function mapAt (int index) { - return maps[index]; +public class ConcurrentBusyWaitingIntIntMap extends ConcurrentIntIntMap { + public ConcurrentBusyWaitingIntIntMap (int numBuckets, int initialCapacity, float loadFactor, int defaultValue) { + super(numBuckets, initialCapacity, loadFactor, defaultValue); } @Override @@ -84,8 +64,6 @@ public int put(int key, int value) { } } - @Override public int getDefaultValue (){ return defaultValue; } - @Override public int remove(int key) { int bucket = getBucket(key); diff --git a/src/main/java/com/trivago/fastutilconcurrentwrapper/map/ConcurrentIntFloatMap.java b/src/main/java/com/trivago/fastutilconcurrentwrapper/intkey/ConcurrentIntFloatMap.java similarity index 75% rename from src/main/java/com/trivago/fastutilconcurrentwrapper/map/ConcurrentIntFloatMap.java rename to src/main/java/com/trivago/fastutilconcurrentwrapper/intkey/ConcurrentIntFloatMap.java index 76043b7..8cf7244 100644 --- a/src/main/java/com/trivago/fastutilconcurrentwrapper/map/ConcurrentIntFloatMap.java +++ b/src/main/java/com/trivago/fastutilconcurrentwrapper/intkey/ConcurrentIntFloatMap.java @@ -1,6 +1,7 @@ -package com.trivago.fastutilconcurrentwrapper.map; +package com.trivago.fastutilconcurrentwrapper.intkey; -import com.trivago.fastutilconcurrentwrapper.IntFloatMap; +import com.trivago.fastutilconcurrentwrapper.PrimitiveConcurrentMap; +import com.trivago.fastutilconcurrentwrapper.PrimitiveMapBuilder; import it.unimi.dsi.fastutil.Function; import it.unimi.dsi.fastutil.ints.Int2FloatFunction; import it.unimi.dsi.fastutil.ints.Int2FloatOpenHashMap; @@ -8,9 +9,9 @@ import java.util.concurrent.locks.Lock; import java.util.function.BiFunction; -public class ConcurrentIntFloatMap extends PrimitiveConcurrentMap implements IntFloatMap { - private final Int2FloatOpenHashMap[] maps; - private final float defaultValue; +public class ConcurrentIntFloatMap extends PrimitiveConcurrentMap { + protected final Int2FloatOpenHashMap[] maps; + protected final float defaultValue; public ConcurrentIntFloatMap( int numBuckets, @@ -30,7 +31,6 @@ protected Function mapAt (int index) { return maps[index]; } - @Override public boolean containsKey(int key) { int bucket = getBucket(key); @@ -43,7 +43,6 @@ public boolean containsKey(int key) { } } - @Override public float get(int key) { int bucket = getBucket(key); @@ -56,7 +55,6 @@ public float get(int key) { } } - @Override public float put(int key, float value) { int bucket = getBucket(key); @@ -69,9 +67,8 @@ public float put(int key, float value) { } } - @Override public float getDefaultValue (){ return defaultValue; } + public float getDefaultValue (){ return defaultValue; } - @Override public float remove(int key) { int bucket = getBucket(key); @@ -84,7 +81,6 @@ public float remove(int key) { } } - @Override public boolean remove(int key, float value) { int bucket = getBucket(key); @@ -97,7 +93,6 @@ public boolean remove(int key, float value) { } } - @Override public float computeIfAbsent(int key, Int2FloatFunction mappingFunction) { int bucket = getBucket(key); @@ -110,7 +105,6 @@ public float computeIfAbsent(int key, Int2FloatFunction mappingFunction) { } } - @Override public float computeIfPresent(int key, BiFunction mappingFunction) { int bucket = getBucket(key); @@ -122,4 +116,17 @@ public float computeIfPresent(int key, BiFunction mapping writeLock.unlock(); } } + + public static PrimitiveMapBuilder newBuilder () { + return new PrimitiveMapBuilder<>(){ + @Override + public ConcurrentIntFloatMap build () { + float def = super.defaultValue != null ? super.defaultValue : 0; + return switch (mapMode){ + case BUSY_WAITING -> new ConcurrentBusyWaitingIntFloatMap(buckets, initialCapacity, loadFactor, def); + case BLOCKING -> new ConcurrentIntFloatMap(buckets, initialCapacity, loadFactor, def); + }; + } + }; + } } \ No newline at end of file diff --git a/src/main/java/com/trivago/fastutilconcurrentwrapper/map/ConcurrentIntIntMap.java b/src/main/java/com/trivago/fastutilconcurrentwrapper/intkey/ConcurrentIntIntMap.java similarity index 75% rename from src/main/java/com/trivago/fastutilconcurrentwrapper/map/ConcurrentIntIntMap.java rename to src/main/java/com/trivago/fastutilconcurrentwrapper/intkey/ConcurrentIntIntMap.java index 5616322..f738b7e 100644 --- a/src/main/java/com/trivago/fastutilconcurrentwrapper/map/ConcurrentIntIntMap.java +++ b/src/main/java/com/trivago/fastutilconcurrentwrapper/intkey/ConcurrentIntIntMap.java @@ -1,6 +1,7 @@ -package com.trivago.fastutilconcurrentwrapper.map; +package com.trivago.fastutilconcurrentwrapper.intkey; -import com.trivago.fastutilconcurrentwrapper.IntIntMap; +import com.trivago.fastutilconcurrentwrapper.PrimitiveConcurrentMap; +import com.trivago.fastutilconcurrentwrapper.PrimitiveMapBuilder; import it.unimi.dsi.fastutil.Function; import it.unimi.dsi.fastutil.ints.Int2IntFunction; import it.unimi.dsi.fastutil.ints.Int2IntOpenHashMap; @@ -8,9 +9,9 @@ import java.util.concurrent.locks.Lock; import java.util.function.BiFunction; -public class ConcurrentIntIntMap extends PrimitiveConcurrentMap implements IntIntMap { - private final Int2IntOpenHashMap[] maps; - private final int defaultValue; +public class ConcurrentIntIntMap extends PrimitiveConcurrentMap { + protected final Int2IntOpenHashMap[] maps; + protected final int defaultValue; public ConcurrentIntIntMap( int numBuckets, @@ -30,7 +31,6 @@ protected Function mapAt (int index) { return maps[index]; } - @Override public boolean containsKey(int key) { int bucket = getBucket(key); @@ -43,7 +43,6 @@ public boolean containsKey(int key) { } } - @Override public int get (int key) { int bucket = getBucket(key); @@ -56,7 +55,6 @@ public int get (int key) { } } - @Override public int put(int key, int value) { int bucket = getBucket(key); @@ -69,9 +67,8 @@ public int put(int key, int value) { } } - @Override public int getDefaultValue (){ return defaultValue; } + public int getDefaultValue (){ return defaultValue; } - @Override public int remove(int key) { int bucket = getBucket(key); @@ -84,7 +81,6 @@ public int remove(int key) { } } - @Override public boolean remove(int key, int value) { int bucket = getBucket(key); @@ -97,7 +93,6 @@ public boolean remove(int key, int value) { } } - @Override public int computeIfAbsent(int key, Int2IntFunction mappingFunction) { int bucket = getBucket(key); @@ -110,7 +105,6 @@ public int computeIfAbsent(int key, Int2IntFunction mappingFunction) { } } - @Override public int computeIfPresent(int key, BiFunction mappingFunction) { int bucket = getBucket(key); @@ -122,4 +116,17 @@ public int computeIfPresent(int key, BiFunction mappi writeLock.unlock(); } } + + public static PrimitiveMapBuilder newBuilder () { + return new PrimitiveMapBuilder<>(){ + @Override + public ConcurrentIntIntMap build () { + int def = super.defaultValue != null ? super.defaultValue : 0; + return switch (mapMode){ + case BUSY_WAITING -> new ConcurrentBusyWaitingIntIntMap(buckets, initialCapacity, loadFactor, def); + case BLOCKING -> new ConcurrentIntIntMap(buckets, initialCapacity, loadFactor, def); + }; + } + }; + } } \ No newline at end of file diff --git a/src/main/java/com/trivago/fastutilconcurrentwrapper/map/ConcurrentBusyWaitingLongFloatMap.java b/src/main/java/com/trivago/fastutilconcurrentwrapper/longkey/ConcurrentBusyWaitingLongFloatMap.java similarity index 77% rename from src/main/java/com/trivago/fastutilconcurrentwrapper/map/ConcurrentBusyWaitingLongFloatMap.java rename to src/main/java/com/trivago/fastutilconcurrentwrapper/longkey/ConcurrentBusyWaitingLongFloatMap.java index 2f6f5f6..9e18a06 100644 --- a/src/main/java/com/trivago/fastutilconcurrentwrapper/map/ConcurrentBusyWaitingLongFloatMap.java +++ b/src/main/java/com/trivago/fastutilconcurrentwrapper/longkey/ConcurrentBusyWaitingLongFloatMap.java @@ -1,33 +1,13 @@ -package com.trivago.fastutilconcurrentwrapper.map; +package com.trivago.fastutilconcurrentwrapper.longkey; -import com.trivago.fastutilconcurrentwrapper.LongFloatMap; -import it.unimi.dsi.fastutil.Function; import it.unimi.dsi.fastutil.longs.Long2FloatFunction; -import it.unimi.dsi.fastutil.longs.Long2FloatOpenHashMap; import java.util.concurrent.locks.Lock; import java.util.function.BiFunction; -public class ConcurrentBusyWaitingLongFloatMap extends PrimitiveConcurrentMap implements LongFloatMap { - private final Long2FloatOpenHashMap[] maps; - private final float defaultValue; - - public ConcurrentBusyWaitingLongFloatMap( - int numBuckets, - int initialCapacity, - float loadFactor, - float defaultValue - ){ - super(numBuckets); - this.defaultValue = defaultValue; - this.maps = new Long2FloatOpenHashMap[numBuckets]; - for (int i = 0; i < numBuckets; i++) - maps[i] = new Long2FloatOpenHashMap(initialCapacity, loadFactor); - } - - @Override - protected Function mapAt (int index) { - return maps[index]; +public class ConcurrentBusyWaitingLongFloatMap extends ConcurrentLongFloatMap { + public ConcurrentBusyWaitingLongFloatMap (int numBuckets, int initialCapacity, float loadFactor, float defaultValue) { + super(numBuckets, initialCapacity, loadFactor, defaultValue); } @Override @@ -84,8 +64,6 @@ public float put(long key, float value) { } } - @Override public float getDefaultValue (){ return defaultValue; } - @Override public float remove(long key) { int bucket = getBucket(key); diff --git a/src/main/java/com/trivago/fastutilconcurrentwrapper/map/ConcurrentBusyWaitingLongIntMap.java b/src/main/java/com/trivago/fastutilconcurrentwrapper/longkey/ConcurrentBusyWaitingLongIntMap.java similarity index 77% rename from src/main/java/com/trivago/fastutilconcurrentwrapper/map/ConcurrentBusyWaitingLongIntMap.java rename to src/main/java/com/trivago/fastutilconcurrentwrapper/longkey/ConcurrentBusyWaitingLongIntMap.java index 778b796..a08057d 100644 --- a/src/main/java/com/trivago/fastutilconcurrentwrapper/map/ConcurrentBusyWaitingLongIntMap.java +++ b/src/main/java/com/trivago/fastutilconcurrentwrapper/longkey/ConcurrentBusyWaitingLongIntMap.java @@ -1,33 +1,13 @@ -package com.trivago.fastutilconcurrentwrapper.map; +package com.trivago.fastutilconcurrentwrapper.longkey; -import com.trivago.fastutilconcurrentwrapper.LongIntMap; -import it.unimi.dsi.fastutil.Function; import it.unimi.dsi.fastutil.longs.Long2IntFunction; -import it.unimi.dsi.fastutil.longs.Long2IntOpenHashMap; import java.util.concurrent.locks.Lock; import java.util.function.BiFunction; -public class ConcurrentBusyWaitingLongIntMap extends PrimitiveConcurrentMap implements LongIntMap { - private final Long2IntOpenHashMap[] maps; - private final int defaultValue; - - public ConcurrentBusyWaitingLongIntMap( - int numBuckets, - int initialCapacity, - float loadFactor, - int defaultValue - ){ - super(numBuckets); - this.maps = new Long2IntOpenHashMap[numBuckets]; - this.defaultValue = defaultValue; - for (int i = 0; i < numBuckets; i++) - maps[i] = new Long2IntOpenHashMap(initialCapacity, loadFactor); - } - - @Override - protected Function mapAt (int index) { - return maps[index]; +public class ConcurrentBusyWaitingLongIntMap extends ConcurrentLongIntMap { + public ConcurrentBusyWaitingLongIntMap (int numBuckets, int initialCapacity, float loadFactor, int defaultValue) { + super(numBuckets, initialCapacity, loadFactor, defaultValue); } @Override @@ -84,8 +64,6 @@ public int put(long key, int value) { } } - @Override public int getDefaultValue (){ return defaultValue; } - @Override public int remove(long key) { int bucket = getBucket(key); diff --git a/src/main/java/com/trivago/fastutilconcurrentwrapper/map/ConcurrentBusyWaitingLongLongMap.java b/src/main/java/com/trivago/fastutilconcurrentwrapper/longkey/ConcurrentBusyWaitingLongLongMap.java similarity index 77% rename from src/main/java/com/trivago/fastutilconcurrentwrapper/map/ConcurrentBusyWaitingLongLongMap.java rename to src/main/java/com/trivago/fastutilconcurrentwrapper/longkey/ConcurrentBusyWaitingLongLongMap.java index 6a77083..80917cd 100644 --- a/src/main/java/com/trivago/fastutilconcurrentwrapper/map/ConcurrentBusyWaitingLongLongMap.java +++ b/src/main/java/com/trivago/fastutilconcurrentwrapper/longkey/ConcurrentBusyWaitingLongLongMap.java @@ -1,33 +1,13 @@ -package com.trivago.fastutilconcurrentwrapper.map; +package com.trivago.fastutilconcurrentwrapper.longkey; -import com.trivago.fastutilconcurrentwrapper.LongLongMap; -import it.unimi.dsi.fastutil.Function; import it.unimi.dsi.fastutil.longs.Long2LongFunction; -import it.unimi.dsi.fastutil.longs.Long2LongOpenHashMap; import java.util.concurrent.locks.Lock; import java.util.function.BiFunction; -public class ConcurrentBusyWaitingLongLongMap extends PrimitiveConcurrentMap implements LongLongMap { - private final Long2LongOpenHashMap[] maps; - private final long defaultValue; - - public ConcurrentBusyWaitingLongLongMap( - int numBuckets, - int initialCapacity, - float loadFactor, - long defaultValue - ){ - super(numBuckets); - this.maps = new Long2LongOpenHashMap[numBuckets]; - this.defaultValue = defaultValue; - for (int i = 0; i < numBuckets; i++) - maps[i] = new Long2LongOpenHashMap(initialCapacity, loadFactor); - } - - @Override - protected Function mapAt (int index) { - return maps[index]; +public class ConcurrentBusyWaitingLongLongMap extends ConcurrentLongLongMap { + public ConcurrentBusyWaitingLongLongMap (int numBuckets, int initialCapacity, float loadFactor, long defaultValue) { + super(numBuckets, initialCapacity, loadFactor, defaultValue); } @Override @@ -84,8 +64,6 @@ public long put(long key, long value) { } } - @Override public long getDefaultValue (){ return defaultValue; } - @Override public long remove(long key) { int bucket = getBucket(key); diff --git a/src/main/java/com/trivago/fastutilconcurrentwrapper/longkey/ConcurrentBusyWaitingLongObjectMap.java b/src/main/java/com/trivago/fastutilconcurrentwrapper/longkey/ConcurrentBusyWaitingLongObjectMap.java new file mode 100644 index 0000000..76e2352 --- /dev/null +++ b/src/main/java/com/trivago/fastutilconcurrentwrapper/longkey/ConcurrentBusyWaitingLongObjectMap.java @@ -0,0 +1,138 @@ +package com.trivago.fastutilconcurrentwrapper.longkey; + +import it.unimi.dsi.fastutil.longs.Long2ObjectFunction; + +import java.util.concurrent.locks.Lock; +import java.util.function.BiFunction; + +public class ConcurrentBusyWaitingLongObjectMap extends ConcurrentLongObjectMap { + public ConcurrentBusyWaitingLongObjectMap (int numBuckets, int initialCapacity, float loadFactor, V defaultValue) { + super(numBuckets, initialCapacity, loadFactor, defaultValue); + } + + @Override + public boolean containsKey(long key) { + int bucket = getBucket(key); + + Lock readLock = locks[bucket].readLock(); + + while (true) { + if (readLock.tryLock()) { + try { + return maps[bucket].containsKey(key); + } finally { + readLock.unlock(); + } + } + Thread.onSpinWait(); + } + } + + @Override + public V get (long key) { + int bucket = getBucket(key); + + Lock readLock = locks[bucket].readLock(); + + while (true) { + if (readLock.tryLock()) { + try { + return maps[bucket].getOrDefault(key, defaultValue); + } finally { + readLock.unlock(); + } + } + Thread.onSpinWait(); + } + } + + @Override + public V put (long key, V value) { + int bucket = getBucket(key); + + Lock writeLock = locks[bucket].writeLock(); + + while (true) { + if (writeLock.tryLock()) { + try { + return maps[bucket].put(key, value); + } finally { + writeLock.unlock(); + } + } + Thread.onSpinWait(); + } + } + + @Override + public V remove (long key) { + int bucket = getBucket(key); + + Lock writeLock = locks[bucket].writeLock(); + + while (true) { + if (writeLock.tryLock()) { + try { + return maps[bucket].remove(key); + } finally { + writeLock.unlock(); + } + } + Thread.onSpinWait(); + } + } + + @Override + public boolean remove (long key, V value) { + int bucket = getBucket(key); + + Lock writeLock = locks[bucket].writeLock(); + + while (true) { + if (writeLock.tryLock()) { + try { + return maps[bucket].remove(key, value); + } finally { + writeLock.unlock(); + } + } + Thread.onSpinWait(); + } + } + + @Override + public V computeIfAbsent (long key, Long2ObjectFunction mappingFunction) { + int bucket = getBucket(key); + + Lock writeLock = locks[bucket].writeLock(); + + while (true) { + if (writeLock.tryLock()) { + try { + return maps[bucket].computeIfAbsent(key, mappingFunction); + } finally { + writeLock.unlock(); + } + } + Thread.onSpinWait(); + } + } + + @Override + public V computeIfPresent (long key, BiFunction mappingFunction) { + int bucket = getBucket(key); + + Lock writeLock = locks[bucket].writeLock(); + + while (true) { + if (writeLock.tryLock()) { + try { + return maps[bucket].computeIfPresent(key, mappingFunction); + } finally { + writeLock.unlock(); + } + } + Thread.onSpinWait(); + } + } +} \ No newline at end of file diff --git a/src/main/java/com/trivago/fastutilconcurrentwrapper/map/ConcurrentLongFloatMap.java b/src/main/java/com/trivago/fastutilconcurrentwrapper/longkey/ConcurrentLongFloatMap.java similarity index 75% rename from src/main/java/com/trivago/fastutilconcurrentwrapper/map/ConcurrentLongFloatMap.java rename to src/main/java/com/trivago/fastutilconcurrentwrapper/longkey/ConcurrentLongFloatMap.java index 86cf126..8809d30 100644 --- a/src/main/java/com/trivago/fastutilconcurrentwrapper/map/ConcurrentLongFloatMap.java +++ b/src/main/java/com/trivago/fastutilconcurrentwrapper/longkey/ConcurrentLongFloatMap.java @@ -1,6 +1,7 @@ -package com.trivago.fastutilconcurrentwrapper.map; +package com.trivago.fastutilconcurrentwrapper.longkey; -import com.trivago.fastutilconcurrentwrapper.LongFloatMap; +import com.trivago.fastutilconcurrentwrapper.PrimitiveConcurrentMap; +import com.trivago.fastutilconcurrentwrapper.PrimitiveMapBuilder; import it.unimi.dsi.fastutil.Function; import it.unimi.dsi.fastutil.longs.Long2FloatFunction; import it.unimi.dsi.fastutil.longs.Long2FloatOpenHashMap; @@ -8,9 +9,9 @@ import java.util.concurrent.locks.Lock; import java.util.function.BiFunction; -public class ConcurrentLongFloatMap extends PrimitiveConcurrentMap implements LongFloatMap { - private final Long2FloatOpenHashMap[] maps; - private final float defaultValue; +public class ConcurrentLongFloatMap extends PrimitiveConcurrentMap { + protected final Long2FloatOpenHashMap[] maps; + protected final float defaultValue; public ConcurrentLongFloatMap ( int numBuckets, @@ -25,14 +26,13 @@ public ConcurrentLongFloatMap ( maps[i] = new Long2FloatOpenHashMap(initialCapacity, loadFactor); } - @Override public float getDefaultValue (){ return defaultValue; } + public float getDefaultValue (){ return defaultValue; } @Override protected Function mapAt (int index) { return maps[index]; } - @Override public boolean containsKey(long key) { int bucket = getBucket(key); @@ -45,7 +45,6 @@ public boolean containsKey(long key) { } } - @Override public float get(long key) { int bucket = getBucket(key); @@ -58,7 +57,6 @@ public float get(long key) { } } - @Override public float put(long key, float value) { int bucket = getBucket(key); @@ -71,7 +69,6 @@ public float put(long key, float value) { } } - @Override public float remove(long key) { int bucket = getBucket(key); @@ -84,7 +81,6 @@ public float remove(long key) { } } - @Override public boolean remove(long key, float value) { int bucket = getBucket(key); @@ -97,7 +93,6 @@ public boolean remove(long key, float value) { } } - @Override public float computeIfAbsent(long key, Long2FloatFunction mappingFunction) { int bucket = getBucket(key); @@ -110,7 +105,6 @@ public float computeIfAbsent(long key, Long2FloatFunction mappingFunction) { } } - @Override public float computeIfPresent(int key, BiFunction mappingFunction) { int bucket = getBucket(key); @@ -122,4 +116,17 @@ public float computeIfPresent(int key, BiFunction mappingFun writeLock.unlock(); } } + + public static PrimitiveMapBuilder newBuilder () { + return new PrimitiveMapBuilder<>(){ + @Override + public ConcurrentLongFloatMap build () { + float def = super.defaultValue != null ? super.defaultValue : 0; + return switch (mapMode){ + case BUSY_WAITING -> new ConcurrentBusyWaitingLongFloatMap(buckets, initialCapacity, loadFactor, def); + case BLOCKING -> new ConcurrentLongFloatMap(buckets, initialCapacity, loadFactor, def); + }; + } + }; + } } \ No newline at end of file diff --git a/src/main/java/com/trivago/fastutilconcurrentwrapper/map/ConcurrentLongIntMap.java b/src/main/java/com/trivago/fastutilconcurrentwrapper/longkey/ConcurrentLongIntMap.java similarity index 74% rename from src/main/java/com/trivago/fastutilconcurrentwrapper/map/ConcurrentLongIntMap.java rename to src/main/java/com/trivago/fastutilconcurrentwrapper/longkey/ConcurrentLongIntMap.java index 06e33bb..31ec48b 100644 --- a/src/main/java/com/trivago/fastutilconcurrentwrapper/map/ConcurrentLongIntMap.java +++ b/src/main/java/com/trivago/fastutilconcurrentwrapper/longkey/ConcurrentLongIntMap.java @@ -1,6 +1,7 @@ -package com.trivago.fastutilconcurrentwrapper.map; +package com.trivago.fastutilconcurrentwrapper.longkey; -import com.trivago.fastutilconcurrentwrapper.LongIntMap; +import com.trivago.fastutilconcurrentwrapper.PrimitiveConcurrentMap; +import com.trivago.fastutilconcurrentwrapper.PrimitiveMapBuilder; import it.unimi.dsi.fastutil.Function; import it.unimi.dsi.fastutil.longs.Long2IntFunction; import it.unimi.dsi.fastutil.longs.Long2IntOpenHashMap; @@ -8,11 +9,11 @@ import java.util.concurrent.locks.Lock; import java.util.function.BiFunction; -public class ConcurrentLongIntMap extends PrimitiveConcurrentMap implements LongIntMap { - private final Long2IntOpenHashMap[] maps; - private final int defaultValue; +public class ConcurrentLongIntMap extends PrimitiveConcurrentMap { + protected final Long2IntOpenHashMap[] maps; + protected final int defaultValue; - public ConcurrentLongIntMap( + public ConcurrentLongIntMap ( int numBuckets, int initialCapacity, float loadFactor, @@ -30,7 +31,6 @@ protected Function mapAt (int index) { return maps[index]; } - @Override public boolean containsKey(long key) { int bucket = getBucket(key); @@ -43,7 +43,6 @@ public boolean containsKey(long key) { } } - @Override public int get(long key) { int bucket = getBucket(key); @@ -56,7 +55,6 @@ public int get(long key) { } } - @Override public int put(long key, int value) { int bucket = getBucket(key); @@ -69,9 +67,8 @@ public int put(long key, int value) { } } - @Override public int getDefaultValue (){ return defaultValue; } + public int getDefaultValue (){ return defaultValue; } - @Override public int remove(long key) { int bucket = getBucket(key); @@ -84,7 +81,6 @@ public int remove(long key) { } } - @Override public boolean remove(long key, int value) { int bucket = getBucket(key); @@ -97,7 +93,6 @@ public boolean remove(long key, int value) { } } - @Override public int computeIfAbsent(long key, Long2IntFunction mappingFunction) { int bucket = getBucket(key); @@ -110,7 +105,6 @@ public int computeIfAbsent(long key, Long2IntFunction mappingFunction) { } } - @Override public int computeIfPresent(long key, BiFunction mappingFunction) { int bucket = getBucket(key); @@ -122,4 +116,17 @@ public int computeIfPresent(long key, BiFunction mapping writeLock.unlock(); } } + + public static PrimitiveMapBuilder newBuilder () { + return new PrimitiveMapBuilder<>(){ + @Override + public ConcurrentLongIntMap build() { + int def = super.defaultValue != null ? super.defaultValue : 0; + return switch (mapMode){ + case BUSY_WAITING -> new ConcurrentBusyWaitingLongIntMap(buckets, initialCapacity, loadFactor, def); + case BLOCKING -> new ConcurrentLongIntMap(buckets, initialCapacity, loadFactor, def); + }; + } + }; + } } \ No newline at end of file diff --git a/src/main/java/com/trivago/fastutilconcurrentwrapper/map/ConcurrentLongLongMap.java b/src/main/java/com/trivago/fastutilconcurrentwrapper/longkey/ConcurrentLongLongMap.java similarity index 74% rename from src/main/java/com/trivago/fastutilconcurrentwrapper/map/ConcurrentLongLongMap.java rename to src/main/java/com/trivago/fastutilconcurrentwrapper/longkey/ConcurrentLongLongMap.java index ac8a0da..afbfd9d 100644 --- a/src/main/java/com/trivago/fastutilconcurrentwrapper/map/ConcurrentLongLongMap.java +++ b/src/main/java/com/trivago/fastutilconcurrentwrapper/longkey/ConcurrentLongLongMap.java @@ -1,6 +1,7 @@ -package com.trivago.fastutilconcurrentwrapper.map; +package com.trivago.fastutilconcurrentwrapper.longkey; -import com.trivago.fastutilconcurrentwrapper.LongLongMap; +import com.trivago.fastutilconcurrentwrapper.PrimitiveConcurrentMap; +import com.trivago.fastutilconcurrentwrapper.PrimitiveMapBuilder; import it.unimi.dsi.fastutil.Function; import it.unimi.dsi.fastutil.longs.Long2LongFunction; import it.unimi.dsi.fastutil.longs.Long2LongOpenHashMap; @@ -8,9 +9,9 @@ import java.util.concurrent.locks.Lock; import java.util.function.BiFunction; -public class ConcurrentLongLongMap extends PrimitiveConcurrentMap implements LongLongMap { - private final Long2LongOpenHashMap[] maps; - private final long defaultValue; +public class ConcurrentLongLongMap extends PrimitiveConcurrentMap { + protected final Long2LongOpenHashMap[] maps; + protected final long defaultValue; public ConcurrentLongLongMap( int numBuckets, @@ -26,11 +27,10 @@ public ConcurrentLongLongMap( } @Override - protected Function mapAt (int index) { + protected final Function mapAt (int index) { return maps[index]; } - @Override public boolean containsKey(long key) { int bucket = getBucket(key); @@ -43,7 +43,6 @@ public boolean containsKey(long key) { } } - @Override public long get (long key) { int bucket = getBucket(key); @@ -56,7 +55,6 @@ public long get (long key) { } } - @Override public long put(long key, long value) { int bucket = getBucket(key); @@ -69,9 +67,8 @@ public long put(long key, long value) { } } - @Override public long getDefaultValue (){ return defaultValue; } + public long getDefaultValue (){ return defaultValue; } - @Override public long remove(long key) { int bucket = getBucket(key); @@ -84,7 +81,6 @@ public long remove(long key) { } } - @Override public boolean remove(long key, long value) { int bucket = getBucket(key); @@ -97,7 +93,6 @@ public boolean remove(long key, long value) { } } - @Override public long computeIfAbsent(long key, Long2LongFunction mappingFunction) { int bucket = getBucket(key); @@ -110,7 +105,6 @@ public long computeIfAbsent(long key, Long2LongFunction mappingFunction) { } } - @Override public long computeIfPresent(long key, BiFunction mappingFunction) { int bucket = getBucket(key); @@ -122,4 +116,17 @@ public long computeIfPresent(long key, BiFunction mappingFunct writeLock.unlock(); } } + + public static PrimitiveMapBuilder newBuilder () { + return new PrimitiveMapBuilder<>(){ + @Override + public ConcurrentLongLongMap build() { + long def = super.defaultValue != null ? super.defaultValue : 0; + return switch (mapMode){ + case BUSY_WAITING -> new ConcurrentBusyWaitingLongLongMap(buckets, initialCapacity, loadFactor, def); + case BLOCKING -> new ConcurrentLongLongMap(buckets, initialCapacity, loadFactor, def); + }; + } + }; + } } \ No newline at end of file diff --git a/src/main/java/com/trivago/fastutilconcurrentwrapper/longkey/ConcurrentLongObjectMap.java b/src/main/java/com/trivago/fastutilconcurrentwrapper/longkey/ConcurrentLongObjectMap.java new file mode 100644 index 0000000..9f5180a --- /dev/null +++ b/src/main/java/com/trivago/fastutilconcurrentwrapper/longkey/ConcurrentLongObjectMap.java @@ -0,0 +1,132 @@ +package com.trivago.fastutilconcurrentwrapper.longkey; + +import com.trivago.fastutilconcurrentwrapper.PrimitiveConcurrentMap; +import com.trivago.fastutilconcurrentwrapper.PrimitiveMapBuilder; +import it.unimi.dsi.fastutil.Function; +import it.unimi.dsi.fastutil.longs.Long2ObjectFunction; +import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap; + +import java.util.concurrent.locks.Lock; +import java.util.function.BiFunction; + +public class ConcurrentLongObjectMap extends PrimitiveConcurrentMap { + protected final Long2ObjectOpenHashMap[] maps; + protected final V defaultValue; + + @SuppressWarnings("unchecked") + public ConcurrentLongObjectMap ( + int numBuckets, + int initialCapacity, + float loadFactor, + V defaultValue + ){ + super(numBuckets); + this.maps = new Long2ObjectOpenHashMap[numBuckets]; + this.defaultValue = defaultValue; + for (int i = 0; i < numBuckets; i++) + maps[i] = new Long2ObjectOpenHashMap<>(initialCapacity, loadFactor); + } + + @Override + protected final Function mapAt (int index) { + return maps[index]; + } + + public boolean containsKey(long key) { + int bucket = getBucket(key); + + Lock readLock = locks[bucket].readLock(); + readLock.lock(); + try { + return maps[bucket].containsKey(key); + } finally { + readLock.unlock(); + } + } + + public V get (long key) { + int bucket = getBucket(key); + + Lock readLock = locks[bucket].readLock(); + readLock.lock(); + try { + return maps[bucket].getOrDefault(key, defaultValue); + } finally { + readLock.unlock(); + } + } + + public V put (long key, V value) { + int bucket = getBucket(key); + + Lock writeLock = locks[bucket].writeLock(); + writeLock.lock(); + try { + return maps[bucket].put(key, value); + } finally { + writeLock.unlock(); + } + } + + public V getDefaultValue (){ return defaultValue; } + + public V remove (long key) { + int bucket = getBucket(key); + + Lock writeLock = locks[bucket].writeLock(); + writeLock.lock(); + try { + return maps[bucket].remove(key); + } finally { + writeLock.unlock(); + } + } + + public boolean remove(long key, V value) { + int bucket = getBucket(key); + + Lock writeLock = locks[bucket].writeLock(); + writeLock.lock(); + try { + return maps[bucket].remove(key, value); + } finally { + writeLock.unlock(); + } + } + + public V computeIfAbsent (long key, Long2ObjectFunction mappingFunction) { + int bucket = getBucket(key); + + Lock writeLock = locks[bucket].writeLock(); + writeLock.lock(); + try { + return maps[bucket].computeIfAbsent(key, mappingFunction); + } finally { + writeLock.unlock(); + } + } + + public V computeIfPresent (long key, BiFunction mappingFunction) { + int bucket = getBucket(key); + + Lock writeLock = locks[bucket].writeLock(); + writeLock.lock(); + try { + return maps[bucket].computeIfPresent(key, mappingFunction); + } finally { + writeLock.unlock(); + } + } + + public static PrimitiveMapBuilder,V> newBuilder () { + return new PrimitiveMapBuilder<>(){ + @Override + public ConcurrentLongObjectMap build() { + return switch (mapMode){ + case BUSY_WAITING -> new ConcurrentBusyWaitingLongObjectMap<>(buckets, initialCapacity, loadFactor, super.defaultValue); + case BLOCKING -> new ConcurrentLongObjectMap<>(buckets, initialCapacity, loadFactor, super.defaultValue); + }; + } + }; + } +} \ No newline at end of file diff --git a/src/main/java/com/trivago/fastutilconcurrentwrapper/objkey/ConcurrentBusyWaitingObjectLongMap.java b/src/main/java/com/trivago/fastutilconcurrentwrapper/objkey/ConcurrentBusyWaitingObjectLongMap.java new file mode 100644 index 0000000..7813157 --- /dev/null +++ b/src/main/java/com/trivago/fastutilconcurrentwrapper/objkey/ConcurrentBusyWaitingObjectLongMap.java @@ -0,0 +1,138 @@ +package com.trivago.fastutilconcurrentwrapper.objkey; + +import it.unimi.dsi.fastutil.objects.Object2LongFunction; + +import java.util.concurrent.locks.Lock; +import java.util.function.BiFunction; + +public class ConcurrentBusyWaitingObjectLongMap extends ConcurrentObjectLongMap { + public ConcurrentBusyWaitingObjectLongMap (int numBuckets, int initialCapacity, float loadFactor, long defaultValue) { + super(numBuckets, initialCapacity, loadFactor, defaultValue); + } + + @Override + public boolean containsKey (K key) { + int bucket = getBucket(key); + + Lock readLock = locks[bucket].readLock(); + + while (true) { + if (readLock.tryLock()) { + try { + return maps[bucket].containsKey(key); + } finally { + readLock.unlock(); + } + } + Thread.onSpinWait(); + } + } + + @Override + public long get (K key) { + int bucket = getBucket(key); + + Lock readLock = locks[bucket].readLock(); + + while (true) { + if (readLock.tryLock()) { + try { + return maps[bucket].getOrDefault(key, defaultValue); + } finally { + readLock.unlock(); + } + } + Thread.onSpinWait(); + } + } + + @Override + public long put (K key, long value) { + int bucket = getBucket(key); + + Lock writeLock = locks[bucket].writeLock(); + + while (true) { + if (writeLock.tryLock()) { + try { + return maps[bucket].put(key, value); + } finally { + writeLock.unlock(); + } + } + Thread.onSpinWait(); + } + } + + @Override + public long remove (K key) { + int bucket = getBucket(key); + + Lock writeLock = locks[bucket].writeLock(); + + while (true) { + if (writeLock.tryLock()) { + try { + return maps[bucket].removeLong(key); + } finally { + writeLock.unlock(); + } + } + Thread.onSpinWait(); + } + } + + @Override + public boolean remove (K key, long value) { + int bucket = getBucket(key); + + Lock writeLock = locks[bucket].writeLock(); + + while (true) { + if (writeLock.tryLock()) { + try { + return maps[bucket].remove(key, value); + } finally { + writeLock.unlock(); + } + } + Thread.onSpinWait(); + } + } + + @Override + public long computeIfAbsent (K key, Object2LongFunction mappingFunction) { + int bucket = getBucket(key); + + Lock writeLock = locks[bucket].writeLock(); + + while (true) { + if (writeLock.tryLock()) { + try { + return maps[bucket].computeIfAbsent(key, mappingFunction); + } finally { + writeLock.unlock(); + } + } + Thread.onSpinWait(); + } + } + + @Override + public long computeIfPresent (K key, BiFunction mappingFunction) { + int bucket = getBucket(key); + + Lock writeLock = locks[bucket].writeLock(); + + while (true) { + if (writeLock.tryLock()) { + try { + return maps[bucket].computeIfPresent(key, mappingFunction); + } finally { + writeLock.unlock(); + } + } + Thread.onSpinWait(); + } + } +} \ No newline at end of file diff --git a/src/main/java/com/trivago/fastutilconcurrentwrapper/objkey/ConcurrentObjectLongMap.java b/src/main/java/com/trivago/fastutilconcurrentwrapper/objkey/ConcurrentObjectLongMap.java new file mode 100644 index 0000000..f20120e --- /dev/null +++ b/src/main/java/com/trivago/fastutilconcurrentwrapper/objkey/ConcurrentObjectLongMap.java @@ -0,0 +1,132 @@ +package com.trivago.fastutilconcurrentwrapper.objkey; + +import com.trivago.fastutilconcurrentwrapper.PrimitiveConcurrentMap; +import com.trivago.fastutilconcurrentwrapper.PrimitiveMapBuilder; +import it.unimi.dsi.fastutil.Function; +import it.unimi.dsi.fastutil.objects.Object2LongFunction; +import it.unimi.dsi.fastutil.objects.Object2LongOpenHashMap; + +import java.util.concurrent.locks.Lock; +import java.util.function.BiFunction; + +public class ConcurrentObjectLongMap extends PrimitiveConcurrentMap { + protected final Object2LongOpenHashMap[] maps; + protected final long defaultValue; + + @SuppressWarnings("unchecked") + public ConcurrentObjectLongMap ( + int numBuckets, + int initialCapacity, + float loadFactor, + long defaultValue + ){ + super(numBuckets); + this.maps = new Object2LongOpenHashMap[numBuckets]; + this.defaultValue = defaultValue; + for (int i = 0; i < numBuckets; i++) + maps[i] = new Object2LongOpenHashMap<>(initialCapacity, loadFactor); + } + + @Override + protected final Function mapAt (int index) { + return maps[index]; + } + + public boolean containsKey(K key) { + int bucket = getBucket(key); + + Lock readLock = locks[bucket].readLock(); + readLock.lock(); + try { + return maps[bucket].containsKey(key); + } finally { + readLock.unlock(); + } + } + + public long get (K key) { + int bucket = getBucket(key); + + Lock readLock = locks[bucket].readLock(); + readLock.lock(); + try { + return maps[bucket].getOrDefault(key, defaultValue); + } finally { + readLock.unlock(); + } + } + + public long put (K key, long value) { + int bucket = getBucket(key); + + Lock writeLock = locks[bucket].writeLock(); + writeLock.lock(); + try { + return maps[bucket].put(key, value); + } finally { + writeLock.unlock(); + } + } + + public long getDefaultValue (){ return defaultValue; } + + public long remove (K key) { + int bucket = getBucket(key); + + Lock writeLock = locks[bucket].writeLock(); + writeLock.lock(); + try { + return maps[bucket].removeLong(key); + } finally { + writeLock.unlock(); + } + } + + public boolean remove (K key, long value) { + int bucket = getBucket(key); + + Lock writeLock = locks[bucket].writeLock(); + writeLock.lock(); + try { + return maps[bucket].remove(key, value); + } finally { + writeLock.unlock(); + } + } + + public long computeIfAbsent (K key, Object2LongFunction mappingFunction) { + int bucket = getBucket(key); + + Lock writeLock = locks[bucket].writeLock(); + writeLock.lock(); + try { + return maps[bucket].computeIfAbsent(key, mappingFunction); + } finally { + writeLock.unlock(); + } + } + + public long computeIfPresent (K key, BiFunction mappingFunction) { + int bucket = getBucket(key); + + Lock writeLock = locks[bucket].writeLock(); + writeLock.lock(); + try { + return maps[bucket].computeIfPresent(key, mappingFunction); + } finally { + writeLock.unlock(); + } + } + + public static PrimitiveMapBuilder,Long> newBuilder () { + return new PrimitiveMapBuilder<>(){ + @Override + public ConcurrentObjectLongMap build() { + return switch (mapMode){ + case BUSY_WAITING -> new ConcurrentBusyWaitingObjectLongMap<>(buckets, initialCapacity, loadFactor, super.defaultValue); + case BLOCKING -> new ConcurrentObjectLongMap<>(buckets, initialCapacity, loadFactor, super.defaultValue); + }; + } + }; + } +} \ No newline at end of file diff --git a/src/test/java/com/trivago/fastutilconcurrentwrapper/ConcurrentIntFloatMapBuilderTest.java b/src/test/java/com/trivago/fastutilconcurrentwrapper/ConcurrentIntFloatMapBuilderTest.java index 8e3bc2f..fabb218 100644 --- a/src/test/java/com/trivago/fastutilconcurrentwrapper/ConcurrentIntFloatMapBuilderTest.java +++ b/src/test/java/com/trivago/fastutilconcurrentwrapper/ConcurrentIntFloatMapBuilderTest.java @@ -1,7 +1,7 @@ package com.trivago.fastutilconcurrentwrapper; -import com.trivago.fastutilconcurrentwrapper.map.ConcurrentBusyWaitingIntFloatMap; -import com.trivago.fastutilconcurrentwrapper.map.ConcurrentIntFloatMap; +import com.trivago.fastutilconcurrentwrapper.intkey.ConcurrentBusyWaitingIntFloatMap; +import com.trivago.fastutilconcurrentwrapper.intkey.ConcurrentIntFloatMap; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; @@ -11,14 +11,14 @@ public class ConcurrentIntFloatMapBuilderTest { @Test public void buildsBusyWaitingMap() { - var b = IntFloatMap.newBuilder() + var b = ConcurrentIntFloatMap.newBuilder() .withBuckets(2) .withDefaultValue(DEFAULT_VALUE) .withInitialCapacity(100) .withMode(PrimitiveMapBuilder.MapMode.BUSY_WAITING) .withLoadFactor(0.8f); - IntFloatMap map = b.build(); + ConcurrentIntFloatMap map = b.build(); map.put(1, 10.1f); float v = map.get(1); @@ -30,19 +30,20 @@ public void buildsBusyWaitingMap() { @Test public void buildsBlockingMap() { - var b = IntFloatMap.newBuilder() + var b = ConcurrentIntFloatMap.newBuilder() .withBuckets(2) .withDefaultValue(DEFAULT_VALUE) .withInitialCapacity(100) .withMode(PrimitiveMapBuilder.MapMode.BLOCKING) .withLoadFactor(0.8f); - IntFloatMap map = b.build(); + ConcurrentIntFloatMap map = b.build(); map.put(1, 10.1f); float v = map.get(1); assertInstanceOf(ConcurrentIntFloatMap.class, map); + assertSame(ConcurrentIntFloatMap.class, map.getClass()); assertEquals(10.1f, v); assertEquals(map.get(2), map.getDefaultValue()); } diff --git a/src/test/java/com/trivago/fastutilconcurrentwrapper/ConcurrentIntIntMapBuilderTest.java b/src/test/java/com/trivago/fastutilconcurrentwrapper/ConcurrentIntIntMapBuilderTest.java index 3b22299..d095aeb 100644 --- a/src/test/java/com/trivago/fastutilconcurrentwrapper/ConcurrentIntIntMapBuilderTest.java +++ b/src/test/java/com/trivago/fastutilconcurrentwrapper/ConcurrentIntIntMapBuilderTest.java @@ -1,7 +1,7 @@ package com.trivago.fastutilconcurrentwrapper; -import com.trivago.fastutilconcurrentwrapper.map.ConcurrentBusyWaitingIntIntMap; -import com.trivago.fastutilconcurrentwrapper.map.ConcurrentIntIntMap; +import com.trivago.fastutilconcurrentwrapper.intkey.ConcurrentBusyWaitingIntIntMap; +import com.trivago.fastutilconcurrentwrapper.intkey.ConcurrentIntIntMap; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; @@ -11,14 +11,14 @@ public class ConcurrentIntIntMapBuilderTest { @Test public void buildsBusyWaitingMap() { - var b = IntIntMap.newBuilder() + var b = ConcurrentIntIntMap.newBuilder() .withBuckets(2) .withDefaultValue(DEFAULT_VALUE) .withInitialCapacity(100) .withMode(PrimitiveMapBuilder.MapMode.BUSY_WAITING) .withLoadFactor(0.8f); - IntIntMap map = b.build(); + ConcurrentIntIntMap map = b.build(); map.put(1, 10); int v = map.get(1); @@ -30,19 +30,20 @@ public void buildsBusyWaitingMap() { @Test public void buildsBlockingMap() { - var b = IntIntMap.newBuilder() + var b = ConcurrentIntIntMap.newBuilder() .withBuckets(2) .withDefaultValue(DEFAULT_VALUE) .withInitialCapacity(100) .withMode(PrimitiveMapBuilder.MapMode.BLOCKING) .withLoadFactor(0.8f); - IntIntMap map = b.build(); + ConcurrentIntIntMap map = b.build(); map.put(1, 10); int v = map.get(1); assertInstanceOf(ConcurrentIntIntMap.class, map); + assertSame(ConcurrentIntIntMap.class, map.getClass()); assertEquals(10, v); assertEquals(map.get(2), map.getDefaultValue()); } diff --git a/src/test/java/com/trivago/fastutilconcurrentwrapper/ConcurrentLongFloatMapBuilderTest.java b/src/test/java/com/trivago/fastutilconcurrentwrapper/ConcurrentLongFloatMapBuilderTest.java index f02d72d..77cfab4 100644 --- a/src/test/java/com/trivago/fastutilconcurrentwrapper/ConcurrentLongFloatMapBuilderTest.java +++ b/src/test/java/com/trivago/fastutilconcurrentwrapper/ConcurrentLongFloatMapBuilderTest.java @@ -1,7 +1,7 @@ package com.trivago.fastutilconcurrentwrapper; -import com.trivago.fastutilconcurrentwrapper.map.ConcurrentBusyWaitingLongFloatMap; -import com.trivago.fastutilconcurrentwrapper.map.ConcurrentLongFloatMap; +import com.trivago.fastutilconcurrentwrapper.longkey.ConcurrentBusyWaitingLongFloatMap; +import com.trivago.fastutilconcurrentwrapper.longkey.ConcurrentLongFloatMap; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; @@ -11,14 +11,14 @@ public class ConcurrentLongFloatMapBuilderTest { @Test public void buildsBusyWaitingMap() { - var b = LongFloatMap.newBuilder() + var b = ConcurrentLongFloatMap.newBuilder() .withBuckets(2) .withDefaultValue(DEFAULT_VALUE) .withInitialCapacity(100) .withMode(PrimitiveMapBuilder.MapMode.BUSY_WAITING) .withLoadFactor(0.8f); - LongFloatMap map = b.build(); + ConcurrentLongFloatMap map = b.build(); map.put(1L, 10.1f); float v = map.get(1L); @@ -30,19 +30,20 @@ public void buildsBusyWaitingMap() { @Test public void buildsBlockingMap() { - var b = LongFloatMap.newBuilder() + var b = ConcurrentLongFloatMap.newBuilder() .withBuckets(2) .withDefaultValue(DEFAULT_VALUE) .withInitialCapacity(100) .withMode(PrimitiveMapBuilder.MapMode.BLOCKING) .withLoadFactor(0.8f); - LongFloatMap map = b.build(); + ConcurrentLongFloatMap map = b.build(); map.put(1L, 10.1f); float v = map.get(1L); assertInstanceOf(ConcurrentLongFloatMap.class, map); + assertSame(ConcurrentLongFloatMap.class, map.getClass()); assertEquals(10.1f, v); assertEquals(map.get(2L), map.getDefaultValue()); } diff --git a/src/test/java/com/trivago/fastutilconcurrentwrapper/ConcurrentLongIntMapBuilderTest.java b/src/test/java/com/trivago/fastutilconcurrentwrapper/ConcurrentLongIntMapBuilderTest.java index 63bef79..88cdbdf 100644 --- a/src/test/java/com/trivago/fastutilconcurrentwrapper/ConcurrentLongIntMapBuilderTest.java +++ b/src/test/java/com/trivago/fastutilconcurrentwrapper/ConcurrentLongIntMapBuilderTest.java @@ -1,7 +1,7 @@ package com.trivago.fastutilconcurrentwrapper; -import com.trivago.fastutilconcurrentwrapper.map.ConcurrentBusyWaitingLongIntMap; -import com.trivago.fastutilconcurrentwrapper.map.ConcurrentLongIntMap; +import com.trivago.fastutilconcurrentwrapper.longkey.ConcurrentBusyWaitingLongIntMap; +import com.trivago.fastutilconcurrentwrapper.longkey.ConcurrentLongIntMap; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; @@ -11,14 +11,14 @@ public class ConcurrentLongIntMapBuilderTest { @Test public void buildsBusyWaitingMap() { - var b = LongIntMap.newBuilder() + var b = ConcurrentLongIntMap.newBuilder() .withBuckets(2) .withDefaultValue(DEFAULT_VALUE) .withInitialCapacity(100) .withMode(PrimitiveMapBuilder.MapMode.BUSY_WAITING) .withLoadFactor(0.9f); - LongIntMap map = b.build(); + ConcurrentLongIntMap map = b.build(); map.put(1L, 10); int v = map.get(1L); @@ -30,14 +30,14 @@ public void buildsBusyWaitingMap() { @Test public void buildsBlockingMap() { - var b = LongIntMap.newBuilder() + var b = ConcurrentLongIntMap.newBuilder() .withBuckets(2) .withDefaultValue(DEFAULT_VALUE) .withInitialCapacity(100) .withMode(PrimitiveMapBuilder.MapMode.BLOCKING) .withLoadFactor(0.9f); - LongIntMap map = b.build(); + ConcurrentLongIntMap map = b.build(); map.put(1L, 10); long v = map.get(1L); diff --git a/src/test/java/com/trivago/fastutilconcurrentwrapper/ConcurrentLongLongMapBuilderTest.java b/src/test/java/com/trivago/fastutilconcurrentwrapper/ConcurrentLongLongMapBuilderTest.java index 1de5ac8..aa50e80 100644 --- a/src/test/java/com/trivago/fastutilconcurrentwrapper/ConcurrentLongLongMapBuilderTest.java +++ b/src/test/java/com/trivago/fastutilconcurrentwrapper/ConcurrentLongLongMapBuilderTest.java @@ -1,7 +1,7 @@ package com.trivago.fastutilconcurrentwrapper; -import com.trivago.fastutilconcurrentwrapper.map.ConcurrentBusyWaitingLongLongMap; -import com.trivago.fastutilconcurrentwrapper.map.ConcurrentLongLongMap; +import com.trivago.fastutilconcurrentwrapper.longkey.ConcurrentBusyWaitingLongLongMap; +import com.trivago.fastutilconcurrentwrapper.longkey.ConcurrentLongLongMap; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; @@ -11,14 +11,14 @@ public class ConcurrentLongLongMapBuilderTest { @Test public void simpleBuilderTest() { - var b = LongLongMap.newBuilder() + var b = ConcurrentLongLongMap.newBuilder() .withBuckets(2) .withDefaultValue(DEFAULT_VALUE) .withInitialCapacity(100) .withMode(PrimitiveMapBuilder.MapMode.BUSY_WAITING) .withLoadFactor(0.9f); - LongLongMap map = b.build(); + var map = (ConcurrentBusyWaitingLongLongMap) b.build(); map.put(1L, 10L); long v = map.get(1L); @@ -30,14 +30,14 @@ public void simpleBuilderTest() { @Test public void buildsBlockingMap() { - var b = LongLongMap.newBuilder() + var b = ConcurrentLongLongMap.newBuilder() .withBuckets(2) .withDefaultValue(DEFAULT_VALUE) .withInitialCapacity(100) .withMode(PrimitiveMapBuilder.MapMode.BLOCKING) .withLoadFactor(0.9f); - LongLongMap map = b.build(); + ConcurrentLongLongMap map = b.build(); map.put(1L, 10L); long v = map.get(1L); diff --git a/src/test/java/com/trivago/fastutilconcurrentwrapper/map/ConcurrentBusyWaitingIntFloatMapTest.java b/src/test/java/com/trivago/fastutilconcurrentwrapper/intkey/ConcurrentBusyWaitingIntFloatMapTest.java similarity index 76% rename from src/test/java/com/trivago/fastutilconcurrentwrapper/map/ConcurrentBusyWaitingIntFloatMapTest.java rename to src/test/java/com/trivago/fastutilconcurrentwrapper/intkey/ConcurrentBusyWaitingIntFloatMapTest.java index 52dec90..c9e25a6 100644 --- a/src/test/java/com/trivago/fastutilconcurrentwrapper/map/ConcurrentBusyWaitingIntFloatMapTest.java +++ b/src/test/java/com/trivago/fastutilconcurrentwrapper/intkey/ConcurrentBusyWaitingIntFloatMapTest.java @@ -1,6 +1,5 @@ -package com.trivago.fastutilconcurrentwrapper.map; +package com.trivago.fastutilconcurrentwrapper.intkey; -import com.trivago.fastutilconcurrentwrapper.IntFloatMap; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; @@ -10,7 +9,7 @@ class ConcurrentBusyWaitingIntFloatMapTest { @Test void basic () { - for (var m : new IntFloatMap[]{new ConcurrentBusyWaitingIntFloatMap(10, 11, 0.5f, -1), new ConcurrentIntFloatMap(10, 11, 0.5f, -1)}){ + for (var m : new ConcurrentIntFloatMap[]{new ConcurrentBusyWaitingIntFloatMap(10, 11, 0.5f, -1), new ConcurrentIntFloatMap(10, 11, 0.5f, -1)}){ assertEquals(-1.0f, m.getDefaultValue()); assertEquals(0, m.size()); assertFalse(m.containsKey(42)); diff --git a/src/test/java/com/trivago/fastutilconcurrentwrapper/map/PrimitiveConcurrentMapTest.java b/src/test/java/com/trivago/fastutilconcurrentwrapper/intkey/PrimitiveConcurrentMapTest.java similarity index 86% rename from src/test/java/com/trivago/fastutilconcurrentwrapper/map/PrimitiveConcurrentMapTest.java rename to src/test/java/com/trivago/fastutilconcurrentwrapper/intkey/PrimitiveConcurrentMapTest.java index 70aa0e4..4d57b1c 100644 --- a/src/test/java/com/trivago/fastutilconcurrentwrapper/map/PrimitiveConcurrentMapTest.java +++ b/src/test/java/com/trivago/fastutilconcurrentwrapper/intkey/PrimitiveConcurrentMapTest.java @@ -1,5 +1,6 @@ -package com.trivago.fastutilconcurrentwrapper.map; +package com.trivago.fastutilconcurrentwrapper.intkey; +import com.trivago.fastutilconcurrentwrapper.PrimitiveConcurrentMap; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; diff --git a/src/test/java/com/trivago/fastutilconcurrentwrapper/longint/AbstractLongIntMapTest.java b/src/test/java/com/trivago/fastutilconcurrentwrapper/longint/AbstractLongIntMapTest.java index ce42f0c..8c4c8ac 100644 --- a/src/test/java/com/trivago/fastutilconcurrentwrapper/longint/AbstractLongIntMapTest.java +++ b/src/test/java/com/trivago/fastutilconcurrentwrapper/longint/AbstractLongIntMapTest.java @@ -1,23 +1,19 @@ package com.trivago.fastutilconcurrentwrapper.longint; import com.trivago.fastutilconcurrentwrapper.AbstractMapTest; -import com.trivago.fastutilconcurrentwrapper.LongIntMap; +import com.trivago.fastutilconcurrentwrapper.longkey.ConcurrentLongIntMap; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.*; abstract class AbstractLongIntMapTest extends AbstractMapTest { // Some methods return the default value of the underlying Fastutil implementation. private static final int FASTUTIL_DEFAULT_VALUE = 0; - protected int defaultValue; - private LongIntMap map; + private ConcurrentLongIntMap map; // Keep the default value to easily verify that this value is returned. - - abstract LongIntMap createMap(); + abstract ConcurrentLongIntMap createMap (); @BeforeEach void initializeMap() { @@ -206,4 +202,4 @@ protected void checkingValueIfNotPresentReturnsDefaultValue() { assertEquals(result, map.getDefaultValue()); } -} +} \ No newline at end of file diff --git a/src/test/java/com/trivago/fastutilconcurrentwrapper/longint/ConcurrentBusyWaitingLongIntMapTest.java b/src/test/java/com/trivago/fastutilconcurrentwrapper/longint/ConcurrentBusyWaitingLongIntMapTest.java index 471b17f..05161af 100644 --- a/src/test/java/com/trivago/fastutilconcurrentwrapper/longint/ConcurrentBusyWaitingLongIntMapTest.java +++ b/src/test/java/com/trivago/fastutilconcurrentwrapper/longint/ConcurrentBusyWaitingLongIntMapTest.java @@ -1,12 +1,10 @@ package com.trivago.fastutilconcurrentwrapper.longint; -import com.trivago.fastutilconcurrentwrapper.LongIntMap; -import com.trivago.fastutilconcurrentwrapper.map.ConcurrentBusyWaitingLongIntMap; +import com.trivago.fastutilconcurrentwrapper.longkey.ConcurrentBusyWaitingLongIntMap; public class ConcurrentBusyWaitingLongIntMapTest extends AbstractLongIntMapTest { - @Override - LongIntMap createMap() { + ConcurrentBusyWaitingLongIntMap createMap() { return new ConcurrentBusyWaitingLongIntMap(16, 16, 0.9F, defaultValue); } -} +} \ No newline at end of file diff --git a/src/test/java/com/trivago/fastutilconcurrentwrapper/longint/ConcurrentPrimitiveLongIntMapTest.java b/src/test/java/com/trivago/fastutilconcurrentwrapper/longint/ConcurrentPrimitiveLongIntMapTest.java index 0046d77..73a3135 100644 --- a/src/test/java/com/trivago/fastutilconcurrentwrapper/longint/ConcurrentPrimitiveLongIntMapTest.java +++ b/src/test/java/com/trivago/fastutilconcurrentwrapper/longint/ConcurrentPrimitiveLongIntMapTest.java @@ -1,13 +1,11 @@ package com.trivago.fastutilconcurrentwrapper.longint; -import com.trivago.fastutilconcurrentwrapper.LongIntMap; -import com.trivago.fastutilconcurrentwrapper.map.ConcurrentLongIntMap; +import com.trivago.fastutilconcurrentwrapper.longkey.ConcurrentLongIntMap; public class ConcurrentPrimitiveLongIntMapTest extends AbstractLongIntMapTest { @Override - LongIntMap createMap() { + ConcurrentLongIntMap createMap() { return new ConcurrentLongIntMap(16, 16, 0.9F, defaultValue); } - -} +} \ No newline at end of file diff --git a/src/test/java/com/trivago/fastutilconcurrentwrapper/longint/PrimitiveFastutilLongIntWrapperTest.java b/src/test/java/com/trivago/fastutilconcurrentwrapper/longint/PrimitiveFastutilLongIntWrapperTest.java index 3298fe8..58c9b12 100644 --- a/src/test/java/com/trivago/fastutilconcurrentwrapper/longint/PrimitiveFastutilLongIntWrapperTest.java +++ b/src/test/java/com/trivago/fastutilconcurrentwrapper/longint/PrimitiveFastutilLongIntWrapperTest.java @@ -1,12 +1,10 @@ package com.trivago.fastutilconcurrentwrapper.longint; -import com.trivago.fastutilconcurrentwrapper.LongIntMap; -import com.trivago.fastutilconcurrentwrapper.map.ConcurrentLongIntMap; +import com.trivago.fastutilconcurrentwrapper.longkey.ConcurrentLongIntMap; public class PrimitiveFastutilLongIntWrapperTest extends AbstractLongIntMapTest { - @Override - LongIntMap createMap() { + ConcurrentLongIntMap createMap() { return new ConcurrentLongIntMap(1, 5, 0.9F, defaultValue); } } \ No newline at end of file diff --git a/src/test/java/com/trivago/fastutilconcurrentwrapper/longlong/AbstractLongLongMapTest.java b/src/test/java/com/trivago/fastutilconcurrentwrapper/longlong/AbstractLongLongMapTest.java index a394bc9..3ec2ff0 100644 --- a/src/test/java/com/trivago/fastutilconcurrentwrapper/longlong/AbstractLongLongMapTest.java +++ b/src/test/java/com/trivago/fastutilconcurrentwrapper/longlong/AbstractLongLongMapTest.java @@ -1,22 +1,20 @@ package com.trivago.fastutilconcurrentwrapper.longlong; import com.trivago.fastutilconcurrentwrapper.AbstractMapTest; -import com.trivago.fastutilconcurrentwrapper.LongLongMap; +import com.trivago.fastutilconcurrentwrapper.longkey.ConcurrentLongLongMap; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.*; abstract class AbstractLongLongMapTest extends AbstractMapTest { - private LongLongMap map; + private ConcurrentLongLongMap map; // Keep the default value to easily verify that this value is returned. protected long defaultValue; // Some methods return the default value of the underlying Fastutil implementation. private static final long FASTUTIL_DEFAULT_VALUE = 0L; - abstract LongLongMap createMap(); + abstract ConcurrentLongLongMap createMap (); @BeforeEach void initializeMap() { @@ -24,7 +22,7 @@ void initializeMap() { map = createMap(); } - @Test + @Test @Override protected void containsKeyReturnsFalseIfMapIsEmpty() { final long key = nextLong(); @@ -33,7 +31,7 @@ protected void containsKeyReturnsFalseIfMapIsEmpty() { assertFalse(contains); } - @Test + @Test @Override protected void containsKeyReturnsTrueIfKeyExists() { long key = nextLong(); long value = nextLong(); @@ -44,7 +42,7 @@ protected void containsKeyReturnsTrueIfKeyExists() { assertTrue(contains); } - @Test + @Test @Override protected void containsKeyReturnsFalseIfKeyWasRemoved() { long key = nextLong(); long value = nextLong(); @@ -56,14 +54,14 @@ protected void containsKeyReturnsFalseIfKeyWasRemoved() { assertFalse(contains); } - @Test + @Test @Override protected void mapIsEmptyWhenNothingWasInserted() { final boolean empty = map.isEmpty(); assertTrue(empty); } - @Test + @Test @Override protected void mapIsEmptyWhenAllKeysAreDeleted() { int entryCount = (Math.abs(nextInt()) % 100) + 1; long value = nextLong(); @@ -80,7 +78,7 @@ protected void mapIsEmptyWhenAllKeysAreDeleted() { assertTrue(empty); } - @Test + @Test @Override protected void sizeIsCorrect() { int entries = (Math.abs(nextInt()) % 50) + 1; long value = nextLong(); @@ -94,7 +92,7 @@ protected void sizeIsCorrect() { assertEquals(entries, size); } - @Test + @Test @Override protected void gettingExistingValueReturnsCorrectValue() { long key = nextLong(); long value = nextLong(); @@ -104,7 +102,7 @@ protected void gettingExistingValueReturnsCorrectValue() { assertEquals(value, returnedValue); } - @Test + @Test @Override protected void gettingNonExistingValueReturnsCorrectValue() { long key = nextLong(); final long returnedValue = map.get(key); @@ -112,7 +110,7 @@ protected void gettingNonExistingValueReturnsCorrectValue() { assertEquals(defaultValue, returnedValue); } - @Test + @Test @Override protected void removingNonExistingKeyReturnsDefaultValue() { long key = nextLong(); final long removedValue = map.remove(key); @@ -120,7 +118,7 @@ protected void removingNonExistingKeyReturnsDefaultValue() { assertEquals(FASTUTIL_DEFAULT_VALUE, removedValue); } - @Test + @Test @Override protected void removingExistingKeyReturnsPreviousValue() { long key = nextLong(); long value = nextLong(); @@ -130,7 +128,7 @@ protected void removingExistingKeyReturnsPreviousValue() { assertEquals(value, removedValue); } - @Test + @Test @Override protected void removingWithValueWhenKeyDoesNotExistReturnsFalse() { long key = nextLong(); long value = nextLong(); @@ -139,7 +137,7 @@ protected void removingWithValueWhenKeyDoesNotExistReturnsFalse() { assertFalse(result); } - @Test + @Test @Override protected void removingWithValueWhenValueIsDifferentReturnsFalse() { long key = nextLong(); long value = nextLong(); @@ -149,7 +147,7 @@ protected void removingWithValueWhenValueIsDifferentReturnsFalse() { assertFalse(result); } - @Test + @Test @Override protected void removingWithValueWhenValueIsSameReturnsTrue() { long key = nextLong(); long value = nextLong(); @@ -159,7 +157,7 @@ protected void removingWithValueWhenValueIsSameReturnsTrue() { assertTrue(result); } - @Test + @Test @Override protected void puttingValueIfAbsentReturnsSameValue() { long key = nextLong(); long value = nextLong(); @@ -170,7 +168,7 @@ protected void puttingValueIfAbsentReturnsSameValue() { assertEquals(result, value); } - @Test + @Test @Override protected void checkingValueIfNotAbsentReturnsSameValue() { long key = nextLong(); long value = nextLong(); @@ -183,7 +181,7 @@ protected void checkingValueIfNotAbsentReturnsSameValue() { assertEquals(value, returned); } - @Test + @Test @Override protected void replacingValueIfPresentReturnsNewValue() { long key = nextLong(); long value = nextLong(); @@ -196,7 +194,7 @@ protected void replacingValueIfPresentReturnsNewValue() { assertEquals(result, key + value); } - @Test + @Test @Override protected void checkingValueIfNotPresentReturnsDefaultValue() { long key = nextLong(); map.computeIfPresent(key, Long::sum); @@ -205,4 +203,4 @@ protected void checkingValueIfNotPresentReturnsDefaultValue() { assertEquals(result, map.getDefaultValue()); } -} +} \ No newline at end of file diff --git a/src/test/java/com/trivago/fastutilconcurrentwrapper/longlong/ConcurrentBusyWaitingLongLongMapTest.java b/src/test/java/com/trivago/fastutilconcurrentwrapper/longlong/ConcurrentBusyWaitingLongLongMapTest.java index 87c55f3..038cde0 100644 --- a/src/test/java/com/trivago/fastutilconcurrentwrapper/longlong/ConcurrentBusyWaitingLongLongMapTest.java +++ b/src/test/java/com/trivago/fastutilconcurrentwrapper/longlong/ConcurrentBusyWaitingLongLongMapTest.java @@ -1,12 +1,11 @@ package com.trivago.fastutilconcurrentwrapper.longlong; -import com.trivago.fastutilconcurrentwrapper.LongLongMap; -import com.trivago.fastutilconcurrentwrapper.map.ConcurrentBusyWaitingLongLongMap; +import com.trivago.fastutilconcurrentwrapper.longkey.ConcurrentBusyWaitingLongLongMap; public class ConcurrentBusyWaitingLongLongMapTest extends AbstractLongLongMapTest { @Override - LongLongMap createMap() { + ConcurrentBusyWaitingLongLongMap createMap() { return new ConcurrentBusyWaitingLongLongMap(16, 16, 0.9F, defaultValue); } -} +} \ No newline at end of file diff --git a/src/test/java/com/trivago/fastutilconcurrentwrapper/longlong/ConcurrentLongLongMapTest.java b/src/test/java/com/trivago/fastutilconcurrentwrapper/longlong/ConcurrentLongLongMapTest.java index 32830be..3e0559b 100644 --- a/src/test/java/com/trivago/fastutilconcurrentwrapper/longlong/ConcurrentLongLongMapTest.java +++ b/src/test/java/com/trivago/fastutilconcurrentwrapper/longlong/ConcurrentLongLongMapTest.java @@ -1,12 +1,11 @@ package com.trivago.fastutilconcurrentwrapper.longlong; -import com.trivago.fastutilconcurrentwrapper.LongLongMap; -import com.trivago.fastutilconcurrentwrapper.map.ConcurrentLongLongMap; +import com.trivago.fastutilconcurrentwrapper.longkey.ConcurrentLongLongMap; public class ConcurrentLongLongMapTest extends AbstractLongLongMapTest { @Override - LongLongMap createMap() { + ConcurrentLongLongMap createMap() { return new ConcurrentLongLongMap(16, 16, 0.9F, defaultValue); } -} +} \ No newline at end of file diff --git a/src/test/java/com/trivago/fastutilconcurrentwrapper/longlong/PrimitiveFastutilLongLongWrapperTest.java b/src/test/java/com/trivago/fastutilconcurrentwrapper/longlong/PrimitiveFastutilLongLongWrapperTest.java index 78f81de..b4efeb9 100644 --- a/src/test/java/com/trivago/fastutilconcurrentwrapper/longlong/PrimitiveFastutilLongLongWrapperTest.java +++ b/src/test/java/com/trivago/fastutilconcurrentwrapper/longlong/PrimitiveFastutilLongLongWrapperTest.java @@ -1,12 +1,10 @@ package com.trivago.fastutilconcurrentwrapper.longlong; -import com.trivago.fastutilconcurrentwrapper.LongLongMap; -import com.trivago.fastutilconcurrentwrapper.map.ConcurrentLongLongMap; +import com.trivago.fastutilconcurrentwrapper.longkey.ConcurrentLongLongMap; public class PrimitiveFastutilLongLongWrapperTest extends AbstractLongLongMapTest { - @Override - LongLongMap createMap() { + ConcurrentLongLongMap createMap() { return new ConcurrentLongLongMap(1, 5, 0.9F, defaultValue); } } \ No newline at end of file diff --git a/src/test/java/com/trivago/fastutilconcurrentwrapper/objkey/ConcurrentObjectLongMapBuilderTest.java b/src/test/java/com/trivago/fastutilconcurrentwrapper/objkey/ConcurrentObjectLongMapBuilderTest.java new file mode 100644 index 0000000..0f1e8a7 --- /dev/null +++ b/src/test/java/com/trivago/fastutilconcurrentwrapper/objkey/ConcurrentObjectLongMapBuilderTest.java @@ -0,0 +1,48 @@ +package com.trivago.fastutilconcurrentwrapper.objkey; + +import com.trivago.fastutilconcurrentwrapper.PrimitiveMapBuilder; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +public class ConcurrentObjectLongMapBuilderTest { + private static final long DEFAULT_VALUE = -1L; + + @Test + public void simpleBuilderTest() { + var b = ConcurrentObjectLongMap.newBuilder() + .withBuckets(2) + .withDefaultValue(DEFAULT_VALUE) + .withInitialCapacity(100) + .withMode(PrimitiveMapBuilder.MapMode.BUSY_WAITING) + .withLoadFactor(0.9f); + + ConcurrentObjectLongMap map = b.build(); + + map.put((short) 17, 10L); + long v = map.get((short) 17); + + assertInstanceOf(ConcurrentBusyWaitingObjectLongMap.class, map); + assertEquals(10L, v); + assertEquals(map.get((short) 99), map.getDefaultValue()); + } + + @Test + public void buildsBlockingMap() { + var b = ConcurrentObjectLongMap.newBuilder() + .withBuckets(2) + .withDefaultValue(DEFAULT_VALUE) + .withInitialCapacity(100) + .withMode(PrimitiveMapBuilder.MapMode.BLOCKING) + .withLoadFactor(0.9f); + + ConcurrentObjectLongMap map = b.build(); + + map.put((short)41, 10L); + long v = map.get((short)41); + + assertInstanceOf(ConcurrentObjectLongMap.class, map); + assertEquals(10L, v); + assertEquals(map.get((short)47), map.getDefaultValue()); + } +} \ No newline at end of file