use of io.vavr.Tuple2 in project janusgraph by JanusGraph.
the class CQLResultSetKeyIteratorTest method testPartialIterateColumns.
@Test
public void testPartialIterateColumns() throws IOException {
final Random random = new Random();
final Function1<Integer, ByteBuffer> randomLong = idx -> {
final ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES).putLong(random.nextLong());
buffer.flip();
return buffer;
};
final Array<Tuple2<ByteBuffer, Array<Tuple2<ByteBuffer, ByteBuffer>>>> keysMap = Array.range(0, random.nextInt(100) + 100).map(randomLong).map(key -> Tuple.of(key, Array.rangeClosed(0, random.nextInt(100) + 1).map(idx -> Tuple.of(randomLong.apply(idx), randomLong.apply(idx)))));
final Seq<Row> rows = keysMap.flatMap(tuple -> tuple._2.map(columnAndValue -> {
final Row row = mock(Row.class);
when(row.getBytes("key")).thenReturn(tuple._1);
when(row.getBytes("column1")).thenReturn(columnAndValue._1);
when(row.getBytes("value")).thenReturn(columnAndValue._2);
return row;
}));
final ResultSet resultSet = mock(ResultSet.class);
when(resultSet.iterator()).thenReturn(rows.iterator());
final CQLColValGetter getter = new CQLColValGetter(new EntryMetaData[0]);
try (final CQLResultSetKeyIterator resultSetKeyIterator = new CQLResultSetKeyIterator(ALL_COLUMNS, getter, resultSet)) {
final Iterator<Tuple2<ByteBuffer, Array<Tuple2<ByteBuffer, ByteBuffer>>>> iterator = keysMap.iterator();
while (resultSetKeyIterator.hasNext()) {
final StaticBuffer next = resultSetKeyIterator.next();
try (final RecordIterator<Entry> entries = resultSetKeyIterator.getEntries()) {
final Tuple2<ByteBuffer, Array<Tuple2<ByteBuffer, ByteBuffer>>> current = iterator.next();
final ByteBuffer currentKey = current._1;
final Array<Tuple2<ByteBuffer, ByteBuffer>> columnValues = current._2;
final Iterator<Tuple2<ByteBuffer, ByteBuffer>> columnIterator = columnValues.iterator();
while (entries.hasNext()) {
final Entry entry = entries.next();
final Tuple2<ByteBuffer, ByteBuffer> columnAndValue = columnIterator.next();
assertEquals(currentKey, next.asByteBuffer());
assertEquals(columnAndValue._1, entry.getColumn().asByteBuffer());
assertEquals(columnAndValue._2, entry.getValue().asByteBuffer());
assertEquals(columnIterator.hasNext(), entries.hasNext());
// 10% of the time, don't complete the iteration
if (random.nextInt(10) == 0) {
break;
}
}
}
}
}
}
use of io.vavr.Tuple2 in project janusgraph by JanusGraph.
the class CQLResultSetKeyIteratorTest method testUneven.
@Test
public void testUneven() throws IOException {
final Random random = new Random();
final Function1<Integer, ByteBuffer> randomLong = idx -> {
final ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES).putLong(random.nextLong());
buffer.flip();
return buffer;
};
final Array<Tuple2<ByteBuffer, Array<Tuple2<ByteBuffer, ByteBuffer>>>> keysMap = Array.range(0, random.nextInt(100) + 100).map(randomLong).map(key -> Tuple.of(key, Array.rangeClosed(0, random.nextInt(100) + 1).map(idx -> Tuple.of(randomLong.apply(idx), randomLong.apply(idx)))));
final Seq<Row> rows = keysMap.flatMap(tuple -> tuple._2.map(columnAndValue -> {
final Row row = mock(Row.class);
when(row.getBytes("key")).thenReturn(tuple._1);
when(row.getBytes("column1")).thenReturn(columnAndValue._1);
when(row.getBytes("value")).thenReturn(columnAndValue._2);
return row;
}));
final ResultSet resultSet = mock(ResultSet.class);
when(resultSet.iterator()).thenReturn(rows.iterator());
final CQLColValGetter getter = new CQLColValGetter(new EntryMetaData[0]);
try (final CQLResultSetKeyIterator resultSetKeyIterator = new CQLResultSetKeyIterator(ALL_COLUMNS, getter, resultSet)) {
final Iterator<Tuple2<ByteBuffer, Array<Tuple2<ByteBuffer, ByteBuffer>>>> iterator = keysMap.iterator();
while (resultSetKeyIterator.hasNext()) {
final StaticBuffer next = resultSetKeyIterator.next();
try (final RecordIterator<Entry> entries = resultSetKeyIterator.getEntries()) {
final Tuple2<ByteBuffer, Array<Tuple2<ByteBuffer, ByteBuffer>>> current = iterator.next();
final ByteBuffer currentKey = current._1;
final Array<Tuple2<ByteBuffer, ByteBuffer>> columnValues = current._2;
final Iterator<Tuple2<ByteBuffer, ByteBuffer>> columnIterator = columnValues.iterator();
while (entries.hasNext()) {
final Entry entry = entries.next();
final Tuple2<ByteBuffer, ByteBuffer> columnAndValue = columnIterator.next();
assertEquals(currentKey, next.asByteBuffer());
assertEquals(columnAndValue._1, entry.getColumn().asByteBuffer());
assertEquals(columnAndValue._2, entry.getValue().asByteBuffer());
assertEquals(columnIterator.hasNext(), entries.hasNext());
}
}
}
}
}
use of io.vavr.Tuple2 in project janusgraph by JanusGraph.
the class CQLResultSetKeyIteratorTest method testNoIterateColumns.
@Test
public void testNoIterateColumns() throws IOException {
final Random random = new Random();
final Function1<Integer, ByteBuffer> randomLong = idx -> {
final ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES).putLong(random.nextLong());
buffer.flip();
return buffer;
};
final Array<Tuple2<ByteBuffer, Array<Tuple2<ByteBuffer, ByteBuffer>>>> keysMap = Array.range(0, random.nextInt(100) + 100).map(randomLong).map(key -> Tuple.of(key, Array.rangeClosed(0, random.nextInt(100) + 1).map(idx -> Tuple.of(randomLong.apply(idx), randomLong.apply(idx)))));
final Seq<Row> rows = keysMap.flatMap(tuple -> tuple._2.map(columnAndValue -> {
final Row row = mock(Row.class);
when(row.getBytes("key")).thenReturn(tuple._1);
when(row.getBytes("column1")).thenReturn(columnAndValue._1);
when(row.getBytes("value")).thenReturn(columnAndValue._2);
return row;
}));
final ResultSet resultSet = mock(ResultSet.class);
when(resultSet.iterator()).thenReturn(rows.iterator());
final CQLColValGetter getter = new CQLColValGetter(new EntryMetaData[0]);
try (final CQLResultSetKeyIterator resultSetKeyIterator = new CQLResultSetKeyIterator(ALL_COLUMNS, getter, resultSet)) {
final Iterator<Tuple2<ByteBuffer, Array<Tuple2<ByteBuffer, ByteBuffer>>>> iterator = keysMap.iterator();
while (resultSetKeyIterator.hasNext()) {
final StaticBuffer next = resultSetKeyIterator.next();
assertEquals(iterator.next()._1, next.asByteBuffer());
}
}
}
Aggregations