Search in sources :

Example 36 with Sha256Hash

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

Example 37 with Sha256Hash

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

Example 38 with Sha256Hash

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

Example 39 with Sha256Hash

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;
}
Also used : Sha256Hash(com.palantir.util.crypto.Sha256Hash) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) RowColumnRangeIterator(com.palantir.atlasdb.keyvalue.api.RowColumnRangeIterator) Value(com.palantir.atlasdb.keyvalue.api.Value) Cell(com.palantir.atlasdb.keyvalue.api.Cell) Map(java.util.Map) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) SortedMap(java.util.SortedMap)

Example 40 with Sha256Hash

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);
        }
    }
}
Also used : ColumnOrSuperColumn(org.apache.cassandra.thrift.ColumnOrSuperColumn) Sha256Hash(com.palantir.util.crypto.Sha256Hash) List(java.util.List) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ByteBuffer(java.nio.ByteBuffer)

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