improved chunking in putAll()

This commit is contained in:
Rob Austin
2015-03-13 17:20:58 +00:00
parent e43765cf44
commit a89f31eae6
2 changed files with 32 additions and 14 deletions
@@ -277,6 +277,7 @@ class ClientWiredStatelessChronicleMap<K, V> implements ChronicleMap<K, V>, Clon
Set<? extends Map.Entry<? extends K, ? extends V>> entries = map.entrySet();
Iterator<? extends Map.Entry<? extends K, ? extends V>> iterator = entries.iterator();
OUTER:
while (iterator.hasNext()) {
hub.outBytesLock().lock();
try {
@@ -295,17 +296,26 @@ class ClientWiredStatelessChronicleMap<K, V> implements ChronicleMap<K, V>, Clon
wire.write(Fields.CHANNEL_ID).int16(channelID);
writeField(Fields.METHOD_NAME, "PUT_ALL");
Map.Entry<? extends K, ? extends V> e = iterator.next();
hub.outWire().write(Fields.HAS_NEXT).bool(iterator.hasNext());
writeField(Fields.ARG_1, e.getKey());
writeField(Fields.ARG_2, e.getValue());
hub.writeSocket(hub.outWire());
while (iterator.hasNext()) {
Map.Entry<? extends K, ? extends V> e = iterator.next();
hub.outWire().write(Fields.HAS_NEXT).bool(iterator.hasNext());
writeField(Fields.ARG_1, e.getKey());
writeField(Fields.ARG_2, e.getValue());
if (wire.bytes().remaining() < wire.bytes().capacity() * 0.5)
break OUTER;
}
} finally {
hub.writeSocket(hub.outWire());
hub.outBytesLock().unlock();
}
@@ -448,12 +448,18 @@ class MapWireHandler<K, V> implements WireHandler, Consumer<WireHandlers> {
@Override
public void run() {
boolean hasNext = inWire.read(HAS_NEXT).bool();
collectData.put(toBytes(ARG_1), toBytes(ARG_2));
if (!hasNext) {
// the old code assumed that all the data would fit into a single buffer
// this assumption is invalid
if (!collectData.isEmpty()) {
// the old code assumed that ALL of the entries would fit into a single buffer
// this assumption is invalid
boolean hasNext;
for(;;) {
hasNext = inWire.read(HAS_NEXT).bool();
collectData.put(toBytes(ARG_1), toBytes(ARG_2));
if (!hasNext) {
incompleteWork.remove(transactionId);
@@ -463,8 +469,10 @@ class MapWireHandler<K, V> implements WireHandler, Consumer<WireHandlers> {
bytesMap.delegate.putAll((Map) collectData);
return null;
});
return;
}
if (inWire.bytes().remaining() == 0)
return;
}
}
};