Skip to content

Commit

Permalink
Remove length field in ReplicatedTransation codec
Browse files Browse the repository at this point in the history
  • Loading branch information
RagnarW authored and martinfurmanski committed Sep 10, 2018
1 parent c560a45 commit 24351d3
Show file tree
Hide file tree
Showing 5 changed files with 5 additions and 25 deletions.
Expand Up @@ -83,11 +83,6 @@ public ByteBuf readChunk( ByteBufAllocator allocator ) throws Exception
{ {
// Ensure that the written buffers does not overflow the allocators chunk size. // Ensure that the written buffers does not overflow the allocators chunk size.
channel = new ChunkingNetworkChannel( allocator, CHUNK_SIZE, chunks ); channel = new ChunkingNetworkChannel( allocator, CHUNK_SIZE, chunks );
/*
Unknown length. The reason for sending this int is to avoid conflicts with Raft V1.
This way, the serialized result of this object is identical to a serialized byte array. Which is the only type in Raft V1.
*/
channel.putInt( -1 );
} }


// write to chunks if empty and there is more to write // write to chunks if empty and there is more to write
Expand Down
Expand Up @@ -43,13 +43,9 @@ private ReplicatedTransactionSerializer()
{ {
} }


public static ReplicatedTransaction unmarshal( ByteBuf byteBuf ) public static ReplicatedTransaction decode( ByteBuf byteBuf )
{ {
int length = byteBuf.readInt(); int length = byteBuf.readableBytes();
if ( length == -1 )
{
length = byteBuf.readableBytes();
}
byte[] bytes = new byte[length]; byte[] bytes = new byte[length];
byteBuf.readBytes( bytes ); byteBuf.readBytes( bytes );
return ReplicatedTransaction.from( bytes ); return ReplicatedTransaction.from( bytes );
Expand Down
Expand Up @@ -62,11 +62,6 @@ private int available()
return content.length - pos; return content.length - pos;
} }


private boolean isFirst()
{
return pos == 0;
}

@Override @Override
public boolean isEndOfInput() public boolean isEndOfInput()
{ {
Expand All @@ -92,16 +87,10 @@ public ByteBuf readChunk( ByteBufAllocator allocator )
{ {
return null; return null;
} }
int extraBytes = isFirst() ? Integer.BYTES : 0; int toWrite = Math.min( available(), chunkSize );
int toWrite = Math.min( available() + extraBytes, chunkSize );
ByteBuf buffer = allocator.buffer( toWrite ); ByteBuf buffer = allocator.buffer( toWrite );
try try
{ {
if ( isFirst() )
{
buffer.writeInt( content.length );
toWrite -= extraBytes;
}
buffer.writeBytes( content, pos, toWrite ); buffer.writeBytes( content, pos, toWrite );
pos += toWrite; pos += toWrite;
return buffer; return buffer;
Expand Down
Expand Up @@ -78,7 +78,7 @@ private ContentBuilder<ReplicatedContent> unmarshal( byte contentType, ByteBuf b
{ {
case TX_CONTENT_TYPE: case TX_CONTENT_TYPE:
{ {
return ContentBuilder.finished( ReplicatedTransactionSerializer.unmarshal( buffer ) ); return ContentBuilder.finished( ReplicatedTransactionSerializer.decode( buffer ) );
} }
default: default:
return unmarshal( contentType, new NetworkReadableClosableChannelNetty4( buffer ) ); return unmarshal( contentType, new NetworkReadableClosableChannelNetty4( buffer ) );
Expand Down
Expand Up @@ -60,7 +60,7 @@ public void shouldDecodeAndUnmarshalSameBytes() throws IOException
TransactionRepresentationReplicatedTransaction replicatedTransaction = ReplicatedTransaction.from( expectedTx ); TransactionRepresentationReplicatedTransaction replicatedTransaction = ReplicatedTransaction.from( expectedTx );
replicatedTransaction.marshal( new BoundedNetworkWritableChannel( buffer ) ); replicatedTransaction.marshal( new BoundedNetworkWritableChannel( buffer ) );


ReplicatedTransaction decoded = ReplicatedTransactionSerializer.unmarshal( buffer ); ReplicatedTransaction decoded = ReplicatedTransactionSerializer.decode( buffer );
buffer.readerIndex( 0 ); buffer.readerIndex( 0 );
ReplicatedTransaction unmarshaled = ReplicatedTransactionSerializer.unmarshal( new NetworkReadableClosableChannelNetty4( buffer ) ); ReplicatedTransaction unmarshaled = ReplicatedTransactionSerializer.unmarshal( new NetworkReadableClosableChannelNetty4( buffer ) );


Expand Down

0 comments on commit 24351d3

Please sign in to comment.