Search in sources :

Example 1 with Serializer

use of io.atomix.utils.serializer.Serializer in project atomix by atomix.

the class ConsistentTreeMapProxyBuilder method buildAsync.

@Override
@SuppressWarnings("unchecked")
public CompletableFuture<ConsistentTreeMap<V>> buildAsync() {
    PrimitiveProtocol protocol = protocol();
    return managementService.getPartitionService().getPartitionGroup(protocol).getPartition(name()).getPrimitiveClient().newProxy(name(), primitiveType(), protocol).connect().thenApply(proxy -> {
        ConsistentTreeMapProxy rawMap = new ConsistentTreeMapProxy(proxy);
        Serializer serializer = serializer();
        return new TranscodingAsyncConsistentTreeMap<V, byte[]>(rawMap, value -> value == null ? null : serializer.encode(value), bytes -> serializer.decode(bytes)).sync();
    });
}
Also used : PrimitiveManagementService(io.atomix.primitive.PrimitiveManagementService) PrimitiveProtocol(io.atomix.primitive.PrimitiveProtocol) ConsistentTreeMapBuilder(io.atomix.core.map.ConsistentTreeMapBuilder) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) CompletableFuture(java.util.concurrent.CompletableFuture) Serializer(io.atomix.utils.serializer.Serializer) AsyncConsistentTreeMap(io.atomix.core.map.AsyncConsistentTreeMap) ConsistentTreeMap(io.atomix.core.map.ConsistentTreeMap) PrimitiveProtocol(io.atomix.primitive.PrimitiveProtocol) Serializer(io.atomix.utils.serializer.Serializer)

Example 2 with Serializer

use of io.atomix.utils.serializer.Serializer in project atomix by atomix.

the class AtomicCounterMapProxyBuilder method buildAsync.

@Override
@SuppressWarnings("unchecked")
public CompletableFuture<AtomicCounterMap<K>> buildAsync() {
    PrimitiveProtocol protocol = protocol();
    return managementService.getPartitionService().getPartitionGroup(protocol).getPartition(name()).getPrimitiveClient().newProxy(name(), primitiveType(), protocol).connect().thenApply(proxy -> {
        AtomicCounterMapProxy rawMap = new AtomicCounterMapProxy(proxy);
        Serializer serializer = serializer();
        return new TranscodingAsyncAtomicCounterMap<K, String>(rawMap, key -> BaseEncoding.base16().encode(serializer.encode(key)), string -> serializer.decode(BaseEncoding.base16().decode(string))).sync();
    });
}
Also used : BaseEncoding(com.google.common.io.BaseEncoding) PrimitiveManagementService(io.atomix.primitive.PrimitiveManagementService) PrimitiveProtocol(io.atomix.primitive.PrimitiveProtocol) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) CompletableFuture(java.util.concurrent.CompletableFuture) Serializer(io.atomix.utils.serializer.Serializer) AtomicCounterMap(io.atomix.core.map.AtomicCounterMap) AtomicCounterMapBuilder(io.atomix.core.map.AtomicCounterMapBuilder) PrimitiveProtocol(io.atomix.primitive.PrimitiveProtocol) Serializer(io.atomix.utils.serializer.Serializer)

Example 3 with Serializer

use of io.atomix.utils.serializer.Serializer in project atomix by atomix.

the class ConsistentMapProxy method computeIf.

@Override
@SuppressWarnings("unchecked")
public CompletableFuture<Versioned<byte[]>> computeIf(String key, Predicate<? super byte[]> condition, BiFunction<? super String, ? super byte[], ? extends byte[]> remappingFunction) {
    return get(key).thenCompose(r1 -> {
        byte[] existingValue = r1 == null ? null : r1.value();
        // if the condition evaluates to false, return existing value.
        if (!condition.test(existingValue)) {
            return CompletableFuture.completedFuture(r1);
        }
        byte[] computedValue;
        try {
            computedValue = remappingFunction.apply(key, existingValue);
        } catch (Exception e) {
            return Futures.exceptionalFuture(e);
        }
        if (computedValue == null && r1 == null) {
            return CompletableFuture.completedFuture(null);
        }
        if (r1 == null) {
            return proxy.<Put, MapEntryUpdateResult<String, byte[]>>invoke(PUT_IF_ABSENT, serializer()::encode, new Put(key, computedValue, 0), serializer()::decode).whenComplete((r, e) -> throwIfLocked(r)).thenCompose(r -> checkLocked(r)).thenApply(result -> new Versioned<>(computedValue, result.version()));
        } else if (computedValue == null) {
            return proxy.<RemoveVersion, MapEntryUpdateResult<String, byte[]>>invoke(REMOVE_VERSION, serializer()::encode, new RemoveVersion(key, r1.version()), serializer()::decode).whenComplete((r, e) -> throwIfLocked(r)).thenCompose(r -> checkLocked(r)).thenApply(v -> null);
        } else {
            return proxy.<ReplaceVersion, MapEntryUpdateResult<String, byte[]>>invoke(REPLACE_VERSION, serializer()::encode, new ReplaceVersion(key, r1.version(), computedValue), serializer()::decode).whenComplete((r, e) -> throwIfLocked(r)).thenCompose(r -> checkLocked(r)).thenApply(result -> result.status() == MapEntryUpdateResult.Status.OK ? new Versioned(computedValue, result.version()) : result.result());
        }
    });
}
Also used : TransactionId(io.atomix.core.transaction.TransactionId) BiFunction(java.util.function.BiFunction) REMOVE_LISTENER(io.atomix.core.map.impl.ConsistentMapOperations.REMOVE_LISTENER) MapEventListener(io.atomix.core.map.MapEventListener) IS_EMPTY(io.atomix.core.map.impl.ConsistentMapOperations.IS_EMPTY) Remove(io.atomix.core.map.impl.ConsistentMapOperations.Remove) KEY_SET(io.atomix.core.map.impl.ConsistentMapOperations.KEY_SET) Duration(java.time.Duration) Map(java.util.Map) COMMIT(io.atomix.core.map.impl.ConsistentMapOperations.COMMIT) ENTRY_SET(io.atomix.core.map.impl.ConsistentMapOperations.ENTRY_SET) REPLACE_VERSION(io.atomix.core.map.impl.ConsistentMapOperations.REPLACE_VERSION) ADD_LISTENER(io.atomix.core.map.impl.ConsistentMapOperations.ADD_LISTENER) ReplaceVersion(io.atomix.core.map.impl.ConsistentMapOperations.ReplaceVersion) Predicate(java.util.function.Predicate) REMOVE(io.atomix.core.map.impl.ConsistentMapOperations.REMOVE) Collection(java.util.Collection) RemoveValue(io.atomix.core.map.impl.ConsistentMapOperations.RemoveValue) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) CONTAINS_KEY(io.atomix.core.map.impl.ConsistentMapOperations.CONTAINS_KEY) PUT_AND_GET(io.atomix.core.map.impl.ConsistentMapOperations.PUT_AND_GET) Versioned(io.atomix.utils.time.Versioned) ContainsValue(io.atomix.core.map.impl.ConsistentMapOperations.ContainsValue) CHANGE(io.atomix.core.map.impl.ConsistentMapEvents.CHANGE) List(java.util.List) GET_OR_DEFAULT(io.atomix.core.map.impl.ConsistentMapOperations.GET_OR_DEFAULT) KryoNamespaces(io.atomix.utils.serializer.KryoNamespaces) TransactionPrepare(io.atomix.core.map.impl.ConsistentMapOperations.TransactionPrepare) Entry(java.util.Map.Entry) Get(io.atomix.core.map.impl.ConsistentMapOperations.Get) Serializer(io.atomix.utils.serializer.Serializer) GET(io.atomix.core.map.impl.ConsistentMapOperations.GET) ConcurrentModificationException(java.util.ConcurrentModificationException) GET_ALL_PRESENT(io.atomix.core.map.impl.ConsistentMapOperations.GET_ALL_PRESENT) REMOVE_VALUE(io.atomix.core.map.impl.ConsistentMapOperations.REMOVE_VALUE) GetOrDefault(io.atomix.core.map.impl.ConsistentMapOperations.GetOrDefault) ConsistentMapException(io.atomix.core.map.ConsistentMapException) PUT_IF_ABSENT(io.atomix.core.map.impl.ConsistentMapOperations.PUT_IF_ABSENT) TransactionCommit(io.atomix.core.map.impl.ConsistentMapOperations.TransactionCommit) CompletableFuture(java.util.concurrent.CompletableFuture) CLEAR(io.atomix.core.map.impl.ConsistentMapOperations.CLEAR) HashSet(java.util.HashSet) AsyncConsistentMap(io.atomix.core.map.AsyncConsistentMap) ConsistentMap(io.atomix.core.map.ConsistentMap) MapEvent(io.atomix.core.map.MapEvent) Replace(io.atomix.core.map.impl.ConsistentMapOperations.Replace) KryoNamespace(io.atomix.utils.serializer.KryoNamespace) REPLACE_VALUE(io.atomix.core.map.impl.ConsistentMapOperations.REPLACE_VALUE) ROLLBACK(io.atomix.core.map.impl.ConsistentMapOperations.ROLLBACK) REMOVE_VERSION(io.atomix.core.map.impl.ConsistentMapOperations.REMOVE_VERSION) TransactionRollback(io.atomix.core.map.impl.ConsistentMapOperations.TransactionRollback) Futures(io.atomix.utils.concurrent.Futures) ContainsKey(io.atomix.core.map.impl.ConsistentMapOperations.ContainsKey) Executor(java.util.concurrent.Executor) AbstractAsyncPrimitive(io.atomix.primitive.impl.AbstractAsyncPrimitive) RemoveVersion(io.atomix.core.map.impl.ConsistentMapOperations.RemoveVersion) SIZE(io.atomix.core.map.impl.ConsistentMapOperations.SIZE) CONTAINS_VALUE(io.atomix.core.map.impl.ConsistentMapOperations.CONTAINS_VALUE) REPLACE(io.atomix.core.map.impl.ConsistentMapOperations.REPLACE) VALUES(io.atomix.core.map.impl.ConsistentMapOperations.VALUES) PREPARE(io.atomix.core.map.impl.ConsistentMapOperations.PREPARE) Put(io.atomix.core.map.impl.ConsistentMapOperations.Put) PUT(io.atomix.core.map.impl.ConsistentMapOperations.PUT) PrimitiveProxy(io.atomix.primitive.proxy.PrimitiveProxy) TransactionLog(io.atomix.core.transaction.TransactionLog) GetAllPresent(io.atomix.core.map.impl.ConsistentMapOperations.GetAllPresent) ReplaceValue(io.atomix.core.map.impl.ConsistentMapOperations.ReplaceValue) RemoveVersion(io.atomix.core.map.impl.ConsistentMapOperations.RemoveVersion) Versioned(io.atomix.utils.time.Versioned) ReplaceVersion(io.atomix.core.map.impl.ConsistentMapOperations.ReplaceVersion) ConcurrentModificationException(java.util.ConcurrentModificationException) ConsistentMapException(io.atomix.core.map.ConsistentMapException) Put(io.atomix.core.map.impl.ConsistentMapOperations.Put)

Example 4 with Serializer

use of io.atomix.utils.serializer.Serializer 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)

Example 5 with Serializer

use of io.atomix.utils.serializer.Serializer in project atomix by atomix.

the class ConsistentMultimapProxyBuilder method buildAsync.

@Override
@SuppressWarnings("unchecked")
public CompletableFuture<ConsistentMultimap<K, V>> buildAsync() {
    PrimitiveProtocol protocol = protocol();
    return managementService.getPartitionService().getPartitionGroup(protocol).getPartition(name()).getPrimitiveClient().newProxy(name(), primitiveType(), protocol).connect().thenApply(proxy -> {
        AsyncConsistentMultimap<String, byte[]> rawMap = new ConsistentSetMultimapProxy(proxy);
        Serializer serializer = serializer();
        return new TranscodingAsyncConsistentMultimap<K, V, String, byte[]>(rawMap, key -> BaseEncoding.base16().encode(serializer.encode(key)), string -> serializer.decode(BaseEncoding.base16().decode(string)), value -> serializer.encode(value), bytes -> serializer.decode(bytes)).sync();
    });
}
Also used : AsyncConsistentMultimap(io.atomix.core.multimap.AsyncConsistentMultimap) BaseEncoding(com.google.common.io.BaseEncoding) PrimitiveManagementService(io.atomix.primitive.PrimitiveManagementService) PrimitiveProtocol(io.atomix.primitive.PrimitiveProtocol) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) ConsistentMultimapBuilder(io.atomix.core.multimap.ConsistentMultimapBuilder) CompletableFuture(java.util.concurrent.CompletableFuture) ConsistentMultimap(io.atomix.core.multimap.ConsistentMultimap) Serializer(io.atomix.utils.serializer.Serializer) PrimitiveProtocol(io.atomix.primitive.PrimitiveProtocol) Serializer(io.atomix.utils.serializer.Serializer)

Aggregations

Serializer (io.atomix.utils.serializer.Serializer)6 CompletableFuture (java.util.concurrent.CompletableFuture)5 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)4 PrimitiveManagementService (io.atomix.primitive.PrimitiveManagementService)4 PrimitiveProtocol (io.atomix.primitive.PrimitiveProtocol)4 BaseEncoding (com.google.common.io.BaseEncoding)3 AsyncConsistentMap (io.atomix.core.map.AsyncConsistentMap)2 ConsistentMap (io.atomix.core.map.ConsistentMap)2 Futures (io.atomix.utils.concurrent.Futures)2 KryoNamespaces (io.atomix.utils.serializer.KryoNamespaces)2 List (java.util.List)2 Map (java.util.Map)2 Set (java.util.Set)2 Lists (com.google.common.collect.Lists)1 Maps (com.google.common.collect.Maps)1 Hashing (com.google.common.hash.Hashing)1 MoreExecutors (com.google.common.util.concurrent.MoreExecutors)1 ClusterMetadata (io.atomix.cluster.ClusterMetadata)1 ClusterService (io.atomix.cluster.ClusterService)1 Node (io.atomix.cluster.Node)1