public static int alignedFramedLength(int msgLength) { return alignedLength(framedLength(msgLength)); }
public static int alignedFramedLength(int msgLength) { return alignedLength(framedLength(msgLength)); }
/** * Add a new fragment to the batch. * * @param length the length of the fragment * @param streamId the stream id of the fragment * @return the position of the fragment * @throws IllegalArgumentException if the given length is greater than the remaining capacity. In * this case, you should try with smaller length, or abort the whole batch. */ @SuppressWarnings("restriction") public long nextFragment(int length, int streamId) { currentOffset = nextOffset; final int framedLength = framedLength(length); nextOffset += alignedLength(framedLength); // ensure that there is enough capacity for padding message, or less than frame alignment which // omits the padding message final int remainingCapacity = buffer.capacity() - nextOffset; if (remainingCapacity < 0 || (FRAME_ALIGNMENT <= remainingCapacity && remainingCapacity < HEADER_LENGTH)) { throw new IllegalArgumentException( String.format(ERROR_MESSAGE, currentOffset, length, buffer.capacity())); } // set negative length => uncommitted fragment buffer.putIntOrdered(lengthOffset(currentOffset), -framedLength); UNSAFE.storeFence(); buffer.putShort(typeOffset(currentOffset), TYPE_MESSAGE); buffer.putInt(streamIdOffset(currentOffset), streamId); return position(partitionId, partitionOffset + nextOffset); }
/** * Add a new fragment to the batch. * * @param length the length of the fragment * @param streamId the stream id of the fragment * @return the position of the fragment * @throws IllegalArgumentException if the given length is greater than the remaining capacity. In * this case, you should try with smaller length, or abort the whole batch. */ @SuppressWarnings("restriction") public long nextFragment(int length, int streamId) { currentOffset = nextOffset; final int framedLength = framedLength(length); nextOffset += alignedLength(framedLength); // ensure that there is enough capacity for padding message, or less than frame alignment which // omits the padding message final int remainingCapacity = buffer.capacity() - nextOffset; if (remainingCapacity < 0 || (FRAME_ALIGNMENT <= remainingCapacity && remainingCapacity < HEADER_LENGTH)) { throw new IllegalArgumentException( String.format(ERROR_MESSAGE, currentOffset, length, buffer.capacity())); } // set negative length => uncommitted fragment buffer.putIntOrdered(lengthOffset(currentOffset), -framedLength); UNSAFE.storeFence(); buffer.putShort(typeOffset(currentOffset), TYPE_MESSAGE); buffer.putInt(streamIdOffset(currentOffset), streamId); return position(partitionId, partitionOffset + nextOffset); }
@SuppressWarnings("restriction") public int claim( final LogBufferPartition partition, final int activePartitionId, final ClaimedFragment claim, final int length, final int streamId, Runnable onComplete) { final int partitionSize = partition.getPartitionSize(); final int framedMessageLength = framedLength(length); final int alignedFrameLength = alignedLength(framedMessageLength); // move the tail of the partition final int frameOffset = partition.getAndAddTail(alignedFrameLength); int newTail = frameOffset + alignedFrameLength; if (newTail <= (partitionSize - HEADER_LENGTH)) { final UnsafeBuffer buffer = partition.getDataBuffer(); // write negative length field buffer.putIntOrdered(lengthOffset(frameOffset), -framedMessageLength); UNSAFE.storeFence(); buffer.putShort(typeOffset(frameOffset), TYPE_MESSAGE); buffer.putInt(streamIdOffset(frameOffset), streamId); claim.wrap(buffer, frameOffset, framedMessageLength, onComplete); // Do not commit the message } else { newTail = onEndOfPartition(partition, frameOffset); } return newTail; }
@SuppressWarnings("restriction") public int claim( final LogBufferPartition partition, final int activePartitionId, final ClaimedFragment claim, final int length, final int streamId, Runnable onComplete) { final int partitionSize = partition.getPartitionSize(); final int framedMessageLength = framedLength(length); final int alignedFrameLength = alignedLength(framedMessageLength); // move the tail of the partition final int frameOffset = partition.getAndAddTail(alignedFrameLength); int newTail = frameOffset + alignedFrameLength; if (newTail <= (partitionSize - HEADER_LENGTH)) { final UnsafeBuffer buffer = partition.getDataBuffer(); // write negative length field buffer.putIntOrdered(lengthOffset(frameOffset), -framedMessageLength); UNSAFE.storeFence(); buffer.putShort(typeOffset(frameOffset), TYPE_MESSAGE); buffer.putInt(streamIdOffset(frameOffset), streamId); claim.wrap(buffer, frameOffset, framedMessageLength, onComplete); // Do not commit the message } else { newTail = onEndOfPartition(partition, frameOffset); } return newTail; }
@SuppressWarnings("restriction") public int appendFrame( final LogBufferPartition partition, final int activePartitionId, final DirectBuffer msg, final int start, final int length, final int streamId) { final int partitionSize = partition.getPartitionSize(); final int framedLength = framedLength(length); final int alignedFrameLength = alignedLength(framedLength); // move the tail of the partition final int frameOffset = partition.getAndAddTail(alignedFrameLength); int newTail = frameOffset + alignedFrameLength; if (newTail <= (partitionSize - HEADER_LENGTH)) { final UnsafeBuffer buffer = partition.getDataBuffer(); // write negative length field buffer.putIntOrdered(lengthOffset(frameOffset), -framedLength); UNSAFE.storeFence(); buffer.putShort(typeOffset(frameOffset), TYPE_MESSAGE); buffer.putInt(streamIdOffset(frameOffset), streamId); buffer.putBytes(messageOffset(frameOffset), msg, start, length); // commit the message buffer.putIntOrdered(lengthOffset(frameOffset), framedLength); } else { newTail = onEndOfPartition(partition, frameOffset); } return newTail; }
@SuppressWarnings("restriction") public int appendFrame( final LogBufferPartition partition, final int activePartitionId, final DirectBuffer msg, final int start, final int length, final int streamId) { final int partitionSize = partition.getPartitionSize(); final int framedLength = framedLength(length); final int alignedFrameLength = alignedLength(framedLength); // move the tail of the partition final int frameOffset = partition.getAndAddTail(alignedFrameLength); int newTail = frameOffset + alignedFrameLength; if (newTail <= (partitionSize - HEADER_LENGTH)) { final UnsafeBuffer buffer = partition.getDataBuffer(); // write negative length field buffer.putIntOrdered(lengthOffset(frameOffset), -framedLength); UNSAFE.storeFence(); buffer.putShort(typeOffset(frameOffset), TYPE_MESSAGE); buffer.putInt(streamIdOffset(frameOffset), streamId); buffer.putBytes(messageOffset(frameOffset), msg, start, length); // commit the message buffer.putIntOrdered(lengthOffset(frameOffset), framedLength); } else { newTail = onEndOfPartition(partition, frameOffset); } return newTail; }