use of com.palantir.util.crypto.Sha256Hash in project atlasdb by palantir.
the class TestHashComponentsStreamStore method putHashIndexTask.
private void putHashIndexTask(Transaction t, Map<TestHashComponentsStreamMetadataTable.TestHashComponentsStreamMetadataRow, StreamMetadata> rowsToMetadata) {
Multimap<TestHashComponentsStreamHashAidxTable.TestHashComponentsStreamHashAidxRow, TestHashComponentsStreamHashAidxTable.TestHashComponentsStreamHashAidxColumnValue> indexMap = HashMultimap.create();
for (Entry<TestHashComponentsStreamMetadataTable.TestHashComponentsStreamMetadataRow, StreamMetadata> e : rowsToMetadata.entrySet()) {
TestHashComponentsStreamMetadataTable.TestHashComponentsStreamMetadataRow row = e.getKey();
StreamMetadata metadata = e.getValue();
Preconditions.checkArgument(metadata.getStatus() == Status.STORED, "Should only index successfully stored streams.");
Sha256Hash hash = Sha256Hash.EMPTY;
if (metadata.getHash() != com.google.protobuf.ByteString.EMPTY) {
hash = new Sha256Hash(metadata.getHash().toByteArray());
}
TestHashComponentsStreamHashAidxTable.TestHashComponentsStreamHashAidxRow hashRow = TestHashComponentsStreamHashAidxTable.TestHashComponentsStreamHashAidxRow.of(hash);
TestHashComponentsStreamHashAidxTable.TestHashComponentsStreamHashAidxColumn column = TestHashComponentsStreamHashAidxTable.TestHashComponentsStreamHashAidxColumn.of(row.getId());
TestHashComponentsStreamHashAidxTable.TestHashComponentsStreamHashAidxColumnValue columnValue = TestHashComponentsStreamHashAidxTable.TestHashComponentsStreamHashAidxColumnValue.of(column, 0L);
indexMap.put(hashRow, columnValue);
}
TestHashComponentsStreamHashAidxTable hiTable = tables.getTestHashComponentsStreamHashAidxTable(t);
hiTable.put(indexMap);
}
use of com.palantir.util.crypto.Sha256Hash in project atlasdb by palantir.
the class ProfileStoreTest method testStoreImage.
@Test
public void testStoreImage() {
final UUID userId = storeUser();
storeImage(userId);
runWithRetry(store -> {
InputStream image = store.getImageForUser(userId);
try {
Sha256Hash hash = Sha256Hash.createFrom(image);
Assert.assertEquals(Sha256Hash.computeHash(IMAGE), hash);
} catch (IOException e) {
throw Throwables.throwUncheckedException(e);
} finally {
Closeables.closeQuietly(image);
}
return null;
});
}
use of com.palantir.util.crypto.Sha256Hash in project atlasdb by palantir.
the class UserPhotosStreamStore method deleteStreams.
/**
* This should only be used from the cleanup tasks.
*/
void deleteStreams(Transaction t, final Set<Long> streamIds) {
if (streamIds.isEmpty()) {
return;
}
Set<UserPhotosStreamMetadataTable.UserPhotosStreamMetadataRow> smRows = Sets.newHashSet();
Multimap<UserPhotosStreamHashAidxTable.UserPhotosStreamHashAidxRow, UserPhotosStreamHashAidxTable.UserPhotosStreamHashAidxColumn> shToDelete = HashMultimap.create();
for (Long streamId : streamIds) {
smRows.add(UserPhotosStreamMetadataTable.UserPhotosStreamMetadataRow.of(streamId));
}
UserPhotosStreamMetadataTable table = tables.getUserPhotosStreamMetadataTable(t);
Map<UserPhotosStreamMetadataTable.UserPhotosStreamMetadataRow, StreamMetadata> metadatas = table.getMetadatas(smRows);
Set<UserPhotosStreamValueTable.UserPhotosStreamValueRow> streamValueToDelete = Sets.newHashSet();
for (Entry<UserPhotosStreamMetadataTable.UserPhotosStreamMetadataRow, StreamMetadata> e : metadatas.entrySet()) {
Long streamId = e.getKey().getId();
long blocks = getNumberOfBlocksFromMetadata(e.getValue());
for (long i = 0; i < blocks; i++) {
streamValueToDelete.add(UserPhotosStreamValueTable.UserPhotosStreamValueRow.of(streamId, i));
}
ByteString streamHash = e.getValue().getHash();
Sha256Hash hash = Sha256Hash.EMPTY;
if (streamHash != com.google.protobuf.ByteString.EMPTY) {
hash = new Sha256Hash(streamHash.toByteArray());
} else {
log.error("Empty hash for stream {}", streamId);
}
UserPhotosStreamHashAidxTable.UserPhotosStreamHashAidxRow hashRow = UserPhotosStreamHashAidxTable.UserPhotosStreamHashAidxRow.of(hash);
UserPhotosStreamHashAidxTable.UserPhotosStreamHashAidxColumn column = UserPhotosStreamHashAidxTable.UserPhotosStreamHashAidxColumn.of(streamId);
shToDelete.put(hashRow, column);
}
tables.getUserPhotosStreamHashAidxTable(t).delete(shToDelete);
tables.getUserPhotosStreamValueTable(t).delete(streamValueToDelete);
table.delete(smRows);
}
use of com.palantir.util.crypto.Sha256Hash in project atlasdb by palantir.
the class KeyValueServices method filterGetRowsToColumnRange.
// TODO(gsheasby): kill this when we can properly implement this on all KVSes
public static Map<byte[], RowColumnRangeIterator> filterGetRowsToColumnRange(KeyValueService kvs, TableReference tableRef, Iterable<byte[]> rows, BatchColumnRangeSelection columnRangeSelection, long timestamp) {
log.warn("Using inefficient postfiltering for getRowsColumnRange because the KVS doesn't support it natively. " + "Production environments should use a KVS with a proper implementation.");
Map<Cell, Value> allValues = kvs.getRows(tableRef, rows, ColumnSelection.all(), timestamp);
Map<Sha256Hash, byte[]> hashesToBytes = Maps.newHashMap();
Map<Sha256Hash, ImmutableSortedMap.Builder<byte[], Value>> rowsToColumns = Maps.newHashMap();
for (byte[] row : rows) {
Sha256Hash rowHash = Sha256Hash.computeHash(row);
hashesToBytes.put(rowHash, row);
ImmutableSortedMap.Builder<byte[], Value> builder = ImmutableSortedMap.<byte[], Value>orderedBy(UnsignedBytes.lexicographicalComparator());
rowsToColumns.put(rowHash, builder);
}
for (Map.Entry<Cell, Value> e : allValues.entrySet()) {
Sha256Hash rowHash = Sha256Hash.computeHash(e.getKey().getRowName());
rowsToColumns.get(rowHash).put(e.getKey().getColumnName(), e.getValue());
}
Map<byte[], RowColumnRangeIterator> results = Maps.newHashMap();
for (Map.Entry<Sha256Hash, ImmutableSortedMap.Builder<byte[], Value>> row : rowsToColumns.entrySet()) {
SortedMap<byte[], Value> map = row.getValue().build();
Set<Map.Entry<byte[], Value>> subMap;
if ((columnRangeSelection.getStartCol().length == 0) && (columnRangeSelection.getEndCol().length == 0)) {
subMap = map.entrySet();
} else if (columnRangeSelection.getStartCol().length == 0) {
subMap = map.headMap(columnRangeSelection.getEndCol()).entrySet();
} else if (columnRangeSelection.getEndCol().length == 0) {
subMap = map.tailMap(columnRangeSelection.getStartCol()).entrySet();
} else {
subMap = map.subMap(columnRangeSelection.getStartCol(), columnRangeSelection.getEndCol()).entrySet();
}
byte[] rowName = hashesToBytes.get(row.getKey());
results.put(hashesToBytes.get(row.getKey()), new LocalRowColumnRangeIterator(Iterators.transform(subMap.iterator(), e -> Pair.<Cell, Value>of(Cell.create(rowName, e.getKey()), e.getValue()))));
}
return results;
}
use of com.palantir.util.crypto.Sha256Hash in project atlasdb by palantir.
the class RowColumnRangeExtractor method extractResults.
public void extractResults(Iterable<byte[]> canonicalRows, Map<ByteBuffer, List<ColumnOrSuperColumn>> colsByKey, long startTs) {
// Make sure returned maps are keyed by the given rows
Map<Sha256Hash, byte[]> canonicalRowsByHash = Maps.uniqueIndex(canonicalRows, Sha256Hash::computeHash);
for (Map.Entry<ByteBuffer, List<ColumnOrSuperColumn>> colEntry : colsByKey.entrySet()) {
byte[] rawRow = CassandraKeyValueServices.getBytesFromByteBuffer(colEntry.getKey());
byte[] row = canonicalRowsByHash.get(Sha256Hash.computeHash(rawRow));
List<ColumnOrSuperColumn> columns = colEntry.getValue();
if (!columns.isEmpty()) {
rowsToLastCompositeColumns.put(row, columns.get(columns.size() - 1).getColumn());
} else {
emptyRows.add(row);
}
rowsToRawColumnCount.put(row, columns.size());
for (ColumnOrSuperColumn c : columns) {
Pair<byte[], Long> pair = CassandraKeyValueServices.decomposeName(c.getColumn());
internalExtractResult(startTs, row, pair.lhSide, c.getColumn().getValue(), pair.rhSide);
}
}
}
Aggregations