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

org.infinispan.query.affinity.LocalModeShardDistribution Maven / Gradle / Ivy

package org.infinispan.query.affinity;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import org.infinispan.commons.util.CollectionFactory;
import org.infinispan.remoting.transport.Address;
import org.infinispan.remoting.transport.LocalModeAddress;

/**
 * {@link ShardDistribution} for non-clustered indexes, all shardsIdentifiers are associated with a single server.
 *
 * @since 9.0
 */
final class LocalModeShardDistribution implements ShardDistribution {

   private final Set segments;
   private final Set shardsIdentifiers;
   private final Address localAddress = LocalModeAddress.INSTANCE;

   private final Map shardPerSegmentMap = CollectionFactory.makeConcurrentMap();
   private final Map> segmentPerShardMap = CollectionFactory.makeConcurrentMap();

   LocalModeShardDistribution(int numSegments, int numShards) {
      this.segments = IntStream.range(0, numSegments).boxed().collect(Collectors.toSet());
      this.shardsIdentifiers = IntStream.range(0, numShards).boxed().map(String::valueOf).collect(Collectors.toSet());
      this.distribute(numShards);
   }

   private void distribute(int numShards) {
      List> shardsDistribution = this.split(segments, numShards);
      int i = 0;
      for (Set shardSegments : shardsDistribution) {
         String shardId = String.valueOf(i++);
         segmentPerShardMap.put(shardId, shardSegments);
         shardSegments.forEach(s -> shardPerSegmentMap.put(s, shardId));
      }
   }

   @Override
   public Set getShardsIdentifiers() {
      return Collections.unmodifiableSet(shardsIdentifiers);
   }

   @Override
   public Address getOwner(String shardId) {
      return localAddress;
   }

   @Override
   public Set getShards(Address address) {
      return Collections.unmodifiableSet(shardsIdentifiers);
   }

   @Override
   public String getShardFromSegment(Integer segment) {
      return shardPerSegmentMap.get(segment);
   }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy