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

The following examples show how to use org.agrona.MutableDirectBuffer#getInt() . 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: 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 2
Source File: ExclusivePublication.java    From aeron with Apache License 2.0 6 votes vote down vote up
private void checkFirstFrame(final MutableDirectBuffer buffer, final int offset)
{
    final int frameType = HDR_TYPE_DATA;
    final int blockTermOffset = buffer.getInt(offset + TERM_OFFSET_FIELD_OFFSET, LITTLE_ENDIAN);
    final int blockSessionId = buffer.getInt(offset + SESSION_ID_FIELD_OFFSET, LITTLE_ENDIAN);
    final int blockStreamId = buffer.getInt(offset + STREAM_ID_FIELD_OFFSET, LITTLE_ENDIAN);
    final int blockTermId = buffer.getInt(offset + TERM_ID_FIELD_OFFSET, LITTLE_ENDIAN);
    final int blockFrameType = buffer.getShort(offset + TYPE_FIELD_OFFSET, LITTLE_ENDIAN) & 0xFFFF;

    if (blockTermOffset != termOffset ||
        blockSessionId != sessionId ||
        blockStreamId != streamId ||
        blockTermId != termId ||
        frameType != blockFrameType)
    {
        throw new IllegalArgumentException("improperly formatted block:" +
            " termOffset=" + blockTermOffset + " (expected=" + termOffset + ")," +
            " sessionId=" + blockSessionId + " (expected=" + sessionId + ")," +
            " streamId=" + blockStreamId + " (expected=" + streamId + ")," +
            " termId=" + blockTermId + " (expected=" + termId + ")," +
            " frameType=" + blockFrameType + " (expected=" + frameType + ")");
    }
}
 
Example 3
Source File: ClusterEventDissector.java    From aeron with Apache License 2.0 6 votes vote down vote up
static void dissectStateChange(
    final ClusterEventCode eventCode,
    final MutableDirectBuffer buffer,
    final int offset,
    final StringBuilder builder)
{
    int absoluteOffset = offset;
    absoluteOffset += dissectLogHeader(CONTEXT, eventCode, buffer, absoluteOffset, builder);

    final int memberId = buffer.getInt(absoluteOffset, LITTLE_ENDIAN);
    absoluteOffset += SIZE_OF_INT;

    builder.append(": memberId=").append(memberId);
    builder.append(", ");
    buffer.getStringAscii(absoluteOffset, builder);
}
 
Example 4
Source File: ManyToOneRingBufferBenchmark.java    From benchmarks with Apache License 2.0 5 votes vote down vote up
public void onMessage(final int msgTypeId, final MutableDirectBuffer buffer, final int index, final int length)
{
    final int value = buffer.getInt(index);
    if (value >= 0)
    {
        final Queue<Integer> responseQueue = responseQueues[value];
        while (!responseQueue.offer(SENTINEL))
        {
            ThreadHints.onSpinWait();
        }
    }
}
 
Example 5
Source File: ClusterEventDissector.java    From aeron with Apache License 2.0 5 votes vote down vote up
static void dissectNewLeadershipTerm(
    final MutableDirectBuffer buffer, final int offset, final StringBuilder builder)
{
    int absoluteOffset = offset;
    absoluteOffset += dissectLogHeader(CONTEXT, NEW_LEADERSHIP_TERM, buffer, absoluteOffset, builder);

    final long logLeadershipTermId = buffer.getLong(absoluteOffset, LITTLE_ENDIAN);
    absoluteOffset += SIZE_OF_LONG;

    final long logTruncatePosition = buffer.getLong(absoluteOffset, LITTLE_ENDIAN);
    absoluteOffset += SIZE_OF_LONG;

    final long leadershipTermId = buffer.getLong(absoluteOffset, LITTLE_ENDIAN);
    absoluteOffset += SIZE_OF_LONG;

    final long logPosition = buffer.getLong(absoluteOffset, LITTLE_ENDIAN);
    absoluteOffset += SIZE_OF_LONG;

    final long timestamp = buffer.getLong(absoluteOffset, LITTLE_ENDIAN);
    absoluteOffset += SIZE_OF_LONG;

    final int leaderMemberId = buffer.getInt(absoluteOffset, LITTLE_ENDIAN);
    absoluteOffset += SIZE_OF_INT;

    final int logSessionId = buffer.getInt(absoluteOffset, LITTLE_ENDIAN);
    absoluteOffset += SIZE_OF_INT;

    final boolean isStartup = 1 == buffer.getInt(absoluteOffset, LITTLE_ENDIAN);

    builder.append(": logLeadershipTermId=").append(logLeadershipTermId)
        .append(", logTruncatePosition=").append(logTruncatePosition)
        .append(", leadershipTermId=").append(leadershipTermId)
        .append(", logPosition=").append(logPosition)
        .append(", timestamp=").append(timestamp)
        .append(", leaderMemberId=").append(leaderMemberId)
        .append(", logSessionId=").append(logSessionId)
        .append(", isStartup=").append(isStartup);
}
 
Example 6
Source File: CommonEventDissector.java    From aeron with Apache License 2.0 5 votes vote down vote up
static int dissectLogHeader(
    final String context,
    final Enum<?> code,
    final MutableDirectBuffer buffer,
    final int offset,
    final StringBuilder builder)
{
    int relativeOffset = 0;

    final int captureLength = buffer.getInt(offset + relativeOffset, LITTLE_ENDIAN);
    relativeOffset += SIZE_OF_INT;

    final int bufferLength = buffer.getInt(offset + relativeOffset, LITTLE_ENDIAN);
    relativeOffset += SIZE_OF_INT;

    final long timestampNs = buffer.getLong(offset + relativeOffset, LITTLE_ENDIAN);
    relativeOffset += SIZE_OF_LONG;

    builder
        .append('[')
        .append(((double)timestampNs) / NANOS_PER_SECOND)
        .append("] ")
        .append(context)
        .append(": ")
        .append(code.name())
        .append(" [")
        .append(captureLength)
        .append('/')
        .append(bufferLength)
        .append(']');

    return relativeOffset;
}