HCOLL-119 moved the builder into chronicle map

This commit is contained in:
Rob Austin
2014-07-29 09:58:37 +01:00
parent 78a70128bb
commit c3904e6930
14 changed files with 2343 additions and 9195 deletions
@@ -18,311 +18,10 @@
package net.openhft.chronicle.map;
import net.openhft.collections.ConcurrentMapLamdadSupport;
import net.openhft.collections.SharedHashMap;
import java.util.Map;
import java.util.Objects;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Function;
/**
* @author Rob Austin.
*/
public interface ChronicleMap<K, V> extends SharedHashMap<K, V>, ConcurrentMapLamdadSupport<K, V> {
/**
* {@inheritDoc}
*
* @throws ClassCastException {@inheritDoc}
* @throws NullPointerException {@inheritDoc}
* @implNote This implementation assumes that the ConcurrentMap cannot contain null values and {@code
* get()} returning null unambiguously means the key is absent. Implementations which support null values
* <strong>must</strong> override this default implementation.
* @since 1.8
*/
default V getOrDefault(Object key, V defaultValue) {
V v;
return ((v = get(key)) != null) ? v : defaultValue;
}
/**
* {@inheritDoc}
*
* @throws NullPointerException {@inheritDoc}
* @implSpec The default implementation is equivalent to, for this {@code map}:
* <pre> {@code
* for ((Map.Entry<K, V> entry : map.entrySet())
* action.accept(entry.getKey(), entry.getValue());
* }</pre>
* @implNote The default implementation assumes that {@code IllegalStateException} thrown by {@code
* getKey()} or {@code getValue()} indicates that the entry has been removed and cannot be processed.
* Operation continues for subsequent entries.
* @since 1.8
*/
default void forEach(BiConsumer<? super K, ? super V> action) {
Objects.requireNonNull(action);
for (Map.Entry<K, V> entry : entrySet()) {
K k;
V v;
try {
k = entry.getKey();
v = entry.getValue();
} catch (IllegalStateException ise) {
// this usually means the entry is no longer in the map.
continue;
}
action.accept(k, v);
}
}
/**
* {@inheritDoc}
*
* @throws UnsupportedOperationException {@inheritDoc}
* @throws NullPointerException {@inheritDoc}
* @throws ClassCastException {@inheritDoc}
* @throws IllegalArgumentException {@inheritDoc}
* @implSpec <p>The default implementation is equivalent to, for this {@code map}:
* <pre> {@code
* for ((Map.Entry<K, V> entry : map.entrySet())
* do {
* K k = entry.getKey();
* V v = entry.getValue();
* } while(!replace(k, v, function.apply(k, v)));
* }</pre>
*
* The default implementation may retry these steps when multiple threads attempt updates including
* potentially calling the function repeatedly for a given key.
*
* <p>This implementation assumes that the ConcurrentMap cannot contain null values and {@code get()}
* returning null unambiguously means the key is absent. Implementations which support null values
* <strong>must</strong> override this default implementation.
* @since 1.8
*/
default void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) {
Objects.requireNonNull(function);
final BiConsumer<K, V> action = (k, v) -> {
while (!replace(k, v, function.apply(k, v))) {
// v changed or k is gone
if ((v = get(k)) == null) {
// k is no longer in the map.
break;
}
}
};
forEach(action);
}
/**
* {@inheritDoc}
*
* @throws UnsupportedOperationException {@inheritDoc}
* @throws ClassCastException {@inheritDoc}
* @throws NullPointerException {@inheritDoc}
* @implSpec The default implementation is equivalent to the following steps for this {@code map}, then
* returning the current value or {@code null} if now absent:
*
* <pre> {@code
* if (map.get(key) == null) {
* V newValue = mappingFunction.apply(key);
* if (newValue != null)
* return map.putIfAbsent(key, newValue);
* }
* }</pre>
*
* The default implementation may retry these steps when multiple threads attempt updates including
* potentially calling the mapping function multiple times.
*
* <p>This implementation assumes that the ConcurrentMap cannot contain null values and {@code get()}
* returning null unambiguously means the key is absent. Implementations which support null values
* <strong>must</strong> override this default implementation.
* @since 1.8
*/
default V computeIfAbsent(K key,
Function<? super K, ? extends V> mappingFunction) {
Objects.requireNonNull(mappingFunction);
V v, newValue;
return ((v = get(key)) == null &&
(newValue = mappingFunction.apply(key)) != null &&
(v = putIfAbsent(key, newValue)) == null) ? newValue : v;
}
/**
* {@inheritDoc}
*
* @throws UnsupportedOperationException {@inheritDoc}
* @throws ClassCastException {@inheritDoc}
* @throws NullPointerException {@inheritDoc}
* @implSpec The default implementation is equivalent to performing the following steps for this {@code
* map}, then returning the current value or {@code null} if now absent. :
*
* <pre> {@code
* if (map.get(key) != null) {
* V oldValue = map.get(key);
* V newValue = remappingFunction.apply(key, oldValue);
* if (newValue != null)
* map.replace(key, oldValue, newValue);
* else
* map.remove(key, oldValue);
* }
* }</pre>
*
* The default implementation may retry these steps when multiple threads attempt updates including
* potentially calling the remapping function multiple times.
*
* <p>This implementation assumes that the ConcurrentMap cannot contain null values and {@code get()}
* returning null unambiguously means the key is absent. Implementations which support null values
* <strong>must</strong> override this default implementation.
* @since 1.8
*/
default V computeIfPresent(K key,
BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
Objects.requireNonNull(remappingFunction);
V oldValue;
while ((oldValue = get(key)) != null) {
V newValue = remappingFunction.apply(key, oldValue);
if (newValue != null) {
if (replace(key, oldValue, newValue))
return newValue;
} else if (remove(key, oldValue))
return null;
}
return oldValue;
}
/**
* {@inheritDoc}
*
* @throws UnsupportedOperationException {@inheritDoc}
* @throws ClassCastException {@inheritDoc}
* @throws NullPointerException {@inheritDoc}
* @implSpec The default implementation is equivalent to performing the following steps for this {@code
* map}, then returning the current value or {@code null} if absent:
*
* <pre> {@code
* V oldValue = map.get(key);
* V newValue = remappingFunction.apply(key, oldValue);
* if (oldValue != null ) {
* if (newValue != null)
* map.replace(key, oldValue, newValue);
* else
* map.remove(key, oldValue);
* } else {
* if (newValue != null)
* map.putIfAbsent(key, newValue);
* else
* return null;
* }
* }</pre>
*
* The default implementation may retry these steps when multiple threads attempt updates including
* potentially calling the remapping function multiple times.
*
* <p>This implementation assumes that the ConcurrentMap cannot contain null values and {@code get()}
* returning null unambiguously means the key is absent. Implementations which support null values
* <strong>must</strong> override this default implementation.
* @since 1.8
*/
default V compute(K key,
BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
Objects.requireNonNull(remappingFunction);
V oldValue = get(key);
for (; ; ) {
V newValue = remappingFunction.apply(key, oldValue);
if (newValue == null) {
// delete mapping
if (oldValue != null || containsKey(key)) {
// something to remove
if (remove(key, oldValue)) {
// removed the old value as expected
return null;
}
// some other value replaced old value. try again.
oldValue = get(key);
} else {
// nothing to do. Leave things as they were.
return null;
}
} else {
// add or replace old mapping
if (oldValue != null) {
// replace
if (replace(key, oldValue, newValue)) {
// replaced as expected.
return newValue;
}
// some other value replaced old value. try again.
oldValue = get(key);
} else {
// add (replace if oldValue was null)
if ((oldValue = putIfAbsent(key, newValue)) == null) {
// replaced
return newValue;
}
// some other value replaced old value. try again.
}
}
}
}
/**
* {@inheritDoc}
*
* @throws UnsupportedOperationException {@inheritDoc}
* @throws ClassCastException {@inheritDoc}
* @throws NullPointerException {@inheritDoc}
* @implSpec The default implementation is equivalent to performing the following steps for this {@code
* map}, then returning the current value or {@code null} if absent:
*
* <pre> {@code
* V oldValue = map.get(key);
* V newValue = (oldValue == null) ? value :
* remappingFunction.apply(oldValue, value);
* if (newValue == null)
* map.remove(key);
* else
* map.put(key, newValue);
* }</pre>
*
* <p>The default implementation may retry these steps when multiple threads attempt updates including
* potentially calling the remapping function multiple times.
*
* <p>This implementation assumes that the ConcurrentMap cannot contain null values and {@code get()}
* returning null unambiguously means the key is absent. Implementations which support null values
* <strong>must</strong> override this default implementation.
* @since 1.8
*/
default V merge(K key, V value,
BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
Objects.requireNonNull(remappingFunction);
Objects.requireNonNull(value);
V oldValue = get(key);
for (; ; ) {
if (oldValue != null) {
V newValue = remappingFunction.apply(oldValue, value);
if (newValue != null) {
if (replace(key, oldValue, newValue))
return newValue;
} else if (remove(key, oldValue)) {
return null;
}
oldValue = get(key);
} else {
if ((oldValue = putIfAbsent(key, value)) == null) {
return value;
}
}
}
}
public interface ChronicleMap<K, V> extends SharedHashMap<K, V> {
}
@@ -18,13 +18,237 @@
package net.openhft.chronicle.map;
import net.openhft.collections.ChronicleMapBuilder0;
import net.openhft.collections.*;
import net.openhft.lang.io.serialization.BytesMarshallerFactory;
import net.openhft.lang.io.serialization.ObjectSerializer;
import net.openhft.lang.model.constraints.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
/**
* @author Rob Austin.
*/
public class ChronicleMapBuilder extends ChronicleMapBuilder0 {
public ChronicleMapBuilder(Class aClass, Class aClass2) {
super(aClass, aClass2);
public class ChronicleMapBuilder<K, V> extends SharedHashMapBuilder<K, V> implements Cloneable {
private static final Logger LOG = LoggerFactory.getLogger(ChronicleMapBuilder.class.getName());
public ChronicleMapBuilder(Class<K> kClass, Class<V> vClass) {
super(kClass, vClass);
}
public ChronicleMapBuilder() {
super();
}
public static <K, V> ChronicleMapBuilder<K, V> of(Class<K> kClass, Class<V> vClass) {
return new ChronicleMapBuilder<K, V>(kClass, vClass);
}
@Override
public ChronicleMapBuilder<K, V> clone() {
return (ChronicleMapBuilder) super.clone();
}
/**
* Set minimum number of segments. See concurrencyLevel in {@link java.util.concurrent.ConcurrentHashMap}.
*
* @return this builder object back
*/
public ChronicleMapBuilder minSegments(int minSegments) {
super.minSegments(minSegments);
return this;
}
/**
* <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(net.openhft.collections.Alignment)
* @see #entryAndValueAlignment()
*/
public ChronicleMapBuilder entrySize(int entrySize) {
super.entrySize(entrySize);
return this;
}
/**
* 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 ChronicleMapBuilder<K, V> entryAndValueAlignment(Alignment alignment) {
super.entryAndValueAlignment(alignment);
return this;
}
public ChronicleMapBuilder<K, V> entries(long entries) {
super.entries(entries);
return this;
}
public ChronicleMapBuilder<K, V> replicas(int replicas) {
super.replicas(replicas);
return this;
}
public ChronicleMapBuilder<K, V> actualEntriesPerSegment(int actualEntriesPerSegment) {
super.actualEntriesPerSegment(actualEntriesPerSegment);
return this;
}
public ChronicleMapBuilder<K, V> actualSegments(int actualSegments) {
super.actualSegments(actualSegments);
return this;
}
/**
* Not supported yet.
*
* @param transactional if the built map should be transactional
* @return this {@code SharedHashMapBuilder} back
*/
public ChronicleMapBuilder<K, V> transactional(boolean transactional) {
super.transactional(transactional);
return this;
}
public ChronicleMapBuilder kClass(Class kClass) {
super.kClass(kClass);
return this;
}
public SharedHashMapBuilder<K, V> file(File file) {
super.file(file);
return this;
}
public ChronicleMapBuilder lockTimeOutMS(long lockTimeOutMS) {
super.lockTimeOutMS(lockTimeOutMS);
return this;
}
public ChronicleMapBuilder errorListener(SharedMapErrorListener errorListener) {
super.errorListener(errorListener);
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.
*
* @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 ChronicleMapBuilder putReturnsNull(boolean putReturnsNull) {
super.putReturnsNull(putReturnsNull);
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.
*
* @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 ChronicleMapBuilder removeReturnsNull(boolean removeReturnsNull) {
super.removeReturnsNull(removeReturnsNull);
return this;
}
public ChronicleMapBuilder generatedValueType(boolean generatedValueType) {
super.generatedValueType(generatedValueType);
return this;
}
public ChronicleMapBuilder largeSegments(boolean largeSegments) {
super.largeSegments(largeSegments);
return this;
}
public ChronicleMapBuilder metaDataBytes(int metaDataBytes) {
super.metaDataBytes(metaDataBytes);
return this;
}
public ChronicleMapBuilder canReplicate(boolean canReplicate) {
super.canReplicate(canReplicate);
return this;
}
public ChronicleMapBuilder timeProvider(TimeProvider timeProvider) {
super.timeProvider(timeProvider);
return this;
}
public ChronicleMapBuilder identifier(byte identifier) {
super.identifier(identifier);
return this;
}
public ChronicleMapBuilder tcpReplicatorBuilder(TcpReplicatorBuilder tcpReplicatorBuilder) {
super.tcpReplicatorBuilder(tcpReplicatorBuilder);
return this;
}
public ChronicleMapBuilder udpReplicatorBuilder(UdpReplicatorBuilder udpReplicatorBuilder) {
super.udpReplicatorBuilder(udpReplicatorBuilder);
return this;
}
public ChronicleMapBuilder bytesMarshallerFactory(BytesMarshallerFactory bytesMarshallerFactory) {
super.bytesMarshallerFactory(bytesMarshallerFactory);
return this;
}
public ChronicleMapBuilder objectSerializer(ObjectSerializer objectSerializer) {
super.objectSerializer(objectSerializer);
return this;
}
public ChronicleMapBuilder externalReplicatorBuilder(@NotNull ExternalReplicatorBuilder externalReplicatorBuilder) {
super.externalReplicatorBuilder(externalReplicatorBuilder);
return this;
}
}
@@ -1,150 +0,0 @@
/*
* 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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
/**
* @author Rob Austin.
*/
public class ChronicleMapBuilder0<K, V> extends SharedHashMapBuilder<K, V> implements Cloneable {
private static final Logger LOG = LoggerFactory.getLogger(ChronicleMapBuilder0.class.getName());
public ChronicleMapBuilder0(Class<K> kClass, Class<V> vClass) {
super(kClass, vClass);
}
public static <K, V> ChronicleMapBuilder0<K, V> of(Class<K> kClass, Class<V> vClass) {
return new ChronicleMapBuilder0<K, V>(kClass, vClass);
}
/**
* Not supported yet.
*
* @param transactional if the built map should be transactional
* @return this {@code SharedHashMapBuilder} back
*/
public ChronicleMapBuilder0<K, V> transactional(boolean transactional) {
this.transactional = transactional;
return this;
}
public boolean transactional() {
return transactional;
}
public ChronicleMapBuilder0 kClass(Class kClass) {
this.kClass = kClass;
return this;
}
public ChronicleMapBuilder0 vClass(Class vClass) {
this.vClass = vClass;
return this;
}
public ChronicleMapBuilder0<K, V> file(File file) {
this.file = file;
return this;
}
public ChronicleMap<K, V> create() throws IOException {
super.create();
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");
ChronicleMapBuilder0<K, V> builder = toBuilder();
if (!canReplicate())
return new ChronicleMap0<K, V>(builder, file, kClass, vClass);
if (identifier <= 0)
throw new IllegalArgumentException("Identifier must be positive, " + identifier + " given");
final ChronicleReplicatedMap<K, V> result =
new ChronicleReplicatedMap<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;
}
ChronicleMapBuilder0<K, V> toBuilder() throws IOException {
ChronicleMapBuilder0<K, V> 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;
}
@Override
public ChronicleMapBuilder0<K, V> clone() {
return (ChronicleMapBuilder0<K, V>) super.clone();
}
}
@@ -1,80 +0,0 @@
/*
* 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.util.Collection;
import java.util.Iterator;
/**
* @author Rob Austin.
*/
public interface CollectionView<K, V, E> {
AbstractVanillaSharedMap<K, V> getMap();
/**
* Removes all of the elements from this view, by removing all the mappings from the map backing this
* view.
*/
void clear();
int size();
boolean isEmpty();
// implementations below rely on concrete classes supplying these
// abstract methods
/**
* Returns an iterator over the elements in this collection.
*
* <p>The returned iterator is <a href="package-summary.html#Weakly"><i>weakly consistent</i></a>.
*
* @return an iterator over the elements in this collection
*/
Iterator<E> iterator();
boolean contains(Object o);
boolean remove(Object o);
static final String oomeMsg = "Required array size too large";
Object[] toArray();
@SuppressWarnings("unchecked")
<T> T[] toArray(T[] a);
/**
* Returns a string representation of this collection. The string representation consists of the string
* representations of the collection's elements in the order they are returned by its iterator, enclosed
* in square brackets ({@code "[]"}). Adjacent elements are separated by the characters {@code ", "}
* (comma and space). Elements are converted to strings as by {@link String#valueOf(Object)}.
*
* @return a string representation of this collection
*/
String toString();
boolean containsAll(Collection<?> c);
boolean removeAll(Collection<?> c);
boolean retainAll(Collection<?> c);
}
File diff suppressed because it is too large Load Diff
@@ -1,177 +0,0 @@
/*
* 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.util.Map;
import java.util.function.*;
/**
* @author Rob Austin.
*/
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
*/
net.openhft.collections.KeySetView<K, V> keySet();
long mappingCount();
net.openhft.collections.KeySetView<K, V> keySet(V mappedValue);
void forEach(long parallelismThreshold,
BiConsumer<? super K, ? super V> action);
<U> void forEach(long parallelismThreshold,
BiFunction<? super K, ? super V, ? extends U> transformer,
Consumer<? super U> action);
<U> U search(long parallelismThreshold,
BiFunction<? super K, ? super V, ? extends U> searchFunction);
<U> U reduce(long parallelismThreshold,
BiFunction<? super K, ? super V, ? extends U> transformer,
BiFunction<? super U, ? super U, ? extends U> reducer);
double reduceToDouble(long parallelismThreshold,
ToDoubleBiFunction<? super K, ? super V> transformer,
double basis,
DoubleBinaryOperator reducer);
long reduceToLong(long parallelismThreshold,
ToLongBiFunction<? super K, ? super V> transformer,
long basis,
LongBinaryOperator reducer);
int reduceToInt(long parallelismThreshold,
ToIntBiFunction<? super K, ? super V> transformer,
int basis,
IntBinaryOperator reducer);
void forEachKey(long parallelismThreshold,
Consumer<? super K> action);
<U> void forEachKey(long parallelismThreshold,
Function<? super K, ? extends U> transformer,
Consumer<? super U> action);
<U> U searchKeys(long parallelismThreshold,
Function<? super K, ? extends U> searchFunction);
K reduceKeys(long parallelismThreshold,
BiFunction<? super K, ? super K, ? extends K> reducer);
<U> U reduceKeys(long parallelismThreshold,
Function<? super K, ? extends U> transformer,
BiFunction<? super U, ? super U, ? extends U> reducer);
double reduceKeysToDouble(long parallelismThreshold,
ToDoubleFunction<? super K> transformer,
double basis,
DoubleBinaryOperator reducer);
long reduceKeysToLong(long parallelismThreshold,
ToLongFunction<? super K> transformer,
long basis,
LongBinaryOperator reducer);
int reduceKeysToInt(long parallelismThreshold,
ToIntFunction<? super K> transformer,
int basis,
IntBinaryOperator reducer);
void forEachValue(long parallelismThreshold,
Consumer<? super V> action);
<U> void forEachValue(long parallelismThreshold,
Function<? super V, ? extends U> transformer,
Consumer<? super U> action);
<U> U searchValues(long parallelismThreshold,
Function<? super V, ? extends U> searchFunction);
V reduceValues(long parallelismThreshold,
BiFunction<? super V, ? super V, ? extends V> reducer);
<U> U reduceValues(long parallelismThreshold,
Function<? super V, ? extends U> transformer,
BiFunction<? super U, ? super U, ? extends U> reducer);
double reduceValuesToDouble(long parallelismThreshold,
ToDoubleFunction<? super V> transformer,
double basis,
DoubleBinaryOperator reducer);
long reduceValuesToLong(long parallelismThreshold,
ToLongFunction<? super V> transformer,
long basis,
LongBinaryOperator reducer);
int reduceValuesToInt(long parallelismThreshold,
ToIntFunction<? super V> transformer,
int basis,
IntBinaryOperator reducer);
void forEachEntry(long parallelismThreshold,
Consumer<? super Map.Entry<K, V>> action);
<U> void forEachEntry(long parallelismThreshold,
Function<Map.Entry<K, V>, ? extends U> transformer,
Consumer<? super U> action);
<U> U searchEntries(long parallelismThreshold,
Function<Map.Entry<K, V>, ? extends U> searchFunction);
Map.Entry<K, V> reduceEntries(long parallelismThreshold,
BiFunction<Map.Entry<K, V>, Map.Entry<K, V>, ? extends Map.Entry<K, V>> reducer);
<U> U reduceEntries(long parallelismThreshold,
Function<Map.Entry<K, V>, ? extends U> transformer,
BiFunction<? super U, ? super U, ? extends U> reducer);
double reduceEntriesToDouble(long parallelismThreshold,
ToDoubleFunction<Map.Entry<K, V>> transformer,
double basis,
DoubleBinaryOperator reducer);
long reduceEntriesToLong(long parallelismThreshold,
ToLongFunction<Map.Entry<K, V>> transformer,
long basis,
LongBinaryOperator reducer);
int reduceEntriesToInt(long parallelismThreshold,
ToIntFunction<Map.Entry<K, V>> transformer,
int basis,
IntBinaryOperator reducer);
}
@@ -1,90 +0,0 @@
/*
* 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.util.Collection;
import java.util.Iterator;
import java.util.Set;
import java.util.Spliterator;
import java.util.function.Consumer;
/**
* @author Rob Austin.
*/
public interface KeySetView<K, V> extends Collection<K>, java.io.Serializable, Set<K>, CollectionView<K, V,
K> {
/**
* Returns the default mapped value for additions, or {@code null} if additions are not supported.
*
* @return the default mapped value for additions, or {@code null} if not supported
*/
V getMappedValue();
/**
* {@inheritDoc}
*
* @throws NullPointerException if the specified key is null
*/
boolean contains(Object o);
/**
* Removes the key from this map view, by removing the key (and its corresponding value) from the backing
* map. This method does nothing if the key is not in the map.
*
* @param o the key to be removed from the backing map
* @return {@code true} if the backing map contained the specified key
* @throws NullPointerException if the specified key is null
*/
boolean remove(Object o);
/**
* @return an iterator over the keys of the backing map
*/
Iterator<K> iterator();
/**
* Adds the specified key to this set view by mapping the key to the default mapped value in the backing
* map, if defined.
*
* @param e key to be added
* @return {@code true} if this set changed as a result of the call
* @throws NullPointerException if the specified key is null
* @throws UnsupportedOperationException if no default mapped value for additions was provided
*/
boolean add(K e);
/**
* Adds all of the elements in the specified collection to this set, as if by calling {@link #add} on each
* one.
*
* @param c the elements to be inserted into this set
* @return {@code true} if this set changed as a result of the call
* @throws NullPointerException if the collection or any of its elements are {@code null}
* @throws UnsupportedOperationException if no default mapped value for additions was provided
*/
boolean addAll(Collection<? extends K> c);
int hashCode();
boolean equals(Object o);
Spliterator<K> spliterator();
void forEach(Consumer<? super K> action);
}
@@ -292,7 +292,7 @@ public class SharedHashMapBuilder<K, V> implements Cloneable {
SharedHashMapBuilder<K, V> builder = toBuilder();
if (!canReplicate())
return new VanillaSharedMap<K, V>(builder, file, kClass, vClass);
return new VanillaSharedHashMap<K, V>(builder, file, kClass, vClass);
if (identifier <= 0)
throw new IllegalArgumentException("Identifier must be positive, " + identifier + " given");
@@ -1,36 +0,0 @@
/*
* 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 java.io.File;
import java.io.IOException;
/**
* @author Rob Austin.
*/
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
@@ -18,6 +18,7 @@
package net.openhft.collections;
import net.openhft.chronicle.map.ChronicleMap;
import net.openhft.lang.Maths;
import net.openhft.lang.collection.ATSDirectBitSet;
import net.openhft.lang.io.*;
@@ -79,8 +80,8 @@ import static net.openhft.lang.collection.DirectBitSet.NOT_FOUND;
* @param <K> the entries key type
* @param <V> the entries value type
*/
class VanillaSharedReplicatedMap<K, V> extends AbstractVanillaSharedMap<K, V>
implements SharedHashMap<K, V>, ReplicaExternalizable<K, V>, EntryResolver<K, V>,
class VanillaSharedReplicatedMap<K, V> extends AbstractVanillaSharedHashMap<K, V>
implements ChronicleMap<K, V>, ReplicaExternalizable<K, V>, EntryResolver<K, V>,
Closeable {
static final int MAX_UNSIGNED_SHORT = Character.MAX_VALUE;
@@ -129,7 +130,7 @@ class VanillaSharedReplicatedMap<K, V> extends AbstractVanillaSharedMap<K, V>
}
@Override
VanillaSharedMap<K, V>.Segment createSegment(NativeBytes bytes, int index) {
VanillaSharedHashMap<K, V>.Segment createSegment(NativeBytes bytes, int index) {
return new Segment(bytes, index);
}
@@ -374,7 +375,7 @@ class VanillaSharedReplicatedMap<K, V> extends AbstractVanillaSharedMap<K, V>
}
class Segment extends VanillaSharedMap<K, V>.Segment {
class Segment extends VanillaSharedHashMap<K, V>.Segment {
private volatile IntIntMultiMap hashLookupLiveAndDeleted;
private volatile IntIntMultiMap hashLookupLiveOnly;
@@ -408,7 +409,7 @@ class VanillaSharedReplicatedMap<K, V> extends AbstractVanillaSharedMap<K, V>
/**
* @see VanillaSharedMap.Segment#acquire(net.openhft.lang.io.Bytes, Object, Object, int, boolean)
* @see net.openhft.collections.VanillaSharedHashMap.Segment#acquire(net.openhft.lang.io.Bytes, Object, Object, int, boolean)
*/
V acquire(Bytes keyBytes, K key, V usingValue, int hash2, boolean create, long timestamp) {
lock();
@@ -691,7 +692,7 @@ class VanillaSharedReplicatedMap<K, V> extends AbstractVanillaSharedMap<K, V>
* put)</s> this method is called only from usual put or acquire
* @param searchedHashLookup the hash lookup that used to find the entry based on the key
* @return offset of the written entry in the Segment bytes
* @see VanillaSharedMap.Segment#putEntry(net.openhft.lang.io.Bytes, Object, boolean)
* @see net.openhft.collections.VanillaSharedHashMap.Segment#putEntry(net.openhft.lang.io.Bytes, Object, boolean)
*/
private long putEntry(Bytes keyBytes, int hash2, V value, boolean usingValue,
final int identifier, final long timestamp,
@@ -739,7 +740,7 @@ class VanillaSharedReplicatedMap<K, V> extends AbstractVanillaSharedMap<K, V>
}
/**
* @see VanillaSharedMap.Segment#remove(net.openhft.lang.io.Bytes, Object, Object, int)
* @see net.openhft.collections.VanillaSharedHashMap.Segment#remove(net.openhft.lang.io.Bytes, Object, Object, int)
*/
public V remove(Bytes keyBytes, K key, V expectedValue, int hash2,
final long timestamp, final byte identifier) {
@@ -810,7 +811,7 @@ class VanillaSharedReplicatedMap<K, V> extends AbstractVanillaSharedMap<K, V>
}
/**
* @see VanillaSharedMap.Segment#remove(net.openhft.lang.io.Bytes, Object, Object, int)
* @see net.openhft.collections.VanillaSharedHashMap.Segment#remove(net.openhft.lang.io.Bytes, Object, Object, int)
*/
public V replace(Bytes keyBytes, K key, V expectedValue, V newValue, int hash2,
long timestamp) {