Java Code Examples for org.agrona.concurrent.UnsafeBuffer#putBytes()

The following examples show how to use org.agrona.concurrent.UnsafeBuffer#putBytes() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CommonEventEncoder.java    From aeron with Apache License 2.0 6 votes vote down vote up
static int encodeSocketAddress(
    final UnsafeBuffer encodingBuffer, final int offset, final InetSocketAddress dstAddress)
{
    int relativeOffset = 0;
    /*
     * Stream of values:
     * - port (int) (unsigned short int)
     * - IP address length (int) (4 or 16)
     * - IP address (4 or 16 bytes)
     */

    encodingBuffer.putInt(offset + relativeOffset, dstAddress.getPort(), LITTLE_ENDIAN);
    relativeOffset += SIZE_OF_INT;

    final byte[] addressBytes = dstAddress.getAddress().getAddress();
    encodingBuffer.putInt(offset + relativeOffset, addressBytes.length, LITTLE_ENDIAN);
    relativeOffset += SIZE_OF_INT;

    encodingBuffer.putBytes(offset + relativeOffset, addressBytes);
    relativeOffset += addressBytes.length;

    return relativeOffset;
}
 
Example 2
Source File: DriverEventEncoder.java    From aeron with Apache License 2.0 6 votes vote down vote up
static void encode(
    final UnsafeBuffer encodingBuffer,
    final int offset,
    final int captureLength,
    final int length,
    final DirectBuffer srcBuffer,
    final int srcOffset,
    final InetSocketAddress dstAddress)
{
    int relativeOffset = encodeLogHeader(encodingBuffer, offset, captureLength, length);

    final int encodedSocketLength = encodeSocketAddress(encodingBuffer, offset + relativeOffset, dstAddress);
    relativeOffset += encodedSocketLength;

    final int bufferCaptureLength = captureLength - encodedSocketLength;
    encodingBuffer.putBytes(offset + relativeOffset, srcBuffer, srcOffset, bufferCaptureLength);
}
 
Example 3
Source File: BufferBuilderTest.java    From aeron with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldAppendTwoBuffersWithoutResizing()
{
    final UnsafeBuffer srcBuffer = new UnsafeBuffer(new byte[MIN_ALLOCATED_CAPACITY]);
    final byte[] bytes = "1111111122222222".getBytes(StandardCharsets.UTF_8);
    srcBuffer.putBytes(0, bytes, 0, bytes.length);

    bufferBuilder.append(srcBuffer, 0, bytes.length / 2);
    bufferBuilder.append(srcBuffer, bytes.length / 2, bytes.length / 2);

    final byte[] temp = new byte[bytes.length];
    bufferBuilder.buffer().getBytes(0, temp, 0, bytes.length);

    assertEquals(bytes.length, bufferBuilder.limit());
    assertEquals(MIN_ALLOCATED_CAPACITY, bufferBuilder.capacity());
    assertArrayEquals(temp, bytes);
}
 
Example 4
Source File: TermRebuilder.java    From aeron with Apache License 2.0 5 votes vote down vote up
/**
 * Insert a packet of frames into the log at the appropriate termOffset as indicated by the term termOffset header.
 * <p>
 * If the packet has already been inserted then this is a noop.
 *
 * @param termBuffer into which the packet should be inserted.
 * @param termOffset in the term at which the packet should be inserted.
 * @param packet     containing a sequence of frames.
 * @param length     of the packet of frames in bytes.
 */
public static void insert(
    final UnsafeBuffer termBuffer, final int termOffset, final UnsafeBuffer packet, final int length)
{
    if (0 == termBuffer.getInt(termOffset))
    {
        termBuffer.putBytes(termOffset + HEADER_LENGTH, packet, HEADER_LENGTH, length - HEADER_LENGTH);

        termBuffer.putLong(termOffset + 24, packet.getLong(24));
        termBuffer.putLong(termOffset + 16, packet.getLong(16));
        termBuffer.putLong(termOffset + 8, packet.getLong(8));

        termBuffer.putLongOrdered(termOffset, packet.getLong(0));
    }
}
 
Example 5
Source File: SenderTest.java    From aeron with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldBeAbleToSendOnChannel()
{
    final StatusMessageFlyweight msg = mock(StatusMessageFlyweight.class);
    when(msg.consumptionTermId()).thenReturn(INITIAL_TERM_ID);
    when(msg.consumptionTermOffset()).thenReturn(0);
    when(msg.receiverWindowLength()).thenReturn(ALIGNED_FRAME_LENGTH);

    publication.onStatusMessage(msg, rcvAddress);

    final UnsafeBuffer buffer = new UnsafeBuffer(ByteBuffer.allocateDirect(PAYLOAD.length));
    buffer.putBytes(0, PAYLOAD);

    termAppenders[0].appendUnfragmentedMessage(headerWriter, buffer, 0, PAYLOAD.length, null, INITIAL_TERM_ID);
    sender.doWork();

    assertThat(receivedFrames.size(), is(2));
    setupHeader.wrap(new UnsafeBuffer(receivedFrames.remove()));
    assertThat(setupHeader.headerType(), is(HeaderFlyweight.HDR_TYPE_SETUP));

    dataHeader.wrap(new UnsafeBuffer(receivedFrames.remove()));
    assertThat(dataHeader.frameLength(), is(FRAME_LENGTH));
    assertThat(dataHeader.termId(), is(INITIAL_TERM_ID));
    assertThat(dataHeader.streamId(), is(STREAM_ID));
    assertThat(dataHeader.sessionId(), is(SESSION_ID));
    assertThat(dataHeader.termOffset(), is(offsetOfMessage(1)));
    assertThat(dataHeader.headerType(), is(HeaderFlyweight.HDR_TYPE_DATA));
    assertThat(dataHeader.flags(), is(DataHeaderFlyweight.BEGIN_AND_END_FLAGS));
    assertThat(dataHeader.version(), is((short)HeaderFlyweight.CURRENT_VERSION));
}
 
Example 6
Source File: SenderTest.java    From aeron with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldSendSetupFrameAfterReceivingStatusMessageWithSetupBit()
{
    final StatusMessageFlyweight msg = mock(StatusMessageFlyweight.class);
    when(msg.consumptionTermId()).thenReturn(INITIAL_TERM_ID);
    when(msg.consumptionTermOffset()).thenReturn(0);
    when(msg.receiverWindowLength()).thenReturn(ALIGNED_FRAME_LENGTH);

    publication.onStatusMessage(msg, rcvAddress);

    final UnsafeBuffer buffer = new UnsafeBuffer(ByteBuffer.allocateDirect(PAYLOAD.length));
    buffer.putBytes(0, PAYLOAD);

    termAppenders[0].appendUnfragmentedMessage(headerWriter, buffer, 0, PAYLOAD.length, null, INITIAL_TERM_ID);
    sender.doWork();

    assertThat(receivedFrames.size(), is(2)); // setup then data
    receivedFrames.remove();
    receivedFrames.remove();

    publication.triggerSendSetupFrame();

    sender.doWork();
    assertThat(receivedFrames.size(), is(0)); // setup has been sent already, have to wait

    nanoClock.advance(Configuration.PUBLICATION_SETUP_TIMEOUT_NS + 10);
    sender.doWork();

    assertThat(receivedFrames.size(), is(1));

    setupHeader.wrap(new UnsafeBuffer(receivedFrames.remove()));
    assertThat(setupHeader.headerType(), is(HeaderFlyweight.HDR_TYPE_SETUP));
}
 
Example 7
Source File: SenderTest.java    From aeron with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldNotSendUntilStatusMessageReceived()
{
    final UnsafeBuffer buffer = new UnsafeBuffer(ByteBuffer.allocateDirect(PAYLOAD.length));
    buffer.putBytes(0, PAYLOAD);
    termAppenders[0].appendUnfragmentedMessage(headerWriter, buffer, 0, PAYLOAD.length, null, INITIAL_TERM_ID);

    sender.doWork();
    assertThat(receivedFrames.size(), is(1));
    setupHeader.wrap(receivedFrames.remove());
    assertThat(setupHeader.headerType(), is(HeaderFlyweight.HDR_TYPE_SETUP));

    final StatusMessageFlyweight msg = mock(StatusMessageFlyweight.class);
    when(msg.consumptionTermId()).thenReturn(INITIAL_TERM_ID);
    when(msg.consumptionTermOffset()).thenReturn(0);
    when(msg.receiverWindowLength()).thenReturn(ALIGNED_FRAME_LENGTH);

    publication.onStatusMessage(msg, rcvAddress);
    sender.doWork();

    assertThat(receivedFrames.size(), is(1));

    dataHeader.wrap(new UnsafeBuffer(receivedFrames.remove()));

    assertThat(dataHeader.frameLength(), is(FRAME_LENGTH));
    assertThat(dataHeader.termId(), is(INITIAL_TERM_ID));
    assertThat(dataHeader.streamId(), is(STREAM_ID));
    assertThat(dataHeader.sessionId(), is(SESSION_ID));
    assertThat(dataHeader.termOffset(), is(offsetOfMessage(1)));
    assertThat(dataHeader.headerType(), is(HeaderFlyweight.HDR_TYPE_DATA));
    assertThat(dataHeader.flags(), is(DataHeaderFlyweight.BEGIN_AND_END_FLAGS));
    assertThat(dataHeader.version(), is((short)HeaderFlyweight.CURRENT_VERSION));
}
 
Example 8
Source File: CounterMessageFlyweightTest.java    From aeron with Apache License 2.0 5 votes vote down vote up
private DirectBuffer newBuffer(final int length)
{
    final byte[] bytes = new byte[length];
    fill(bytes, (byte)1);
    final UnsafeBuffer buffer = new UnsafeBuffer(allocateDirectAligned(4 + length, 4));
    buffer.putBytes(4, bytes);
    return buffer;
}
 
Example 9
Source File: SenderTest.java    From aeron with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldNotBeAbleToSendAfterUsingUpYourWindow()
{
    final UnsafeBuffer buffer = new UnsafeBuffer(ByteBuffer.allocateDirect(PAYLOAD.length));
    buffer.putBytes(0, PAYLOAD);
    termAppenders[0].appendUnfragmentedMessage(headerWriter, buffer, 0, PAYLOAD.length, null, INITIAL_TERM_ID);

    final StatusMessageFlyweight msg = mock(StatusMessageFlyweight.class);
    when(msg.consumptionTermId()).thenReturn(INITIAL_TERM_ID);
    when(msg.consumptionTermOffset()).thenReturn(0);
    when(msg.receiverWindowLength()).thenReturn(ALIGNED_FRAME_LENGTH);

    publication.onStatusMessage(msg, rcvAddress);

    sender.doWork();

    assertThat(receivedFrames.size(), is(2));
    receivedFrames.remove();                   // skip setup

    dataHeader.wrap(new UnsafeBuffer(receivedFrames.remove()));
    assertThat(dataHeader.frameLength(), is(FRAME_LENGTH));
    assertThat(dataHeader.termId(), is(INITIAL_TERM_ID));
    assertThat(dataHeader.streamId(), is(STREAM_ID));
    assertThat(dataHeader.sessionId(), is(SESSION_ID));
    assertThat(dataHeader.termOffset(), is(offsetOfMessage(1)));
    assertThat(dataHeader.headerType(), is(HeaderFlyweight.HDR_TYPE_DATA));
    assertThat(dataHeader.flags(), is(DataHeaderFlyweight.BEGIN_AND_END_FLAGS));
    assertThat(dataHeader.version(), is((short)HeaderFlyweight.CURRENT_VERSION));

    termAppenders[0].appendUnfragmentedMessage(headerWriter, buffer, 0, PAYLOAD.length, null, INITIAL_TERM_ID);
    sender.doWork();

    assertThat(receivedFrames.size(), is(0));
}
 
Example 10
Source File: ExpandableRingBuffer.java    From agrona with Apache License 2.0 5 votes vote down vote up
private void resize(final int newMessageLength)
{
    final int newCapacity = BitUtil.findNextPositivePowerOfTwo(capacity + newMessageLength);
    if (newCapacity < capacity || newCapacity > maxCapacity)
    {
        return;
    }

    final UnsafeBuffer tempBuffer = new UnsafeBuffer(
        isDirect ? ByteBuffer.allocateDirect(newCapacity) : ByteBuffer.allocate(newCapacity));

    final int headOffset = (int)head & mask;
    final int remaining = (int)(tail - head);
    final int firstCopyLength = Math.min(remaining, capacity - headOffset);
    tempBuffer.putBytes(0, buffer, headOffset, firstCopyLength);
    int tailOffset = firstCopyLength;

    if (firstCopyLength < remaining)
    {
        final int length = remaining - firstCopyLength;
        tempBuffer.putBytes(firstCopyLength, buffer, 0, length);
        tailOffset += length;
    }

    buffer.wrap(tempBuffer);
    capacity = newCapacity;
    mask = newCapacity - 1;
    head = 0;
    tail = tailOffset;
}
 
Example 11
Source File: CommonEventEncoder.java    From aeron with Apache License 2.0 5 votes vote down vote up
static int encode(
    final UnsafeBuffer encodingBuffer,
    final int offset,
    final int captureLength,
    final int length,
    final DirectBuffer srcBuffer,
    final int srcOffset)
{
    int relativeOffset = encodeLogHeader(encodingBuffer, offset, captureLength, length);

    encodingBuffer.putBytes(offset + relativeOffset, srcBuffer, srcOffset, captureLength);
    relativeOffset += captureLength;

    return relativeOffset;
}
 
Example 12
Source File: LogBufferDescriptor.java    From aeron with Apache License 2.0 5 votes vote down vote up
/**
 * Store the default frame header to the log meta data buffer.
 *
 * @param metadataBuffer into which the default headers should be stored.
 * @param defaultHeader  to be stored.
 * @throws IllegalArgumentException if the defaultHeader larger than {@link #LOG_DEFAULT_FRAME_HEADER_MAX_LENGTH}
 */
public static void storeDefaultFrameHeader(final UnsafeBuffer metadataBuffer, final DirectBuffer defaultHeader)
{
    if (defaultHeader.capacity() != HEADER_LENGTH)
    {
        throw new IllegalArgumentException(
            "Default header length not equal to HEADER_LENGTH: length=" + defaultHeader.capacity());
    }

    metadataBuffer.putInt(LOG_DEFAULT_FRAME_HEADER_LENGTH_OFFSET, HEADER_LENGTH);
    metadataBuffer.putBytes(LOG_DEFAULT_FRAME_HEADER_OFFSET, defaultHeader, 0, HEADER_LENGTH);
}
 
Example 13
Source File: SenderTest.java    From aeron with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldSendLastDataFrameAsHeartbeatWhenIdle()
{
    final StatusMessageFlyweight msg = mock(StatusMessageFlyweight.class);
    when(msg.consumptionTermId()).thenReturn(INITIAL_TERM_ID);
    when(msg.consumptionTermOffset()).thenReturn(0);
    when(msg.receiverWindowLength()).thenReturn(ALIGNED_FRAME_LENGTH);

    publication.onStatusMessage(msg, rcvAddress);

    final UnsafeBuffer buffer = new UnsafeBuffer(ByteBuffer.allocateDirect(PAYLOAD.length));
    buffer.putBytes(0, PAYLOAD);

    termAppenders[0].appendUnfragmentedMessage(headerWriter, buffer, 0, PAYLOAD.length, null, INITIAL_TERM_ID);
    sender.doWork();

    assertThat(receivedFrames.size(), is(2));  // should send ticks
    receivedFrames.remove();                   // skip setup & data frame
    receivedFrames.remove();

    nanoClock.advance(Configuration.PUBLICATION_HEARTBEAT_TIMEOUT_NS - 1);
    sender.doWork();

    assertThat(receivedFrames.size(), is(0));  // should not send yet
    nanoClock.advance(10);
    sender.doWork();

    assertThat(receivedFrames.size(), greaterThanOrEqualTo(1));  // should send ticks

    dataHeader.wrap(receivedFrames.remove());
    assertThat(dataHeader.frameLength(), is(0));
    assertThat(dataHeader.termOffset(), is(offsetOfMessage(2)));
}
 
Example 14
Source File: RecordingWriterTests.java    From aeron with Apache License 2.0 5 votes vote down vote up
@Test
void onBlockShouldWriteHeaderOfThePaddingFrameAndAdvanceFilePositionByThePaddingLength() throws IOException
{
    final int segmentOffset = 96;
    final long startPosition = 7 * TERM_LENGTH + segmentOffset;
    final Image image = mockImage(startPosition);
    final RecordingWriter recordingWriter = new RecordingWriter(
        5, startPosition, SEGMENT_LENGTH, image, new Context().archiveDir(archiveDir), null, null, null);
    recordingWriter.init();
    final UnsafeBuffer termBuffer = new UnsafeBuffer(allocate(1024));
    frameType(termBuffer, 0, HDR_TYPE_PAD);
    frameLengthOrdered(termBuffer, 0, 1024);
    frameSessionId(termBuffer, 0, 111);

    final byte[] data = new byte[992];
    fill(data, (byte)-1);
    termBuffer.putBytes(HEADER_LENGTH, data);

    recordingWriter.onBlock(termBuffer, 0, 1024, -1, -1);

    recordingWriter.close();
    final File segmentFile = segmentFile(5, startPosition);
    assertTrue(segmentFile.exists());
    assertEquals(SEGMENT_LENGTH, segmentFile.length());

    final UnsafeBuffer fileBuffer = new UnsafeBuffer();
    fileBuffer.wrap(readAllBytes(segmentFile.toPath()));

    final byte[] preamble = new byte[segmentOffset];
    fileBuffer.getBytes(0, preamble, 0, segmentOffset);
    assertArrayEquals(new byte[segmentOffset], preamble);
    assertEquals(HDR_TYPE_PAD, frameType(fileBuffer, segmentOffset));
    assertEquals(1024, frameLength(fileBuffer, segmentOffset));
    assertEquals(111, frameSessionId(fileBuffer, segmentOffset));

    final byte[] fileBytes = new byte[992];
    fileBuffer.getBytes(segmentOffset + HEADER_LENGTH, fileBytes, 0, 992);
    assertArrayEquals(new byte[992], fileBytes);
}
 
Example 15
Source File: RecordingWriterTests.java    From aeron with Apache License 2.0 5 votes vote down vote up
@Test
void onBlockShouldWriteHeaderAndContentsOfTheNonPaddingFrame() throws IOException
{
    final Image image = mockImage(0L);
    final RecordingWriter recordingWriter = new RecordingWriter(
        1, 0, SEGMENT_LENGTH, image, new Context().archiveDir(archiveDir), null, null, null);
    recordingWriter.init();
    final UnsafeBuffer termBuffer = new UnsafeBuffer(allocate(128));
    frameType(termBuffer, 0, HDR_TYPE_DATA);
    frameLengthOrdered(termBuffer, 0, 128);
    final byte[] data = new byte[96];
    fill(data, (byte)7);
    termBuffer.putBytes(HEADER_LENGTH, data);

    recordingWriter.onBlock(termBuffer, 0, 128, -1, -1);

    recordingWriter.close();
    final File segmentFile = segmentFile(1, 0);
    assertTrue(segmentFile.exists());
    assertEquals(SEGMENT_LENGTH, segmentFile.length());

    final UnsafeBuffer fileBuffer = new UnsafeBuffer();
    fileBuffer.wrap(readAllBytes(segmentFile.toPath()));
    assertEquals(HDR_TYPE_DATA, frameType(fileBuffer, 0));
    assertEquals(128, frameLength(fileBuffer, 0));
    assertEquals(0, frameSessionId(fileBuffer, 0));

    final byte[] fileBytes = new byte[96];
    fileBuffer.getBytes(HEADER_LENGTH, fileBytes, 0, 96);
    assertArrayEquals(data, fileBytes);
}
 
Example 16
Source File: MetaDataTest.java    From artio with Apache License 2.0 4 votes vote down vote up
private void updateExpectedBuffer(final UnsafeBuffer writeBuffer, final UnsafeBuffer updateBuffer)
{
    writeBuffer.putBytes(UPDATE_OFFSET, updateBuffer, 0, SIZE_OF_SHORT);
}
 
Example 17
Source File: ExclusiveTermAppender.java    From aeron with Apache License 2.0 4 votes vote down vote up
/**
 * Append a fragmented message to the the term buffer.
 * The message will be split up into fragments of MTU length minus header.
 *
 * @param termId                for the current term.
 * @param termOffset            in the term at which to append.
 * @param header                for writing the default header.
 * @param vectors               to the buffers.
 * @param length                of the message in the source buffer.
 * @param maxPayloadLength      that the message will be fragmented into.
 * @param reservedValueSupplier {@link ReservedValueSupplier} for the frame.
 * @return the resulting offset of the term after the append on success otherwise {@link #FAILED}.
 */
public int appendFragmentedMessage(
    final int termId,
    final int termOffset,
    final HeaderWriter header,
    final DirectBufferVector[] vectors,
    final int length,
    final int maxPayloadLength,
    final ReservedValueSupplier reservedValueSupplier)
{
    final int numMaxPayloads = length / maxPayloadLength;
    final int remainingPayload = length % maxPayloadLength;
    final int lastFrameLength = remainingPayload > 0 ? align(remainingPayload + HEADER_LENGTH, FRAME_ALIGNMENT) : 0;
    final int requiredLength = (numMaxPayloads * (maxPayloadLength + HEADER_LENGTH)) + lastFrameLength;
    final UnsafeBuffer termBuffer = this.termBuffer;
    final int termLength = termBuffer.capacity();

    int resultingOffset = termOffset + requiredLength;
    putRawTailOrdered(termId, resultingOffset);

    if (resultingOffset > termLength)
    {
        resultingOffset = handleEndOfLogCondition(termBuffer, termOffset, header, termLength, termId);
    }
    else
    {
        int frameOffset = termOffset;
        byte flags = BEGIN_FRAG_FLAG;
        int remaining = length;
        int vectorIndex = 0;
        int vectorOffset = 0;

        do
        {
            final int bytesToWrite = Math.min(remaining, maxPayloadLength);
            final int frameLength = bytesToWrite + HEADER_LENGTH;
            final int alignedLength = align(frameLength, FRAME_ALIGNMENT);

            header.write(termBuffer, frameOffset, frameLength, termId);

            int bytesWritten = 0;
            int payloadOffset = frameOffset + HEADER_LENGTH;
            do
            {
                final DirectBufferVector vector = vectors[vectorIndex];
                final int vectorRemaining = vector.length - vectorOffset;
                final int numBytes = Math.min(bytesToWrite - bytesWritten, vectorRemaining);

                termBuffer.putBytes(payloadOffset, vector.buffer, vector.offset + vectorOffset, numBytes);

                bytesWritten += numBytes;
                payloadOffset += numBytes;
                vectorOffset += numBytes;

                if (vectorRemaining <= numBytes)
                {
                    vectorIndex++;
                    vectorOffset = 0;
                }
            }
            while (bytesWritten < bytesToWrite);

            if (remaining <= maxPayloadLength)
            {
                flags |= END_FRAG_FLAG;
            }

            frameFlags(termBuffer, frameOffset, flags);

            if (null != reservedValueSupplier)
            {
                final long reservedValue = reservedValueSupplier.get(termBuffer, frameOffset, frameLength);
                termBuffer.putLong(frameOffset + RESERVED_VALUE_OFFSET, reservedValue, LITTLE_ENDIAN);
            }

            frameLengthOrdered(termBuffer, frameOffset, frameLength);

            flags = 0;
            frameOffset += alignedLength;
            remaining -= bytesToWrite;
        }
        while (remaining > 0);
    }

    return resultingOffset;
}
 
Example 18
Source File: DriverEventDissectorTest.java    From aeron with Apache License 2.0 4 votes vote down vote up
private DirectBuffer newBuffer(final byte[] bytes)
{
    final UnsafeBuffer buffer = new UnsafeBuffer(allocate(bytes.length));
    buffer.putBytes(0, bytes);
    return buffer;
}
 
Example 19
Source File: ExclusiveTermAppender.java    From aeron with Apache License 2.0 4 votes vote down vote up
/**
 * Append an unfragmented message to the the term buffer as a gathering of vectors.
 *
 * @param termId                for the current term.
 * @param termOffset            in the term at which to append.
 * @param header                for writing the default header.
 * @param vectors               to the buffers.
 * @param length                of the message as a sum of the vectors.
 * @param reservedValueSupplier {@link ReservedValueSupplier} for the frame.
 * @return the resulting offset of the term after the append on success otherwise {@link #FAILED}.
 */
public int appendUnfragmentedMessage(
    final int termId,
    final int termOffset,
    final HeaderWriter header,
    final DirectBufferVector[] vectors,
    final int length,
    final ReservedValueSupplier reservedValueSupplier)
{
    final int frameLength = length + HEADER_LENGTH;
    final int alignedLength = align(frameLength, FRAME_ALIGNMENT);
    final UnsafeBuffer termBuffer = this.termBuffer;
    final int termLength = termBuffer.capacity();

    int resultingOffset = termOffset + alignedLength;
    putRawTailOrdered(termId, resultingOffset);

    if (resultingOffset > termLength)
    {
        resultingOffset = handleEndOfLogCondition(termBuffer, termOffset, header, termLength, termId);
    }
    else
    {
        header.write(termBuffer, termOffset, frameLength, termId);

        int offset = termOffset + HEADER_LENGTH;
        for (final DirectBufferVector vector : vectors)
        {
            termBuffer.putBytes(offset, vector.buffer, vector.offset, vector.length);
            offset += vector.length;
        }

        if (null != reservedValueSupplier)
        {
            final long reservedValue = reservedValueSupplier.get(termBuffer, termOffset, frameLength);
            termBuffer.putLong(termOffset + RESERVED_VALUE_OFFSET, reservedValue, LITTLE_ENDIAN);
        }

        frameLengthOrdered(termBuffer, termOffset, frameLength);
    }

    return resultingOffset;
}
 
Example 20
Source File: RecordingWriterTests.java    From aeron with Apache License 2.0 4 votes vote down vote up
@Test
void onBlockShouldRollOverToTheNextSegmentFile() throws IOException
{
    final Image image = mockImage(0L);
    final RecordingWriter recordingWriter = new RecordingWriter(
        13, 0, SEGMENT_LENGTH, image, new Context().archiveDir(archiveDir), null, null, null);
    recordingWriter.init();

    final byte[] data1 = new byte[992];
    fill(data1, (byte)13);

    final UnsafeBuffer termBuffer = new UnsafeBuffer(allocate(TERM_LENGTH));
    frameType(termBuffer, 0, HDR_TYPE_DATA);
    frameLengthOrdered(termBuffer, 0, 1024);
    termBuffer.putBytes(HEADER_LENGTH, data1);

    for (int i = 0; i < SEGMENT_LENGTH / 1024; i++)
    {
        recordingWriter.onBlock(termBuffer, 0, 1024, -1, -1);
    }

    frameType(termBuffer, 0, HDR_TYPE_DATA);
    frameLengthOrdered(termBuffer, 0, 192);
    final byte[] data2 = new byte[160];
    fill(data2, (byte)22);
    termBuffer.putBytes(HEADER_LENGTH, data2);

    recordingWriter.onBlock(termBuffer, 0, 192, -1, -1);
    recordingWriter.close();

    final File segmentFile1 = segmentFile(13, 0);
    assertTrue(segmentFile1.exists());
    assertEquals(SEGMENT_LENGTH, segmentFile1.length());

    final UnsafeBuffer fileBuffer = new UnsafeBuffer();
    fileBuffer.wrap(readAllBytes(segmentFile1.toPath()));
    assertEquals(HDR_TYPE_DATA, frameType(fileBuffer, 0));
    assertEquals(1024, frameLength(fileBuffer, 0));

    byte[] fileBytes = new byte[992];
    fileBuffer.getBytes(HEADER_LENGTH, fileBytes, 0, 992);
    assertArrayEquals(data1, fileBytes);

    final File segmentFile2 = segmentFile(13, SEGMENT_LENGTH);
    assertTrue(segmentFile2.exists());
    assertEquals(SEGMENT_LENGTH, segmentFile2.length());
    fileBuffer.wrap(readAllBytes(segmentFile2.toPath()));
    assertEquals(HDR_TYPE_DATA, frameType(fileBuffer, 0));
    assertEquals(192, frameLength(fileBuffer, 0));
    fileBytes = new byte[160];
    fileBuffer.getBytes(HEADER_LENGTH, fileBytes, 0, 160);
    assertArrayEquals(data2, fileBytes);
}