/* * 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 extends AbstractChronicleMapBuilder> { public static ChronicleMapBuilderI of( @NotNull Class keyClass, @NotNull Class 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(keyClass, valueClass); } OffHeapUpdatableChronicleMapBuilder(Class keyClass, Class valueClass) { super(keyClass, valueClass); prepareValueBytesOnAcquire(new ZeroOutValueBytes(valueSize())); } @Override OffHeapUpdatableChronicleMapBuilder self() { return this; } @Override public OffHeapUpdatableChronicleMapBuilder valueSize(int valueSize) { throw new UnsupportedOperationException(); } /** * {@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 OffHeapUpdatableChronicleMapBuilder 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. * *

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

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 defaultValueProvider( @NotNull DefaultValueProvider 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. * *

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 prepareValueBytesOnAcquire( @NotNull PrepareValueBytes prepareValueBytes) { return super.prepareValueBytesOnAcquire(prepareValueBytes); } public ChronicleMapBuilderI valueMarshaller(@NotNull BytesMarshaller valueMarshaller) { throw new UnsupportedOperationException("not supported for this combination of key/value type"); } public ChronicleMapBuilderI valueMarshallers(@NotNull BytesWriter valueWriter, @NotNull BytesReader valueReader) { throw new UnsupportedOperationException("not supported for this combination of key/value type"); } public ChronicleMapBuilderI valueSizeMarshaller(@NotNull SizeMarshaller valueSizeMarshaller) { throw new UnsupportedOperationException("not supported for this combination of key/value type"); } @Override public OffHeapUpdatableChronicleMapBuilder constantValueSizeBySample(V sampleValue) { throw new UnsupportedOperationException("not supported for this combination of key/value type"); } }