HCOLL-81 made publicly a single ChronicleMapBuilder, that delegates onto internal implementations

This commit is contained in:
Rob Austin
2014-11-15 09:05:26 +00:00
parent 88df95fc76
commit 49fc123755
54 changed files with 1373 additions and 524 deletions
@@ -57,7 +57,7 @@ import static net.openhft.chronicle.map.Objects.builderEquals;
/**
* {@code AbstractChronicleMapBuilder} manages most of {@link ChronicleMap} configurations; has two concrete
* subclasses: {@link ChronicleMapOnHeapUpdatableBuilder} should be used to create maps with ordinary values, {@link
* subclasses: {@link OnHeapUpdatableChronicleMapBuilder} should be used to create maps with ordinary values, {@link
* ChronicleMapBuilder} -- maps with {@link Byteable} values, which point directly to off-heap
* memory; could be used as a classic builder and/or factory. This means
* that in addition to the standard builder usage pattern: <pre>{@code
@@ -66,7 +66,7 @@ import static net.openhft.chronicle.map.Objects.builderEquals;
* .entries(100500)
* // ... other configurations
* .create();}</pre>
* one of concrete {@code AbstractChronicleMapBuilder} subclasses, {@link ChronicleMapOnHeapUpdatableBuilder} or {@link
* one of concrete {@code AbstractChronicleMapBuilder} subclasses, {@link OnHeapUpdatableChronicleMapBuilder} or {@link
* ChronicleMapBuilder}, could be prepared and used to create many similar
* maps: <pre>{@code
* ChronicleMapBuilder<Key, Value> builder = ChronicleMapBuilder
@@ -227,7 +227,7 @@ public abstract class AbstractChronicleMapBuilder<K, V,
*
* <p>For example, if your keys are Git commit hashes:<pre>{@code
* Map<byte[], String> gitCommitMessagesByHash =
* ChronicleMapOnHeapUpdatableBuilder.of(byte[].class, String.class)
* ChronicleMapBuilder.of(byte[].class, String.class)
* .constantKeySizeBySample(new byte[20])
* .create();}</pre>
*
@@ -839,12 +839,10 @@ public abstract class AbstractChronicleMapBuilder<K, V,
}
/**
* Only for testing. Shouldn't be called concurrently on a single builder instance.
*
* @param identifier id
* @return map
*/
ChronicleMap<K, V> createReplicated(byte identifier) throws IOException {
public ChronicleMap<K, V> createReplicated(byte identifier) throws IOException {
preMapConstruction(true);
VanillaChronicleMap<K, ?, ?, V, ?, ?> map =
new ReplicatedChronicleMap<K, Object, MetaBytesInterop<K, Object>,
@@ -93,7 +93,7 @@ public interface ChronicleMap<K, V> extends ConcurrentMap<K, V>, ChronicleHash {
* for the key
* @see #get(Object)
* @see #acquireUsing(Object, Object)
* @see ChronicleMapOnHeapUpdatableBuilder#valueMarshaller(BytesMarshaller)
* @see OnHeapUpdatableChronicleMapBuilder#valueMarshaller(BytesMarshaller)
*/
V getUsing(K key, V usingValue);
@@ -134,7 +134,7 @@ public interface ChronicleMap<K, V> extends ConcurrentMap<K, V>, ChronicleHash {
* no mapping for the key
* @see #get(Object)
* @see #getUsing(Object, Object)
* @see ChronicleMapOnHeapUpdatableBuilder#valueMarshaller(BytesMarshaller)
* @see OnHeapUpdatableChronicleMapBuilder#valueMarshaller(BytesMarshaller)
*/
@NotNull
ReadContext<K, V> getUsingLocked(@NotNull K key, @NotNull V usingValue);
@@ -1,80 +1,109 @@
/*
* Copyright 2014 Higher Frequency Trading http://www.higherfrequencytrading.com
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.openhft.chronicle.map;
import net.openhft.chronicle.hash.ChronicleHashErrorListener;
import net.openhft.chronicle.hash.ChronicleHashInstanceConfig;
import net.openhft.chronicle.hash.StatelessClientConfig;
import net.openhft.chronicle.hash.replication.SingleChronicleHashReplication;
import net.openhft.chronicle.hash.replication.TcpTransportAndNetworkConfig;
import net.openhft.chronicle.hash.replication.TimeProvider;
import net.openhft.chronicle.hash.serialization.BytesReader;
import net.openhft.chronicle.hash.serialization.BytesWriter;
import net.openhft.chronicle.hash.serialization.SizeMarshaller;
import net.openhft.lang.io.serialization.BytesMarshaller;
import net.openhft.lang.io.serialization.BytesMarshallerFactory;
import net.openhft.lang.io.serialization.ObjectFactory;
import net.openhft.lang.io.serialization.ObjectSerializer;
import net.openhft.lang.model.Byteable;
import net.openhft.lang.model.DataValueClasses;
import org.jetbrains.annotations.NotNull;
public final class ChronicleMapBuilder<K, V>
extends AbstractChronicleMapBuilder<K, V, ChronicleMapBuilder<K, V>> {
import java.io.File;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.concurrent.TimeUnit;
/**
* {@code ChronicleMapBuilder} is intended to be used to create {@link ChronicleMap}s
*
* @param <K> key type of the maps, created by this builder
* @param <V> value type of the maps, created by this builder
* @see AbstractChronicleMapBuilder
* @see net.openhft.chronicle.hash.ChronicleHashBuilder
*/
public final class ChronicleMapBuilder<K, V> implements ChronicleMapBuilderI<K, V> {
final ChronicleMapBuilderI<K, V> delegate;
/**
* Returns a new {@code ChronicleMapBuilder} instance which is able to {@linkplain #create() create} maps
* with the specified key and value classes.
*
* <p>{@code ChronicleMapBuilder} analyzes provided key and value classes and automatically chooses the
* most specific internal builder.
*
* @param keyClass class object used to infer key type and discover it's properties via reflection
* @param valueClass class object used to infer value type and discover it's properties via reflection
* @param <K> key type of the maps, created by the returned builder
* @param <V> value type of the maps, created by the returned builder
* @return a new builder for the given key and value classes
*/
public static <K, V> ChronicleMapBuilder<K, V> of(
@NotNull Class<K> keyClass, @NotNull Class<V> valueClass) {
if (valueClass.isEnum())
return new ChronicleMapBuilder<K, V>(OnHeapUpdatableChronicleMapBuilder.of(keyClass, valueClass));
if (keyClass.isInterface() && keyClass != CharSequence.class) {
keyClass = DataValueClasses.directClassFor(keyClass);
}
if (valueClass.isInterface()) {
if ((valueClass.isInterface() && valueClass != CharSequence.class)) {
valueClass = DataValueClasses.directClassFor(valueClass);
} else if (!offHeapReference(valueClass)) {
throw new IllegalArgumentException(
"Value class should be either Byteable subclass or interface," +
"allowing direct Byteable implementation generation");
return new ChronicleMapBuilder<K, V>(OnHeapUpdatableChronicleMapBuilder.of(keyClass, valueClass));
}
return new ChronicleMapBuilder<K, V>(keyClass, valueClass);
OffHeapUpdatableChronicleMapBuilder<K, V> kvOffHeapUpdatableChronicleMapBuilder = new
OffHeapUpdatableChronicleMapBuilder<K, V>(keyClass, valueClass);
return new ChronicleMapBuilder<K, V>(kvOffHeapUpdatableChronicleMapBuilder);
}
ChronicleMapBuilder(Class<K> keyClass, Class<V> valueClass) {
super(keyClass, valueClass);
prepareValueBytesOnAcquire(new ZeroOutValueBytes<K, V>(valueSize()));
ChronicleMapBuilder(ChronicleMapBuilderI<K, V> delegate) {
this.delegate = delegate;
}
@Override
ChronicleMapBuilder<K, V> self() {
return this;
private static boolean offHeapReference(Class valueClass) {
return Byteable.class.isAssignableFrom(valueClass);
}
/**
* {@inheritDoc} With respect to {@linkplain #entryAndValueAlignment(Alignment) alignment}.
*
* <p>Note that the actual entrySize will be aligned to 4 (default {@linkplain
* #entryAndValueAlignment(Alignment) entry alignment}). I. e. if you set entry size to 30,
* and entry alignment is set to {@link Alignment#OF_4_BYTES}, the actual entry size will be 32
* (30 aligned to 4 bytes).
* #entryAndValueAlignment(Alignment) entry alignment}). I. e. if you set entry size to 30, and entry
* alignment is set to {@link Alignment#OF_4_BYTES}, the actual entry size will be 32 (30 aligned to 4
* bytes).
*
* @see #entryAndValueAlignment(Alignment)
* @see #entries(long)
* @see #entryAndValueAlignment(Alignment) // * @see #entries(long)
*/
@Override
public ChronicleMapBuilder<K, V> entrySize(int entrySize) {
return super.entrySize(entrySize);
delegate.entrySize(entrySize);
return this;
}
/**
* Configures alignment strategy of address in memory of entries and independently of address in
* memory of values within entries in ChronicleMaps, created by this builder.
* Configures alignment strategy of address in memory of entries and independently of address in memory of
* values within entries in ChronicleMaps, created by this builder.
*
* <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>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>Note that {@linkplain #entrySize(int) entry size} will be aligned according to this
* alignment. I. e. if you set {@code entrySize(20)} and {@link Alignment#OF_8_BYTES}, actual
* entry size will be 24 (20 aligned to 8 bytes).
* <p>Note that {@linkplain #entrySize(int) entry size} will be aligned according to this alignment. I. e.
* if you set {@code entrySize(20)} and {@link Alignment#OF_8_BYTES}, actual entry size will be 24 (20
* aligned to 8 bytes).
*
* <p>Default is {@link Alignment#OF_4_BYTES} for Byteable values.
*
@@ -83,58 +112,369 @@ public final class ChronicleMapBuilder<K, V>
*/
@Override
public ChronicleMapBuilder<K, V> entryAndValueAlignment(Alignment alignment) {
return super.entryAndValueAlignment(alignment);
delegate.entryAndValueAlignment(alignment);
return this;
}
/**
* {@inheritDoc} Also, it overrides any previous {@link #prepareValueBytesOnAcquire}
* configuration to this {@code ChronicleMapBuilder}.
* {@inheritDoc} Also, it overrides any previous {@link #prepareValueBytesOnAcquire} configuration to this
* {@code ChronicleMapBuilder}.
*
* <p>By default, the default value is not specified, default {@linkplain
* #prepareValueBytesOnAcquire prepare value bytes routine} is specified instead.
* <p>By default, the default value is not specified, default {@linkplain #prepareValueBytesOnAcquire
* prepare value bytes routine} is specified instead.
*
* @see #defaultValueProvider(DefaultValueProvider)
* @see #prepareValueBytesOnAcquire(PrepareValueBytes)
*/
@Override
public ChronicleMapBuilder<K, V> defaultValue(V defaultValue) {
return super.defaultValue(defaultValue);
delegate.defaultValue(defaultValue);
return this;
}
/**
* {@inheritDoc} Also, it overrides any previous {@link #prepareValueBytesOnAcquire}
* configuration to this {@code ChronicleMapBuilder}.
*
* <p>By default, the default value provider is not specified, default {@linkplain
* #prepareValueBytesOnAcquire prepare value bytes routine} is specified instead.
*
* @see #defaultValue(Object)
* @see #prepareValueBytesOnAcquire(PrepareValueBytes)
*/
@Override
public ChronicleMapBuilder<K, V> defaultValueProvider(
@NotNull DefaultValueProvider<K, V> defaultValueProvider) {
return super.defaultValueProvider(defaultValueProvider);
}
/**
* Configures the procedure which is called on the bytes, which later the returned value is
* pointing to, if the key is absent, on {@link ChronicleMap#acquireUsing(Object, Object)
* acquireUsing()} call on maps, created by this builder. See {@link PrepareValueBytes} for
* more information.
* Configures the procedure which is called on the bytes, which later the returned value is pointing to,
* if the key is absent, on {@link ChronicleMap#acquireUsing(Object, Object) acquireUsing()} call on maps,
* created by this builder. See {@link PrepareValueBytes} for more information.
*
* <p>The default preparation callback zeroes out the value bytes.
*
* @param prepareValueBytes what to do with the value bytes before assigning them into the
* {@link Byteable} value to return from {@code acquireUsing()} call
* @param prepareValueBytes what to do with the value bytes before assigning them into the {@link
* net.openhft.lang.model.Byteable} value to return from {@code acquireUsing()}
* call
* @return this builder back
* @see PrepareValueBytes
* @see #defaultValue(Object)
* @see #defaultValueProvider(DefaultValueProvider)
*/
@Override
public ChronicleMapBuilder<K, V> prepareValueBytesOnAcquire(
@NotNull PrepareValueBytes<K, V> prepareValueBytes) {
return super.prepareValueBytesOnAcquire(prepareValueBytes);
public ChronicleMapBuilder<K, V> prepareValueBytesOnAcquire(@NotNull PrepareValueBytes<K, V> prepareValueBytes) {
delegate.prepareValueBytesOnAcquire(prepareValueBytes);
return this;
}
@Override
public ChronicleMapBuilder<K, V> keySize(int keySize) {
delegate.keySize(keySize);
return this;
}
@Override
public ChronicleMapBuilder<K, V> constantKeySizeBySample(K sampleKey) {
delegate.constantKeySizeBySample(sampleKey);
return this;
}
/**
* Configures the optimal number of bytes, taken by serialized form of values, put into maps, created by
* this builder. If value size is always the same, call {@link #constantValueSizeBySample(Object)} method
* instead of this one.
*
* <p>If value is a boxed primitive type, i. e. if value size is known statically, it is automatically
* accounted and shouldn't be specified by user.
*
* <p>If value size varies moderately, specify the size higher than average, but lower than the maximum
* possible, to minimize average memory overuse. If value size varies in a wide range, it's better to use
* {@linkplain #entrySize(int) entry size} in "chunk" mode and configure it directly.
*
* @param valueSize number of bytes, taken by serialized form of values
* @return this {@code ChronicleMapOnHeapUpdatableBuilder} back
* @see #constantValueSizeBySample(Object)
* @see #keySize(int)
* @see #entrySize(int)
*/
@Override
public ChronicleMapBuilder<K, V> valueSize(int valueSize) {
delegate.valueSize(valueSize);
return this;
}
/**
* Configures the constant number of bytes, taken by serialized form of values, put into maps, created by
* this builder. This is done by providing the {@code sampleValue}, all values should take the same number
* of bytes in serialized form, as this sample object.
*
* <p>If values are of boxed primitive type or {@link net.openhft.lang.model.Byteable} subclass, i. e. if
* value size is known statically, it is automatically accounted and this method shouldn't be called.
*
* <p>If value size varies, method {@link #valueSize(int)} or {@link #entrySize(int)} should be called
* instead of this one.
*
* @param sampleValue the sample value
* @return this builder back
* @see #valueSize(int)
* @see #constantKeySizeBySample(Object)
*/
@Override
public ChronicleMapBuilder<K, V> constantValueSizeBySample(@NotNull V sampleValue) {
delegate.constantValueSizeBySample(sampleValue);
return this;
}
/**
* {@inheritDoc}
*
* <p>Example: <pre>{@code Map<Key, Value> map =
* ChronicleMapBuilder.of(Key.class, Value.class)
* .entries(1_000_000)
* .keySize(50).valueSize(200)
* // this class hasn't implemented yet, just for example
* .objectSerializer(new KryoObjectSerializer())
* .create();}</pre>
*/
@Override
public ChronicleMapBuilder<K, V> objectSerializer(@NotNull ObjectSerializer objectSerializer) {
delegate.objectSerializer(objectSerializer);
return this;
}
/**
* {@inheritDoc}
*
* @throws IllegalStateException if custom value marshaller is specified or value class is not either
* {@code BytesMarshallable} or {@code Externalizable}
*/
@Override
public ChronicleMapBuilder<K, V> valueDeserializationFactory(@NotNull ObjectFactory<V> valueDeserializationFactory) {
delegate.valueDeserializationFactory(valueDeserializationFactory);
return this;
}
/**
* {@inheritDoc}
*
* <p>By default, default value provider is not specified, {@link #defaultValue(Object) default value} is
* specified instead.
*
* @see #defaultValue(Object)
*/
@Override
public ChronicleMapBuilder<K, V> defaultValueProvider(@NotNull DefaultValueProvider<K, V> defaultValueProvider) {
delegate.defaultValueProvider(defaultValueProvider);
return this;
}
/**
* Configures the {@code BytesMarshaller} used to serialize/deserialize values to/from off-heap memory in
* maps, created by this builder. See <a href="https://github.com/OpenHFT/Chronicle-Map#serialization">the
* section about serialization in ChronicleMap manual</a> for more information.
*
* @param valueMarshaller the marshaller used to serialize values
* @return this builder back
* @see #valueMarshallers(net.openhft.chronicle.hash.serialization.BytesWriter,
* net.openhft.chronicle.hash.serialization.BytesReader)
* @see ChronicleMapBuilder
* @see #objectSerializer(ObjectSerializer)
*/
@Override
public ChronicleMapBuilder<K, V> valueMarshaller(
@NotNull BytesMarshaller<V> valueMarshaller) {
delegate.valueMarshaller(valueMarshaller);
return this;
}
/**
* Configures the marshallers, used to serialize/deserialize values to/from off-heap memory in maps,
* created by this builder. See <a href="https://github.com/OpenHFT/Chronicle-Map#serialization">the
* section about serialization in ChronicleMap manual</a> for more information.
*
* <p>Configuring marshalling this way results to a little bit more compact in-memory layout of the map,
* comparing to a single interface configuration: {@link #valueMarshaller(BytesMarshaller)}.
*
* <p>Passing {@link net.openhft.chronicle.hash.serialization.BytesInterop} instead of plain {@link
* net.openhft.chronicle.hash.serialization.BytesWriter} is, of cause, possible, but currently pointless
* for values.
*
* @param valueWriter the new value object &rarr; {@link net.openhft.lang.io.Bytes} writer (interop)
* strategy
* @param valueReader the new {@link net.openhft.lang.io.Bytes} &rarr; value object reader strategy
* @return this builder back
* @see #valueMarshaller(BytesMarshaller)
*/
@Override
public ChronicleMapBuilder<K, V> valueMarshallers(@NotNull BytesWriter<V> valueWriter, @NotNull BytesReader<V> valueReader) {
delegate.valueMarshallers(valueWriter, valueReader);
return this;
}
/**
* Configures the marshaller used to serialize actual value sizes to off-heap memory in maps, created by
* this builder.
*
* <p>Default value size marshaller is so-called {@linkplain net.openhft.chronicle.hash.serialization.SizeMarshallers#stopBit()
* stop bit encoding marshalling}. If {@linkplain #constantValueSizeBySample(Object) constant value size}
* is configured, or defaulted if the value type is always constant and {@code ChronicleHashBuilder}
* implementation knows about it, this configuration takes no effect, because a special {@link
* net.openhft.chronicle.hash.serialization.SizeMarshaller} implementation, which doesn't actually do any
* marshalling, and just returns the known constant size on {@link net.openhft.chronicle.hash.serialization.SizeMarshaller#readSize(
*net.openhft.lang.io.Bytes)} calls, is used instead of any {@code SizeMarshaller} configured using this
* method.
*
* @param valueSizeMarshaller the new marshaller, used to serialize actual value sizes to off-heap memory
* @return this builder back
*/
@Override
public ChronicleMapBuilder<K, V> valueSizeMarshaller(@NotNull SizeMarshaller valueSizeMarshaller) {
delegate.valueSizeMarshaller(valueSizeMarshaller);
return this;
}
@Override
public ChronicleMapBuilder<K, V> entries(long l) {
delegate.entries(l);
return this;
}
@Override
public ChronicleMapBuilder<K, V> replication(SingleChronicleHashReplication withId) {
delegate.replication(withId);
return this;
}
@Override
public ChronicleMap create() throws IOException {
return delegate.create();
}
@Override
public ChronicleMapBuilder<K, V> actualSegments(int actualSegments) {
delegate.actualSegments(actualSegments);
return this;
}
@Override
public ChronicleMapBuilder<K, V> minSegments(int minSegments) {
delegate.minSegments(minSegments);
return this;
}
@Override
public ChronicleMapBuilder<K, V> actualEntriesPerSegment(long actualEntriesPerSegment) {
delegate.actualEntriesPerSegment(actualEntriesPerSegment);
return this;
}
@Override
public ChronicleMapBuilder<K, V> lockTimeOut(long lockTimeOut, TimeUnit unit) {
delegate.lockTimeOut(lockTimeOut, unit);
return this;
}
@Override
public ChronicleMapBuilder<K, V> errorListener(ChronicleHashErrorListener errorListener) {
delegate.errorListener(errorListener);
return this;
}
@Override
public ChronicleMapBuilder<K, V> metaDataBytes(int metaDataBytes) {
delegate.metaDataBytes(metaDataBytes);
return this;
}
@Override
public ChronicleMapBuilder<K, V> timeProvider(TimeProvider timeProvider) {
delegate.timeProvider(timeProvider);
return this;
}
@Override
public ChronicleMapBuilder<K, V> bytesMarshallerFactory(BytesMarshallerFactory bytesMarshallerFactory) {
delegate.bytesMarshallerFactory(bytesMarshallerFactory);
return this;
}
@Override
public ChronicleMapBuilder<K, V> keyMarshaller(BytesMarshaller<K> keyMarshaller) {
delegate.keyMarshaller(keyMarshaller);
return this;
}
@Override
public ChronicleMapBuilder<K, V> keyMarshallers(BytesWriter<K> keyWriter, BytesReader<K> keyReader) {
delegate.keyMarshallers(keyWriter, keyReader);
return this;
}
@Override
public ChronicleMapBuilder<K, V> keySizeMarshaller(SizeMarshaller keySizeMarshaller) {
delegate.keySizeMarshaller(keySizeMarshaller);
return this;
}
@Override
public ChronicleMapBuilder<K, V> keyDeserializationFactory(ObjectFactory<K> keyDeserializationFactory) {
delegate.keyDeserializationFactory(keyDeserializationFactory);
return this;
}
@Override
public ChronicleMapBuilder<K, V> immutableKeys() {
delegate.immutableKeys();
return this;
}
@Override
public StatelessClientConfig<ChronicleMap<K, V>> statelessClient(InetSocketAddress remoteAddress) {
return delegate.statelessClient(remoteAddress);
}
@Override
public ChronicleHashInstanceConfig<ChronicleMap<K, V>> instance() {
return delegate.instance();
}
@Override
public ChronicleMapBuilder<K, V> replication(byte identifier) {
delegate.replication(identifier);
return this;
}
@Override
public ChronicleMapBuilder<K, V> replication(byte identifier, TcpTransportAndNetworkConfig tcpTransportAndNetwork) {
delegate.replication(identifier, tcpTransportAndNetwork);
return this;
}
@Override
public ChronicleMapBuilder<K, V> clone() {
delegate.clone();
return this;
}
@Override
public ChronicleMapBuilder<K, V> putReturnsNull(boolean b) {
delegate.putReturnsNull(b);
return this;
}
@Override
public ChronicleMapBuilder<K, V> removeReturnsNull(boolean b) {
delegate.removeReturnsNull(b);
return this;
}
@Override
public ChronicleMapBuilder<K, V> eventListener(MapEventListener<K, V, ChronicleMap<K, V>> eventListener) {
delegate.eventListener(eventListener);
return this;
}
@Override
public ChronicleMap<K, V> createReplicated(byte b) throws IOException {
return delegate.createReplicated(b);
}
@Override
public ChronicleMap<K, V> createPersistedTo(File file) throws IOException {
return delegate.createPersistedTo(file);
}
}
@@ -0,0 +1,299 @@
package net.openhft.chronicle.map;
import net.openhft.chronicle.hash.ChronicleHashErrorListener;
import net.openhft.chronicle.hash.ChronicleHashInstanceConfig;
import net.openhft.chronicle.hash.StatelessClientConfig;
import net.openhft.chronicle.hash.replication.SingleChronicleHashReplication;
import net.openhft.chronicle.hash.replication.TcpTransportAndNetworkConfig;
import net.openhft.chronicle.hash.replication.TimeProvider;
import net.openhft.chronicle.hash.serialization.BytesReader;
import net.openhft.chronicle.hash.serialization.BytesWriter;
import net.openhft.chronicle.hash.serialization.SizeMarshaller;
import net.openhft.lang.io.serialization.BytesMarshaller;
import net.openhft.lang.io.serialization.BytesMarshallerFactory;
import net.openhft.lang.io.serialization.ObjectFactory;
import net.openhft.lang.io.serialization.ObjectSerializer;
import org.jetbrains.annotations.NotNull;
import java.io.File;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.concurrent.TimeUnit;
/**
* @author Rob Austin.
*/
interface ChronicleMapBuilderI<K, V> {
/**
* {@inheritDoc} With respect to {@linkplain #entryAndValueAlignment(Alignment) alignment}.
*
* <p>Note that the actual entrySize will be aligned to 4 (default {@linkplain
* #entryAndValueAlignment(Alignment) entry alignment}). I. e. if you set entry size to 30, and entry
* alignment is set to {@link Alignment#OF_4_BYTES}, the actual entry size will be 32 (30 aligned to 4
* bytes).
*
* @see #entryAndValueAlignment(Alignment) // * @see #entries(long)
*/
ChronicleMapBuilderI<K, V> entrySize(int entrySize);
/**
* Configures alignment strategy of address in memory of entries and independently of address in memory of
* values within entries in ChronicleMaps, created by this builder.
*
* <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>Note that {@linkplain #entrySize(int) entry size} will be aligned according to this alignment. I. e.
* if you set {@code entrySize(20)} and {@link Alignment#OF_8_BYTES}, actual entry size will be 24 (20
* aligned to 8 bytes).
*
* <p>Default is {@link Alignment#OF_4_BYTES} for Byteable values.
*
* @param alignment the new alignment of the maps constructed by this builder
* @return this {@code ChronicleMapOnHeapUpdatableBuilder} back
*/
ChronicleMapBuilderI<K, V> entryAndValueAlignment(Alignment alignment);
/**
* {@inheritDoc} Also, it overrides any previous {@link #prepareValueBytesOnAcquire} configuration to this
* {@code ChronicleMapBuilder}.
*
* <p>By default, the default value is not specified, default {@linkplain #prepareValueBytesOnAcquire
* prepare value bytes routine} is specified instead.
*
* @see #defaultValueProvider(DefaultValueProvider)
* @see #prepareValueBytesOnAcquire(PrepareValueBytes)
*/
ChronicleMapBuilderI<K, V> defaultValue(V defaultValue);
/**
* Configures the procedure which is called on the bytes, which later the returned value is pointing to,
* if the key is absent, on {@link ChronicleMap#acquireUsing(Object, Object) acquireUsing()} call on maps,
* created by this builder. See {@link PrepareValueBytes} for more information.
*
* <p>The default preparation callback zeroes out the value bytes.
*
* @param prepareValueBytes what to do with the value bytes before assigning them into the {@link
* net.openhft.lang.model.Byteable} value to return from {@code acquireUsing()}
* call
* @return this builder back
* @see PrepareValueBytes
* @see #defaultValue(Object)
* @see #defaultValueProvider(DefaultValueProvider)
*/
ChronicleMapBuilderI<K, V> prepareValueBytesOnAcquire(
@NotNull PrepareValueBytes<K, V> prepareValueBytes);
/**
* {@inheritDoc}
*
* @see #constantKeySizeBySample(Object)
* @see #valueSize(int)
* @see #entrySize(int)
*/
ChronicleMapBuilderI<K, V> keySize(int keySize);
/**
* {@inheritDoc}
*
* @see #keySize(int)
* @see #constantValueSizeBySample(Object)
*/
ChronicleMapBuilderI<K, V> constantKeySizeBySample(K sampleKey);
/**
* Configures the optimal number of bytes, taken by serialized form of values, put into maps, created by
* this builder. If value size is always the same, call {@link #constantValueSizeBySample(Object)} method
* instead of this one.
*
* <p>If value is a boxed primitive type, i. e. if value size is known statically, it is automatically
* accounted and shouldn't be specified by user.
*
* <p>If value size varies moderately, specify the size higher than average, but lower than the maximum
* possible, to minimize average memory overuse. If value size varies in a wide range, it's better to use
* {@linkplain #entrySize(int) entry size} in "chunk" mode and configure it directly.
*
* @param valueSize number of bytes, taken by serialized form of values
* @return this {@code ChronicleMapOnHeapUpdatableBuilder} back
* @see #constantValueSizeBySample(Object)
* @see #keySize(int)
* @see #entrySize(int)
*/
ChronicleMapBuilderI<K, V> valueSize(int valueSize);
/**
* Configures the constant number of bytes, taken by serialized form of values, put into maps, created by
* this builder. This is done by providing the {@code sampleValue}, all values should take the same number
* of bytes in serialized form, as this sample object.
*
* <p>If values are of boxed primitive type or {@link net.openhft.lang.model.Byteable} subclass, i. e. if
* value size is known statically, it is automatically accounted and this method shouldn't be called.
*
* <p>If value size varies, method {@link #valueSize(int)} or {@link #entrySize(int)} should be called
* instead of this one.
*
* @param sampleValue the sample value
* @return this builder back
* @see #valueSize(int)
* @see #constantKeySizeBySample(Object)
*/
ChronicleMapBuilderI<K, V> constantValueSizeBySample(
@NotNull V sampleValue);
/**
* {@inheritDoc}
*
* <p>Example: <pre>{@code Map<Key, Value> map =
* ChronicleMapBuilder.of(Key.class, Value.class)
* .entries(1_000_000)
* .keySize(50).valueSize(200)
* // this class hasn't implemented yet, just for example
* .objectSerializer(new KryoObjectSerializer())
* .create();}</pre>
*/
ChronicleMapBuilderI<K, V> objectSerializer(
@NotNull ObjectSerializer objectSerializer);
/**
* {@inheritDoc}
*
* @throws IllegalStateException if custom value marshaller is specified or value class is not either
* {@code BytesMarshallable} or {@code Externalizable}
*/
ChronicleMapBuilderI<K, V> valueDeserializationFactory(
@NotNull ObjectFactory<V> valueDeserializationFactory);
/**
* {@inheritDoc}
*
* <p>By default, default value provider is not specified, {@link #defaultValue(Object) default value} is
* specified instead.
*
* @see #defaultValue(Object)
*/
ChronicleMapBuilderI<K, V> defaultValueProvider(
@NotNull DefaultValueProvider<K, V> defaultValueProvider);
/**
* Configures the {@code BytesMarshaller} used to serialize/deserialize values to/from off-heap memory in
* maps, created by this builder. See <a href="https://github.com/OpenHFT/Chronicle-Map#serialization">the
* section about serialization in ChronicleMap manual</a> for more information.
*
* @param valueMarshaller the marshaller used to serialize values
* @return this builder back
* @see #valueMarshallers(net.openhft.chronicle.hash.serialization.BytesWriter,
* net.openhft.chronicle.hash.serialization.BytesReader)
* @see ChronicleMapBuilder
* @see #objectSerializer(ObjectSerializer)
*/
ChronicleMapBuilderI<K, V> valueMarshaller(
@NotNull BytesMarshaller<V> valueMarshaller);
/**
* Configures the marshallers, used to serialize/deserialize values to/from off-heap memory in maps,
* created by this builder. See <a href="https://github.com/OpenHFT/Chronicle-Map#serialization">the
* section about serialization in ChronicleMap manual</a> for more information.
*
* <p>Configuring marshalling this way results to a little bit more compact in-memory layout of the map,
* comparing to a single interface configuration: {@link #valueMarshaller(BytesMarshaller)}.
*
* <p>Passing {@link net.openhft.chronicle.hash.serialization.BytesInterop} instead of plain {@link
* net.openhft.chronicle.hash.serialization.BytesWriter} is, of cause, possible, but currently pointless
* for values.
*
* @param valueWriter the new value object &rarr; {@link net.openhft.lang.io.Bytes} writer (interop)
* strategy
* @param valueReader the new {@link net.openhft.lang.io.Bytes} &rarr; value object reader strategy
* @return this builder back
* @see #valueMarshaller(BytesMarshaller)
*/
ChronicleMapBuilderI<K, V> valueMarshallers(
@NotNull BytesWriter<V> valueWriter, @NotNull BytesReader<V> valueReader);
/**
* Configures the marshaller used to serialize actual value sizes to off-heap memory in maps, created by
* this builder.
*
* <p>Default value size marshaller is so-called {@linkplain net.openhft.chronicle.hash.serialization.SizeMarshallers#stopBit()
* stop bit encoding marshalling}. If {@linkplain #constantValueSizeBySample(Object) constant value size}
* is configured, or defaulted if the value type is always constant and {@code ChronicleHashBuilder}
* implementation knows about it, this configuration takes no effect, because a special {@link
* net.openhft.chronicle.hash.serialization.SizeMarshaller} implementation, which doesn't actually do any
* marshalling, and just returns the known constant size on {@link net.openhft.chronicle.hash.serialization.SizeMarshaller#readSize(
*net.openhft.lang.io.Bytes)} calls, is used instead of any {@code SizeMarshaller} configured using this
* method.
*
* @param valueSizeMarshaller the new marshaller, used to serialize actual value sizes to off-heap memory
* @return this builder back
*/
ChronicleMapBuilderI<K, V> valueSizeMarshaller(
@NotNull SizeMarshaller valueSizeMarshaller);
ChronicleMapBuilderI<K, V> entries(long l);
ChronicleMapBuilderI<K, V> replication(SingleChronicleHashReplication withId);
ChronicleMap<K, V> create() throws IOException;
ChronicleMapBuilderI<K, V> actualSegments(int actualSegments);
ChronicleMapBuilderI<K, V> minSegments(int minSegments);
ChronicleMapBuilderI<K, V> actualEntriesPerSegment(long actualEntriesPerSegment);
ChronicleMapBuilderI<K, V> lockTimeOut(long lockTimeOut, TimeUnit unit);
ChronicleMapBuilderI<K, V> errorListener(ChronicleHashErrorListener errorListener);
ChronicleMapBuilderI<K, V> metaDataBytes(int metaDataBytes);
ChronicleMapBuilderI<K, V> timeProvider(TimeProvider timeProvider);
ChronicleMapBuilderI<K, V> bytesMarshallerFactory(BytesMarshallerFactory bytesMarshallerFactory);
ChronicleMapBuilderI<K, V> keyMarshaller(BytesMarshaller<K> keyMarshaller);
ChronicleMapBuilderI<K, V> keyMarshallers(BytesWriter<K> keyWriter, BytesReader<K> keyReader);
ChronicleMapBuilderI<K, V> keySizeMarshaller(SizeMarshaller keySizeMarshaller);
ChronicleMapBuilderI<K, V> keyDeserializationFactory(ObjectFactory<K> keyDeserializationFactory);
ChronicleMapBuilderI<K, V> immutableKeys();
ChronicleMap<K, V> createPersistedTo(File file) throws IOException;
StatelessClientConfig<ChronicleMap<K, V>> statelessClient(InetSocketAddress remoteAddress);
ChronicleHashInstanceConfig<ChronicleMap<K, V>> instance();
ChronicleMapBuilderI<K, V> replication(byte identifier);
ChronicleMapBuilderI<K, V> replication(byte identifier, TcpTransportAndNetworkConfig tcpTransportAndNetwork);
ChronicleMapBuilderI<K, V> clone();
ChronicleMapBuilderI<K, V> eventListener(MapEventListener<K, V, ChronicleMap<K, V>> eventListener);
ChronicleMapBuilderI<K, V> putReturnsNull(boolean b);
ChronicleMap<K, V> createReplicated(byte b) throws IOException;
ChronicleMapBuilderI<K, V> removeReturnsNull(boolean b);
}
@@ -1,284 +0,0 @@
/*
* Copyright 2014 Higher Frequency Trading http://www.higherfrequencytrading.com
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.openhft.chronicle.map;
import net.openhft.chronicle.hash.ChronicleHashBuilder;
import net.openhft.chronicle.hash.serialization.*;
import net.openhft.lang.io.Bytes;
import net.openhft.lang.io.serialization.BytesMarshallable;
import net.openhft.lang.io.serialization.BytesMarshaller;
import net.openhft.lang.io.serialization.ObjectFactory;
import net.openhft.lang.io.serialization.ObjectSerializer;
import net.openhft.lang.model.Byteable;
import org.jetbrains.annotations.NotNull;
import java.io.Externalizable;
import java.io.Serializable;
/**
* {@code ChronicleMapOnHeapUpdatableBuilder} is intended to be used to configure {@link ChronicleMap}s with
* non-{@link Byteable} values, which don't point to off-heap memory directly, including primitives
* ({@link Long}, {@link Double}, etc.), {@link String}s and {@link CharSequence}s, values
* implementing {@link BytesMarshallable}, {@link Externalizable} or {@link Serializable} interface,
* or any other values for which {@linkplain #valueMarshaller(BytesMarshaller) custom marshaller}
* is provided.
*
* <p>Use static {@link #of(Class, Class) of(Key.class, Value.class)} method to obtain
* a {@code ChronicleMapOnHeapUpdatableBuilder} instance.
*
* @param <K> key type of the maps, created by this builder
* @param <V> value type of the maps, created by this builder
* @see ChronicleMapBuilder
* @see AbstractChronicleMapBuilder
* @see ChronicleHashBuilder
*/
public final class ChronicleMapOnHeapUpdatableBuilder<K, V>
extends AbstractChronicleMapBuilder<K, V, ChronicleMapOnHeapUpdatableBuilder<K, V>> {
/**
* Returns a new {@code ChronicleMapOnHeapUpdatableBuilder} instance which is able to {@linkplain #create()
* create} maps with the specified key and value classes.
*
* <p>{@code ChronicleMapOnHeapUpdatableBuilder} analyzes provided key and value classes and automatically
* chooses the most specific and effective serializer which it is aware about. Read
* <a href="https://github.com/OpenHFT/Chronicle-Map#serialization">the section about
* serialization in Chronicle Map manual</a> for more information.
*
* @param keyClass class object used to infer key type and discover it's properties
* via reflection
* @param valueClass class object used to infer value type and discover it's properties
* via reflection
* @param <K> key type of the maps, created by the returned builder
* @param <V> value type of the maps, created by the returned builder
* @return a new builder for the given key and value classes
*/
public static <K, V> ChronicleMapOnHeapUpdatableBuilder<K, V> of(
@NotNull Class<K> keyClass, @NotNull Class<V> valueClass) {
return new ChronicleMapOnHeapUpdatableBuilder<K, V>(keyClass, valueClass);
}
ChronicleMapOnHeapUpdatableBuilder(Class<K> keyClass, Class<V> valueClass) {
super(keyClass, valueClass);
}
@Override
ChronicleMapOnHeapUpdatableBuilder<K, V> self() {
return this;
}
/**
* {@inheritDoc}
*
* @see #constantKeySizeBySample(Object)
* @see #valueSize(int)
* @see #entrySize(int)
*/
@Override
public ChronicleMapOnHeapUpdatableBuilder<K, V> keySize(int keySize) {
return super.keySize(keySize);
}
/**
* {@inheritDoc}
*
* @see #keySize(int)
* @see #constantValueSizeBySample(Object)
*/
@Override
public ChronicleMapOnHeapUpdatableBuilder<K, V> constantKeySizeBySample(K sampleKey) {
return super.constantKeySizeBySample(sampleKey);
}
/**
* Configures the optimal number of bytes, taken by serialized form of values, put into maps,
* created by this builder. If value size is always the same, call {@link
* #constantValueSizeBySample(Object)} method instead of this one.
*
* <p>If value is a boxed primitive type, i. e. if value size is known statically,
* it is automatically accounted and shouldn't be specified by user.
*
* <p>If value size varies moderately, specify the size higher than average, but lower than the
* maximum possible, to minimize average memory overuse. If value size varies in a wide range,
* it's better to use {@linkplain #entrySize(int) entry size} in "chunk" mode and configure it
* directly.
*
* @param valueSize number of bytes, taken by serialized form of values
* @return this {@code ChronicleMapOnHeapUpdatableBuilder} back
* @see #constantValueSizeBySample(Object)
* @see #keySize(int)
* @see #entrySize(int)
*/
@Override
public ChronicleMapOnHeapUpdatableBuilder<K, V> valueSize(int valueSize) {
return super.valueSize(valueSize);
}
/**
* Configures the constant number of bytes, taken by serialized form of values, put into maps,
* created by this builder. This is done by providing the {@code sampleValue}, all values should
* take the same number of bytes in serialized form, as this sample object.
*
* <p>If values are of boxed primitive type or {@link Byteable} subclass, i. e. if value size is
* known statically, it is automatically accounted and this method shouldn't be called.
*
* <p>If value size varies, method {@link #valueSize(int)} or {@link #entrySize(int)} should be
* called instead of this one.
*
* @param sampleValue the sample value
* @return this builder back
* @see #valueSize(int)
* @see #constantKeySizeBySample(Object)
*/
@Override
public ChronicleMapOnHeapUpdatableBuilder<K, V> constantValueSizeBySample(
@NotNull V sampleValue) {
return super.constantValueSizeBySample(sampleValue);
}
/**
* {@inheritDoc}
*
* <p>Example: <pre>{@code Map<Key, Value> map =
* ChronicleMapOnHeapUpdatableBuilder.of(Key.class, Value.class)
* .entries(1_000_000)
* .keySize(50).valueSize(200)
* // this class hasn't implemented yet, just for example
* .objectSerializer(new KryoObjectSerializer())
* .create();}</pre>
*
* <p>This serializer is used to serialize both keys and values, if they both require this:
* loosely typed, nullable, and custom {@linkplain #keyMarshaller(BytesMarshaller) key} and
* {@linkplain #valueMarshaller(BytesMarshaller) value} marshallers are not configured.
*/
@Override
public ChronicleMapOnHeapUpdatableBuilder<K, V> objectSerializer(
@NotNull ObjectSerializer objectSerializer) {
return super.objectSerializer(objectSerializer);
}
/**
* {@inheritDoc}
*
* <p>If {@linkplain #valueMarshaller(BytesMarshaller) custom value marshaller} is configured,
* this configuration is unused, because it is incapsulated in {@link
* BytesMarshaller#read(Bytes)} method (without provided instance to read the data into),
* i. e. it's is the user-side responsibility. Actually this is just a convenience method
* supporting value marshaller configurations, made initially during {@link #of(Class, Class)}
* call, if the value class is {@link BytesMarshallable} or {@link Externalizable} subclass.
*
* @throws IllegalStateException if custom value marshaller is specified or value class is not
* either {@code BytesMarshallable} or {@code Externalizable}
*/
@Override
public ChronicleMapOnHeapUpdatableBuilder<K, V> valueDeserializationFactory(
@NotNull ObjectFactory<V> valueDeserializationFactory) {
return super.valueDeserializationFactory(valueDeserializationFactory);
}
/**
* {@inheritDoc}
*
* <p>By default, the default value is set to {@code null}.
*
* @see #defaultValueProvider(DefaultValueProvider)
*/
@Override
public ChronicleMapOnHeapUpdatableBuilder<K, V> defaultValue(V defaultValue) {
return super.defaultValue(defaultValue);
}
/**
* {@inheritDoc}
*
* <p>By default, default value provider is not specified, {@link #defaultValue(Object) default
* value} is specified instead.
*
* @see #defaultValue(Object)
*/
@Override
public ChronicleMapOnHeapUpdatableBuilder<K, V> defaultValueProvider(
@NotNull DefaultValueProvider<K, V> defaultValueProvider) {
return super.defaultValueProvider(defaultValueProvider);
}
/**
* Configures the {@code BytesMarshaller} used to serialize/deserialize values to/from off-heap
* memory in maps, created by this builder. See
* <a href="https://github.com/OpenHFT/Chronicle-Map#serialization">the
* section about serialization in ChronicleMap manual</a> for more information.
*
* @param valueMarshaller the marshaller used to serialize values
* @return this builder back
* @see #valueMarshallers(BytesWriter, BytesReader)
* @see ChronicleMapBuilder
* @see #objectSerializer(ObjectSerializer)
*/
public ChronicleMapOnHeapUpdatableBuilder<K, V> valueMarshaller(
@NotNull BytesMarshaller<V> valueMarshaller) {
valueBuilder.marshaller(valueMarshaller);
return this;
}
/**
* Configures the marshallers, used to serialize/deserialize values to/from off-heap
* memory in maps, created by this builder. See
* <a href="https://github.com/OpenHFT/Chronicle-Map#serialization">the section about
* serialization in ChronicleMap manual</a> for more information.
*
* <p>Configuring marshalling this way results to a little bit more compact in-memory layout
* of the map, comparing to a single interface configuration:
* {@link #valueMarshaller(BytesMarshaller)}.
*
* <p>Passing {@link BytesInterop} instead of plain {@link BytesWriter} is, of cause, possible,
* but currently pointless for values.
*
* @param valueWriter the new value object &rarr; {@link Bytes} writer (interop) strategy
* @param valueReader the new {@link Bytes} &rarr; value object reader strategy
* @return this builder back
* @see #valueMarshaller(BytesMarshaller)
*/
public ChronicleMapOnHeapUpdatableBuilder<K, V> valueMarshallers(
@NotNull BytesWriter<V> valueWriter, @NotNull BytesReader<V> valueReader) {
valueBuilder.writer(valueWriter);
valueBuilder.reader(valueReader);
return this;
}
/**
* Configures the marshaller used to serialize actual value sizes to off-heap memory in maps,
* created by this builder.
*
* <p>Default value size marshaller is so-called {@linkplain SizeMarshallers#stopBit()
* stop bit encoding marshalling}. If {@linkplain #constantValueSizeBySample(Object) constant
* value size} is configured, or defaulted if the value type is always constant and {@code
* ChronicleHashBuilder} implementation knows about it, this configuration takes no effect,
* because a special {@link SizeMarshaller} implementation, which doesn't actually do any
* marshalling, and just returns the known constant size on {@link SizeMarshaller#readSize(
* Bytes)} calls, is used instead of any {@code SizeMarshaller} configured using this method.
*
* @param valueSizeMarshaller the new marshaller, used to serialize actual value sizes to
* off-heap memory
* @return this builder back
*/
public ChronicleMapOnHeapUpdatableBuilder<K, V> valueSizeMarshaller(
@NotNull SizeMarshaller valueSizeMarshaller) {
valueBuilder.sizeMarshaller(valueSizeMarshaller);
return this;
}
}
@@ -124,8 +124,6 @@ public class NodeDiscovery {
if (proposedIdentifier == ourProposedIdentifier.get())
useAnotherIdentifier.set(true);
}
}
countDownLatch.get().countDown();
@@ -231,7 +229,7 @@ public class NodeDiscovery {
LOG.info("Using Remote identifier=" + identifier);
nodeDiscoveryEventListenerAtomicReference.set(null);
return ChronicleMapOnHeapUpdatableBuilder.of(Integer.class,
return ChronicleMapBuilder.of(Integer.class,
CharSequence.class)
.entries(20000L)
.replication(SingleChronicleHashReplication.builder()
@@ -0,0 +1,172 @@
/*
* Copyright 2014 Higher Frequency Trading http://www.higherfrequencytrading.com
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.openhft.chronicle.map;
import net.openhft.chronicle.hash.serialization.BytesReader;
import net.openhft.chronicle.hash.serialization.BytesWriter;
import net.openhft.chronicle.hash.serialization.SizeMarshaller;
import net.openhft.lang.io.serialization.BytesMarshaller;
import net.openhft.lang.model.Byteable;
import net.openhft.lang.model.DataValueClasses;
import org.jetbrains.annotations.NotNull;
final class OffHeapUpdatableChronicleMapBuilder<K, V>
extends AbstractChronicleMapBuilder<K, V, OffHeapUpdatableChronicleMapBuilder<K, V>> implements ChronicleMapBuilderI<K, V> {
public static <K, V> ChronicleMapBuilderI<K, V> of(
@NotNull Class<K> keyClass, @NotNull Class<V> valueClass) {
if (valueClass.isEnum())
return OnHeapUpdatableChronicleMapBuilder.of(keyClass, valueClass);
if (keyClass.isInterface() && keyClass != CharSequence.class) {
keyClass = DataValueClasses.directClassFor(keyClass);
}
if ((valueClass.isInterface() && valueClass != CharSequence.class)) {
valueClass = DataValueClasses.directClassFor(valueClass);
} else if (!offHeapReference(valueClass)) {
return OnHeapUpdatableChronicleMapBuilder.of(keyClass, valueClass);
}
return new OffHeapUpdatableChronicleMapBuilder<K, V>(keyClass, valueClass);
}
OffHeapUpdatableChronicleMapBuilder(Class<K> keyClass, Class<V> valueClass) {
super(keyClass, valueClass);
prepareValueBytesOnAcquire(new ZeroOutValueBytes<K, V>(valueSize()));
}
@Override
OffHeapUpdatableChronicleMapBuilder<K, V> self() {
return this;
}
@Override
public OffHeapUpdatableChronicleMapBuilder<K, V> valueSize(int valueSize) {
throw new UnsupportedOperationException();
}
/**
* {@inheritDoc} With respect to {@linkplain #entryAndValueAlignment(Alignment) alignment}.
*
* <p>Note that the actual entrySize will be aligned to 4 (default {@linkplain
* #entryAndValueAlignment(Alignment) entry alignment}). I. e. if you set entry size to 30, and entry
* alignment is set to {@link Alignment#OF_4_BYTES}, the actual entry size will be 32 (30 aligned to 4
* bytes).
*
* @see #entryAndValueAlignment(Alignment)
* @see #entries(long)
*/
@Override
public OffHeapUpdatableChronicleMapBuilder<K, V> entrySize(int entrySize) {
return super.entrySize(entrySize);
}
/**
* Configures alignment strategy of address in memory of entries and independently of address in memory of
* values within entries in ChronicleMaps, created by this builder.
*
* <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>Note that {@linkplain #entrySize(int) entry size} will be aligned according to this alignment. I. e.
* if you set {@code entrySize(20)} and {@link Alignment#OF_8_BYTES}, actual entry size will be 24 (20
* aligned to 8 bytes).
*
* <p>Default is {@link Alignment#OF_4_BYTES} for Byteable values.
*
* @param alignment the new alignment of the maps constructed by this builder
* @return this {@code ChronicleMapOnHeapUpdatableBuilder} back
*/
@Override
public OffHeapUpdatableChronicleMapBuilder<K, V> entryAndValueAlignment(Alignment alignment) {
return super.entryAndValueAlignment(alignment);
}
/**
* {@inheritDoc} Also, it overrides any previous {@link #prepareValueBytesOnAcquire} configuration to this
* {@code ChronicleMapBuilder}.
*
* <p>By default, the default value is not specified, default {@linkplain #prepareValueBytesOnAcquire
* prepare value bytes routine} is specified instead.
*
* @see #defaultValueProvider(DefaultValueProvider)
* @see #prepareValueBytesOnAcquire(PrepareValueBytes)
*/
@Override
public OffHeapUpdatableChronicleMapBuilder<K, V> defaultValue(V defaultValue) {
return super.defaultValue(defaultValue);
}
/**
* {@inheritDoc} Also, it overrides any previous {@link #prepareValueBytesOnAcquire} configuration to this
* {@code ChronicleMapBuilder}.
*
* <p>By default, the default value provider is not specified, default {@linkplain
* #prepareValueBytesOnAcquire prepare value bytes routine} is specified instead.
*
* @see #defaultValue(Object)
* @see #prepareValueBytesOnAcquire(PrepareValueBytes)
*/
@Override
public OffHeapUpdatableChronicleMapBuilder<K, V> defaultValueProvider(
@NotNull DefaultValueProvider<K, V> defaultValueProvider) {
return super.defaultValueProvider(defaultValueProvider);
}
/**
* Configures the procedure which is called on the bytes, which later the returned value is pointing to,
* if the key is absent, on {@link ChronicleMap#acquireUsing(Object, Object) acquireUsing()} call on maps,
* created by this builder. See {@link PrepareValueBytes} for more information.
*
* <p>The default preparation callback zeroes out the value bytes.
*
* @param prepareValueBytes what to do with the value bytes before assigning them into the {@link
* Byteable} value to return from {@code acquireUsing()} call
* @return this builder back
* @see PrepareValueBytes
* @see #defaultValue(Object)
* @see #defaultValueProvider(DefaultValueProvider)
*/
@Override
public OffHeapUpdatableChronicleMapBuilder<K, V> prepareValueBytesOnAcquire(
@NotNull PrepareValueBytes<K, V> prepareValueBytes) {
return super.prepareValueBytesOnAcquire(prepareValueBytes);
}
public ChronicleMapBuilderI<K, V> valueMarshaller(@NotNull BytesMarshaller<V> valueMarshaller) {
throw new UnsupportedOperationException("not supported for this combination of key/value type");
}
public ChronicleMapBuilderI<K, V> valueMarshallers(@NotNull BytesWriter<V> valueWriter, @NotNull BytesReader<V> valueReader) {
throw new UnsupportedOperationException("not supported for this combination of key/value type");
}
public ChronicleMapBuilderI<K, V> valueSizeMarshaller(@NotNull SizeMarshaller valueSizeMarshaller) {
throw new UnsupportedOperationException("not supported for this combination of key/value type");
}
@Override
public OffHeapUpdatableChronicleMapBuilder<K, V> constantValueSizeBySample(V sampleValue) {
throw new UnsupportedOperationException("not supported for this combination of key/value type");
}
}
@@ -0,0 +1,311 @@
/*
* Copyright 2014 Higher Frequency Trading http://www.higherfrequencytrading.com
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.openhft.chronicle.map;
import net.openhft.chronicle.hash.ChronicleHashBuilder;
import net.openhft.chronicle.hash.serialization.BytesReader;
import net.openhft.chronicle.hash.serialization.BytesWriter;
import net.openhft.chronicle.hash.serialization.SizeMarshaller;
import net.openhft.lang.io.Bytes;
import net.openhft.lang.io.serialization.BytesMarshallable;
import net.openhft.lang.io.serialization.BytesMarshaller;
import net.openhft.lang.io.serialization.ObjectFactory;
import net.openhft.lang.io.serialization.ObjectSerializer;
import net.openhft.lang.model.Byteable;
import org.jetbrains.annotations.NotNull;
import java.io.Externalizable;
import java.io.IOException;
import java.io.Serializable;
/**
* {@code ChronicleMapOnHeapUpdatableBuilder} is intended to be used to configure {@link ChronicleMap}s with
* non-{@link Byteable} values, which don't point to off-heap memory directly, including primitives ({@link
* Long}, {@link Double}, etc.), {@link String}s and {@link CharSequence}s, values implementing {@link
* BytesMarshallable}, {@link Externalizable} or {@link Serializable} interface, or any other values for which
* {@linkplain #valueMarshaller(BytesMarshaller) custom marshaller} is provided.
*
* <p>Use static {@link #of(Class, Class) of(Key.class, Value.class)} method to obtain a {@code
* ChronicleMapOnHeapUpdatableBuilder} instance.
*
* @param <K> key type of the maps, created by this builder
* @param <V> value type of the maps, created by this builder
* @see ChronicleMapBuilder
* @see AbstractChronicleMapBuilder
* @see ChronicleHashBuilder
*/
final class OnHeapUpdatableChronicleMapBuilder<K, V>
extends AbstractChronicleMapBuilder<K, V, OnHeapUpdatableChronicleMapBuilder<K, V>> implements
ChronicleMapBuilderI<K, V> {
/**
* Returns a new {@code ChronicleMapOnHeapUpdatableBuilder} instance which is able to {@linkplain
* #create() create} maps with the specified key and value classes.
*
* <p>{@code ChronicleMapOnHeapUpdatableBuilder} analyzes provided key and value classes and automatically
* chooses the most specific and effective serializer which it is aware about. Read <a
* href="https://github.com/OpenHFT/Chronicle-Map#serialization">the section about serialization in
* Chronicle Map manual</a> for more information.
*
* @param keyClass class object used to infer key type and discover it's properties via reflection
* @param valueClass class object used to infer value type and discover it's properties via reflection
* @param <K> key type of the maps, created by the returned builder
* @param <V> value type of the maps, created by the returned builder
* @return a new builder for the given key and value classes
*/
public static <K, V> ChronicleMapBuilderI<K, V> of(
@NotNull Class<K> keyClass, @NotNull Class<V> valueClass) {
return new OnHeapUpdatableChronicleMapBuilder<K, V>(keyClass, valueClass);
}
OnHeapUpdatableChronicleMapBuilder(Class<K> keyClass, Class<V> valueClass) {
super(keyClass, valueClass);
}
@Override
OnHeapUpdatableChronicleMapBuilder<K, V> self() {
return this;
}
public ChronicleMap<K, V> create() throws IOException {
return super.create();
}
/**
* {@inheritDoc}
*
* @see #constantKeySizeBySample(Object)
* @see #valueSize(int)
* @see #entrySize(int)
*/
@Override
public OnHeapUpdatableChronicleMapBuilder<K, V> keySize(int keySize) {
return super.keySize(keySize);
}
/**
* {@inheritDoc}
*
* @see #keySize(int)
* @see #constantValueSizeBySample(Object)
*/
@Override
public OnHeapUpdatableChronicleMapBuilder<K, V> constantKeySizeBySample(K sampleKey) {
return super.constantKeySizeBySample(sampleKey);
}
/**
* Configures the optimal number of bytes, taken by serialized form of values, put into maps, created by
* this builder. If value size is always the same, call {@link #constantValueSizeBySample(Object)} method
* instead of this one.
*
* <p>If value is a boxed primitive type, i. e. if value size is known statically, it is automatically
* accounted and shouldn't be specified by user.
*
* <p>If value size varies moderately, specify the size higher than average, but lower than the maximum
* possible, to minimize average memory overuse. If value size varies in a wide range, it's better to use
* {@linkplain #entrySize(int) entry size} in "chunk" mode and configure it directly.
*
* @param valueSize number of bytes, taken by serialized form of values
* @return this {@code ChronicleMapOnHeapUpdatableBuilder} back
* @see #constantValueSizeBySample(Object)
* @see #keySize(int)
* @see #entrySize(int)
*/
@Override
public OnHeapUpdatableChronicleMapBuilder<K, V> valueSize(int valueSize) {
return super.valueSize(valueSize);
}
/**
* Configures the constant number of bytes, taken by serialized form of values, put into maps, created by
* this builder. This is done by providing the {@code sampleValue}, all values should take the same number
* of bytes in serialized form, as this sample object.
*
* <p>If values are of boxed primitive type or {@link Byteable} subclass, i. e. if value size is known
* statically, it is automatically accounted and this method shouldn't be called.
*
* <p>If value size varies, method {@link #valueSize(int)} or {@link #entrySize(int)} should be called
* instead of this one.
*
* @param sampleValue the sample value
* @return this builder back
* @see #valueSize(int)
* @see #constantKeySizeBySample(Object)
*/
@Override
public OnHeapUpdatableChronicleMapBuilder<K, V> constantValueSizeBySample(
@NotNull V sampleValue) {
return super.constantValueSizeBySample(sampleValue);
}
/**
* {@inheritDoc}
*
* <p>Example: <pre>{@code Map<Key, Value> map =
* ChronicleMapBuilder.of(Key.class, Value.class)
* .entries(1_000_000)
* .keySize(50).valueSize(200)
* // this class hasn't implemented yet, just for example
* .objectSerializer(new KryoObjectSerializer())
* .create();}</pre>
*
* <p>This serializer is used to serialize both keys and values, if they both require this: loosely typed,
* nullable, and custom {@linkplain #keyMarshaller(BytesMarshaller) key} and {@linkplain
* #valueMarshaller(BytesMarshaller) value} marshallers are not configured.
*/
@Override
public OnHeapUpdatableChronicleMapBuilder<K, V> objectSerializer(
@NotNull ObjectSerializer objectSerializer) {
return super.objectSerializer(objectSerializer);
}
/**
* {@inheritDoc}
*
* <p>If {@linkplain #valueMarshaller(BytesMarshaller) custom value marshaller} is configured, this
* configuration is unused, because it is incapsulated in {@link BytesMarshaller#read(Bytes)} method
* (without provided instance to read the data into), i. e. it's is the user-side responsibility. Actually
* this is just a convenience method supporting value marshaller configurations, made initially during
* {@link #of(Class, Class)} call, if the value class is {@link BytesMarshallable} or {@link
* Externalizable} subclass.
*
* @throws IllegalStateException if custom value marshaller is specified or value class is not either
* {@code BytesMarshallable} or {@code Externalizable}
*/
@Override
public OnHeapUpdatableChronicleMapBuilder<K, V> valueDeserializationFactory(
@NotNull ObjectFactory<V> valueDeserializationFactory) {
return super.valueDeserializationFactory(valueDeserializationFactory);
}
/**
* {@inheritDoc}
*
* <p>By default, the default value is set to {@code null}.
*
* @see #defaultValueProvider(DefaultValueProvider)
*/
@Override
public OnHeapUpdatableChronicleMapBuilder<K, V> defaultValue(V defaultValue) {
return super.defaultValue(defaultValue);
}
/**
* {@inheritDoc}
*
* <p>By default, default value provider is not specified, {@link #defaultValue(Object) default value} is
* specified instead.
*
* @see #defaultValue(Object)
*/
@Override
public OnHeapUpdatableChronicleMapBuilder<K, V> defaultValueProvider(
@NotNull DefaultValueProvider<K, V> defaultValueProvider) {
return super.defaultValueProvider(defaultValueProvider);
}
/**
* Configures the {@code BytesMarshaller} used to serialize/deserialize values to/from off-heap memory in
* maps, created by this builder. See <a href="https://github.com/OpenHFT/Chronicle-Map#serialization">the
* section about serialization in ChronicleMap manual</a> for more information.
*
* @param valueMarshaller the marshaller used to serialize values
* @return this builder back
* @see #valueMarshallers(BytesWriter, BytesReader)
* @see ChronicleMapBuilder
* @see #objectSerializer(ObjectSerializer)
*/
public OnHeapUpdatableChronicleMapBuilder<K, V> valueMarshaller(
@NotNull BytesMarshaller<V> valueMarshaller) {
valueBuilder.marshaller(valueMarshaller);
return this;
}
/**
* Configures the marshallers, used to serialize/deserialize values to/from off-heap memory in maps,
* created by this builder. See <a href="https://github.com/OpenHFT/Chronicle-Map#serialization">the
* section about serialization in ChronicleMap manual</a> for more information.
*
* <p>Configuring marshalling this way results to a little bit more compact in-memory layout of the map,
* comparing to a single interface configuration: {@link #valueMarshaller(BytesMarshaller)}.
*
* <p>Passing {@link net.openhft.chronicle.hash.serialization.BytesInterop} instead of plain {@link
* BytesWriter} is, of cause, possible, but currently pointless for values.
*
* @param valueWriter the new value object &rarr; {@link Bytes} writer (interop) strategy
* @param valueReader the new {@link Bytes} &rarr; value object reader strategy
* @return this builder back
* @see #valueMarshaller(BytesMarshaller)
*/
public OnHeapUpdatableChronicleMapBuilder<K, V> valueMarshallers(
@NotNull BytesWriter<V> valueWriter, @NotNull BytesReader<V> valueReader) {
valueBuilder.writer(valueWriter);
valueBuilder.reader(valueReader);
return this;
}
/**
* Configures the marshaller used to serialize actual value sizes to off-heap memory in maps, created by
* this builder.
*
* <p>Default value size marshaller is so-called {@linkplain net.openhft.chronicle.hash.serialization.SizeMarshallers#stopBit()
* stop bit encoding marshalling}. If {@linkplain #constantValueSizeBySample(Object) constant value size}
* is configured, or defaulted if the value type is always constant and {@code ChronicleHashBuilder}
* implementation knows about it, this configuration takes no effect, because a special {@link
* SizeMarshaller} implementation, which doesn't actually do any marshalling, and just returns the known
* constant size on {@link SizeMarshaller#readSize(Bytes)} calls, is used instead of any {@code
* SizeMarshaller} configured using this method.
*
* @param valueSizeMarshaller the new marshaller, used to serialize actual value sizes to off-heap memory
* @return this builder back
*/
public OnHeapUpdatableChronicleMapBuilder<K, V> valueSizeMarshaller(
@NotNull SizeMarshaller valueSizeMarshaller) {
valueBuilder.sizeMarshaller(valueSizeMarshaller);
return this;
}
/**
* Configures the procedure which is called on the bytes, which later the returned value is pointing to,
* if the key is absent, on {@link ChronicleMap#acquireUsing(Object, Object) acquireUsing()} call on maps,
* created by this builder. See {@link PrepareValueBytes} for more information.
*
* <p>The default preparation callback zeroes out the value bytes.
*
* @param prepareValueBytes what to do with the value bytes before assigning them into the {@link
* Byteable} value to return from {@code acquireUsing()} call
* @return this builder back
* @see PrepareValueBytes
* @see #defaultValue(Object)
* @see #defaultValueProvider(DefaultValueProvider)
*/
@Override
public OnHeapUpdatableChronicleMapBuilder<K, V> prepareValueBytesOnAcquire(
@NotNull PrepareValueBytes<K, V> prepareValueBytes) {
throw new UnsupportedOperationException("not supported for this combination of key/value type");
}
public OnHeapUpdatableChronicleMapBuilder<K, V> entryAndValueAlignment(Alignment a) {
throw new UnsupportedOperationException("not supported for this combination of key/value type");
}
}
@@ -138,7 +138,7 @@ class StatelessChronicleMap<K, V> implements ChronicleMap<K, V>, Closeable {
result = AbstractChannelReplicator.openSocketChannel(closeables);
result.connect(builder.remoteAddress());
result.socket().setTcpNoDelay(true);
result.socket().setReuseAddress(true);
doHandShaking(result);
break;
@@ -240,6 +240,11 @@ class StatelessChronicleMap<K, V> implements ChronicleMap<K, V>, Closeable {
if (closeables != null)
closeables.closeQuietly();
closeables = null;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
//
}
}
@@ -29,7 +29,7 @@ import net.openhft.chronicle.hash.serialization.BytesReader;
import net.openhft.chronicle.hash.serialization.BytesWriter;
import net.openhft.chronicle.hash.serialization.SizeMarshaller;
import net.openhft.chronicle.map.ChronicleMap;
import net.openhft.chronicle.map.ChronicleMapOnHeapUpdatableBuilder;
import net.openhft.chronicle.map.ChronicleMapBuilder;
import net.openhft.lang.io.serialization.BytesMarshaller;
import net.openhft.lang.io.serialization.BytesMarshallerFactory;
import net.openhft.lang.io.serialization.ObjectFactory;
@@ -55,10 +55,10 @@ import java.util.concurrent.TimeUnit;
public class ChronicleSetBuilder<E>
implements ChronicleHashBuilder<E, ChronicleSet<E>, ChronicleSetBuilder<E>> {
private ChronicleMapOnHeapUpdatableBuilder<E, DummyValue> chronicleMapBuilderOnHeap;
private ChronicleMapBuilder<E, DummyValue> chronicleMapBuilderOnHeap;
ChronicleSetBuilder(Class<E> keyClass) {
chronicleMapBuilderOnHeap = ChronicleMapOnHeapUpdatableBuilder.of(keyClass, DummyValue.class)
chronicleMapBuilderOnHeap = ChronicleMapBuilder.of(keyClass, DummyValue.class)
.valueMarshallers(DummyValueMarshaller.INSTANCE, DummyValueMarshaller.INSTANCE)
.valueSizeMarshaller(DummyValueMarshaller.INSTANCE);
}
@@ -18,6 +18,6 @@
package net.openhft.chronicle.set;
enum DummyValue {
enum DummyValue {
DUMMY_VALUE
}