Search in sources :

Example 91 with ByteArraySegment

use of io.pravega.common.util.ByteArraySegment in project pravega by pravega.

the class BTreeIndexTests method testIterator.

/**
 * Tests the ability to iterate through entries using {@link BTreeIndex#iterator}.
 */
@Test
public void testIterator() {
    final int count = 1000;
    val ds = new DataSource();
    val index = defaultBuilder(ds).build();
    index.initialize(TIMEOUT).join();
    val entries = generate(count);
    index.update(entries, TIMEOUT).join();
    sort(entries);
    for (int i = 0; i < entries.size() / 2; i++) {
        int startIndex = i;
        int endIndex = entries.size() - i - 1;
        ByteArraySegment firstKey = entries.get(startIndex).getKey();
        ByteArraySegment lastKey = entries.get(endIndex).getKey();
        // We make sure that throughout the test we check all possible combinations of firstInclusive & lastInclusive.
        boolean firstInclusive = i % 2 == 0;
        boolean lastInclusive = i % 4 < 2;
        if (i == entries.size() / 2) {
            // For same keys, they must both be inclusive.
            firstInclusive = true;
            lastInclusive = true;
        }
        val iterator = index.iterator(firstKey, firstInclusive, lastKey, lastInclusive, TIMEOUT);
        val actualEntries = new ArrayList<PageEntry>();
        iterator.forEachRemaining(actualEntries::addAll, executorService()).join();
        // Determine expected keys.
        if (!firstInclusive) {
            startIndex++;
        }
        if (!lastInclusive) {
            endIndex--;
        }
        val expectedEntries = entries.subList(startIndex, endIndex + 1);
        AssertExtensions.assertListEquals("Wrong result for " + i + ".", expectedEntries, actualEntries, (e, a) -> KEY_COMPARATOR.compare(e.getKey(), a.getKey()) == 0 && KEY_COMPARATOR.compare(e.getValue(), a.getValue()) == 0);
    }
}
Also used : lombok.val(lombok.val) ByteArraySegment(io.pravega.common.util.ByteArraySegment) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 92 with ByteArraySegment

use of io.pravega.common.util.ByteArraySegment in project pravega by pravega.

the class BTreeIndexTests method testUpdate.

/**
 * Tests the put() method with the ability to replace entries.
 */
@Test
public void testUpdate() {
    final int count = 10000;
    val ds = new DataSource();
    val index = defaultBuilder(ds).build();
    index.initialize(TIMEOUT).join();
    val entries = generate(count);
    sort(entries);
    index.update(entries, TIMEOUT).join();
    // Delete every 1/3 of the keys
    val toUpdate = new ArrayList<PageEntry>();
    val expectedEntries = new ArrayList<PageEntry>(entries);
    val rnd = new Random(0);
    for (int i = entries.size() - 1; i >= 0; i--) {
        PageEntry e = expectedEntries.get(i);
        boolean delete = i % 3 == 0;
        boolean update = i % 2 == 0;
        if (delete && !update) {
            // Delete about 1/3 of the entries.
            toUpdate.add(PageEntry.noValue(expectedEntries.get(i).getKey()));
            expectedEntries.remove(i);
        }
        if (update) {
            // Update (reinsert or update) 1/2 of the entries.
            val newValue = new byte[VALUE_LENGTH];
            rnd.nextBytes(newValue);
            e = new PageEntry(e.getKey(), new ByteArraySegment(newValue));
            toUpdate.add(e);
            expectedEntries.set(i, e);
        }
    }
    // Perform the removals and updates.
    index.update(toUpdate, TIMEOUT).join();
    // Verify final result.
    check("Unexpected index contents.", index, expectedEntries, 0);
}
Also used : lombok.val(lombok.val) ByteArraySegment(io.pravega.common.util.ByteArraySegment) Random(java.util.Random) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 93 with ByteArraySegment

use of io.pravega.common.util.ByteArraySegment in project pravega by pravega.

the class BTreePageTests method serializeLong.

private ByteArraySegment serializeLong(long value) {
    ByteArraySegment r = new ByteArraySegment(new byte[Long.BYTES]);
    r.setLong(0, value);
    return r;
}
Also used : ByteArraySegment(io.pravega.common.util.ByteArraySegment)

Example 94 with ByteArraySegment

use of io.pravega.common.util.ByteArraySegment in project pravega by pravega.

the class Scope method getIdInBytes.

default byte[] getIdInBytes(UUID id) {
    byte[] b = new byte[2 * Long.BYTES];
    BitConverter.writeUUID(new ByteArraySegment(b), id);
    return b;
}
Also used : ByteArraySegment(io.pravega.common.util.ByteArraySegment)

Example 95 with ByteArraySegment

use of io.pravega.common.util.ByteArraySegment in project pravega by pravega.

the class ZKScope method addKVTableToScope.

public CompletableFuture<Void> addKVTableToScope(String kvt, UUID id) {
    return Futures.toVoid(getKVTableInScopeZNodePath(this.scopeName, kvt).thenCompose(path -> store.createZNodeIfNotExist(path).thenCompose(x -> {
        byte[] b = new byte[2 * Long.BYTES];
        BitConverter.writeUUID(new ByteArraySegment(b), id);
        return store.setData(path, b, new Version.IntVersion(0));
    })));
}
Also used : NotImplementedException(org.apache.commons.lang3.NotImplementedException) OperationContext(io.pravega.controller.store.stream.OperationContext) SneakyThrows(lombok.SneakyThrows) Exceptions(io.pravega.common.Exceptions) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) RevisionDataInput(io.pravega.common.io.serialization.RevisionDataInput) BitConverter(io.pravega.common.util.BitConverter) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) Strings(com.google.common.base.Strings) ZKPaths(org.apache.curator.utils.ZKPaths) StoreException(io.pravega.controller.store.stream.StoreException) Pair(org.apache.commons.lang3.tuple.Pair) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LinkedList(java.util.LinkedList) RevisionDataOutput(io.pravega.common.io.serialization.RevisionDataOutput) VersionedSerializer(io.pravega.common.io.serialization.VersionedSerializer) Executor(java.util.concurrent.Executor) Predicate(java.util.function.Predicate) IOException(java.io.IOException) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) ObjectBuilder(io.pravega.common.ObjectBuilder) Base64(java.util.Base64) List(java.util.List) ByteArraySegment(io.pravega.common.util.ByteArraySegment) Builder(lombok.Builder) Data(lombok.Data) Preconditions(com.google.common.base.Preconditions) Comparator(java.util.Comparator) Futures(io.pravega.common.concurrent.Futures) ByteArraySegment(io.pravega.common.util.ByteArraySegment)

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