Search in sources :

Example 6 with Sha256Hash

use of com.palantir.util.crypto.Sha256Hash in project atlasdb by palantir.

the class StreamTest method testAddDelete.

@Test
public void testAddDelete() throws Exception {
    final byte[] data = PtBytes.toBytes("streamed");
    final long streamId = txManager.runTaskWithRetry((TransactionTask<Long, Exception>) t -> {
        Sha256Hash hash = Sha256Hash.computeHash(data);
        byte[] reference = "ref".getBytes();
        return defaultStore.getByHashOrStoreStreamAndMarkAsUsed(t, hash, new ByteArrayInputStream(data), reference);
    });
    txManager.runTaskWithRetry((TransactionTask<Void, Exception>) t -> {
        Optional<InputStream> inputStream = defaultStore.loadSingleStream(t, streamId);
        assertTrue(inputStream.isPresent());
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        outputStream.write(inputStream.get());
        assertArrayEquals(data, outputStream.toByteArray());
        return null;
    });
}
Also used : StreamTestWithHashStreamMetadataRow(com.palantir.atlasdb.schema.stream.generated.StreamTestWithHashStreamMetadataTable.StreamTestWithHashStreamMetadataRow) Arrays(java.util.Arrays) Matchers.not(org.hamcrest.Matchers.not) TestHashComponentsStreamValueRow(com.palantir.atlasdb.schema.stream.generated.TestHashComponentsStreamValueTable.TestHashComponentsStreamValueRow) Random(java.util.Random) Assert.assertThat(org.junit.Assert.assertThat) Future(java.util.concurrent.Future) TestHashComponentsStreamIdxRow(com.palantir.atlasdb.schema.stream.generated.TestHashComponentsStreamIdxTable.TestHashComponentsStreamIdxRow) ByteArrayInputStream(java.io.ByteArrayInputStream) PTExecutors(com.palantir.common.concurrent.PTExecutors) StreamTestTableFactory(com.palantir.atlasdb.schema.stream.generated.StreamTestTableFactory) Map(java.util.Map) Assert.fail(org.junit.Assert.fail) StreamTestWithHashStreamMetadataTable(com.palantir.atlasdb.schema.stream.generated.StreamTestWithHashStreamMetadataTable) StreamTestWithHashStreamStore(com.palantir.atlasdb.schema.stream.generated.StreamTestWithHashStreamStore) Parameterized(org.junit.runners.Parameterized) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) Collection(java.util.Collection) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) Set(java.util.Set) StreamTestStreamHashAidxTable(com.palantir.atlasdb.schema.stream.generated.StreamTestStreamHashAidxTable) Sets(com.google.common.collect.Sets) ByteString(com.google.protobuf.ByteString) TransactionConflictException(com.palantir.atlasdb.transaction.api.TransactionConflictException) CountDownLatch(java.util.concurrent.CountDownLatch) IOUtils(org.apache.commons.io.IOUtils) Transaction(com.palantir.atlasdb.transaction.api.Transaction) Assert.assertFalse(org.junit.Assert.assertFalse) StreamTestStreamStore(com.palantir.atlasdb.schema.stream.generated.StreamTestStreamStore) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Optional(java.util.Optional) Matchers.is(org.hamcrest.Matchers.is) TestHashComponentsStreamStore(com.palantir.atlasdb.schema.stream.generated.TestHashComponentsStreamStore) Iterables(com.google.common.collect.Iterables) StreamMetadata(com.palantir.atlasdb.protos.generated.StreamPersistence.StreamMetadata) StreamTestStreamMetadataTable(com.palantir.atlasdb.schema.stream.generated.StreamTestStreamMetadataTable) RunWith(org.junit.runner.RunWith) Parameters(org.junit.runners.Parameterized.Parameters) Schemas(com.palantir.atlasdb.table.description.Schemas) Multimap(com.google.common.collect.Multimap) AtlasDbTestCase(com.palantir.atlasdb.AtlasDbTestCase) PtBytes(com.palantir.atlasdb.encoding.PtBytes) StreamTestStreamValueTable(com.palantir.atlasdb.schema.stream.generated.StreamTestStreamValueTable) Assert.assertArrayEquals(org.junit.Assert.assertArrayEquals) TestHashComponentsStreamMetadataRow(com.palantir.atlasdb.schema.stream.generated.TestHashComponentsStreamMetadataTable.TestHashComponentsStreamMetadataRow) StreamTestWithHashStreamIdxRow(com.palantir.atlasdb.schema.stream.generated.StreamTestWithHashStreamIdxTable.StreamTestWithHashStreamIdxRow) StreamTestWithHashStreamValueRow(com.palantir.atlasdb.schema.stream.generated.StreamTestWithHashStreamValueTable.StreamTestWithHashStreamValueRow) PersistentStreamStore(com.palantir.atlasdb.stream.PersistentStreamStore) TestHashComponentsStreamMetadataTable(com.palantir.atlasdb.schema.stream.generated.TestHashComponentsStreamMetadataTable) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) Sha256Hash(com.palantir.util.crypto.Sha256Hash) ExecutorService(java.util.concurrent.ExecutorService) Before(org.junit.Before) Assert.assertNotNull(org.junit.Assert.assertNotNull) TestHashComponentsStreamHashAidxTable(com.palantir.atlasdb.schema.stream.generated.TestHashComponentsStreamHashAidxTable) Assert.assertTrue(org.junit.Assert.assertTrue) Throwables(com.google.common.base.Throwables) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) Test(org.junit.Test) File(java.io.File) DeletingStreamStore(com.palantir.atlasdb.schema.stream.generated.DeletingStreamStore) Pair(com.palantir.util.Pair) Futures(com.google.common.util.concurrent.Futures) StreamPersistence(com.palantir.atlasdb.protos.generated.StreamPersistence) Assert.assertNull(org.junit.Assert.assertNull) Rule(org.junit.Rule) TransactionTask(com.palantir.atlasdb.transaction.api.TransactionTask) StreamTestMaxMemStreamStore(com.palantir.atlasdb.schema.stream.generated.StreamTestMaxMemStreamStore) Assert.assertEquals(org.junit.Assert.assertEquals) TemporaryFolder(org.junit.rules.TemporaryFolder) InputStream(java.io.InputStream) KeyValueTable(com.palantir.atlasdb.schema.stream.generated.KeyValueTable) Optional(java.util.Optional) ByteArrayInputStream(java.io.ByteArrayInputStream) Sha256Hash(com.palantir.util.crypto.Sha256Hash) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) TransactionConflictException(com.palantir.atlasdb.transaction.api.TransactionConflictException) IOException(java.io.IOException) Test(org.junit.Test)

Example 7 with Sha256Hash

use of com.palantir.util.crypto.Sha256Hash in project atlasdb by palantir.

the class StreamTestMaxMemStreamStore method lookupStreamIdsByHash.

@Override
public Map<Sha256Hash, Long> lookupStreamIdsByHash(Transaction t, final Set<Sha256Hash> hashes) {
    if (hashes.isEmpty()) {
        return ImmutableMap.of();
    }
    StreamTestMaxMemStreamHashAidxTable idx = tables.getStreamTestMaxMemStreamHashAidxTable(t);
    Set<StreamTestMaxMemStreamHashAidxTable.StreamTestMaxMemStreamHashAidxRow> rows = getHashIndexRowsForHashes(hashes);
    Multimap<StreamTestMaxMemStreamHashAidxTable.StreamTestMaxMemStreamHashAidxRow, StreamTestMaxMemStreamHashAidxTable.StreamTestMaxMemStreamHashAidxColumnValue> m = idx.getRowsMultimap(rows);
    Map<Long, Sha256Hash> hashForStreams = Maps.newHashMap();
    for (StreamTestMaxMemStreamHashAidxTable.StreamTestMaxMemStreamHashAidxRow r : m.keySet()) {
        for (StreamTestMaxMemStreamHashAidxTable.StreamTestMaxMemStreamHashAidxColumnValue 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 8 with Sha256Hash

use of com.palantir.util.crypto.Sha256Hash in project atlasdb by palantir.

the class StreamTestStreamStore method deleteStreams.

/**
 * This should only be used from the cleanup tasks.
 */
void deleteStreams(Transaction t, final Set<Long> streamIds) {
    if (streamIds.isEmpty()) {
        return;
    }
    Set<StreamTestStreamMetadataTable.StreamTestStreamMetadataRow> smRows = Sets.newHashSet();
    Multimap<StreamTestStreamHashAidxTable.StreamTestStreamHashAidxRow, StreamTestStreamHashAidxTable.StreamTestStreamHashAidxColumn> shToDelete = HashMultimap.create();
    for (Long streamId : streamIds) {
        smRows.add(StreamTestStreamMetadataTable.StreamTestStreamMetadataRow.of(streamId));
    }
    StreamTestStreamMetadataTable table = tables.getStreamTestStreamMetadataTable(t);
    Map<StreamTestStreamMetadataTable.StreamTestStreamMetadataRow, StreamMetadata> metadatas = table.getMetadatas(smRows);
    Set<StreamTestStreamValueTable.StreamTestStreamValueRow> streamValueToDelete = Sets.newHashSet();
    for (Entry<StreamTestStreamMetadataTable.StreamTestStreamMetadataRow, StreamMetadata> e : metadatas.entrySet()) {
        Long streamId = e.getKey().getId();
        long blocks = getNumberOfBlocksFromMetadata(e.getValue());
        for (long i = 0; i < blocks; i++) {
            streamValueToDelete.add(StreamTestStreamValueTable.StreamTestStreamValueRow.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);
        }
        StreamTestStreamHashAidxTable.StreamTestStreamHashAidxRow hashRow = StreamTestStreamHashAidxTable.StreamTestStreamHashAidxRow.of(hash);
        StreamTestStreamHashAidxTable.StreamTestStreamHashAidxColumn column = StreamTestStreamHashAidxTable.StreamTestStreamHashAidxColumn.of(streamId);
        shToDelete.put(hashRow, column);
    }
    tables.getStreamTestStreamHashAidxTable(t).delete(shToDelete);
    tables.getStreamTestStreamValueTable(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)

Example 9 with Sha256Hash

use of com.palantir.util.crypto.Sha256Hash in project atlasdb by palantir.

the class StreamTestWithHashStreamStore method deleteStreams.

/**
 * This should only be used from the cleanup tasks.
 */
void deleteStreams(Transaction t, final Set<Long> streamIds) {
    if (streamIds.isEmpty()) {
        return;
    }
    Set<StreamTestWithHashStreamMetadataTable.StreamTestWithHashStreamMetadataRow> smRows = Sets.newHashSet();
    Multimap<StreamTestWithHashStreamHashAidxTable.StreamTestWithHashStreamHashAidxRow, StreamTestWithHashStreamHashAidxTable.StreamTestWithHashStreamHashAidxColumn> shToDelete = HashMultimap.create();
    for (Long streamId : streamIds) {
        smRows.add(StreamTestWithHashStreamMetadataTable.StreamTestWithHashStreamMetadataRow.of(streamId));
    }
    StreamTestWithHashStreamMetadataTable table = tables.getStreamTestWithHashStreamMetadataTable(t);
    Map<StreamTestWithHashStreamMetadataTable.StreamTestWithHashStreamMetadataRow, StreamMetadata> metadatas = table.getMetadatas(smRows);
    Set<StreamTestWithHashStreamValueTable.StreamTestWithHashStreamValueRow> streamValueToDelete = Sets.newHashSet();
    for (Entry<StreamTestWithHashStreamMetadataTable.StreamTestWithHashStreamMetadataRow, StreamMetadata> e : metadatas.entrySet()) {
        Long streamId = e.getKey().getId();
        long blocks = getNumberOfBlocksFromMetadata(e.getValue());
        for (long i = 0; i < blocks; i++) {
            streamValueToDelete.add(StreamTestWithHashStreamValueTable.StreamTestWithHashStreamValueRow.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);
        }
        StreamTestWithHashStreamHashAidxTable.StreamTestWithHashStreamHashAidxRow hashRow = StreamTestWithHashStreamHashAidxTable.StreamTestWithHashStreamHashAidxRow.of(hash);
        StreamTestWithHashStreamHashAidxTable.StreamTestWithHashStreamHashAidxColumn column = StreamTestWithHashStreamHashAidxTable.StreamTestWithHashStreamHashAidxColumn.of(streamId);
        shToDelete.put(hashRow, column);
    }
    tables.getStreamTestWithHashStreamHashAidxTable(t).delete(shToDelete);
    tables.getStreamTestWithHashStreamValueTable(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)

Example 10 with Sha256Hash

use of com.palantir.util.crypto.Sha256Hash in project atlasdb by palantir.

the class UserPhotosStreamStore method lookupStreamIdsByHash.

@Override
public Map<Sha256Hash, Long> lookupStreamIdsByHash(Transaction t, final Set<Sha256Hash> hashes) {
    if (hashes.isEmpty()) {
        return ImmutableMap.of();
    }
    UserPhotosStreamHashAidxTable idx = tables.getUserPhotosStreamHashAidxTable(t);
    Set<UserPhotosStreamHashAidxTable.UserPhotosStreamHashAidxRow> rows = getHashIndexRowsForHashes(hashes);
    Multimap<UserPhotosStreamHashAidxTable.UserPhotosStreamHashAidxRow, UserPhotosStreamHashAidxTable.UserPhotosStreamHashAidxColumnValue> m = idx.getRowsMultimap(rows);
    Map<Long, Sha256Hash> hashForStreams = Maps.newHashMap();
    for (UserPhotosStreamHashAidxTable.UserPhotosStreamHashAidxRow r : m.keySet()) {
        for (UserPhotosStreamHashAidxTable.UserPhotosStreamHashAidxColumnValue 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)

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