use of io.pravega.common.util.ByteArraySegment in project pravega by pravega.
the class SerializationTest method verify.
private void verify(ReaderGroupInitSerializer serializer, InitialUpdate<ReaderGroupState> value) throws IOException {
ByteArraySegment bytes = serializer.serialize(value);
Update<ReaderGroupState> deserialized = serializer.deserialize(bytes);
assertEquals(value, deserialized);
}
use of io.pravega.common.util.ByteArraySegment in project pravega by pravega.
the class RevisionDataOutputStreamTests method testImpl.
private void testImpl(RevisionDataOutputStream impl, Supplier<ByteArraySegment> getWrittenData) throws Exception {
final byte b = 123;
final short sn = 1234;
final int n = 123456;
final long l = (long) Integer.MAX_VALUE + 1;
final String s = getUTFString();
final byte[] array = s.getBytes(Charsets.UTF_8);
int expectedLength = Byte.BYTES + Short.BYTES + Integer.BYTES + Long.BYTES + impl.getUTFLength(s) + array.length + impl.getCompactIntLength(array.length) + array.length;
if (impl.requiresExplicitLength()) {
// Verify a few methods that shouldn't be allowed to run without setting length beforehand.
Arrays.<AssertExtensions.RunnableWithException>asList(() -> impl.write(1), () -> impl.write(new byte[1], 0, 1), () -> impl.writeInt(1), () -> impl.writeShort(1), () -> impl.writeLong(1), () -> impl.writeUTF("test")).forEach(r -> AssertExtensions.assertThrows("write was allowed without setting length first.", r, ex -> ex instanceof IllegalStateException));
}
impl.length(expectedLength);
impl.writeByte(b);
impl.writeShort(sn);
impl.writeInt(n);
impl.writeLong(l);
impl.writeUTF(s);
impl.write(array);
impl.writeBuffer(new ByteArraySegment(array));
// Need to close so we flush any remaining stuff to the underlying stream.
impl.close();
// Verify the written data can be read back.
@Cleanup 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());
Assert.assertEquals("Unexpected short read back.", sn, inputStream.readShort());
Assert.assertEquals("Unexpected int read back.", n, inputStream.readInt());
Assert.assertEquals("Unexpected long read back.", l, inputStream.readLong());
Assert.assertEquals("Unexpected string read back.", s, inputStream.readUTF());
byte[] readArray = new byte[array.length];
int readBytes = inputStream.read(readArray);
Assert.assertEquals("Unexpected number of bytes read for array.", readArray.length, readBytes);
Assert.assertArrayEquals("Unexpected array read back.", array, readArray);
readArray = inputStream.readArray();
Assert.assertEquals("Unexpected number of bytes read for array.", array.length, readArray.length);
Assert.assertArrayEquals("Unexpected array read back.", array, readArray);
Assert.assertEquals("Not expecting any more data. ", -1, inputStream.read());
AssertExtensions.assertThrows("Expecting EOF.", () -> inputStream.readFully(new byte[1]), ex -> ex instanceof EOFException);
}
use of io.pravega.common.util.ByteArraySegment in project pravega by pravega.
the class BTreeIndex method processSplitPage.
/**
* Processes a Page Split result. The first split page will replace the existing page, while the remaining pages
* will need to be inserted as children into the parent.
*
* @param splitResult The result of the original BTreePage's splitIfNecessary() call.
* @param context Processing context.
*/
private void processSplitPage(List<BTreePage> splitResult, PageModificationContext context) {
PageWrapper originalPage = context.getPageWrapper();
for (int i = 0; i < splitResult.size(); i++) {
val page = splitResult.get(i);
ByteArraySegment newPageKey;
long newOffset;
long minOffset;
PageWrapper processedPage;
if (i == 0) {
// The original page will be replaced by the first split. Nothing changes about its pointer key.
originalPage.setPage(page);
newPageKey = originalPage.getPageKey();
context.getPageCollection().complete(originalPage);
processedPage = originalPage;
} else {
// Insert the new pages and assign them new virtual offsets. Each page will use its first
// Key as a Page Key.
newPageKey = page.getKeyAt(0);
processedPage = PageWrapper.wrapNew(page, originalPage.getParent(), new PagePointer(newPageKey, PagePointer.NO_OFFSET, page.getLength()));
context.getPageCollection().insert(processedPage);
context.getPageCollection().complete(processedPage);
}
// Fetch new offset, and update minimum offsets.
newOffset = processedPage.getOffset();
minOffset = calculateMinOffset(processedPage);
processedPage.setMinOffset(minOffset);
// Record changes.
context.updatePagePointer(new PagePointer(newPageKey, newOffset, page.getLength(), minOffset));
}
}
use of io.pravega.common.util.ByteArraySegment in project pravega by pravega.
the class BTreeIndex method get.
/**
* Looks up the value of multiple keys.
*
* @param keys A list of ByteArraySegments representing the keys to look up.
* @param timeout Timeout for the operation.
* @return A CompletableFuture that, when completed normally, will contain a List with ByteArraySegments representing
* the values associated with the given keys. The values in this list will be in the same order as the given Keys, so
* they can be matched to their sought keys by their index. If the operation failed, the Future
* will be completed with the appropriate exception.
*/
public CompletableFuture<List<ByteArraySegment>> get(@NonNull List<ByteArraySegment> keys, @NonNull Duration timeout) {
if (keys.size() == 1) {
// Shortcut for single key.
return get(keys.get(0), timeout).thenApply(Collections::singletonList);
}
// Lookup all the keys in parallel, and make sure to apply their resulting values in the same order that their keys
// where provided to us.
ensureInitialized();
TimeoutTimer timer = new TimeoutTimer(timeout);
PageCollection pageCollection = new PageCollection(this.state.length);
val gets = keys.stream().map(key -> locatePage(key, pageCollection, timer).thenApplyAsync(page -> page.getPage().searchExact(key), this.executor)).collect(Collectors.toList());
return Futures.allOfWithResults(gets);
}
use of io.pravega.common.util.ByteArraySegment in project pravega by pravega.
the class ByteBufferOutputStreamTests method testDirectDataOutput.
/**
* Tests the {@link DirectDataOutput} implementation.
*/
@Test
public void testDirectDataOutput() {
final int shortCount = 21;
final int intCount = 19;
final int longCount = 17;
final int bufferSize = 919;
final int maxSize = shortCount * Short.BYTES + intCount * Integer.BYTES + longCount * Long.BYTES + bufferSize;
@Cleanup val s = new ByteBufferOutputStream(INITIAL_LENGTH);
val expected = new ByteArraySegment(new byte[maxSize]);
int offset = 0;
val rnd = new Random(0);
// writeShort()
for (int i = 0; i < shortCount; i++) {
short n = (short) (rnd.nextInt() % (2 * Short.MAX_VALUE) - Short.MAX_VALUE);
s.writeShort(n);
expected.setShort(offset, n);
offset += Short.BYTES;
}
// writeInt()
for (int i = 0; i < intCount; i++) {
int n = rnd.nextInt();
s.writeInt(n);
expected.setInt(offset, n);
offset += Integer.BYTES;
}
// writeLong()
for (int i = 0; i < longCount; i++) {
long n = rnd.nextLong();
s.writeLong(n);
expected.setLong(offset, n);
offset += Long.BYTES;
}
// writeBuffer()
val randomBuffer = new byte[bufferSize];
rnd.nextBytes(randomBuffer);
s.writeBuffer(new ByteArraySegment(randomBuffer));
expected.copyFrom(new ByteArraySegment(randomBuffer), offset, randomBuffer.length);
// Check.
Assert.assertEquals(expected, s.getData());
}
Aggregations