From f74d6f4ad1e369cf5bd7bce6e90713ae80551a29 Mon Sep 17 00:00:00 2001 From: Rob Austin Date: Tue, 5 May 2015 17:30:08 +0100 Subject: [PATCH] updated to release versions --- .../net/openhft/chronicle/map/EngineMap.java | 463 ------------------ .../chronicle/map/MapWireConnectionHub.java | 154 ------ .../openhft/chronicle/map/MapWireHandler.java | 41 -- .../map/MapWireHandlerProcessor.java | 463 ------------------ 4 files changed, 1121 deletions(-) delete mode 100644 src/main/java/net/openhft/chronicle/map/EngineMap.java delete mode 100755 src/main/java/net/openhft/chronicle/map/MapWireConnectionHub.java delete mode 100644 src/main/java/net/openhft/chronicle/map/MapWireHandler.java delete mode 100644 src/main/java/net/openhft/chronicle/map/MapWireHandlerProcessor.java diff --git a/src/main/java/net/openhft/chronicle/map/EngineMap.java b/src/main/java/net/openhft/chronicle/map/EngineMap.java deleted file mode 100644 index 49116b725..000000000 --- a/src/main/java/net/openhft/chronicle/map/EngineMap.java +++ /dev/null @@ -1,463 +0,0 @@ -package net.openhft.chronicle.map; - -import net.openhft.chronicle.bytes.Bytes; -import net.openhft.chronicle.hash.ChronicleHashInstanceBuilder; -import net.openhft.chronicle.hash.function.SerializableFunction; -import net.openhft.chronicle.wire.TextWire; -import net.openhft.chronicle.wire.Wire; -import org.jetbrains.annotations.NotNull; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.Closeable; -import java.io.File; -import java.io.IOException; -import java.nio.ByteBuffer; -import java.util.*; -import java.util.function.Consumer; -import java.util.function.Predicate; -import java.util.function.Supplier; - -import static net.openhft.chronicle.map.ChronicleMapBuilder.of; - -/** - * Created by Rob Austin - */ -public class EngineMap implements ChronicleMap { - - private static final Logger LOG = LoggerFactory.getLogger(EngineMap.class); - - private final Class kClass; - private final Class vClass; - - private final Bytes buffer = Bytes.elasticByteBuffer(); - private final Map map; - private final Class wireType; - - public EngineMap(Map underlyingMap, - Class kClass, - Class vClass, - Class wireType ) throws IOException { - this.wireType = wireType; - this.kClass = kClass; - this.vClass = vClass; - this.map = underlyingMap; - - } - - - public static Map underlyingMap(@NotNull final String name, - @NotNull final MapWireConnectionHub - mapWireConnectionHub, - final long entries) throws IOException { - - - // todo - for the moment we will default to 100 entries per map, but this is for engine to - // todo decided later. - final ChronicleHashInstanceBuilder instance - = of(byte[].class, byte[].class).entries(entries).instance(); - - final BytesChronicleMap b = mapWireConnectionHub.acquireMap(name, instance); - return (Map) b.delegate; - } - - @Override - public int size() { - return map.size(); - } - - @Override - public boolean isEmpty() { - return map.isEmpty(); - } - - - Wire toWire() { - buffer.clear(); - if (TextWire.class.isAssignableFrom(wireType)) { - return new TextWire(buffer); - } else throw new UnsupportedOperationException("todo"); - } - - - Wire toWire(byte[] bytes) { - - Bytes wrap = Bytes.wrap(bytes); - - if (TextWire.class.isAssignableFrom(wireType)) { - return new TextWire(wrap); - } else throw new UnsupportedOperationException("todo"); - } - - @Override - public boolean containsKey(Object key) { - return map.containsKey(bytes(key)); - } - - private byte[] bytes(Object b) { - - if (b instanceof byte[]) - return (byte[]) b; - - if (b == null) - return null; - final Wire wire = toWire(); - wire.getValueOut().object(b); - wire.bytes().flip(); - - return toWire().getValueIn().bytes(); - } - - - @Override - public boolean containsValue(Object value) { - return map.containsValue(bytes(value)); - } - - @Override - public V get(@NotNull final Object key) { - - return toObject(vClass, () -> { - - final byte[] bytes = bytes(key); - return map.get(bytes); - }); - } - - @Override - public V getUsing(K key, V usingValue) { - throw new UnsupportedOperationException("todo"); - } - - @Override - public V acquireUsing(@NotNull K key, V usingValue) { - throw new UnsupportedOperationException("todo"); - } - - @NotNull - @Override - public MapKeyContext acquireContext(@NotNull K key, @NotNull V usingValue) { - throw new UnsupportedOperationException("todo"); - } - - @Override - public R getMapped(K key, @NotNull SerializableFunction function) { - throw new UnsupportedOperationException("todo"); - } - - @Override - public V putMapped(K key, @NotNull UnaryOperator unaryOperator) { - throw new UnsupportedOperationException("todo"); - } - - @Override - public void getAll(File toFile) throws IOException { - throw new UnsupportedOperationException("todo"); - } - - @Override - public void putAll(File fromFile) throws IOException { - throw new UnsupportedOperationException("todo"); - } - - @Override - public V newValueInstance() { - throw new UnsupportedOperationException("todo"); - } - - @Override - public K newKeyInstance() { - return null; - } - - @Override - public Class valueClass() { - return null; - } - - private E toObject(Class eClass, Supplier b) { - - final byte[] bytes = b.get(); - if (byte[].class.isAssignableFrom(eClass)) - return (E) bytes; - - if (bytes == null) - return null; - final Wire wire = toWire(bytes); - buffer.flip(); - return wire.getValueIn().object(eClass); - } - - @Override - public V put(final K key, final V value) { - nullCheck(key); - return toObject(vClass, () -> { - final byte[] put = map.put(bytes(key), bytes(value)); - return put; - }); - } - - @Override - public V remove(Object key) { - nullCheck(key); - return toObject(vClass, () -> map.remove(bytes(key))); - } - - @Override - public void putAll(Map m) { - - for (final Map.Entry e : m.entrySet()) { - map.put(bytes(e.getKey()), bytes(e.getValue())); - } - - } - - @Override - public void clear() { - map.clear(); - } - - - @NotNull - @Override - public Set keySet() { - - return new AbstractSet() { - - public Iterator iterator() { - return new Iterator() { - private Iterator> i = entrySet().iterator(); - - public boolean hasNext() { - return i.hasNext(); - } - - public K next() { - return i.next().getKey(); - } - - public void remove() { - i.remove(); - } - }; - } - - public int size() { - return EngineMap.this.size(); - } - - public boolean isEmpty() { - return EngineMap.this.isEmpty(); - } - - public void clear() { - EngineMap.this.clear(); - } - - public boolean contains(Object k) { - return EngineMap.this.containsKey(k); - } - }; - - } - - - public Collection values() { - return new AbstractCollection() { - public Iterator iterator() { - return new Iterator() { - private Iterator> i = entrySet().iterator(); - - public boolean hasNext() { - return i.hasNext(); - } - - public V next() { - return i.next().getValue(); - } - - public void remove() { - i.remove(); - } - }; - } - - public int size() { - return EngineMap.this.size(); - } - - public boolean isEmpty() { - return EngineMap.this.isEmpty(); - } - - public void clear() { - EngineMap.this.clear(); - } - - public boolean contains(Object v) { - return EngineMap.this.containsValue(v); - } - }; - } - - - @NotNull - @Override - public Set> entrySet() { - - return new AbstractSet>() { - - - @NotNull - @Override - public Iterator> iterator() { - final Iterator> iterator = map.entrySet().iterator(); - return new Iterator>() { - - @Override - public boolean hasNext() { - return iterator.hasNext(); - } - - @Override - public Entry next() { - - final Entry next = iterator.next(); - - return new Entry() { - - @Override - public K getKey() { - return toObject(kClass, () -> next.getKey()); - } - - @Override - public V getValue() { - return toObject(vClass, () -> next.getValue()); - } - - @Override - public V setValue(V value) { - throw new UnsupportedOperationException("todo"); - } - }; - - - } - }; - } - - - public int size() { - return EngineMap.this.size(); - } - - public boolean isEmpty() { - return EngineMap.this.isEmpty(); - } - - public void clear() { - EngineMap.this.clear(); - } - - public boolean contains(Object v) { - return EngineMap.this.containsValue(v); - } - - - @Override - public boolean remove(Object o) { - return EngineMap.this.remove(o) != null; - } - }; - } - - - @Override - public V putIfAbsent(K key, V value) { - nullCheck(key); - // nullCheck(value); - return toObject(vClass, () -> map.putIfAbsent(bytes(key), bytes(value))); - } - - private void nullCheck(Object o) { - if (o == null) - throw new NullPointerException(); - } - - @Override - public boolean remove(Object key, Object value) { - nullCheck(key); - - return map.remove(bytes(key), bytes(value)); - } - - @Override - public boolean replace(K key, V oldValue, V newValue) { - - nullCheck(key); - nullCheck(oldValue); - nullCheck(newValue); - return map.replace(bytes(key), bytes(oldValue), bytes(newValue)); - } - - @Override - public V replace(K key, V value) { - nullCheck(key); - // nullCheck(value); - return toObject(vClass, () -> map.replace(bytes(key), bytes(value))); - } - - @Override - public File file() { - throw new UnsupportedOperationException("todo"); - } - - @Override - public long longSize() { - return map.size(); - } - - @Override - public MapKeyContext context(K key) { - throw new UnsupportedOperationException("todo"); - } - - @Override - public Class keyClass() { - throw new UnsupportedOperationException("todo"); - } - - @Override - public boolean forEachEntryWhile(Predicate> predicate) { - throw new UnsupportedOperationException("todo"); - } - - @Override - public void forEachEntry(Consumer> action) { - throw new UnsupportedOperationException("todo"); - } - - @Override - public void close() { - - } - - - @Override - public String toString() { - if (isEmpty()) - return "{}"; - StringBuilder sb = new StringBuilder(); - sb.append('{'); - forEach((k, v) -> sb - .append(k != this ? k : "(this Map)") - .append('=') - .append(v != this ? v : "(this Map)") - .append(',').append(' ')); - if (sb.length() > 2) - sb.setLength(sb.length() - 2); - sb.append('}'); - return sb.toString(); - } -} diff --git a/src/main/java/net/openhft/chronicle/map/MapWireConnectionHub.java b/src/main/java/net/openhft/chronicle/map/MapWireConnectionHub.java deleted file mode 100755 index 48a6676f3..000000000 --- a/src/main/java/net/openhft/chronicle/map/MapWireConnectionHub.java +++ /dev/null @@ -1,154 +0,0 @@ -package net.openhft.chronicle.map; - -import net.openhft.chronicle.hash.ChronicleHashInstanceBuilder; -import net.openhft.chronicle.hash.replication.ReplicationHub; -import net.openhft.chronicle.hash.replication.TcpTransportAndNetworkConfig; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.Closeable; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Map; -import java.util.function.Supplier; - -import static java.util.concurrent.TimeUnit.SECONDS; -import static net.openhft.chronicle.map.ChronicleMapBuilder.of; - -/** - * Created by Rob Austin - */ -public class MapWireConnectionHub implements Closeable { - - public static final int MAP_SERVICE = 3; - private static final Logger LOG = LoggerFactory.getLogger(MapWireHandlerProcessor.class); - private final Map channelMap; - private final ReplicationHub hub; - private final ArrayList bytesChronicleMaps = new ArrayList<>(); - private final ChannelProvider provider; - private final Supplier>> mapFactory; - protected ChronicleMap channelNameToId; - - - public MapWireConnectionHub( - byte localIdentifier, - int serverPort) throws IOException { - - // this is used to hold the name to channel id - final Supplier>> - channelNameToIdFactory = () -> of(String.class, Integer.class).instance(); - - final TcpTransportAndNetworkConfig tcpConfig = TcpTransportAndNetworkConfig - .of(serverPort) - .heartBeatInterval(1, SECONDS); - - hub = ReplicationHub.builder().tcpTransportAndNetwork(tcpConfig).createWithId(localIdentifier); - - channelNameToId = (ChronicleMap) channelNameToIdFactory.get() - .replicatedViaChannel(hub.createChannel(MAP_SERVICE)).create(); - - provider = ChannelProvider.getProvider(hub); - channelMap = provider.chronicleChannelMap(); - - mapFactory = () -> of(byte[].class, byte[].class).instance(); - - - } - - - /** - * @return the next free channel id - */ - short getNextFreeChannel() { - - // todo this is a horrible hack, it slow and not processor safe, but was added to get - // todo something working for now. - - int max = 3; - for (Integer channel : channelNameToId.values()) { - max = Math.max(max, channel); - } - - return (short) (max + 1); - - } - - /** - * gets the channel id for a name, or creates a new one if this name is not yet assosiated to a - * channel - * - * @param fromName the name of the channel - * @param builder - * @return the id associated with this name - */ - BytesChronicleMap acquireMap(@NotNull final String fromName, - @NotNull final ChronicleHashInstanceBuilder builder) - throws IOException { - - final Integer channelId = channelNameToId.get(fromName); - - if (channelId != null) - return bytesMap(channelId); - - final int nextFreeChannel = getNextFreeChannel(); - - mapFactory.get().replicatedViaChannel(hub.createChannel(nextFreeChannel)).create(); - channelNameToId.put(fromName, nextFreeChannel); - - return bytesMap(nextFreeChannel); - } - - - /** - * this is used to push the data straight into the entry in memory - * - * @param channelId the ID of the map - * @return a BytesChronicleMap used to update the memory which holds the chronicle map - */ - @Nullable - BytesChronicleMap bytesMap(int channelId) { - - final BytesChronicleMap bytesChronicleMap = (channelId < bytesChronicleMaps.size()) - ? bytesChronicleMaps.get(channelId) - : null; - - if (bytesChronicleMap != null) - return bytesChronicleMap; - - // grow the array - for (int i = bytesChronicleMaps.size(); i <= channelId; i++) { - bytesChronicleMaps.add(null); - } - - final ReplicatedChronicleMap delegate = map(channelId); - final BytesChronicleMap element = new BytesChronicleMap(delegate); - bytesChronicleMaps.set(channelId, element); - return element; - - } - - /** - * gets the map for this channel id - * - * @param channelId the ID of the map - * @return the chronicle map with this {@code channelId} - */ - @NotNull - private ReplicatedChronicleMap map(int channelId) { - - // todo this cast is a bit of a hack, improve later - final ReplicatedChronicleMap map = - (ReplicatedChronicleMap) channelMap.get(channelId); - - if (map != null) - return map; - - throw new IllegalStateException(); - } - - public void close() throws IOException { - provider.close(); - } -} diff --git a/src/main/java/net/openhft/chronicle/map/MapWireHandler.java b/src/main/java/net/openhft/chronicle/map/MapWireHandler.java deleted file mode 100644 index 282ef1f8a..000000000 --- a/src/main/java/net/openhft/chronicle/map/MapWireHandler.java +++ /dev/null @@ -1,41 +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.wire.ValueIn; -import net.openhft.chronicle.wire.ValueOut; -import net.openhft.chronicle.wire.Wire; - -import java.io.StreamCorruptedException; -import java.util.Map; -import java.util.function.BiConsumer; -import java.util.function.Function; - - -public interface MapWireHandler { - - void process(Wire in, - Wire out, - Map map, - CharSequence csp, - BiConsumer vToWire, - Function kFromWire, - Function vFromWire) throws StreamCorruptedException; - -} diff --git a/src/main/java/net/openhft/chronicle/map/MapWireHandlerProcessor.java b/src/main/java/net/openhft/chronicle/map/MapWireHandlerProcessor.java deleted file mode 100644 index c72fdab3f..000000000 --- a/src/main/java/net/openhft/chronicle/map/MapWireHandlerProcessor.java +++ /dev/null @@ -1,463 +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; - -/** - * Created by Rob Austin - */ - -import net.openhft.chronicle.bytes.Bytes; -import net.openhft.chronicle.engine.client.ClientWiredStatelessTcpConnectionHub; -import net.openhft.chronicle.hash.impl.util.BuildVersion; -import net.openhft.chronicle.network.event.EventGroup; -import net.openhft.chronicle.wire.*; -import org.jetbrains.annotations.NotNull; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.IOException; -import java.io.StreamCorruptedException; -import java.util.HashMap; -import java.util.Map; -import java.util.concurrent.atomic.AtomicLong; -import java.util.function.BiConsumer; -import java.util.function.Consumer; -import java.util.function.Function; - -import static net.openhft.chronicle.map.MapWireHandlerProcessor.EventId.*; -import static net.openhft.chronicle.map.MapWireHandlerProcessor.Params.*; -import static net.openhft.chronicle.wire.CoreFields.reply; -import static net.openhft.chronicle.wire.Wires.acquireStringBuilder; - - -/** - * @author Rob Austin. - */ -public class MapWireHandlerProcessor implements - MapWireHandler, - Consumer { - - private CharSequence csp; - private BiConsumer vToWire; - private Function wireToK; - private Function wireToV; - - - @Override - public void process(Wire in, Wire out, Map map, CharSequence csp, BiConsumer vToWire, Function kFromWire, Function vFromWire) throws StreamCorruptedException { - - this.vToWire = vToWire; - this.wireToK = kFromWire; - this.wireToV = vFromWire; - - try { - this.inWire = in; - this.outWire = out; - this.map = map; - this.csp = csp; - inWire.readDocument(metaDataConsumer, dataConsumer); - } catch (Exception e) { - LOG.error("", e); - } - } - - - enum Params implements WireKey { - key, - value, - oldValue, - newValue - } - - public enum EventId implements ParameterizeWireKey { - longSize, - size, - containsKey(key), - containsValue(value), - get(key), - getAndPut(key, value), - put(key, value), - remove(key), - removeWithoutAcc(key), - clear, - keySet, - values, - entrySet, - entrySetRestricted, - replace(key, value), - replaceWithOldAndNewValue(key, oldValue, newValue), - putIfAbsent(key, value), - removeWithValue(key, value), - toString, - getApplicationVersion, - persistedDataVersion, - putAll, - putAllWithoutAcc, - hashCode, - mapForKey, - putMapped, - keyBuilder, - valueBuilder, - createChannel, - remoteIdentifier; - - private final WireKey[] params; - -

EventId(P... params) { - this.params = params; - } - - public

P[] params() { - return (P[]) this.params; - } - } - - - private static final Logger LOG = LoggerFactory.getLogger(MapWireHandlerProcessor.class); - - public static final int SIZE_OF_SIZE = ClientWiredStatelessTcpConnectionHub.SIZE_OF_SIZE; - - private final Map cidToCsp; - @NotNull - private final Map cspToCid = new HashMap<>(); - - private Wire inWire = null; - private Wire outWire = null; - - - private final Consumer metaDataConsumer = new Consumer() { - - @Override - public void accept(WireIn wireIn) { - - StringBuilder sb = Wires.acquireStringBuilder(); - - for (; ; ) { - final ValueIn read = inWire.read(sb); - if (CoreFields.tid.contentEquals(sb)) { - tid = read.int64(); - break; - } - } - - } - }; - - private Map map; - - public MapWireHandlerProcessor(@NotNull final Map cidToCsp) throws IOException { - this.cidToCsp = cidToCsp; - } - - @Override - public void accept(@NotNull final WireHandlers wireHandlers) { - - } - - public long tid; - private final AtomicLong cid = new AtomicLong(); - - /** - * create a new cid if one does not already exist for this csp - * - * @param csp the csp we wish to check for a cid - * @return the cid for this csp - */ - private long createCid(CharSequence csp) { - - final long newCid = cid.incrementAndGet(); - final Long aLong = cspToCid.putIfAbsent(csp, newCid); - - if (aLong != null) - return aLong; - - cidToCsp.put(newCid, csp.toString()); - return newCid; - - } - - final StringBuilder eventName = new StringBuilder(); - - private final Consumer dataConsumer = new Consumer() { - - @Override - public void accept(WireIn wireIn) { - final Bytes outBytes = outWire.bytes(); - try { - - final ValueIn valueIn = inWire.readEventName(eventName); - - outWire.writeDocument(true, wire -> outWire.write(CoreFields.tid).int64(tid)); - - writeData(out -> { - - if (clear.contentEquals(eventName)) { - map.clear(); - - return; - } - - if (putAll.contentEquals(eventName)) { - - final Map data = new HashMap(); - while (valueIn.hasNext()) { - valueIn.sequence(v -> valueIn.marshallable(wire -> data.put( - wireToK.apply(wire.read(put.params()[0])), - wireToV.apply(wire.read(put.params()[1]))))); - } - - map.putAll(data); - return; - } - - if (EventId.putIfAbsent.contentEquals(eventName)) { - - valueIn.marshallable(wire -> { - - final Params[] params = putIfAbsent.params(); - final K key = wireToK.apply(wire.read(params[0])); - final V value = wireToV.apply(wire.read(params[1])); - final V v = map.putIfAbsent(key, value); - vToWire.accept(outWire.writeEventName(reply), v); - - }); - - return; - } - - if (size.contentEquals(eventName)) { - outWire.writeEventName(reply).int64(map.size()); - return; - } - - if (keySet.contentEquals(eventName)) { - outWire.writeEventName(reply).type("set-proxy").writeValue() - - .marshallable(w -> { - CharSequence root = csp.subSequence(0, csp - .length() - "#map".length()); - - final StringBuilder csp = acquireStringBuilder() - .append(root) - .append("#keySet"); - - w.write(CoreFields.csp).text(csp); - w.write(CoreFields.cid).int64(createCid(csp)); - }); - - - return; - } - - if (values.contentEquals(eventName)) { - throw new UnsupportedOperationException("todo"); - } - - - if (entrySet.contentEquals(eventName)) { - outWire.writeEventName(reply).type("set-proxy").writeValue() - - .marshallable(w -> { - CharSequence root = csp.subSequence(0, csp - .length() - "#map".length()); - - final StringBuilder csp = acquireStringBuilder() - .append(root) - .append("#entrySet"); - - w.write(CoreFields.csp).text(csp); - w.write(CoreFields.cid).int64(createCid(csp)); - }); - - - return; - } - - if (size.contentEquals(eventName)) { - outWire.writeEventName(reply).int64(map.size()); - return; - } - - - if (containsKey.contentEquals(eventName)) { - outWire.writeEventName(reply) - .bool(map.containsKey(wireToK.apply(valueIn))); - return; - } - - if (containsValue.contentEquals(eventName)) { - outWire.writeEventName(reply).bool( - map.containsValue(wireToV.apply(valueIn))); - return; - } - - if (get.contentEquals(eventName)) { - final K key = wireToK.apply(valueIn); - vToWire.accept(outWire.writeEventName(reply), - map.get(key)); - return; - } - - if (getAndPut.contentEquals(eventName)) { - - valueIn.marshallable(wire -> { - - final Params[] params = getAndPut.params(); - final K key = wireToK.apply(wire.read(params[0])); - final V value = wireToV.apply(wire.read(params[1])); - - vToWire.accept(outWire.writeEventName(reply), - map.put(key, value)); - - }); - - return; - } - - if (remove.contentEquals(eventName)) { - final K key = wireToK.apply(valueIn); - vToWire.accept(outWire.writeEventName(reply), map.remove(key)); - return; - } - - - if (replace.contentEquals(eventName)) { - valueIn.marshallable(wire -> { - final Params[] params = replace.params(); - final K key = wireToK.apply(wire.read(params[0])); - final V value = wireToV.apply(wire.read(params[1])); - - vToWire.accept(outWire.writeEventName(reply), - map.replace(key, value)); - - }); - - - return; - } - - if (replaceWithOldAndNewValue.contentEquals(eventName)) { - valueIn.marshallable(wire -> { - final Params[] params = replaceWithOldAndNewValue.params(); - final K key = wireToK.apply(wire.read(params[0])); - final V oldValue = wireToV.apply(wire.read(params[1])); - final V newValue = wireToV.apply(wire.read(params[2])); - outWire.writeEventName(reply).bool(map.replace(key, oldValue, newValue)); - - }); - return; - } - - if (putIfAbsent.contentEquals(eventName)) { - valueIn.marshallable(wire -> { - final Params[] params = putIfAbsent.params(); - final K key = wireToK.apply(wire.read(params[0])); - final V value = wireToV.apply(wire.read(params[1])); - vToWire.accept(outWire.writeEventName(reply), - map.putIfAbsent(key, value)); - - }); - - return; - } - - if (removeWithValue.contentEquals(eventName)) { - throw new UnsupportedOperationException("todo"); - } - - - if (getApplicationVersion.contentEquals(eventName)) { - outWire.writeEventName(reply).text(applicationVersion()); - return; - } - - - if (hashCode.contentEquals(eventName)) { - outWire.writeEventName(reply).int32(map.hashCode()); - return; - } - - throw new IllegalStateException("unsupported event=" + eventName); - }); - } catch (Exception e) { - LOG.error("", e); - } finally - - { - - if (EventGroup.IS_DEBUG) { - - long len = outBytes.position() - SIZE_OF_SIZE; - if (len == 0) { - System.out.println("--------------------------------------------\n" + - "server writes:\n\n"); - } else { - - - System.out.println("--------------------------------------------\n" + - "server writes:\n\n" + - Wires.fromSizePrefixedBlobs(outBytes, SIZE_OF_SIZE, len)); - - } - } - } - - - } - }; - - - @NotNull - private CharSequence applicationVersion() { - return BuildVersion.version(); - } - private long start; - - - /** - * write and exceptions and rolls back if no data was written - */ - private void writeData(@NotNull Consumer c) { - - - outWire.writeDocument(false, out -> { - - final long position = outWire.bytes().position(); - try { - c.accept(outWire); - - } catch (Exception exception) { - outWire.bytes().position(position); - outWire.writeEventName(() -> "exception").marshallable( - new net.openhft.chronicle.wire.util.ExceptionMarshaller(exception)); - } - - // write 'reply : {} ' if no data was sent - if (position == outWire.bytes().position()) { - outWire.writeEventName(reply).marshallable(AbstactStatelessClient.EMPTY); - } - - }); - - - } - - -} -