use of org.janusgraph.diskstorage.StaticBuffer in project janusgraph by JanusGraph.
the class CQLStoreManager method mutateManyLogged.
// Use a single logged batch
private void mutateManyLogged(final Map<String, Map<StaticBuffer, KCVMutation>> mutations, final StoreTransaction txh) throws BackendException {
final MaskedTimestamp commitTime = new MaskedTimestamp(txh);
final BatchStatement batchStatement = new BatchStatement(Type.LOGGED);
batchStatement.setConsistencyLevel(getTransaction(txh).getWriteConsistencyLevel());
batchStatement.addAll(Iterator.ofAll(mutations.entrySet()).flatMap(tableNameAndMutations -> {
final String tableName = tableNameAndMutations.getKey();
final Map<StaticBuffer, KCVMutation> tableMutations = tableNameAndMutations.getValue();
final CQLKeyColumnValueStore columnValueStore = Option.of(this.openStores.get(tableName)).getOrElseThrow(() -> new IllegalStateException("Store cannot be found: " + tableName));
return Iterator.ofAll(tableMutations.entrySet()).flatMap(keyAndMutations -> {
final StaticBuffer key = keyAndMutations.getKey();
final KCVMutation keyMutations = keyAndMutations.getValue();
final Iterator<Statement> deletions = Iterator.of(commitTime.getDeletionTime(this.times)).flatMap(deleteTime -> Iterator.ofAll(keyMutations.getDeletions()).map(deletion -> columnValueStore.deleteColumn(key, deletion, deleteTime)));
final Iterator<Statement> additions = Iterator.of(commitTime.getAdditionTime(this.times)).flatMap(addTime -> Iterator.ofAll(keyMutations.getAdditions()).map(addition -> columnValueStore.insertColumn(key, addition, addTime)));
return Iterator.concat(deletions, additions);
});
}));
final Future<ResultSet> result = Future.fromJavaFuture(this.executorService, this.session.executeAsync(batchStatement));
result.await();
if (result.isFailure()) {
throw EXCEPTION_MAPPER.apply(result.getCause().get());
}
sleepAfterWrite(txh, commitTime);
}
use of org.janusgraph.diskstorage.StaticBuffer 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 org.janusgraph.diskstorage.StaticBuffer 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());
}
}
}
use of org.janusgraph.diskstorage.StaticBuffer in project janusgraph by JanusGraph.
the class HBaseStoreManager method mutateMany.
@Override
public void mutateMany(Map<String, Map<StaticBuffer, KCVMutation>> mutations, StoreTransaction txh) throws BackendException {
final MaskedTimestamp commitTime = new MaskedTimestamp(txh);
// In case of an addition and deletion with identical timestamps, the
// deletion tombstone wins.
// http://hbase.apache.org/book/versions.html#d244e4250
final Map<StaticBuffer, Pair<List<Put>, Delete>> commandsPerKey = convertToCommands(mutations, commitTime.getAdditionTime(times), commitTime.getDeletionTime(times));
// actual batch operation
final List<Row> batch = new ArrayList<>(commandsPerKey.size());
// convert sorted commands into representation required for 'batch' operation
for (Pair<List<Put>, Delete> commands : commandsPerKey.values()) {
if (commands.getFirst() != null && !commands.getFirst().isEmpty())
batch.addAll(commands.getFirst());
if (commands.getSecond() != null)
batch.add(commands.getSecond());
}
try {
TableMask table = null;
try {
table = cnx.getTable(tableName);
table.batch(batch, new Object[batch.size()]);
} finally {
IOUtils.closeQuietly(table);
}
} catch (IOException | InterruptedException e) {
throw new TemporaryBackendException(e);
}
sleepAfterWrite(txh, commitTime);
}
use of org.janusgraph.diskstorage.StaticBuffer in project janusgraph by JanusGraph.
the class HBaseStoreManagerMutationTest method testKCVMutationToPuts.
@Test
public void testKCVMutationToPuts() throws Exception {
final Map<String, Map<StaticBuffer, KCVMutation>> storeMutationMap = new HashMap<>();
final Map<StaticBuffer, KCVMutation> rowkeyMutationMap = new HashMap<>();
final List<Long> expectedColumnsWithTTL = new ArrayList<>();
final List<Long> expectedColumnsWithoutTTL = new ArrayList<>();
final List<Long> expectedColumnDelete = new ArrayList<>();
StaticArrayEntry e = null;
StaticBuffer rowkey, col, val;
// 2 rows
for (int row = 0; row < 2; row++) {
rowkey = KeyColumnValueStoreUtil.longToByteBuffer(row);
List<Entry> additions = new ArrayList<>();
List<StaticBuffer> deletions = new ArrayList<>();
// 100 columns each row
int i;
for (i = 0; i < 100; i++) {
col = KeyColumnValueStoreUtil.longToByteBuffer(i);
val = KeyColumnValueStoreUtil.longToByteBuffer(i + 100);
e = (StaticArrayEntry) StaticArrayEntry.of(col, val);
// Set half of the columns with TTL, also vary the TTL values
if (i % 2 == 0) {
e.setMetaData(EntryMetaData.TTL, i % 10 + 1);
// Collect the columns with TTL. Only do this for one row
if (row == 1) {
expectedColumnsWithTTL.add((long) i);
}
additions.add(e);
} else {
// Collect the columns without TTL. Only do this for one row
if (row == 1) {
expectedColumnsWithoutTTL.add((long) i);
}
additions.add(e);
}
}
// Add one deletion to the row
if (row == 1) {
expectedColumnDelete.add((long) (i - 1));
}
deletions.add(e);
rowkeyMutationMap.put(rowkey, new KCVMutation(additions, deletions));
}
storeMutationMap.put("store1", rowkeyMutationMap);
HBaseStoreManager manager = new HBaseStoreManager(HBaseStorageSetup.getHBaseConfiguration());
final Map<StaticBuffer, Pair<List<Put>, Delete>> commandsPerRowKey = manager.convertToCommands(storeMutationMap, 0, 0);
// 2 rows
Assert.assertEquals(commandsPerRowKey.size(), 2);
// Verify puts
final List<Long> putColumnsWithTTL = new ArrayList<>();
final List<Long> putColumnsWithoutTTL = new ArrayList<>();
Pair<List<Put>, Delete> commands = commandsPerRowKey.values().iterator().next();
long colName;
for (Put p : commands.getFirst()) {
// In Put, Long.MAX_VALUE means no TTL
for (Map.Entry<byte[], List<Cell>> me : p.getFamilyCellMap().entrySet()) {
for (Cell c : me.getValue()) {
colName = KeyColumnValueStoreUtil.bufferToLong(new StaticArrayBuffer(CellUtil.cloneQualifier(c)));
if (p.getTTL() < Long.MAX_VALUE) {
putColumnsWithTTL.add(colName);
} else {
putColumnsWithoutTTL.add(colName);
}
}
}
}
Collections.sort(putColumnsWithoutTTL);
Collections.sort(putColumnsWithTTL);
Assert.assertArrayEquals(expectedColumnsWithoutTTL.toArray(), putColumnsWithoutTTL.toArray());
Assert.assertArrayEquals(expectedColumnsWithTTL.toArray(), putColumnsWithTTL.toArray());
// Verify deletes
final List<Long> deleteColumns = new ArrayList<>();
Delete d = commands.getSecond();
for (Map.Entry<byte[], List<Cell>> me : d.getFamilyCellMap().entrySet()) {
for (Cell c : me.getValue()) {
colName = KeyColumnValueStoreUtil.bufferToLong(new StaticArrayBuffer(CellUtil.cloneQualifier(c)));
deleteColumns.add(colName);
}
}
Collections.sort(deleteColumns);
Assert.assertArrayEquals(expectedColumnDelete.toArray(), deleteColumns.toArray());
}
Aggregations