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);
}
}
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);
}
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;
}
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;
}
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));
})));
}
Aggregations