use of voldemort.common.nio.ByteBufferBackedInputStream in project voldemort by voldemort.
the class AsyncRequestHandler method initializeStreams.
@Override
protected void initializeStreams(int socketBufferSize, CommBufferSizeStats commBufferStats) {
ByteBufferContainer inputBufferContainer, outputBufferContainer;
inputBufferContainer = new ByteBufferContainer(socketBufferSize, resizeThreshold, commBufferStats.getCommReadBufferSizeTracker());
if (requestHandlerFactory.shareReadWriteBuffer()) {
outputBufferContainer = inputBufferContainer;
} else {
outputBufferContainer = new ByteBufferContainer(socketBufferSize, resizeThreshold, commBufferStats.getCommWriteBufferSizeTracker());
}
this.inputStream = new ByteBufferBackedInputStream(inputBufferContainer);
this.outputStream = new ByteBufferBackedOutputStream(outputBufferContainer);
}
use of voldemort.common.nio.ByteBufferBackedInputStream in project voldemort by voldemort.
the class AdminServiceRequestHandler method isCompleteRequest.
/**
* This method is used by non-blocking code to determine if the give buffer
* represents a complete request. Because the non-blocking code can by
* definition not just block waiting for more data, it's possible to get
* partial reads, and this identifies that case.
*
* @param buffer Buffer to check; the buffer is reset to position 0 before
* calling this method and the caller must reset it after the call
* returns
* @return True if the buffer holds a complete request, false otherwise
*/
@Override
public boolean isCompleteRequest(ByteBuffer buffer) {
DataInputStream inputStream = new DataInputStream(new ByteBufferBackedInputStream(buffer));
try {
int dataSize = inputStream.readInt();
if (logger.isTraceEnabled())
logger.trace("In isCompleteRequest, dataSize: " + dataSize + ", buffer position: " + buffer.position());
if (dataSize == -1)
return true;
// Here we skip over the data (without reading it in) and
// move our position to just past it.
buffer.position(buffer.position() + dataSize);
return true;
} catch (Exception e) {
// throws an InvalidArgumentException.
if (logger.isTraceEnabled())
logger.trace("In isCompleteRequest, probable partial read occurred: " + e);
return false;
}
}
use of voldemort.common.nio.ByteBufferBackedInputStream in project voldemort by voldemort.
the class VoldemortNativeRequestHandler method isCompleteRequest.
/**
* This is pretty ugly. We end up mimicking the request logic here, so this
* needs to stay in sync with handleRequest.
*/
@Override
public boolean isCompleteRequest(final ByteBuffer buffer) throws VoldemortException {
DataInputStream inputStream = new DataInputStream(new ByteBufferBackedInputStream(buffer));
try {
byte opCode = inputStream.readByte();
// Store Name
inputStream.readUTF();
// Store routing type
getRoutingType(inputStream);
switch(opCode) {
case VoldemortOpCode.GET_VERSION_OP_CODE:
if (!GetVersionRequestHandler.isCompleteRequest(inputStream, buffer))
return false;
break;
case VoldemortOpCode.GET_OP_CODE:
if (!GetRequestHandler.isCompleteRequest(inputStream, buffer, protocolVersion))
return false;
break;
case VoldemortOpCode.GET_ALL_OP_CODE:
if (!GetAllRequestHandler.isCompleteRequest(inputStream, buffer, protocolVersion))
return false;
break;
case VoldemortOpCode.PUT_OP_CODE:
{
if (!PutRequestHandler.isCompleteRequest(inputStream, buffer, protocolVersion))
return false;
break;
}
case VoldemortOpCode.DELETE_OP_CODE:
{
if (!DeleteRequestHandler.isCompleteRequest(inputStream, buffer))
return false;
break;
}
default:
throw new VoldemortException(" Unrecognized Voldemort OpCode " + opCode);
}
// data, there is something wrong.
if (buffer.hasRemaining()) {
logger.info("Probably a client bug, Discarding additional bytes in isCompleteRequest. Opcode: " + opCode + ", remaining bytes: " + buffer.remaining());
}
return true;
} catch (IOException e) {
// throws an InvalidArgumentException.
if (logger.isDebugEnabled())
logger.debug("Probable partial read occurred causing exception", e);
return false;
}
}
use of voldemort.common.nio.ByteBufferBackedInputStream in project voldemort by voldemort.
the class VoldemortNativeClientRequestFormat method isCompleteGetResponse.
public boolean isCompleteGetResponse(ByteBuffer buffer) {
try {
DataInputStream inputStream = new DataInputStream(new ByteBufferBackedInputStream(buffer));
checkException(inputStream);
if (!skipResults(inputStream, buffer)) {
return false;
}
} catch (VoldemortException e) {
} catch (IOException e) {
return false;
}
return !buffer.hasRemaining();
}
use of voldemort.common.nio.ByteBufferBackedInputStream in project voldemort by voldemort.
the class VoldemortNativeClientRequestFormat method isCompleteGetAllResponse.
@Override
public boolean isCompleteGetAllResponse(ByteBuffer buffer) {
try {
DataInputStream inputStream = new DataInputStream(new ByteBufferBackedInputStream(buffer));
checkException(inputStream);
int numResults = inputStream.readInt();
for (int i = 0; i < numResults; i++) {
int keySize = inputStream.readInt();
if (!ByteUtils.skipByteArray(buffer, keySize)) {
return false;
}
if (!skipResults(inputStream, buffer)) {
return false;
}
}
} catch (VoldemortException e) {
} catch (IOException e) {
return false;
}
return !buffer.hasRemaining();
}
Aggregations