@Override public void requestContent(Duration timeout) { this.content.complete(new ReadResultEntryContents( data.getReader(this.relativeOffset, this.requestedReadLength), this.requestedReadLength)); }
streams.add(pageContents.getReader()); length.addAndGet(pageContents.getLength());
@Override @SneakyThrows(IOException.class) protected byte[] getData(EnhancedByteArrayOutputStream stream) { val b = stream.getData(); return StreamHelpers.readAll(b.getReader(), b.getLength()); } }
@Override @SneakyThrows(IOException.class) protected byte[] getData(EnhancedByteArrayOutputStream stream) { val b = stream.getData(); return StreamHelpers.readAll(b.getReader(), b.getLength()); } }
@Override @SneakyThrows(IOException.class) protected byte[] getData(FixedByteArrayOutputStream stream) { val b = stream.getData(); return StreamHelpers.readAll(b.getReader(), b.getLength()); } }
@SneakyThrows(IOException.class) private byte[] serialize(RollingSegmentHandle handle) { val s = HandleSerializer.serialize(handle); return StreamHelpers.readAll(s.getReader(), s.getLength()); }
@Override @SneakyThrows(IOException.class) protected byte[] getData(FixedByteArrayOutputStream stream) { val b = stream.getData(); return StreamHelpers.readAll(b.getReader(), b.getLength()); } }
private void queueStorageRead(long offset, int length, Consumer<ReadResultEntryContents> successCallback, Consumer<Throwable> failureCallback, Duration timeout) { // Create a callback that inserts into the ReadIndex (and cache) and invokes the success callback. Consumer<StorageReadManager.Result> doneCallback = result -> { ByteArraySegment data = result.getData(); // Make sure we invoke our callback first, before any chance of exceptions from insert() may block it. successCallback.accept(new ReadResultEntryContents(data.getReader(), data.getLength())); if (!result.isDerived()) { // Only insert primary results into the cache. Derived results are always sub-portions of primaries // and there is no need to insert them too, as they are already contained within. insert(offset, data); } }; // Queue the request for async processing. length = getReadAlignedLength(offset, length); this.storageReadManager.execute(new StorageReadManager.Request(offset, length, doneCallback, failureCallback, timeout)); }
/** * Tests the ability to serialize and deserialize objects sharing a common base class. */ private <T extends OutputStream> void testMultiType(T outStream, Function<T, ByteArraySegment> getData) throws IOException { val s = new BaseClassSerializer(); // Serialize all test data into a single output stream. for (val o : MULTI_TYPE_DATA) { s.serialize(outStream, o); } // Deserialize them back and verify contents. val inputStream = getData.apply(outStream).getReader(); for (val expected : MULTI_TYPE_DATA) { val deserialized = s.deserialize(inputStream); checkMultiType(expected, deserialized); } }
private void serializeHandle(RollingSegmentHandle handle) throws StreamSegmentException { ByteArraySegment handleData = HandleSerializer.serialize(handle); try { this.baseStorage.write(handle.getHeaderHandle(), 0, handleData.getReader(), handleData.getLength()); handle.setHeaderLength(handleData.getLength()); log.debug("Header for '{}' fully serialized to '{}'.", handle.getSegmentName(), handle.getHeaderHandle().getSegmentName()); } catch (BadOffsetException ex) { // If we get BadOffsetException when writing the Handle, it means it was modified externally. throw new StorageNotPrimaryException(handle.getSegmentName(), ex); } }
private <T extends OutputStream> void testZeroLength(Supplier<T> newBaseStream, Function<T, ByteArraySegment> getWrittenData) throws Exception { @Cleanup val os = newBaseStream.get(); // Open and immediately close the RevisionDataOutputStream. @Cleanup val rdos = RevisionDataOutputStream.wrap(os); rdos.close(); val data = getWrittenData.apply(os); @Cleanup val rdis = RevisionDataInputStream.wrap(data.getReader()); Assert.assertEquals("Unexpected length encoded.", 0, rdis.getLength()); AssertExtensions.assertThrows( "Expecting EOF.", () -> rdis.readFully(new byte[1]), ex -> ex instanceof EOFException); }
AssertExtensions.assertStreamEquals("Unexpected read contents for request " + request, new ByteArrayInputStream(segmentData, (int) request.getOffset(), expectedReadLength), readData.getData().getReader(), expectedReadLength);
private <T> void testEncodeDecode(BiConsumerWithException<RevisionDataOutputStream, T> write, FunctionWithException<RevisionDataInputStream, T> read, BiFunction<RevisionDataOutputStream, T, Integer> getLength, T value, BiPredicate<T, T> equalityTester) throws Exception { @Cleanup val os = new EnhancedByteArrayOutputStream(); @Cleanup val rdos = RevisionDataOutputStream.wrap(os); write.accept(rdos, value); rdos.close(); os.close(); val actualLength = os.size() - Integer.BYTES; // Subtract 4 because this is the Length being encoded. Assert.assertEquals("Unexpected length for value " + value, (int) getLength.apply(rdos, value), actualLength); @Cleanup val rdis = RevisionDataInputStream.wrap(os.getData().getReader()); val actualValue = read.apply(rdis); Assert.assertTrue(String.format("Encoding/decoding failed for %s (decoded %s).", value, actualValue), equalityTester.test(value, actualValue)); }
/** * Tests the functionality of getReader (the ability to return an InputStream from a sub-segment of the main buffer). */ @Test public void testGetReader() throws IOException { final byte[] buffer = createFormattedBuffer(); ByteArraySegment segment = new ByteArraySegment(buffer); for (int offset = 0; offset < buffer.length / 2; offset++) { int length = buffer.length - offset * 2; byte[] readBuffer = new byte[length]; try (InputStream stream = segment.getReader(offset, length)) { int readBytes = StreamHelpers.readAll(stream, readBuffer, 0, readBuffer.length); Assert.assertEquals("Unexpected number of bytes read from the InputStream at offset " + offset, length, readBytes); } for (int i = 0; i < length; i++) { Assert.assertEquals("Unexpected value at index " + i + " after reading from offset " + offset, segment.get(i + offset), readBuffer[i]); } } }
val inputStream = RevisionDataInputStream.wrap(getWrittenData.get().getReader()); Assert.assertEquals("Unexpected length read back.", expectedLength, inputStream.getLength()); Assert.assertEquals("Unexpected byte read back.", b, inputStream.read());