use of com.palantir.util.crypto.Sha256Hash in project atlasdb by palantir.
the class StreamTest method testLookupStreamIdsByHash.
@Test
public void testLookupStreamIdsByHash() throws Exception {
final byte[] bytes1 = new byte[2 * StreamTestStreamStore.BLOCK_SIZE_IN_BYTES];
final byte[] bytes2 = new byte[2 * StreamTestStreamStore.BLOCK_SIZE_IN_BYTES];
long id1 = timestampService.getFreshTimestamp();
long id2 = timestampService.getFreshTimestamp();
Random rand = new Random();
rand.nextBytes(bytes1);
rand.nextBytes(bytes2);
Sha256Hash hash1 = Sha256Hash.computeHash(bytes1);
Sha256Hash hash2 = Sha256Hash.computeHash(bytes2);
Sha256Hash hash3 = Sha256Hash.EMPTY;
ImmutableMap<Long, InputStream> streams = ImmutableMap.of(id1, new ByteArrayInputStream(bytes1), id2, new ByteArrayInputStream(bytes2));
txManager.runTaskWithRetry(t -> defaultStore.storeStreams(t, streams));
Map<Sha256Hash, Long> sha256HashLongMap = txManager.runTaskWithRetry(t -> defaultStore.lookupStreamIdsByHash(t, ImmutableSet.of(hash1, hash2, hash3)));
assertEquals(id1, sha256HashLongMap.get(hash1).longValue());
assertEquals(id2, sha256HashLongMap.get(hash2).longValue());
assertNull(sha256HashLongMap.get(hash3));
}
use of com.palantir.util.crypto.Sha256Hash in project atlasdb by palantir.
the class StreamTest method deleteStreamHashEntryHashedComponentsStream.
private void deleteStreamHashEntryHashedComponentsStream(StreamTestTableFactory tableFactory, Transaction tx, Long streamId, ByteString streamHash) {
Sha256Hash hash = new Sha256Hash(streamHash.toByteArray());
TestHashComponentsStreamHashAidxTable.TestHashComponentsStreamHashAidxRow hashRow = TestHashComponentsStreamHashAidxTable.TestHashComponentsStreamHashAidxRow.of(hash);
TestHashComponentsStreamHashAidxTable.TestHashComponentsStreamHashAidxColumn column = TestHashComponentsStreamHashAidxTable.TestHashComponentsStreamHashAidxColumn.of(streamId);
Multimap<TestHashComponentsStreamHashAidxTable.TestHashComponentsStreamHashAidxRow, TestHashComponentsStreamHashAidxTable.TestHashComponentsStreamHashAidxColumn> shToDelete = ImmutableMultimap.of(hashRow, column);
tableFactory.getTestHashComponentsStreamHashAidxTable(tx).delete(shToDelete);
}
use of com.palantir.util.crypto.Sha256Hash in project atlasdb by palantir.
the class StreamTest method deleteStreamHashEntry.
private void deleteStreamHashEntry(StreamTestTableFactory tableFactory, Transaction tx, Long streamId, ByteString streamHash) {
Sha256Hash hash = new Sha256Hash(streamHash.toByteArray());
StreamTestStreamHashAidxTable.StreamTestStreamHashAidxRow hashRow = StreamTestStreamHashAidxTable.StreamTestStreamHashAidxRow.of(hash);
StreamTestStreamHashAidxTable.StreamTestStreamHashAidxColumn column = StreamTestStreamHashAidxTable.StreamTestStreamHashAidxColumn.of(streamId);
Multimap<StreamTestStreamHashAidxTable.StreamTestStreamHashAidxRow, StreamTestStreamHashAidxTable.StreamTestStreamHashAidxColumn> shToDelete = ImmutableMultimap.of(hashRow, column);
tableFactory.getStreamTestStreamHashAidxTable(tx).delete(shToDelete);
}
use of com.palantir.util.crypto.Sha256Hash in project atlasdb by palantir.
the class TestHashComponentsStreamStore method lookupStreamIdsByHash.
@Override
public Map<Sha256Hash, Long> lookupStreamIdsByHash(Transaction t, final Set<Sha256Hash> hashes) {
if (hashes.isEmpty()) {
return ImmutableMap.of();
}
TestHashComponentsStreamHashAidxTable idx = tables.getTestHashComponentsStreamHashAidxTable(t);
Set<TestHashComponentsStreamHashAidxTable.TestHashComponentsStreamHashAidxRow> rows = getHashIndexRowsForHashes(hashes);
Multimap<TestHashComponentsStreamHashAidxTable.TestHashComponentsStreamHashAidxRow, TestHashComponentsStreamHashAidxTable.TestHashComponentsStreamHashAidxColumnValue> m = idx.getRowsMultimap(rows);
Map<Long, Sha256Hash> hashForStreams = Maps.newHashMap();
for (TestHashComponentsStreamHashAidxTable.TestHashComponentsStreamHashAidxRow r : m.keySet()) {
for (TestHashComponentsStreamHashAidxTable.TestHashComponentsStreamHashAidxColumnValue v : m.get(r)) {
Long streamId = v.getColumnName().getStreamId();
Sha256Hash hash = r.getHash();
if (hashForStreams.containsKey(streamId)) {
AssertUtils.assertAndLog(log, hashForStreams.get(streamId).equals(hash), "(BUG) Stream ID has 2 different hashes: " + streamId);
}
hashForStreams.put(streamId, hash);
}
}
Map<Long, StreamMetadata> metadata = getMetadata(t, hashForStreams.keySet());
Map<Sha256Hash, Long> ret = Maps.newHashMap();
for (Map.Entry<Long, StreamMetadata> e : metadata.entrySet()) {
if (e.getValue().getStatus() != Status.STORED) {
continue;
}
Sha256Hash hash = hashForStreams.get(e.getKey());
ret.put(hash, e.getKey());
}
return ret;
}
use of com.palantir.util.crypto.Sha256Hash in project atlasdb by palantir.
the class TestHashComponentsStreamStore method deleteStreams.
/**
* This should only be used from the cleanup tasks.
*/
void deleteStreams(Transaction t, final Set<Long> streamIds) {
if (streamIds.isEmpty()) {
return;
}
Set<TestHashComponentsStreamMetadataTable.TestHashComponentsStreamMetadataRow> smRows = Sets.newHashSet();
Multimap<TestHashComponentsStreamHashAidxTable.TestHashComponentsStreamHashAidxRow, TestHashComponentsStreamHashAidxTable.TestHashComponentsStreamHashAidxColumn> shToDelete = HashMultimap.create();
for (Long streamId : streamIds) {
smRows.add(TestHashComponentsStreamMetadataTable.TestHashComponentsStreamMetadataRow.of(streamId));
}
TestHashComponentsStreamMetadataTable table = tables.getTestHashComponentsStreamMetadataTable(t);
Map<TestHashComponentsStreamMetadataTable.TestHashComponentsStreamMetadataRow, StreamMetadata> metadatas = table.getMetadatas(smRows);
Set<TestHashComponentsStreamValueTable.TestHashComponentsStreamValueRow> streamValueToDelete = Sets.newHashSet();
for (Entry<TestHashComponentsStreamMetadataTable.TestHashComponentsStreamMetadataRow, StreamMetadata> e : metadatas.entrySet()) {
Long streamId = e.getKey().getId();
long blocks = getNumberOfBlocksFromMetadata(e.getValue());
for (long i = 0; i < blocks; i++) {
streamValueToDelete.add(TestHashComponentsStreamValueTable.TestHashComponentsStreamValueRow.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);
}
TestHashComponentsStreamHashAidxTable.TestHashComponentsStreamHashAidxRow hashRow = TestHashComponentsStreamHashAidxTable.TestHashComponentsStreamHashAidxRow.of(hash);
TestHashComponentsStreamHashAidxTable.TestHashComponentsStreamHashAidxColumn column = TestHashComponentsStreamHashAidxTable.TestHashComponentsStreamHashAidxColumn.of(streamId);
shToDelete.put(hashRow, column);
}
tables.getTestHashComponentsStreamHashAidxTable(t).delete(shToDelete);
tables.getTestHashComponentsStreamValueTable(t).delete(streamValueToDelete);
table.delete(smRows);
}
Aggregations