Java Code Examples for org.agrona.concurrent.UnsafeBuffer#getBytes()
The following examples show how to use
org.agrona.concurrent.UnsafeBuffer#getBytes() .
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: RocksDbStorage.java From nd4j with Apache License 2.0 | 6 votes |
/** * 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++; }
Example 2
Source File: RocksDbStorage.java From deeplearning4j with Apache License 2.0 | 6 votes |
/** * 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++; }
Example 3
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 4
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 5
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); }