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; 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 key type of the maps, created by this builder * @param value type of the maps, created by this builder * @see AbstractChronicleMapBuilder * @see net.openhft.chronicle.hash.ChronicleHashBuilder */ public final class ChronicleMapBuilder implements ChronicleMapBuilderI { final ChronicleMapBuilderI delegate; /** * Returns a new {@code ChronicleMapBuilder} instance which is able to {@linkplain #create() create} maps * with the specified key and value classes. * *

{@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 key type of the maps, created by the returned builder * @param value type of the maps, created by the returned builder * @return a new builder for the given key and value classes */ public static ChronicleMapBuilder of( @NotNull Class keyClass, @NotNull Class valueClass) { if (valueClass.isEnum()) return new ChronicleMapBuilder(OnHeapUpdatableChronicleMapBuilder.of(keyClass, valueClass)); if (keyClass.isInterface() && !builtInType(keyClass)) keyClass = DataValueClasses.directClassFor(keyClass); if ((valueClass.isInterface() && !builtInType(valueClass))) { valueClass = DataValueClasses.directClassFor(valueClass); } else if (!offHeapReference(valueClass)) { return new ChronicleMapBuilder(OnHeapUpdatableChronicleMapBuilder.of(keyClass, valueClass)); } ChronicleMapBuilderI builder = new OffHeapUpdatableChronicleMapBuilder(keyClass, valueClass); return new ChronicleMapBuilder(builder); } static boolean builtInType(Class clazz) { return clazz.getClassLoader() == Class.class.getClassLoader(); } ChronicleMapBuilder(ChronicleMapBuilderI delegate) { this.delegate = delegate; } private static boolean offHeapReference(Class valueClass) { return Byteable.class.isAssignableFrom(valueClass); } /** * {@inheritDoc} With respect to {@linkplain #entryAndValueAlignment(Alignment) alignment}. * *

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 ChronicleMapBuilder entrySize(int 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. * *

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. * *

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). * *

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 ChronicleMapBuilder entryAndValueAlignment(Alignment alignment) { delegate.entryAndValueAlignment(alignment); return this; } /** * {@inheritDoc} Also, it overrides any previous {@link #prepareValueBytesOnAcquire} configuration to this * {@code ChronicleMapBuilder}. * *

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 defaultValue(V defaultValue) { delegate.defaultValue(defaultValue); 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. * *

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) */ @Override public ChronicleMapBuilder prepareValueBytesOnAcquire(@NotNull PrepareValueBytes prepareValueBytes) { delegate.prepareValueBytesOnAcquire(prepareValueBytes); return this; } @Override public ChronicleMapBuilder keySize(int keySize) { delegate.keySize(keySize); return this; } @Override public ChronicleMapBuilder 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. * *

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. * *

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 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. * *

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. * *

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 constantValueSizeBySample(@NotNull V sampleValue) { delegate.constantValueSizeBySample(sampleValue); return this; } /** * {@inheritDoc} * *

Example:

{@code Map 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();}
*/ @Override public ChronicleMapBuilder 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 valueDeserializationFactory(@NotNull ObjectFactory valueDeserializationFactory) { delegate.valueDeserializationFactory(valueDeserializationFactory); return this; } /** * {@inheritDoc} * *

By default, default value provider is not specified, {@link #defaultValue(Object) default value} is * specified instead. * * @see #defaultValue(Object) */ @Override public ChronicleMapBuilder defaultValueProvider(@NotNull DefaultValueProvider 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 the * section about serialization in ChronicleMap manual 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 valueMarshaller( @NotNull BytesMarshaller 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 the * section about serialization in ChronicleMap manual for more information. * *

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)}. * *

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 → {@link net.openhft.lang.io.Bytes} writer (interop) * strategy * @param valueReader the new {@link net.openhft.lang.io.Bytes} → value object reader strategy * @return this builder back * @see #valueMarshaller(BytesMarshaller) */ @Override public ChronicleMapBuilder valueMarshallers(@NotNull BytesWriter valueWriter, @NotNull BytesReader 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. * *

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 valueSizeMarshaller(@NotNull SizeMarshaller valueSizeMarshaller) { delegate.valueSizeMarshaller(valueSizeMarshaller); return this; } @Override public ChronicleMapBuilder entries(long l) { delegate.entries(l); return this; } /** * each map can be identified by a name * * @param name the name of the map * @return self */ @Override public ChronicleMapBuilder name(String name) { delegate.name(name); return this; } @Override public String name() { return delegate.name(); } @Override public ChronicleMapBuilder replication(SingleChronicleHashReplication withId) { delegate.replication(withId); return this; } @Override public ChronicleMapBuilder pushTo(InetSocketAddress... addresses) { delegate.pushTo(addresses); return this; } @Override public ChronicleMap create() { return delegate.create(); } @Override public ChronicleMapBuilder actualSegments(int actualSegments) { delegate.actualSegments(actualSegments); return this; } @Override public ChronicleMapBuilder minSegments(int minSegments) { delegate.minSegments(minSegments); return this; } @Override public ChronicleMapBuilder actualEntriesPerSegment(long actualEntriesPerSegment) { delegate.actualEntriesPerSegment(actualEntriesPerSegment); return this; } @Override public ChronicleMapBuilder lockTimeOut(long lockTimeOut, TimeUnit unit) { delegate.lockTimeOut(lockTimeOut, unit); return this; } @Override public ChronicleMapBuilder errorListener(ChronicleHashErrorListener errorListener) { delegate.errorListener(errorListener); return this; } @Override public ChronicleMapBuilder metaDataBytes(int metaDataBytes) { delegate.metaDataBytes(metaDataBytes); return this; } @Override public ChronicleMapBuilder timeProvider(TimeProvider timeProvider) { delegate.timeProvider(timeProvider); return this; } @Override public ChronicleMapBuilder bytesMarshallerFactory(BytesMarshallerFactory bytesMarshallerFactory) { delegate.bytesMarshallerFactory(bytesMarshallerFactory); return this; } @Override public ChronicleMapBuilder keyMarshaller(BytesMarshaller keyMarshaller) { delegate.keyMarshaller(keyMarshaller); return this; } @Override public ChronicleMapBuilder keyMarshallers(BytesWriter keyWriter, BytesReader keyReader) { delegate.keyMarshallers(keyWriter, keyReader); return this; } @Override public ChronicleMapBuilder keySizeMarshaller(SizeMarshaller keySizeMarshaller) { delegate.keySizeMarshaller(keySizeMarshaller); return this; } @Override public ChronicleMapBuilder keyDeserializationFactory(ObjectFactory keyDeserializationFactory) { delegate.keyDeserializationFactory(keyDeserializationFactory); return this; } @Override public ChronicleMapBuilder immutableKeys() { delegate.immutableKeys(); return this; } @Override public StatelessClientConfig> statelessClient(InetSocketAddress remoteAddress) { return delegate.statelessClient(remoteAddress); } @Override public ChronicleHashInstanceConfig> instance() { return delegate.instance(); } @Override public ChronicleMapBuilder replication(byte identifier) { delegate.replication(identifier); return this; } @Override public ChronicleMapBuilder replication(byte identifier, TcpTransportAndNetworkConfig tcpTransportAndNetwork) { delegate.replication(identifier, tcpTransportAndNetwork); return this; } @Override public ChronicleMapBuilder clone() { delegate.clone(); return this; } @Override public ChronicleMapBuilder putReturnsNull(boolean b) { delegate.putReturnsNull(b); return this; } @Override public ChronicleMapBuilder removeReturnsNull(boolean b) { delegate.removeReturnsNull(b); return this; } @Override public ChronicleMapBuilder eventListener(MapEventListener> eventListener) { delegate.eventListener(eventListener); return this; } @Override public ChronicleMap createPersistedTo(File file) throws IOException { return delegate.createPersistedTo(file); } /** * @param bootstapOnlyLocalEntries if set to true - when a new node joins a TCP replication grid, the new * node will be populated with data, only for the nodes that created that * data. Otherwise, all the nodes will publish all the data they have ( * potentially swamping the new node with duplicates ) however this does * guarantee that all the data is replicated over to the new node, and is * useful especially in the case that the originating node is not * currently running. */ public ChronicleMapBuilder bootstapOnlyLocalEntries(boolean bootstapOnlyLocalEntries) { delegate.bootstapOnlyLocalEntries(bootstapOnlyLocalEntries); return this; } }