extends KeyValueResponseTransmitter
{
@Override
protected void packAndSend (final Session session, final CompletionToken token, final KeyValueOperations operation, final Object result, final boolean isError)
{
Message message;
this.logger.trace ("MemcachedTransmitter: send response for " + operation + " request " + token.getMessageId () + " client id " + token.getClientId ());
if (isError) {
// NOTE: create error message
final Builder errorPayload = IdlCommon.Error.newBuilder ();
errorPayload.setToken (token);
errorPayload.setErrorMessage (result.toString ());
message = new Message (KeyValueMessage.ERROR, errorPayload.build ());
} else {
switch (operation) {
case ADD :
case APPEND :
case REPLACE :
case PREPEND :
case CAS :
final boolean success = (Boolean) result;
if (success) {
final Ok.Builder okPayload = IdlCommon.Ok.newBuilder ();
okPayload.setToken (token);
message = new Message (KeyValueMessage.OK, okPayload.build ());
} else {
final NotOk.Builder nokPayload = IdlCommon.NotOk.newBuilder ();
nokPayload.setToken (token);
message = new Message (KeyValueMessage.NOK, nokPayload.build ());
}
break;
case GET_BULK :
final GetReply.Builder getPayload = KeyValuePayloads.GetReply.newBuilder ();
getPayload.setToken (token);
@SuppressWarnings ("unchecked") final Map<String, eu.mosaic_cloud.platform.interop.common.kv.KeyValueMessage> resMap = (Map<String, eu.mosaic_cloud.platform.interop.common.kv.KeyValueMessage>) result;
final List<KVEntry> getResults = new ArrayList<KVEntry> ();
for (final Entry<String, eu.mosaic_cloud.platform.interop.common.kv.KeyValueMessage> entry : resMap.entrySet ()) {
final KVEntry.Builder kvEntry = KeyValuePayloads.KVEntry.newBuilder ();
kvEntry.setKey (entry.getKey ());
final IdlCommon.Envelope.Builder envelope = IdlCommon.Envelope.newBuilder ();
if (null != entry.getValue ().getContentEncoding ())
envelope.setContentEncoding (entry.getValue ().getContentEncoding ());
else
envelope.setContentEncoding ("");
if (null != entry.getValue ().getContentType ())
envelope.setContentType (entry.getValue ().getContentType ());
else
envelope.setContentType ("");
kvEntry.setEnvelope (envelope.build ());
if (entry.getValue ().getData () == null) {
kvEntry.setValue (ByteString.EMPTY);
} else {
kvEntry.setValue (ByteString.copyFrom (entry.getValue ().getData ()));
}
getResults.add (kvEntry.build ());
}
getPayload.addAllResults (getResults);
message = new Message (KeyValueMessage.GET_REPLY, getPayload.build ());
break;
default:
message = super.buildKeyValueResponse (operation, token, result);
break;
}