Skip to content

Commit

Permalink
CE-23 local and remote wire maps can now share the same replication hub
Browse files Browse the repository at this point in the history
  • Loading branch information
Rob Austin committed Apr 22, 2015
1 parent bd643b1 commit f400352
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 22 deletions.
Expand Up @@ -19,7 +19,7 @@
/**
* Created by Rob Austin
*/
public class MapWireConnectionHub<K, V> {
public class MapWireConnectionHub<K, V> implements Cloneable{

private static final Logger LOG = LoggerFactory.getLogger(MapWireHandler.class);
public static final int MAP_SERVICE = 3;
Expand All @@ -31,6 +31,7 @@ public class MapWireConnectionHub<K, V> {
private final ReplicationHub hub;

private final ArrayList<BytesChronicleMap> bytesChronicleMaps = new ArrayList<>();
private final ChannelProvider provider;


public MapWireConnectionHub(
Expand All @@ -52,7 +53,7 @@ public MapWireConnectionHub(
channelNameToId = (ChronicleMap) channelNameToIdFactory.get()
.replicatedViaChannel(hub.createChannel(MAP_SERVICE)).create();

ChannelProvider provider = ChannelProvider.getProvider(hub);
provider = ChannelProvider.getProvider(hub);
channelMap = provider.chronicleChannelMap();

}
Expand Down Expand Up @@ -156,4 +157,7 @@ private ReplicatedChronicleMap map(int channelId) {
throw new IllegalStateException();
}

public void close() throws IOException {
provider.close();
}
}
@@ -1,7 +1,6 @@
package net.openhft.chronicle.map;

import net.openhft.chronicle.bytes.Bytes;
import net.openhft.chronicle.engine.client.ClientWiredStatelessTcpConnectionHub.CoreFields;
import net.openhft.chronicle.wire.TextWire;
import net.openhft.chronicle.wire.Wire;
import org.jetbrains.annotations.NotNull;
Expand All @@ -11,11 +10,12 @@
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.function.Supplier;

/**
* Created by Rob Austin
*/
public class MapWireConnectionHubByName<K, V> implements Map<K, V> {
public class WireMap<K, V> implements Map<K, V> {

private final Class<K> kClass;
private final Class<V> vClass;
Expand All @@ -27,13 +27,16 @@ public class MapWireConnectionHubByName<K, V> implements Map<K, V> {
private final Map<byte[], byte[]> map;


Class<? extends Wire> wireType = TextWire.class;
// todo
private final Class<? extends Wire> wireType;

public MapWireConnectionHubByName(String name,
Class<V> vClass,
Class<K> kClass,
MapWireConnectionHub mapWireConnectionHub) throws IOException {
public WireMap(String name,
Class<V> vClass,
Class<K> kClass,
MapWireConnectionHub mapWireConnectionHub,
Class<? extends Wire> wireType) throws IOException {
this.mapWireConnectionHub = mapWireConnectionHub;
this.wireType = wireType;
final BytesChronicleMap b = this.mapWireConnectionHub.acquireMap(name);
this.map = (Map) b.delegate;
this.vClass = vClass;
Expand Down Expand Up @@ -75,7 +78,6 @@ public boolean containsKey(Object key) {

private byte[] bytes(Object key) {


final Wire wire = toWire();
AbstactStatelessClient.writeField(key, wire.getValueOut());
wire.bytes().flip();
Expand All @@ -87,32 +89,39 @@ private byte[] bytes(Object key) {
@Override
public boolean containsValue(Object value) {
return map.containsValue(bytes(value));

}

@Override
public V get(Object key) {
final byte[] bytes1 = bytes(key);
byte[] bytes = map.get(bytes1);
public V get(@NotNull final Object key) {

return toObject(vClass, new Supplier<byte[]>() {
@Override
public byte[] get() {

final byte[] bytes = bytes(key);
final byte[] bytes1 = map.get(bytes);
return bytes1;
}
});
}

private <E> E toObject(Class<E> eClass, Supplier<byte[]> b) {
final byte[] bytes = b.get();
if (bytes == null)
return null;

final Wire wire = toWire(bytes);
buffer.flip();
return AbstactStatelessClient.<V>readObject(wire.getValueIn(), null, vClass);
return AbstactStatelessClient.readObject(wire.getValueIn(), null, eClass);
}

@Override
public V put(K key, V value) {
final Wire wire = toWire(map.put(bytes(key), bytes(value)));
return AbstactStatelessClient.<V>readObject(CoreFields.reply, wire, null, vClass);
public V put(@NotNull final K key, @NotNull final V value) {
return toObject(vClass, () -> map.put(bytes(key), bytes(value)));
}

@Override
public V remove(Object key) {
final Wire wire = toWire(map.remove(bytes(key)));
return AbstactStatelessClient.<V>readObject(CoreFields.reply, wire, null, vClass);
return toObject(vClass, () -> map.remove(bytes(key)));
}

@Override
Expand Down Expand Up @@ -140,7 +149,7 @@ public Collection<V> values() {
@NotNull
@Override
public Set<Entry<K, V>> entrySet() {
return null;
throw new UnsupportedOperationException("todo");
}

}

0 comments on commit f400352

Please sign in to comment.