diff --git a/src/main/java/net/openhft/chronicle/hash/ChronicleHashBuilder.java b/src/main/java/net/openhft/chronicle/hash/ChronicleHashBuilder.java index 4ffa24c73..1d997ea2e 100755 --- a/src/main/java/net/openhft/chronicle/hash/ChronicleHashBuilder.java +++ b/src/main/java/net/openhft/chronicle/hash/ChronicleHashBuilder.java @@ -428,7 +428,7 @@ public interface ChronicleHashBuilder instance(); + ChronicleHashInstanceBuilder instance(); /** * Creates a new hash container, storing it's data in off-heap memory, not mapped to any file. @@ -488,11 +488,7 @@ public interface ChronicleHashBuilder statelessClient(InetSocketAddress remoteAddress); - - C createStatelessClient(InetSocketAddress serverAddress) throws IOException; } diff --git a/src/main/java/net/openhft/chronicle/hash/ChronicleHashInstanceConfig.java b/src/main/java/net/openhft/chronicle/hash/ChronicleHashInstanceBuilder.java similarity index 81% rename from src/main/java/net/openhft/chronicle/hash/ChronicleHashInstanceConfig.java rename to src/main/java/net/openhft/chronicle/hash/ChronicleHashInstanceBuilder.java index 8afd3d188..ee6c86575 100644 --- a/src/main/java/net/openhft/chronicle/hash/ChronicleHashInstanceConfig.java +++ b/src/main/java/net/openhft/chronicle/hash/ChronicleHashInstanceBuilder.java @@ -23,11 +23,11 @@ import net.openhft.chronicle.hash.replication.TcpTransportAndNetworkConfig; import java.io.File; import java.io.IOException; -public interface ChronicleHashInstanceConfig { +public interface ChronicleHashInstanceBuilder { - ChronicleHashInstanceConfig replicated(byte identifier, TcpTransportAndNetworkConfig tcpTransportAndNetwork); + ChronicleHashInstanceBuilder replicated(byte identifier, TcpTransportAndNetworkConfig tcpTransportAndNetwork); - ChronicleHashInstanceConfig replicated(SingleChronicleHashReplication replication); + ChronicleHashInstanceBuilder replicated(SingleChronicleHashReplication replication); /** * Configures replication of the hash containers, created by this builder, via so called @@ -49,11 +49,11 @@ public interface ChronicleHashInstanceConfig { * @return this builder object back * @see #replicated(SingleChronicleHashReplication) */ - ChronicleHashInstanceConfig replicatedViaChannel(ReplicationChannel channel); + ChronicleHashInstanceBuilder replicatedViaChannel(ReplicationChannel channel); - ChronicleHashInstanceConfig persistedTo(File file); + ChronicleHashInstanceBuilder persistedTo(File file); - ChronicleHashInstanceConfig name(String name); + ChronicleHashInstanceBuilder name(String name); C create() throws IOException; } diff --git a/src/main/java/net/openhft/chronicle/hash/StatelessClientConfig.java b/src/main/java/net/openhft/chronicle/hash/ChronicleHashStatelessClientBuilder.java similarity index 83% rename from src/main/java/net/openhft/chronicle/hash/StatelessClientConfig.java rename to src/main/java/net/openhft/chronicle/hash/ChronicleHashStatelessClientBuilder.java index 9f24fc72f..153c36ac1 100644 --- a/src/main/java/net/openhft/chronicle/hash/StatelessClientConfig.java +++ b/src/main/java/net/openhft/chronicle/hash/ChronicleHashStatelessClientBuilder.java @@ -35,13 +35,16 @@ import java.util.concurrent.TimeUnit; * consider to be a ClientProxy to {@link ChronicleMap} or {@link ChronicleSet} running on * another host * - * @param the type of {@code ChronicleHash} accessed remotely, {@code ChronicleMap} or + * @param the type of {@code ChronicleHash} accessed remotely, {@code ChronicleMap} or * {@code ChronicleSet} */ -public interface StatelessClientConfig { - StatelessClientConfig timeout(long timeout, TimeUnit units); +public interface ChronicleHashStatelessClientBuilder< + C extends ChronicleHashStatelessClientBuilder, H extends ChronicleHash> { + C timeout(long timeout, TimeUnit units); - StatelessClientConfig name(String name); + C name(String name); - C create() throws IOException; + C tcpBufferSize(int tcpBufferSize); + + H create() throws IOException; } diff --git a/src/main/java/net/openhft/chronicle/hash/replication/ReplicationChannel.java b/src/main/java/net/openhft/chronicle/hash/replication/ReplicationChannel.java index 29ae03377..5c20b5669 100644 --- a/src/main/java/net/openhft/chronicle/hash/replication/ReplicationChannel.java +++ b/src/main/java/net/openhft/chronicle/hash/replication/ReplicationChannel.java @@ -17,14 +17,14 @@ package net.openhft.chronicle.hash.replication; import net.openhft.chronicle.hash.ChronicleHash; -import net.openhft.chronicle.hash.ChronicleHashInstanceConfig; +import net.openhft.chronicle.hash.ChronicleHashInstanceBuilder; import org.jetbrains.annotations.NotNull; import java.io.Serializable; /** * A token which should be created via {@link ReplicationHub#createChannel(int)} call and passed to {@link - * ChronicleHashInstanceConfig#replicatedViaChannel(ReplicationChannel)} method, to establish a channel + * ChronicleHashInstanceBuilder#replicatedViaChannel(ReplicationChannel)} method, to establish a channel * replication. See * the corresponding section in ChronicleMap manual for more information. * diff --git a/src/main/java/net/openhft/chronicle/hash/replication/ReplicationHub.java b/src/main/java/net/openhft/chronicle/hash/replication/ReplicationHub.java index 2240a5da5..635e71613 100644 --- a/src/main/java/net/openhft/chronicle/hash/replication/ReplicationHub.java +++ b/src/main/java/net/openhft/chronicle/hash/replication/ReplicationHub.java @@ -18,7 +18,7 @@ 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 net.openhft.chronicle.hash.ChronicleHashInstanceBuilder; import net.openhft.chronicle.map.ChronicleMap; import net.openhft.chronicle.set.ChronicleSet; import org.jetbrains.annotations.NotNull; @@ -46,7 +46,7 @@ import java.util.Arrays; * .persistedTo(myMapFile) // optional, for this example * .create();} * - * @see ChronicleHashInstanceConfig#replicatedViaChannel(ReplicationChannel) + * @see ChronicleHashInstanceBuilder#replicatedViaChannel(ReplicationChannel) * @see ReplicationChannel */ public final class ReplicationHub extends AbstractReplication { diff --git a/src/main/java/net/openhft/chronicle/map/ChronicleMapBuilder.java b/src/main/java/net/openhft/chronicle/map/ChronicleMapBuilder.java index 75d82388a..35ea5b639 100755 --- a/src/main/java/net/openhft/chronicle/map/ChronicleMapBuilder.java +++ b/src/main/java/net/openhft/chronicle/map/ChronicleMapBuilder.java @@ -46,9 +46,7 @@ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.net.InetSocketAddress; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; -import java.util.Map; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; @@ -99,7 +97,8 @@ import static net.openhft.lang.model.DataValueGenerator.firstPrimitiveFieldType; * @see ChronicleSetBuilder */ public final class ChronicleMapBuilder implements Cloneable, - ChronicleHashBuilder, ChronicleMapBuilder>, Serializable { + ChronicleHashBuilder, ChronicleMapBuilder>, + MapBuilder>, Serializable { static final byte UDP_REPLICATION_MODIFICATION_ITERATOR_ID = (byte) 128; private static final int DEFAULT_KEY_OR_VALUE_SIZE = 120; @@ -108,7 +107,6 @@ public final class ChronicleMapBuilder implements Cloneable, private static final int MAX_SEGMENTS_TO_CHAISE_COMPACT_MULTI_MAPS = (1 << 20); private static final Logger LOG = LoggerFactory.getLogger(ChronicleMapBuilder.class.getName()); - static final long DEFAULT_STATELESS_CLIENT_TIMEOUT = TimeUnit.SECONDS.toMillis(10); private static final StringBuilder EMTRY_STRING_BUILDER = new StringBuilder(); @@ -117,9 +115,6 @@ public final class ChronicleMapBuilder implements Cloneable, private static final int XML_SERIALIZATION = 1; private static final int BINARY_SERIALIZATION = 2; - - private List jsonConverters = new ArrayList(); - private static boolean isDefined(double config) { return !Double.isNaN(config); } @@ -288,30 +283,6 @@ public final class ChronicleMapBuilder implements Cloneable, return this; } - - /** - * Used to a add a custom xStream converter, this converter may be used while chronicle maps - * serialises/deserialized and object(s) to and from JSON. You should add a custom converter if - * you wish to customise the JSON format, or support object(s) that can not be serialised into - * JSON. - * - * The method signature purposely has a loose coupling to xStream, it exposes {@code Object} - * parameter, rather than the com.thoughtworks.xstream.jsonConverters.Converter this is so that - * the the {@code ChronicleMapBuilder} class does not require xStream artifact/jars, however the - * parameter pass must be of type com.thoughtworks.xstream.jsonConverters.Converter as no other - * type is currently accepted. - * - * @param converter an xStream converter of type com.thoughtworks.xstream.jsonConverters.Converter - * @return this builder back - * @see ChronicleMap#putAll(java.io.File) - * @see ChronicleMap#putAll(java.io.File) - */ - public ChronicleMapBuilder addJsonConverter(Object converter) { - this.jsonConverters.add(converter); - return this; - } - - private double averageKeySize() { return averageKeyOrValueSize(averageKeySize, keyBuilder); } @@ -417,10 +388,6 @@ public final class ChronicleMapBuilder implements Cloneable, return this; } - List jsonConverters() { - return this.jsonConverters; - } - static class EntrySizeInfo { final double averageEntrySize; final int worstAlignment; @@ -858,22 +825,7 @@ public final class ChronicleMapBuilder implements Cloneable, return errorListener; } - /** - * Configures if the maps created by this builder should return {@code null} instead of previous - * mapped values on {@link ChronicleMap#put(Object, Object) ChornicleMap.put(key, value)} calls. - *

{@link Map#put(Object, Object) Map.put()} returns the previous value, functionality - * which is rarely used but fairly cheap for {@link HashMap}. In the case, for an off heap - * collection, it has to create a new object and deserialize the data from off-heap memory. It's - * expensive for something you probably don't use.

By default, of cause, {@code - * ChronicleMap} conforms the general {@code Map} contract and returns the previous mapped value - * on {@code put()} calls. - * - * @param putReturnsNull {@code true} if you want {@link ChronicleMap#put(Object, Object) - * ChronicleMap.put()} to not return the value that was replaced but - * instead return {@code null} - * @return this builder back - * @see #removeReturnsNull(boolean) - */ + @Override public ChronicleMapBuilder putReturnsNull(boolean putReturnsNull) { this.putReturnsNull = putReturnsNull; return this; @@ -883,21 +835,7 @@ public final class ChronicleMapBuilder implements Cloneable, return putReturnsNull; } - /** - * Configures if the maps created by this builder should return {@code null} instead of the last - * mapped value on {@link ChronicleMap#remove(Object) ChronicleMap.remove(key)} calls.

- *

{@link Map#remove(Object) Map.remove()} returns the previous value, functionality which is - * rarely used but fairly cheap for {@link HashMap}. In the case, for an off heap collection, it - * has to create a new object and deserialize the data from off-heap memory. It's expensive for - * something you probably don't use.

By default, of cause, {@code ChronicleMap} conforms - * the general {@code Map} contract and returns the mapped value on {@code remove()} calls. - * - * @param removeReturnsNull {@code true} if you want {@link ChronicleMap#remove(Object) - * ChronicleMap.remove()} to not return the value of the removed entry - * but instead return {@code null} - * @return this builder back - * @see #putReturnsNull(boolean) - */ + @Override public ChronicleMapBuilder removeReturnsNull(boolean removeReturnsNull) { this.removeReturnsNull = removeReturnsNull; return this; @@ -1274,24 +1212,11 @@ public final class ChronicleMapBuilder implements Cloneable, } @Override - public ChronicleHashInstanceConfig> instance() { - return new MapInstanceConfig<>(this.clone(), singleHashReplication, null, null, null, + public ChronicleHashInstanceBuilder> instance() { + return new MapInstanceBuilder<>(this.clone(), singleHashReplication, null, null, null, new AtomicBoolean(false)); } - @Override - public ChronicleMap createStatelessClient(InetSocketAddress serverAddress) - throws IOException { - return statelessClient(serverAddress).create(); - } - - ChronicleMap createStatelessClient(StatelessMapConfig statelessClientConfig) - throws IOException { - pushingToMapEventListener(); - preMapConstruction(false); - return new StatelessChronicleMap<>(statelessClientConfig, this); - } - @Override public ChronicleMap createPersistedTo(File file) throws IOException { // clone() to make this builder instance thread-safe, because createWithFile() method @@ -1300,13 +1225,6 @@ public final class ChronicleMapBuilder implements Cloneable, return clone().createWithFile(file, singleHashReplication, null); } - @Override - public StatelessClientConfig> statelessClient( - InetSocketAddress remoteAddress) { - return new StatelessMapConfig<>(clone(), remoteAddress, DEFAULT_STATELESS_CLIENT_TIMEOUT, - null, new AtomicBoolean(false)); - } - @Override public ChronicleMap create() { // clone() to make this builder instance thread-safe, because createWithoutFile() method @@ -1315,7 +1233,7 @@ public final class ChronicleMapBuilder implements Cloneable, return clone().createWithoutFile(singleHashReplication, null); } - ChronicleMap create(MapInstanceConfig ib) throws IOException { + ChronicleMap create(MapInstanceBuilder ib) throws IOException { if (ib.file != null) { return createWithFile(ib.file, ib.singleHashReplication, ib.channel); } else { @@ -1450,7 +1368,8 @@ public final class ChronicleMapBuilder implements Cloneable, ChronicleMapBuilder cmb = clone(); cmb.pushTo((InetSocketAddress[]) null); for (int i = 0; i < pushToAddresses.length; i++) { - statelessClients[i] = cmb.createStatelessClient(pushToAddresses[i]); + statelessClients[i] = ChronicleMapStatelessClientBuilder.of(pushToAddresses[i]) + .create(); } eventListener = (MapEventListener) constructor.newInstance((Object) statelessClients); diff --git a/src/main/java/net/openhft/chronicle/map/ChronicleMapStatelessClientBuilder.java b/src/main/java/net/openhft/chronicle/map/ChronicleMapStatelessClientBuilder.java new file mode 100644 index 000000000..56671f35c --- /dev/null +++ b/src/main/java/net/openhft/chronicle/map/ChronicleMapStatelessClientBuilder.java @@ -0,0 +1,141 @@ +/* + * 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.ChronicleHashStatelessClientBuilder; +import net.openhft.lang.MemoryUnit; + +import java.io.IOException; +import java.net.InetSocketAddress; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; + +/** + * @author Rob Austin. + */ +public final class ChronicleMapStatelessClientBuilder implements + ChronicleHashStatelessClientBuilder, + ChronicleMap>, + MapBuilder> { + + public static ChronicleMapStatelessClientBuilder of( + InetSocketAddress serverAddress) { + return new ChronicleMapStatelessClientBuilder<>(serverAddress); + } + + // This method is meaningful, despite you could call of().create(), because Java 7 doesn't + // infer type parameters if case of chained calls, like this; and then you couldn't omit class + // name. Then it is + // ChronicleMapStatelessClientBuilder..of(addr).create() + // vs + // createClientOf(addr) // static import + // -- the second is really shorter. + + /** + * Equivalent of {@code ChronicleMapStatelessClientBuilder.of(serverAddress).create()}. + * + * @param serverAddress address of the server map + * @param key type of the map + * @param value type of the map + * @return stateless client of the ChronicleMap of server + */ + public static ChronicleMap createClientOf(InetSocketAddress serverAddress) + throws IOException { + return ChronicleMapStatelessClientBuilder.of(serverAddress).create(); + } + + private final InetSocketAddress remoteAddress; + private boolean putReturnsNull = false; + private boolean removeReturnsNull = false; + private long timeoutMs = TimeUnit.SECONDS.toMillis(10); + private String name; + private int tcpBufferSize = (int) MemoryUnit.KILOBYTES.toBytes(64); + + private final AtomicBoolean used = new AtomicBoolean(false); + + ChronicleMapStatelessClientBuilder(InetSocketAddress remoteAddress) { + this.remoteAddress = remoteAddress; + } + + InetSocketAddress remoteAddress() { + return remoteAddress; + } + + @Override + public ChronicleMapStatelessClientBuilder putReturnsNull(boolean putReturnsNull) { + this.putReturnsNull = putReturnsNull; + return this; + } + + boolean putReturnsNull() { + return putReturnsNull; + } + + @Override + public ChronicleMapStatelessClientBuilder removeReturnsNull(boolean removeReturnsNull) { + this.removeReturnsNull = removeReturnsNull; + return this; + } + + boolean removeReturnsNull() { + return removeReturnsNull; + } + + @Override + public ChronicleMapStatelessClientBuilder timeout(long timeout, TimeUnit units) { + this.timeoutMs = units.toMillis(timeout); + return this; + } + + long timeoutMs() { + return timeoutMs; + } + + @Override + public ChronicleMapStatelessClientBuilder name(String name) { + this.name = name; + return this; + } + + String name() { + return name; + } + + @Override + public ChronicleMapStatelessClientBuilder tcpBufferSize(int tcpBufferSize) { + this.tcpBufferSize = tcpBufferSize; + return this; + } + + int tcpBufferSize() { + return tcpBufferSize; + } + + @Override + public ChronicleMap create() throws IOException { + if (!used.getAndSet(true)) { + return new StatelessChronicleMap<>(this); + } else { + throw new IllegalStateException( + "A stateless client has already been created using this builder. " + + "Create a new ChronicleMapStatelessClientBuilder " + + "to create a new stateless client"); + } + } +} diff --git a/src/main/java/net/openhft/chronicle/map/MapBuilder.java b/src/main/java/net/openhft/chronicle/map/MapBuilder.java new file mode 100644 index 000000000..3ff20d183 --- /dev/null +++ b/src/main/java/net/openhft/chronicle/map/MapBuilder.java @@ -0,0 +1,74 @@ +/* + * 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 java.util.HashMap; +import java.util.Map; + +/** + * Common configurations of {@link ChronicleMapBuilder} and + * {@link ChronicleMapStatelessClientBuilder}. + * + * @param concrete builder class + */ +public interface MapBuilder> { + + /** + * Configures if the maps created by this {@code MapBuilder} should return {@code null} + * instead of previous mapped values on {@link ChronicleMap#put(Object, Object) + * ChornicleMap.put(key, value)} calls. + * + *

{@link Map#put(Object, Object) Map.put()} returns the previous value, functionality + * which is rarely used but fairly cheap for simple in-process, on-heap implementations like + * {@link HashMap}. But an off-heap collection has to create a new object and deserialize + * the data from off-heap memory. A collection hiding remote queries over the network should + * send the value back in addition to that. It's expensive for something you probably don't use. + * + *

By default, of cause, {@code ChronicleMap} conforms the general {@code Map} contract and + * returns the previous mapped value on {@code put()} calls. + * + * @param putReturnsNull {@code true} if you want {@link ChronicleMap#put(Object, Object) + * ChronicleMap.put()} to not return the value that was replaced but + * instead return {@code null} + * @return {@code MapBuilder} with this configuration applied + * @see #removeReturnsNull(boolean) + */ + C putReturnsNull(boolean putReturnsNull); + + + /** + * Configures if the maps created by this {@code MapBuilder} should return {@code null} + * instead of the last mapped value on {@link ChronicleMap#remove(Object) + * ChronicleMap.remove(key)} calls. + * + *

{@link Map#remove(Object) Map.remove()} returns the previous value, functionality which is + * rarely used but fairly cheap for simple in-process, on-heap implementations like {@link + * HashMap}. But an off-heap collection has to create a new object and deserialize the data + * from off-heap memory. A collection hiding remote queries over the network should send + * the value back in addition to that. It's expensive for something you probably don't use. + * + *

By default, of cause, {@code ChronicleMap} conforms the general {@code Map} contract and + * returns the mapped value on {@code remove()} calls. + * + * @param removeReturnsNull {@code true} if you want {@link ChronicleMap#remove(Object) + * ChronicleMap.remove()} to not return the value of the removed entry + * but instead return {@code null} + * @return {@code MapBuilder} with this configuration applied + * @see #putReturnsNull(boolean) + */ + C removeReturnsNull(boolean removeReturnsNull); +} diff --git a/src/main/java/net/openhft/chronicle/map/MapInstanceConfig.java b/src/main/java/net/openhft/chronicle/map/MapInstanceBuilder.java similarity index 62% rename from src/main/java/net/openhft/chronicle/map/MapInstanceConfig.java rename to src/main/java/net/openhft/chronicle/map/MapInstanceBuilder.java index 67959f6a8..6e201f834 100644 --- a/src/main/java/net/openhft/chronicle/map/MapInstanceConfig.java +++ b/src/main/java/net/openhft/chronicle/map/MapInstanceBuilder.java @@ -16,7 +16,7 @@ package net.openhft.chronicle.map; -import net.openhft.chronicle.hash.ChronicleHashInstanceConfig; +import net.openhft.chronicle.hash.ChronicleHashInstanceBuilder; import net.openhft.chronicle.hash.replication.ReplicationChannel; import net.openhft.chronicle.hash.replication.SingleChronicleHashReplication; import net.openhft.chronicle.hash.replication.TcpTransportAndNetworkConfig; @@ -26,21 +26,21 @@ import java.io.IOException; import java.io.Serializable; import java.util.concurrent.atomic.AtomicBoolean; -final class MapInstanceConfig - implements ChronicleHashInstanceConfig>, Serializable{ +final class MapInstanceBuilder + implements ChronicleHashInstanceBuilder>, Serializable{ final ChronicleMapBuilder mapBuilder; - final SingleChronicleHashReplication singleHashReplication; - final ReplicationChannel channel; - final File file; - final String name; + SingleChronicleHashReplication singleHashReplication; + ReplicationChannel channel; + File file; + String name; final AtomicBoolean used; - MapInstanceConfig(ChronicleMapBuilder mapBuilder, - SingleChronicleHashReplication singleHashReplication, - ReplicationChannel channel, - File file, String name, AtomicBoolean used) { + MapInstanceBuilder(ChronicleMapBuilder mapBuilder, + SingleChronicleHashReplication singleHashReplication, + ReplicationChannel channel, + File file, String name, AtomicBoolean used) { this.mapBuilder = mapBuilder; this.singleHashReplication = singleHashReplication; this.channel = channel; @@ -50,30 +50,36 @@ final class MapInstanceConfig } @Override - public MapInstanceConfig replicated( + public MapInstanceBuilder replicated( byte identifier, TcpTransportAndNetworkConfig tcpTransportAndNetwork) { return replicated(SingleChronicleHashReplication.builder() .tcpTransportAndNetwork(tcpTransportAndNetwork).createWithId(identifier)); } @Override - public MapInstanceConfig replicated(SingleChronicleHashReplication replication) { - return new MapInstanceConfig<>(mapBuilder, replication, null, file, name, used); + public MapInstanceBuilder replicated(SingleChronicleHashReplication replication) { + singleHashReplication = replication; + channel = null; + return this; } @Override - public MapInstanceConfig replicatedViaChannel(ReplicationChannel channel) { - return new MapInstanceConfig<>(mapBuilder, null, channel, file, name, used); + public MapInstanceBuilder replicatedViaChannel(ReplicationChannel channel) { + singleHashReplication = null; + this.channel = channel; + return this; } @Override - public MapInstanceConfig persistedTo(File file) { - return new MapInstanceConfig<>(mapBuilder, singleHashReplication, channel, file, name, used); + public MapInstanceBuilder persistedTo(File file) { + this.file = file; + return this; } @Override - public MapInstanceConfig name(String name) { - return new MapInstanceConfig<>(mapBuilder, singleHashReplication, channel, file, name, used); + public MapInstanceBuilder name(String name) { + this.name = name; + return this; } @Override diff --git a/src/main/java/net/openhft/chronicle/map/ReplicationHubFindByName.java b/src/main/java/net/openhft/chronicle/map/ReplicationHubFindByName.java index 3f4aade83..291a15620 100644 --- a/src/main/java/net/openhft/chronicle/map/ReplicationHubFindByName.java +++ b/src/main/java/net/openhft/chronicle/map/ReplicationHubFindByName.java @@ -18,7 +18,7 @@ package net.openhft.chronicle.map; import net.openhft.chronicle.hash.ChronicleHash; import net.openhft.chronicle.hash.ChronicleHashBuilder; -import net.openhft.chronicle.hash.ChronicleHashInstanceConfig; +import net.openhft.chronicle.hash.ChronicleHashInstanceBuilder; import net.openhft.chronicle.hash.replication.ReplicationChannel; import net.openhft.chronicle.hash.replication.ReplicationHub; import org.slf4j.Logger; @@ -39,7 +39,7 @@ class ReplicationHubFindByName implements FindByName { public static final int MAP_BY_NAME_CHANNEL = 1; private final AtomicInteger nextFreeChannel = new AtomicInteger(2); - private final Map map; + private final Map map; private final ReplicationHub replicationHub; /** @@ -52,12 +52,12 @@ class ReplicationHubFindByName implements FindByName { this.replicationHub = replicationHub; ReplicationChannel channel = replicationHub.createChannel((short) MAP_BY_NAME_CHANNEL); - final MapEventListener listener = - new MapEventListener() { + final MapEventListener listener = + new MapEventListener() { // creates a map based on the details that are sent to the map of builders @Override - public void onPut(CharSequence key, MapInstanceConfig value, - MapInstanceConfig replacedValue) { + public void onPut(CharSequence key, MapInstanceBuilder value, + MapInstanceBuilder replacedValue) { super.onPut(key, value, replacedValue); boolean added = replacedValue == null; if (!added || value == null) @@ -80,7 +80,7 @@ class ReplicationHubFindByName implements FindByName { }; this.map = (Map) ChronicleMapBuilder - .of(CharSequence.class, MapInstanceConfig.class) + .of(CharSequence.class, MapInstanceBuilder.class) .averageKeySize(10) .averageValueSize(4000) .entries(128) @@ -93,7 +93,7 @@ class ReplicationHubFindByName implements FindByName { } public T create( - MapInstanceConfig config) + MapInstanceBuilder config) throws IOException, TimeoutException, InterruptedException { int withChannelId = nextFreeChannel.incrementAndGet(); @@ -109,11 +109,11 @@ class ReplicationHubFindByName implements FindByName { return (T) get(name).create(); } - MapInstanceConfig get(String name) throws IllegalArgumentException, + MapInstanceBuilder get(String name) throws IllegalArgumentException, TimeoutException, InterruptedException { - MapInstanceConfig config = waitTillEntryReceived(5000, name); + MapInstanceBuilder config = waitTillEntryReceived(5000, name); if (config == null) throw new IllegalArgumentException("A map name=" + name + " can not be found."); @@ -126,11 +126,11 @@ class ReplicationHubFindByName implements FindByName { * @param timeOutMs timeout in milliseconds * @throws InterruptedException */ - private MapInstanceConfig waitTillEntryReceived(final int timeOutMs, String name) + private MapInstanceBuilder waitTillEntryReceived(final int timeOutMs, String name) throws TimeoutException, InterruptedException { int t = 0; for (; t < timeOutMs; t++) { - MapInstanceConfig config = map.get(name); + MapInstanceBuilder config = map.get(name); if (config != null) return config; @@ -156,7 +156,7 @@ class ReplicationHubFindByName implements FindByName { return (T) get(name).persistedTo(file).create(); } - private ChronicleHashInstanceConfig toReplicatedViaChannel(ChronicleMapBuilder builder) { + private ChronicleHashInstanceBuilder toReplicatedViaChannel(ChronicleMapBuilder builder) { int channelId = nextFreeChannel.incrementAndGet(); if (channelId > replicationHub.maxNumberOfChannels()) throw new IllegalStateException("There are no more free channels, you can increase the number " + @@ -164,7 +164,7 @@ class ReplicationHubFindByName implements FindByName { return builder.instance().replicatedViaChannel(replicationHub.createChannel((short) channelId)); } - private ChronicleHashInstanceConfig toReplicatedViaChannel(ChronicleHashBuilder builder, int + private ChronicleHashInstanceBuilder toReplicatedViaChannel(ChronicleHashBuilder builder, int withChannelId) { return builder.instance().replicatedViaChannel(replicationHub.createChannel((short) withChannelId)); } diff --git a/src/main/java/net/openhft/chronicle/map/StatelessChronicleMap.java b/src/main/java/net/openhft/chronicle/map/StatelessChronicleMap.java index 7fc971ffd..b8c64cac7 100644 --- a/src/main/java/net/openhft/chronicle/map/StatelessChronicleMap.java +++ b/src/main/java/net/openhft/chronicle/map/StatelessChronicleMap.java @@ -43,6 +43,7 @@ import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.locks.ReentrantLock; import static java.nio.ByteBuffer.allocateDirect; +import static java.util.Collections.emptyList; import static net.openhft.chronicle.map.AbstractChannelReplicator.SIZE_OF_SIZE; import static net.openhft.chronicle.map.AbstractChannelReplicator.SIZE_OF_TRANSACTION_ID; import static net.openhft.chronicle.map.StatelessChronicleMap.EventId.*; @@ -59,7 +60,6 @@ class StatelessChronicleMap implements ChronicleMap, Closeable, Clon private final ByteBuffer connectionOutBuffer = ByteBuffer.wrap(connectionByte); private final String name; - private final List jsonConverters; private ByteBuffer outBuffer; private ByteBufferBytes outBytes; @@ -78,11 +78,12 @@ class StatelessChronicleMap implements ChronicleMap, Closeable, Clon @Nullable private CloseablesManager closeables; - @NotNull - private final StatelessMapConfig config; - - private final Class kClass; - private final Class vClass; + private final InetSocketAddress remoteAddress; + private final long timeoutMs; + private final int tcpBufferSize; + + private Class kClass; + private Class vClass; private boolean putReturnsNull; private boolean removeReturnsNull; @@ -144,12 +145,13 @@ class StatelessChronicleMap implements ChronicleMap, Closeable, Clon @NotNull private final AtomicLong transactionID = new AtomicLong(0); - StatelessChronicleMap(@NotNull final StatelessMapConfig config, - @NotNull final ChronicleMapBuilder chronicleMapBuilder) { - - this.config = config; - this.jsonConverters = chronicleMapBuilder.jsonConverters(); - + StatelessChronicleMap(@NotNull final ChronicleMapStatelessClientBuilder config) { + this.remoteAddress = config.remoteAddress(); + this.timeoutMs = config.timeoutMs(); + this.tcpBufferSize = config.tcpBufferSize(); + this.name = config.name(); + this.putReturnsNull = config.putReturnsNull(); + this.removeReturnsNull = config.removeReturnsNull(); outBuffer = allocateDirect(128).order(ByteOrder.nativeOrder()); outBytes = new ByteBufferBytes(outBuffer.slice()); @@ -157,22 +159,19 @@ class StatelessChronicleMap implements ChronicleMap, Closeable, Clon inBuffer = allocateDirect(128).order(ByteOrder.nativeOrder()); inBytes = new ByteBufferBytes(inBuffer.slice()); - this.vClass = chronicleMapBuilder.valueBuilder.eClass; - this.kClass = chronicleMapBuilder.keyBuilder.eClass; - this.name = config.name(); - - attemptConnect(config.remoteAddress()); + attemptConnect(remoteAddress); checkVersion(); loadKeyValueSerializers(); - - this.putReturnsNull = chronicleMapBuilder.putReturnsNull(); - this.removeReturnsNull = chronicleMapBuilder.removeReturnsNull(); } private void loadKeyValueSerializers() { - final SerializationBuilder keyBuilder = fetchObject(SerializationBuilder.class, KEY_BUILDER); - final SerializationBuilder valueBuilder = fetchObject(SerializationBuilder.class, VALUE_BUILDER); + final SerializationBuilder keyBuilder = + fetchObject(SerializationBuilder.class, KEY_BUILDER); + kClass = keyBuilder.eClass; + final SerializationBuilder valueBuilder = + fetchObject(SerializationBuilder.class, VALUE_BUILDER); + vClass = valueBuilder.eClass; keyReaderWithSize = new ReaderWithSize(keyBuilder); keyWriterWithSize = new WriterWithSize(keyBuilder, outBufferResizer); @@ -198,12 +197,12 @@ class StatelessChronicleMap implements ChronicleMap, Closeable, Clon @Override public void getAll(File toFile) throws IOException { - JsonSerializer.getAll(toFile, this, jsonConverters); + JsonSerializer.getAll(toFile, this, emptyList()); } @Override public void putAll(File fromFile) throws IOException { - JsonSerializer.putAll(fromFile, this, jsonConverters); + JsonSerializer.putAll(fromFile, this, emptyList()); } @Override @@ -255,7 +254,7 @@ class StatelessChronicleMap implements ChronicleMap, Closeable, Clon try { result = AbstractChannelReplicator.openSocketChannel(closeables); - if (!result.connect(config.remoteAddress())) { + if (!result.connect(remoteAddress)) { try { Thread.sleep(100); } catch (InterruptedException e) { @@ -361,7 +360,7 @@ class StatelessChronicleMap implements ChronicleMap, Closeable, Clon connectionByte[0] = STATELESS_CLIENT_IDENTIFIER; this.connectionOutBuffer.clear(); - long timeoutTime = System.currentTimeMillis() + config.timeoutMs(); + long timeoutTime = System.currentTimeMillis() + timeoutMs; // write a single byte while (connectionOutBuffer.hasRemaining()) { @@ -619,8 +618,6 @@ class StatelessChronicleMap implements ChronicleMap, Closeable, Clon } private Bytes resizeBufferOutBuffer(int newCapacity, long start) { - - assert outBytesLock.isHeldByCurrentThread(); assert !inBytesLock.isHeldByCurrentThread(); @@ -720,7 +717,7 @@ class StatelessChronicleMap implements ChronicleMap, Closeable, Clon final long sizeLocation = writeEventAnSkip(VALUES); final long startTime = System.currentTimeMillis(); - timeoutTime = System.currentTimeMillis() + config.timeoutMs(); + timeoutTime = System.currentTimeMillis() + timeoutMs; transactionId = send(sizeLocation, startTime); } finally { outBytesLock.unlock(); @@ -764,7 +761,7 @@ class StatelessChronicleMap implements ChronicleMap, Closeable, Clon try { final long sizeLocation = writeEventAnSkip(ENTRY_SET); final long startTime = System.currentTimeMillis(); - timeoutTime = System.currentTimeMillis() + config.timeoutMs(); + timeoutTime = System.currentTimeMillis() + timeoutMs; transactionId = send(sizeLocation, startTime); } finally { outBytesLock.unlock(); @@ -818,7 +815,7 @@ class StatelessChronicleMap implements ChronicleMap, Closeable, Clon try { final long sizeLocation = writeEventAnSkip(ENTRY_SET); final long startTime = System.currentTimeMillis(); - timeoutTime = System.currentTimeMillis() + config.timeoutMs(); + timeoutTime = System.currentTimeMillis() + timeoutMs; transactionId = send(sizeLocation, startTime); } finally { outBytesLock.unlock(); @@ -871,7 +868,7 @@ class StatelessChronicleMap implements ChronicleMap, Closeable, Clon outBytesLock.unlock(); } final long startTime = System.currentTimeMillis(); - final long timeoutTime = startTime + config.timeoutMs(); + final long timeoutTime = startTime + timeoutMs; final long transactionId; final int numberOfEntries = map.size(); int numberOfEntriesReadSoFar = 0; @@ -940,7 +937,7 @@ class StatelessChronicleMap implements ChronicleMap, Closeable, Clon try { final long sizeLocation = writeEventAnSkip(KEY_SET); final long startTime = System.currentTimeMillis(); - timeoutTime = startTime + config.timeoutMs(); + timeoutTime = startTime + timeoutMs; transactionId = send(sizeLocation, startTime); } finally { outBytesLock.unlock(); @@ -980,7 +977,7 @@ class StatelessChronicleMap implements ChronicleMap, Closeable, Clon assert !outBytesLock.isHeldByCurrentThread(); - final long timeoutTime = startTime + this.config.timeoutMs(); + final long timeoutTime = startTime + this.timeoutMs; // receive inBytesLock.lock(); @@ -1094,12 +1091,12 @@ class StatelessChronicleMap implements ChronicleMap, Closeable, Clon assert !inBytesLock.isHeldByCurrentThread(); long transactionId = nextUniqueTransaction(startTime); - final long timeoutTime = startTime + this.config.timeoutMs(); + final long timeoutTime = startTime + this.timeoutMs; try { for (; ; ) { if (clientChannel == null) { - lazyConnect(config.timeoutMs(), config.remoteAddress()); + lazyConnect(timeoutMs, remoteAddress); } try { @@ -1115,7 +1112,7 @@ class StatelessChronicleMap implements ChronicleMap, Closeable, Clon } catch (@NotNull java.nio.channels.ClosedChannelException | ClosedConnectionException e) { checkTimeout(timeoutTime); - lazyConnect(config.timeoutMs(), config.remoteAddress()); + lazyConnect(timeoutMs, remoteAddress); } } } catch (IOException e) { @@ -1187,7 +1184,7 @@ class StatelessChronicleMap implements ChronicleMap, Closeable, Clon for (int i = stes.size() - 1; i > 0 && stes.get(i).getClassName().startsWith("Thread"); i--) { stes.remove(i); } - InetSocketAddress address = config.remoteAddress(); + InetSocketAddress address = remoteAddress; stes.add(new StackTraceElement("~ remote", "tcp ~", address.getHostName(), address.getPort())); StackTraceElement[] stackTrace2 = Thread.currentThread().getStackTrace(); //noinspection ManualArrayToCollectionCopy @@ -1336,7 +1333,7 @@ class StatelessChronicleMap implements ChronicleMap, Closeable, Clon } - long lagestEntrySoFar = 0; + long largestEntrySoFar = 0; private long limitOfLast = 0; private void writeBytesToSocket(long timeoutTime) throws IOException { @@ -1347,7 +1344,7 @@ class StatelessChronicleMap implements ChronicleMap, Closeable, Clon // if we have other threads waiting to send and the buffer is not full, let the other threads // write to the buffer if (outBytesLock.hasQueuedThreads() && - outBytes.position() + lagestEntrySoFar <= config.tcpBufferSize()) { + outBytes.position() + largestEntrySoFar <= tcpBufferSize) { return; } @@ -1355,8 +1352,8 @@ class StatelessChronicleMap implements ChronicleMap, Closeable, Clon outBuffer.position(0); int sizeOfThisMessage = (int) (outBuffer.limit() - limitOfLast); - if (lagestEntrySoFar < sizeOfThisMessage) - lagestEntrySoFar = sizeOfThisMessage; + if (largestEntrySoFar < sizeOfThisMessage) + largestEntrySoFar = sizeOfThisMessage; limitOfLast = outBuffer.limit(); @@ -1371,8 +1368,7 @@ class StatelessChronicleMap implements ChronicleMap, Closeable, Clon // if we have queued threads then we don't have to write all the bytes as the other // threads will write the remains bytes. if (outBuffer.remaining() > 0 && outBytesLock.hasQueuedThreads() && - outBuffer.remaining() + lagestEntrySoFar <= config.tcpBufferSize()) { - + outBuffer.remaining() + largestEntrySoFar <= tcpBufferSize) { LOG.debug("continuing - without all the data being written to the buffer as " + "it will be written by the next thread"); @@ -1475,7 +1471,7 @@ class StatelessChronicleMap implements ChronicleMap, Closeable, Clon private V readValue(long transactionId, long startTime, ThreadLocalCopies copies, V usingValue) { assert !outBytesLock.isHeldByCurrentThread(); - long timeoutTime = startTime + this.config.timeoutMs(); + long timeoutTime = startTime + this.timeoutMs; // receive inBytesLock.lock(); @@ -1495,7 +1491,7 @@ class StatelessChronicleMap implements ChronicleMap, Closeable, Clon private O readObject(long transactionId, long startTime) { assert !outBytesLock.isHeldByCurrentThread(); - long timeoutTime = startTime + this.config.timeoutMs(); + long timeoutTime = startTime + this.timeoutMs; // receive inBytesLock.lock(); @@ -1509,7 +1505,7 @@ class StatelessChronicleMap implements ChronicleMap, Closeable, Clon private boolean readBoolean(long transactionId, long startTime) { assert !outBytesLock.isHeldByCurrentThread(); - long timeoutTime = startTime + this.config.timeoutMs(); + long timeoutTime = startTime + this.timeoutMs; // receive inBytesLock.lock(); @@ -1524,7 +1520,7 @@ class StatelessChronicleMap implements ChronicleMap, Closeable, Clon private int readInt(long transactionId, long startTime) { assert !outBytesLock.isHeldByCurrentThread(); assert inBytesLock.isHeldByCurrentThread(); - long timeoutTime = startTime + this.config.timeoutMs(); + long timeoutTime = startTime + this.timeoutMs; // receive inBytesLock.lock(); @@ -1704,7 +1700,7 @@ class StatelessChronicleMap implements ChronicleMap, Closeable, Clon } private void readVoid(long transactionId, long startTime) { - long timeoutTime = startTime + this.config.timeoutMs(); + long timeoutTime = startTime + this.timeoutMs; // receive inBytesLock.lock(); @@ -1729,7 +1725,7 @@ class StatelessChronicleMap implements ChronicleMap, Closeable, Clon outBytesLock.unlock(); } - long timeoutTime = startTime + this.config.timeoutMs(); + long timeoutTime = startTime + this.timeoutMs; // receive inBytesLock.lock(); diff --git a/src/main/java/net/openhft/chronicle/map/StatelessMapConfig.java b/src/main/java/net/openhft/chronicle/map/StatelessMapConfig.java deleted file mode 100644 index 204b317a2..000000000 --- a/src/main/java/net/openhft/chronicle/map/StatelessMapConfig.java +++ /dev/null @@ -1,96 +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.StatelessClientConfig; -import net.openhft.lang.MemoryUnit; - -import java.io.IOException; -import java.net.InetSocketAddress; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicBoolean; - -/** - * @author Rob Austin. - */ -class StatelessMapConfig implements StatelessClientConfig> { - - private final ChronicleMapBuilder mapBuilder; - private final InetSocketAddress remoteAddress; - private final long timeoutMs; - private final String name; - - private int tcpBufferSize = (int) MemoryUnit.KILOBYTES.toBytes(64); - - private final AtomicBoolean used; - - StatelessMapConfig(ChronicleMapBuilder mapBuilder, - InetSocketAddress remoteAddress, long timeoutMs, - String name, AtomicBoolean used) { - this.mapBuilder = mapBuilder; - this.remoteAddress = remoteAddress; - this.timeoutMs = timeoutMs; - this.name = name; - this.used = used; - } - - public long timeoutMs() { - return timeoutMs; - } - - public InetSocketAddress remoteAddress() { - return remoteAddress; - } - - @Override - public StatelessMapConfig timeout(long timeout, TimeUnit units) { - return new StatelessMapConfig<>(mapBuilder, remoteAddress, units.toMillis(timeout), - name, used); - } - - @Override - public StatelessClientConfig> name(String name) { - return new StatelessMapConfig<>(mapBuilder, remoteAddress, timeoutMs, name, used); - } - - String name() { - return name; - } - - @Override - public ChronicleMap create() throws IOException { - if (!used.getAndSet(true)) { - return mapBuilder.createStatelessClient(this); - } else { - throw new IllegalStateException( - "A stateless client has already been created using this config chain. " + - "Create a new StatelessClientConfig (builder.statelessClient()) " + - "to create a new stateless client"); - } - } - - public int tcpBufferSize() { - return tcpBufferSize; - } - - public StatelessMapConfig tcpBufferSize(int packetSize) { - this.tcpBufferSize = packetSize; - return this; - } -} diff --git a/src/main/java/net/openhft/chronicle/map/VanillaChronicleMap.java b/src/main/java/net/openhft/chronicle/map/VanillaChronicleMap.java index a4dc7b882..58a0d4571 100755 --- a/src/main/java/net/openhft/chronicle/map/VanillaChronicleMap.java +++ b/src/main/java/net/openhft/chronicle/map/VanillaChronicleMap.java @@ -51,6 +51,7 @@ import java.util.concurrent.TimeUnit; import static java.lang.Long.numberOfTrailingZeros; import static java.lang.Math.max; import static java.lang.Thread.currentThread; +import static java.util.Collections.emptyList; import static net.openhft.chronicle.map.Asserts.assertNotNull; import static net.openhft.chronicle.map.ChronicleMapBuilder.greatestCommonDivisor; import static net.openhft.lang.MemoryUnit.*; @@ -101,7 +102,7 @@ class VanillaChronicleMap, final Class nativeValueClass; final MultiMapFactory multiMapFactory; final int maxChunksPerEntry; - private final List jsonConverters; + transient Provider> keyReaderProvider; transient Provider keyInteropProvider; transient Provider> valueReaderProvider; @@ -119,7 +120,6 @@ class VanillaChronicleMap, keySizeMarshaller = keyBuilder.sizeMarshaller(); originalKeyReader = keyBuilder.reader(); dataFileVersion = BuildVersion.version(); - jsonConverters = builder.jsonConverters(); originalKeyInterop = (KI) keyBuilder.interop(); @@ -672,12 +672,12 @@ class VanillaChronicleMap, @Override public synchronized void getAll(File toFile) throws IOException { - JsonSerializer.getAll(toFile, this, jsonConverters); + JsonSerializer.getAll(toFile, this, emptyList()); } @Override public synchronized void putAll(File fromFile) throws IOException { - JsonSerializer.putAll(fromFile, this, jsonConverters); + JsonSerializer.putAll(fromFile, this, emptyList()); } @Override diff --git a/src/main/java/net/openhft/chronicle/set/ChronicleSetBuilder.java b/src/main/java/net/openhft/chronicle/set/ChronicleSetBuilder.java index 1db8d0c3d..374fa7075 100755 --- a/src/main/java/net/openhft/chronicle/set/ChronicleSetBuilder.java +++ b/src/main/java/net/openhft/chronicle/set/ChronicleSetBuilder.java @@ -20,8 +20,7 @@ package net.openhft.chronicle.set; import net.openhft.chronicle.hash.ChronicleHashBuilder; import net.openhft.chronicle.hash.ChronicleHashErrorListener; -import net.openhft.chronicle.hash.ChronicleHashInstanceConfig; -import net.openhft.chronicle.hash.StatelessClientConfig; +import net.openhft.chronicle.hash.ChronicleHashInstanceBuilder; import net.openhft.chronicle.hash.replication.SingleChronicleHashReplication; import net.openhft.chronicle.hash.replication.TcpTransportAndNetworkConfig; import net.openhft.chronicle.hash.replication.TimeProvider; @@ -38,7 +37,6 @@ import org.jetbrains.annotations.NotNull; import java.io.File; import java.io.IOException; -import java.net.InetSocketAddress; import java.util.concurrent.TimeUnit; /** @@ -289,20 +287,8 @@ public final class ChronicleSetBuilder } @Override - public StatelessClientConfig> statelessClient( - InetSocketAddress remoteAddress) { - return new StatelessSetConfig<>(chronicleMapBuilder.statelessClient(remoteAddress)); - } - - @Override - public ChronicleSet createStatelessClient(InetSocketAddress serverAddress) - throws IOException { - return statelessClient(serverAddress).create(); - } - - @Override - public ChronicleHashInstanceConfig> instance() { - return new SetInstanceConfig<>(chronicleMapBuilder.instance()); + public ChronicleHashInstanceBuilder> instance() { + return new SetInstanceBuilder<>(chronicleMapBuilder.instance()); } @Override diff --git a/src/main/java/net/openhft/chronicle/set/ChronicleSetStatelessClientBuilder.java b/src/main/java/net/openhft/chronicle/set/ChronicleSetStatelessClientBuilder.java new file mode 100644 index 000000000..b4224a502 --- /dev/null +++ b/src/main/java/net/openhft/chronicle/set/ChronicleSetStatelessClientBuilder.java @@ -0,0 +1,62 @@ +/* + * 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.set; + +import net.openhft.chronicle.hash.ChronicleHashStatelessClientBuilder; +import net.openhft.chronicle.map.ChronicleMapStatelessClientBuilder; + +import java.io.IOException; +import java.net.InetSocketAddress; +import java.util.concurrent.TimeUnit; + +public final class ChronicleSetStatelessClientBuilder implements + ChronicleHashStatelessClientBuilder< + ChronicleSetStatelessClientBuilder, ChronicleSet> { + + public static ChronicleSetStatelessClientBuilder of(InetSocketAddress serverAddress) { + return new ChronicleSetStatelessClientBuilder<>(serverAddress); + } + + private final ChronicleMapStatelessClientBuilder mapClientBuilder; + + ChronicleSetStatelessClientBuilder(InetSocketAddress serverAddress) { + this.mapClientBuilder = ChronicleMapStatelessClientBuilder.of(serverAddress); + } + + @Override + public ChronicleSetStatelessClientBuilder timeout(long timeout, TimeUnit units) { + mapClientBuilder.timeout(timeout, units); + return this; + } + + @Override + public ChronicleSetStatelessClientBuilder name(String name) { + mapClientBuilder.name(name); + return this; + } + + @Override + public ChronicleSetStatelessClientBuilder tcpBufferSize(int tcpBufferSize) { + mapClientBuilder.tcpBufferSize(tcpBufferSize); + return this; + } + + @Override + public ChronicleSet create() throws IOException { + return new SetFromMap<>(mapClientBuilder.create()); + } +} diff --git a/src/main/java/net/openhft/chronicle/set/DummyValueMarshaller.java b/src/main/java/net/openhft/chronicle/set/DummyValueMarshaller.java index feca01b7f..508d607db 100644 --- a/src/main/java/net/openhft/chronicle/set/DummyValueMarshaller.java +++ b/src/main/java/net/openhft/chronicle/set/DummyValueMarshaller.java @@ -31,7 +31,7 @@ enum DummyValueMarshaller @Override public boolean startsWith(Bytes bytes, DummyValue dummyValue) { - throw new UnsupportedOperationException(); + return true; } @Override diff --git a/src/main/java/net/openhft/chronicle/set/SetFromMap.java b/src/main/java/net/openhft/chronicle/set/SetFromMap.java index 6ca6b9f4e..5491af73b 100644 --- a/src/main/java/net/openhft/chronicle/set/SetFromMap.java +++ b/src/main/java/net/openhft/chronicle/set/SetFromMap.java @@ -60,7 +60,7 @@ class SetFromMap extends AbstractSet } public boolean remove(Object o) { - return m.remove(o) == DUMMY_VALUE; + return m.remove(o, DUMMY_VALUE); } public boolean add(E e) { @@ -91,6 +91,8 @@ class SetFromMap extends AbstractSet return o == this || s.equals(o); } + // TODO optimize in case of stateless clients -- because bulk ops are optimized on maps + // but on key set they delegate to individual element queries => individual remote calls public boolean containsAll(Collection c) { return s.containsAll(c); } diff --git a/src/main/java/net/openhft/chronicle/set/SetInstanceConfig.java b/src/main/java/net/openhft/chronicle/set/SetInstanceBuilder.java similarity index 61% rename from src/main/java/net/openhft/chronicle/set/SetInstanceConfig.java rename to src/main/java/net/openhft/chronicle/set/SetInstanceBuilder.java index 337168998..61ea12ef1 100644 --- a/src/main/java/net/openhft/chronicle/set/SetInstanceConfig.java +++ b/src/main/java/net/openhft/chronicle/set/SetInstanceBuilder.java @@ -16,7 +16,7 @@ package net.openhft.chronicle.set; -import net.openhft.chronicle.hash.ChronicleHashInstanceConfig; +import net.openhft.chronicle.hash.ChronicleHashInstanceBuilder; import net.openhft.chronicle.hash.replication.ReplicationChannel; import net.openhft.chronicle.hash.replication.SingleChronicleHashReplication; import net.openhft.chronicle.hash.replication.TcpTransportAndNetworkConfig; @@ -25,46 +25,51 @@ import net.openhft.chronicle.map.ChronicleMap; import java.io.File; import java.io.IOException; -final class SetInstanceConfig implements ChronicleHashInstanceConfig> { +final class SetInstanceBuilder implements ChronicleHashInstanceBuilder> { - private final ChronicleHashInstanceConfig> mapConfig; + private final ChronicleHashInstanceBuilder> mapBuilder; - SetInstanceConfig(ChronicleHashInstanceConfig> mapConfig) { - this.mapConfig = mapConfig; + SetInstanceBuilder(ChronicleHashInstanceBuilder> mapBuilder) { + this.mapBuilder = mapBuilder; } @Override - public ChronicleHashInstanceConfig> replicated( + public ChronicleHashInstanceBuilder> replicated( byte identifier, TcpTransportAndNetworkConfig tcpTransportAndNetwork) { - return new SetInstanceConfig<>(mapConfig.replicated(identifier, tcpTransportAndNetwork)); + mapBuilder.replicated(identifier, tcpTransportAndNetwork); + return this; } @Override - public ChronicleHashInstanceConfig> replicated( + public ChronicleHashInstanceBuilder> replicated( SingleChronicleHashReplication replication) { - return new SetInstanceConfig<>(mapConfig.replicated(replication)); + mapBuilder.replicated(replication); + return this; } @Override - public ChronicleHashInstanceConfig> replicatedViaChannel( + public ChronicleHashInstanceBuilder> replicatedViaChannel( ReplicationChannel channel) { - return new SetInstanceConfig<>(mapConfig.replicatedViaChannel(channel)); + mapBuilder.replicatedViaChannel(channel); + return this; } @Override - public ChronicleHashInstanceConfig> persistedTo(File file) { - return new SetInstanceConfig<>(mapConfig.persistedTo(file)); + public ChronicleHashInstanceBuilder> persistedTo(File file) { + mapBuilder.persistedTo(file); + return this; } @Override - public ChronicleHashInstanceConfig> name(String name) { - return new SetInstanceConfig<>(mapConfig.name(name)); + public ChronicleHashInstanceBuilder> name(String name) { + mapBuilder.name(name); + return this; } @Override public synchronized ChronicleSet create() throws IOException { try { - return new SetFromMap<>(mapConfig.create()); + return new SetFromMap<>(mapBuilder.create()); } catch (IllegalStateException e) { if (e.getMessage() != null && e.getMessage().startsWith("A ChronicleMap")) { throw new IllegalStateException( diff --git a/src/main/java/net/openhft/chronicle/set/StatelessSetConfig.java b/src/main/java/net/openhft/chronicle/set/StatelessSetConfig.java deleted file mode 100644 index c5ab66a84..000000000 --- a/src/main/java/net/openhft/chronicle/set/StatelessSetConfig.java +++ /dev/null @@ -1,47 +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.set; - -import net.openhft.chronicle.hash.StatelessClientConfig; -import net.openhft.chronicle.map.ChronicleMap; - -import java.io.IOException; -import java.util.concurrent.TimeUnit; - -final class StatelessSetConfig implements StatelessClientConfig> { - - private final StatelessClientConfig> mapClientBuilder; - - StatelessSetConfig(StatelessClientConfig> mapClientBuilder) { - this.mapClientBuilder = mapClientBuilder; - } - - @Override - public StatelessClientConfig> timeout(long timeout, TimeUnit units) { - return new StatelessSetConfig<>(mapClientBuilder.timeout(timeout, units)); - } - - @Override - public StatelessClientConfig> name(String name) { - return new StatelessSetConfig<>(mapClientBuilder.name(name)); - } - - @Override - public ChronicleSet create() throws IOException { - return new SetFromMap<>(mapClientBuilder.create()); - } -} diff --git a/src/test/java/eg/BGChronicleTest.java b/src/test/java/eg/BGChronicleTest.java index fbef4bf3e..f88c5e23f 100644 --- a/src/test/java/eg/BGChronicleTest.java +++ b/src/test/java/eg/BGChronicleTest.java @@ -3,6 +3,7 @@ package eg; import net.openhft.chronicle.hash.replication.TcpTransportAndNetworkConfig; import net.openhft.chronicle.map.ChronicleMap; import net.openhft.chronicle.map.ChronicleMapBuilder; +import net.openhft.chronicle.map.ChronicleMapStatelessClientBuilder; import java.io.Closeable; import java.io.File; @@ -99,13 +100,12 @@ public class BGChronicleTest { futureList.add(es.submit(new Callable() { @Override public Void call() throws IOException { - try (ChronicleMap map = ChronicleMapBuilder - .of(byte[].class, byte[].class) - .constantKeySizeBySample(new byte[KEY_SIZE]) - .constantValueSizeBySample(new byte[VALUE_SIZE]) + InetSocketAddress serverAddress = + REPLICAS > 1 && clientId % 2 == 0 ? remoteAddress2 : remoteAddress; + try (ChronicleMap map = ChronicleMapStatelessClientBuilder + .of(serverAddress) .putReturnsNull(true) .removeReturnsNull(true) - .statelessClient(REPLICAS > 1 && clientId % 2 == 0 ? remoteAddress2 : remoteAddress) .create()) { System.out.println((clientId + 1) + "/" + CLIENTS + " client started"); @@ -138,13 +138,10 @@ public class BGChronicleTest { futureList2.add(es.submit(new Callable() { @Override public long[] call() throws IOException, InterruptedException { - try (ChronicleMap map = ChronicleMapBuilder - .of(byte[].class, byte[].class) - .constantKeySizeBySample(new byte[KEY_SIZE]) - .constantValueSizeBySample(new byte[VALUE_SIZE]) + try (ChronicleMap map = ChronicleMapStatelessClientBuilder + .of(remoteAddress) .putReturnsNull(true) .removeReturnsNull(true) - .statelessClient(remoteAddress) .create()) { map.size(); diff --git a/src/test/java/net/openhft/chronicle/map/CHMUseCasesTest.java b/src/test/java/net/openhft/chronicle/map/CHMUseCasesTest.java index f67628be1..053e6ac5a 100644 --- a/src/test/java/net/openhft/chronicle/map/CHMUseCasesTest.java +++ b/src/test/java/net/openhft/chronicle/map/CHMUseCasesTest.java @@ -26,6 +26,7 @@ import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import static java.util.Arrays.asList; +import static net.openhft.chronicle.map.StatelessClientTest.localClient; import static net.openhft.chronicle.map.fromdocs.OpenJDKAndHashMapExamplesTest.parseYYYYMMDD; import static org.junit.Assert.*; @@ -294,9 +295,7 @@ public class CHMUseCasesTest { } { - map1 = builder - .statelessClient(new InetSocketAddress("localhost", 8086)) - .create(); + map1 = localClient(8086); closeables.add(map1); return map1; diff --git a/src/test/java/net/openhft/chronicle/map/ChronicleMapImportExportTest.java b/src/test/java/net/openhft/chronicle/map/ChronicleMapImportExportTest.java index ddde2e047..cef2c236f 100644 --- a/src/test/java/net/openhft/chronicle/map/ChronicleMapImportExportTest.java +++ b/src/test/java/net/openhft/chronicle/map/ChronicleMapImportExportTest.java @@ -15,6 +15,7 @@ import java.net.InetSocketAddress; import java.util.HashMap; import java.util.Map; +import static net.openhft.chronicle.map.StatelessClientTest.localClient; import static org.junit.Assert.assertEquals; /** @@ -210,14 +211,12 @@ public class ChronicleMapImportExportTest { file.deleteOnExit(); File file2 = new File(TMP + "/chronicle-map-" + System.nanoTime() + "-2.json"); file.deleteOnExit(); - try (ChronicleMap expected = ChronicleMapBuilder.of(CharSequence.class, CharSequence - .class) - .create()) { - try (ChronicleMap serverMap = ChronicleMapBuilder.of(CharSequence.class, CharSequence.class) + try (ChronicleMap expected = + ChronicleMapBuilder.of(CharSequence.class, CharSequence.class).create()) { + try (ChronicleMap serverMap = ChronicleMapBuilder + .of(CharSequence.class, CharSequence.class) .replication((byte) 2, TcpTransportAndNetworkConfig.of(8056)).create()) { - try (ChronicleMap actual = ChronicleMapBuilder.of(CharSequence - .class, CharSequence.class) - .statelessClient(new InetSocketAddress("localhost", 8056)).create()) { + try (ChronicleMap actual = localClient(8056)) { expected.put("hello", "world"); expected.getAll(file); diff --git a/src/test/java/net/openhft/chronicle/map/ChronicleMapTest.java b/src/test/java/net/openhft/chronicle/map/ChronicleMapTest.java index 04fb12c05..2184a3322 100755 --- a/src/test/java/net/openhft/chronicle/map/ChronicleMapTest.java +++ b/src/test/java/net/openhft/chronicle/map/ChronicleMapTest.java @@ -40,6 +40,7 @@ import java.util.concurrent.*; import java.util.concurrent.atomic.AtomicInteger; import static net.openhft.chronicle.map.Alignment.*; +import static net.openhft.chronicle.map.StatelessClientTest.localClient; import static org.junit.Assert.*; @SuppressWarnings({"unchecked", "ResultOfMethodCallIgnored"}) @@ -1710,11 +1711,7 @@ public class ChronicleMapTest { .constantKeySizeBySample(new byte[14]) .createPersistedTo(mapFile)) { - try (ChronicleMap map2 = ChronicleMapBuilder - .of(byte[].class, byte[][].class) - .constantKeySizeBySample(new byte[14]) - .statelessClient(new InetSocketAddress("localhost", 8877)) - .create()) { + try (ChronicleMap map2 = localClient(8877)) { byte[] key = new byte[14]; diff --git a/src/test/java/net/openhft/chronicle/map/MapByNameTest.java b/src/test/java/net/openhft/chronicle/map/MapByNameTest.java index 8a1dc3db8..609ad0696 100644 --- a/src/test/java/net/openhft/chronicle/map/MapByNameTest.java +++ b/src/test/java/net/openhft/chronicle/map/MapByNameTest.java @@ -46,8 +46,8 @@ public class MapByNameTest { @Test public void testSerializingBuilder() throws TimeoutException, InterruptedException, IOException { - final MapInstanceConfig config = - (MapInstanceConfig) + final MapInstanceBuilder config = + (MapInstanceBuilder) ChronicleMapBuilder.of(CharSequence.class, CharSequence.class) .averageKeySize("hello".length()).averageValueSize("world".length()) .entries(2) @@ -73,8 +73,8 @@ public class MapByNameTest { NodeDiscovery nodeDiscovery = new NodeDiscovery(); - MapInstanceConfig config = - (MapInstanceConfig) + MapInstanceBuilder config = + (MapInstanceBuilder) ChronicleMapBuilder.of(CharSequence.class, CharSequence.class) .minSegments(2) .removeReturnsNull(true) @@ -93,8 +93,8 @@ public class MapByNameTest { final ReplicationHubFindByName mapByName = nodeDiscovery.mapByName(); Thread.sleep(2000); - MapInstanceConfig config = - (MapInstanceConfig) + MapInstanceBuilder config = + (MapInstanceBuilder) ChronicleMapBuilder.of(CharSequence.class, CharSequence.class) .minSegments(2) .entries(2) diff --git a/src/test/java/net/openhft/chronicle/map/StatelessClientTest.java b/src/test/java/net/openhft/chronicle/map/StatelessClientTest.java index 78f17cc0f..860610016 100644 --- a/src/test/java/net/openhft/chronicle/map/StatelessClientTest.java +++ b/src/test/java/net/openhft/chronicle/map/StatelessClientTest.java @@ -24,6 +24,7 @@ import net.openhft.lang.io.ByteBufferBytes; import net.openhft.lang.io.Bytes; import org.junit.After; import org.junit.Before; +import org.junit.Ignore; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -35,6 +36,7 @@ import java.util.*; import java.util.concurrent.*; import java.util.concurrent.atomic.AtomicInteger; +import static net.openhft.chronicle.map.ChronicleMapStatelessClientBuilder.createClientOf; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; @@ -114,10 +116,11 @@ public class StatelessClientTest { .create()) { serverMap.put(10, new StringBuilder("Hello World")); - try (ChronicleMap statelessMap = ChronicleMapBuilder.of(Integer - .class, StringBuilder.class) + try (ChronicleMap statelessMap = + ChronicleMapStatelessClientBuilder + .of(new InetSocketAddress("localhost", port)) .putReturnsNull(true) - .statelessClient(new InetSocketAddress("localhost", port)).create()) { + .create()) { String actual = statelessMap.getMapped(10, ToString.INSTANCE); assertEquals("{10=Hello World}", statelessMap.toString()); @@ -126,6 +129,10 @@ public class StatelessClientTest { } } + public static ChronicleMap localClient(int port) throws IOException { + return createClientOf(new InetSocketAddress("localhost", port)); + } + @Test(timeout = 10000) public void testMapForKeyWhenNoEntry() throws IOException, InterruptedException { int port = s_port++; @@ -136,9 +143,7 @@ public class StatelessClientTest { .replication((byte) 2, TcpTransportAndNetworkConfig.of(port)).create()) { serverMap.put(10, new StringBuilder("Hello World")); - try (ChronicleMap statelessMap = ChronicleMapBuilder.of(Integer - .class, StringBuilder.class) - .statelessClient(new InetSocketAddress("localhost", port)).create()) { + try (ChronicleMap statelessMap = localClient(port)) { String actual = statelessMap.getMapped(11, ToString.INSTANCE); assertEquals(null, actual); @@ -146,7 +151,9 @@ public class StatelessClientTest { } } - // @Test(timeout = 10000) + // TODO ignore why? + @Ignore + @Test(timeout = 10000) public void testBufferOverFlowPutAllAndEntrySet() throws IOException, InterruptedException { int port = s_port++; try (ChronicleMap serverMap = ChronicleMapBuilder @@ -154,10 +161,7 @@ public class StatelessClientTest { .entries(SIZE) .replication((byte) 2, TcpTransportAndNetworkConfig.of(port)) .create()) { - try (ChronicleMap statelessMap = ChronicleMapBuilder - .of(Integer.class, CharSequence.class) - .statelessClient(new InetSocketAddress("localhost", port)) - .create()) { + try (ChronicleMap statelessMap = localClient(port)) { Map payload = new HashMap(); for (int i = 0; i < SIZE; i++) { @@ -186,10 +190,7 @@ public class StatelessClientTest { .entries(SIZE) .replication((byte) 2, TcpTransportAndNetworkConfig.of(port)) .create()) { - try (ChronicleMap statelessMap = ChronicleMapBuilder - .of(Integer.class, CharSequence.class) - .statelessClient(new InetSocketAddress("localhost", port)) - .create()) { + try (ChronicleMap statelessMap = localClient(port)) { Map payload = new HashMap(); for (int i = 0; i < SIZE; i++) { @@ -217,10 +218,7 @@ public class StatelessClientTest { .create()) { Thread.sleep(100); - try (ChronicleMap statelessMap = ChronicleMapBuilder - .of(Integer.class, CharSequence.class) - .statelessClient(new InetSocketAddress("localhost", port)) - .create()) { + try (ChronicleMap statelessMap = localClient(port)) { Map payload = new HashMap(); for (int i = 0; i < SIZE; i++) { @@ -246,9 +244,7 @@ public class StatelessClientTest { .replication((byte) 2, TcpTransportAndNetworkConfig.of(port)) .create()) { serverMap.put(10, "EXAMPLE-10"); - try (ChronicleMap statelessMap = ChronicleMapBuilder.of(Integer - .class, CharSequence.class) - .statelessClient(new InetSocketAddress("localhost", port)).create()) { + try (ChronicleMap statelessMap = localClient(port)) { assertEquals("EXAMPLE-10", statelessMap.get(10)); int size = statelessMap.size(); @@ -261,11 +257,10 @@ public class StatelessClientTest { public void testClientCreatedBeforeServer() throws IOException, InterruptedException { int port = s_port++; - try (ChronicleMap serverMap = ChronicleMapBuilder.of(Integer.class, CharSequence.class) + try (ChronicleMap serverMap = + ChronicleMapBuilder.of(Integer.class, CharSequence.class) .replication((byte) 2, TcpTransportAndNetworkConfig.of(port)).create()) { - try (ChronicleMap statelessMap = ChronicleMapBuilder.of(Integer - .class, CharSequence.class) - .statelessClient(new InetSocketAddress("localhost", port)).create()) { + try (ChronicleMap statelessMap = localClient(port)) { serverMap.put(10, "EXAMPLE-10"); assertEquals("EXAMPLE-10", statelessMap.get(10)); @@ -282,10 +277,7 @@ public class StatelessClientTest { .entries(SIZE) .replication((byte) 2, TcpTransportAndNetworkConfig.of(8056)) .create()) { - try (ChronicleMap statelessMap = ChronicleMapBuilder - .of(String.class, Map.class) - .statelessClient(new InetSocketAddress("localhost", 8056)) - .create()) { + try (ChronicleMap statelessMap = localClient(8056)) { serverMap.put("hello", Collections.singletonMap("hello", "world")); assertEquals(Collections.singletonMap("hello", "world"), statelessMap.get("hello")); @@ -303,10 +295,7 @@ public class StatelessClientTest { .entries(SIZE) .replication((byte) 2, TcpTransportAndNetworkConfig.of(8056)) .create()) { - try (ChronicleMap statelessMap = ChronicleMapBuilder - .of(String.class, String.class) - .statelessClient(new InetSocketAddress("localhost", 8056)) - .create()) { + try (ChronicleMap statelessMap = localClient(8056)) { serverMap.put("hello", "world"); @@ -329,10 +318,7 @@ public class StatelessClientTest { .entries(SIZE) .replication((byte) 2, TcpTransportAndNetworkConfig.of(8056)) .create()) { - try (ChronicleMap statelessMap = ChronicleMapBuilder - .of(String.class, String.class) - .statelessClient(new InetSocketAddress("localhost", 8056)) - .create()) { + try (ChronicleMap statelessMap = localClient(8056)) { assertTrue(statelessMap.isEmpty()); assertEquals(0, statelessMap.size()); @@ -358,10 +344,7 @@ public class StatelessClientTest { .entries(1000) .replication((byte) 2, TcpTransportAndNetworkConfig.of(8056)) .create()) { - try (ChronicleMap statelessMap = ChronicleMapBuilder - .of(String.class, Map.class) - .statelessClient(new InetSocketAddress("localhost", 8056)) - .create()) { + try (ChronicleMap statelessMap = localClient(8056)) { statelessMap.putIfAbsent("hello", data); assertEquals(data, serverMap.get("hello")); @@ -377,10 +360,7 @@ public class StatelessClientTest { .entries(SIZE) .replication((byte) 2, TcpTransportAndNetworkConfig.of(8056)) .create()) { - try (ChronicleMap statelessMap = ChronicleMapBuilder - .of(Integer.class, CharSequence.class) - .statelessClient(new InetSocketAddress("localhost", 8056)) - .create()) { + try (ChronicleMap statelessMap = localClient(8056)) { Map payload = new HashMap(); for (int i = 0; i < SIZE; i++) { @@ -398,17 +378,15 @@ public class StatelessClientTest { } @Test(timeout = 10000) - public void testBufferOverFlowPutAllWherePutReturnsNull() throws IOException, InterruptedException { + public void testBufferOverFlowPutAllWherePutReturnsNull() + throws IOException, InterruptedException { try (ChronicleMap serverMap = ChronicleMapBuilder .of(Integer.class, CharSequence.class) .entries(SIZE) .replication((byte) 2, TcpTransportAndNetworkConfig.of(8056)) .create()) { - try (ChronicleMap statelessMap = ChronicleMapBuilder - .of(Integer.class, CharSequence.class) - .statelessClient(new InetSocketAddress("localhost", 8056)) - .create()) { + try (ChronicleMap statelessMap = localClient(8056)) { Map payload = new HashMap(); for (int i = 0; i < SIZE; i++) { @@ -429,11 +407,10 @@ public class StatelessClientTest { public void testPutWherePutReturnsNull() throws IOException, InterruptedException { - try (ChronicleMap serverMap = ChronicleMapBuilder.of(Integer.class, CharSequence.class) + try (ChronicleMap serverMap = ChronicleMapBuilder + .of(Integer.class, CharSequence.class) .replication((byte) 2, TcpTransportAndNetworkConfig.of(8056)).create()) { - try (ChronicleMap statelessMap = ChronicleMapBuilder.of(Integer - .class, CharSequence.class) - .statelessClient(new InetSocketAddress("localhost", 8056)).create()) { + try (ChronicleMap statelessMap = localClient(8056)) { statelessMap.put(1, "some value"); assertEquals("some value", statelessMap.get(1)); @@ -446,11 +423,10 @@ public class StatelessClientTest { public void testRemoveWhereRemoveReturnsNull() throws IOException, InterruptedException { - try (ChronicleMap serverMap = ChronicleMapBuilder.of(Integer.class, CharSequence.class) + try (ChronicleMap serverMap = ChronicleMapBuilder + .of(Integer.class, CharSequence.class) .replication((byte) 2, TcpTransportAndNetworkConfig.of(8056)).create()) { - try (ChronicleMap statelessMap = ChronicleMapBuilder.of(Integer - .class, CharSequence.class) - .statelessClient(new InetSocketAddress("localhost", 8056)).create()) { + try (ChronicleMap statelessMap = localClient(8056)) { statelessMap.put(1, "some value"); assertEquals("some value", statelessMap.get(1)); @@ -482,11 +458,7 @@ public class StatelessClientTest { .replication((byte) 2, TcpTransportAndNetworkConfig.of(8056) .tcpBufferSize(128)) .create()) { - try (ChronicleMap statelessMap = ChronicleMapBuilder.of(Integer - .class, CharSequence.class) - .entries(1) - .constantValueSizeBySample(sampleValue) - .statelessClient(new InetSocketAddress("localhost", 8056)).create()) { + try (ChronicleMap statelessMap = localClient(8056)) { statelessMap.put(1, new String(value)); @@ -505,11 +477,10 @@ public class StatelessClientTest { public void testGetAndEntryWeDontHave() throws IOException, InterruptedException, ExecutionException { - try (ChronicleMap serverMap = ChronicleMapBuilder.of(Integer.class, CharSequence.class) + try (ChronicleMap serverMap = ChronicleMapBuilder + .of(Integer.class, CharSequence.class) .replication((byte) 2, TcpTransportAndNetworkConfig.of(8056)).create()) { - try (ChronicleMap statelessMap = ChronicleMapBuilder.of(Integer - .class, CharSequence.class) - .statelessClient(new InetSocketAddress("localhost", 8056)).create()) { + try (ChronicleMap statelessMap = localClient(8056)) { assertEquals(null, statelessMap.get(3)); } @@ -534,12 +505,8 @@ public class StatelessClientTest { // stateless client { - statelessMap1 = ChronicleMapBuilder.of(Integer - .class, CharSequence.class) - .statelessClient(new InetSocketAddress("localhost", 8056)).create(); - statelessMap2 = ChronicleMapBuilder.of(Integer - .class, CharSequence.class) - .statelessClient(new InetSocketAddress("localhost", 8077)).create(); + statelessMap1 = localClient(8056); + statelessMap2 = localClient(8077); } Map payload = new HashMap(); @@ -570,14 +537,13 @@ public class StatelessClientTest { long startTime = System.currentTimeMillis(); // server - try (ChronicleMap server = ChronicleMapBuilder.of(Integer.class, Integer.class) + try (ChronicleMap server = ChronicleMapBuilder + .of(Integer.class, Integer.class) .putReturnsNull(true) .replication((byte) 1, TcpTransportAndNetworkConfig.of(8047)).create()) { // stateless client - try (ChronicleMap client = ChronicleMapBuilder.of(Integer.class, - Integer.class) - .statelessClient(new InetSocketAddress("localhost", 8047)).create()) { + try (ChronicleMap client = localClient(8047)) { for (int i = 0; i < count; i++) { @@ -618,7 +584,8 @@ public class StatelessClientTest { if (result.equals(j)) { got.incrementAndGet(); } else { - System.out.println("expected j=" + j + " but got back=" + result); + System.out.println("expected j=" + j + " but got back=" + + result); } latch.countDown(); @@ -636,7 +603,7 @@ public class StatelessClientTest { latch.await(25, TimeUnit.SECONDS); System.out.println("" + count + " messages took " + TimeUnit.MILLISECONDS.toSeconds(System - .currentTimeMillis() - startTime) + " seconds, using " + nThreads + "" + + .currentTimeMillis() - startTime) + " seconds, using " + nThreads + " threads"); assertEquals(count, got.get()); @@ -666,20 +633,20 @@ public class StatelessClientTest { // server - try (ChronicleMap server = ChronicleMapBuilder.of(Integer.class, Integer.class) + try (ChronicleMap server = ChronicleMapBuilder + .of(Integer.class, Integer.class) .putReturnsNull(true) .replication((byte) 1, TcpTransportAndNetworkConfig.of(8047)).create()) { - try (ChronicleMap server2 = ChronicleMapBuilder.of(Integer.class, Integer.class) + try (ChronicleMap server2 = ChronicleMapBuilder + .of(Integer.class, Integer.class) .putReturnsNull(true) .replication((byte) 2, TcpTransportAndNetworkConfig.of(8046, new InetSocketAddress("localhost", 8047))).create()) { // stateless client - try (ChronicleMap client = ChronicleMapBuilder.of(Integer.class, - Integer.class) - .statelessClient(new InetSocketAddress("localhost", 8046)).create()) { + try (ChronicleMap client = localClient(8046)) { for (int i = 0; i < count; i++) { @@ -720,7 +687,8 @@ public class StatelessClientTest { if (result.equals(j)) { got.incrementAndGet(); } else { - System.out.println("expected j=" + j + " but got back=" + result); + System.out.println("expected j=" + j + " but got back=" + + result); } latch.countDown(); @@ -738,8 +706,8 @@ public class StatelessClientTest { latch.await(25, TimeUnit.SECONDS); System.out.println("" + count + " messages took " + TimeUnit.MILLISECONDS.toSeconds(System - .currentTimeMillis() - startTime) + " seconds, using " + nThreads + "" + - " threads"); + .currentTimeMillis() - startTime) + " seconds, using " + + nThreads + " threads"); assertEquals(count, got.get()); @@ -766,12 +734,7 @@ public class StatelessClientTest { .replication((byte) 2, TcpTransportAndNetworkConfig.of(8056)) .create()) { - try (ChronicleMap statelessMap = ChronicleMapBuilder - .of(byte[].class, byte[].class) - .averageKeySize(4) - .averageValueSize(4) - .statelessClient(new InetSocketAddress("localhost", 8056)) - .create()) { + try (ChronicleMap statelessMap = localClient(8056)) { for (int i = 0; i < SIZE; i++) { keyBuffer.clear(); @@ -795,10 +758,7 @@ public class StatelessClientTest { .constantKeySizeBySample(new byte[14]) .create()) { - try (ChronicleMap map2 = ChronicleMapBuilder - .of(byte[].class, CharSequence.class) - .statelessClient(new InetSocketAddress("localhost", 8875)) - .create()) { + try (ChronicleMap map2 = localClient(8875)) { byte[] key = new byte[14]; System.arraycopy("A".getBytes(), 0, key, 0, "A".length()); @@ -819,11 +779,10 @@ public class StatelessClientTest { .replication((byte) 2, TcpTransportAndNetworkConfig.of(port)) .create()) { - try (ChronicleMap statelessMap = ChronicleMapBuilder.of(Integer - .class, StringBuilder.class) - .statelessClient(new InetSocketAddress("localhost", port)).create()) { + try (ChronicleMap statelessMap = localClient(port)) { - assertTrue(!((StatelessChronicleMap) statelessMap).serverApplicationVersion().isEmpty()); + assertTrue(!((StatelessChronicleMap) statelessMap).serverApplicationVersion() + .isEmpty()); } } diff --git a/src/test/java/net/openhft/chronicle/map/example/ChannelReplicationTest.java b/src/test/java/net/openhft/chronicle/map/example/ChannelReplicationTest.java index 71868871f..fd91db34e 100644 --- a/src/test/java/net/openhft/chronicle/map/example/ChannelReplicationTest.java +++ b/src/test/java/net/openhft/chronicle/map/example/ChannelReplicationTest.java @@ -18,7 +18,7 @@ package net.openhft.chronicle.map.example; -import net.openhft.chronicle.hash.ChronicleHashInstanceConfig; +import net.openhft.chronicle.hash.ChronicleHashInstanceBuilder; import net.openhft.chronicle.hash.replication.ReplicationHub; import net.openhft.chronicle.hash.replication.TcpTransportAndNetworkConfig; import net.openhft.chronicle.map.ChronicleMap; @@ -197,7 +197,7 @@ public class ChannelReplicationTest { // this demotes favoriteColour short channel1 = (short) 1; - ChronicleHashInstanceConfig> instance = builder.instance().replicatedViaChannel(hubOnServer.createChannel(channel1)); + ChronicleHashInstanceBuilder> instance = builder.instance().replicatedViaChannel(hubOnServer.createChannel(channel1)); favoriteColourServer2 = instance.create(); } diff --git a/src/test/java/net/openhft/chronicle/map/example/ServersMapMain.java b/src/test/java/net/openhft/chronicle/map/example/ServersMapMain.java index b4fdd57b7..04f8e3e3c 100644 --- a/src/test/java/net/openhft/chronicle/map/example/ServersMapMain.java +++ b/src/test/java/net/openhft/chronicle/map/example/ServersMapMain.java @@ -21,6 +21,7 @@ package net.openhft.chronicle.map.example; import net.openhft.chronicle.hash.replication.TcpTransportAndNetworkConfig; import net.openhft.chronicle.map.ChronicleMap; import net.openhft.chronicle.map.ChronicleMapBuilder; +import net.openhft.chronicle.map.ChronicleMapStatelessClientBuilder; import java.io.File; import java.io.IOException; @@ -54,10 +55,10 @@ public class ServersMapMain { public static void startRemoteClient(String hostname) throws IOException { final ChronicleMap map; if (stateless) { - map = ChronicleMapBuilder.of( - byte[].class, byte[].class) + map = ChronicleMapStatelessClientBuilder + .of(new InetSocketAddress(hostname, port)) .putReturnsNull(true) - .statelessClient(new InetSocketAddress(hostname, port)).create(); + .create(); } else { File file = File.createTempFile("testServersMapMain", ".deleteme"); file.deleteOnExit(); diff --git a/src/test/java/net/openhft/chronicle/map/example/StringsMapMain.java b/src/test/java/net/openhft/chronicle/map/example/StringsMapMain.java index 34fa7da8b..98eabbd71 100644 --- a/src/test/java/net/openhft/chronicle/map/example/StringsMapMain.java +++ b/src/test/java/net/openhft/chronicle/map/example/StringsMapMain.java @@ -21,6 +21,7 @@ package net.openhft.chronicle.map.example; import net.openhft.chronicle.hash.replication.TcpTransportAndNetworkConfig; import net.openhft.chronicle.map.ChronicleMap; import net.openhft.chronicle.map.ChronicleMapBuilder; +import net.openhft.chronicle.map.ChronicleMapStatelessClientBuilder; import java.io.File; import java.io.IOException; @@ -55,9 +56,10 @@ public class StringsMapMain { public static void startRemoteClient(String hostname) throws IOException { final ChronicleMap map; if (stateless) { - map = ChronicleMapBuilder.of(CharSequence.class, CharSequence.class) + map = ChronicleMapStatelessClientBuilder + .of(new InetSocketAddress(hostname, port)) .putReturnsNull(true) - .statelessClient(new InetSocketAddress(hostname, port)).create(); + .create(); } else { File file = File.createTempFile("testServersMapMain", ".deleteme"); file.deleteOnExit(); diff --git a/src/test/java/net/openhft/chronicle/map/jsr166/map/StatelessChronicleMapTest.java b/src/test/java/net/openhft/chronicle/map/jsr166/map/StatelessChronicleMapTest.java index 097c38e75..eb4c1f226 100755 --- a/src/test/java/net/openhft/chronicle/map/jsr166/map/StatelessChronicleMapTest.java +++ b/src/test/java/net/openhft/chronicle/map/jsr166/map/StatelessChronicleMapTest.java @@ -34,6 +34,7 @@ import java.net.InetSocketAddress; import java.nio.ByteBuffer; import java.util.*; +import static net.openhft.chronicle.map.StatelessClientTest.localClient; import static org.junit.Assert.*; /* @@ -239,9 +240,7 @@ public class StatelessChronicleMapTest extends JSR166TestCase { .replication((byte) 1, TcpTransportAndNetworkConfig.of(port)) .create(); - final ChronicleMap statelessMap = ChronicleMapBuilder.of(Integer - .class, String.class) - .statelessClient(new InetSocketAddress("localhost", port)).create(); + final ChronicleMap statelessMap = localClient(port); return new SingleCloseMap(statelessMap, statelessMap, serverMap); } @@ -254,10 +253,7 @@ public class StatelessChronicleMapTest extends JSR166TestCase { .entries(SIZE) .replication((byte) 2, TcpTransportAndNetworkConfig.of(port)).create(); - final ChronicleMap statelessMap = ChronicleMapBuilder - .of(CharSequence.class, CharSequence.class) - .statelessClient(new InetSocketAddress("localhost", port)) - .create(); + final ChronicleMap statelessMap = localClient(port); return new SingleCloseMap<>(statelessMap, statelessMap, serverMap); } @@ -271,10 +267,10 @@ public class StatelessChronicleMapTest extends JSR166TestCase { .replication((byte) 2, TcpTransportAndNetworkConfig.of(port)) .create(); - final ChronicleMap statelessMap = ChronicleMapBuilder - .of(byte[].class, byte[].class) + final ChronicleMap statelessMap = ChronicleMapStatelessClientBuilder + .of(new InetSocketAddress("localhost", port)) .putReturnsNull(true) - .statelessClient(new InetSocketAddress("localhost", port)).create(); + .create(); return new SingleCloseMap<>(statelessMap, statelessMap, serverMap); }