From 22d860f610b1da5b3ef213a26faf66a89714ac5a Mon Sep 17 00:00:00 2001 From: Rob Austin Date: Sat, 7 Mar 2015 14:33:08 +0000 Subject: [PATCH] added some code for putAll --- .../map/StatelessWiredConnector.java | 114 +++++++++++------- 1 file changed, 72 insertions(+), 42 deletions(-) diff --git a/src/main/java/net/openhft/chronicle/map/StatelessWiredConnector.java b/src/main/java/net/openhft/chronicle/map/StatelessWiredConnector.java index 0f3fb58dd..99715bb02 100644 --- a/src/main/java/net/openhft/chronicle/map/StatelessWiredConnector.java +++ b/src/main/java/net/openhft/chronicle/map/StatelessWiredConnector.java @@ -78,10 +78,11 @@ class StatelessWiredConnector channelList; - + private Runnable runnable = null; private byte remoteIdentifier; private byte localIdentifer; + public StatelessWiredConnector(@Nullable List channelList, byte localIdentifier) { if (channelList == null) { @@ -205,7 +206,7 @@ class StatelessWiredConnector "TRANSACTION_ID").int64(); @@ -213,18 +214,25 @@ class StatelessWiredConnector "CHANNEL_ID").int16(); inWire.read(() -> "METHOD_NAME").text(methodName); - if ("PUT_WITHOUT_ACC".contentEquals(methodName)) - - return writeVoid(bytesMap -> { + if ("PUT_WITHOUT_ACC".contentEquals(methodName)) { + writeVoid(bytesMap -> { final net.openhft.lang.io.Bytes reader = toReader(inWire, "ARG_1", "ARG_2"); bytesMap.put(reader, reader, timestamp, identifier()); }); + return; + } // for the length long markStart = outWire.bytes().position(); outWire.bytes().skip(4); + // process the messages for remaining chunks, + if (runnable != null) { + runnable.run(); + return; + } + try { // write the transaction id @@ -233,91 +241,115 @@ class StatelessWiredConnector "RESULT").int8(); - if ("LONG_SIZE".contentEquals(methodName)) - return write(b -> outWire.write(() -> "RESULT").int64(b.longSize())); + if ("LONG_SIZE".contentEquals(methodName)) { + write(b -> outWire.write(() -> "RESULT").int64(b.longSize())); + return; + } - else if ("IS_EMPTY".contentEquals(methodName)) - return write(b -> outWire.write(() -> "RESULT").bool(b.isEmpty())); + + if ("IS_EMPTY".contentEquals(methodName)) + write(b -> outWire.write(() -> "RESULT").bool(b.isEmpty())); else if ("CONTAINS_KEY".contentEquals(methodName)) - return write(b -> outWire.write(() -> "RESULT"). + write(b -> outWire.write(() -> "RESULT"). bool(b.containsKey(toReader(inWire, "ARG_1")))); else if ("CONTAINS_VALUE".contentEquals(methodName)) - return write(b -> outWire.write(() -> "RESULT"). + write(b -> outWire.write(() -> "RESULT"). bool(b.containsKey(toReader(inWire, "ARG_1")))); else if ("GET".contentEquals(methodName)) - return writeValue(b -> b.get(toReader(inWire, "ARG_1"))); + writeValue(b -> b.get(toReader(inWire, "ARG_1"))); - if ("PUT".contentEquals(methodName)) - return writeValue(bytesMap -> { + else if ("PUT".contentEquals(methodName)) + writeValue(bytesMap -> { final net.openhft.lang.io.Bytes reader = toReader(inWire, "ARG_1", "ARG_2"); bytesMap.put(reader, reader, timestamp, identifier()); }); else if ("REMOVE".contentEquals(methodName)) - return writeValue(b -> b.remove(toReader(inWire, "ARG_1"))); + writeValue(b -> b.remove(toReader(inWire, "ARG_1"))); else if ("CLEAR".contentEquals(methodName)) - return writeVoid(BytesChronicleMap::clear); + writeVoid(BytesChronicleMap::clear); else if ("REPLACE".contentEquals(methodName)) - return writeValue(bytesMap -> { + writeValue(bytesMap -> { final net.openhft.lang.io.Bytes reader = toReader(inWire, "ARG_1", "ARG_2"); bytesMap.replace(reader, reader); }); else if ("REPLACE_WITH_OLD_AND_NEW_VALUE".contentEquals(methodName)) - return writeValue(bytesMap -> { + writeValue(bytesMap -> { final net.openhft.lang.io.Bytes reader = toReader(inWire, "ARG_1", "ARG_2", "ARG_3"); bytesMap.replace(reader, reader, reader); }); else if ("PUT_IF_ABSENT".contentEquals(methodName)) - return writeValue(bytesMap -> { + writeValue(bytesMap -> { final net.openhft.lang.io.Bytes reader = toReader(inWire, "ARG_1", "ARG_2"); bytesMap.putIfAbsent(reader, reader, timestamp, identifier()); }); else if ("REMOVE_WITH_VALUE".contentEquals(methodName)) - return writeValue(bytesMap -> { + writeValue(bytesMap -> { final net.openhft.lang.io.Bytes reader = toReader(inWire, "ARG_1", "ARG_2"); bytesMap.remove(reader, reader, timestamp, identifier()); }); else if ("TO_STRING".contentEquals(methodName)) - return write(b -> outWire.write(() -> "RESULT").text(b.toString())); + write(b -> outWire.write(() -> "RESULT").text(b.toString())); else if ("APPLICATION_VERSION".contentEquals(methodName)) - return write(b -> outWire.write(() -> "RESULT").text(applicationVersion())); + write(b -> outWire.write(() -> "RESULT").text(applicationVersion())); else if ("PERSISTED_DATA_VERSION".contentEquals(methodName)) - return write(b -> outWire.write(() -> "RESULT").text(persistedDataVersion())); + write(b -> outWire.write(() -> "RESULT").text(persistedDataVersion())); else if ("HASH_CODE".contentEquals(methodName)) - return write(b -> outWire.write(() -> "RESULT").int32(b.hashCode())); + write(b -> outWire.write(() -> "RESULT").int32(b.hashCode())); + else if ("PUT_ALL".contentEquals(methodName)) + putAll(); + else + throw new IllegalStateException("unsupported event=" + methodName); - // todo - // if ("PUT_ALL".contentEquals(methodName)) // else if ("MAP_FOR_KEY".contentEquals(methodName) // else if ("PUT_MAPPED".contentEquals(methodName)) // else if ("VALUE_BUILDER".contentEquals(methodName)) - } catch (Exception e) { - LOG.error("", e); } finally { int len = (int) (outWire.bytes().position() - markStart); outWire.bytes().writeInt(markStart, len); } - throw new IllegalStateException("unsupported event=" + methodName); + } + + private void putAll() { + + runnable = () -> { + if (!inWire.read(() -> "HAS_MORE_CHUNKS").bool()) + // finished reading all the chunks + runnable = null; + + BytesChronicleMap bytesMap = bytesMap(StatelessWiredConnector.this.channelId); + + // todo handle when bytesMap is null + + bytesMap.output = outMessageAdapter.outBuffer; + final net.openhft.lang.io.Bytes reader = toReader(inWire, "ARG_1", "ARG_2"); + try { + bytesMap.put(reader, reader, timestamp, identifier()); + } catch (Exception e) { + // todo send back exceptions + } + + }; } private byte identifier() { @@ -433,13 +465,13 @@ class StatelessWiredConnector process) { + private void writeValue(@NotNull Consumer process) { final BytesChronicleMap bytesMap = bytesMap(channelId); if (bytesMap == null) { LOG.error("no map for channelId=" + channelId + " can be found."); - return null; + return; } inLanByteBuffer.clear(); @@ -457,24 +489,23 @@ class StatelessWiredConnector "IS_EXCEPTION").bool(true); outWire.write(() -> "EXCEPTION").text(toString(e)); LOG.error("", e); - return null; + return; } outMessageAdapter.accept(outWire); nofityDataWritten(); - return null; + } @SuppressWarnings("SameReturnValue") - @Nullable - private Work write(@NotNull Consumer c) { + private void write(@NotNull Consumer c) { final BytesChronicleMap bytesMap = bytesMap(channelId); if (bytesMap == null) { LOG.error("no map for channelId=" + channelId + " can be found."); - return null; + return; } bytesMap.output = null; @@ -491,11 +522,11 @@ class StatelessWiredConnector "IS_EXCEPTION").bool(true); outWire.write(() -> "EXCEPTION").text(toString(e)); LOG.error("", e); - return null; + return; } nofityDataWritten(); - return null; + } /** @@ -508,14 +539,14 @@ class StatelessWiredConnector process) { + private void writeVoid(@NotNull Consumer process) { // skip 4 bytes where we will write the size final BytesChronicleMap bytesMap = bytesMap(channelId); if (bytesMap == null) { LOG.error("no map for channelId=" + channelId + " can be found."); - return null; + return; } bytesMap.output = null; @@ -523,11 +554,10 @@ class StatelessWiredConnector