HCOLL-119 Add Lamda support to SHM

This commit is contained in:
Rob Austin
2014-07-24 12:57:56 +01:00
parent fd91368197
commit ca899a90e2
12 changed files with 10215 additions and 93 deletions
+1 -4
View File
@@ -1,13 +1,10 @@
*.class
# Mobile Tools for Java (J2ME)
.mtj.tmp/
#.mtj.tmp/
# Package Files #
*.jar
*.war
*.ear
*.iml
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
@@ -18,6 +18,7 @@
package net.openhft.chronicle.map;
import net.openhft.collections.ConcurrentMapLamdadSupport;
import net.openhft.collections.SharedHashMap;
import java.util.Map;
@@ -29,7 +30,7 @@ import java.util.function.Function;
/**
* @author Rob Austin.
*/
public interface ChronicleMap<K, V> extends SharedHashMap<K, V> { // }, ConcurrentHashMap3 {
public interface ChronicleMap<K, V> extends SharedHashMap<K, V>, ConcurrentMapLamdadSupport<K, V> {
@@ -95,8 +95,8 @@ public class ChronicleMapBuilder0<K, V> extends SharedHashMapBuilder<K, V> imple
if (identifier <= 0)
throw new IllegalArgumentException("Identifier must be positive, " + identifier + " given");
final ChronicleReplicatedHashMap<K, V> result =
new ChronicleReplicatedHashMap<K, V>(builder, kClass, vClass);
final ChronicleReplicatedMap<K, V> result =
new ChronicleReplicatedMap<K, V>(builder, kClass, vClass);
if (externalReplicatorBuilder != null)
externalReplicator = applyExternalReplicator(result, externalReplicatorBuilder, kClass, vClass);
@@ -26,9 +26,9 @@ import java.io.IOException;
/**
* @author Rob Austin.
*/
public class ChronicleReplicatedHashMap<K, V> extends VanillaSharedReplicatedHashMap<K, V> implements ChronicleMap<K, V> {
public class ChronicleReplicatedMap<K, V> extends VanillaSharedReplicatedMap<K, V> implements ChronicleMap<K, V> {
public ChronicleReplicatedHashMap(@NotNull SharedHashMapBuilder builder, @NotNull Class aClass, @NotNull Class aClass2) throws IOException {
public ChronicleReplicatedMap(@NotNull SharedHashMapBuilder builder, @NotNull Class aClass, @NotNull Class aClass2) throws IOException {
super(builder, aClass, aClass2);
}
File diff suppressed because it is too large Load Diff
@@ -19,18 +19,35 @@
package net.openhft.collections;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.*;
/**
* @author Rob Austin.
*/
public interface ConcurrentHashMap3<K, V> {
public interface ConcurrentMapLamdadSupport<K, V> {
/**
* Returns a {@link java.util.Set} view of the keys contained in this map. The set is backed by the map,
* so changes to the map are reflected in the set, and vice-versa. The set supports element removal, which
* removes the corresponding mapping from this map, via the {@code Iterator.remove}, {@code Set.remove},
* {@code removeAll}, {@code retainAll}, and {@code clear} operations. It does not support the {@code
* add} or {@code addAll} operations.
*
* <p>The view's iterators and spliterators are <a href="package-summary.html#Weakly"><i>weakly
* consistent</i></a>.
*
* <p>The view's {@code spliterator} reports {@link java.util.Spliterator#CONCURRENT}, {@link
* java.util.Spliterator#DISTINCT}, and {@link java.util.Spliterator#NONNULL}.
*
* @return the set view
*/
public net.openhft.collections.ConcurrentHashMap.KeySetView<K, V> keySet();
long mappingCount();
ConcurrentHashMap.KeySetView<K, V> keySet(V mappedValue);
net.openhft.collections.ConcurrentHashMap.KeySetView<K, V> keySet(V mappedValue);
void forEach(long parallelismThreshold,
BiConsumer<? super K, ? super V> action);
@@ -61,6 +78,7 @@ public interface ConcurrentHashMap3<K, V> {
int basis,
IntBinaryOperator reducer);
void forEachKey(long parallelismThreshold,
Consumer<? super K> action);
@@ -0,0 +1,57 @@
/*
* Copyright 2014 Higher Frequency Trading
* <p/>
* http://www.higherfrequencytrading.com
* <p/>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.openhft.collections;
import java.io.Closeable;
import java.io.File;
import java.util.concurrent.ConcurrentMap;
public interface SharedHashMap<K, V> extends ConcurrentMap<K, V>, Closeable {
/**
* @return size as a long.
* @see java.util.Map size()
*/
long longSize();
/**
* Get a value for a key if available. If the value is Byteable, it will be assigned to
* reference the value, instead of copying the data for zero copy access to the collection.
*
* @param key to lookup.
* @param value to reuse if possible. If null, a new object will be created.
* @return value found or null if not.
*/
V getUsing(K key, V value);
/**
* Acquire a value for a key, creating if absent. If the value is Byteable, it will be assigned
* to reference the value, instead of copying the data.
*
* @param key to lookup.
* @param value to reuse if possible. If null, a new object will be created.
* @return value created or found.
*/
V acquireUsing(K key, V value);
/**
* @return The file or directory for this SharedHashMap
*/
File file();
}
@@ -0,0 +1,762 @@
/*
* Copyright 2014 Higher Frequency Trading
* <p/>
* http://www.higherfrequencytrading.com
* <p/>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.openhft.collections;
import net.openhft.chronicle.map.ChronicleMap;
import net.openhft.lang.Maths;
import net.openhft.lang.io.serialization.BytesMarshallableSerializer;
import net.openhft.lang.io.serialization.BytesMarshallerFactory;
import net.openhft.lang.io.serialization.JDKObjectSerializer;
import net.openhft.lang.io.serialization.ObjectSerializer;
import net.openhft.lang.io.serialization.impl.VanillaBytesMarshallerFactory;
import net.openhft.lang.model.constraints.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.net.InetAddress;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Arrays;
public class SharedHashMapBuilder<K, V> implements Cloneable {
private static final Logger LOG = LoggerFactory.getLogger(net.openhft.collections.SharedHashMapBuilder.class.getName());
static final int HEADER_SIZE = 128;
static final int SEGMENT_HEADER = 64;
private static final byte[] MAGIC = "SharedHM".getBytes();
public static final short UDP_REPLICATION_MODIFICATION_ITERATOR_ID = 128;
public static final short JDBC_REPLICATION_MODIFICATION_ITERATOR_ID = 129;
public static final short FILE_REPLICATION_MODIFICATION_ITERATOR_ID = 130;
// used when configuring the number of segments.
private int minSegments = -1;
private int actualSegments = -1;
// used when reading the number of entries per
private int actualEntriesPerSegment = -1;
private int entrySize = 256;
private Alignment alignment = Alignment.OF_4_BYTES;
private long entries = 1 << 20;
private int replicas = 0;
boolean transactional = false;
private long lockTimeOutMS = 20000;
private int metaDataBytes = 0;
private SharedMapEventListener eventListener = SharedMapEventListeners.nop();
private SharedMapErrorListener errorListener = SharedMapErrorListeners.logging();
private boolean putReturnsNull = false;
private boolean removeReturnsNull = false;
private boolean generatedKeyType = false;
private boolean generatedValueType = false;
private boolean largeSegments = false;
// replication
private boolean canReplicate;
byte identifier = Byte.MIN_VALUE;
TcpReplicatorBuilder tcpReplicatorBuilder;
ExternalReplicatorBuilder externalReplicatorBuilder;
private TimeProvider timeProvider = TimeProvider.SYSTEM;
UdpReplicatorBuilder udpReplicatorBuilder;
private BytesMarshallerFactory bytesMarshallerFactory;
private ObjectSerializer objectSerializer;
ExternalReplicator externalReplicator;
File file;
Class kClass;
Class vClass;
public SharedHashMapBuilder() {
}
public SharedHashMapBuilder(Class<K> kClass, Class<V> vClass) {
this.kClass = kClass;
this.vClass = vClass;
}
public static <K, V> SharedHashMapBuilder<K, V> of(Class<K> kClass, Class<V> vClass) {
return new SharedHashMapBuilder<K, V>(kClass, vClass);
}
@Override
public SharedHashMapBuilder<K, V> clone() {
try {
@SuppressWarnings("unchecked")
final SharedHashMapBuilder<K, V> result = (SharedHashMapBuilder) super.clone();
if (tcpReplicatorBuilder() != null)
result.tcpReplicatorBuilder(tcpReplicatorBuilder().clone());
if (udpReplicatorBuilder() != null)
result.udpReplicatorBuilder(udpReplicatorBuilder().clone());
return result;
} catch (CloneNotSupportedException e) {
throw new AssertionError(e);
}
}
/**
* Set minimum number of segments. See concurrencyLevel in {@link java.util.concurrent.ConcurrentHashMap}.
*
* @return this builder object back
*/
public SharedHashMapBuilder minSegments(int minSegments) {
this.minSegments = minSegments;
return this;
}
public int minSegments() {
return minSegments < 1 ? tryMinSegments(4, 65536) : minSegments;
}
private int tryMinSegments(int min, int max) {
for (int i = min; i < max; i <<= 1) {
if (i * i * i >= alignedEntrySize() * 2)
return i;
}
return max;
}
/**
* <p>Note that the actual entrySize will be aligned to 4 (default entry alignment). I. e. if you set
* entry size to 30, the actual entry size will be 32 (30 aligned to 4 bytes). If you don't want entry
* size to be aligned, set {@code entryAndValueAlignment(Alignment.NO_ALIGNMENT)}.
*
* @param entrySize the size in bytes
* @return this {@code SharedHashMapBuilder} back
* @see #entryAndValueAlignment(Alignment)
* @see #entryAndValueAlignment()
*/
public SharedHashMapBuilder entrySize(int entrySize) {
this.entrySize = entrySize;
return this;
}
public int entrySize() {
return entrySize;
}
int alignedEntrySize() {
return entryAndValueAlignment().alignSize(entrySize());
}
/**
* Specifies alignment of address in memory of entries and independently of address in memory of values
* within entries. <p/> <p>Useful when values of the map are updated intensively, particularly fields with
* volatile access, because it doesn't work well if the value crosses cache lines. Also, on some (nowadays
* rare) architectures any misaligned memory access is more expensive than aligned. <p/> <p>Note that
* specified {@link #entrySize()} will be aligned according to this alignment. I. e. if you set {@code
* entrySize(20)} and {@link net.openhft.collections.Alignment#OF_8_BYTES}, actual entry size will be 24
* (20 aligned to 8 bytes).
*
* @return this {@code SharedHashMapBuilder} back
* @see #entryAndValueAlignment()
*/
public SharedHashMapBuilder<K, V> entryAndValueAlignment(Alignment alignment) {
this.alignment = alignment;
return this;
}
/**
* Returns alignment of addresses in memory of entries and independently of values within entries. <p/>
* <p>Default is {@link net.openhft.collections.Alignment#OF_4_BYTES}.
*
* @see #entryAndValueAlignment(Alignment)
*/
public Alignment entryAndValueAlignment() {
return alignment;
}
public SharedHashMapBuilder<K, V> entries(long entries) {
this.entries = entries;
return this;
}
public long entries() {
return entries;
}
public SharedHashMapBuilder<K, V> replicas(int replicas) {
this.replicas = replicas;
return this;
}
public int replicas() {
return replicas;
}
public SharedHashMapBuilder<K, V> actualEntriesPerSegment(int actualEntriesPerSegment) {
this.actualEntriesPerSegment = actualEntriesPerSegment;
return this;
}
public int actualEntriesPerSegment() {
if (actualEntriesPerSegment > 0)
return actualEntriesPerSegment;
int as = actualSegments();
// round up to the next multiple of 64.
return (int) (Math.max(1, entries * 2L / as) + 63) & ~63;
}
public SharedHashMapBuilder<K, V> actualSegments(int actualSegments) {
this.actualSegments = actualSegments;
return this;
}
public int actualSegments() {
if (actualSegments > 0)
return actualSegments;
if (!largeSegments && entries > (long) minSegments() << 15) {
long segments = Maths.nextPower2(entries >> 15, 128);
if (segments < 1 << 20)
return (int) segments;
}
// try to keep it 16-bit sizes segments
return (int) Maths.nextPower2(Math.max((entries >> 30) + 1, minSegments()), 1);
}
/**
* Not supported yet.
*
* @param transactional if the built map should be transactional
* @return this {@code SharedHashMapBuilder} back
*/
public SharedHashMapBuilder<K, V> transactional(boolean transactional) {
this.transactional = transactional;
return this;
}
public boolean transactional() {
return transactional;
}
public SharedHashMapBuilder kClass(Class kClass) {
this.kClass = kClass;
return this;
}
public SharedHashMapBuilder vClass(Class vClass) {
this.vClass = vClass;
return this;
}
public SharedHashMapBuilder<K, V> file(File file) {
this.file = file;
return this;
}
public File file() {
return this.file;
}
public <K> Class<K> kClass() {
return this.kClass;
}
public <V> Class<V> vClass() {
return this.vClass;
}
public ChronicleMap<K, V> create() throws IOException {
if (kClass == null)
throw new IllegalArgumentException("missing mandatory parameter kClass");
if (vClass == null)
throw new IllegalArgumentException("missing mandatory parameter vClass");
if (file == null)
throw new IllegalArgumentException("missing mandatory parameter file");
SharedHashMapBuilder<K, V> builder = toBuilder();
if (!canReplicate())
return new VanillaSharedMap<K, V>(builder, file, kClass, vClass);
if (identifier <= 0)
throw new IllegalArgumentException("Identifier must be positive, " + identifier + " given");
final VanillaSharedReplicatedMap<K, V> result =
new VanillaSharedReplicatedMap<K, V>(builder, kClass, vClass);
if (externalReplicatorBuilder != null)
externalReplicator = applyExternalReplicator(result, externalReplicatorBuilder, kClass, vClass);
if (tcpReplicatorBuilder != null)
applyTcpReplication(result, tcpReplicatorBuilder);
if (udpReplicatorBuilder != null) {
if (tcpReplicatorBuilder == null)
LOG.warn("MISSING TCP REPLICATION : The UdpReplicator only attempts to read data (" +
"it does not enforce or guarantee delivery), you should use the UdpReplicator if " +
"you have a large number of nodes, and you wish to receive the data before it " +
"becomes available on TCP/IP. Since data delivery is not guaranteed, it is " +
"recommended that you only use the UDP" +
" " +
"Replicator " +
"in conjunction with a TCP Replicator");
applyUdpReplication(result, udpReplicatorBuilder);
}
return result;
}
/**
* Its recommended that you use net.openhft.collections.SharedHashMapBuilder#create() instead as this
* method will be shortly removed
*/
@Deprecated
public <K, V> ChronicleMap<K, V> create(File file, Class<K> kClass, Class<V> vClass) throws IOException {
return file(file).kClass(kClass).vClass(vClass).create();
}
SharedHashMapBuilder<K, V> toBuilder() throws IOException {
SharedHashMapBuilder builder = clone();
for (int i = 0; i < 10; i++) {
if (file.exists() && file.length() > 0) {
readFile(file, builder);
break;
}
if (file.createNewFile() || file.length() == 0) {
newFile(file);
break;
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
throw new IOException(e);
}
}
if (builder == null || !file.exists())
throw new FileNotFoundException("Unable to create " + file);
return builder;
}
static void readFile(File file, SharedHashMapBuilder builder) throws IOException {
ByteBuffer bb = ByteBuffer.allocateDirect(HEADER_SIZE).order(ByteOrder.nativeOrder());
FileInputStream fis = new FileInputStream(file);
fis.getChannel().read(bb);
fis.close();
bb.flip();
if (bb.remaining() < 22) throw new IOException("File too small, corrupted? " + file);
byte[] bytes = new byte[8];
bb.get(bytes);
if (!Arrays.equals(bytes, MAGIC))
throw new IOException("Unknown magic number, was " + new String(bytes, "ISO-8859-1"));
builder.actualSegments(bb.getInt());
builder.actualEntriesPerSegment(bb.getInt());
builder.entrySize(bb.getInt());
builder.entryAndValueAlignment(Alignment.fromOrdinal(bb.get()));
builder.replicas(bb.getInt());
builder.transactional(bb.get() == 'Y');
builder.metaDataBytes(bb.get() & 0xFF);
if (builder.actualSegments() <= 0 || builder.actualEntriesPerSegment() <= 0 || builder.entrySize() <= 0)
throw new IOException("Corrupt header for " + file);
}
void newFile(File file) throws IOException {
ByteBuffer bb = ByteBuffer.allocateDirect(HEADER_SIZE).order(ByteOrder.nativeOrder());
bb.put(MAGIC);
bb.putInt(actualSegments());
bb.putInt(actualEntriesPerSegment());
bb.putInt(entrySize());
bb.put((byte) entryAndValueAlignment().ordinal());
bb.putInt(replicas());
bb.put((byte) (transactional ? 'Y' : 'N'));
bb.put((byte) metaDataBytes);
bb.flip();
FileOutputStream fos = new FileOutputStream(file);
fos.getChannel().write(bb);
fos.close();
}
public SharedHashMapBuilder lockTimeOutMS(long lockTimeOutMS) {
this.lockTimeOutMS = lockTimeOutMS;
return this;
}
public long lockTimeOutMS() {
return lockTimeOutMS;
}
public SharedHashMapBuilder errorListener(SharedMapErrorListener errorListener) {
this.errorListener = errorListener;
return this;
}
public SharedMapErrorListener errorListener() {
return errorListener;
}
/**
* Map.put() returns the previous value, functionality which is rarely used but fairly cheap for HashMap.
* In the case, for an off heap collection, it has to create a new object (or return a recycled one)
* Either way it's expensive for something you probably don't use.
*
* @param putReturnsNull false if you want SharedHashMap.put() to not return the object that was replaced
* but instead return null
* @return an instance of the map builder
*/
public SharedHashMapBuilder putReturnsNull(boolean putReturnsNull) {
this.putReturnsNull = putReturnsNull;
return this;
}
/**
* Map.put() returns the previous value, functionality which is rarely used but fairly cheap for HashMap.
* In the case, for an off heap collection, it has to create a new object (or return a recycled one)
* Either way it's expensive for something you probably don't use.
*
* @return true if SharedHashMap.put() is not going to return the object that was replaced but instead
* return null
*/
public boolean putReturnsNull() {
return putReturnsNull;
}
/**
* Map.remove() returns the previous value, functionality which is rarely used but fairly cheap for
* HashMap. In the case, for an off heap collection, it has to create a new object (or return a recycled
* one) Either way it's expensive for something you probably don't use.
*
* @param removeReturnsNull false if you want SharedHashMap.remove() to not return the object that was
* removed but instead return null
* @return an instance of the map builder
*/
public SharedHashMapBuilder removeReturnsNull(boolean removeReturnsNull) {
this.removeReturnsNull = removeReturnsNull;
return this;
}
/**
* Map.remove() returns the previous value, functionality which is rarely used but fairly cheap for
* HashMap. In the case, for an off heap collection, it has to create a new object (or return a recycled
* one) Either way it's expensive for something you probably don't use.
*
* @return true if SharedHashMap.remove() is not going to return the object that was removed but instead
* return null
*/
public boolean removeReturnsNull() {
return removeReturnsNull;
}
public boolean generatedKeyType() {
return generatedKeyType;
}
public SharedHashMapBuilder generatedKeyType(boolean generatedKeyType) {
this.generatedKeyType = generatedKeyType;
return this;
}
public boolean generatedValueType() {
return generatedValueType;
}
public SharedHashMapBuilder generatedValueType(boolean generatedValueType) {
this.generatedValueType = generatedValueType;
return this;
}
public boolean largeSegments() {
return entries > 1L << (20 + 15) || largeSegments;
}
public SharedHashMapBuilder largeSegments(boolean largeSegments) {
this.largeSegments = largeSegments;
return this;
}
public SharedHashMapBuilder metaDataBytes(int metaDataBytes) {
if ((metaDataBytes & 0xFF) != metaDataBytes)
throw new IllegalArgumentException("MetaDataBytes must be [0..255] was " + metaDataBytes);
this.metaDataBytes = metaDataBytes;
return this;
}
public int metaDataBytes() {
return metaDataBytes;
}
public SharedHashMapBuilder eventListener(SharedMapEventListener eventListener) {
this.eventListener = eventListener;
return this;
}
public SharedMapEventListener eventListener() {
return eventListener;
}
@Override
public String toString() {
return "SharedHashMapBuilder{" +
"actualSegments=" + actualSegments() +
", minSegments=" + minSegments() +
", actualEntriesPerSegment=" + actualEntriesPerSegment() +
", entrySize=" + entrySize() +
", entryAndValueAlignment=" + entryAndValueAlignment() +
", entries=" + entries() +
", replicas=" + replicas() +
", transactional=" + transactional() +
", lockTimeOutMS=" + lockTimeOutMS() +
", metaDataBytes=" + metaDataBytes() +
", eventListener=" + eventListener() +
", errorListener=" + errorListener() +
", putReturnsNull=" + putReturnsNull() +
", removeReturnsNull=" + removeReturnsNull() +
", generatedKeyType=" + generatedKeyType() +
", generatedValueType=" + generatedValueType() +
", largeSegments=" + largeSegments() +
", canReplicate=" + canReplicate() +
", identifier=" + identifierToString() +
", tcpReplicatorBuilder=" + tcpReplicatorBuilder() +
", udpReplicatorBuilder=" + udpReplicatorBuilder() +
", timeProvider=" + timeProvider() +
", bytesMarshallerfactory=" + bytesMarshallerFactory() +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
SharedHashMapBuilder that = (SharedHashMapBuilder) o;
if (actualEntriesPerSegment != that.actualEntriesPerSegment) return false;
if (actualSegments != that.actualSegments) return false;
if (canReplicate != that.canReplicate) return false;
if (entries != that.entries) return false;
if (entrySize != that.entrySize) return false;
if (generatedKeyType != that.generatedKeyType) return false;
if (generatedValueType != that.generatedValueType) return false;
if (identifier != that.identifier) return false;
if (largeSegments != that.largeSegments) return false;
if (lockTimeOutMS != that.lockTimeOutMS) return false;
if (metaDataBytes != that.metaDataBytes) return false;
if (minSegments != that.minSegments) return false;
if (putReturnsNull != that.putReturnsNull) return false;
if (removeReturnsNull != that.removeReturnsNull) return false;
if (replicas != that.replicas) return false;
if (transactional != that.transactional) return false;
if (alignment != that.alignment) return false;
if (errorListener != null ? !errorListener.equals(that.errorListener) : that.errorListener != null)
return false;
if (eventListener != null ? !eventListener.equals(that.eventListener) : that.eventListener != null)
return false;
if (tcpReplicatorBuilder != null ? !tcpReplicatorBuilder.equals(that.tcpReplicatorBuilder) : that.tcpReplicatorBuilder != null)
return false;
if (timeProvider != null ? !timeProvider.equals(that.timeProvider) : that.timeProvider != null)
return false;
if (udpReplicatorBuilder != null ? !udpReplicatorBuilder.equals(that.udpReplicatorBuilder) : that.udpReplicatorBuilder != null)
return false;
return true;
}
@Override
public int hashCode() {
int result = minSegments;
result = 31 * result + actualSegments;
result = 31 * result + actualEntriesPerSegment;
result = 31 * result + entrySize;
result = 31 * result + (alignment != null ? alignment.hashCode() : 0);
result = 31 * result + (int) (entries ^ (entries >>> 32));
result = 31 * result + replicas;
result = 31 * result + (transactional ? 1 : 0);
result = 31 * result + (int) (lockTimeOutMS ^ (lockTimeOutMS >>> 32));
result = 31 * result + metaDataBytes;
result = 31 * result + (eventListener != null ? eventListener.hashCode() : 0);
result = 31 * result + (errorListener != null ? errorListener.hashCode() : 0);
result = 31 * result + (putReturnsNull ? 1 : 0);
result = 31 * result + (removeReturnsNull ? 1 : 0);
result = 31 * result + (generatedKeyType ? 1 : 0);
result = 31 * result + (generatedValueType ? 1 : 0);
result = 31 * result + (largeSegments ? 1 : 0);
result = 31 * result + (canReplicate ? 1 : 0);
result = 31 * result + (int) identifier;
result = 31 * result + (tcpReplicatorBuilder != null ? tcpReplicatorBuilder.hashCode() : 0);
result = 31 * result + (timeProvider != null ? timeProvider.hashCode() : 0);
result = 31 * result + (udpReplicatorBuilder != null ? udpReplicatorBuilder.hashCode() : 0);
return result;
}
public boolean canReplicate() {
return canReplicate || tcpReplicatorBuilder != null || udpReplicatorBuilder != null;
}
public SharedHashMapBuilder canReplicate(boolean canReplicate) {
this.canReplicate = canReplicate;
return this;
}
<K, V> ExternalReplicator.AbstractExternalReplicator applyExternalReplicator
(VanillaSharedReplicatedMap<K, V> map,
ExternalReplicatorBuilder builder,
Class<K> kClass,
Class<V> vClass) {
try {
final ExternalReplicator.AbstractExternalReplicator externalReplicator;
final short id;
if (builder instanceof ExternalJDBCReplicatorBuilder) {
externalReplicator = new ExternalJDBCReplicator<K, V>(kClass, vClass,
(ExternalJDBCReplicatorBuilder) builder, map);
id = JDBC_REPLICATION_MODIFICATION_ITERATOR_ID;
} else if (builder instanceof ExternalFileReplicatorBuilder) {
externalReplicator = new ExternalFileReplicator<K, V>(kClass, vClass,
(ExternalFileReplicatorBuilder) builder, map);
id = FILE_REPLICATION_MODIFICATION_ITERATOR_ID;
} else
throw new IllegalStateException("builder of type " + builder.getClass() + " is not supported.");
final Replica.ModificationIterator modIterator
= map.acquireModificationIterator(id, externalReplicator);
externalReplicator.setModificationIterator(modIterator);
map.addCloseable(externalReplicator);
return externalReplicator;
} catch (Exception e) {
LOG.error("", e);
}
return null;
}
<K, V> void applyUdpReplication(VanillaSharedReplicatedMap<K, V> result,
UdpReplicatorBuilder udpReplicatorBuilder)
throws IOException {
final InetAddress address = udpReplicatorBuilder.address();
if (address == null) {
throw new IllegalArgumentException("address can not be null");
}
if (address.isMulticastAddress() && udpReplicatorBuilder.networkInterface() == null) {
throw new IllegalArgumentException("MISSING: NetworkInterface, " +
"When using a multicast addresses, please provided a " +
"networkInterface");
}
final UdpReplicator udpReplicator =
new UdpReplicator(result, udpReplicatorBuilder.clone(), entrySize(), result.identifier(), UDP_REPLICATION_MODIFICATION_ITERATOR_ID);
result.addCloseable(udpReplicator);
}
<K, V> void applyTcpReplication(@NotNull VanillaSharedReplicatedMap<K, V> result,
@NotNull TcpReplicatorBuilder tcpReplicatorBuilder)
throws IOException {
result.addCloseable(new TcpReplicator(result, result, tcpReplicatorBuilder, entrySize()));
}
public SharedHashMapBuilder timeProvider(TimeProvider timeProvider) {
this.timeProvider = timeProvider;
return this;
}
public TimeProvider timeProvider() {
return timeProvider;
}
public byte identifier() {
if (identifier == Byte.MIN_VALUE)
throw new IllegalStateException("identifier is not set.");
return identifier;
}
private String identifierToString() {
return identifier == Byte.MIN_VALUE ? "identifier is not set" : (identifier + "");
}
public SharedHashMapBuilder identifier(byte identifier) {
this.identifier = identifier;
return this;
}
public SharedHashMapBuilder tcpReplicatorBuilder(TcpReplicatorBuilder tcpReplicatorBuilder) {
this.tcpReplicatorBuilder = tcpReplicatorBuilder;
return this;
}
public TcpReplicatorBuilder tcpReplicatorBuilder() {
return tcpReplicatorBuilder;
}
public UdpReplicatorBuilder udpReplicatorBuilder() {
return udpReplicatorBuilder;
}
public SharedHashMapBuilder udpReplicatorBuilder(UdpReplicatorBuilder udpReplicatorBuilder) {
this.udpReplicatorBuilder = udpReplicatorBuilder;
return this;
}
public BytesMarshallerFactory bytesMarshallerFactory() {
return bytesMarshallerFactory == null ? bytesMarshallerFactory = new VanillaBytesMarshallerFactory() : bytesMarshallerFactory;
}
public SharedHashMapBuilder bytesMarshallerFactory(BytesMarshallerFactory bytesMarshallerFactory) {
this.bytesMarshallerFactory = bytesMarshallerFactory;
return this;
}
public ObjectSerializer objectSerializer() {
return objectSerializer == null ? objectSerializer = BytesMarshallableSerializer.create(bytesMarshallerFactory(), JDKObjectSerializer.INSTANCE) : objectSerializer;
}
public SharedHashMapBuilder objectSerializer(ObjectSerializer objectSerializer) {
this.objectSerializer = objectSerializer;
return this;
}
public SharedHashMapBuilder externalReplicatorBuilder(@NotNull ExternalReplicatorBuilder externalReplicatorBuilder) {
this.externalReplicatorBuilder = externalReplicatorBuilder;
return this;
}
public <T extends ExternalReplicator> T externalReplicator() {
return (T) externalReplicator;
}
}
@@ -26,7 +26,7 @@ import java.io.IOException;
/**
* @author Rob Austin.
*/
class ChronicleMap0<K, V> extends VanillaSharedHashMap<K, V> implements ChronicleMap<K, V> {
class ChronicleMap0<K, V> extends VanillaSharedMap<K, V> implements ChronicleMap<K, V> {
public ChronicleMap0(ChronicleMapBuilder0<K, V> builder, File file, Class<K> kClass, Class<V> vClass)
throws IOException {
super(builder, file, kClass, vClass);
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
@@ -25,10 +25,14 @@ package net.openhft.chronicle.map;/*
/*import junit.framework.Test;
import junit.framework.TestSuite;*/
import net.openhft.collections.SharedHashMapBuilder;
import net.openhft.collections.jrs166.JSR166TestCase;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.LongAdder;
import java.util.function.*;
@@ -36,15 +40,15 @@ import java.util.function.*;
import static java.util.Spliterator.*;
import static org.junit.Assert.*;
public class ConcurrentHashMap8Test extends JSR166TestCase {
public class ChronicleMap8Test extends JSR166TestCase {
private static final double EPSILON = 1E-5;
private static ConcurrentHashMap<Long, Long> newLongMap() {
private static ChronicleMap<Long, Long> newLongMap() {
//First create (or access if already created) the shared map
/* final ConcurrentHashMapBuilder0<Long, Long> builder = ConcurrentHashMapBuilder0.of(Long.class, Long.class);
final SharedHashMapBuilder<Long, Long> builder = SharedHashMapBuilder.of(Long.class, Long.class);
//// don't include this, just to check it is as expected.
@@ -56,17 +60,18 @@ public class ConcurrentHashMap8Test extends JSR166TestCase {
return builder.file(new File(shmPath)).create();
} catch (IOException e) {
throw new IllegalStateException(e);
}*/
}
return new ConcurrentHashMap(5);
// return new ChronicleMap(5);
}
private static ConcurrentHashMap newIntegerStringMap() {
private static ChronicleMap newIntegerStringMap() {
/* final ConcurrentHashMapBuilder0<Long, String> builder = ConcurrentHashMapBuilder0.of(Long.class, String.class);
final SharedHashMapBuilder<Integer, String> builder = SharedHashMapBuilder.of(Integer.class,
String.class);
//// don't include this, just to check it is as expected.
@@ -79,25 +84,26 @@ public class ConcurrentHashMap8Test extends JSR166TestCase {
return builder.file(new File(shmPath)).create();
} catch (IOException e) {
throw new IllegalStateException(e);
}*/
}
return new ConcurrentHashMap(5);
}
/**
* Returns a new map from Integers 1-5 to Strings "A"-"E".
*/
private static ConcurrentHashMap map5() {
ConcurrentHashMap map = newIntegerStringMap();
private static ChronicleMap map5() {
ChronicleMap map = newIntegerStringMap();
assertTrue(map.isEmpty());
map.put(one, "A");
map.put(two, "B");
map.put(three, "C");
map.put(four, "D");
map.put(five, "E");
assertFalse(map.isEmpty());
final boolean empty = map.isEmpty();
assertFalse(empty);
assertEquals(5, map.size());
return map;
}
@@ -108,7 +114,7 @@ public class ConcurrentHashMap8Test extends JSR166TestCase {
*/
@Test
public void testGetOrDefault() {
ConcurrentHashMap map = map5();
ChronicleMap map = map5();
assertEquals(map.getOrDefault(one, "Z"), "A");
assertEquals(map.getOrDefault(six, "Z"), "Z");
}
@@ -118,7 +124,7 @@ public class ConcurrentHashMap8Test extends JSR166TestCase {
*/
@Test
public void testComputeIfAbsent() {
ConcurrentHashMap map = map5();
ChronicleMap map = map5();
map.computeIfAbsent(six, (x) -> "Z");
assertTrue(map.containsKey(six));
}
@@ -128,7 +134,7 @@ public class ConcurrentHashMap8Test extends JSR166TestCase {
*/
@Test
public void testComputeIfAbsent2() {
ConcurrentHashMap map = map5();
ChronicleMap map = map5();
assertEquals("A", map.computeIfAbsent(one, (x) -> "Z"));
}
@@ -137,7 +143,7 @@ public class ConcurrentHashMap8Test extends JSR166TestCase {
*/
@Test
public void testComputeIfAbsent3() {
ConcurrentHashMap map = map5();
ChronicleMap map = map5();
map.computeIfAbsent(six, (x) -> null);
assertFalse(map.containsKey(six));
}
@@ -147,7 +153,7 @@ public class ConcurrentHashMap8Test extends JSR166TestCase {
*/
@Test
public void testComputeIfPresent() {
ConcurrentHashMap map = map5();
ChronicleMap map = map5();
map.computeIfPresent(six, (x, y) -> "Z");
assertFalse(map.containsKey(six));
}
@@ -157,7 +163,7 @@ public class ConcurrentHashMap8Test extends JSR166TestCase {
*/
@Test
public void testComputeIfPresent2() {
ConcurrentHashMap map = map5();
ChronicleMap map = map5();
assertEquals("Z", map.computeIfPresent(one, (x, y) -> "Z"));
}
@@ -166,7 +172,7 @@ public class ConcurrentHashMap8Test extends JSR166TestCase {
*/
@Test
public void testCompute() {
ConcurrentHashMap map = map5();
ChronicleMap map = map5();
map.compute(six, (x, y) -> null);
assertFalse(map.containsKey(six));
}
@@ -176,7 +182,7 @@ public class ConcurrentHashMap8Test extends JSR166TestCase {
*/
@Test
public void testCompute2() {
ConcurrentHashMap map = map5();
ChronicleMap map = map5();
assertEquals("Z", map.compute(six, (x, y) -> "Z"));
}
@@ -185,7 +191,7 @@ public class ConcurrentHashMap8Test extends JSR166TestCase {
*/
@Test
public void testCompute3() {
ConcurrentHashMap map = map5();
ChronicleMap map = map5();
assertEquals("Z", map.compute(one, (x, y) -> "Z"));
}
@@ -194,7 +200,7 @@ public class ConcurrentHashMap8Test extends JSR166TestCase {
*/
@Test
public void testCompute4() {
ConcurrentHashMap map = map5();
ChronicleMap map = map5();
map.compute(one, (x, y) -> null);
assertFalse(map.containsKey(one));
}
@@ -204,7 +210,7 @@ public class ConcurrentHashMap8Test extends JSR166TestCase {
*/
@Test
public void testMerge1() {
ConcurrentHashMap map = map5();
ChronicleMap map = map5();
assertEquals("Y", map.merge(six, "Y", (x, y) -> "Z"));
}
@@ -213,7 +219,7 @@ public class ConcurrentHashMap8Test extends JSR166TestCase {
*/
@Test
public void testMerge2() {
ConcurrentHashMap map = map5();
ChronicleMap map = map5();
assertEquals("Z", map.merge(one, "Y", (x, y) -> "Z"));
}
@@ -222,7 +228,7 @@ public class ConcurrentHashMap8Test extends JSR166TestCase {
*/
@Test
public void testMerge3() {
ConcurrentHashMap map = map5();
ChronicleMap map = map5();
map.merge(one, "Y", (x, y) -> null);
assertFalse(map.containsKey(one));
}
@@ -252,7 +258,7 @@ public class ConcurrentHashMap8Test extends JSR166TestCase {
*/
@Test
public void testReplaceAll() {
ConcurrentHashMap<Integer, String> map = map5();
ChronicleMap<Integer, String> map = map5();
map.replaceAll((x, y) -> {
return x > 3 ? "Z" : y;
});
@@ -277,7 +283,7 @@ public class ConcurrentHashMap8Test extends JSR166TestCase {
*/
@Test
public void testKeySetAddRemove() {
ConcurrentHashMap map = map5();
ChronicleMap map = map5();
Set set1 = map.keySet();
Set set2 = map.keySet(true);
set2.add(six);
@@ -374,14 +380,14 @@ public class ConcurrentHashMap8Test extends JSR166TestCase {
*/
@Test
public void testGetMappedValue() {
ConcurrentHashMap map = map5();
ChronicleMap map = map5();
assertNull(map.keySet().getMappedValue());
try {
map.keySet(null);
shouldThrow();
} catch (NullPointerException e) {
}
final ConcurrentHashMap.KeySetView set = map.keySet(one);
final net.openhft.collections.ConcurrentHashMap.KeySetView set = map.keySet(one);
set.add(one);
set.add(six);
set.add(seven);
@@ -403,7 +409,7 @@ public class ConcurrentHashMap8Test extends JSR166TestCase {
@Test
public void testKeySetSpliterator() {
LongAdder adder = new LongAdder();
ConcurrentHashMap map = map5();
ChronicleMap map = map5();
Set set = map.keySet();
Spliterator<Integer> sp = set.spliterator();
checkSpliteratorCharacteristics(sp, CONCURRENT | DISTINCT | NONNULL);
@@ -649,9 +655,9 @@ public class ConcurrentHashMap8Test extends JSR166TestCase {
}
static final int SIZE = 10000;
static ConcurrentHashMap<Long, Long> longMap;
static ChronicleMap<Long, Long> longMap;
static ConcurrentHashMap<Long, Long> longMap() {
static ChronicleMap<Long, Long> longMap() {
if (longMap == null) {
longMap = newLongMap();
for (int i = 0; i < SIZE; ++i)
@@ -677,7 +683,7 @@ public class ConcurrentHashMap8Test extends JSR166TestCase {
@Test
public void testForEachKeySequentially() {
LongAdder adder = new LongAdder();
ConcurrentHashMap<Long, Long> m = longMap();
ChronicleMap<Long, Long> m = longMap();
final Consumer<Long> consumer = (Long x) -> adder.add(x.longValue());
m.forEachKey(Long.MAX_VALUE, consumer);
assertEquals(adder.sum(), SIZE * (SIZE - 1) / 2);
@@ -689,7 +695,7 @@ public class ConcurrentHashMap8Test extends JSR166TestCase {
@Test
public void testForEachValueSequentially() {
LongAdder adder = new LongAdder();
ConcurrentHashMap<Long, Long> m = (ConcurrentHashMap) longMap();
ChronicleMap<Long, Long> m = (ChronicleMap) longMap();
final Consumer<Long> action = (Long x) -> adder.add(x.longValue());
m.forEachValue(Long.MAX_VALUE, action);
assertEquals(adder.sum(), SIZE * (SIZE - 1));
@@ -701,7 +707,7 @@ public class ConcurrentHashMap8Test extends JSR166TestCase {
@Test
public void testForEachSequentially() {
LongAdder adder = new LongAdder();
ConcurrentHashMap<Long, Long> m = longMap();
ChronicleMap<Long, Long> m = longMap();
final BiConsumer<Long, Long> action = (Long x, Long y) -> adder.add(x.longValue() + y.longValue());
m.forEach(Long.MAX_VALUE, action);
assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2);
@@ -713,7 +719,7 @@ public class ConcurrentHashMap8Test extends JSR166TestCase {
@Test
public void testForEachEntrySequentially() {
LongAdder adder = new LongAdder();
ConcurrentHashMap<Long, Long> m = longMap();
ChronicleMap<Long, Long> m = longMap();
final Consumer<Map.Entry<Long, Long>> action = (Map.Entry<Long, Long> e) -> adder.add(e.getKey().longValue() + e.getValue().longValue());
m.forEachEntry(Long.MAX_VALUE, action);
assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2);
@@ -725,7 +731,7 @@ public class ConcurrentHashMap8Test extends JSR166TestCase {
@Test
public void testForEachKeyInParallel() {
LongAdder adder = new LongAdder();
ConcurrentHashMap<Long, Long> m = longMap();
ChronicleMap<Long, Long> m = longMap();
final Consumer<Long> action = (Long x) -> adder.add(x.longValue());
m.forEachKey(1L, action);
@@ -738,7 +744,7 @@ public class ConcurrentHashMap8Test extends JSR166TestCase {
@Test
public void testForEachValueInParallel() {
LongAdder adder = new LongAdder();
ConcurrentHashMap<Long, Long> m = longMap();
ChronicleMap<Long, Long> m = longMap();
final Consumer<Long> action = (Long x) -> adder.add(x.longValue());
m.forEachValue(1L, action);
assertEquals(adder.sum(), SIZE * (SIZE - 1));
@@ -750,7 +756,7 @@ public class ConcurrentHashMap8Test extends JSR166TestCase {
@Test
public void testForEachInParallel() {
LongAdder adder = new LongAdder();
ConcurrentHashMap<Long, Long> m = longMap();
ChronicleMap<Long, Long> m = longMap();
final BiConsumer<Long, Long> action = (Long x, Long y) -> adder.add(x.longValue() + y.longValue());
m.forEach(1L, action);
assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2);
@@ -762,7 +768,7 @@ public class ConcurrentHashMap8Test extends JSR166TestCase {
@Test
public void testForEachEntryInParallel() {
LongAdder adder = new LongAdder();
ConcurrentHashMap<Long, Long> m = longMap();
ChronicleMap<Long, Long> m = longMap();
final Consumer<Map.Entry<Long, Long>> action = (Map.Entry<Long, Long> e) -> adder.add(e.getKey().longValue() + e.getValue().longValue());
m.forEachEntry(1L, action);
assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2);
@@ -774,7 +780,7 @@ public class ConcurrentHashMap8Test extends JSR166TestCase {
@Test
public void testMappedForEachKeySequentially() {
LongAdder adder = new LongAdder();
ConcurrentHashMap<Long, Long> m = longMap();
ChronicleMap<Long, Long> m = longMap();
final Function<Long, Long> transformer = (Long x) -> Long.valueOf(4 * x.longValue());
final Consumer<Long> action = (Long x) -> adder.add(x.longValue());
m.forEachKey(Long.MAX_VALUE, transformer,
@@ -788,7 +794,7 @@ public class ConcurrentHashMap8Test extends JSR166TestCase {
@Test
public void testMappedForEachValueSequentially() {
LongAdder adder = new LongAdder();
ConcurrentHashMap<Long, Long> m = longMap();
ChronicleMap<Long, Long> m = longMap();
final Function<Long, Long> transformer = (Long x) -> Long.valueOf(4 * x.longValue());
final Consumer<Long> action = (Long x) -> adder.add(x.longValue());
m.forEachValue(Long.MAX_VALUE, transformer,
@@ -802,7 +808,7 @@ public class ConcurrentHashMap8Test extends JSR166TestCase {
@Test
public void testMappedForEachSequentially() {
LongAdder adder = new LongAdder();
ConcurrentHashMap<Long, Long> m = longMap();
ChronicleMap<Long, Long> m = longMap();
final BiFunction<Long, Long, Long> transformer = (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue());
final Consumer<Long> action = (Long x) -> adder.add(x.longValue());
m.forEach(Long.MAX_VALUE, transformer,
@@ -816,7 +822,7 @@ public class ConcurrentHashMap8Test extends JSR166TestCase {
@Test
public void testMappedForEachEntrySequentially() {
LongAdder adder = new LongAdder();
ConcurrentHashMap<Long, Long> m = longMap();
ChronicleMap<Long, Long> m = longMap();
final Function<Map.Entry<Long, Long>, Long> transformer = (Map.Entry<Long, Long> e) -> Long.valueOf(e.getKey().longValue() + e.getValue().longValue());
final Consumer<Long> action = (Long x) -> adder.add(x.longValue());
m.forEachEntry(Long.MAX_VALUE, transformer,
@@ -830,7 +836,7 @@ public class ConcurrentHashMap8Test extends JSR166TestCase {
@Test
public void testMappedForEachKeyInParallel() {
LongAdder adder = new LongAdder();
ConcurrentHashMap<Long, Long> m = longMap();
ChronicleMap<Long, Long> m = longMap();
final Function<Long, Long> transformer = (Long x) -> Long.valueOf(4 * x.longValue());
final Consumer<Long> action = (Long x) -> adder.add(x.longValue());
m.forEachKey(1L, transformer,
@@ -844,7 +850,7 @@ public class ConcurrentHashMap8Test extends JSR166TestCase {
@Test
public void testMappedForEachValueInParallel() {
LongAdder adder = new LongAdder();
ConcurrentHashMap<Long, Long> m = longMap();
ChronicleMap<Long, Long> m = longMap();
final Function<Long, Long> transformer = (Long x) -> Long.valueOf(4 * x.longValue());
final Consumer<Long> action = (Long x) -> adder.add(x.longValue());
m.forEachValue(1L, transformer,
@@ -858,7 +864,7 @@ public class ConcurrentHashMap8Test extends JSR166TestCase {
@Test
public void testMappedForEachInParallel() {
LongAdder adder = new LongAdder();
ConcurrentHashMap<Long, Long> m = longMap();
ChronicleMap<Long, Long> m = longMap();
final BiFunction<Long, Long, Long> transformer = (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue());
final Consumer<Long> action = (Long x) -> adder.add(x.longValue());
m.forEach(1L, transformer,
@@ -872,7 +878,7 @@ public class ConcurrentHashMap8Test extends JSR166TestCase {
@Test
public void testMappedForEachEntryInParallel() {
LongAdder adder = new LongAdder();
ConcurrentHashMap<Long, Long> m = longMap();
ChronicleMap<Long, Long> m = longMap();
final Function<Map.Entry<Long, Long>, Long> transformer = (Map.Entry<Long, Long> e) -> Long.valueOf(e.getKey().longValue() + e.getValue().longValue());
final Consumer<Long> action = (Long x) -> adder.add(x.longValue());
m.forEachEntry(1L, transformer,
@@ -885,7 +891,7 @@ public class ConcurrentHashMap8Test extends JSR166TestCase {
*/
@Test
public void testReduceKeysSequentially() {
ConcurrentHashMap<Long, Long> m = longMap();
ChronicleMap<Long, Long> m = longMap();
Long r;
final BiFunction<Long, Long, Long> reducer = (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue());
r = m.reduceKeys(Long.MAX_VALUE, reducer);
@@ -897,7 +903,7 @@ public class ConcurrentHashMap8Test extends JSR166TestCase {
*/
@Test
public void testReduceValuesSequentially() {
ConcurrentHashMap<Long, Long> m = longMap();
ChronicleMap<Long, Long> m = longMap();
Long r;
final BiFunction<Long, Long, Long> reducer = (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue());
r = m.reduceKeys(Long.MAX_VALUE, reducer);
@@ -909,7 +915,7 @@ public class ConcurrentHashMap8Test extends JSR166TestCase {
*/
@Test
public void testReduceEntriesSequentially() {
ConcurrentHashMap<Long, Long> m = longMap();
ChronicleMap<Long, Long> m = longMap();
Map.Entry<Long, Long> r;
final AddKeys reducer = new AddKeys();
r = m.reduceEntries(Long.MAX_VALUE, reducer);
@@ -921,7 +927,7 @@ public class ConcurrentHashMap8Test extends JSR166TestCase {
*/
@Test
public void testReduceKeysInParallel() {
ConcurrentHashMap<Long, Long> m = longMap();
ChronicleMap<Long, Long> m = longMap();
Long r;
final BiFunction<Long, Long, Long> reducer = (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue());
r = m.reduceKeys(1L, reducer);
@@ -933,7 +939,7 @@ public class ConcurrentHashMap8Test extends JSR166TestCase {
*/
@Test
public void testReduceValuesInParallel() {
ConcurrentHashMap<Long, Long> m = longMap();
ChronicleMap<Long, Long> m = longMap();
Long r;
final BiFunction<Long, Long, Long> reducer = (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue());
r = m.reduceValues(1L, reducer);
@@ -945,7 +951,7 @@ public class ConcurrentHashMap8Test extends JSR166TestCase {
*/
@Test
public void testReduceEntriesInParallel() {
ConcurrentHashMap<Long, Long> m = longMap();
ChronicleMap<Long, Long> m = longMap();
Map.Entry<Long, Long> r;
r = m.reduceEntries(1L, new AddKeys());
assertEquals(r.getKey().longValue(), (long) SIZE * (SIZE - 1) / 2);
@@ -956,7 +962,7 @@ public class ConcurrentHashMap8Test extends JSR166TestCase {
*/
@Test
public void testMapReduceKeysSequentially() {
ConcurrentHashMap<Long, Long> m = longMap();
ChronicleMap<Long, Long> m = longMap();
final Function<Long, Long> transformer = (Long x) -> Long.valueOf(4 * x.longValue());
final BiFunction<Long, Long, Long> reducer = (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue());
Long r = m.reduceKeys(Long.MAX_VALUE, transformer,
@@ -969,7 +975,7 @@ public class ConcurrentHashMap8Test extends JSR166TestCase {
*/
@Test
public void testMapReduceValuesSequentially() {
ConcurrentHashMap<Long, Long> m = longMap();
ChronicleMap<Long, Long> m = longMap();
final Function<Long, Long> transformer = (Long x) -> Long.valueOf(4 * x.longValue());
final BiFunction<Long, Long, Long> reducer = (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue());
Long r = m.reduceValues(Long.MAX_VALUE, transformer,
@@ -982,7 +988,7 @@ public class ConcurrentHashMap8Test extends JSR166TestCase {
*/
@Test
public void testMappedReduceSequentially() {
ConcurrentHashMap<Long, Long> m = longMap();
ChronicleMap<Long, Long> m = longMap();
final BiFunction<Long, Long, Long> transformer = (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue());
final BiFunction<Long, Long, Long> reducer = (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue());
Long r = m.reduce(Long.MAX_VALUE, transformer,
@@ -996,7 +1002,7 @@ public class ConcurrentHashMap8Test extends JSR166TestCase {
*/
@Test
public void testMapReduceKeysInParallel() {
ConcurrentHashMap<Long, Long> m = longMap();
ChronicleMap<Long, Long> m = longMap();
final Function<Long, Long> transformer = (Long x) -> Long.valueOf(4 * x.longValue());
final BiFunction<Long, Long, Long> reducer = (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue());
Long r = m.reduceKeys(1L, transformer,
@@ -1009,7 +1015,7 @@ public class ConcurrentHashMap8Test extends JSR166TestCase {
*/
@Test
public void testMapReduceValuesInParallel() {
ConcurrentHashMap<Long, Long> m = longMap();
ChronicleMap<Long, Long> m = longMap();
final Function<Long, Long> transformer = (Long x) -> Long.valueOf(4 * x.longValue());
final BiFunction<Long, Long, Long> reducer = (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue());
Long r = m.reduceValues(1L, transformer,
@@ -1022,7 +1028,7 @@ public class ConcurrentHashMap8Test extends JSR166TestCase {
*/
@Test
public void testMappedReduceInParallel() {
ConcurrentHashMap<Long, Long> m = longMap();
ChronicleMap<Long, Long> m = longMap();
Long r;
final BiFunction<Long, Long, Long> transformer = (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue());
final BiFunction<Long, Long, Long> reducer = (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue());
@@ -1036,7 +1042,7 @@ public class ConcurrentHashMap8Test extends JSR166TestCase {
*/
@Test
public void testReduceKeysToLongSequentially() {
ConcurrentHashMap<Long, Long> m = longMap();
ChronicleMap<Long, Long> m = longMap();
final ToLongFunction<Long> transformer = (Long x) -> x.longValue();
final LongBinaryOperator sum = Long::sum;
long lr = m.reduceKeysToLong(Long.MAX_VALUE, transformer, 0L, sum);
@@ -1048,7 +1054,7 @@ public class ConcurrentHashMap8Test extends JSR166TestCase {
*/
@Test
public void testReduceKeysToIntSequentially() {
ConcurrentHashMap<Long, Long> m = longMap();
ChronicleMap<Long, Long> m = longMap();
final ToIntFunction<Long> transformer = (Long x) -> x.intValue();
final IntBinaryOperator sum = Integer::sum;
int ir = m.reduceKeysToInt(Long.MAX_VALUE, transformer, 0, sum);
@@ -1060,7 +1066,7 @@ public class ConcurrentHashMap8Test extends JSR166TestCase {
*/
@Test
public void testReduceKeysToDoubleSequentially() {
ConcurrentHashMap<Long, Long> m = longMap();
ChronicleMap<Long, Long> m = longMap();
final ToDoubleFunction<Long> transformer = (Long x) -> x.doubleValue();
double dr = m.reduceKeysToDouble(Long.MAX_VALUE, transformer, 0.0, Double::sum);
assertEquals(dr, (double) SIZE * (SIZE - 1) / 2, EPSILON);
@@ -1071,7 +1077,7 @@ public class ConcurrentHashMap8Test extends JSR166TestCase {
*/
@Test
public void testReduceValuesToLongSequentially() {
ConcurrentHashMap<Long, Long> m = longMap();
ChronicleMap<Long, Long> m = longMap();
final ToLongFunction<Long> transformer = (Long x) -> x.longValue();
long lr = m.reduceValuesToLong(Long.MAX_VALUE, transformer, 0L, Long::sum);
assertEquals(lr, (long) SIZE * (SIZE - 1));
@@ -1082,7 +1088,7 @@ public class ConcurrentHashMap8Test extends JSR166TestCase {
*/
@Test
public void testReduceValuesToIntSequentially() {
ConcurrentHashMap<Long, Long> m = longMap();
ChronicleMap<Long, Long> m = longMap();
final ToIntFunction<Long> transformer = (Long x) -> x.intValue();
int ir = m.reduceValuesToInt(Long.MAX_VALUE, transformer, 0, Integer::sum);
assertEquals(ir, SIZE * (SIZE - 1));
@@ -1093,7 +1099,7 @@ public class ConcurrentHashMap8Test extends JSR166TestCase {
*/
@Test
public void testReduceValuesToDoubleSequentially() {
ConcurrentHashMap<Long, Long> m = longMap();
ChronicleMap<Long, Long> m = longMap();
final ToDoubleFunction<Long> transformer = (Long x) -> x.doubleValue();
double dr = m.reduceValuesToDouble(Long.MAX_VALUE, transformer, 0.0, Double::sum);
assertEquals(dr, (double) SIZE * (SIZE - 1), EPSILON);
@@ -1104,7 +1110,7 @@ public class ConcurrentHashMap8Test extends JSR166TestCase {
*/
@Test
public void testReduceKeysToLongInParallel() {
ConcurrentHashMap<Long, Long> m = longMap();
ChronicleMap<Long, Long> m = longMap();
final ToLongFunction<Long> transformer = (Long x) -> x.longValue();
long lr = m.reduceKeysToLong(1L, transformer, 0L, Long::sum);
assertEquals(lr, (long) SIZE * (SIZE - 1) / 2);
@@ -1115,7 +1121,7 @@ public class ConcurrentHashMap8Test extends JSR166TestCase {
*/
@Test
public void testReduceKeysToIntInParallel() {
ConcurrentHashMap<Long, Long> m = longMap();
ChronicleMap<Long, Long> m = longMap();
final ToIntFunction<Long> transformer = (Long x) -> x.intValue();
int ir = m.reduceKeysToInt(1L, transformer, 0, Integer::sum);
assertEquals(ir, SIZE * (SIZE - 1) / 2);
@@ -1126,7 +1132,7 @@ public class ConcurrentHashMap8Test extends JSR166TestCase {
*/
@Test
public void testReduceKeysToDoubleInParallel() {
ConcurrentHashMap<Long, Long> m = longMap();
ChronicleMap<Long, Long> m = longMap();
final ToDoubleFunction<Long> transformer = (Long x) -> x.doubleValue();
double dr = m.reduceKeysToDouble(1L, transformer, 0.0, Double::sum);
assertEquals(dr, (double) SIZE * (SIZE - 1) / 2, 0.01);
@@ -1137,7 +1143,7 @@ public class ConcurrentHashMap8Test extends JSR166TestCase {
*/
@Test
public void testReduceValuesToLongInParallel() {
ConcurrentHashMap<Long, Long> m = longMap();
ChronicleMap<Long, Long> m = longMap();
final ToLongFunction<Long> transformer = (Long x) -> x.longValue();
long lr = m.reduceValuesToLong(1L, transformer, 0L, Long::sum);
assertEquals(lr, (long) SIZE * (SIZE - 1));
@@ -1148,7 +1154,7 @@ public class ConcurrentHashMap8Test extends JSR166TestCase {
*/
@Test
public void testReduceValuesToIntInParallel() {
ConcurrentHashMap<Long, Long> m = longMap();
ChronicleMap<Long, Long> m = longMap();
final ToIntFunction<Long> transformer = (Long x) -> x.intValue();
int ir = m.reduceValuesToInt(1L, transformer, 0, Integer::sum);
assertEquals(ir, SIZE * (SIZE - 1));
@@ -1159,7 +1165,7 @@ public class ConcurrentHashMap8Test extends JSR166TestCase {
*/
@Test
public void testReduceValuesToDoubleInParallel() {
ConcurrentHashMap<Long, Long> m = longMap();
ChronicleMap<Long, Long> m = longMap();
final ToDoubleFunction<Long> transformer = (Long x) -> x.doubleValue();
double dr = m.reduceValuesToDouble(1L, transformer, 0.0, Double::sum);
assertEquals(dr, (double) SIZE * (SIZE - 1), EPSILON);
@@ -1170,7 +1176,7 @@ public class ConcurrentHashMap8Test extends JSR166TestCase {
*/
@Test
public void testSearchKeysSequentially() {
ConcurrentHashMap<Long, Long> m = longMap();
ChronicleMap<Long, Long> m = longMap();
Long r;
final Function<Long, Long> searchFunction = (Long x) -> x.longValue() == (long) (SIZE / 2) ? x : null;
r = m.searchKeys(Long.MAX_VALUE, searchFunction);
@@ -1185,7 +1191,7 @@ public class ConcurrentHashMap8Test extends JSR166TestCase {
*/
@Test
public void testSearchValuesSequentially() {
ConcurrentHashMap<Long, Long> m = longMap();
ChronicleMap<Long, Long> m = longMap();
Long r;
final Function<Long, Long> searchFunction = (Long x) -> x.longValue() == (long) (SIZE / 2) ? x : null;
r = m.searchValues(Long.MAX_VALUE, searchFunction);
@@ -1200,7 +1206,7 @@ public class ConcurrentHashMap8Test extends JSR166TestCase {
*/
@Test
public void testSearchSequentially() {
ConcurrentHashMap<Long, Long> m = longMap();
ChronicleMap<Long, Long> m = longMap();
Long r;
final BiFunction<Long, Long, Long> searchFunction = (Long x, Long y) -> x.longValue() == (long) (SIZE / 2) ? x : null;
r = m.search(Long.MAX_VALUE, searchFunction);
@@ -1215,7 +1221,7 @@ public class ConcurrentHashMap8Test extends JSR166TestCase {
*/
@Test
public void testSearchEntriesSequentially() {
ConcurrentHashMap<Long, Long> m = longMap();
ChronicleMap<Long, Long> m = longMap();
Long r;
final Function<Map.Entry<Long, Long>, Long> searchFunction = (Map.Entry<Long, Long> e) -> e.getKey().longValue() == (long) (SIZE / 2) ? e.getKey() : null;
r = m.searchEntries(Long.MAX_VALUE, searchFunction);
@@ -1230,7 +1236,7 @@ public class ConcurrentHashMap8Test extends JSR166TestCase {
*/
@Test
public void testSearchKeysInParallel() {
ConcurrentHashMap<Long, Long> m = longMap();
ChronicleMap<Long, Long> m = longMap();
Long r;
final Function<Long, Long> searchFunction = (Long x) -> x.longValue() == (long) (SIZE / 2) ? x : null;
r = m.searchKeys(1L, searchFunction);
@@ -1245,7 +1251,7 @@ public class ConcurrentHashMap8Test extends JSR166TestCase {
*/
@Test
public void testSearchValuesInParallel() {
ConcurrentHashMap<Long, Long> m = longMap();
ChronicleMap<Long, Long> m = longMap();
Long r;
final Function<Long, Long> searchFunction = (Long x) -> x.longValue() == (long) (SIZE / 2) ? x : null;
r = m.searchValues(1L, searchFunction);
@@ -1260,7 +1266,7 @@ public class ConcurrentHashMap8Test extends JSR166TestCase {
*/
@Test
public void testSearchInParallel() {
ConcurrentHashMap<Long, Long> m = longMap();
ChronicleMap<Long, Long> m = longMap();
Long r;
final BiFunction<Long, Long, Long> searchFunction = (Long x, Long y) -> x.longValue() == (long) (SIZE / 2) ? x : null;
r = m.search(1L, searchFunction);
@@ -1275,7 +1281,7 @@ public class ConcurrentHashMap8Test extends JSR166TestCase {
*/
@Test
public void testSearchEntriesInParallel() {
ConcurrentHashMap<Long, Long> m = longMap();
ChronicleMap<Long, Long> m = longMap();
Long r;
final Function<Map.Entry<Long, Long>, Long> searchFunction = (Map.Entry<Long, Long> e) -> e.getKey().longValue() == (long) (SIZE / 2) ? e.getKey() : null;
r = m.searchEntries(1L, searchFunction);