本文整理汇总了Java中org.agrona.concurrent.UnsafeBuffer.capacity方法的典型用法代码示例。如果您正苦于以下问题:Java UnsafeBuffer.capacity方法的具体用法?Java UnsafeBuffer.capacity怎么用?Java UnsafeBuffer.capacity使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.agrona.concurrent.UnsafeBuffer
的用法示例。
在下文中一共展示了UnsafeBuffer.capacity方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: forEachFragment
import org.agrona.concurrent.UnsafeBuffer; //导入方法依赖的package包/类
public void forEachFragment(
final StreamIdentifier streamId,
final FragmentHandler handler,
final ErrorHandler errorHandler)
{
final UnsafeBuffer termBuffer = new UnsafeBuffer(0, 0);
for (final File logFile : directoryDescriptor.listLogFiles(streamId))
{
final ByteBuffer byteBuffer = LoggerUtil.mapExistingFile(logFile);
if (byteBuffer.capacity() > 0)
{
termBuffer.wrap(byteBuffer);
final int initialTermId = LogBufferDescriptor.initialTermId(termBuffer);
final Header header = new Header(initialTermId, termBuffer.capacity());
TermReader.read(
termBuffer,
0,
handler,
Integer.MAX_VALUE,
header,
errorHandler);
}
}
}
示例2: verifyData
import org.agrona.concurrent.UnsafeBuffer; //导入方法依赖的package包/类
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;
assertThat("same at " + index, capturedBuffer.getByte(index), is(srcBuffer.getByte(i)));
}
}
示例3: 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));
}
示例4: shouldDoNotingIfMidArrivesWithoutBegin
import org.agrona.concurrent.UnsafeBuffer; //导入方法依赖的package包/类
@Test
public void shouldDoNotingIfMidArrivesWithoutBegin()
{
when(header.flags())
.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() / 2;
adapter.onFragment(srcBuffer, offset, length, header);
adapter.onFragment(srcBuffer, offset, length, header);
verify(delegateFragmentHandler, never()).onFragment(any(), anyInt(), anyInt(), any());
}
示例5: addUpdate
import org.agrona.concurrent.UnsafeBuffer; //导入方法依赖的package包/类
/**
* Add an ndarray to the storage
*
* @param array the array to add
*/
@Override
public void addUpdate(NDArrayMessage array) {
UnsafeBuffer directBuffer = (UnsafeBuffer) NDArrayMessage.toBuffer(array);
byte[] data = directBuffer.byteArray();
if (data == null) {
data = new byte[directBuffer.capacity()];
directBuffer.getBytes(0, data, 0, data.length);
}
byte[] key = ByteBuffer.allocate(4).putInt(size).array();
try {
db.put(key, data);
} catch (RocksDBException e) {
throw new RuntimeException(e);
}
size++;
}
示例6: claim
import org.agrona.concurrent.UnsafeBuffer; //导入方法依赖的package包/类
/**
* Claim length of a the term buffer for writing in the message with zero copy semantics.
*
* @param termId for the current term.
* @param termOffset in the term at which to append.
* @param header for writing the default header.
* @param length of the message to be written.
* @param bufferClaim to be updated with the claimed region.
* @return the resulting offset of the term after the append on success otherwise {@link #FAILED}.
*/
public int claim(
final int termId,
final int termOffset,
final HeaderWriter header,
final int length,
final BufferClaim bufferClaim)
{
final int frameLength = length + HEADER_LENGTH;
final int alignedLength = align(frameLength, FRAME_ALIGNMENT);
final UnsafeBuffer termBuffer = this.termBuffer;
final int termLength = termBuffer.capacity();
int resultingOffset = termOffset + alignedLength;
putRawTailOrdered(termId, resultingOffset);
if (resultingOffset > termLength)
{
resultingOffset = handleEndOfLogCondition(termBuffer, termOffset, header, termLength, termId);
}
else
{
header.write(termBuffer, termOffset, frameLength, termId);
bufferClaim.wrap(termBuffer, termOffset, frameLength);
}
return resultingOffset;
}
示例7: appendPadding
import org.agrona.concurrent.UnsafeBuffer; //导入方法依赖的package包/类
/**
* Pad a length of the term buffer with a padding record.
*
* @param termId for the current term.
* @param termOffset in the term at which to append.
* @param header for writing the default header.
* @param length of the padding to be written.
* @return the resulting offset of the term after success otherwise {@link #FAILED}.
*/
public int appendPadding(
final int termId,
final int termOffset,
final HeaderWriter header,
final int length)
{
final int frameLength = length + HEADER_LENGTH;
final int alignedLength = align(frameLength, FRAME_ALIGNMENT);
final UnsafeBuffer termBuffer = this.termBuffer;
final int termLength = termBuffer.capacity();
int resultingOffset = termOffset + alignedLength;
putRawTailOrdered(termId, resultingOffset);
if (resultingOffset > termLength)
{
resultingOffset = handleEndOfLogCondition(termBuffer, termOffset, header, termLength, termId);
}
else
{
header.write(termBuffer, termOffset, frameLength, termId);
frameType(termBuffer, termOffset, PADDING_FRAME_TYPE);
frameLengthOrdered(termBuffer, termOffset, frameLength);
}
return resultingOffset;
}
示例8: appendUnfragmentedMessage
import org.agrona.concurrent.UnsafeBuffer; //导入方法依赖的package包/类
/**
* Append an unfragmented message to the the term buffer.
*
* @param termId for the current term.
* @param termOffset in the term at which to append.
* @param header for writing the default header.
* @param srcBuffer containing the message.
* @param srcOffset at which the message begins.
* @param length of the message in the source buffer.
* @param reservedValueSupplier {@link ReservedValueSupplier} for the frame.
* @return the resulting offset of the term after the append on success otherwise {@link #FAILED}.
*/
public int appendUnfragmentedMessage(
final int termId,
final int termOffset,
final HeaderWriter header,
final DirectBuffer srcBuffer,
final int srcOffset,
final int length,
final ReservedValueSupplier reservedValueSupplier)
{
final int frameLength = length + HEADER_LENGTH;
final int alignedLength = align(frameLength, FRAME_ALIGNMENT);
final UnsafeBuffer termBuffer = this.termBuffer;
final int termLength = termBuffer.capacity();
int resultingOffset = termOffset + alignedLength;
putRawTailOrdered(termId, resultingOffset);
if (resultingOffset > termLength)
{
resultingOffset = handleEndOfLogCondition(termBuffer, termOffset, header, termLength, termId);
}
else
{
header.write(termBuffer, termOffset, frameLength, termId);
termBuffer.putBytes(termOffset + HEADER_LENGTH, srcBuffer, srcOffset, length);
if (null != reservedValueSupplier)
{
final long reservedValue = reservedValueSupplier.get(termBuffer, termOffset, frameLength);
termBuffer.putLong(termOffset + RESERVED_VALUE_OFFSET, reservedValue, LITTLE_ENDIAN);
}
frameLengthOrdered(termBuffer, termOffset, frameLength);
}
return resultingOffset;
}
示例9: rawPoll
import org.agrona.concurrent.UnsafeBuffer; //导入方法依赖的package包/类
/**
* 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.
*
* @param rawBlockHandler 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 rawBlockHandler, final int blockLengthLimit)
{
if (isClosed)
{
return 0;
}
final long position = subscriberPosition.get();
final int termOffset = (int)position & termLengthMask;
final int activeIndex = indexByPosition(position, positionBitsToShift);
final UnsafeBuffer termBuffer = termBuffers[activeIndex];
final int capacity = termBuffer.capacity();
final int limit = Math.min(termOffset + blockLengthLimit, capacity);
final int resultingOffset = TermBlockScanner.scan(termBuffer, termOffset, limit);
final int length = resultingOffset - termOffset;
if (resultingOffset > termOffset)
{
try
{
final long fileOffset = ((long)capacity * activeIndex) + termOffset;
final int termId = termBuffer.getInt(termOffset + TERM_ID_FIELD_OFFSET, LITTLE_ENDIAN);
rawBlockHandler.onBlock(
logBuffers.fileChannel(), fileOffset, termBuffer, termOffset, length, sessionId, termId);
}
catch (final Throwable t)
{
errorHandler.onError(t);
}
finally
{
subscriberPosition.setOrdered(position + length);
}
}
return length;
}
示例10: 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));
}
示例11: 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));
}
示例12: shouldDoNotingIfEndArrivesWithoutBegin
import org.agrona.concurrent.UnsafeBuffer; //导入方法依赖的package包/类
@Test
public void shouldDoNotingIfEndArrivesWithoutBegin()
{
when(header.flags()).thenReturn(FrameDescriptor.END_FRAG_FLAG);
final UnsafeBuffer srcBuffer = new UnsafeBuffer(new byte[1024]);
final int offset = 0;
final int length = srcBuffer.capacity() / 2;
adapter.onFragment(srcBuffer, offset, length, header);
verify(delegateFragmentHandler, never()).onFragment(any(), anyInt(), anyInt(), any());
}
示例13: 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);
}
示例14: scan
import org.agrona.concurrent.UnsafeBuffer; //导入方法依赖的package包/类
/**
* Scan for gaps and handle received data.
* <p>
* The handler keeps track from scan to scan what is a gap and what must have been repaired.
*
* @param termBuffer to scan
* @param rebuildPosition to start scanning from
* @param hwmPosition to scan up to
* @param nowNs time in nanoseconds
* @param termLengthMask used for offset calculation
* @param positionBitsToShift used for position calculation
* @param initialTermId used by the scanner
* @return packed outcome of the scan.
*/
public long scan(
final UnsafeBuffer termBuffer,
final long rebuildPosition,
final long hwmPosition,
final long nowNs,
final int termLengthMask,
final int positionBitsToShift,
final int initialTermId)
{
boolean lossFound = false;
int rebuildOffset = (int)rebuildPosition & termLengthMask;
if (rebuildPosition < hwmPosition)
{
final int rebuildTermCount = (int)(rebuildPosition >>> positionBitsToShift);
final int hwmTermCount = (int)(hwmPosition >>> positionBitsToShift);
final int rebuildTermId = initialTermId + rebuildTermCount;
final int hwmTermOffset = (int)hwmPosition & termLengthMask;
final int limitOffset = rebuildTermCount == hwmTermCount ? hwmTermOffset : termBuffer.capacity();
rebuildOffset = scanForGap(termBuffer, rebuildTermId, rebuildOffset, limitOffset, this);
if (rebuildOffset < limitOffset)
{
if (!scannedGap.matches(activeGap))
{
activateGap(nowNs, scannedGap);
lossFound = true;
}
checkTimerExpiry(nowNs);
}
}
return pack(rebuildOffset, lossFound);
}
示例15: 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();
}
}