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

The following examples show how to use org.agrona.MutableDirectBuffer#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: ChannelEndpointStatus.java    From aeron with Apache License 2.0 6 votes vote down vote up
/**
 * Allocate an indicator for tracking the status of a channel endpoint.
 *
 * @param tempBuffer      to be used for labels and metadata.
 * @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 channel         for the stream of messages.
 * @return a new {@link AtomicCounter} for tracking the status.
 */
public static AtomicCounter allocate(
    final MutableDirectBuffer tempBuffer,
    final String name,
    final int typeId,
    final CountersManager countersManager,
    final String channel)
{
    final int keyLength = tempBuffer.putStringWithoutLengthAscii(
        CHANNEL_OFFSET + SIZE_OF_INT, channel, 0, MAX_CHANNEL_LENGTH);
    tempBuffer.putInt(CHANNEL_OFFSET, keyLength);

    int labelLength = 0;
    labelLength += tempBuffer.putStringWithoutLengthAscii(keyLength + labelLength, name);
    labelLength += tempBuffer.putStringWithoutLengthAscii(keyLength + labelLength, ": ");
    labelLength += tempBuffer.putStringWithoutLengthAscii(
        keyLength + labelLength, channel, 0, MAX_LABEL_LENGTH - labelLength);

    if (labelLength < MAX_LABEL_LENGTH)
    {
        tempBuffer.putByte(keyLength + labelLength, (byte)' ');
        labelLength += 1;
    }

    return countersManager.newCounter(typeId, tempBuffer, 0, keyLength, tempBuffer, keyLength, labelLength);
}
 
Example 2
Source File: ExclusiveTermAppender.java    From aeron with Apache License 2.0 6 votes vote down vote up
/**
 * Append pre-formatted block of message fragments into the term buffer.
 * <p>
 * <em>WARNING: This is internal API used by {@code ExclusivePublication#offerBlock} method.</em>
 * </p>
 *
 * @param termId     for the current term.
 * @param termOffset in the term at which to append.
 * @param buffer     which contains block of messages.
 * @param offset     within the buffer at which the block begins.
 * @param length     of the block in bytes (always aligned).
 * @return the resulting offset of the term after success otherwise {@link #FAILED}.
 */
public int appendBlock(
    final int termId,
    final int termOffset,
    final MutableDirectBuffer buffer,
    final int offset,
    final int length)
{
    final int resultingOffset = termOffset + length;
    final int lengthOfFirstFrame = buffer.getInt(offset, LITTLE_ENDIAN);

    buffer.putInt(offset, 0, LITTLE_ENDIAN);
    termBuffer.putBytes(termOffset, buffer, offset, length);
    frameLengthOrdered(termBuffer, termOffset, lengthOfFirstFrame);
    putRawTailOrdered(termId, resultingOffset);

    return resultingOffset;
}
 
Example 3
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 4
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 5
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 6
Source File: TestUtils.java    From lmdbjava with Apache License 2.0 4 votes vote down vote up
static byte[] ba(final int value) {
  final MutableDirectBuffer b = new UnsafeBuffer(new byte[4]);
  b.putInt(0, value);
  return b.byteArray();
}
 
Example 7
Source File: TestUtils.java    From lmdbjava with Apache License 2.0 4 votes vote down vote up
static MutableDirectBuffer mdb(final int value) {
  final MutableDirectBuffer b = new UnsafeBuffer(allocateDirect(BYTES));
  b.putInt(0, value);
  return b;
}
 
Example 8
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 9
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 10
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;
    }
}