mirror of
https://github.com/OpenHFT/Chronicle-Map.git
synced 2021-09-13 01:26:12 +00:00
HCOLL-134 added test to serialize the builders
This commit is contained in:
@@ -18,7 +18,6 @@ package net.openhft.chronicle.hash.replication;
|
||||
|
||||
|
||||
import net.openhft.chronicle.hash.ChronicleHash;
|
||||
import net.openhft.chronicle.hash.ChronicleHashBuilder;
|
||||
import net.openhft.chronicle.hash.ChronicleHashInstanceConfig;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
|
||||
@@ -57,17 +57,17 @@ import static net.openhft.chronicle.map.Objects.builderEquals;
|
||||
|
||||
/**
|
||||
* {@code AbstractChronicleMapBuilder} manages most of {@link ChronicleMap} configurations; has two concrete
|
||||
* 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
|
||||
* 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
|
||||
* ChronicleMap<Key, Value> map = ChronicleMapOnHeapUpdatableBuilder
|
||||
* .of(Key.class, Value.class)
|
||||
* .entries(100500)
|
||||
* // ... other configurations
|
||||
* .create();}</pre>
|
||||
* one of concrete {@code AbstractChronicleMapBuilder} subclasses, {@link OnHeapUpdatableChronicleMapBuilder} or {@link
|
||||
* ChronicleMapBuilder}, could be prepared and used to create many similar
|
||||
* 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
|
||||
* .of(Key.class, Value.class)
|
||||
@@ -94,9 +94,9 @@ import static net.openhft.chronicle.map.Objects.builderEquals;
|
||||
* @see ChronicleMap
|
||||
* @see ChronicleSetBuilder
|
||||
*/
|
||||
public abstract class AbstractChronicleMapBuilder<K, V,
|
||||
abstract class AbstractChronicleMapBuilder<K, V,
|
||||
B extends AbstractChronicleMapBuilder<K, V, B>>
|
||||
implements Cloneable, ChronicleHashBuilder<K, ChronicleMap<K, V>, B> {
|
||||
implements Cloneable, ChronicleHashBuilder<K, ChronicleMap<K, V>, B>, ChronicleMapBuilderI<K, V> {
|
||||
|
||||
static final short UDP_REPLICATION_MODIFICATION_ITERATOR_ID = 128;
|
||||
private static final Bytes EMPTY_BYTES = new ByteBufferBytes(ByteBuffer.allocate(0));
|
||||
@@ -110,6 +110,8 @@ public abstract class AbstractChronicleMapBuilder<K, V,
|
||||
SerializationBuilder<K> keyBuilder;
|
||||
SerializationBuilder<V> valueBuilder;
|
||||
|
||||
private String name;
|
||||
|
||||
// used when configuring the number of segments.
|
||||
private int minSegments = -1;
|
||||
private int actualSegments = -1;
|
||||
@@ -243,14 +245,14 @@ public abstract class AbstractChronicleMapBuilder<K, V,
|
||||
return keyOrValueSize(keySize, keyBuilder);
|
||||
}
|
||||
|
||||
B valueSize(int valueSize) {
|
||||
public B valueSize(int valueSize) {
|
||||
if (valueSize <= 0)
|
||||
throw new IllegalArgumentException("Value size must be positive");
|
||||
this.valueSize = valueSize;
|
||||
return self();
|
||||
}
|
||||
|
||||
B constantValueSizeBySample(V sampleValue) {
|
||||
public B constantValueSizeBySample(V sampleValue) {
|
||||
this.sampleValue = sampleValue;
|
||||
return self();
|
||||
}
|
||||
@@ -316,7 +318,7 @@ public abstract class AbstractChronicleMapBuilder<K, V,
|
||||
return entryAndValueAlignment().alignSize(size);
|
||||
}
|
||||
|
||||
B entryAndValueAlignment(Alignment alignment) {
|
||||
public B entryAndValueAlignment(Alignment alignment) {
|
||||
this.alignment = alignment;
|
||||
return self();
|
||||
}
|
||||
@@ -716,7 +718,7 @@ public abstract class AbstractChronicleMapBuilder<K, V,
|
||||
return self();
|
||||
}
|
||||
|
||||
B prepareValueBytesOnAcquire(@NotNull PrepareValueBytes<K, V> prepareValueBytes) {
|
||||
public B prepareValueBytesOnAcquire(@NotNull PrepareValueBytes<K, V> prepareValueBytes) {
|
||||
this.prepareValueBytes = prepareValueBytes;
|
||||
this.defaultValue = null;
|
||||
this.defaultValueProvider = null;
|
||||
@@ -842,7 +844,7 @@ public abstract class AbstractChronicleMapBuilder<K, V,
|
||||
* @param identifier id
|
||||
* @return map
|
||||
*/
|
||||
public 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>,
|
||||
@@ -952,5 +954,16 @@ public abstract class AbstractChronicleMapBuilder<K, V,
|
||||
return (int) Math.min(Math.max(2L, entries() >> 10),
|
||||
VanillaChronicleMap.MAX_ENTRY_OVERSIZE_FACTOR);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ChronicleMapBuilderI<K, V> name(String name) {
|
||||
this.name = name;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
public String name() {
|
||||
return this.name;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -330,6 +330,23 @@ public final class ChronicleMapBuilder<K, V> implements ChronicleMapBuilderI<K,
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* each map can be identified by a name
|
||||
* @param name the name of the map
|
||||
* @return self
|
||||
*/
|
||||
@Override
|
||||
public ChronicleMapBuilder<K, V> name(String name) {
|
||||
delegate.name(name);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String name() {
|
||||
return delegate.name() ;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ChronicleMapBuilder<K, V> replication(SingleChronicleHashReplication withId) {
|
||||
delegate.replication(withId);
|
||||
|
||||
@@ -17,13 +17,14 @@ import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.Serializable;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* @author Rob Austin.
|
||||
*/
|
||||
interface ChronicleMapBuilderI<K, V> {
|
||||
interface ChronicleMapBuilderI<K, V> extends Serializable {
|
||||
|
||||
|
||||
/**
|
||||
@@ -296,4 +297,9 @@ interface ChronicleMapBuilderI<K, V> {
|
||||
ChronicleMapBuilderI<K, V> removeReturnsNull(boolean b);
|
||||
|
||||
|
||||
ChronicleMapBuilderI<K, V> name(String name);
|
||||
|
||||
String name();
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
* 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.replication.ReplicationHub;
|
||||
import net.openhft.chronicle.hash.replication.TcpTransportAndNetworkConfig;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* @author Rob Austin.
|
||||
*/
|
||||
class FindMapByName {
|
||||
|
||||
int maxEntrySize = 10*1024;
|
||||
|
||||
|
||||
private final ReplicationHub replicationHub;
|
||||
private Map<String,ChronicleMapBuilder> map;
|
||||
|
||||
public FindMapByName(byte identifier) throws IOException {
|
||||
|
||||
|
||||
final TcpTransportAndNetworkConfig tcpConfig =
|
||||
TcpTransportAndNetworkConfig.of(8076)
|
||||
.heartBeatInterval(1, TimeUnit.SECONDS);
|
||||
|
||||
replicationHub = ReplicationHub.builder()
|
||||
.maxEntrySize(maxEntrySize)
|
||||
.tcpTransportAndNetwork(tcpConfig)
|
||||
.createWithId(identifier);
|
||||
|
||||
map = (Map)ChronicleMapBuilder.of(CharSequence.class, ChronicleMapBuilder.class)
|
||||
.entrySize(300)
|
||||
.instance()
|
||||
.replicatedViaChannel(replicationHub.createChannel((short) 1)).create();
|
||||
}
|
||||
|
||||
public ChronicleMapBuilder get(String name) throws IOException {
|
||||
return map.get(name);
|
||||
}
|
||||
|
||||
|
||||
public void add(ChronicleMapBuilder builder) throws IOException {
|
||||
builder.name();
|
||||
this.map.put(builder.name(), builder);
|
||||
}
|
||||
}
|
||||
@@ -42,7 +42,7 @@ import static net.openhft.chronicle.map.Objects.hash;
|
||||
import static net.openhft.chronicle.hash.serialization.SizeMarshallers.stopBit;
|
||||
import static net.openhft.chronicle.map.SerializationBuilder.Role.KEY;
|
||||
|
||||
final class SerializationBuilder<E> implements Cloneable {
|
||||
final class SerializationBuilder<E> implements Cloneable, Serializable{
|
||||
|
||||
private static boolean concreteClass(Class c) {
|
||||
return !c.isInterface() && !Modifier.isAbstract(c.getModifiers());
|
||||
|
||||
Reference in New Issue
Block a user