Search in sources :

Example 81 with ByteArraySegment

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);
}
Also used : ByteArraySegment(io.pravega.common.util.ByteArraySegment) CompactReaderGroupState(io.pravega.client.stream.impl.ReaderGroupState.CompactReaderGroupState)

Example 82 with ByteArraySegment

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);
}
Also used : OutputStream(java.io.OutputStream) IntStream(java.util.stream.IntStream) Charsets(com.google.common.base.Charsets) DataOutput(java.io.DataOutput) Arrays(java.util.Arrays) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AssertExtensions(io.pravega.test.common.AssertExtensions) lombok.val(lombok.val) Cleanup(lombok.Cleanup) IOException(java.io.IOException) Test(org.junit.Test) EOFException(java.io.EOFException) Function(java.util.function.Function) Supplier(java.util.function.Supplier) Collectors(java.util.stream.Collectors) ByteBufferOutputStream(io.pravega.common.io.ByteBufferOutputStream) ByteArraySegment(io.pravega.common.util.ByteArraySegment) ByteArrayInputStream(java.io.ByteArrayInputStream) DataOutputStream(java.io.DataOutputStream) DirectDataOutput(io.pravega.common.io.DirectDataOutput) SerializationException(io.pravega.common.io.SerializationException) RandomStringUtils(org.apache.commons.lang3.RandomStringUtils) Assert(org.junit.Assert) lombok.val(lombok.val) ByteArraySegment(io.pravega.common.util.ByteArraySegment) Cleanup(lombok.Cleanup) AssertExtensions(io.pravega.test.common.AssertExtensions) EOFException(java.io.EOFException)

Example 83 with ByteArraySegment

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));
    }
}
Also used : lombok.val(lombok.val) ByteArraySegment(io.pravega.common.util.ByteArraySegment)

Example 84 with ByteArraySegment

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);
}
Also used : lombok.val(lombok.val) Arrays(java.util.Arrays) Setter(lombok.Setter) BufferViewComparator(io.pravega.common.util.BufferViewComparator) Getter(lombok.Getter) SneakyThrows(lombok.SneakyThrows) RequiredArgsConstructor(lombok.RequiredArgsConstructor) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Duration(java.time.Duration) Iterator(java.util.Iterator) TimeoutTimer(io.pravega.common.TimeoutTimer) Executor(java.util.concurrent.Executor) Predicate(java.util.function.Predicate) NonNull(lombok.NonNull) Collection(java.util.Collection) lombok.val(lombok.val) AsyncIterator(io.pravega.common.util.AsyncIterator) IOException(java.io.IOException) CompletionException(java.util.concurrent.CompletionException) Collectors(java.util.stream.Collectors) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) ByteArraySegment(io.pravega.common.util.ByteArraySegment) Builder(lombok.Builder) Data(lombok.Data) Preconditions(com.google.common.base.Preconditions) IllegalDataFormatException(io.pravega.common.util.IllegalDataFormatException) Collections(java.util.Collections) Futures(io.pravega.common.concurrent.Futures) NotThreadSafe(javax.annotation.concurrent.NotThreadSafe) Collections(java.util.Collections) TimeoutTimer(io.pravega.common.TimeoutTimer)

Example 85 with ByteArraySegment

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());
}
Also used : lombok.val(lombok.val) ByteArraySegment(io.pravega.common.util.ByteArraySegment) Random(java.util.Random) Cleanup(lombok.Cleanup) Test(org.junit.Test)

Aggregations

ByteArraySegment (io.pravega.common.util.ByteArraySegment)222 lombok.val (lombok.val)158 Test (org.junit.Test)145 Cleanup (lombok.Cleanup)114 ArrayList (java.util.ArrayList)88 CompletableFuture (java.util.concurrent.CompletableFuture)58 BufferView (io.pravega.common.util.BufferView)54 HashMap (java.util.HashMap)54 List (java.util.List)52 AssertExtensions (io.pravega.test.common.AssertExtensions)50 Assert (org.junit.Assert)49 Duration (java.time.Duration)48 AtomicReference (java.util.concurrent.atomic.AtomicReference)44 Collectors (java.util.stream.Collectors)42 IOException (java.io.IOException)41 AtomicLong (java.util.concurrent.atomic.AtomicLong)41 IntentionalException (io.pravega.test.common.IntentionalException)40 Random (java.util.Random)40 Map (java.util.Map)39 Rule (org.junit.Rule)39