Java Code Examples for org.agrona.MutableDirectBuffer#putLong()

The following examples show how to use org.agrona.MutableDirectBuffer#putLong() . 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: HeartbeatTimestamp.java    From aeron with Apache License 2.0 6 votes vote down vote up
/**
 * Allocate a counter id for tracking the last heartbeat of an entity.
 *
 * @param tempBuffer      to be used for labels and key.
 * @param name            of the counter for the label.
 * @param typeId          of the counter for classification.
 * @param countersManager from which to allocated the underlying storage.
 * @param registrationId  to be associated with the counter.
 * @return the counter id to be used.
 */
public static int allocateCounterId(
    final MutableDirectBuffer tempBuffer,
    final String name,
    final int typeId,
    final CountersManager countersManager,
    final long registrationId)
{
    tempBuffer.putLong(REGISTRATION_ID_OFFSET, registrationId);
    final int keyLength = REGISTRATION_ID_OFFSET + SIZE_OF_LONG;

    final int labelOffset = BitUtil.align(keyLength, SIZE_OF_INT);
    int labelLength = 0;
    labelLength += tempBuffer.putStringWithoutLengthAscii(labelOffset + labelLength, name);
    labelLength += tempBuffer.putStringWithoutLengthAscii(labelOffset + labelLength, ": ");
    labelLength += tempBuffer.putLongAscii(labelOffset + labelLength, registrationId);

    return countersManager.allocate(
        typeId,
        tempBuffer,
        0,
        keyLength,
        tempBuffer,
        labelOffset,
        labelLength);
}
 
Example 2
Source File: InternalILink3Connection.java    From artio with Apache License 2.0 5 votes vote down vote up
public long tryClaim(
    final MessageEncoderFlyweight message, final int variableLength)
{
    validateCanSend();

    final long position = proxy.claimILinkMessage(
        message.sbeBlockLength() + variableLength, message);

    if (position > 0)
    {
        final int templateId = message.sbeTemplateId();
        final MutableDirectBuffer buffer = message.buffer();
        final int messageOffset = message.offset();

        final int seqNumOffset = offsets.seqNumOffset(templateId);
        if (seqNumOffset != MISSING_OFFSET)
        {
            buffer.putInt(messageOffset + seqNumOffset, (int)nextSentSeqNo++, LITTLE_ENDIAN);
        }

        // NB: possRetrans field does not need to be set because it is always false in this claim API
        // and the false byte is 0, which is what Aeron buffers are initialised to.

        final int sendingTimeEpochOffset = offsets.sendingTimeEpochOffset(templateId);
        if (sendingTimeEpochOffset != MISSING_OFFSET)
        {
            buffer.putLong(messageOffset + sendingTimeEpochOffset, requestTimestamp(), LITTLE_ENDIAN);
        }
    }

    return position;
}
 
Example 3
Source File: FileSender.java    From aeron with Apache License 2.0 5 votes vote down vote up
private static void sendChunk(
    final Publication publication,
    final BufferClaim bufferClaim,
    final long correlationId,
    final UnsafeBuffer fileBuffer,
    final int chunkOffset,
    final int chunkLength)
{
    long result;
    while ((result = publication.tryClaim(CHUNK_PAYLOAD_OFFSET + chunkLength, bufferClaim)) < 0)
    {
        checkResult(result);
        Thread.yield();
    }

    final MutableDirectBuffer buffer = bufferClaim.buffer();
    final int offset = bufferClaim.offset();

    buffer.putInt(offset + VERSION_OFFSET, VERSION, LITTLE_ENDIAN);
    buffer.putInt(offset + TYPE_OFFSET, FILE_CHUNK_TYPE, LITTLE_ENDIAN);
    buffer.putLong(offset + CORRELATION_ID_OFFSET, correlationId, LITTLE_ENDIAN);
    buffer.putLong(offset + CHUNK_OFFSET_OFFSET, chunkOffset, LITTLE_ENDIAN);
    buffer.putLong(offset + CHUNK_LENGTH_OFFSET, chunkLength, LITTLE_ENDIAN);
    buffer.putBytes(offset + CHUNK_PAYLOAD_OFFSET, fileBuffer, chunkOffset, chunkLength);

    bufferClaim.commit();
}
 
Example 4
Source File: BufferAlignmentAgentTest.java    From agrona with Apache License 2.0 5 votes vote down vote up
private void testAlignedWriteMethods(final MutableDirectBuffer buffer, final int offset)
{
    buffer.putLong(offset + SIZE_OF_LONG, Long.MAX_VALUE);
    buffer.putLong(offset + SIZE_OF_LONG, Long.MAX_VALUE, BIG_ENDIAN);
    buffer.putDouble(offset + SIZE_OF_DOUBLE, Double.MAX_VALUE);
    buffer.putDouble(offset + SIZE_OF_DOUBLE, Double.MAX_VALUE, BIG_ENDIAN);

    buffer.putInt(offset + SIZE_OF_INT, Integer.MAX_VALUE);
    buffer.putInt(offset + SIZE_OF_INT, Integer.MAX_VALUE, BIG_ENDIAN);
    buffer.putFloat(offset + SIZE_OF_FLOAT, Float.MAX_VALUE);
    buffer.putFloat(offset + SIZE_OF_FLOAT, Float.MAX_VALUE, BIG_ENDIAN);

    buffer.putShort(offset + SIZE_OF_SHORT, Short.MAX_VALUE);
    buffer.putShort(offset + SIZE_OF_SHORT, Short.MAX_VALUE, BIG_ENDIAN);
    buffer.putChar(offset + SIZE_OF_CHAR, Character.MAX_VALUE);
    buffer.putChar(offset + SIZE_OF_CHAR, Character.MAX_VALUE, BIG_ENDIAN);

    buffer.putByte(offset + SIZE_OF_BYTE, Byte.MAX_VALUE);
    buffer.putByte(offset + SIZE_OF_BYTE, Byte.MAX_VALUE);

    buffer.putStringUtf8(offset + SIZE_OF_INT, TEST_STRING);
    buffer.putStringUtf8(offset + SIZE_OF_INT, TEST_STRING, BIG_ENDIAN);
    buffer.putStringUtf8(offset + SIZE_OF_INT, TEST_STRING, Integer.MAX_VALUE);
    buffer.putStringUtf8(offset + SIZE_OF_INT, TEST_STRING, BIG_ENDIAN, Integer.MAX_VALUE);
    buffer.putStringAscii(offset + SIZE_OF_INT, TEST_STRING);
    buffer.putStringAscii(offset + SIZE_OF_INT, TEST_STRING, BIG_ENDIAN);

    // string size is not read for these method => no need for 4-bytes
    // alignment
    buffer.putStringWithoutLengthUtf8(offset + SIZE_OF_BYTE, TEST_STRING);
    buffer.putStringWithoutLengthAscii(offset + SIZE_OF_BYTE, TEST_STRING);
}
 
Example 5
Source File: BufferAlignmentAgentTest.java    From agrona with Apache License 2.0 5 votes vote down vote up
private void testUnAlignedWriteMethods(final MutableDirectBuffer buffer, final int offset)
{
    buffer.putLong(offset, Long.MAX_VALUE); // assert that buffer[offset] is
    // 8-bytes aligned

    assertUnaligned(offset + SIZE_OF_INT, (i) -> buffer.putLong(i, Long.MAX_VALUE));
    assertUnaligned(offset + SIZE_OF_INT, (i) -> buffer.putLong(i, Long.MAX_VALUE, BIG_ENDIAN));
    assertUnaligned(offset + SIZE_OF_FLOAT, (i) -> buffer.putDouble(i, Double.MAX_VALUE));
    assertUnaligned(offset + SIZE_OF_FLOAT, (i) -> buffer.putDouble(i, Double.MAX_VALUE, BIG_ENDIAN));

    assertUnaligned(offset + SIZE_OF_SHORT, (i) -> buffer.putInt(i, Integer.MAX_VALUE));
    assertUnaligned(offset + SIZE_OF_SHORT, (i) -> buffer.putInt(i, Integer.MAX_VALUE, BIG_ENDIAN));
    assertUnaligned(offset + SIZE_OF_SHORT, (i) -> buffer.putFloat(i, Float.MAX_VALUE));
    assertUnaligned(offset + SIZE_OF_SHORT, (i) -> buffer.putFloat(i, Float.MAX_VALUE, BIG_ENDIAN));

    assertUnaligned(offset + SIZE_OF_BYTE, (i) -> buffer.putShort(i, Short.MAX_VALUE));
    assertUnaligned(offset + SIZE_OF_BYTE, (i) -> buffer.putShort(i, Short.MAX_VALUE, BIG_ENDIAN));
    assertUnaligned(offset + SIZE_OF_BYTE, (i) -> buffer.putChar(i, Character.MAX_VALUE));
    assertUnaligned(offset + SIZE_OF_BYTE, (i) -> buffer.putChar(i, Character.MAX_VALUE, BIG_ENDIAN));

    assertUnaligned(offset + SIZE_OF_SHORT, (i) -> buffer.putStringAscii(i, TEST_STRING));
    assertUnaligned(offset + SIZE_OF_SHORT, (i) -> buffer.putStringAscii(i, TEST_STRING, BIG_ENDIAN));
    assertUnaligned(offset + SIZE_OF_SHORT, (i) -> buffer.putStringUtf8(i, TEST_STRING));
    assertUnaligned(offset + SIZE_OF_SHORT, (i) -> buffer.putStringUtf8(i, TEST_STRING, BIG_ENDIAN));
    assertUnaligned(offset + SIZE_OF_SHORT, (i) -> buffer.putStringUtf8(i, TEST_STRING, Integer.MAX_VALUE));
    assertUnaligned(offset + SIZE_OF_SHORT,
        (i) -> buffer.putStringUtf8(i, TEST_STRING, BIG_ENDIAN, Integer.MAX_VALUE));
}
 
Example 6
Source File: CountersManagerTest.java    From agrona with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldStoreRawData()
{
    final int typeIdOne = 333;
    final long keyOne = 777L;
    final MutableDirectBuffer keyOneBuffer = new UnsafeBuffer(ByteBuffer.allocateDirect(8));
    keyOneBuffer.putLong(0, keyOne);
    final DirectBuffer labelOneBuffer = new UnsafeBuffer("Test Label One".getBytes(US_ASCII));

    final int typeIdTwo = 222;
    final long keyTwo = 444;
    final MutableDirectBuffer keyTwoBuffer = new UnsafeBuffer(ByteBuffer.allocateDirect(8));
    keyTwoBuffer.putLong(0, keyTwo);
    final DirectBuffer labelTwoBuffer = new UnsafeBuffer("Test Label Two".getBytes(US_ASCII));

    final int counterIdOne = manager.allocate(
        typeIdOne, keyOneBuffer, 0, keyOneBuffer.capacity(), labelOneBuffer, 0, labelOneBuffer.capacity());

    final int counterIdTwo = manager.allocate(
        typeIdTwo, keyTwoBuffer, 0, keyTwoBuffer.capacity(), labelTwoBuffer, 0, labelTwoBuffer.capacity());

    manager.forEach(metaData);

    final ArgumentCaptor<DirectBuffer> argCaptorOne = ArgumentCaptor.forClass(DirectBuffer.class);
    final ArgumentCaptor<DirectBuffer> argCaptorTwo = ArgumentCaptor.forClass(DirectBuffer.class);

    final InOrder inOrder = Mockito.inOrder(metaData);
    inOrder.verify(metaData).accept(eq(counterIdOne), eq(typeIdOne), argCaptorOne.capture(), eq("Test Label One"));
    inOrder.verify(metaData).accept(eq(counterIdTwo), eq(typeIdTwo), argCaptorTwo.capture(), eq("Test Label Two"));
    inOrder.verifyNoMoreInteractions();

    final DirectBuffer keyOneBufferCapture = argCaptorOne.getValue();
    assertThat(keyOneBufferCapture.getLong(0), is(keyOne));

    final DirectBuffer keyTwoBufferCapture = argCaptorTwo.getValue();
    assertThat(keyTwoBufferCapture.getLong(0), is(keyTwo));
}
 
Example 7
Source File: StreamCounter.java    From aeron with Apache License 2.0 4 votes vote down vote up
public static int allocateCounterId(
    final MutableDirectBuffer tempBuffer,
    final String name,
    final int typeId,
    final CountersManager countersManager,
    final long registrationId,
    final int sessionId,
    final int streamId,
    final String channel)
{
    tempBuffer.putLong(REGISTRATION_ID_OFFSET, registrationId);
    tempBuffer.putInt(SESSION_ID_OFFSET, sessionId);
    tempBuffer.putInt(STREAM_ID_OFFSET, streamId);

    final int channelLength = tempBuffer.putStringWithoutLengthAscii(
        CHANNEL_OFFSET + SIZE_OF_INT, channel, 0, MAX_CHANNEL_LENGTH);
    tempBuffer.putInt(CHANNEL_OFFSET, channelLength);
    final int keyLength = CHANNEL_OFFSET + SIZE_OF_INT + channelLength;

    final int labelOffset = BitUtil.align(keyLength, SIZE_OF_INT);
    int labelLength = 0;
    labelLength += tempBuffer.putStringWithoutLengthAscii(labelOffset + labelLength, name);
    labelLength += tempBuffer.putStringWithoutLengthAscii(labelOffset + labelLength, ": ");
    labelLength += tempBuffer.putLongAscii(labelOffset + labelLength, registrationId);
    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, channel, 0, MAX_LABEL_LENGTH - labelLength);

    return countersManager.allocate(
        typeId,
        tempBuffer,
        0,
        keyLength,
        tempBuffer,
        labelOffset,
        labelLength);
}
 
Example 8
Source File: StreamCounter.java    From aeron with Apache License 2.0 4 votes vote down vote up
/**
 * Allocate a counter for tracking a position on a stream of messages.
 *
 * @param tempBuffer      to be used for labels and key.
 * @param name            of the counter for the label.
 * @param typeId          of the counter for classification.
 * @param countersManager from which to allocated the underlying storage.
 * @param registrationId  to be associated with the counter.
 * @param sessionId       for the stream of messages.
 * @param streamId        for the stream of messages.
 * @param channel         for the stream of messages.
 * @param joinPosition    for the label.
 * @return a new {@link UnsafeBufferPosition} for tracking the stream.
 */
public static UnsafeBufferPosition allocate(
    final MutableDirectBuffer tempBuffer,
    final String name,
    final int typeId,
    final CountersManager countersManager,
    final long registrationId,
    final int sessionId,
    final int streamId,
    final String channel,
    final long joinPosition)
{
    tempBuffer.putLong(REGISTRATION_ID_OFFSET, registrationId);
    tempBuffer.putInt(SESSION_ID_OFFSET, sessionId);
    tempBuffer.putInt(STREAM_ID_OFFSET, streamId);

    final int channelLength = tempBuffer.putStringWithoutLengthAscii(
        CHANNEL_OFFSET + SIZE_OF_INT, channel, 0, MAX_CHANNEL_LENGTH);
    tempBuffer.putInt(CHANNEL_OFFSET, channelLength);
    final int keyLength = CHANNEL_OFFSET + SIZE_OF_INT + channelLength;

    final int labelOffset = BitUtil.align(keyLength, SIZE_OF_INT);
    int labelLength = 0;
    labelLength += tempBuffer.putStringWithoutLengthAscii(labelOffset + labelLength, name);
    labelLength += tempBuffer.putStringWithoutLengthAscii(labelOffset + labelLength, ": ");
    labelLength += tempBuffer.putLongAscii(labelOffset + labelLength, registrationId);
    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, channel, 0, MAX_LABEL_LENGTH - labelLength);

    if (labelLength < (MAX_LABEL_LENGTH - 20))
    {
        labelLength += tempBuffer.putStringWithoutLengthAscii(labelOffset + labelLength, " @");
        labelLength += tempBuffer.putLongAscii(labelOffset + labelLength, joinPosition);
    }

    final int counterId = countersManager.allocate(
        typeId,
        tempBuffer,
        0,
        keyLength,
        tempBuffer,
        labelOffset,
        labelLength);

    return new UnsafeBufferPosition((UnsafeBuffer)countersManager.valuesBuffer(), counterId, countersManager);
}
 
Example 9
Source File: IrUtil.java    From simple-binary-encoding with Apache License 2.0 4 votes vote down vote up
public static int put(final MutableDirectBuffer buffer, final PrimitiveValue value, final PrimitiveType type)
{
    if (value == null)
    {
        return 0;
    }

    switch (type)
    {
        case CHAR:
            if (value.size() == 1)
            {
                if (value.representation() == PrimitiveValue.Representation.LONG)
                {
                    buffer.putByte(0, (byte)value.longValue());
                }
                else
                {
                    buffer.putByte(0, value.byteArrayValue()[0]);
                }
                return 1;
            }
            else
            {
                buffer.putBytes(0, value.byteArrayValue(), 0, value.byteArrayValue().length);
                return value.byteArrayValue().length;
            }

        case INT8:
            buffer.putByte(0, (byte)value.longValue());
            return 1;

        case INT16:
            buffer.putShort(0, (short)value.longValue(), ByteOrder.LITTLE_ENDIAN);
            return 2;

        case INT32:
            buffer.putInt(0, (int)value.longValue(), ByteOrder.LITTLE_ENDIAN);
            return 4;

        case INT64:
            buffer.putLong(0, value.longValue(), ByteOrder.LITTLE_ENDIAN);
            return 8;

        case UINT8:
            buffer.putByte(0, (byte)value.longValue());
            return 1;

        case UINT16:
            buffer.putShort(0, (short)value.longValue(), ByteOrder.LITTLE_ENDIAN);
            return 2;

        case UINT32:
            buffer.putInt(0, (int)value.longValue(), ByteOrder.LITTLE_ENDIAN);
            return 4;

        case UINT64:
            buffer.putLong(0, value.longValue(), ByteOrder.LITTLE_ENDIAN);
            return 8;

        case FLOAT:
            buffer.putFloat(0, (float)value.doubleValue(), ByteOrder.LITTLE_ENDIAN);
            return 4;

        case DOUBLE:
            buffer.putDouble(0, value.doubleValue(), ByteOrder.LITTLE_ENDIAN);
            return 8;

        default:
            return 0;
    }
}