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

The following examples show how to use org.agrona.concurrent.AtomicBuffer#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: ClusterCounters.java    From aeron with Apache License 2.0 6 votes vote down vote up
/**
 * Find the counter id for a type of counter in a cluster.
 *
 * @param counters  to search within.
 * @param typeId    of the counter.
 * @param clusterId to which the allocated counter belongs.
 * @return the matching counter id or {@link Aeron#NULL_VALUE} if not found.
 */
public static int find(final CountersReader counters, final int typeId, final int clusterId)
{
    final AtomicBuffer buffer = counters.metaDataBuffer();

    for (int i = 0, size = counters.maxCounterId(); i < size; i++)
    {
        final int recordOffset = CountersReader.metaDataOffset(i);

        if (counters.getCounterState(i) == RECORD_ALLOCATED &&
            counters.getCounterTypeId(i) == typeId &&
            buffer.getInt(recordOffset + KEY_OFFSET) == clusterId)
        {
            return i;
        }
    }

    return Aeron.NULL_VALUE;
}
 
Example 2
Source File: CountersReader.java    From agrona with Apache License 2.0 6 votes vote down vote up
/**
 * Iterate over all the metadata in the buffer.
 *
 * @param metaData function to be called for each metadata record.
 */
public void forEach(final MetaData metaData)
{
    int counterId = 0;

    final AtomicBuffer metaDataBuffer = this.metaDataBuffer;

    for (int i = 0, capacity = metaDataBuffer.capacity(); i < capacity; i += METADATA_LENGTH)
    {
        final int recordStatus = metaDataBuffer.getIntVolatile(i);
        if (RECORD_ALLOCATED == recordStatus)
        {
            final int typeId = metaDataBuffer.getInt(i + TYPE_ID_OFFSET);
            final String label = labelValue(metaDataBuffer, i);
            final DirectBuffer keyBuffer = new UnsafeBuffer(metaDataBuffer, i + KEY_OFFSET, MAX_KEY_LENGTH);

            metaData.accept(counterId, typeId, keyBuffer, label);
        }
        else if (RECORD_UNUSED == recordStatus)
        {
            break;
        }

        counterId++;
    }
}
 
Example 3
Source File: BroadcastReceiver.java    From agrona with Apache License 2.0 5 votes vote down vote up
/**
 * Non-blocking receive of next message from the transmission stream.
 * <p>
 * If loss has occurred then {@link #lappedCount()} will be incremented.
 *
 * @return true if transmission is available with {@link #offset()}, {@link #length()} and {@link #typeId()}
 * set for the next message to be consumed. If no transmission is available then false.
 */
public boolean receiveNext()
{
    boolean isAvailable = false;
    final AtomicBuffer buffer = this.buffer;
    final long tail = buffer.getLongVolatile(tailCounterIndex);
    long cursor = nextRecord;

    if (tail > cursor)
    {
        final int capacity = this.capacity;
        int recordOffset = (int)cursor & (capacity - 1);

        if (!validate(cursor, buffer, capacity))
        {
            lappedCount.lazySet(lappedCount.get() + 1);

            cursor = buffer.getLong(latestCounterIndex);
            recordOffset = (int)cursor & (capacity - 1);
        }

        this.cursor = cursor;
        nextRecord = cursor + align(buffer.getInt(lengthOffset(recordOffset)), RECORD_ALIGNMENT);

        if (PADDING_MSG_TYPE_ID == buffer.getInt(typeOffset(recordOffset)))
        {
            recordOffset = 0;
            this.cursor = nextRecord;
            nextRecord += align(buffer.getInt(lengthOffset(recordOffset)), RECORD_ALIGNMENT);
        }

        this.recordOffset = recordOffset;
        isAvailable = true;
    }

    return isAvailable;
}
 
Example 4
Source File: OneToOneRingBuffer.java    From agrona with Apache License 2.0 5 votes vote down vote up
private int verifyClaimedSpaceNotReleased(final AtomicBuffer buffer, final int recordIndex)
{
    final int recordLength = buffer.getInt(lengthOffset(recordIndex));
    if (recordLength < 0)
    {
        return recordLength;
    }

    throw new IllegalStateException("claimed space previously " +
        (PADDING_MSG_TYPE_ID == buffer.getInt(typeOffset(recordIndex)) ? "aborted" : "committed"));
}
 
Example 5
Source File: ManyToOneRingBuffer.java    From agrona with Apache License 2.0 5 votes vote down vote up
private int verifyClaimedSpaceNotReleased(final AtomicBuffer buffer, final int recordIndex)
{
    final int recordLength = buffer.getInt(lengthOffset(recordIndex));
    if (recordLength < 0)
    {
        return recordLength;
    }

    throw new IllegalStateException("claimed space previously " +
        (PADDING_MSG_TYPE_ID == buffer.getInt(typeOffset(recordIndex)) ? "aborted" : "committed"));
}
 
Example 6
Source File: SequenceNumberIndexWriter.java    From artio with Apache License 2.0 4 votes vote down vote up
private boolean fileHasBeenInitialized(final AtomicBuffer fileBuffer)
{
    return fileBuffer.getShort(0) != 0 || fileBuffer.getInt(FIRST_CHECKSUM_LOCATION) != 0;
}
 
Example 7
Source File: OneToOneRingBuffer.java    From agrona with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public int read(final MessageHandler handler, final int messageCountLimit)
{
    int messagesRead = 0;

    final AtomicBuffer buffer = this.buffer;
    final int headPositionIndex = this.headPositionIndex;
    final long head = buffer.getLong(headPositionIndex);

    int bytesRead = 0;

    final int capacity = this.capacity;
    final int headIndex = (int)head & (capacity - 1);
    final int contiguousBlockLength = capacity - headIndex;

    try
    {
        while ((bytesRead < contiguousBlockLength) && (messagesRead < messageCountLimit))
        {
            final int recordIndex = headIndex + bytesRead;
            final int recordLength = buffer.getIntVolatile(lengthOffset(recordIndex));
            if (recordLength <= 0)
            {
                break;
            }

            bytesRead += align(recordLength, ALIGNMENT);

            final int messageTypeId = buffer.getInt(typeOffset(recordIndex));
            if (PADDING_MSG_TYPE_ID == messageTypeId)
            {
                continue;
            }

            ++messagesRead;
            handler.onMessage(messageTypeId, buffer, recordIndex + HEADER_LENGTH, recordLength - HEADER_LENGTH);
        }
    }
    finally
    {
        if (bytesRead != 0)
        {
            buffer.putLongOrdered(headPositionIndex, head + bytesRead);
        }
    }

    return messagesRead;
}
 
Example 8
Source File: ManyToOneRingBuffer.java    From agrona with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public int read(final MessageHandler handler, final int messageCountLimit)
{
    int messagesRead = 0;

    final AtomicBuffer buffer = this.buffer;
    final int headPositionIndex = this.headPositionIndex;
    final long head = buffer.getLong(headPositionIndex);

    final int capacity = this.capacity;
    final int headIndex = (int)head & (capacity - 1);
    final int maxBlockLength = capacity - headIndex;
    int bytesRead = 0;

    try
    {
        while ((bytesRead < maxBlockLength) && (messagesRead < messageCountLimit))
        {
            final int recordIndex = headIndex + bytesRead;
            final int recordLength = buffer.getIntVolatile(lengthOffset(recordIndex));
            if (recordLength <= 0)
            {
                break;
            }

            bytesRead += align(recordLength, ALIGNMENT);

            final int messageTypeId = buffer.getInt(typeOffset(recordIndex));
            if (PADDING_MSG_TYPE_ID == messageTypeId)
            {
                continue;
            }

            ++messagesRead;
            handler.onMessage(messageTypeId, buffer, recordIndex + HEADER_LENGTH, recordLength - HEADER_LENGTH);
        }
    }
    finally
    {
        if (bytesRead != 0)
        {
            buffer.setMemory(headIndex, bytesRead, (byte)0);
            buffer.putLongOrdered(headPositionIndex, head + bytesRead);
        }
    }

    return messagesRead;
}