org.agrona.DirectBuffer Java Examples

The following examples show how to use org.agrona.DirectBuffer. 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: RecordingPos.java    From aeron with Apache License 2.0 6 votes vote down vote up
/**
 * Find the active counter id for a stream based on the recording id.
 *
 * @param countersReader to search within.
 * @param recordingId    for the active recording.
 * @return the counter id if found otherwise {@link CountersReader#NULL_COUNTER_ID}.
 */
public static int findCounterIdByRecording(final CountersReader countersReader, final long recordingId)
{
    final DirectBuffer buffer = countersReader.metaDataBuffer();

    for (int i = 0, size = countersReader.maxCounterId(); i < size; i++)
    {
        if (countersReader.getCounterState(i) == RECORD_ALLOCATED &&
            countersReader.getCounterTypeId(i) == RECORDING_POSITION_TYPE_ID)
        {
            if (buffer.getLong(CountersReader.metaDataOffset(i) + KEY_OFFSET + RECORDING_ID_OFFSET) == recordingId)
            {
                return i;
            }
        }
    }

    return NULL_COUNTER_ID;
}
 
Example #2
Source File: HeartbeatTimestamp.java    From aeron with Apache License 2.0 6 votes vote down vote up
/**
 * Find the active counter id for a heartbeat timestamp.
 *
 * @param countersReader to search within.
 * @param counterTypeId  to match on.
 * @param registrationId for the active client.
 * @return the counter id if found otherwise {@link CountersReader#NULL_COUNTER_ID}.
 */
public static int findCounterIdByRegistrationId(
    final CountersReader countersReader, final int counterTypeId, final long registrationId)
{
    final DirectBuffer buffer = countersReader.metaDataBuffer();

    for (int i = 0, size = countersReader.maxCounterId(); i < size; i++)
    {
        if (countersReader.getCounterState(i) == RECORD_ALLOCATED &&
            countersReader.getCounterTypeId(i) == counterTypeId)
        {
            final int recordOffset = CountersReader.metaDataOffset(i);

            if (buffer.getLong(recordOffset + KEY_OFFSET + REGISTRATION_ID_OFFSET) == registrationId)
            {
                return i;
            }
        }
    }

    return NULL_COUNTER_ID;
}
 
Example #3
Source File: NDArrayMessageTest.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testNDArrayMessageToAndFrom() {
    NDArrayMessage message = NDArrayMessage.wholeArrayUpdate(Nd4j.scalar(1.0));
    DirectBuffer bufferConvert = NDArrayMessage.toBuffer(message);
    bufferConvert.byteBuffer().rewind();
    NDArrayMessage newMessage = NDArrayMessage.fromBuffer(bufferConvert, 0);
    assertEquals(message, newMessage);

    INDArray compressed = Nd4j.getCompressor().compress(Nd4j.scalar(1.0), "GZIP");
    NDArrayMessage messageCompressed = NDArrayMessage.wholeArrayUpdate(compressed);
    DirectBuffer bufferConvertCompressed = NDArrayMessage.toBuffer(messageCompressed);
    NDArrayMessage newMessageTest = NDArrayMessage.fromBuffer(bufferConvertCompressed, 0);
    assertEquals(messageCompressed, newMessageTest);


}
 
Example #4
Source File: NDArrayMessage.java    From nd4j with Apache License 2.0 6 votes vote down vote up
/**
 * Returns an array of
 * message chunks meant to be sent
 * in parallel.
 * Each message chunk has the layout:
 * messageType
 * number of chunks
 * chunkSize
 * length of uuid
 * uuid
 * buffer index
 * actual raw data
 * @param message the message to turn into chunks
 * @param chunkSize the chunk size
 * @return an array of buffers
 */
public static NDArrayMessageChunk[] chunks(NDArrayMessage message, int chunkSize) {
    int numChunks = numChunksForMessage(message, chunkSize);
    NDArrayMessageChunk[] ret = new NDArrayMessageChunk[numChunks];
    DirectBuffer wholeBuffer = NDArrayMessage.toBuffer(message);
    String messageId = UUID.randomUUID().toString();
    for (int i = 0; i < ret.length; i++) {
        //data: only grab a chunk of the data
        ByteBuffer view = (ByteBuffer) wholeBuffer.byteBuffer().asReadOnlyBuffer().position(i * chunkSize);
        view.limit(Math.min(i * chunkSize + chunkSize, wholeBuffer.capacity()));
        view.order(ByteOrder.nativeOrder());
        view = view.slice();
        NDArrayMessageChunk chunk = NDArrayMessageChunk.builder().id(messageId).chunkSize(chunkSize)
                        .numChunks(numChunks).messageType(MessageType.CHUNKED).chunkIndex(i).data(view).build();
        //insert in to the array itself
        ret[i] = chunk;
    }

    return ret;
}
 
Example #5
Source File: SenderEndPoint.java    From artio with Apache License 2.0 6 votes vote down vote up
Action onSlowOutboundMessage(
    final DirectBuffer directBuffer,
    final int offsetAfterHeader,
    final int length,
    final long position,
    final int bodyLength,
    final int libraryId,
    final long timeInMs,
    final int metaDataLength,
    final int sequenceNumber)
{
    if (isWrongLibraryId(libraryId))
    {
        invalidLibraryAttempts.increment();
        return CONTINUE;
    }

    if (replayPaused)
    {
        return blockPosition(position, length, outboundTracker);
    }

    return attemptSlowMessage(
        directBuffer, offsetAfterHeader, length, position, bodyLength, timeInMs, outboundTracker, metaDataLength,
        sequenceNumber);
}
 
Example #6
Source File: Indexer.java    From artio with Apache License 2.0 6 votes vote down vote up
public Action onFragment(final DirectBuffer buffer, final int offset, final int length, final Header header)
{
    final int streamId = header.streamId();
    final int aeronSessionId = header.sessionId();
    final long endPosition = header.position();
    DebugLogger.log(
        LogTag.INDEX,
        indexingFormatter,
        endPosition,
        streamId,
        aeronSessionId);

    for (int i = 0, size = indices.size(); i < size; i++)
    {
        final Index index = indices.get(i);
        index.onFragment(buffer, offset, length, header);
    }

    return CONTINUE;
}
 
Example #7
Source File: EncoderGenerator.java    From artio with Apache License 2.0 6 votes vote down vote up
protected void generateAggregateFile(final Aggregate aggregate, final AggregateType aggregateType)
{
    final String className = encoderClassName(aggregate.name());

    outputManager.withOutput(
        className,
        (out) ->
        {
            out.append(fileHeader(thisPackage));

            if (REQUIRED_SESSION_CODECS.contains(className))
            {
                out.append(importFor("uk.co.real_logic.artio.builder.Abstract" + className));
            }

            generateImports(
                "Encoder",
                aggregateType,
                out,
                DirectBuffer.class,
                MutableDirectBuffer.class,
                UnsafeBuffer.class,
                AsciiSequenceView.class);
            generateAggregateClass(aggregate, aggregateType, className, out);
        });
}
 
Example #8
Source File: NDArrayMessage.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
/**
 * Returns an array of
 * message chunks meant to be sent
 * in parallel.
 * Each message chunk has the layout:
 * messageType
 * number of chunks
 * chunkSize
 * length of uuid
 * uuid
 * buffer index
 * actual raw data
 * @param message the message to turn into chunks
 * @param chunkSize the chunk size
 * @return an array of buffers
 */
public static NDArrayMessageChunk[] chunks(NDArrayMessage message, int chunkSize) {
    int numChunks = numChunksForMessage(message, chunkSize);
    NDArrayMessageChunk[] ret = new NDArrayMessageChunk[numChunks];
    DirectBuffer wholeBuffer = NDArrayMessage.toBuffer(message);
    String messageId = UUID.randomUUID().toString();
    for (int i = 0; i < ret.length; i++) {
        //data: only grab a chunk of the data
        ByteBuffer view = (ByteBuffer) wholeBuffer.byteBuffer().asReadOnlyBuffer().position(i * chunkSize);
        view.limit(Math.min(i * chunkSize + chunkSize, wholeBuffer.capacity()));
        view.order(ByteOrder.nativeOrder());
        view = view.slice();
        NDArrayMessageChunk chunk = NDArrayMessageChunk.builder().id(messageId).chunkSize(chunkSize)
                        .numChunks(numChunks).messageType(MessageType.CHUNKED).chunkIndex(i).data(view).build();
        //insert in to the array itself
        ret[i] = chunk;
    }

    return ret;
}
 
Example #9
Source File: JsonTokenListener.java    From simple-binary-encoding with Apache License 2.0 6 votes vote down vote up
public void onVarData(
    final Token fieldToken,
    final DirectBuffer buffer,
    final int bufferIndex,
    final int length,
    final Token typeToken)
{
    try
    {
        property(fieldToken.name());
        doubleQuote();

        final byte[] tempBuffer = new byte[length];
        buffer.getBytes(bufferIndex, tempBuffer, 0, length);
        final String str = new String(tempBuffer, 0, length, typeToken.encoding().characterEncoding());

        escape(str);

        doubleQuote();
        next();
    }
    catch (final UnsupportedEncodingException ex)
    {
        ex.printStackTrace();
    }
}
 
Example #10
Source File: SenderEndPoint.java    From artio with Apache License 2.0 6 votes vote down vote up
Action onReplayMessage(
    final DirectBuffer directBuffer,
    final int offset,
    final int bodyLength,
    final long timeInMs,
    final long position)
{
    if (!isSlowConsumer())
    {
        replayPaused = true;
    }

    attemptFramedMessage(directBuffer, offset, bodyLength, timeInMs, position, replayTracker);

    return CONTINUE;
}
 
Example #11
Source File: MultiSubscriberTest.java    From aeron with Apache License 2.0 6 votes vote down vote up
private void verifyData(final UnsafeBuffer srcBuffer, final FragmentHandler mockFragmentHandler)
{
    final ArgumentCaptor<DirectBuffer> bufferArg = ArgumentCaptor.forClass(DirectBuffer.class);
    final ArgumentCaptor<Integer> offsetArg = ArgumentCaptor.forClass(Integer.class);

    verify(mockFragmentHandler, times(1)).onFragment(
        bufferArg.capture(), offsetArg.capture(), eq(srcBuffer.capacity()), any(Header.class));

    final DirectBuffer capturedBuffer = bufferArg.getValue();
    final int offset = offsetArg.getValue();
    for (int i = 0; i < srcBuffer.capacity(); i++)
    {
        final int index = offset + i;
        assertEquals(srcBuffer.getByte(i), capturedBuffer.getByte(index), "same at " + index);
    }
}
 
Example #12
Source File: UserRequestExtractor.java    From artio with Apache License 2.0 6 votes vote down vote up
void onUserRequest(
    final DirectBuffer buffer,
    final int offset,
    final int length,
    final AuthenticationStrategy authenticationStrategy,
    final long connectionId,
    final long sessionId)
{
    if (userRequest == null)
    {
        errorHandler.onError(new IllegalStateException(String.format(
            "Received User Request message despite there being no user request message type defined " +
            "in dictionary (dict=%s, conn=%d, sess=%d)",
            dictionary.getClass().getName(),
            connectionId,
            sessionId)));

        return;
    }

    asciiBuffer.wrap(buffer);
    userRequest.reset();
    userRequest.decode(asciiBuffer, offset, length);

    authenticationStrategy.onUserRequest(userRequest, sessionId);
}
 
Example #13
Source File: LibraryProtocolSubscription.java    From artio with Apache License 2.0 6 votes vote down vote up
private Action onWriteMetaDataReply(
    final DirectBuffer buffer, final int offset, final int blockLength, final int version)
{
    writeMetaDataReply.wrap(buffer, offset, blockLength, version);
    final int libraryId = writeMetaDataReply.libraryId();
    final Action action = handler.onApplicationHeartbeat(libraryId);
    if (action == ABORT)
    {
        return action;
    }

    return handler.onWriteMetaDataReply(
        libraryId,
        writeMetaDataReply.replyToId(),
        writeMetaDataReply.status());
}
 
Example #14
Source File: UpdateDecoder.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
public void wrap(final UpdateDecoder parentMessage, final DirectBuffer buffer) {
    this.parentMessage = parentMessage;
    this.buffer = buffer;
    dimensions.wrap(buffer, parentMessage.limit());
    blockLength = dimensions.blockLength();
    count = dimensions.numInGroup();
    index = -1;
    parentMessage.limit(parentMessage.limit() + HEADER_SIZE);
}
 
Example #15
Source File: CountersManager.java    From agrona with Apache License 2.0 5 votes vote down vote up
/**
 * Set an {@link AtomicCounter} key based on counterId, copying the key metadata from the supplied buffer.
 *
 * @param id        to be set
 * @param keyBuffer containing the updated key
 * @param offset    offset into buffer
 * @param length    length of data to copy
 */
public void setCounterKey(final int id, final DirectBuffer keyBuffer, final int offset, final int length)
{
    if (length > MAX_KEY_LENGTH)
    {
        throw new IllegalArgumentException("Supplied key is too long: " + length + ", max: " + MAX_KEY_LENGTH);
    }

    metaDataBuffer.putBytes(metaDataOffset(id) + KEY_OFFSET, keyBuffer, offset, length);
}
 
Example #16
Source File: ExampleTokenListener.java    From simple-binary-encoding with Apache License 2.0 5 votes vote down vote up
public void onEncoding(
    final Token fieldToken,
    final DirectBuffer buffer,
    final int index,
    final Token typeToken,
    final int actingVersion)
{
    final CharSequence value = readEncodingAsString(buffer, index, typeToken, actingVersion);

    printScope();
    out.append(compositeLevel > 0 ? typeToken.name() : fieldToken.name())
        .append('=')
        .append(value)
        .println();
}
 
Example #17
Source File: ImageTest.java    From aeron with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldUpdatePositionOnRethrownExceptionInControlledPoll()
{
    final long initialPosition = computePosition(INITIAL_TERM_ID, 0, POSITION_BITS_TO_SHIFT, INITIAL_TERM_ID);
    position.setOrdered(initialPosition);
    final Image image = createImage();

    insertDataFrame(INITIAL_TERM_ID, offsetForFrame(0));

    when(mockControlledFragmentHandler.onFragment(any(DirectBuffer.class), anyInt(), anyInt(), any(Header.class)))
        .thenThrow(new RuntimeException());

    doThrow(new RuntimeException()).when(errorHandler).onError(any());

    boolean thrown = false;
    try
    {
        image.controlledPoll(mockControlledFragmentHandler, Integer.MAX_VALUE);
    }
    catch (final Exception ignore)
    {
        thrown = true;
    }

    assertTrue(thrown);
    assertThat(image.position(), is(initialPosition + ALIGNED_FRAME_LENGTH));

    verify(mockControlledFragmentHandler).onFragment(
        any(UnsafeBuffer.class), eq(HEADER_LENGTH), eq(DATA.length), any(Header.class));
}
 
Example #18
Source File: DebugLogger.java    From artio with Apache License 2.0 5 votes vote down vote up
public static void log(
    final LogTag tag,
    final String prefixString,
    final DirectBuffer buffer,
    final int offset,
    final int length)
{
    if (isEnabled(tag))
    {
        THREAD_LOCAL.get().log(tag, prefixString, buffer, offset, length);
    }
}
 
Example #19
Source File: ReplayIndex.java    From artio with Apache License 2.0 5 votes vote down vote up
public void onFragment(
    final DirectBuffer buffer,
    final int offset,
    final int length,
    final Header header)
{
    final int streamId = header.streamId();
    if (streamId == requiredStreamId)
    {
        onFragment(buffer, offset, length, header, NULL_RECORDING_ID);
    }
}
 
Example #20
Source File: UpdateEncoder.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
public UpdateEncoder putDataSetMetaDataClassName(final DirectBuffer src, final int srcOffset, final int length) {
    if (length > 1073741824) {
        throw new IllegalArgumentException("length > max value for type: " + length);
    }

    final int headerLength = 4;
    final int limit = parentMessage.limit();
    parentMessage.limit(limit + headerLength + length);
    buffer.putInt(limit, (int) length, java.nio.ByteOrder.LITTLE_ENDIAN);
    buffer.putBytes(limit + headerLength, src, srcOffset, length);

    return this;
}
 
Example #21
Source File: DirectBufferProxy.java    From lmdbjava with Apache License 2.0 5 votes vote down vote up
@Override
protected void in(final DirectBuffer buffer, final Pointer ptr,
                  final long ptrAddr) {
  final long addr = buffer.addressOffset();
  final long size = buffer.capacity();
  UNSAFE.putLong(ptrAddr + STRUCT_FIELD_OFFSET_DATA, addr);
  UNSAFE.putLong(ptrAddr + STRUCT_FIELD_OFFSET_SIZE, size);
}
 
Example #22
Source File: StaticInfoEncoder.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
public StaticInfoEncoder putSwNd4jBackendClass(final DirectBuffer src, final int srcOffset, final int length) {
    if (length > 1073741824) {
        throw new IllegalArgumentException("length > max value for type: " + length);
    }

    final int headerLength = 4;
    final int limit = parentMessage.limit();
    parentMessage.limit(limit + headerLength + length);
    buffer.putInt(limit, (int) length, java.nio.ByteOrder.LITTLE_ENDIAN);
    buffer.putBytes(limit + headerLength, src, srcOffset, length);

    return this;
}
 
Example #23
Source File: SettlPriceTypeDecoder.java    From java-cme-mdp3-handler with GNU Lesser General Public License v3.0 5 votes vote down vote up
public SettlPriceTypeDecoder wrap(final DirectBuffer buffer, final int offset)
{
    this.buffer = buffer;
    this.offset = offset;

    return this;
}
 
Example #24
Source File: FrameCodecDecoder.java    From simple-binary-encoding with Apache License 2.0 5 votes vote down vote up
public void wrapPackageName(final DirectBuffer wrapBuffer)
{
    final int headerLength = 2;
    final int limit = parentMessage.limit();
    final int dataLength = (buffer.getShort(limit, java.nio.ByteOrder.LITTLE_ENDIAN) & 0xFFFF);
    parentMessage.limit(limit + headerLength + dataLength);
    wrapBuffer.wrap(buffer, limit + headerLength, dataLength);
}
 
Example #25
Source File: FixArchivePrinter.java    From artio with Apache License 2.0 5 votes vote down vote up
private static void print(
    final FixMessageDecoder message,
    final DirectBuffer buffer,
    final int offset,
    final int length,
    final Header header)
{
    System.out.println(message.body());
}
 
Example #26
Source File: TokenCodecEncoder.java    From simple-binary-encoding with Apache License 2.0 5 votes vote down vote up
public TokenCodecEncoder putEpoch(final DirectBuffer src, final int srcOffset, final int length)
{
    if (length > 65534)
    {
        throw new IllegalStateException("length > maxValue for type: " + length);
    }

    final int headerLength = 2;
    final int limit = parentMessage.limit();
    parentMessage.limit(limit + headerLength + length);
    buffer.putShort(limit, (short)length, java.nio.ByteOrder.LITTLE_ENDIAN);
    buffer.putBytes(limit + headerLength, src, srcOffset, length);

    return this;
}
 
Example #27
Source File: MDIncrementalRefreshSessionStatistics35Decoder.java    From java-cme-mdp3-handler with GNU Lesser General Public License v3.0 5 votes vote down vote up
public MDIncrementalRefreshSessionStatistics35Decoder wrap(
    final DirectBuffer buffer, final int offset, final int actingBlockLength, final int actingVersion)
{
    this.buffer = buffer;
    this.offset = offset;
    this.actingBlockLength = actingBlockLength;
    this.actingVersion = actingVersion;
    limit(offset + actingBlockLength);

    return this;
}
 
Example #28
Source File: GatewayPublication.java    From artio with Apache License 2.0 5 votes vote down vote up
public long saveWriteMetaData(
    final int libraryId,
    final long sessionId,
    final int metaDataOffset,
    final long correlationId,
    final DirectBuffer srcBuffer,
    final int srcOffset,
    final int srcLength)
{
    final long position = claim(WRITE_META_DATA_LENGTH + srcLength);
    if (position < 0)
    {
        return position;
    }

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

    writeMetaData
        .wrapAndApplyHeader(buffer, offset, header)
        .libraryId(libraryId)
        .session(sessionId)
        .correlationId(correlationId)
        .metaDataOffset(metaDataOffset)
        .putMetaData(srcBuffer, srcOffset, srcLength);

    bufferClaim.commit();

    logSbeMessage(GATEWAY_MESSAGE, writeMetaData);

    return position;
}
 
Example #29
Source File: MDInstrumentDefinitionOption41Decoder.java    From java-cme-mdp3-handler with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void wrap(
    final MDInstrumentDefinitionOption41Decoder parentMessage, final DirectBuffer buffer)
{
    this.parentMessage = parentMessage;
    this.buffer = buffer;
    dimensions.wrap(buffer, parentMessage.limit());
    blockLength = dimensions.blockLength();
    count = dimensions.numInGroup();
    index = -1;
    parentMessage.limit(parentMessage.limit() + HEADER_SIZE);
}
 
Example #30
Source File: OtfParser.java    From artio with Apache License 2.0 5 votes vote down vote up
public void onMessage(final DirectBuffer buffer, final int offset, final int length)
{
    string.wrap(buffer);
    if (acceptor.onNext() == STOP)
    {
        return;
    }

    tag = UNKNOWN;
    this.messageType = UNKNOWN;

    checksum = NO_CHECKSUM;
    checksumOffset = 0;

    try
    {
        if (parseFields(offset, offset + length, UNKNOWN, null, 0) < 0)
        {
            return;
        }

        if (validChecksum(offset, checksum))
        {
            acceptor.onComplete();
        }
        else
        {
            invalidChecksum(this.messageType);
        }
    }
    catch (final NumberFormatException ex)
    {
        parseError(this.messageType, tag);
    }
}