Java Code Examples for org.agrona.concurrent.UnsafeBuffer#wrap()
The following examples show how to use
org.agrona.concurrent.UnsafeBuffer#wrap() .
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: LogBuffers.java From aeron with Apache License 2.0 | 6 votes |
/** * Pre touch memory pages so they are faulted in to be available before access. */ public void preTouch() { final int value = 0; final int pageSize = LogBufferDescriptor.pageSize(logMetaDataBuffer); final UnsafeBuffer atomicBuffer = new UnsafeBuffer(); for (final MappedByteBuffer buffer : mappedByteBuffers) { atomicBuffer.wrap(buffer); for (int i = 0, length = atomicBuffer.capacity(); i < length; i += pageSize) { atomicBuffer.compareAndSetInt(i, value, value); } } }
Example 2
Source File: Catalog.java From aeron with Apache License 2.0 | 5 votes |
boolean wrapDescriptor(final long recordingId, final UnsafeBuffer buffer) { if (recordingId < 0 || recordingId > maxRecordingId) { return false; } buffer.wrap(catalogByteBuffer, recordingDescriptorOffset(recordingId), recordLength); return descriptorLength(buffer) > 0; }
Example 3
Source File: Catalog.java From aeron with Apache License 2.0 | 5 votes |
boolean wrapAndValidateDescriptor(final long recordingId, final UnsafeBuffer buffer) { if (recordingId < 0 || recordingId > maxRecordingId) { return false; } buffer.wrap(catalogByteBuffer, recordingDescriptorOffset(recordingId), recordLength); return descriptorLength(buffer) > 0 && isValidDescriptor(buffer); }
Example 4
Source File: RecordingWriterTests.java From aeron with Apache License 2.0 | 5 votes |
@Test void onBlockShouldWriteHeaderAndContentsOfTheNonPaddingFrame() throws IOException { final Image image = mockImage(0L); final RecordingWriter recordingWriter = new RecordingWriter( 1, 0, SEGMENT_LENGTH, image, new Context().archiveDir(archiveDir), null, null, null); recordingWriter.init(); final UnsafeBuffer termBuffer = new UnsafeBuffer(allocate(128)); frameType(termBuffer, 0, HDR_TYPE_DATA); frameLengthOrdered(termBuffer, 0, 128); final byte[] data = new byte[96]; fill(data, (byte)7); termBuffer.putBytes(HEADER_LENGTH, data); recordingWriter.onBlock(termBuffer, 0, 128, -1, -1); recordingWriter.close(); final File segmentFile = segmentFile(1, 0); assertTrue(segmentFile.exists()); assertEquals(SEGMENT_LENGTH, segmentFile.length()); final UnsafeBuffer fileBuffer = new UnsafeBuffer(); fileBuffer.wrap(readAllBytes(segmentFile.toPath())); assertEquals(HDR_TYPE_DATA, frameType(fileBuffer, 0)); assertEquals(128, frameLength(fileBuffer, 0)); assertEquals(0, frameSessionId(fileBuffer, 0)); final byte[] fileBytes = new byte[96]; fileBuffer.getBytes(HEADER_LENGTH, fileBytes, 0, 96); assertArrayEquals(data, fileBytes); }
Example 5
Source File: RecordingWriterTests.java From aeron with Apache License 2.0 | 5 votes |
@Test void onBlockShouldWriteHeaderOfThePaddingFrameAndAdvanceFilePositionByThePaddingLength() throws IOException { final int segmentOffset = 96; final long startPosition = 7 * TERM_LENGTH + segmentOffset; final Image image = mockImage(startPosition); final RecordingWriter recordingWriter = new RecordingWriter( 5, startPosition, SEGMENT_LENGTH, image, new Context().archiveDir(archiveDir), null, null, null); recordingWriter.init(); final UnsafeBuffer termBuffer = new UnsafeBuffer(allocate(1024)); frameType(termBuffer, 0, HDR_TYPE_PAD); frameLengthOrdered(termBuffer, 0, 1024); frameSessionId(termBuffer, 0, 111); final byte[] data = new byte[992]; fill(data, (byte)-1); termBuffer.putBytes(HEADER_LENGTH, data); recordingWriter.onBlock(termBuffer, 0, 1024, -1, -1); recordingWriter.close(); final File segmentFile = segmentFile(5, startPosition); assertTrue(segmentFile.exists()); assertEquals(SEGMENT_LENGTH, segmentFile.length()); final UnsafeBuffer fileBuffer = new UnsafeBuffer(); fileBuffer.wrap(readAllBytes(segmentFile.toPath())); final byte[] preamble = new byte[segmentOffset]; fileBuffer.getBytes(0, preamble, 0, segmentOffset); assertArrayEquals(new byte[segmentOffset], preamble); assertEquals(HDR_TYPE_PAD, frameType(fileBuffer, segmentOffset)); assertEquals(1024, frameLength(fileBuffer, segmentOffset)); assertEquals(111, frameSessionId(fileBuffer, segmentOffset)); final byte[] fileBytes = new byte[992]; fileBuffer.getBytes(segmentOffset + HEADER_LENGTH, fileBytes, 0, 992); assertArrayEquals(new byte[992], fileBytes); }
Example 6
Source File: RecordingWriterTests.java From aeron with Apache License 2.0 | 5 votes |
@Test void onBlockShouldNotComputeCrcForThePaddingFrame() throws IOException { final Image image = mockImage(0L); final Context ctx = new Context().archiveDir(archiveDir); final UnsafeBuffer checksumBuffer = new UnsafeBuffer(allocateDirectAligned(512, 64)); final RecordingWriter recordingWriter = new RecordingWriter( 1, 0, SEGMENT_LENGTH, image, ctx, null, checksumBuffer, crc32()); recordingWriter.init(); final UnsafeBuffer termBuffer = new UnsafeBuffer(allocate(512)); final int length = 128; final byte[] data = new byte[length - HEADER_LENGTH]; final int sessionId = 5; final int termId = 18; fill(data, (byte)99); frameType(termBuffer, 0, HDR_TYPE_PAD); frameTermId(termBuffer, 0, termId); frameLengthOrdered(termBuffer, 0, length); frameSessionId(termBuffer, 0, sessionId); termBuffer.putBytes(HEADER_LENGTH, data); recordingWriter.onBlock(termBuffer, 0, HEADER_LENGTH, -1, -1); recordingWriter.close(); final File segmentFile = segmentFile(1, 0); assertTrue(segmentFile.exists()); assertEquals(SEGMENT_LENGTH, segmentFile.length()); final UnsafeBuffer fileBuffer = new UnsafeBuffer(); fileBuffer.wrap(readAllBytes(segmentFile.toPath())); assertEquals(HDR_TYPE_PAD, frameType(fileBuffer, 0)); assertEquals(termId, frameTermId(fileBuffer, 0)); assertEquals(length, frameLength(fileBuffer, 0)); assertEquals(sessionId, frameSessionId(fileBuffer, 0)); }
Example 7
Source File: SbeSessionMessenger.java From conga with Apache License 2.0 | 4 votes |
public void init(boolean isClientSession) { this.isClientSession = isClientSession; final MessageHeaderEncoder headerEncoder = new MessageHeaderEncoder(); if (isClientSession()) { negotiateBuffer = ByteBuffer.allocateDirect(256); negotiateEncoder = new NegotiateEncoder(); negotiateMutableBuffer = new UnsafeBuffer(); negotiateMutableBuffer.wrap(negotiateBuffer); negotiateEncoder.wrapAndApplyHeader(negotiateMutableBuffer, 0, headerEncoder); negotiationResponseDecoder = new NegotiationResponseDecoder(); negotiationRejectDecoder = new NegotiationRejectDecoder(); establishBuffer = ByteBuffer.allocateDirect(256); establishEncoder = new EstablishEncoder(); establishMutableBuffer = new UnsafeBuffer(); establishMutableBuffer.wrap(establishBuffer); establishEncoder.wrapAndApplyHeader(establishMutableBuffer, 0, headerEncoder); establishmentAckDecoder = new EstablishmentAckDecoder(); establishmentRejectDecoder = new EstablishmentRejectDecoder(); } else { negotiateDecoder = new NegotiateDecoder(); negotiationResponseBuffer = ByteBuffer.allocateDirect(256); negotiationResponseEncoder = new NegotiationResponseEncoder(); negotiationResponseMutableBuffer = new UnsafeBuffer(); negotiationResponseMutableBuffer.wrap(negotiationResponseBuffer); negotiationResponseEncoder.wrapAndApplyHeader(negotiationResponseMutableBuffer, 0, headerEncoder); negotiationRejectBuffer = ByteBuffer.allocateDirect(256); negotiationRejectEncoder = new NegotiationRejectEncoder(); negotiationRejectMutableBuffer = new UnsafeBuffer(); negotiationRejectMutableBuffer.wrap(negotiationRejectBuffer); negotiationRejectEncoder.wrapAndApplyHeader(negotiationRejectMutableBuffer, 0, headerEncoder); establishDecoder = new EstablishDecoder(); establishmentAckBuffer = ByteBuffer.allocateDirect(256); establishmentAckEncoder = new EstablishmentAckEncoder(); establishmentAckMutableBuffer = new UnsafeBuffer(); establishmentAckMutableBuffer.wrap(establishmentAckBuffer); establishmentAckEncoder.wrapAndApplyHeader(establishmentAckMutableBuffer, 0, headerEncoder); establishmentRejectBuffer = ByteBuffer.allocateDirect(256); establishmentRejectEncoder = new EstablishmentRejectEncoder(); establishmentRejectMutableBuffer = new UnsafeBuffer(); establishmentRejectMutableBuffer.wrap(establishmentRejectBuffer); establishmentRejectEncoder.wrapAndApplyHeader(establishmentRejectMutableBuffer, 0, headerEncoder); } sequenceMutableBuffer.wrap(sequenceBuffer); sequenceEncoder.wrapAndApplyHeader(sequenceMutableBuffer, 0, headerEncoder); notAppliedMutableBuffer.wrap(notAppliedBuffer); notAppliedEncoder.wrapAndApplyHeader(notAppliedMutableBuffer, 0, headerEncoder); retransmitRequestMutableBuffer.wrap(retransmitRequestBuffer); retransmitRequestEncoder.wrapAndApplyHeader(retransmitRequestMutableBuffer, 0, headerEncoder); retransmissionMutableBuffer.wrap(retransmissionBuffer); retransmissionEncoder.wrapAndApplyHeader(retransmissionMutableBuffer, 0, headerEncoder); finishedSendingMutableBuffer.wrap(finishedSendingBuffer); finishedSendingEncoder.wrapAndApplyHeader(finishedSendingMutableBuffer, 0, headerEncoder); finishedReceivingMutableBuffer.wrap(finishedReceivingBuffer); finishedReceivingEncoder.wrapAndApplyHeader(finishedReceivingMutableBuffer, 0, headerEncoder); }
Example 8
Source File: RecordingWriterTests.java From aeron with Apache License 2.0 | 4 votes |
@Test void onBlockShouldRollOverToTheNextSegmentFile() throws IOException { final Image image = mockImage(0L); final RecordingWriter recordingWriter = new RecordingWriter( 13, 0, SEGMENT_LENGTH, image, new Context().archiveDir(archiveDir), null, null, null); recordingWriter.init(); final byte[] data1 = new byte[992]; fill(data1, (byte)13); final UnsafeBuffer termBuffer = new UnsafeBuffer(allocate(TERM_LENGTH)); frameType(termBuffer, 0, HDR_TYPE_DATA); frameLengthOrdered(termBuffer, 0, 1024); termBuffer.putBytes(HEADER_LENGTH, data1); for (int i = 0; i < SEGMENT_LENGTH / 1024; i++) { recordingWriter.onBlock(termBuffer, 0, 1024, -1, -1); } frameType(termBuffer, 0, HDR_TYPE_DATA); frameLengthOrdered(termBuffer, 0, 192); final byte[] data2 = new byte[160]; fill(data2, (byte)22); termBuffer.putBytes(HEADER_LENGTH, data2); recordingWriter.onBlock(termBuffer, 0, 192, -1, -1); recordingWriter.close(); final File segmentFile1 = segmentFile(13, 0); assertTrue(segmentFile1.exists()); assertEquals(SEGMENT_LENGTH, segmentFile1.length()); final UnsafeBuffer fileBuffer = new UnsafeBuffer(); fileBuffer.wrap(readAllBytes(segmentFile1.toPath())); assertEquals(HDR_TYPE_DATA, frameType(fileBuffer, 0)); assertEquals(1024, frameLength(fileBuffer, 0)); byte[] fileBytes = new byte[992]; fileBuffer.getBytes(HEADER_LENGTH, fileBytes, 0, 992); assertArrayEquals(data1, fileBytes); final File segmentFile2 = segmentFile(13, SEGMENT_LENGTH); assertTrue(segmentFile2.exists()); assertEquals(SEGMENT_LENGTH, segmentFile2.length()); fileBuffer.wrap(readAllBytes(segmentFile2.toPath())); assertEquals(HDR_TYPE_DATA, frameType(fileBuffer, 0)); assertEquals(192, frameLength(fileBuffer, 0)); fileBytes = new byte[160]; fileBuffer.getBytes(HEADER_LENGTH, fileBytes, 0, 160); assertArrayEquals(data2, fileBytes); }
Example 9
Source File: RecordingWriterTests.java From aeron with Apache License 2.0 | 4 votes |
@Test void onBlockShouldComputeCrcUsingTheChecksumBuffer() throws IOException { final Image image = mockImage(0L); final Context ctx = new Context().archiveDir(archiveDir); final UnsafeBuffer checksumBuffer = new UnsafeBuffer(allocateDirectAligned(512, 64)); final Checksum checksum = crc32(); final RecordingWriter recordingWriter = new RecordingWriter( 1, 0, SEGMENT_LENGTH, image, ctx, null, checksumBuffer, checksum); recordingWriter.init(); final UnsafeBuffer termBuffer = new UnsafeBuffer(allocateDirectAligned(512, 64)); frameType(termBuffer, 96, HDR_TYPE_DATA); frameTermId(termBuffer, 96, 96); frameLengthOrdered(termBuffer, 96, 64); frameSessionId(termBuffer, 96, 96); termBuffer.setMemory(96 + HEADER_LENGTH, 32, (byte)96); frameType(termBuffer, 160, HDR_TYPE_DATA); frameTermId(termBuffer, 160, 160); frameLengthOrdered(termBuffer, 160, 288); frameSessionId(termBuffer, 160, 160); termBuffer.setMemory(160 + HEADER_LENGTH, 256, (byte)160); recordingWriter.onBlock(termBuffer, 96, 352, -1, -1); recordingWriter.close(); final File segmentFile = segmentFile(1, 0); assertTrue(segmentFile.exists()); assertEquals(SEGMENT_LENGTH, segmentFile.length()); final UnsafeBuffer fileBuffer = new UnsafeBuffer(); fileBuffer.wrap(readAllBytes(segmentFile.toPath())); assertEquals(HDR_TYPE_DATA, frameType(fileBuffer, 0)); assertEquals(96, frameTermId(fileBuffer, 0)); assertEquals(64, frameLength(fileBuffer, 0)); assertEquals( checksum.compute(termBuffer.addressOffset(), 96 + HEADER_LENGTH, 32), frameSessionId(fileBuffer, 0)); assertEquals(HDR_TYPE_DATA, frameType(fileBuffer, 64)); assertEquals(160, frameTermId(fileBuffer, 64)); assertEquals(288, frameLength(fileBuffer, 64)); assertEquals( checksum.compute(termBuffer.addressOffset(), 160 + HEADER_LENGTH, 256), frameSessionId(fileBuffer, 64)); // Ensure that the source buffer was not modified assertEquals(96, frameSessionId(termBuffer, 96)); assertEquals(160, frameSessionId(termBuffer, 160)); }