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

The following examples show how to use org.agrona.concurrent.UnsafeBuffer#putInt() . 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: DriverEventEncoder.java    From aeron with Apache License 2.0 6 votes vote down vote up
static void encodeImageRemoval(
    final UnsafeBuffer encodingBuffer,
    final int offset,
    final int captureLength,
    final int length,
    final String uri,
    final int sessionId,
    final int streamId,
    final long id)
{
    int relativeOffset = encodeLogHeader(encodingBuffer, offset, captureLength, length);

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

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

    encodingBuffer.putLong(offset + relativeOffset, id, LITTLE_ENDIAN);
    relativeOffset += SIZE_OF_LONG;

    encodeTrailingString(
        encodingBuffer, offset + relativeOffset, captureLength - (SIZE_OF_INT * 2) - SIZE_OF_LONG, uri);
}
 
Example 2
Source File: MetaDataTest.java    From artio with Apache License 2.0 6 votes vote down vote up
@Test(timeout = 10_000L)
public void shouldUpdateWrittenSessionMetaDataFittingWithinSlot()
{
    final UnsafeBuffer writeBuffer = new UnsafeBuffer(new byte[SIZE_OF_INT]);

    writeBuffer.putInt(0, META_DATA_WRONG_VALUE);
    writeMetaData(writeBuffer);

    writeBuffer.putInt(0, META_DATA_VALUE);
    writeMetaData(writeBuffer);

    UnsafeBuffer readBuffer = readSuccessfulMetaData(writeBuffer);
    assertEquals(META_DATA_VALUE, readBuffer.getInt(0));

    final UnsafeBuffer updateBuffer = updateBuffer();

    final Reply<MetaDataStatus> reply = writeMetaData(updateBuffer, META_DATA_SESSION_ID, UPDATE_OFFSET);
    assertEquals(MetaDataStatus.OK, reply.resultIfPresent());

    updateExpectedBuffer(writeBuffer, updateBuffer);

    readBuffer = readSuccessfulMetaData(writeBuffer);
    assertEquals(writeBuffer, readBuffer);
}
 
Example 3
Source File: DriverEventEncoder.java    From aeron with Apache License 2.0 6 votes vote down vote up
static void encodeSubscriptionRemoval(
    final UnsafeBuffer encodingBuffer,
    final int offset,
    final int captureLength,
    final int length,
    final String uri,
    final int streamId,
    final long id)
{
    int relativeOffset = encodeLogHeader(encodingBuffer, offset, captureLength, length);

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

    encodingBuffer.putLong(offset + relativeOffset, id, LITTLE_ENDIAN);
    relativeOffset += SIZE_OF_LONG;

    encodeTrailingString(encodingBuffer, offset + relativeOffset, captureLength - SIZE_OF_INT - SIZE_OF_LONG, uri);
}
 
Example 4
Source File: ArchiveEventEncoder.java    From aeron with Apache License 2.0 6 votes vote down vote up
static <E extends Enum<E>> int encodeSessionStateChange(
    final UnsafeBuffer encodingBuffer,
    final int offset,
    final int captureLength,
    final int length,
    final E from,
    final E to,
    final long id)
{
    int relativeOffset = encodeLogHeader(encodingBuffer, offset, captureLength, length);

    encodingBuffer.putLong(offset + relativeOffset, id, LITTLE_ENDIAN);
    relativeOffset += SIZE_OF_LONG;

    encodingBuffer.putInt(offset + relativeOffset, captureLength - (SIZE_OF_LONG + SIZE_OF_INT), LITTLE_ENDIAN);
    relativeOffset += SIZE_OF_INT;

    relativeOffset += encodingBuffer.putStringWithoutLengthAscii(offset + relativeOffset, from.name());
    relativeOffset += encodingBuffer.putStringWithoutLengthAscii(offset + relativeOffset, STATE_SEPARATOR);
    relativeOffset += encodingBuffer.putStringWithoutLengthAscii(offset + relativeOffset, to.name());

    return relativeOffset;
}
 
Example 5
Source File: TermRebuilderTest.java    From aeron with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldInsertLastFrameIntoBuffer()
{
    final int frameLength = BitUtil.align(256, FRAME_ALIGNMENT);
    final int srcOffset = 0;
    final int tail = TERM_BUFFER_CAPACITY - frameLength;
    final int termOffset = tail;
    final UnsafeBuffer packet = new UnsafeBuffer(ByteBuffer.allocateDirect(frameLength));
    packet.putShort(typeOffset(srcOffset), (short)PADDING_FRAME_TYPE, LITTLE_ENDIAN);
    packet.putInt(srcOffset, frameLength, LITTLE_ENDIAN);

    TermRebuilder.insert(termBuffer, termOffset, packet, frameLength);

    verify(termBuffer).putBytes(
        tail + HEADER_LENGTH, packet, srcOffset + HEADER_LENGTH, frameLength - HEADER_LENGTH);
}
 
Example 6
Source File: DataHeaderFlyweight.java    From aeron with Apache License 2.0 6 votes vote down vote up
/**
 * Return an initialised default Data Frame Header.
 *
 * @param sessionId for the header
 * @param streamId  for the header
 * @param termId    for the header
 * @return byte array containing the header
 */
public static UnsafeBuffer createDefaultHeader(final int sessionId, final int streamId, final int termId)
{
    final UnsafeBuffer buffer = new UnsafeBuffer(
        BufferUtil.allocateDirectAligned(HEADER_LENGTH, CACHE_LINE_LENGTH));

    buffer.putByte(VERSION_FIELD_OFFSET, CURRENT_VERSION);
    buffer.putByte(FLAGS_FIELD_OFFSET, (byte)BEGIN_AND_END_FLAGS);
    buffer.putShort(TYPE_FIELD_OFFSET, (short)HDR_TYPE_DATA, LITTLE_ENDIAN);
    buffer.putInt(SESSION_ID_FIELD_OFFSET, sessionId, LITTLE_ENDIAN);
    buffer.putInt(STREAM_ID_FIELD_OFFSET, streamId, LITTLE_ENDIAN);
    buffer.putInt(TERM_ID_FIELD_OFFSET, termId, LITTLE_ENDIAN);
    buffer.putLong(RESERVED_VALUE_OFFSET, DEFAULT_RESERVE_VALUE);

    return buffer;
}
 
Example 7
Source File: CncFileDescriptor.java    From aeron with Apache License 2.0 6 votes vote down vote up
/**
 * Fill the CnC file with metadata to define its sections.
 *
 * @param cncMetaDataBuffer           that wraps the metadata section of the CnC file.
 * @param toDriverBufferLength        for sending commands to the driver.
 * @param toClientsBufferLength       for broadcasting events to the clients.
 * @param counterMetaDataBufferLength buffer length for counters metadata.
 * @param counterValuesBufferLength   buffer length for counter values.
 * @param clientLivenessTimeoutNs     timeout value in nanoseconds for client liveness and inter-service interval.
 * @param errorLogBufferLength        for recording the distinct error log.
 * @param startTimestampMs            epoch at which the driver started.
 * @param pid                         for the process hosting the driver.
 */
public static void fillMetaData(
    final UnsafeBuffer cncMetaDataBuffer,
    final int toDriverBufferLength,
    final int toClientsBufferLength,
    final int counterMetaDataBufferLength,
    final int counterValuesBufferLength,
    final long clientLivenessTimeoutNs,
    final int errorLogBufferLength,
    final long startTimestampMs,
    final long pid)
{
    cncMetaDataBuffer.putInt(toDriverBufferLengthOffset(0), toDriverBufferLength);
    cncMetaDataBuffer.putInt(toClientsBufferLengthOffset(0), toClientsBufferLength);
    cncMetaDataBuffer.putInt(countersMetaDataBufferLengthOffset(0), counterMetaDataBufferLength);
    cncMetaDataBuffer.putInt(countersValuesBufferLengthOffset(0), counterValuesBufferLength);
    cncMetaDataBuffer.putInt(errorLogBufferLengthOffset(0), errorLogBufferLength);
    cncMetaDataBuffer.putLong(clientLivenessTimeoutOffset(0), clientLivenessTimeoutNs);
    cncMetaDataBuffer.putLong(startTimestampOffset(0), startTimestampMs);
    cncMetaDataBuffer.putLong(pidOffset(0), pid);
}
 
Example 8
Source File: TermRebuilderTest.java    From aeron with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldInsertIntoEmptyBuffer()
{
    final UnsafeBuffer packet = new UnsafeBuffer(ByteBuffer.allocateDirect(256));
    final int termOffset = 0;
    final int srcOffset = 0;
    final int length = 256;
    packet.putInt(srcOffset, length, LITTLE_ENDIAN);

    TermRebuilder.insert(termBuffer, termOffset, packet, length);

    final InOrder inOrder = inOrder(termBuffer);
    inOrder.verify(termBuffer).putBytes(
        termOffset + HEADER_LENGTH, packet, srcOffset + HEADER_LENGTH, length - HEADER_LENGTH);
    inOrder.verify(termBuffer).putLong(termOffset + 24, packet.getLong(24));
    inOrder.verify(termBuffer).putLong(termOffset + 16, packet.getLong(16));
    inOrder.verify(termBuffer).putLong(termOffset + 8, packet.getLong(8));
    inOrder.verify(termBuffer).putLongOrdered(termOffset, packet.getLong(0));
}
 
Example 9
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 10
Source File: ClusterEventEncoder.java    From aeron with Apache License 2.0 6 votes vote down vote up
static <E extends Enum<E>> int encodeStateChange(
    final UnsafeBuffer encodingBuffer,
    final int offset,
    final int captureLength,
    final int length,
    final E from,
    final E to,
    final int memberId)
{
    int relativeOffset = encodeLogHeader(encodingBuffer, offset, captureLength, length);

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

    encodingBuffer.putInt(offset + relativeOffset, captureLength - (SIZE_OF_INT * 2), LITTLE_ENDIAN);
    relativeOffset += SIZE_OF_INT;

    relativeOffset += encodingBuffer.putStringWithoutLengthAscii(offset + relativeOffset, from.name());
    relativeOffset += encodingBuffer.putStringWithoutLengthAscii(offset + relativeOffset, STATE_SEPARATOR);
    relativeOffset += encodingBuffer.putStringWithoutLengthAscii(offset + relativeOffset, to.name());

    return relativeOffset;
}
 
Example 11
Source File: MetaDataTest.java    From artio with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 10_000L)
public void shouldReceiveSessionMetaDataWhenSessionAcquired()
{
    final UnsafeBuffer writeBuffer = new UnsafeBuffer(new byte[SIZE_OF_INT]);
    writeBuffer.putInt(0, META_DATA_VALUE);
    writeMetaData(writeBuffer);

    acquireAcceptingSession();
    assertEquals(MetaDataStatus.OK, acceptingHandler.lastSessionMetaDataStatus());
    final DirectBuffer readBuffer = acceptingHandler.lastSessionMetaData();
    assertEquals(META_DATA_VALUE, readBuffer.getInt(0));
    assertEquals(SIZE_OF_INT, readBuffer.capacity());
}
 
Example 12
Source File: OneToOneRingBufferConcurrentTest.java    From agrona with Apache License 2.0 5 votes vote down vote up
public void run()
{
    final UnsafeBuffer srcBuffer = new UnsafeBuffer(new byte[1024]);
    for (int i = 0; i < REPETITIONS; i++)
    {

        int index = -1;
        try
        {
            while (INSUFFICIENT_CAPACITY == (index = ringBuffer.tryClaim(MSG_TYPE_ID, SIZE_OF_INT)))
            {
                Thread.yield();
            }
            ringBuffer.buffer().putInt(index, -i); // should be skipped
        }
        finally
        {
            ringBuffer.abort(index);
        }

        srcBuffer.putInt(16, i);
        while (!ringBuffer.write(MSG_TYPE_ID, srcBuffer, 16, SIZE_OF_INT))
        {
            Thread.yield();
        }
    }
}
 
Example 13
Source File: RecordingLog.java    From aeron with Apache License 2.0 5 votes vote down vote up
private void writeEntryToBuffer(final Entry entry, final UnsafeBuffer buffer, final ByteBuffer byteBuffer)
{
    buffer.putLong(RECORDING_ID_OFFSET, entry.recordingId, LITTLE_ENDIAN);
    buffer.putLong(LEADERSHIP_TERM_ID_OFFSET, entry.leadershipTermId, LITTLE_ENDIAN);
    buffer.putLong(TERM_BASE_LOG_POSITION_OFFSET, entry.termBaseLogPosition, LITTLE_ENDIAN);
    buffer.putLong(LOG_POSITION_OFFSET, entry.logPosition, LITTLE_ENDIAN);
    buffer.putLong(TIMESTAMP_OFFSET, entry.timestamp, LITTLE_ENDIAN);
    buffer.putInt(SERVICE_ID_OFFSET, entry.serviceId, LITTLE_ENDIAN);
    buffer.putInt(ENTRY_TYPE_OFFSET, entry.type, LITTLE_ENDIAN);

    byteBuffer.limit(ENTRY_LENGTH).position(0);
}
 
Example 14
Source File: RecordingPos.java    From aeron with Apache License 2.0 5 votes vote down vote up
public static Counter allocate(
    final Aeron aeron,
    final UnsafeBuffer tempBuffer,
    final long recordingId,
    final int sessionId,
    final int streamId,
    final String strippedChannel,
    final String sourceIdentity)
{
    tempBuffer.putLong(RECORDING_ID_OFFSET, recordingId);
    tempBuffer.putInt(SESSION_ID_OFFSET, sessionId);

    final int sourceIdentityLength = Math.min(sourceIdentity.length(), MAX_KEY_LENGTH - SOURCE_IDENTITY_OFFSET);
    tempBuffer.putStringAscii(SOURCE_IDENTITY_LENGTH_OFFSET, sourceIdentity);
    final int keyLength = SOURCE_IDENTITY_OFFSET + sourceIdentityLength;

    final int labelOffset = BitUtil.align(keyLength, SIZE_OF_INT);
    int labelLength = 0;
    labelLength += tempBuffer.putStringWithoutLengthAscii(labelOffset, NAME + ": ");
    labelLength += tempBuffer.putLongAscii(labelOffset + labelLength, recordingId);
    labelLength += tempBuffer.putStringWithoutLengthAscii(labelOffset + labelLength, " ");
    labelLength += tempBuffer.putIntAscii(labelOffset + labelLength, sessionId);
    labelLength += tempBuffer.putStringWithoutLengthAscii(labelOffset + labelLength, " ");
    labelLength += tempBuffer.putIntAscii(labelOffset + labelLength, streamId);
    labelLength += tempBuffer.putStringWithoutLengthAscii(labelOffset + labelLength, " ");
    labelLength += tempBuffer.putStringWithoutLengthAscii(
        labelOffset + labelLength, strippedChannel, 0, MAX_LABEL_LENGTH - labelLength);

    return aeron.addCounter(
        RECORDING_POSITION_TYPE_ID, tempBuffer, 0, keyLength, tempBuffer, labelOffset, labelLength);
}
 
Example 15
Source File: MetaDataTest.java    From artio with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 10_000L)
public void shouldUpdateWrittenSessionMetaDataTooBigForOldSlot()
{
    final UnsafeBuffer writeBuffer = new UnsafeBuffer(new byte[SIZE_OF_INT]);

    writeBuffer.putInt(0, META_DATA_WRONG_VALUE);
    writeMetaData(writeBuffer);

    final UnsafeBuffer bigWriteBuffer = new UnsafeBuffer(new byte[SIZE_OF_LONG]);
    bigWriteBuffer.putLong(0, META_DATA_VALUE);
    writeMetaData(bigWriteBuffer);

    UnsafeBuffer readBuffer = readSuccessfulMetaData(bigWriteBuffer);
    assertEquals(META_DATA_VALUE, readBuffer.getInt(0));

    final UnsafeBuffer updateBuffer = updateBuffer();

    final int bigUpdateOffset = SIZE_OF_LONG;
    final Reply<MetaDataStatus> reply = writeMetaData(updateBuffer, META_DATA_SESSION_ID, bigUpdateOffset);
    assertEquals(MetaDataStatus.OK, reply.resultIfPresent());

    final UnsafeBuffer aggregatedBuffer = new UnsafeBuffer(new byte[bigUpdateOffset + SIZE_OF_SHORT]);
    aggregatedBuffer.putLong(0, META_DATA_VALUE);
    aggregatedBuffer.putShort(bigUpdateOffset, UPDATE_VALUE);

    readBuffer = readSuccessfulMetaData(aggregatedBuffer);
    assertEquals(aggregatedBuffer, readBuffer);
}
 
Example 16
Source File: MetaDataTest.java    From artio with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 10_000L)
public void shouldReadWrittenSessionMetaData()
{
    final UnsafeBuffer writeBuffer = new UnsafeBuffer(new byte[SIZE_OF_INT]);
    writeBuffer.putInt(0, META_DATA_VALUE);

    writeMetaData(writeBuffer);

    final UnsafeBuffer readBuffer = readSuccessfulMetaData(writeBuffer);
    assertEquals(META_DATA_VALUE, readBuffer.getInt(0));
}
 
Example 17
Source File: AbstractGatewayToGatewaySystemTest.java    From artio with Apache License 2.0 4 votes vote down vote up
void writeMetaData()
{
    final UnsafeBuffer writeBuffer = new UnsafeBuffer(new byte[SIZE_OF_INT]);
    writeBuffer.putInt(0, META_DATA_VALUE);
    writeMetaData(writeBuffer);
}
 
Example 18
Source File: LogBufferDescriptor.java    From aeron with Apache License 2.0 2 votes vote down vote up
/**
 * Set the value of the current active partition index for the producer.
 *
 * @param metadataBuffer containing the meta data.
 * @param termCount      value of the active term count used by the producer of this log.
 */
public static void activeTermCount(final UnsafeBuffer metadataBuffer, final int termCount)
{
    metadataBuffer.putInt(LOG_ACTIVE_TERM_COUNT_OFFSET, termCount);
}
 
Example 19
Source File: FrameDescriptor.java    From aeron with Apache License 2.0 2 votes vote down vote up
/**
 * Write the term id field for a frame.
 *
 * @param buffer     containing the frame.
 * @param termOffset at which a frame begins.
 * @param termId     value for the frame.
 */
public static void frameTermId(final UnsafeBuffer buffer, final int termOffset, final int termId)
{
    buffer.putInt(termIdOffset(termOffset), termId, LITTLE_ENDIAN);
}
 
Example 20
Source File: FrameDescriptor.java    From aeron with Apache License 2.0 2 votes vote down vote up
/**
 * Write the term offset field for a frame.
 *
 * @param buffer     containing the frame.
 * @param termOffset at which a frame begins.
 */
public static void frameTermOffset(final UnsafeBuffer buffer, final int termOffset)
{
    buffer.putInt(termOffsetOffset(termOffset), termOffset, LITTLE_ENDIAN);
}