Search in sources :

Example 31 with Sha256Hash

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));
}
Also used : Random(java.util.Random) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Sha256Hash(com.palantir.util.crypto.Sha256Hash) Test(org.junit.Test)

Example 32 with Sha256Hash

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);
}
Also used : Sha256Hash(com.palantir.util.crypto.Sha256Hash) TestHashComponentsStreamHashAidxTable(com.palantir.atlasdb.schema.stream.generated.TestHashComponentsStreamHashAidxTable)

Example 33 with Sha256Hash

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);
}
Also used : Sha256Hash(com.palantir.util.crypto.Sha256Hash) StreamTestStreamHashAidxTable(com.palantir.atlasdb.schema.stream.generated.StreamTestStreamHashAidxTable)

Example 34 with Sha256Hash

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;
}
Also used : Sha256Hash(com.palantir.util.crypto.Sha256Hash) StreamMetadata(com.palantir.atlasdb.protos.generated.StreamPersistence.StreamMetadata) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 35 with Sha256Hash

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);
}
Also used : ByteString(com.google.protobuf.ByteString) Sha256Hash(com.palantir.util.crypto.Sha256Hash) StreamMetadata(com.palantir.atlasdb.protos.generated.StreamPersistence.StreamMetadata)

Aggregations

Sha256Hash (com.palantir.util.crypto.Sha256Hash)40 StreamMetadata (com.palantir.atlasdb.protos.generated.StreamPersistence.StreamMetadata)27 Map (java.util.Map)13 ImmutableMap (com.google.common.collect.ImmutableMap)11 ByteString (com.google.protobuf.ByteString)9 ByteArrayInputStream (java.io.ByteArrayInputStream)5 LinkedHashMap (java.util.LinkedHashMap)5 Cell (com.palantir.atlasdb.keyvalue.api.Cell)4 Value (com.palantir.atlasdb.keyvalue.api.Value)3 InputStream (java.io.InputStream)3 HashMap (java.util.HashMap)3 List (java.util.List)3 SortedMap (java.util.SortedMap)3 Test (org.junit.Test)3 ImmutableList (com.google.common.collect.ImmutableList)2 RowColumnRangeIterator (com.palantir.atlasdb.keyvalue.api.RowColumnRangeIterator)2 StreamTestStreamHashAidxTable (com.palantir.atlasdb.schema.stream.generated.StreamTestStreamHashAidxTable)2 ArrayList (java.util.ArrayList)2 Entry (java.util.Map.Entry)2 NavigableMap (java.util.NavigableMap)2