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

The following examples show how to use org.agrona.concurrent.UnsafeBuffer#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: ListRecordingsForUriSessionTest.java    From aeron with Apache License 2.0 6 votes vote down vote up
private Answer<Object> verifySendDescriptor(final MutableLong counter)
{
    return (invocation) ->
    {
        final UnsafeBuffer buffer = invocation.getArgument(1);
        recordingDescriptorDecoder.wrap(
            buffer,
            RecordingDescriptorHeaderDecoder.BLOCK_LENGTH,
            RecordingDescriptorDecoder.BLOCK_LENGTH,
            RecordingDescriptorDecoder.SCHEMA_VERSION);

        final int i = counter.intValue();
        assertEquals(matchingRecordingIds[i], recordingDescriptorDecoder.recordingId());
        counter.set(i + 1);

        return buffer.getInt(0);
    };
}
 
Example 2
Source File: ListRecordingsSessionTest.java    From aeron with Apache License 2.0 6 votes vote down vote up
private Answer<Object> verifySendDescriptor(final MutableLong counter)
{
    return (invocation) ->
    {
        final UnsafeBuffer buffer = invocation.getArgument(1);

        recordingDescriptorDecoder.wrap(
            buffer,
            RecordingDescriptorHeaderDecoder.BLOCK_LENGTH,
            RecordingDescriptorDecoder.BLOCK_LENGTH,
            RecordingDescriptorDecoder.SCHEMA_VERSION);

        final int i = counter.intValue();
        assertEquals(recordingIds[i], recordingDescriptorDecoder.recordingId());
        counter.set(i + 1);

        return buffer.getInt(0);
    };
}
 
Example 3
Source File: Image.java    From aeron with Apache License 2.0 5 votes vote down vote up
/**
 * Poll for new messages in a stream. If new messages are found beyond the last consumed position then they
 * will be delivered to the {@link BlockHandler} up to a limited number of bytes.
 * <p>
 * A scan will terminate if a padding frame is encountered. If first frame in a scan is padding then a block
 * for the padding is notified. If the padding comes after the first frame in a scan then the scan terminates
 * at the offset the padding frame begins. Padding frames are delivered singularly in a block.
 * <p>
 * Padding frames may be for a greater range than the limit offset but only the header needs to be valid so
 * relevant length of the frame is {@link io.aeron.protocol.DataHeaderFlyweight#HEADER_LENGTH}.
 *
 * @param handler          to which block is delivered.
 * @param blockLengthLimit up to which a block may be in length.
 * @return the number of bytes that have been consumed.
 */
public int blockPoll(final BlockHandler handler, final int blockLengthLimit)
{
    if (isClosed)
    {
        return 0;
    }

    final long position = subscriberPosition.get();
    final int offset = (int)position & termLengthMask;
    final UnsafeBuffer termBuffer = activeTermBuffer(position);
    final int limitOffset = Math.min(offset + blockLengthLimit, termBuffer.capacity());
    final int resultingOffset = TermBlockScanner.scan(termBuffer, offset, limitOffset);
    final int length = resultingOffset - offset;

    if (resultingOffset > offset)
    {
        try
        {
            final int termId = termBuffer.getInt(offset + TERM_ID_FIELD_OFFSET, LITTLE_ENDIAN);
            handler.onBlock(termBuffer, offset, length, sessionId, termId);
        }
        catch (final Throwable t)
        {
            errorHandler.onError(t);
        }
        finally
        {
            subscriberPosition.setOrdered(position + length);
        }
    }

    return length;
}
 
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: Image.java    From aeron with Apache License 2.0 5 votes vote down vote up
/**
 * Poll for new messages in a stream. If new messages are found beyond the last consumed position then they
 * will be delivered to the {@link RawBlockHandler} up to a limited number of bytes.
 * <p>
 * This method is useful for operations like bulk archiving a stream to file.
 * <p>
 * A scan will terminate if a padding frame is encountered. If first frame in a scan is padding then a block
 * for the padding is notified. If the padding comes after the first frame in a scan then the scan terminates
 * at the offset the padding frame begins. Padding frames are delivered singularly in a block.
 * <p>
 * Padding frames may be for a greater range than the limit offset but only the header needs to be valid so
 * relevant length of the frame is {@link io.aeron.protocol.DataHeaderFlyweight#HEADER_LENGTH}.
 *
 * @param handler          to which block is delivered.
 * @param blockLengthLimit up to which a block may be in length.
 * @return the number of bytes that have been consumed.
 */
public int rawPoll(final RawBlockHandler handler, final int blockLengthLimit)
{
    if (isClosed)
    {
        return 0;
    }

    final long position = subscriberPosition.get();
    final int offset = (int)position & termLengthMask;
    final int activeIndex = indexByPosition(position, positionBitsToShift);
    final UnsafeBuffer termBuffer = termBuffers[activeIndex];
    final int capacity = termBuffer.capacity();
    final int limitOffset = Math.min(offset + blockLengthLimit, capacity);
    final int resultingOffset = TermBlockScanner.scan(termBuffer, offset, limitOffset);
    final int length = resultingOffset - offset;

    if (resultingOffset > offset)
    {
        try
        {
            final long fileOffset = ((long)capacity * activeIndex) + offset;
            final int termId = termBuffer.getInt(offset + TERM_ID_FIELD_OFFSET, LITTLE_ENDIAN);

            handler.onBlock(logBuffers.fileChannel(), fileOffset, termBuffer, offset, length, sessionId, termId);
        }
        catch (final Throwable t)
        {
            errorHandler.onError(t);
        }
        finally
        {
            subscriberPosition.setOrdered(position + length);
        }
    }

    return length;
}
 
Example 6
Source File: RecordingLog.java    From aeron with Apache License 2.0 5 votes vote down vote up
private void captureEntriesFromBuffer(
    final ByteBuffer byteBuffer, final UnsafeBuffer buffer, final ArrayList<Entry> entries)
{
    for (int i = 0, length = byteBuffer.limit(); i < length; i += ENTRY_LENGTH)
    {
        final int entryType = buffer.getInt(i + ENTRY_TYPE_OFFSET);

        if (NULL_VALUE != entryType)
        {
            final int type = entryType & ~ENTRY_TYPE_INVALID_FLAG;
            final boolean isValid = (entryType & ENTRY_TYPE_INVALID_FLAG) == 0;

            final Entry entry = new Entry(
                buffer.getLong(i + RECORDING_ID_OFFSET, LITTLE_ENDIAN),
                buffer.getLong(i + LEADERSHIP_TERM_ID_OFFSET, LITTLE_ENDIAN),
                buffer.getLong(i + TERM_BASE_LOG_POSITION_OFFSET, LITTLE_ENDIAN),
                buffer.getLong(i + LOG_POSITION_OFFSET, LITTLE_ENDIAN),
                buffer.getLong(i + TIMESTAMP_OFFSET, LITTLE_ENDIAN),
                buffer.getInt(i + SERVICE_ID_OFFSET, LITTLE_ENDIAN),
                type,
                isValid,
                nextEntryIndex);

            entries.add(entry);

            if (isValidTerm(entry))
            {
                cacheIndexByLeadershipTermIdMap.put(entry.leadershipTermId, entries.size() - 1);
            }

            if (ENTRY_TYPE_SNAPSHOT == entry.type && !entry.isValid)
            {
                invalidSnapshots.add(entries.size() - 1);
            }
        }

        ++nextEntryIndex;
    }
}
 
Example 7
Source File: Catalog.java    From aeron with Apache License 2.0 4 votes vote down vote up
static int descriptorLength(final UnsafeBuffer descriptorBuffer)
{
    return descriptorBuffer.getInt(RecordingDescriptorHeaderDecoder.lengthEncodingOffset(), BYTE_ORDER);
}
 
Example 8
Source File: DataHeaderFlyweight.java    From aeron with Apache License 2.0 2 votes vote down vote up
/**
 * Is the frame at data frame at the beginning of packet a heartbeat message?
 *
 * @param packet containing the data frame.
 * @param length of the data frame.
 * @return true if a heartbeat otherwise false.
 */
public static boolean isHeartbeat(final UnsafeBuffer packet, final int length)
{
    return length == HEADER_LENGTH && packet.getInt(0) == 0;
}
 
Example 9
Source File: DataHeaderFlyweight.java    From aeron with Apache License 2.0 2 votes vote down vote up
/**
 * Get the session-id field from the header.
 *
 * @param termBuffer  container the header.
 * @param frameOffset in the buffer where the header starts.
 * @return the session-id field from the header.
 */
public static int sessionId(final UnsafeBuffer termBuffer, final int frameOffset)
{
    return termBuffer.getInt(frameOffset + SESSION_ID_FIELD_OFFSET, LITTLE_ENDIAN);
}
 
Example 10
Source File: DataHeaderFlyweight.java    From aeron with Apache License 2.0 2 votes vote down vote up
/**
 * Get the stream-id field from the header.
 *
 * @param termBuffer  container the header.
 * @param frameOffset in the buffer where the header starts.
 * @return the stream-id field from the header.
 */
public static int streamId(final UnsafeBuffer termBuffer, final int frameOffset)
{
    return termBuffer.getInt(frameOffset + STREAM_ID_FIELD_OFFSET, LITTLE_ENDIAN);
}
 
Example 11
Source File: DataHeaderFlyweight.java    From aeron with Apache License 2.0 2 votes vote down vote up
/**
 * Get the term-id field from the header.
 *
 * @param termBuffer  container the header.
 * @param frameOffset in the buffer where the header starts.
 * @return the term-id field from the header.
 */
public static int termId(final UnsafeBuffer termBuffer, final int frameOffset)
{
    return termBuffer.getInt(frameOffset + TERM_ID_FIELD_OFFSET, LITTLE_ENDIAN);
}
 
Example 12
Source File: DataHeaderFlyweight.java    From aeron with Apache License 2.0 2 votes vote down vote up
/**
 * Get the term-offset field from the header.
 *
 * @param termBuffer  container the header.
 * @param frameOffset in the buffer where the header starts.
 * @return the term-offset field from the header.
 */
public static int termOffset(final UnsafeBuffer termBuffer, final int frameOffset)
{
    return termBuffer.getInt(frameOffset + TERM_OFFSET_FIELD_OFFSET, LITTLE_ENDIAN);
}
 
Example 13
Source File: FrameDescriptor.java    From aeron with Apache License 2.0 2 votes vote down vote up
/**
 * Get the length of a frame from the header.
 *
 * @param buffer     containing the frame.
 * @param termOffset at which a frame begins.
 * @return the value for the frame length.
 */
public static int frameLength(final UnsafeBuffer buffer, final int termOffset)
{
    return buffer.getInt(termOffset, LITTLE_ENDIAN);
}
 
Example 14
Source File: FrameDescriptor.java    From aeron with Apache License 2.0 2 votes vote down vote up
/**
 * Get the term id of a frame from the header.
 *
 * @param buffer     containing the frame.
 * @param termOffset at which a frame begins.
 * @return the value for the term id field.
 */
public static int frameTermId(final UnsafeBuffer buffer, final int termOffset)
{
    return buffer.getInt(termIdOffset(termOffset), LITTLE_ENDIAN);
}
 
Example 15
Source File: FrameDescriptor.java    From aeron with Apache License 2.0 2 votes vote down vote up
/**
 * Get the session id of a frame from the header.
 *
 * @param buffer     containing the frame.
 * @param termOffset at which a frame begins.
 * @return the value for the session id field.
 */
public static int frameSessionId(final UnsafeBuffer buffer, final int termOffset)
{
    return buffer.getInt(sessionIdOffset(termOffset), LITTLE_ENDIAN);
}
 
Example 16
Source File: DataHeaderFlyweight.java    From aeron with Apache License 2.0 2 votes vote down vote up
/**
 * Get the fragment length field from the header.
 *
 * @param termBuffer  container the header.
 * @param frameOffset in the buffer where the header starts.
 * @return the fragment length field from the header.
 */
public static int fragmentLength(final UnsafeBuffer termBuffer, final int frameOffset)
{
    return termBuffer.getInt(frameOffset + FRAME_LENGTH_FIELD_OFFSET, LITTLE_ENDIAN);
}
 
Example 17
Source File: LogBufferDescriptor.java    From aeron with Apache License 2.0 2 votes vote down vote up
/**
 * Get the value of the initial Term id used for this log.
 *
 * @param metadataBuffer containing the meta data.
 * @return the value of the initial Term id used for this log.
 */
public static int initialTermId(final UnsafeBuffer metadataBuffer)
{
    return metadataBuffer.getInt(LOG_INITIAL_TERM_ID_OFFSET);
}
 
Example 18
Source File: LogBufferDescriptor.java    From aeron with Apache License 2.0 2 votes vote down vote up
/**
 * Get the value of the MTU length used for this log.
 *
 * @param metadataBuffer containing the meta data.
 * @return the value of the MTU length used for this log.
 */
public static int mtuLength(final UnsafeBuffer metadataBuffer)
{
    return metadataBuffer.getInt(LOG_MTU_LENGTH_OFFSET);
}
 
Example 19
Source File: LogBufferDescriptor.java    From aeron with Apache License 2.0 2 votes vote down vote up
/**
 * Get the value of the Term Length used for this log.
 *
 * @param metadataBuffer containing the meta data.
 * @return the value of the term length used for this log.
 */
public static int termLength(final UnsafeBuffer metadataBuffer)
{
    return metadataBuffer.getInt(LOG_TERM_LENGTH_OFFSET);
}
 
Example 20
Source File: LogBufferDescriptor.java    From aeron with Apache License 2.0 2 votes vote down vote up
/**
 * Get the value of the page size used for this log.
 *
 * @param metadataBuffer containing the meta data.
 * @return the value of the page size used for this log.
 */
public static int pageSize(final UnsafeBuffer metadataBuffer)
{
    return metadataBuffer.getInt(LOG_PAGE_SIZE_OFFSET);
}