mirror of
https://github.com/OpenHFT/Chronicle-Map.git
synced 2021-09-13 01:26:12 +00:00
HCOLL-167 created StatelessServerConnector, but its still in draft.
This commit is contained in:
@@ -51,7 +51,7 @@ public class Serializer<O, VW, MVW extends MetaBytesWriter<O, VW>> {
|
||||
}
|
||||
}
|
||||
|
||||
public void writeMarshallable(@NotNull Bytes out, O value) {
|
||||
public void writeMarshallable(O value, @NotNull Bytes out) {
|
||||
|
||||
ThreadLocalCopies copies = readerProvider.getCopies(null);
|
||||
VW valueWriter = writerProvider.get(copies, originalWriter);
|
||||
|
||||
@@ -47,6 +47,18 @@ public class StatelessMapClient<K, V> implements ChronicleMap<K, V> {
|
||||
private final Serializer<V, ?, ?> valueSerializer;
|
||||
private final Serializer<K, ?, ?> keySerializer;
|
||||
|
||||
public StatelessMapClient(Bytes out, Class<K> kClass, Class<V> vClass) {
|
||||
|
||||
final SerializationBuilder<K> keyBuilder = new SerializationBuilder<K>(kClass,
|
||||
SerializationBuilder.Role.KEY);
|
||||
final SerializationBuilder<V> valueBuilder = new SerializationBuilder<V>(vClass,
|
||||
SerializationBuilder.Role.VALUE);
|
||||
|
||||
keySerializer = new Serializer(keyBuilder);
|
||||
valueSerializer = new Serializer(valueBuilder);
|
||||
|
||||
this.out = out;
|
||||
}
|
||||
|
||||
@Override
|
||||
public File file() {
|
||||
@@ -76,20 +88,6 @@ public class StatelessMapClient<K, V> implements ChronicleMap<K, V> {
|
||||
private final Bytes out;
|
||||
|
||||
|
||||
public StatelessMapClient(Bytes out, Class<K> kClass, Class<V> vClass) {
|
||||
|
||||
final SerializationBuilder<K> keyBuilder = new SerializationBuilder<K>(kClass,
|
||||
SerializationBuilder.Role.KEY);
|
||||
final SerializationBuilder<V> valueBuilder = new SerializationBuilder<V>(vClass,
|
||||
SerializationBuilder.Role.VALUE);
|
||||
|
||||
keySerializer = new Serializer(keyBuilder);
|
||||
valueSerializer = new Serializer(valueBuilder);
|
||||
|
||||
this.out = out;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public V putIfAbsent(K key, V value) {
|
||||
writeEvent(PUT_IF_ABSENT);
|
||||
@@ -125,7 +123,7 @@ public class StatelessMapClient<K, V> implements ChronicleMap<K, V> {
|
||||
}
|
||||
|
||||
private void writeValue(V newValue) {
|
||||
valueSerializer.writeMarshallable(out, newValue);
|
||||
valueSerializer.writeMarshallable(newValue, out);
|
||||
}
|
||||
|
||||
|
||||
@@ -245,7 +243,7 @@ public class StatelessMapClient<K, V> implements ChronicleMap<K, V> {
|
||||
|
||||
final HashMap<K, V> safeCopy = new HashMap<K, V>(map);
|
||||
|
||||
out.writeLong(safeCopy.size());
|
||||
out.writeStopBit(safeCopy.size());
|
||||
|
||||
final Set<Entry> entries = (Set) safeCopy.entrySet();
|
||||
|
||||
@@ -274,10 +272,10 @@ public class StatelessMapClient<K, V> implements ChronicleMap<K, V> {
|
||||
}
|
||||
|
||||
private Set<K> readKeySet(Bytes in) {
|
||||
int size = in.readInt();
|
||||
long size = in.readStopBit();
|
||||
final HashSet<K> result = new HashSet<>();
|
||||
|
||||
for (int i = 0; i < size; i++) {
|
||||
for (long i = 0; i < size; i++) {
|
||||
result.add(readKey(out));
|
||||
}
|
||||
return result;
|
||||
@@ -293,11 +291,14 @@ public class StatelessMapClient<K, V> implements ChronicleMap<K, V> {
|
||||
writeEvent(VALUES);
|
||||
|
||||
// get the data back from the server
|
||||
Bytes in = blockingFetch();
|
||||
final Bytes in = blockingFetch();
|
||||
|
||||
int size = in.readInt();
|
||||
final long size = in.readStopBit();
|
||||
|
||||
ArrayList<V> result = new ArrayList<V>();
|
||||
if (size > Integer.MAX_VALUE)
|
||||
throw new IllegalStateException("size=" + size + " is too large.");
|
||||
|
||||
final ArrayList<V> result = new ArrayList<V>((int) size);
|
||||
|
||||
for (int i = 0; i < size; i++) {
|
||||
result.add(readValue(in));
|
||||
@@ -314,7 +315,7 @@ public class StatelessMapClient<K, V> implements ChronicleMap<K, V> {
|
||||
// get the data back from the server
|
||||
Bytes bytes = blockingFetch();
|
||||
|
||||
int size = bytes.readInt();
|
||||
long size = bytes.readStopBit();
|
||||
|
||||
Set<Map.Entry<K, V>> result = new HashSet<Map.Entry<K, V>>();
|
||||
|
||||
@@ -322,7 +323,6 @@ public class StatelessMapClient<K, V> implements ChronicleMap<K, V> {
|
||||
K k = keySerializer.readMarshallable(bytes);
|
||||
V v = valueSerializer.readMarshallable(bytes);
|
||||
result.add(new Entry(k, v));
|
||||
|
||||
}
|
||||
|
||||
return result;
|
||||
@@ -335,7 +335,7 @@ public class StatelessMapClient<K, V> implements ChronicleMap<K, V> {
|
||||
* @param key the key of the map
|
||||
*/
|
||||
private void writeKey(K key) {
|
||||
keySerializer.writeMarshallable(out, key);
|
||||
keySerializer.writeMarshallable(key, out);
|
||||
}
|
||||
|
||||
private void writeEvent(EventId event) {
|
||||
|
||||
@@ -0,0 +1,265 @@
|
||||
package net.openhft.chronicle.map;
|
||||
|
||||
import net.openhft.lang.io.Bytes;
|
||||
import net.openhft.lang.model.constraints.NotNull;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* @author Rob Austin.
|
||||
*/
|
||||
public class StatelessServerConnector<K, V> {
|
||||
|
||||
|
||||
private final ChronicleMap<K, V> map;
|
||||
|
||||
|
||||
private final Serializer<V, ?, ?> valueSerializer;
|
||||
private final Serializer<K, ?, ?> keySerializer;
|
||||
|
||||
public StatelessServerConnector(Class<K> kClass, Class<V> vClass, ChronicleMap<K, V> map) {
|
||||
|
||||
this.map = map;
|
||||
|
||||
final SerializationBuilder<K> keyBuilder = new SerializationBuilder<K>(kClass,
|
||||
SerializationBuilder.Role.KEY);
|
||||
final SerializationBuilder<V> valueBuilder = new SerializationBuilder<V>(vClass,
|
||||
SerializationBuilder.Role.VALUE);
|
||||
|
||||
keySerializer = new Serializer(keyBuilder);
|
||||
valueSerializer = new Serializer(valueBuilder);
|
||||
|
||||
}
|
||||
|
||||
|
||||
public void marshall(@NotNull Bytes in, @NotNull Bytes out) {
|
||||
|
||||
byte b = in.readByte();
|
||||
|
||||
StatelessMapClient.EventId eventId = StatelessMapClient.EventId.values()[b];
|
||||
|
||||
switch (eventId) {
|
||||
|
||||
case LONG_SIZE:
|
||||
longSize(in, out);
|
||||
break;
|
||||
|
||||
case IS_EMPTY:
|
||||
isEmpty(in, out);
|
||||
break;
|
||||
|
||||
case CONTAINS_KEY:
|
||||
containsKey(in, out);
|
||||
break;
|
||||
case CONTAINS_VALUE:
|
||||
containsValue(in, out);
|
||||
break;
|
||||
case GET:
|
||||
get(in, out);
|
||||
break;
|
||||
case PUT:
|
||||
put(in, out);
|
||||
break;
|
||||
case REMOVE:
|
||||
remove(in, out);
|
||||
break;
|
||||
case PUT_ALL:
|
||||
putAll(in, out);
|
||||
break;
|
||||
case CLEAR:
|
||||
clear(in, out);
|
||||
break;
|
||||
case KEY_SET:
|
||||
keySet(in, out);
|
||||
break;
|
||||
case VALUES:
|
||||
values(in, out);
|
||||
break;
|
||||
case ENTRY_SET:
|
||||
entrySet(in, out);
|
||||
break;
|
||||
case REPLACE:
|
||||
replace(in, out);
|
||||
break;
|
||||
case REPLACE_WITH_OLD_AND_NEW_VALUE:
|
||||
replaceWithOldAndNew(in, out);
|
||||
break;
|
||||
case PUT_IF_ABSENT:
|
||||
putIfAbsent(in, out);
|
||||
break;
|
||||
case REMOVE_WITH_VALUE:
|
||||
removeWithValue(in, out);
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void removeWithValue(Bytes in, Bytes out) {
|
||||
boolean result = map.remove(readKey(in), readValue(in));
|
||||
reflectTransactionId(in, out);
|
||||
out.writeBoolean(result);
|
||||
}
|
||||
|
||||
private void replaceWithOldAndNew(Bytes in, Bytes out) {
|
||||
final K key = readKey(in);
|
||||
V oldValue = readValue(in);
|
||||
V newValue = readValue(in);
|
||||
reflectTransactionId(in, out);
|
||||
map.replace(key, oldValue, newValue);
|
||||
}
|
||||
|
||||
|
||||
public void longSize(Bytes in, Bytes out) {
|
||||
reflectTransactionId(in, out);
|
||||
out.writeLong(map.longSize());
|
||||
}
|
||||
|
||||
|
||||
public void size(Bytes in, Bytes out) {
|
||||
reflectTransactionId(in, out);
|
||||
out.writeInt(map.size());
|
||||
}
|
||||
|
||||
|
||||
public void isEmpty(Bytes in, Bytes out) {
|
||||
reflectTransactionId(in, out);
|
||||
out.writeBoolean(map.isEmpty());
|
||||
}
|
||||
|
||||
|
||||
public void containsKey(Bytes in, Bytes out) {
|
||||
K k = readKey(in);
|
||||
reflectTransactionId(in, out);
|
||||
out.writeBoolean(map.containsKey(k));
|
||||
}
|
||||
|
||||
|
||||
public void containsValue(Bytes in, Bytes out) {
|
||||
V v = readValue(in);
|
||||
reflectTransactionId(in, out);
|
||||
out.writeBoolean(map.containsValue(v));
|
||||
}
|
||||
|
||||
|
||||
public void get(Bytes in, Bytes out) {
|
||||
K k = readKey(in);
|
||||
reflectTransactionId(in, out);
|
||||
writeValue(map.get(k), out);
|
||||
}
|
||||
|
||||
|
||||
public void put(Bytes in, Bytes out) {
|
||||
K k = readKey(in);
|
||||
V v = readValue(in);
|
||||
reflectTransactionId(in, out);
|
||||
writeValue(map.put(k, v), out);
|
||||
}
|
||||
|
||||
|
||||
public void remove(Bytes in, Bytes out) {
|
||||
final V value = map.remove(readKey(in));
|
||||
reflectTransactionId(in, out);
|
||||
writeValue(value, out);
|
||||
}
|
||||
|
||||
|
||||
public void putAll(Bytes in, Bytes out) {
|
||||
map.putAll(readEntries(in));
|
||||
reflectTransactionId(in, out);
|
||||
}
|
||||
|
||||
private Map<K, V> readEntries(Bytes in) {
|
||||
|
||||
long size = in.readStopBit();
|
||||
final HashMap<K, V> result = new HashMap<K, V>();
|
||||
|
||||
for (long i = 0; i < size; i++) {
|
||||
result.put(readKey(in), readValue(in));
|
||||
}
|
||||
return result;
|
||||
|
||||
}
|
||||
|
||||
public void clear(Bytes in, Bytes out) {
|
||||
map.clear();
|
||||
reflectTransactionId(in, out);
|
||||
}
|
||||
|
||||
|
||||
public void keySet(Bytes in, Bytes out) {
|
||||
reflectTransactionId(in, out);
|
||||
|
||||
final Set<K> ks = map.keySet();
|
||||
out.writeStopBit(ks.size());
|
||||
for (K key : ks) {
|
||||
writeKey(key, out);
|
||||
}
|
||||
}
|
||||
|
||||
public void values(Bytes in, Bytes out) {
|
||||
reflectTransactionId(in, out);
|
||||
final Collection<V> values = map.values();
|
||||
out.writeStopBit(values.size());
|
||||
for (final V value : values) {
|
||||
writeValue(value, out);
|
||||
}
|
||||
}
|
||||
|
||||
public void entrySet(Bytes in, Bytes out) {
|
||||
reflectTransactionId(in, out);
|
||||
|
||||
final Set<Map.Entry<K, V>> entries = map.entrySet();
|
||||
out.writeStopBit(entries.size());
|
||||
for (Map.Entry<K, V> e : entries) {
|
||||
writeKey(e.getKey(), out);
|
||||
writeValue(e.getValue(), out);
|
||||
}
|
||||
}
|
||||
|
||||
public void putIfAbsent(Bytes in, Bytes out) {
|
||||
K key = readKey(in);
|
||||
V v = readValue(in);
|
||||
reflectTransactionId(in, out);
|
||||
writeValue(map.putIfAbsent(key, v), out);
|
||||
}
|
||||
|
||||
|
||||
public void replace(Bytes in, Bytes out) {
|
||||
K k = readKey(in);
|
||||
V v = readValue(in);
|
||||
|
||||
reflectTransactionId(in, out);
|
||||
map.replace(k, v);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* write the keysize and the key to the the {@code target} buffer
|
||||
*
|
||||
* @param key the key of the map
|
||||
*/
|
||||
private void writeKey(K key, Bytes out) {
|
||||
keySerializer.writeMarshallable(key, out);
|
||||
}
|
||||
|
||||
private void reflectTransactionId(Bytes in, Bytes out) {
|
||||
out.writeLong(in.readLong());
|
||||
}
|
||||
|
||||
private void writeValue(V value, final Bytes out) {
|
||||
valueSerializer.writeMarshallable(value, out);
|
||||
}
|
||||
|
||||
private K readKey(Bytes in) {
|
||||
return keySerializer.readMarshallable(in);
|
||||
}
|
||||
|
||||
private V readValue(Bytes in) {
|
||||
return valueSerializer.readMarshallable(in);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user