Search in sources :

Example 1 with AsyncDistributedSet

use of io.atomix.core.set.AsyncDistributedSet in project atomix by atomix.

the class DefaultDistributedSetBuilder method buildAsync.

@Override
@SuppressWarnings("unchecked")
public CompletableFuture<DistributedSet<E>> buildAsync() {
    PrimitiveProtocol protocol = protocol();
    if (protocol instanceof GossipProtocol) {
        if (protocol instanceof SetProtocol) {
            return managementService.getPrimitiveCache().getPrimitive(name, () -> CompletableFuture.completedFuture(((SetProtocol) protocol).<E>newSetDelegate(name, serializer(), managementService)).thenApply(set -> new GossipDistributedSet<>(name, protocol, set))).thenApply(AsyncDistributedSet::sync);
        } else {
            return Futures.exceptionalFuture(new UnsupportedOperationException("Sets are not supported by the provided gossip protocol"));
        }
    } else {
        return newProxy(DistributedSetService.class, new ServiceConfig()).thenCompose(proxy -> new DistributedSetProxy((ProxyClient) proxy, managementService.getPrimitiveRegistry()).connect()).thenApply(rawSet -> {
            Serializer serializer = serializer();
            AsyncDistributedSet<E> set = new TranscodingAsyncDistributedSet<>(rawSet, element -> BaseEncoding.base16().encode(serializer.encode(element)), string -> serializer.decode(BaseEncoding.base16().decode(string)));
            if (config.getCacheConfig().isEnabled()) {
                set = new CachingAsyncDistributedSet<>(set, config.getCacheConfig());
            }
            if (config.isReadOnly()) {
                set = new UnmodifiableAsyncDistributedSet<>(set);
            }
            return set.sync();
        });
    }
}
Also used : ProxyClient(io.atomix.primitive.proxy.ProxyClient) BaseEncoding(com.google.common.io.BaseEncoding) CompletableFuture(java.util.concurrent.CompletableFuture) DistributedSetBuilder(io.atomix.core.set.DistributedSetBuilder) DistributedSet(io.atomix.core.set.DistributedSet) DistributedSetConfig(io.atomix.core.set.DistributedSetConfig) SetProtocol(io.atomix.primitive.protocol.set.SetProtocol) PrimitiveManagementService(io.atomix.primitive.PrimitiveManagementService) ServiceConfig(io.atomix.primitive.service.ServiceConfig) PrimitiveProtocol(io.atomix.primitive.protocol.PrimitiveProtocol) AsyncDistributedSet(io.atomix.core.set.AsyncDistributedSet) GossipProtocol(io.atomix.primitive.protocol.GossipProtocol) Serializer(io.atomix.utils.serializer.Serializer) Futures(io.atomix.utils.concurrent.Futures) AsyncDistributedSet(io.atomix.core.set.AsyncDistributedSet) ServiceConfig(io.atomix.primitive.service.ServiceConfig) GossipProtocol(io.atomix.primitive.protocol.GossipProtocol) ProxyClient(io.atomix.primitive.proxy.ProxyClient) SetProtocol(io.atomix.primitive.protocol.set.SetProtocol) PrimitiveProtocol(io.atomix.primitive.protocol.PrimitiveProtocol) Serializer(io.atomix.utils.serializer.Serializer)

Aggregations

BaseEncoding (com.google.common.io.BaseEncoding)1 AsyncDistributedSet (io.atomix.core.set.AsyncDistributedSet)1 DistributedSet (io.atomix.core.set.DistributedSet)1 DistributedSetBuilder (io.atomix.core.set.DistributedSetBuilder)1 DistributedSetConfig (io.atomix.core.set.DistributedSetConfig)1 PrimitiveManagementService (io.atomix.primitive.PrimitiveManagementService)1 GossipProtocol (io.atomix.primitive.protocol.GossipProtocol)1 PrimitiveProtocol (io.atomix.primitive.protocol.PrimitiveProtocol)1 SetProtocol (io.atomix.primitive.protocol.set.SetProtocol)1 ProxyClient (io.atomix.primitive.proxy.ProxyClient)1 ServiceConfig (io.atomix.primitive.service.ServiceConfig)1 Futures (io.atomix.utils.concurrent.Futures)1 Serializer (io.atomix.utils.serializer.Serializer)1 CompletableFuture (java.util.concurrent.CompletableFuture)1