io.atomix.primitive.service.ServiceConfig Java Examples

The following examples show how to use io.atomix.primitive.service.ServiceConfig. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DefaultAtomicDocumentTreeBuilder.java    From atomix with Apache License 2.0 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public CompletableFuture<AtomicDocumentTree<V>> buildAsync() {
  return newProxy(DocumentTreeService.class, new ServiceConfig())
      .thenCompose(proxy -> new AtomicDocumentTreeProxy(proxy, managementService.getPrimitiveRegistry()).connect())
      .thenApply(treeProxy -> {
        Serializer serializer = serializer();
        AsyncAtomicDocumentTree<V> tree = new TranscodingAsyncAtomicDocumentTree<>(
            treeProxy,
            key -> serializer.encode(key),
            bytes -> serializer.decode(bytes));

        if (config.getCacheConfig().isEnabled()) {
          tree = new CachingAsyncAtomicDocumentTree<V>(tree, config.getCacheConfig());
        }
        return tree.sync();
      });
}
 
Example #2
Source File: DefaultDistributedListBuilder.java    From atomix with Apache License 2.0 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public CompletableFuture<DistributedList<E>> buildAsync() {
  return newProxy(DistributedListService.class, new ServiceConfig())
      .thenCompose(proxy -> new DistributedListProxy(proxy, managementService.getPrimitiveRegistry()).connect())
      .thenApply(rawList -> {
        Serializer serializer = serializer();
        AsyncDistributedList<E> list = new TranscodingAsyncDistributedList<>(
            rawList,
            element -> BaseEncoding.base16().encode(serializer.encode(element)),
            string -> serializer.decode(BaseEncoding.base16().decode(string)));

        if (config.getCacheConfig().isEnabled()) {
          list = new CachingAsyncDistributedList<>(list, config.getCacheConfig());
        }

        if (config.isReadOnly()) {
          list = new UnmodifiableAsyncDistributedList<>(list);
        }
        return list.sync();
      });
}
 
Example #3
Source File: MultiPrimaryProtocol.java    From atomix with Apache License 2.0 6 votes vote down vote up
@Override
public <S> ProxyClient<S> newProxy(String primitiveName, PrimitiveType primitiveType, Class<S> serviceType, ServiceConfig serviceConfig, PartitionService partitionService) {
  PartitionGroup partitionGroup = partitionService.getPartitionGroup(this);
  if (partitionGroup == null) {
    throw new ConfigurationException("No Raft partition group matching the configured protocol exists");
  }

  Collection<SessionClient> partitions = partitionGroup.getPartitions().stream()
      .map(partition -> ((PrimaryBackupPartition) partition).getClient()
          .sessionBuilder(primitiveName, primitiveType, serviceConfig)
          .withConsistency(config.getConsistency())
          .withReplication(config.getReplication())
          .withRecovery(config.getRecovery())
          .withNumBackups(config.getBackups())
          .withMaxRetries(config.getMaxRetries())
          .withRetryDelay(config.getRetryDelay())
          .build())
      .collect(Collectors.toList());
  return new DefaultProxyClient<>(primitiveName, primitiveType, this, serviceType, partitions, config.getPartitioner());
}
 
Example #4
Source File: CorePrimitiveRegistry.java    From atomix with Apache License 2.0 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public CompletableFuture<PrimitiveRegistry> start() {
  ProxyProtocol protocol = partitionService.getSystemPartitionGroup().newProtocol();
  ProxyClient proxy = protocol.newProxy(
      "primitives",
      AtomicMapType.instance(),
      AtomicMapService.class,
      new ServiceConfig(),
      partitionService);
  return proxy.connect()
      .thenApply(v -> {
        AtomicMapProxy mapProxy = new AtomicMapProxy(proxy, this);
        primitives = new TranscodingAsyncAtomicMap<>(
            mapProxy,
            key -> key,
            key -> key,
            value -> value != null ? SERIALIZER.encode(value) : null,
            value -> value != null ? SERIALIZER.decode(value) : null);
        started.set(true);
        return this;
      });
}
 
Example #5
Source File: RaftServiceManager.java    From atomix with Apache License 2.0 6 votes vote down vote up
/**
 * Initializes a new service.
 */
@SuppressWarnings("unchecked")
private RaftServiceContext initializeService(PrimitiveId primitiveId, PrimitiveType primitiveType, String serviceName, byte[] config) {
  RaftServiceContext oldService = raft.getServices().getService(serviceName);
  ServiceConfig serviceConfig = config == null ? new ServiceConfig() : Serializer.using(primitiveType.namespace()).decode(config);
  RaftServiceContext service = new RaftServiceContext(
      primitiveId,
      serviceName,
      primitiveType,
      serviceConfig,
      primitiveType.newService(serviceConfig),
      raft,
      threadContextFactory);
  raft.getServices().registerService(service);

  // If a service with this name was already registered, remove all of its sessions.
  if (oldService != null) {
    raft.getSessions().removeSessions(oldService.serviceId());
  }
  return service;
}
 
Example #6
Source File: DefaultDistributedMultimapBuilder.java    From atomix with Apache License 2.0 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public CompletableFuture<DistributedMultimap<K, V>> buildAsync() {
  return newProxy(AtomicMultimapService.class, new ServiceConfig())
      .thenCompose(proxy -> new AtomicMultimapProxy(proxy, managementService.getPrimitiveRegistry()).connect())
      .thenApply(rawMultimap -> {
        Serializer serializer = serializer();
        AsyncAtomicMultimap<K, V> multimap = new TranscodingAsyncAtomicMultimap<>(
            rawMultimap,
            key -> BaseEncoding.base16().encode(serializer.encode(key)),
            string -> serializer.decode(BaseEncoding.base16().decode(string)),
            value -> serializer.encode(value),
            bytes -> serializer.decode(bytes));
        if (config.getCacheConfig().isEnabled()) {
          multimap = new CachingAsyncAtomicMultimap<>(multimap, config.getCacheConfig());
        }
        return multimap;
      }).thenApply(atomicMultimap -> new DelegatingAsyncDistributedMultimap<>(atomicMultimap).sync());
}
 
Example #7
Source File: DefaultDistributedCounterBuilder.java    From atomix with Apache License 2.0 6 votes vote down vote up
@Override
public CompletableFuture<DistributedCounter> buildAsync() {
  PrimitiveProtocol protocol = protocol();
  if (protocol instanceof GossipProtocol) {
    if (protocol instanceof CounterProtocol) {
      return managementService.getPrimitiveCache().getPrimitive(name, () -> CompletableFuture.completedFuture(
          new GossipDistributedCounter(name, (GossipProtocol) protocol, ((CounterProtocol) protocol)
              .newCounterDelegate(name, managementService))))
          .thenApply(AsyncDistributedCounter::sync);
    } else {
      return Futures.exceptionalFuture(new UnsupportedOperationException("Counter is not supported by the provided gossip protocol"));
    }
  } else if (protocol instanceof ProxyProtocol) {
    return newProxy(AtomicCounterService.class, new ServiceConfig())
        .thenCompose(proxy -> new AtomicCounterProxy(proxy, managementService.getPrimitiveRegistry()).connect())
        .thenApply(DelegatingDistributedCounter::new)
        .thenApply(AsyncDistributedCounter::sync);
  } else {
    return Futures.exceptionalFuture(new ConfigurationException("Invalid protocol type"));
  }
}
 
Example #8
Source File: TestProtocolService.java    From atomix with Apache License 2.0 6 votes vote down vote up
TestProtocolService(
    PartitionId partition,
    String name,
    PrimitiveType primitiveType,
    ServiceConfig config,
    PrimitiveService service,
    TestProtocolServiceRegistry registry,
    ThreadContext context) {
  this.partition = partition;
  this.name = name;
  this.primitiveType = primitiveType;
  this.config = config;
  this.service = service;
  this.registry = registry;
  this.context = context;
  this.clock = context.schedule(Duration.ofMillis(100), Duration.ofMillis(100), this::tick);
  open();
}
 
Example #9
Source File: DefaultDistributedMultisetBuilder.java    From atomix with Apache License 2.0 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public CompletableFuture<DistributedMultiset<E>> buildAsync() {
  return newProxy(DistributedMultisetService.class, new ServiceConfig())
      .thenCompose(proxy -> new DistributedMultisetProxy(proxy, managementService.getPrimitiveRegistry()).connect())
      .thenApply(rawList -> {
        Serializer serializer = serializer();
        AsyncDistributedMultiset<E> list = new TranscodingAsyncDistributedMultiset<>(
            rawList,
            element -> BaseEncoding.base16().encode(serializer.encode(element)),
            string -> serializer.decode(BaseEncoding.base16().decode(string)));

        if (config.getCacheConfig().isEnabled()) {
          list = new CachingAsyncDistributedMultiset<>(list, config.getCacheConfig());
        }

        if (config.isReadOnly()) {
          list = new UnmodifiableAsyncDistributedMultiset<>(list);
        }
        return list.sync();
      });
}
 
Example #10
Source File: MultiRaftProtocol.java    From atomix with Apache License 2.0 6 votes vote down vote up
@Override
public <S> ProxyClient<S> newProxy(String primitiveName, PrimitiveType primitiveType, Class<S> serviceType, ServiceConfig serviceConfig, PartitionService partitionService) {
  PartitionGroup partitionGroup = partitionService.getPartitionGroup(this);
  if (partitionGroup == null) {
    throw new ConfigurationException("No Raft partition group matching the configured protocol exists");
  }

  Collection<SessionClient> partitions = partitionGroup.getPartitions().stream()
      .map(partition -> ((RaftPartition) partition).getClient()
          .sessionBuilder(primitiveName, primitiveType, serviceConfig)
          .withMinTimeout(config.getMinTimeout())
          .withMaxTimeout(config.getMaxTimeout())
          .withReadConsistency(config.getReadConsistency())
          .withCommunicationStrategy(config.getCommunicationStrategy())
          .withRecoveryStrategy(config.getRecoveryStrategy())
          .withMaxRetries(config.getMaxRetries())
          .withRetryDelay(config.getRetryDelay())
          .build())
      .collect(Collectors.toList());
  return new DefaultProxyClient<>(primitiveName, primitiveType, this, serviceType, partitions, config.getPartitioner());
}
 
Example #11
Source File: DefaultDistributedQueueBuilder.java    From atomix with Apache License 2.0 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public CompletableFuture<DistributedQueue<E>> buildAsync() {
  return newProxy(DistributedQueueService.class, new ServiceConfig())
      .thenCompose(proxy -> new DistributedQueueProxy(proxy, managementService.getPrimitiveRegistry()).connect())
      .thenApply(rawQueue -> {
        Serializer serializer = serializer();
        AsyncDistributedQueue<E> queue = new TranscodingAsyncDistributedQueue<>(
            rawQueue,
            element -> BaseEncoding.base16().encode(serializer.encode(element)),
            string -> serializer.decode(BaseEncoding.base16().decode(string)));

        if (config.getCacheConfig().isEnabled()) {
          queue = new CachingAsyncDistributedQueue<>(queue, config.getCacheConfig());
        }

        if (config.isReadOnly()) {
          queue = new UnmodifiableAsyncDistributedQueue<>(queue);
        }
        return queue.sync();
      });
}
 
Example #12
Source File: DefaultAtomicMultimapBuilder.java    From atomix with Apache License 2.0 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public CompletableFuture<AtomicMultimap<K, V>> buildAsync() {
  return newProxy(AtomicMultimapService.class, new ServiceConfig())
      .thenCompose(proxy -> new AtomicMultimapProxy(proxy, managementService.getPrimitiveRegistry()).connect())
      .thenApply(rawMultimap -> {
        Serializer serializer = serializer();
        AsyncAtomicMultimap<K, V> multimap = new TranscodingAsyncAtomicMultimap<>(
            rawMultimap,
            key -> BaseEncoding.base16().encode(serializer.encode(key)),
            string -> serializer.decode(BaseEncoding.base16().decode(string)),
            value -> serializer.encode(value),
            bytes -> serializer.decode(bytes));
        if (config.getCacheConfig().isEnabled()) {
          multimap = new CachingAsyncAtomicMultimap<>(multimap, config.getCacheConfig());
        }
        return multimap.sync();
      });
}
 
Example #13
Source File: LogProxyClient.java    From atomix with Apache License 2.0 5 votes vote down vote up
private static <S> Collection<ProxySession<S>> createSessions(
    String name,
    PrimitiveType primitiveType,
    Class<S> serviceType,
    ServiceConfig serviceConfig,
    Collection<LogSession> partitions) {
  Serializer serializer = Serializer.using(primitiveType.namespace());
  return partitions.stream()
      .map(partition -> new LogProxySession<S>(name, primitiveType, serviceType, serviceConfig, serializer, partition))
      .collect(Collectors.toList());
}
 
Example #14
Source File: DefaultDistributedSortedSetBuilder.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public CompletableFuture<DistributedSortedSet<E>> buildAsync() {
  PrimitiveProtocol protocol = protocol();
  if (protocol instanceof GossipProtocol) {
    if (protocol instanceof SortedSetProtocol) {
      return managementService.getPrimitiveCache().getPrimitive(name, () ->
          CompletableFuture.completedFuture(((SortedSetProtocol) protocol).<E>newSortedSetDelegate(name, serializer(), managementService))
              .thenApply(set -> new GossipDistributedSortedSet<>(name, protocol, set)))
          .thenApply(AsyncDistributedSortedSet::sync);
    } else {
      return Futures.exceptionalFuture(new UnsupportedOperationException("Sets are not supported by the provided gossip protocol"));
    }
  } else {
    return newProxy(DistributedTreeSetService.class, new ServiceConfig())
        .thenCompose(proxy -> new DistributedNavigableSetProxy<E>((ProxyClient) proxy, managementService.getPrimitiveRegistry()).connect())
        .thenApply(set -> {
          if (config.getCacheConfig().isEnabled()) {
            set = new CachingAsyncDistributedNavigableSet<>(set, config.getCacheConfig());
          }

          if (config.isReadOnly()) {
            set = new UnmodifiableAsyncDistributedNavigableSet<>(set);
          }
          return set.sync();
        });
  }
}
 
Example #15
Source File: ReplicatedSessionIdService.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<SessionIdService> start() {
  return systemPartitionGroup.getPartitions().iterator().next().getClient()
      .sessionBuilder(PRIMITIVE_NAME, SessionIdGeneratorType.instance(), new ServiceConfig())
      .build()
      .connect()
      .thenApply(proxy -> {
        this.proxy = proxy;
        started.set(true);
        return this;
      });
}
 
Example #16
Source File: DefaultLeaderElectionBuilder.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public CompletableFuture<LeaderElection<T>> buildAsync() {
  return newProxy(LeaderElectionService.class, new ServiceConfig())
      .thenCompose(proxy -> new LeaderElectionProxy(proxy, managementService.getPrimitiveRegistry()).connect())
      .thenApply(election -> {
        Serializer serializer = serializer();
        return new TranscodingAsyncLeaderElection<T, byte[]>(
            election,
            key -> serializer.encode(key),
            bytes -> serializer.decode(bytes))
            .sync();
      });
}
 
Example #17
Source File: LogProxySession.java    From atomix with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public LogProxySession(String name, PrimitiveType type, Class<S> serviceType, ServiceConfig serviceConfig, Serializer serializer, LogSession session) {
  this.name = checkNotNull(name, "name cannot be null");
  this.type = checkNotNull(type, "type cannot be null");
  this.service = type.newService(serviceConfig);
  this.serviceConfig = serviceConfig;
  this.userSerializer = checkNotNull(serializer, "serializer cannot be null");
  this.session = checkNotNull(session, "session cannot be null");
  ServiceProxyHandler serviceProxyHandler = new ServiceProxyHandler(serviceType);
  S serviceProxy = (S) java.lang.reflect.Proxy.newProxyInstance(serviceType.getClassLoader(), new Class[]{serviceType}, serviceProxyHandler);
  proxy = new ServiceProxy<>(serviceProxy, serviceProxyHandler);
}
 
Example #18
Source File: PrimaryBackupTest.java    From atomix with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new primary-backup proxy.
 */
private SessionClient createProxy(PrimaryBackupClient client, int backups, Replication replication) {
  try {
    return client.sessionBuilder("primary-backup-test", TestPrimitiveType.INSTANCE, new ServiceConfig())
        .withNumBackups(backups)
        .withReplication(replication)
        .build()
        .connect()
        .get(30, TimeUnit.SECONDS);
  } catch (InterruptedException | ExecutionException | TimeoutException e) {
    throw new RuntimeException(e);
  }
}
 
Example #19
Source File: PrimitiveBuilder.java    From atomix with Apache License 2.0 5 votes vote down vote up
protected <S> CompletableFuture<ProxyClient<S>> newProxy(Class<S> serviceType, ServiceConfig config) {
  PrimitiveProtocol protocol = protocol();
  if (protocol instanceof ProxyProtocol) {
    try {
      return CompletableFuture.completedFuture(((ProxyProtocol) protocol)
          .newProxy(name, type, serviceType, config, managementService.getPartitionService()));
    } catch (Exception e) {
      return Futures.exceptionalFuture(e);
    }
  }
  return Futures.exceptionalFuture(new UnsupportedOperationException());
}
 
Example #20
Source File: ClusterManager.java    From submarine with Apache License 2.0 5 votes vote down vote up
private SessionClient createProxy(RaftClient client) {
  return client.sessionBuilder(ClusterPrimitiveType.PRIMITIVE_NAME,
      ClusterPrimitiveType.INSTANCE, new ServiceConfig())
      .withReadConsistency(ReadConsistency.SEQUENTIAL)
      .withCommunicationStrategy(CommunicationStrategy.LEADER)
      .build()
      .connect()
      .join();
}
 
Example #21
Source File: DefaultDistributedNavigableSetBuilder.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public CompletableFuture<DistributedNavigableSet<E>> buildAsync() {
  PrimitiveProtocol protocol = protocol();
  if (protocol instanceof GossipProtocol) {
    if (protocol instanceof NavigableSetProtocol) {
      return managementService.getPrimitiveCache().getPrimitive(name, () ->
          CompletableFuture.completedFuture(((NavigableSetProtocol) protocol).<E>newNavigableSetDelegate(name, serializer(), managementService))
              .thenApply(set -> new GossipDistributedNavigableSet<>(name, protocol, set)))
          .thenApply(AsyncDistributedNavigableSet::sync);
    } else {
      return Futures.exceptionalFuture(new UnsupportedOperationException("Sets are not supported by the provided gossip protocol"));
    }
  } else {
    return newProxy(DistributedTreeSetService.class, new ServiceConfig())
        .thenCompose(proxy -> new DistributedNavigableSetProxy<E>((ProxyClient) proxy, managementService.getPrimitiveRegistry()).connect())
        .thenApply(set -> {
          if (config.getCacheConfig().isEnabled()) {
            set = new CachingAsyncDistributedNavigableSet<>(set, config.getCacheConfig());
          }

          if (config.isReadOnly()) {
            set = new UnmodifiableAsyncDistributedNavigableSet<>(set);
          }
          return set.sync();
        });
  }
}
 
Example #22
Source File: RaftTest.java    From atomix with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a test session.
 */
private SessionClient createSession(RaftClient client, ReadConsistency consistency) throws Exception {
  return client.sessionBuilder("raft-test", TestPrimitiveType.INSTANCE, new ServiceConfig())
      .withReadConsistency(consistency)
      .withMinTimeout(Duration.ofMillis(250))
      .withMaxTimeout(Duration.ofSeconds(5))
      .build()
      .connect()
      .get(10, TimeUnit.SECONDS);
}
 
Example #23
Source File: DefaultAtomicSortedMapBuilder.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public CompletableFuture<AtomicSortedMap<K, V>> buildAsync() {
  return newProxy(AtomicTreeMapService.class, new ServiceConfig())
      .thenCompose(proxy -> new AtomicNavigableMapProxy(proxy, managementService.getPrimitiveRegistry()).connect())
      .thenApply(map -> {
        Serializer serializer = serializer();
        return new TranscodingAsyncAtomicSortedMap<K, V, byte[]>(
            (AsyncAtomicSortedMap) map,
            value -> serializer.encode(value),
            bytes -> serializer.decode(bytes))
            .sync();
      });
}
 
Example #24
Source File: DistributedLogProtocol.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Override
public <S> ProxyClient<S> newProxy(
    String primitiveName,
    PrimitiveType primitiveType,
    Class<S> serviceType,
    ServiceConfig serviceConfig,
    PartitionService partitionService) {
  return new LogProxyClient<S>(primitiveName, primitiveType, this, serviceType, serviceConfig, newClient(partitionService));
}
 
Example #25
Source File: DefaultAtomicNavigableMapBuilder.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public CompletableFuture<AtomicNavigableMap<K, V>> buildAsync() {
  return newProxy(AtomicTreeMapService.class, new ServiceConfig())
      .thenCompose(proxy -> new AtomicNavigableMapProxy(proxy, managementService.getPrimitiveRegistry()).connect())
      .thenApply(map -> {
        Serializer serializer = serializer();
        return new TranscodingAsyncAtomicNavigableMap<K, V, byte[]>(
            (AsyncAtomicNavigableMap) map,
            value -> serializer.encode(value),
            bytes -> serializer.decode(bytes))
            .sync();
      });
}
 
Example #26
Source File: DefaultAtomicCounterBuilder.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public CompletableFuture<AtomicCounter> buildAsync() {
  return newProxy(AtomicCounterService.class, new ServiceConfig())
      .thenCompose(proxy -> new AtomicCounterProxy(proxy, managementService.getPrimitiveRegistry()).connect())
      .thenApply(AsyncAtomicCounter::sync);
}
 
Example #27
Source File: RaftPerformanceTest.java    From atomix with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a test session.
 */
private SessionClient createProxy(RaftClient client) {
  return client.sessionBuilder("raft-performance-test", TestPrimitiveType.INSTANCE, new ServiceConfig())
      .withReadConsistency(READ_CONSISTENCY)
      .withCommunicationStrategy(COMMUNICATION_STRATEGY)
      .build();
}
 
Example #28
Source File: DefaultAtomicCounterMapBuilder.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public CompletableFuture<AtomicCounterMap<K>> buildAsync() {
  return newProxy(AtomicCounterMapService.class, new ServiceConfig())
      .thenCompose(proxy -> new AtomicCounterMapProxy(proxy, managementService.getPrimitiveRegistry()).connect())
      .thenApply(map -> {
        Serializer serializer = serializer();
        return new TranscodingAsyncAtomicCounterMap<K, String>(
            map,
            key -> BaseEncoding.base16().encode(serializer.encode(key)),
            string -> serializer.decode(BaseEncoding.base16().decode(string)))
            .sync();
      });
}
 
Example #29
Source File: DefaultAtomicLockBuilder.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public CompletableFuture<AtomicLock> buildAsync() {
  return newProxy(AtomicLockService.class, new ServiceConfig())
      .thenCompose(proxy -> new AtomicLockProxy(proxy, managementService.getPrimitiveRegistry()).connect())
      .thenApply(AsyncAtomicLock::sync);
}
 
Example #30
Source File: DefaultAtomicMapBuilder.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public CompletableFuture<AtomicMap<K, V>> buildAsync() {
  return newProxy(AtomicMapService.class, new ServiceConfig())
      .thenCompose(proxy -> new AtomicMapProxy((ProxyClient) proxy, managementService.getPrimitiveRegistry()).connect())
      .thenApply(rawMap -> {
        Serializer serializer = serializer();
        return new TranscodingAsyncAtomicMap<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));
      }).thenApply(map -> {
        if (!config.isNullValues()) {
          return new NotNullAsyncAtomicMap<>(map);
        }
        return map;
      }).thenCompose(map -> {
        if (config.getCacheConfig().isEnabled()) {
          if (config.getCacheConfig().getSize() == -1) {
            return new CachedAsyncAtomicMap<>(map).create();
          } else {
            return CompletableFuture.completedFuture(new CachingAsyncAtomicMap<>(map, config.getCacheConfig()));
          }
        }
        return CompletableFuture.completedFuture(map);
      })
      .thenApply(map -> {
        if (config.isReadOnly()) {
          return new UnmodifiableAsyncAtomicMap<>(map);
        }
        return map;
      })
      .thenApply(AsyncAtomicMap::sync);
}