mirror of
https://github.com/OpenHFT/Chronicle-Map.git
synced 2021-09-13 01:26:12 +00:00
updated to release versions
This commit is contained in:
@@ -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<K, V> implements ChronicleMap<K, V> {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(EngineMap.class);
|
||||
|
||||
private final Class<K> kClass;
|
||||
private final Class<V> vClass;
|
||||
|
||||
private final Bytes<ByteBuffer> buffer = Bytes.elasticByteBuffer();
|
||||
private final Map<byte[], byte[]> map;
|
||||
private final Class<? extends Wire> wireType;
|
||||
|
||||
public EngineMap(Map<byte[], byte[]> underlyingMap,
|
||||
Class<K> kClass,
|
||||
Class<V> vClass,
|
||||
Class<? extends Wire> wireType ) throws IOException {
|
||||
this.wireType = wireType;
|
||||
this.kClass = kClass;
|
||||
this.vClass = vClass;
|
||||
this.map = underlyingMap;
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static Map<byte[], byte[]> 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<byte[]> 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<K, V> acquireContext(@NotNull K key, @NotNull V usingValue) {
|
||||
throw new UnsupportedOperationException("todo");
|
||||
}
|
||||
|
||||
@Override
|
||||
public <R> R getMapped(K key, @NotNull SerializableFunction<? super V, R> function) {
|
||||
throw new UnsupportedOperationException("todo");
|
||||
}
|
||||
|
||||
@Override
|
||||
public V putMapped(K key, @NotNull UnaryOperator<V> 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<V> valueClass() {
|
||||
return null;
|
||||
}
|
||||
|
||||
private <E> E toObject(Class<E> eClass, Supplier<byte[]> 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<? extends K, ? extends V> m) {
|
||||
|
||||
for (final Map.Entry<? extends K, ? extends V> e : m.entrySet()) {
|
||||
map.put(bytes(e.getKey()), bytes(e.getValue()));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
map.clear();
|
||||
}
|
||||
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Set<K> keySet() {
|
||||
|
||||
return new AbstractSet<K>() {
|
||||
|
||||
public Iterator<K> iterator() {
|
||||
return new Iterator<K>() {
|
||||
private Iterator<Entry<K, V>> 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<V> values() {
|
||||
return new AbstractCollection<V>() {
|
||||
public Iterator<V> iterator() {
|
||||
return new Iterator<V>() {
|
||||
private Iterator<Entry<K, V>> 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<Entry<K, V>> entrySet() {
|
||||
|
||||
return new AbstractSet<Entry<K, V>>() {
|
||||
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Iterator<Entry<K, V>> iterator() {
|
||||
final Iterator<Entry<byte[], byte[]>> iterator = map.entrySet().iterator();
|
||||
return new Iterator<Entry<K, V>>() {
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return iterator.hasNext();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Entry<K, V> next() {
|
||||
|
||||
final Entry<byte[], byte[]> next = iterator.next();
|
||||
|
||||
return new Entry<K, V>() {
|
||||
|
||||
@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<K, V> context(K key) {
|
||||
throw new UnsupportedOperationException("todo");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<K> keyClass() {
|
||||
throw new UnsupportedOperationException("todo");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean forEachEntryWhile(Predicate<? super MapKeyContext<K, V>> predicate) {
|
||||
throw new UnsupportedOperationException("todo");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void forEachEntry(Consumer<? super MapKeyContext<K, V>> 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();
|
||||
}
|
||||
}
|
||||
@@ -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<Integer, Replica> channelMap;
|
||||
private final ReplicationHub hub;
|
||||
private final ArrayList<BytesChronicleMap> bytesChronicleMaps = new ArrayList<>();
|
||||
private final ChannelProvider provider;
|
||||
private final Supplier<ChronicleHashInstanceBuilder<ChronicleMap<byte[], byte[]>>> mapFactory;
|
||||
protected ChronicleMap<String, Integer> channelNameToId;
|
||||
|
||||
|
||||
public MapWireConnectionHub(
|
||||
byte localIdentifier,
|
||||
int serverPort) throws IOException {
|
||||
|
||||
// this is used to hold the name to channel id
|
||||
final Supplier<ChronicleHashInstanceBuilder<ChronicleMap<String, Integer>>>
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -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<K, V> {
|
||||
|
||||
void process(Wire in,
|
||||
Wire out,
|
||||
Map<K, V> map,
|
||||
CharSequence csp,
|
||||
BiConsumer<ValueOut, V> vToWire,
|
||||
Function<ValueIn, K> kFromWire,
|
||||
Function<ValueIn, V> vFromWire) throws StreamCorruptedException;
|
||||
|
||||
}
|
||||
@@ -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<K, V> implements
|
||||
MapWireHandler<K, V>,
|
||||
Consumer<WireHandlers> {
|
||||
|
||||
private CharSequence csp;
|
||||
private BiConsumer<ValueOut, V> vToWire;
|
||||
private Function<ValueIn, K> wireToK;
|
||||
private Function<ValueIn, V> wireToV;
|
||||
|
||||
|
||||
@Override
|
||||
public void process(Wire in, Wire out, Map<K, V> map, CharSequence csp, BiConsumer<ValueOut, V> vToWire, Function<ValueIn, K> kFromWire, Function<ValueIn, V> 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;
|
||||
|
||||
<P extends WireKey> EventId(P... params) {
|
||||
this.params = params;
|
||||
}
|
||||
|
||||
public <P extends WireKey> 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<Long, CharSequence> cidToCsp;
|
||||
@NotNull
|
||||
private final Map<CharSequence, Long> cspToCid = new HashMap<>();
|
||||
|
||||
private Wire inWire = null;
|
||||
private Wire outWire = null;
|
||||
|
||||
|
||||
private final Consumer<WireIn> metaDataConsumer = new Consumer<WireIn>() {
|
||||
|
||||
@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<K, V> map;
|
||||
|
||||
public MapWireHandlerProcessor(@NotNull final Map<Long, CharSequence> 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<WireIn> dataConsumer = new Consumer<WireIn>() {
|
||||
|
||||
@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<EMPTY>");
|
||||
} 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<WireOut> 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);
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user