All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.dataspray.singletable.DynamoUtil Maven / Gradle / Ivy

Go to download

DynamoDB best practices encourages a single-table design that allows multiple record types to reside within the same table. The goal of this library is to improve the experience of Java developers and make it safer to define non-conflicting schema of each record, serializing and deserializing automatically and working with secondary indexes.

The newest version!
// SPDX-FileCopyrightText: 2019-2022 Matus Faro 
// SPDX-License-Identifier: Apache-2.0
package io.dataspray.singletable;

import com.google.common.base.Charsets;
import com.google.common.collect.ImmutableList;
import com.google.common.hash.Hashing;
import io.dataspray.singletable.DynamoMapperImpl.SchemaImpl;
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;
import software.amazon.awssdk.services.dynamodb.model.QueryRequest;
import software.amazon.awssdk.services.dynamodb.model.QueryResponse;

import java.util.Map;
import java.util.Optional;
import java.util.function.Consumer;

import static com.google.common.base.Preconditions.checkArgument;

@Deprecated
class DynamoUtil {

    @Deprecated
    public  ShardPageResult fetchShardNextPage(DynamoDbClient client, Schema schema, Optional cursorOpt, int maxPageSize) {
        return fetchShardNextPage(client, schema, cursorOpt, maxPageSize, Map.of(), null);
    }

    @Deprecated
    public  ShardPageResult fetchShardNextPage(DynamoDbClient client, Schema schema, Optional cursorOpt, int maxPageSize, Map keyConditions) {
        return fetchShardNextPage(client, schema, cursorOpt, maxPageSize, keyConditions, null);
    }

    @Deprecated
    public  ShardPageResult fetchShardNextPage(DynamoDbClient client, Schema schema, Optional cursorOpt, int maxPageSize, Map keyConditions, Consumer queryRequestConsumer) {
        checkArgument(maxPageSize > 0, "Max page size must be greater than zero");
        Optional shardAndExclusiveStartKeyOpt = cursorOpt.map(schema::toShardedExclusiveStartKey);
        ImmutableList.Builder itemsBuilder = ImmutableList.builder();
        do {
            int shard = shardAndExclusiveStartKeyOpt.map(ShardAndExclusiveStartKey::getShard).orElse(0);
            QueryRequest.Builder queryBuilder = QueryRequest.builder()
                    .tableName(schema.tableName());
            ((SchemaImpl) schema).indexNameOpt().ifPresent(queryBuilder::indexName);
            queryBuilder
                    .keyConditions(schema.attrMapToConditions(schema.shardKey(shard, keyConditions)))
                    .limit(maxPageSize)
                    .exclusiveStartKey(shardAndExclusiveStartKeyOpt
                            .flatMap(ShardAndExclusiveStartKey::getExclusiveStartKey)
                            .orElse(null));
            if (queryRequestConsumer != null) {
                queryRequestConsumer.accept(queryBuilder);
            }
            QueryResponse page = client.query(queryBuilder.build());
            shardAndExclusiveStartKeyOpt = (page.hasLastEvaluatedKey() ? Optional.of(page.lastEvaluatedKey()) : Optional.>empty())
                    .map(lastEvaluatedKey -> schema.wrapShardedLastEvaluatedKey(Optional.of(lastEvaluatedKey), shard))
                    .or(() -> shard < (schema.shardCount() - 1)
                            ? Optional.of(schema.wrapShardedLastEvaluatedKey(Optional.empty(), shard + 1))
                            : Optional.empty());
            ImmutableList nextItems = page.items().stream()
                    .map(schema::fromAttrMap)
                    .collect(ImmutableList.toImmutableList());
            maxPageSize -= nextItems.size();
            itemsBuilder.addAll(nextItems);
        } while (maxPageSize > 0 && shardAndExclusiveStartKeyOpt.isPresent());
        return new ShardPageResult(
                itemsBuilder.build(),
                shardAndExclusiveStartKeyOpt.map(schema::serializeShardedLastEvaluatedKey));
    }

    @Deprecated
    public static int deterministicPartition(String input, int partitionCount) {
        return Math.abs(Hashing.murmur3_32_fixed().hashString(input, Charsets.UTF_8).asInt() % partitionCount);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy