}
@Override
public void put(ByteArray key, Versioned<byte[]> value, byte[] transform)
throws VoldemortException {
RestResponse response = null;
try {
byte[] payload = value.getValue();
// Create the REST request with this byte array
String base64Key = RestUtils.encodeVoldemortKey(key.get());
RestRequestBuilder rb = new RestRequestBuilder(new URI(this.restBootstrapURL + "/"
+ getName() + "/" + base64Key));
// Create a HTTP POST request
rb.setMethod(POST);
rb.setEntity(payload);
rb.setHeader(CONTENT_TYPE, "binary");
rb.setHeader(CONTENT_LENGTH, "" + payload.length);
String timeoutStr = Long.toString(this.config.getTimeoutConfig()
.getOperationTimeout(VoldemortOpCode.PUT_OP_CODE));
rb.setHeader(RestMessageHeaders.X_VOLD_REQUEST_TIMEOUT_MS, timeoutStr);
rb.setHeader(RestMessageHeaders.X_VOLD_REQUEST_ORIGIN_TIME_MS,
String.valueOf(System.currentTimeMillis()));
if(this.routingTypeCode != null) {
rb.setHeader(RestMessageHeaders.X_VOLD_ROUTING_TYPE_CODE, this.routingTypeCode);
}
if(this.zoneId != INVALID_ZONE_ID) {
rb.setHeader(RestMessageHeaders.X_VOLD_ZONE_ID, String.valueOf(this.zoneId));
}
// Serialize the Vector clock
VectorClock vc = (VectorClock) value.getVersion();
// If the given Vector clock is empty, we'll let the receiver of
// this request fetch the existing vector clock and increment before
// doing the put.
if(vc != null) {
String serializedVC = null;
if(!vc.getEntries().isEmpty()) {
serializedVC = RestUtils.getSerializedVectorClock(vc);
}
if(serializedVC != null && serializedVC.length() > 0) {
rb.setHeader(RestMessageHeaders.X_VOLD_VECTOR_CLOCK, serializedVC);
}
}
RestRequest request = rb.build();
Future<RestResponse> f = client.restRequest(request);
// This will block
response = f.get();
String serializedUpdatedVC = response.getHeader(RestMessageHeaders.X_VOLD_VECTOR_CLOCK);
if(serializedUpdatedVC == null || serializedUpdatedVC.length() == 0) {
if(logger.isDebugEnabled()) {
logger.debug("Received empty vector clock in the response");
}
} else {
VectorClock updatedVC = RestUtils.deserializeVectorClock(serializedUpdatedVC);
VectorClock originalVC = (VectorClock) value.getVersion();
originalVC.copyFromVectorClock(updatedVC);
}
final ByteString entity = response.getEntity();
if(entity == null) {
if(logger.isDebugEnabled()) {
logger.debug("Empty response !");
}
}