use of com.palantir.atlasdb.keyvalue.api.Value in project atlasdb by palantir.
the class DbKvs method getRowColumnRange.
private Iterator<Map.Entry<Cell, Value>> getRowColumnRange(TableReference tableRef, byte[] row, BatchColumnRangeSelection batchColumnRangeSelection, long timestamp) {
List<byte[]> rowList = ImmutableList.of(row);
return ClosableIterators.wrap(new AbstractPagingIterable<Entry<Cell, Value>, TokenBackedBasicResultsPage<Entry<Cell, Value>, byte[]>>() {
@Override
protected TokenBackedBasicResultsPage<Entry<Cell, Value>, byte[]> getFirstPage() throws Exception {
return page(batchColumnRangeSelection.getStartCol());
}
@Override
protected TokenBackedBasicResultsPage<Map.Entry<Cell, Value>, byte[]> getNextPage(TokenBackedBasicResultsPage<Map.Entry<Cell, Value>, byte[]> previous) throws Exception {
return page(previous.getTokenForNextPage());
}
TokenBackedBasicResultsPage<Map.Entry<Cell, Value>, byte[]> page(byte[] startCol) throws Exception {
BatchColumnRangeSelection range = BatchColumnRangeSelection.create(startCol, batchColumnRangeSelection.getEndCol(), batchColumnRangeSelection.getBatchHint());
List<Map.Entry<Cell, Value>> nextPage = Iterables.getOnlyElement(extractRowColumnRangePage(tableRef, range, timestamp, rowList).values());
if (nextPage.isEmpty()) {
return SimpleTokenBackedResultsPage.create(startCol, ImmutableList.<Entry<Cell, Value>>of(), false);
}
byte[] lastCol = nextPage.get(nextPage.size() - 1).getKey().getColumnName();
if (isEndOfColumnRange(lastCol, batchColumnRangeSelection.getEndCol())) {
return SimpleTokenBackedResultsPage.create(lastCol, nextPage, false);
}
byte[] nextCol = RangeRequests.nextLexicographicName(lastCol);
return SimpleTokenBackedResultsPage.create(nextCol, nextPage, true);
}
}.iterator());
}
use of com.palantir.atlasdb.keyvalue.api.Value in project atlasdb by palantir.
the class DbKvs method extractResults.
@SuppressWarnings("deprecation")
private Map<Cell, Value> extractResults(DbReadTable table, TableReference tableRef, ClosableIterator<AgnosticLightResultRow> rows) {
Map<Cell, Value> results = Maps.newHashMap();
Map<Cell, OverflowValue> overflowResults = Maps.newHashMap();
try (ClosableIterator<AgnosticLightResultRow> iter = rows) {
boolean hasOverflow = table.hasOverflowValues();
while (iter.hasNext()) {
AgnosticLightResultRow row = iter.next();
Cell cell = Cell.create(row.getBytes(ROW), row.getBytes(COL));
Long overflowId = hasOverflow ? row.getLongObject("overflow") : null;
if (overflowId == null) {
Value value = Value.create(row.getBytes(VAL), row.getLong(TIMESTAMP));
Value oldValue = results.put(cell, value);
if (oldValue != null && oldValue.getTimestamp() > value.getTimestamp()) {
results.put(cell, oldValue);
}
} else {
OverflowValue ov = ImmutableOverflowValue.of(row.getLong(TIMESTAMP), overflowId);
OverflowValue oldOv = overflowResults.put(cell, ov);
if (oldOv != null && oldOv.ts() > ov.ts()) {
overflowResults.put(cell, oldOv);
}
}
}
}
fillOverflowValues(table.getConnectionSupplier(), tableRef, overflowResults, results);
return results;
}
use of com.palantir.atlasdb.keyvalue.api.Value in project atlasdb by palantir.
the class DbKvs method extractRowColumnRangePageInternal.
private Map<byte[], List<Map.Entry<Cell, Value>>> extractRowColumnRangePageInternal(DbReadTable table, TableReference tableRef, Supplier<ClosableIterator<AgnosticLightResultRow>> rowLoader, Collection<byte[]> allRows) {
Map<Sha256Hash, byte[]> hashesToBytes = Maps.newHashMapWithExpectedSize(allRows.size());
Map<Sha256Hash, List<Cell>> cellsByRow = Maps.newHashMap();
for (byte[] row : allRows) {
Sha256Hash rowHash = Sha256Hash.computeHash(row);
hashesToBytes.put(rowHash, row);
cellsByRow.put(rowHash, Lists.newArrayList());
}
boolean hasOverflow = table.hasOverflowValues();
Map<Cell, Value> values = Maps.newHashMap();
Map<Cell, OverflowValue> overflowValues = Maps.newHashMap();
try (ClosableIterator<AgnosticLightResultRow> iter = rowLoader.get()) {
while (iter.hasNext()) {
AgnosticLightResultRow row = iter.next();
Cell cell = Cell.create(row.getBytes(ROW), row.getBytes(COL));
Sha256Hash rowHash = Sha256Hash.computeHash(cell.getRowName());
cellsByRow.get(rowHash).add(cell);
Long overflowId = hasOverflow ? row.getLongObject("overflow") : null;
if (overflowId == null) {
Value value = Value.create(row.getBytes(VAL), row.getLong(TIMESTAMP));
Value oldValue = values.put(cell, value);
if (oldValue != null && oldValue.getTimestamp() > value.getTimestamp()) {
values.put(cell, oldValue);
}
} else {
OverflowValue ov = ImmutableOverflowValue.of(row.getLong(TIMESTAMP), overflowId);
OverflowValue oldOv = overflowValues.put(cell, ov);
if (oldOv != null && oldOv.ts() > ov.ts()) {
overflowValues.put(cell, oldOv);
}
}
}
}
fillOverflowValues(table.getConnectionSupplier(), tableRef, overflowValues, values);
Map<byte[], List<Map.Entry<Cell, Value>>> results = Maps.newHashMapWithExpectedSize(allRows.size());
for (Entry<Sha256Hash, List<Cell>> e : cellsByRow.entrySet()) {
List<Map.Entry<Cell, Value>> fullResults = Lists.newArrayListWithExpectedSize(e.getValue().size());
for (Cell c : e.getValue()) {
fullResults.add(Iterables.getOnlyElement(ImmutableMap.of(c, values.get(c)).entrySet()));
}
results.put(hashesToBytes.get(e.getKey()), fullResults);
}
return results;
}
use of com.palantir.atlasdb.keyvalue.api.Value in project atlasdb by palantir.
the class DbKvs method loadColumnsForBatches.
private Iterator<Iterator<Map.Entry<Cell, Value>>> loadColumnsForBatches(TableReference tableRef, ColumnRangeSelection columnRangeSelection, long timestamp, Map<Sha256Hash, byte[]> rowHashesToBytes, Iterator<Map<Sha256Hash, Integer>> batches, Map<Sha256Hash, Integer> columnCountByRowHash) {
Iterator<Iterator<Map.Entry<Cell, Value>>> results = new AbstractIterator<Iterator<Map.Entry<Cell, Value>>>() {
private Sha256Hash lastRowHashInPreviousBatch = null;
private byte[] lastColumnInPreviousBatch = null;
@Override
protected Iterator<Map.Entry<Cell, Value>> computeNext() {
if (!batches.hasNext()) {
return endOfData();
}
Map<Sha256Hash, Integer> currentBatch = batches.next();
RowsColumnRangeBatchRequest columnRangeSelectionsByRow = getBatchColumnRangeSelectionsByRow(currentBatch, columnCountByRowHash);
Map<byte[], List<Map.Entry<Cell, Value>>> resultsByRow = extractRowColumnRangePage(tableRef, columnRangeSelectionsByRow, timestamp);
int totalEntries = resultsByRow.values().stream().mapToInt(List::size).sum();
if (totalEntries == 0) {
return Collections.emptyIterator();
}
// Ensure order matches that of the provided batch.
List<Map.Entry<Cell, Value>> loadedColumns = new ArrayList<>(totalEntries);
for (Sha256Hash rowHash : currentBatch.keySet()) {
byte[] row = rowHashesToBytes.get(rowHash);
loadedColumns.addAll(resultsByRow.get(row));
}
Cell lastCell = Iterables.getLast(loadedColumns).getKey();
lastRowHashInPreviousBatch = Sha256Hash.computeHash(lastCell.getRowName());
lastColumnInPreviousBatch = lastCell.getColumnName();
return loadedColumns.iterator();
}
private RowsColumnRangeBatchRequest getBatchColumnRangeSelectionsByRow(Map<Sha256Hash, Integer> columnCountsByRowHashInBatch, Map<Sha256Hash, Integer> totalColumnCountsByRowHash) {
ImmutableRowsColumnRangeBatchRequest.Builder rowsColumnRangeBatch = ImmutableRowsColumnRangeBatchRequest.builder().columnRangeSelection(columnRangeSelection);
Iterator<Map.Entry<Sha256Hash, Integer>> entries = columnCountsByRowHashInBatch.entrySet().iterator();
while (entries.hasNext()) {
Map.Entry<Sha256Hash, Integer> entry = entries.next();
Sha256Hash rowHash = entry.getKey();
byte[] row = rowHashesToBytes.get(rowHash);
boolean isPartialFirstRow = Objects.equals(lastRowHashInPreviousBatch, rowHash);
if (isPartialFirstRow) {
byte[] startCol = RangeRequests.nextLexicographicName(lastColumnInPreviousBatch);
BatchColumnRangeSelection columnRange = BatchColumnRangeSelection.create(startCol, columnRangeSelection.getEndCol(), entry.getValue());
rowsColumnRangeBatch.partialFirstRow(Maps.immutableEntry(row, columnRange));
continue;
}
boolean isFullyLoadedRow = totalColumnCountsByRowHash.get(rowHash).equals(entry.getValue());
if (isFullyLoadedRow) {
rowsColumnRangeBatch.addRowsToLoadFully(row);
} else {
Preconditions.checkArgument(!entries.hasNext(), "Only the last row should be partial.");
BatchColumnRangeSelection columnRange = BatchColumnRangeSelection.create(columnRangeSelection, entry.getValue());
rowsColumnRangeBatch.partialLastRow(Maps.immutableEntry(row, columnRange));
}
}
return rowsColumnRangeBatch.build();
}
};
return results;
}
use of com.palantir.atlasdb.keyvalue.api.Value in project atlasdb by palantir.
the class DbKvs method getRowsColumnRange.
@Override
public Map<byte[], RowColumnRangeIterator> getRowsColumnRange(TableReference tableRef, Iterable<byte[]> rows, BatchColumnRangeSelection batchColumnRangeSelection, long timestamp) {
List<byte[]> rowList = ImmutableList.copyOf(rows);
Map<byte[], List<Map.Entry<Cell, Value>>> firstPage = getFirstRowsColumnRangePage(tableRef, rowList, batchColumnRangeSelection, timestamp);
Map<byte[], RowColumnRangeIterator> ret = Maps.newHashMapWithExpectedSize(rowList.size());
for (Entry<byte[], List<Map.Entry<Cell, Value>>> e : firstPage.entrySet()) {
List<Map.Entry<Cell, Value>> results = e.getValue();
if (results.isEmpty()) {
ret.put(e.getKey(), new LocalRowColumnRangeIterator(e.getValue().iterator()));
continue;
}
byte[] lastCol = results.get(results.size() - 1).getKey().getColumnName();
RowColumnRangeIterator firstPageIter = new LocalRowColumnRangeIterator(e.getValue().iterator());
if (isEndOfColumnRange(lastCol, batchColumnRangeSelection.getEndCol())) {
ret.put(e.getKey(), firstPageIter);
} else {
byte[] nextCol = RangeRequests.nextLexicographicName(lastCol);
BatchColumnRangeSelection nextColumnRangeSelection = BatchColumnRangeSelection.create(nextCol, batchColumnRangeSelection.getEndCol(), batchColumnRangeSelection.getBatchHint());
Iterator<Map.Entry<Cell, Value>> nextPagesIter = getRowColumnRange(tableRef, e.getKey(), nextColumnRangeSelection, timestamp);
ret.put(e.getKey(), new LocalRowColumnRangeIterator(Iterators.concat(firstPageIter, nextPagesIter)));
}
}
return ret;
}
Aggregations