当前位置: 首页>>代码示例>>Java>>正文


Java UnsafeBuffer.setMemory方法代码示例

本文整理汇总了Java中org.agrona.concurrent.UnsafeBuffer.setMemory方法的典型用法代码示例。如果您正苦于以下问题:Java UnsafeBuffer.setMemory方法的具体用法?Java UnsafeBuffer.setMemory怎么用?Java UnsafeBuffer.setMemory使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.agrona.concurrent.UnsafeBuffer的用法示例。


在下文中一共展示了UnsafeBuffer.setMemory方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: shouldFreeSessionBuffer

import org.agrona.concurrent.UnsafeBuffer; //导入方法依赖的package包/类
@Test
public void shouldFreeSessionBuffer()
{
    when(header.flags())
        .thenReturn(FrameDescriptor.BEGIN_FRAG_FLAG)
        .thenReturn(FrameDescriptor.END_FRAG_FLAG);

    final UnsafeBuffer srcBuffer = new UnsafeBuffer(new byte[1024]);
    final int offset = 0;
    final int length = srcBuffer.capacity() / 2;

    srcBuffer.setMemory(0, length, (byte)65);
    srcBuffer.setMemory(length, length, (byte)66);

    assertFalse(adapter.freeSessionBuffer(SESSION_ID));

    adapter.onFragment(srcBuffer, offset, length, header);
    adapter.onFragment(srcBuffer, length, length, header);

    assertTrue(adapter.freeSessionBuffer(SESSION_ID));
    assertFalse(adapter.freeSessionBuffer(SESSION_ID));
}
 
开发者ID:real-logic,项目名称:aeron,代码行数:23,代码来源:FragmentAssemblerTest.java

示例2: cleanBuffer

import org.agrona.concurrent.UnsafeBuffer; //导入方法依赖的package包/类
private void cleanBuffer(final long publisherLimit)
{
    final long cleanPosition = this.cleanPosition;
    final long dirtyRange = publisherLimit - cleanPosition;
    final int bufferCapacity = termBufferLength;
    final int reservedRange = bufferCapacity * 2;

    if (dirtyRange > reservedRange)
    {
        final UnsafeBuffer dirtyTerm = termBuffers[indexByPosition(cleanPosition, positionBitsToShift)];
        final int termOffset = (int)cleanPosition & termLengthMask;
        final int bytesForCleaning = (int)(dirtyRange - reservedRange);
        final int length = Math.min(bytesForCleaning, bufferCapacity - termOffset);

        dirtyTerm.setMemory(termOffset, length, (byte)0);
        this.cleanPosition = cleanPosition + length;
    }
}
 
开发者ID:real-logic,项目名称:aeron,代码行数:19,代码来源:NetworkPublication.java

示例3: recordFragment

import org.agrona.concurrent.UnsafeBuffer; //导入方法依赖的package包/类
private void recordFragment(
    final RecordingWriter recordingWriter,
    final UnsafeBuffer buffer,
    final DataHeaderFlyweight headerFlyweight,
    final Header header,
    final int message,
    final byte flags,
    final int type)
{
    final int offset = INITIAL_TERM_OFFSET + message * FRAME_LENGTH;
    headerFlyweight.wrap(buffer, offset, HEADER_LENGTH);
    headerFlyweight
        .streamId(STREAM_ID)
        .sessionId(SESSION_ID)
        .termOffset(offset)
        .termId(INITIAL_TERM_ID)
        .reservedValue(message)
        .headerType(type)
        .flags(flags)
        .frameLength(FRAME_LENGTH);

    buffer.setMemory(
        offset + HEADER_LENGTH,
        FRAME_LENGTH - HEADER_LENGTH,
        (byte)message);

    header.offset(offset);

    recordingWriter.writeFragment(buffer, header);
}
 
开发者ID:real-logic,项目名称:aeron,代码行数:31,代码来源:ReplaySessionTest.java

示例4: shouldAssembleTwoPartMessage

import org.agrona.concurrent.UnsafeBuffer; //导入方法依赖的package包/类
@Test
public void shouldAssembleTwoPartMessage()
{
    when(header.flags())
        .thenReturn(FrameDescriptor.BEGIN_FRAG_FLAG)
        .thenReturn(FrameDescriptor.END_FRAG_FLAG);

    final UnsafeBuffer srcBuffer = new UnsafeBuffer(new byte[1024]);
    final int offset = 0;
    final int length = srcBuffer.capacity() / 2;

    srcBuffer.setMemory(0, length, (byte)65);
    srcBuffer.setMemory(length, length, (byte)66);

    adapter.onFragment(srcBuffer, offset, length, header);
    adapter.onFragment(srcBuffer, length, length, header);

    final ArgumentCaptor<UnsafeBuffer> bufferArg = ArgumentCaptor.forClass(UnsafeBuffer.class);
    final ArgumentCaptor<Header> headerArg = ArgumentCaptor.forClass(Header.class);

    verify(delegateFragmentHandler, times(1)).onFragment(
        bufferArg.capture(), eq(offset), eq(length * 2), headerArg.capture());

    final UnsafeBuffer capturedBuffer = bufferArg.getValue();
    for (int i = 0; i < srcBuffer.capacity(); i++)
    {
        assertThat("same at i=" + i, capturedBuffer.getByte(i), is(srcBuffer.getByte(i)));
    }

    final Header capturedHeader = headerArg.getValue();
    assertThat(capturedHeader.sessionId(), is(SESSION_ID));
    assertThat(capturedHeader.flags(), is(FrameDescriptor.END_FRAG_FLAG));
}
 
开发者ID:real-logic,项目名称:aeron,代码行数:34,代码来源:FragmentAssemblerTest.java

示例5: shouldAssembleFourPartMessage

import org.agrona.concurrent.UnsafeBuffer; //导入方法依赖的package包/类
@Test
public void shouldAssembleFourPartMessage()
{
    when(header.flags())
        .thenReturn(FrameDescriptor.BEGIN_FRAG_FLAG)
        .thenReturn((byte)0)
        .thenReturn((byte)0)
        .thenReturn(FrameDescriptor.END_FRAG_FLAG);

    final UnsafeBuffer srcBuffer = new UnsafeBuffer(new byte[1024]);
    final int offset = 0;
    final int length = srcBuffer.capacity() / 4;

    for (int i = 0; i < 4; i++)
    {
        srcBuffer.setMemory(i * length, length, (byte)(65 + i));
    }

    adapter.onFragment(srcBuffer, offset, length, header);
    adapter.onFragment(srcBuffer, offset + length, length, header);
    adapter.onFragment(srcBuffer, offset + (length * 2), length, header);
    adapter.onFragment(srcBuffer, offset + (length * 3), length, header);

    final ArgumentCaptor<UnsafeBuffer> bufferArg = ArgumentCaptor.forClass(UnsafeBuffer.class);
    final ArgumentCaptor<Header> headerArg = ArgumentCaptor.forClass(Header.class);

    verify(delegateFragmentHandler, times(1)).onFragment(
        bufferArg.capture(), eq(offset), eq(length * 4), headerArg.capture());

    final UnsafeBuffer capturedBuffer = bufferArg.getValue();
    for (int i = 0; i < srcBuffer.capacity(); i++)
    {
        assertThat("same at i=" + i, capturedBuffer.getByte(i), is(srcBuffer.getByte(i)));
    }

    final Header capturedHeader = headerArg.getValue();
    assertThat(capturedHeader.sessionId(), is(SESSION_ID));
    assertThat(capturedHeader.flags(), is(FrameDescriptor.END_FRAG_FLAG));
}
 
开发者ID:real-logic,项目名称:aeron,代码行数:40,代码来源:FragmentAssemblerTest.java

示例6: shouldAppendUnfragmentedFromVectorsToEmptyLog

import org.agrona.concurrent.UnsafeBuffer; //导入方法依赖的package包/类
@Test
public void shouldAppendUnfragmentedFromVectorsToEmptyLog()
{
    final int headerLength = DEFAULT_HEADER.capacity();
    final UnsafeBuffer bufferOne = new UnsafeBuffer(new byte[64]);
    final UnsafeBuffer bufferTwo = new UnsafeBuffer(new byte[256]);
    bufferOne.setMemory(0, bufferOne.capacity(), (byte)'1');
    bufferTwo.setMemory(0, bufferTwo.capacity(), (byte)'2');
    final int msgLength = bufferOne.capacity() + 200;
    final int frameLength = msgLength + headerLength;
    final int alignedFrameLength = align(frameLength, FRAME_ALIGNMENT);
    final int tail = 0;

    logMetaDataBuffer.putLong(TERM_TAIL_COUNTER_OFFSET, packTail(TERM_ID, tail));

    final DirectBufferVector[] vectors = new DirectBufferVector[]
    {
        new DirectBufferVector(bufferOne, 0, bufferOne.capacity()),
        new DirectBufferVector(bufferTwo, 0, 200)
    };

    assertThat(termAppender.appendUnfragmentedMessage(headerWriter, vectors, msgLength, RVS, TERM_ID),
        is(alignedFrameLength));

    assertThat(rawTailVolatile(logMetaDataBuffer, PARTITION_INDEX),
        is(packTail(TERM_ID, tail + alignedFrameLength)));

    final InOrder inOrder = inOrder(termBuffer, headerWriter);
    inOrder.verify(headerWriter, times(1)).write(termBuffer, tail, frameLength, TERM_ID);
    inOrder.verify(termBuffer, times(1)).putBytes(headerLength, bufferOne, 0, bufferOne.capacity());
    inOrder.verify(termBuffer, times(1)).putBytes(headerLength + bufferOne.capacity(), bufferTwo, 0, 200);
    inOrder.verify(termBuffer, times(1)).putLong(tail + RESERVED_VALUE_OFFSET, RV, LITTLE_ENDIAN);
    inOrder.verify(termBuffer, times(1)).putIntOrdered(tail, frameLength);
}
 
开发者ID:real-logic,项目名称:aeron,代码行数:35,代码来源:TermAppenderTest.java

示例7: cleanBuffer

import org.agrona.concurrent.UnsafeBuffer; //导入方法依赖的package包/类
private void cleanBuffer(final long minConsumerPosition)
{
    final long cleanPosition = this.cleanPosition;
    final UnsafeBuffer dirtyTerm = termBuffers[indexByPosition(cleanPosition, positionBitsToShift)];
    final int bytesForCleaning = (int)(minConsumerPosition - cleanPosition);
    final int bufferCapacity = termBufferLength;
    final int termOffset = (int)cleanPosition & (bufferCapacity - 1);
    final int length = Math.min(bytesForCleaning, bufferCapacity - termOffset);

    if (length > 0)
    {
        dirtyTerm.setMemory(termOffset, length, (byte)0);
        this.cleanPosition = cleanPosition + length;
    }
}
 
开发者ID:real-logic,项目名称:aeron,代码行数:16,代码来源:IpcPublication.java

示例8: cleanBufferTo

import org.agrona.concurrent.UnsafeBuffer; //导入方法依赖的package包/类
private void cleanBufferTo(final long newCleanPosition)
{
    final long cleanPosition = this.cleanPosition;
    final int bytesForCleaning = (int)(newCleanPosition - cleanPosition);
    final UnsafeBuffer dirtyTerm = termBuffers[indexByPosition(cleanPosition, positionBitsToShift)];
    final int termOffset = (int)cleanPosition & termLengthMask;
    final int length = Math.min(bytesForCleaning, dirtyTerm.capacity() - termOffset);

    if (length > 0)
    {
        dirtyTerm.setMemory(termOffset, length, (byte)0);
        this.cleanPosition = cleanPosition + length;
    }
}
 
开发者ID:real-logic,项目名称:aeron,代码行数:15,代码来源:PublicationImage.java

示例9: shouldUnblockNonCommittedMessage

import org.agrona.concurrent.UnsafeBuffer; //导入方法依赖的package包/类
@Theory
@Test(timeout = 10000)
public void shouldUnblockNonCommittedMessage(final String channel)
{
    final FragmentHandler mockFragmentHandler = mock(FragmentHandler.class);
    final MediaDriver.Context ctx = new MediaDriver.Context()
        .threadingMode(ThreadingMode.SHARED)
        .errorHandler(Throwable::printStackTrace)
        .timerIntervalNs(TimeUnit.MILLISECONDS.toNanos(100))
        .publicationUnblockTimeoutNs(TimeUnit.MILLISECONDS.toNanos(10));

    try (MediaDriver ignore = MediaDriver.launch(ctx);
        Aeron client = Aeron.connect(new Aeron.Context());
        Publication publicationA = client.addPublication(channel, STREAM_ID);
        Publication publicationB = client.addPublication(channel, STREAM_ID);
        Subscription subscription = client.addSubscription(channel, STREAM_ID))
    {
        final UnsafeBuffer srcBuffer = new UnsafeBuffer(new byte[ctx.mtuLength()]);
        final int length = 128;
        final BufferClaim bufferClaim = new BufferClaim();

        srcBuffer.setMemory(0, length, (byte)66);

        while (publicationA.tryClaim(length, bufferClaim) < 0L)
        {
            Thread.yield();
        }

        bufferClaim.buffer().setMemory(bufferClaim.offset(), length, (byte)65);
        bufferClaim.commit();

        while (publicationB.offer(srcBuffer, 0, length) < 0L)
        {
            Thread.yield();
        }

        while (publicationA.tryClaim(length, bufferClaim) < 0L)
        {
            Thread.yield();
        }

        // no commit of publicationA

        while (publicationB.offer(srcBuffer, 0, length) < 0L)
        {
            Thread.yield();
        }

        final int expectedFragments = 3;
        int numFragments = 0;
        do
        {
            final int fragments = subscription.poll(mockFragmentHandler, FRAGMENT_COUNT_LIMIT);
            if (numFragments == 0)
            {
                Thread.yield();
            }

            numFragments += fragments;
        }
        while (numFragments < expectedFragments);

        assertThat(numFragments, is(3));
    }
    finally
    {
        ctx.deleteAeronDirectory();
    }
}
 
开发者ID:real-logic,项目名称:aeron,代码行数:70,代码来源:PublicationUnblockTest.java

示例10: shouldReceivePublishedMessage

import org.agrona.concurrent.UnsafeBuffer; //导入方法依赖的package包/类
@Test(timeout = 10000)
public void shouldReceivePublishedMessage()
{
    final MediaDriver.Context ctx = new MediaDriver.Context()
        .errorHandler(Throwable::printStackTrace)
        .threadingMode(ThreadingMode.SHARED);

    try (MediaDriver ignore = MediaDriver.launch(ctx);
        Aeron aeron = Aeron.connect();
        Publication publication = aeron.addPublication(CHANNEL, STREAM_ID);
        Subscription subscription = aeron.addSubscription(CHANNEL, STREAM_ID))
    {
        final UnsafeBuffer srcBuffer = new UnsafeBuffer(new byte[PAYLOAD_LENGTH * 4]);

        for (int i = 0; i < 4; i++)
        {
            srcBuffer.setMemory(i * PAYLOAD_LENGTH, PAYLOAD_LENGTH, (byte)(65 + i));
        }

        for (int i = 0; i < 4; i++)
        {
            while (publication.offer(srcBuffer, i * PAYLOAD_LENGTH, PAYLOAD_LENGTH) < 0L)
            {
                Thread.yield();
            }
        }

        final FragmentCollector fragmentCollector = new FragmentCollector();
        int numFragments = 0;
        do
        {
            numFragments += subscription.controlledPoll(fragmentCollector, FRAGMENT_COUNT_LIMIT);
        }
        while (numFragments < 4);

        final UnsafeBuffer collectedBuffer = fragmentCollector.collectedBuffer();

        for (int i = 0; i < srcBuffer.capacity(); i++)
        {
            assertThat("same at i=" + i, collectedBuffer.getByte(i), is(srcBuffer.getByte(i)));
        }
    }
    finally
    {
        ctx.deleteAeronDirectory();
    }
}
 
开发者ID:real-logic,项目名称:aeron,代码行数:48,代码来源:ControlledMessageTest.java

示例11: shouldReceivePublishedMessage

import org.agrona.concurrent.UnsafeBuffer; //导入方法依赖的package包/类
@Theory
@Test(timeout = 10000)
public void shouldReceivePublishedMessage(final String channel)
{
    final FragmentAssembler assembler = new FragmentAssembler(mockFragmentHandler);
    final MediaDriver.Context ctx = new MediaDriver.Context()
        .aeronDirectoryName(CommonContext.generateRandomDirName())
        .errorHandler(Throwable::printStackTrace)
        .threadingMode(ThreadingMode.SHARED);

    try (MediaDriver ignore = MediaDriver.launch(ctx);
        Aeron aeron = Aeron.connect(new Aeron.Context().aeronDirectoryName(ctx.aeronDirectoryName()));
        Publication publication = aeron.addPublication(channel, STREAM_ID);
        Subscription subscription = aeron.addSubscription(channel, STREAM_ID))
    {
        final UnsafeBuffer srcBuffer = new UnsafeBuffer(new byte[ctx.mtuLength() * 4]);
        final int offset = 0;
        final int length = srcBuffer.capacity() / 4;

        for (int i = 0; i < 4; i++)
        {
            srcBuffer.setMemory(i * length, length, (byte)(65 + i));
        }

        while (publication.offer(srcBuffer, offset, srcBuffer.capacity()) < 0L)
        {
            Thread.yield();
        }

        final int expectedFragmentsBecauseOfHeader = 5;
        int numFragments = 0;
        do
        {
            numFragments += subscription.poll(assembler, FRAGMENT_COUNT_LIMIT);
        }
        while (numFragments < expectedFragmentsBecauseOfHeader);

        final ArgumentCaptor<DirectBuffer> bufferArg = ArgumentCaptor.forClass(DirectBuffer.class);
        final ArgumentCaptor<Header> headerArg = ArgumentCaptor.forClass(Header.class);

        verify(mockFragmentHandler, times(1)).onFragment(
            bufferArg.capture(), eq(offset), eq(srcBuffer.capacity()), headerArg.capture());

        final DirectBuffer capturedBuffer = bufferArg.getValue();
        for (int i = 0; i < srcBuffer.capacity(); i++)
        {
            assertThat("same at i=" + i, capturedBuffer.getByte(i), is(srcBuffer.getByte(i)));
        }

        assertThat(headerArg.getValue().flags(), is(END_FRAG_FLAG));
    }
    finally
    {
        ctx.deleteAeronDirectory();
    }
}
 
开发者ID:real-logic,项目名称:aeron,代码行数:57,代码来源:FragmentedMessageTest.java

示例12: shouldReceivePublishedMessage

import org.agrona.concurrent.UnsafeBuffer; //导入方法依赖的package包/类
@Theory
@Test(timeout = 10000)
public void shouldReceivePublishedMessage(final String channel)
{
    final MediaDriver.Context ctx = new MediaDriver.Context()
        .errorHandler(Throwable::printStackTrace);

    try (MediaDriver ignore = MediaDriver.launch(ctx);
        Aeron aeron = Aeron.connect();
        Publication publication = aeron.addPublication(channel, STREAM_ID);
        Subscription subscription = aeron.addSubscription(channel, STREAM_ID);
        Subscription spy = aeron.addSubscription(spyForChannel(channel), STREAM_ID))
    {
        final UnsafeBuffer srcBuffer = new UnsafeBuffer(new byte[PAYLOAD_LENGTH * 4]);

        for (int i = 0; i < 4; i++)
        {
            srcBuffer.setMemory(i * PAYLOAD_LENGTH, PAYLOAD_LENGTH, (byte)(65 + i));
        }

        for (int i = 0; i < 4; i++)
        {
            while (publication.offer(srcBuffer, i * PAYLOAD_LENGTH, PAYLOAD_LENGTH) < 0L)
            {
                Thread.yield();
            }
        }

        int numFragments = 0;
        int numSpyFragments = 0;
        do
        {
            numFragments += subscription.poll(mockFragmentHandler, FRAGMENT_COUNT_LIMIT);
            numSpyFragments += spy.poll(mockSpyFragmentHandler, FRAGMENT_COUNT_LIMIT);
        }
        while (numSpyFragments < 4 || numFragments < 4);

        verify(mockFragmentHandler, times(4)).onFragment(
            any(DirectBuffer.class), anyInt(), eq(PAYLOAD_LENGTH), any(Header.class));
        verify(mockSpyFragmentHandler, times(4)).onFragment(
            any(DirectBuffer.class), anyInt(), eq(PAYLOAD_LENGTH), any(Header.class));
    }
    finally
    {
        ctx.deleteAeronDirectory();
    }
}
 
开发者ID:real-logic,项目名称:aeron,代码行数:48,代码来源:SpySubscriptionTest.java

示例13: shouldAppendFragmentedFromVectorsToEmptyLog

import org.agrona.concurrent.UnsafeBuffer; //导入方法依赖的package包/类
@Test
public void shouldAppendFragmentedFromVectorsToEmptyLog()
{
    final int mtu = 2048;
    final int headerLength = DEFAULT_HEADER.capacity();
    final int maxPayloadLength = mtu - headerLength;
    final int bufferOneLength = 64;
    final int bufferTwoLength = 3000;
    final UnsafeBuffer bufferOne = new UnsafeBuffer(new byte[bufferOneLength]);
    final UnsafeBuffer bufferTwo = new UnsafeBuffer(new byte[bufferTwoLength]);
    bufferOne.setMemory(0, bufferOne.capacity(), (byte)'1');
    bufferTwo.setMemory(0, bufferTwo.capacity(), (byte)'2');
    final int msgLength = bufferOneLength + bufferTwoLength;
    int tail = 0;
    final int frameOneLength = mtu;
    final int frameTwoLength = (msgLength - (mtu - headerLength)) + headerLength;
    final int resultingOffset = frameOneLength + BitUtil.align(frameTwoLength, FRAME_ALIGNMENT);

    logMetaDataBuffer.putLong(TERM_TAIL_COUNTER_OFFSET, packTail(TERM_ID, tail));

    final DirectBufferVector[] vectors = new DirectBufferVector[]
    {
        new DirectBufferVector(bufferOne, 0, bufferOneLength),
        new DirectBufferVector(bufferTwo, 0, bufferTwoLength)
    };

    assertThat(termAppender.appendFragmentedMessage(
        headerWriter, vectors, msgLength, maxPayloadLength, RVS, TERM_ID), is(resultingOffset));

    final InOrder inOrder = inOrder(termBuffer, headerWriter);

    inOrder.verify(headerWriter, times(1)).write(termBuffer, tail, frameOneLength, TERM_ID);
    inOrder.verify(termBuffer, times(1)).putBytes(headerLength, bufferOne, 0, bufferOneLength);
    inOrder.verify(termBuffer, times(1))
        .putBytes(headerLength + bufferOneLength, bufferTwo, 0, maxPayloadLength - bufferOneLength);
    inOrder.verify(termBuffer, times(1)).putLong(tail + RESERVED_VALUE_OFFSET, RV, LITTLE_ENDIAN);
    inOrder.verify(termBuffer, times(1)).putIntOrdered(tail, frameOneLength);

    tail += frameOneLength;
    final int bufferTwoOffset = maxPayloadLength - bufferOneLength;
    final int fragmentTwoPayloadLength = bufferTwoLength - (maxPayloadLength - bufferOneLength);

    inOrder.verify(headerWriter, times(1)).write(termBuffer, tail, frameTwoLength, TERM_ID);
    inOrder.verify(termBuffer, times(1))
        .putBytes(tail + headerLength, bufferTwo, bufferTwoOffset, fragmentTwoPayloadLength);
    inOrder.verify(termBuffer, times(1)).putLong(tail + RESERVED_VALUE_OFFSET, RV, LITTLE_ENDIAN);
    inOrder.verify(termBuffer, times(1)).putIntOrdered(tail, frameTwoLength);
}
 
开发者ID:real-logic,项目名称:aeron,代码行数:49,代码来源:TermAppenderTest.java


注:本文中的org.agrona.concurrent.UnsafeBuffer.setMemory方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。