throws IOException,
ClassNotFoundException
{
Preconditions.checkArgument ((message.specification instanceof KeyValueMessage) || (message.specification instanceof MemcachedMessage));
byte[] data;
CompletionToken token;
String key;
int exp;
IResult<Boolean> resultStore;
DriverOperationFinishedHandler callback;
final eu.mosaic_cloud.platform.interop.common.kv.KeyValueMessage messageData;
final MemcachedDriver driver = super.getDriver (MemcachedDriver.class);
final String mssgPrefix = "MemcachedStub - Received request for ";
if (message.specification instanceof KeyValueMessage) {
// NOTE: handle set with exp
boolean handle = false;
final KeyValueMessage kvMessage = (KeyValueMessage) message.specification;
if (kvMessage == KeyValueMessage.SET_REQUEST) {
final KeyValuePayloads.SetRequest setRequest = (SetRequest) message.payload;
if (setRequest.hasExpTime ()) {
token = setRequest.getToken ();
key = setRequest.getKey ();
MemcachedStub.logger.trace (mssgPrefix + " SET key: " + key + " - request id: " + token.getMessageId () + " client id: " + token.getClientId ());
exp = setRequest.getExpTime ();
data = setRequest.getValue ().toByteArray ();
messageData = new eu.mosaic_cloud.platform.interop.common.kv.KeyValueMessage (key, data, setRequest.getEnvelope ().getContentEncoding (), setRequest.getEnvelope ().getContentType ());
callback = new DriverOperationFinishedHandler (token, session, MemcachedDriver.class, MemcachedResponseTransmitter.class);
resultStore = driver.invokeSetOperation (token.getClientId (), messageData, exp, callback);
callback.setDetails (KeyValueOperations.SET, resultStore);
handle = true;
}
} else if (kvMessage == KeyValueMessage.GET_REQUEST) {
final KeyValuePayloads.GetRequest getRequest = (GetRequest) message.payload;
if (getRequest.getKeyCount () > 1) {
token = getRequest.getToken ();
MemcachedStub.logger.trace (mssgPrefix + "GET_BULK " + " - request id: " + token.getMessageId () + " client id: " + token.getClientId ());
callback = new DriverOperationFinishedHandler (token, session, MemcachedDriver.class, MemcachedResponseTransmitter.class);
final EncodingMetadata expectedEncoding = new EncodingMetadata (getRequest.getEnvelope ().getContentType (), getRequest.getEnvelope ().getContentEncoding ());
final IResult<Map<String, eu.mosaic_cloud.platform.interop.common.kv.KeyValueMessage>> resultGet = driver.invokeGetBulkOperation (token.getClientId (), getRequest.getKeyList (), expectedEncoding, callback);
callback.setDetails (KeyValueOperations.GET_BULK, resultGet);
handle = true;
}
}
if (!handle) {
this.handleKVOperation (message, session, driver, MemcachedResponseTransmitter.class);
}
return;
}
final MemcachedMessage mcMessage = (MemcachedMessage) message.specification;
switch (mcMessage) {
case ADD_REQUEST :
final MemcachedPayloads.AddRequest addRequest = (AddRequest) message.payload;
token = addRequest.getToken ();
key = addRequest.getKey ();
MemcachedStub.logger.trace (mssgPrefix + mcMessage.toString () + " key: " + key + " - request id: " + token.getMessageId () + " client id: " + token.getClientId ());
exp = addRequest.getExpTime ();
data = addRequest.getValue ().toByteArray ();
messageData = new eu.mosaic_cloud.platform.interop.common.kv.KeyValueMessage (key, data, addRequest.getEnvelope ().getContentEncoding (), addRequest.getEnvelope ().getContentType ());
callback = new DriverOperationFinishedHandler (token, session, MemcachedDriver.class, MemcachedResponseTransmitter.class);
resultStore = driver.invokeAddOperation (token.getClientId (), messageData, exp, callback);
callback.setDetails (KeyValueOperations.ADD, resultStore);
break;
case APPEND_REQUEST :
final MemcachedPayloads.AppendRequest appendRequest = (AppendRequest) message.payload;
token = appendRequest.getToken ();
key = appendRequest.getKey ();
MemcachedStub.logger.trace (mssgPrefix + mcMessage.toString () + " key: " + key + " - request id: " + token.getMessageId () + " client id: " + token.getClientId ());
data = appendRequest.getValue ().toByteArray ();
messageData = new eu.mosaic_cloud.platform.interop.common.kv.KeyValueMessage (key, data, appendRequest.getEnvelope ().getContentEncoding (), appendRequest.getEnvelope ().getContentType ());
callback = new DriverOperationFinishedHandler (token, session, MemcachedDriver.class, MemcachedResponseTransmitter.class);
resultStore = driver.invokeAppendOperation (token.getClientId (), messageData, callback);
callback.setDetails (KeyValueOperations.APPEND, resultStore);
break;
case PREPEND_REQUEST :
final MemcachedPayloads.PrependRequest prependRequest = (PrependRequest) message.payload;
token = prependRequest.getToken ();
key = prependRequest.getKey ();
MemcachedStub.logger.trace (mssgPrefix + mcMessage.toString () + " key: " + key + " - request id: " + token.getMessageId () + " client id: " + token.getClientId ());
data = prependRequest.getValue ().toByteArray ();
messageData = new eu.mosaic_cloud.platform.interop.common.kv.KeyValueMessage (key, data, prependRequest.getEnvelope ().getContentEncoding (), prependRequest.getEnvelope ().getContentType ());
callback = new DriverOperationFinishedHandler (token, session, MemcachedDriver.class, MemcachedResponseTransmitter.class);
resultStore = driver.invokePrependOperation (token.getClientId (), messageData, callback);
callback.setDetails (KeyValueOperations.PREPEND, resultStore);
break;
case REPLACE_REQUEST :
final MemcachedPayloads.ReplaceRequest replaceRequest = (ReplaceRequest) message.payload;
token = replaceRequest.getToken ();
key = replaceRequest.getKey ();
MemcachedStub.logger.trace (mssgPrefix + mcMessage.toString () + " key: " + key + " - request id: " + token.getMessageId () + " client id: " + token.getClientId ());
exp = replaceRequest.getExpTime ();
data = replaceRequest.getValue ().toByteArray ();
messageData = new eu.mosaic_cloud.platform.interop.common.kv.KeyValueMessage (key, data, replaceRequest.getEnvelope ().getContentEncoding (), replaceRequest.getEnvelope ().getContentType ());
callback = new DriverOperationFinishedHandler (token, session, MemcachedDriver.class, MemcachedResponseTransmitter.class);
resultStore = driver.invokeReplaceOperation (token.getClientId (), messageData, exp, callback);
callback.setDetails (KeyValueOperations.REPLACE, resultStore);
break;
case CAS_REQUEST :
final MemcachedPayloads.CasRequest casRequest = (CasRequest) message.payload;
token = casRequest.getToken ();
key = casRequest.getKey ();
MemcachedStub.logger.trace (mssgPrefix + mcMessage.toString () + " key: " + key + " - request id: " + token.getMessageId () + " client id: " + token.getClientId ());
data = casRequest.getValue ().toByteArray ();
messageData = new eu.mosaic_cloud.platform.interop.common.kv.KeyValueMessage (key, data, casRequest.getEnvelope ().getContentEncoding (), casRequest.getEnvelope ().getContentType ());
callback = new DriverOperationFinishedHandler (token, session, MemcachedDriver.class, MemcachedResponseTransmitter.class);
resultStore = driver.invokeCASOperation (token.getClientId (), messageData, callback);
callback.setDetails (KeyValueOperations.CAS, resultStore);
break;
default:
break;
}