Search in sources :

Example 1 with ConsistentMap

use of io.atomix.core.map.ConsistentMap in project atomix by atomix.

the class ConsistentMapProxyBuilder method buildAsync.

@Override
@SuppressWarnings("unchecked")
public CompletableFuture<ConsistentMap<K, V>> buildAsync() {
    PrimitiveProtocol protocol = protocol();
    PartitionGroup<?> partitions = managementService.getPartitionService().getPartitionGroup(protocol);
    Map<PartitionId, CompletableFuture<AsyncConsistentMap<byte[], byte[]>>> maps = Maps.newConcurrentMap();
    for (Partition partition : partitions.getPartitions()) {
        maps.put(partition.id(), partition.getPrimitiveClient().newProxy(name(), primitiveType(), protocol).connect().thenApply(proxy -> new TranscodingAsyncConsistentMap<>(new ConsistentMapProxy(proxy), BaseEncoding.base16()::encode, BaseEncoding.base16()::decode, Function.identity(), Function.identity())));
    }
    Partitioner<byte[]> partitioner = key -> {
        int bucket = Math.abs(Hashing.murmur3_32().hashBytes(key).asInt()) % NUM_BUCKETS;
        return partitions.getPartitionIds().get(Hashing.consistentHash(bucket, partitions.getPartitionIds().size()));
    };
    return Futures.allOf(Lists.newArrayList(maps.values())).thenApply(m -> {
        AsyncConsistentMap<byte[], byte[]> partitionedMap = new PartitionedAsyncConsistentMap<>(name(), Maps.transformValues(maps, v -> v.getNow(null)), partitioner);
        Serializer serializer = serializer();
        AsyncConsistentMap<K, V> map = new TranscodingAsyncConsistentMap<>(partitionedMap, key -> serializer.encode(key), bytes -> serializer.decode(bytes), value -> value == null ? null : serializer.encode(value), bytes -> serializer.decode(bytes));
        if (!nullValues()) {
            map = new NotNullAsyncConsistentMap<>(map);
        }
        if (relaxedReadConsistency()) {
            map = new CachingAsyncConsistentMap<>(map);
        }
        if (readOnly()) {
            map = new UnmodifiableAsyncConsistentMap<>(map);
        }
        return map.sync();
    });
}
Also used : BaseEncoding(com.google.common.io.BaseEncoding) ConsistentMapBuilder(io.atomix.core.map.ConsistentMapBuilder) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) Hashing(com.google.common.hash.Hashing) CompletableFuture(java.util.concurrent.CompletableFuture) Maps(com.google.common.collect.Maps) PartitionGroup(io.atomix.primitive.partition.PartitionGroup) Function(java.util.function.Function) PartitionId(io.atomix.primitive.partition.PartitionId) Lists(com.google.common.collect.Lists) AsyncConsistentMap(io.atomix.core.map.AsyncConsistentMap) Partition(io.atomix.primitive.partition.Partition) PrimitiveManagementService(io.atomix.primitive.PrimitiveManagementService) PrimitiveProtocol(io.atomix.primitive.PrimitiveProtocol) ConsistentMap(io.atomix.core.map.ConsistentMap) Map(java.util.Map) Partitioner(io.atomix.primitive.partition.Partitioner) Serializer(io.atomix.utils.serializer.Serializer) Futures(io.atomix.utils.concurrent.Futures) Partition(io.atomix.primitive.partition.Partition) PartitionId(io.atomix.primitive.partition.PartitionId) CompletableFuture(java.util.concurrent.CompletableFuture) PrimitiveProtocol(io.atomix.primitive.PrimitiveProtocol) Serializer(io.atomix.utils.serializer.Serializer)

Aggregations

Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 Lists (com.google.common.collect.Lists)1 Maps (com.google.common.collect.Maps)1 Hashing (com.google.common.hash.Hashing)1 BaseEncoding (com.google.common.io.BaseEncoding)1 AsyncConsistentMap (io.atomix.core.map.AsyncConsistentMap)1 ConsistentMap (io.atomix.core.map.ConsistentMap)1 ConsistentMapBuilder (io.atomix.core.map.ConsistentMapBuilder)1 PrimitiveManagementService (io.atomix.primitive.PrimitiveManagementService)1 PrimitiveProtocol (io.atomix.primitive.PrimitiveProtocol)1 Partition (io.atomix.primitive.partition.Partition)1 PartitionGroup (io.atomix.primitive.partition.PartitionGroup)1 PartitionId (io.atomix.primitive.partition.PartitionId)1 Partitioner (io.atomix.primitive.partition.Partitioner)1 Futures (io.atomix.utils.concurrent.Futures)1 Serializer (io.atomix.utils.serializer.Serializer)1 Map (java.util.Map)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 Function (java.util.function.Function)1