Calculate the vbucket for the given key.

@param key the key to calculate from.
@param numPartitions the number of partitions in the bucket.
@return the calculated partition.
17833
private static int partitionForKey(byte[] key, int numPartitions) {
        CRC32 crc32 = new CRC32();
        crc32.update(key);
        long rv = (crc32.getValue() >> 16) & 0x7fff;
        return (int) rv &numPartitions - 1;
    }
411004
@PublicEvolving
	public PartitionOperator<T> withOrders(Order... orders) {
		Preconditions.checkState(pMethod == PartitionMethod.RANGE, "Orders cannot be applied for %s partition " +
				"method", pMethod);
		Preconditions.checkArgument(pKeys.getOriginalKeyFieldTypes().length == orders.length, "The number of key " +
				"fields and orders should be the same.");
		this.orders = orders;

		return this;
	}
1314547
public int getPartition(byte[] key,
                            byte[] value,
                            int numReduceTasks) {
        try {
            /**
             * {@link partitionId} is the Voldemort primary partition that this
             * record belongs to.
             */
            int partitionId = ByteUtils.readInt(value, ByteUtils.SIZE_OF_INT);

            /**
             * This is the base number we will ultimately mod by {@link numReduceTasks}
             * to determine which reduce task to shuffle to.
             */
            int magicNumber = partitionId;

            if (getSaveKeys() && !buildPrimaryReplicasOnly) {
                /**
                 * When saveKeys is enabled (which also implies we are generating
                 * READ_ONLY_V2 format files), then we are generating files with
                 * a replica type, with one file per replica.
                 *
                 * Each replica is sent to a different reducer, and thus the
                 * {@link magicNumber} is scaled accordingly.
                 *
                 * The downside to this is that it is pretty wasteful. The files
                 * generated for each replicas are identical to one another, so
                 * there's no point in generating them independently in many
                 * reducers.
                 *
                 * This is one of the reasons why buildPrimaryReplicasOnly was
                 * written. In this mode, we only generate the files for the
                 * primary replica, which means the number of reducers is
                 * minimized and {@link magicNumber} does not need to be scaled.
                 */
                int replicaType = (int) ByteUtils.readBytes(value,
                                                            2 * ByteUtils.SIZE_OF_INT,
                                                            ByteUtils.SIZE_OF_BYTE);
                magicNumber = magicNumber * getStoreDef().getReplicationFactor() + replicaType;
            }

            if (!getReducerPerBucket()) {
                /**
                 * Partition files can be split in many chunks in order to limit the
                 * maximum file size downloaded and handled by Voldemort servers.
                 *
                 * {@link chunkId} represents which chunk of partition then current
                 * record belongs to.
                 */
                int chunkId = ReadOnlyUtils.chunk(key, getNumChunks());

                /**
                 * When reducerPerBucket is disabled, all chunks are sent to a
                 * different reducer. This increases parallelism at the expense
                 * of adding more load on Hadoop.
                 *
                 * {@link magicNumber} is thus scaled accordingly, in order to
                 * leverage the extra reducers available to us.
                 */
                magicNumber = magicNumber * getNumChunks() + chunkId;
            }

            /**
             * Finally, we mod {@link magicNumber} by {@link numReduceTasks},
             * since the MapReduce framework expects the return of this function
             * to be bounded by the number of reduce tasks running in the job.
             */
            return magicNumber % numReduceTasks;
        } catch (Exception e) {
            throw new VoldemortException("Caught exception in getPartition()!" +
                                         " key: " + ByteUtils.toHexString(key) +
                                         ", value: " + ByteUtils.toHexString(value) +
                                         ", numReduceTasks: " + numReduceTasks, e);
        }
    }
1202642
def repartition(self, npartitions):
        
        if self.mode == 'spark':
            return self._constructor(self.values.repartition(npartitions)).__finalize__(self)
        else:
            notsupported(self.mode)
399479
private static BucketConfiguration createDefaultConfig(String hostname, int numNodes, int bucketStartPort, int numVBuckets, int numReplicas) {
        BucketConfiguration defaultConfig = new BucketConfiguration();
        defaultConfig.type = BucketType.COUCHBASE;
        defaultConfig.hostname = hostname;
        defaultConfig.numNodes = numNodes;
        if (numReplicas > -1) {
            defaultConfig.numReplicas = numReplicas;
        }

        defaultConfig.bucketStartPort = bucketStartPort;
        defaultConfig.numVBuckets = numVBuckets;
        return defaultConfig;
    }
606711
def partition_by(num_partitions, partition_func=nil)
      num_partitions ||= default_reduce_partitions
      partition_func ||= 'lambda{|x| Spark::Digest.portable_hash(x.to_s)}'

      _partition_by(num_partitions, Spark::Command::PartitionBy::Basic, partition_func)
    end
143157
@Override
    public <R> S3ManifestComparator<R> withKey(String key) {
        return new S3ManifestComparator<>(
                                          key, reader, writer, deleter, stringWriter);
    }
1260876
private double toPerPartitionMaxSize(int maxConfiguredSize, String mapName) {
        int memberCount = clusterService.getSize(DATA_MEMBER_SELECTOR);
        double translatedPartitionSize = (1D * maxConfiguredSize * memberCount / partitionCount);

        if (translatedPartitionSize < 1) {
            translatedPartitionSize = MIN_TRANSLATED_PARTITION_SIZE;
            logMisconfiguredPerNodeMaxSize(mapName, memberCount);
        }

        return translatedPartitionSize;
    }
714779
@SuppressWarnings("unchecked")
    private Map<Integer, Object[]> partitionArguments(final DataSourceProvider dataSourceProvider,
                                                      final Object[] args) {

        // use TreeMap here to maintain ordering by shard ID
        final Map<Integer, Object[]> argumentsByShardId = Maps.newTreeMap();

        // we need to partition by datasource instead of virtual shard ID (different virtual shard IDs are mapped to
        // the same datasource e.g. by VirtualShardMd5Strategy)
        final Map<DataSource, Integer> shardIdByDataSource = Maps.newHashMap();

        // TODO: currently only implemented for single shardKey argument as first argument!
        final List<Object> originalArgument = (List<Object>) args[0];
        if (originalArgument == null || originalArgument.isEmpty()) {
            throw new IllegalArgumentException("ShardKey (first argument) of sproc '" + name + "' not defined");
        }

        List<Object> partitionedArgument = null;
        Object[] partitionedArguments = null;
        int shardId;
        Integer existingShardId;
        DataSource dataSource;
        for (final Object key : originalArgument) {
            shardId = getShardId(new Object[] {key});
            dataSource = dataSourceProvider.getDataSource(shardId);
            existingShardId = shardIdByDataSource.get(dataSource);
            if (existingShardId != null) {

                // we already saw the same datasource => use the virtual shard ID of the first argument with the same
                // datasource
                shardId = existingShardId;
            } else {
                shardIdByDataSource.put(dataSource, shardId);
            }

            partitionedArguments = argumentsByShardId.get(shardId);
            if (partitionedArguments == null) {

                partitionedArgument = Lists.newArrayList();
                partitionedArguments = new Object[args.length];
                partitionedArguments[0] = partitionedArgument;
                if (args.length > 1) {
                    System.arraycopy(args, 1, partitionedArguments, 1, args.length - 1);
                }

                argumentsByShardId.put(shardId, partitionedArguments);
            } else {
                partitionedArgument = (List<Object>) partitionedArguments[0];
            }

            partitionedArgument.add(key);

        }

        return argumentsByShardId;
    }
88889
private HashCalculationResult hashBuckets(byte[] fileWithoutSpaces, double bucketSize) throws IOException {
        // int(bucket_size) will round down the bucket_size: IE: 1.2 -> 1.0
        int bucketIntSize = (int) bucketSize;

        // Get bytes and calculate sha1
        byte[] mostSigBytes = Arrays.copyOfRange(fileWithoutSpaces, 0, bucketIntSize);
        int length = fileWithoutSpaces.length;
        byte[] leastSigBytes = Arrays.copyOfRange(fileWithoutSpaces, length - bucketIntSize, length);
        String fullFileHash = calculateByteArraySHA1(fileWithoutSpaces);
        String mostSigBitsHash = calculateByteArraySHA1(mostSigBytes);
        String leastSigBitsHash = calculateByteArraySHA1(leastSigBytes);
        return new HashCalculationResult(fullFileHash, mostSigBitsHash, leastSigBitsHash);
    }
240940
private static void buildNestedBuckets(HashMap m, String p) {
        String[] components = p.split(":");
        int cl = components.length;
        if(cl == 1) {
            if( ! m.containsKey(components[0]) )
                m.put(components[0], new HashMap());
        } else {
            HashMap temp = m;
            for(int i=0; i < cl; i++) {
                if( ! temp.containsKey(components[i]) )
                    temp.put(components[i], new HashMap());
                temp = (HashMap) temp.get(components[i]);
            }
        }
    }
1259210
private static int getBucketCount(SimpleOrderedMap<Object> solrFacets, int defaultCount) {
        List<SimpleOrderedMap<Object>> solrBuckets = (List<SimpleOrderedMap<Object>>) solrFacets.get("buckets");
        if (solrBuckets == null) {
            for (int i = 0; i < solrFacets.size(); i++) {
                if (solrFacets.getName(i).equals("count")) {
                    return (int) solrFacets.getVal(i);
                }
            }
        }
        return defaultCount;
    }
699961
private static void setBucketCountsAndExemplars(
      List<Bucket> buckets, Distribution.Builder builder) {
    // The first bucket (underflow bucket) should always be 0 count because the Metrics first bucket
    // is [0, first_bound) but StackDriver distribution consists of an underflow bucket (number 0).
    builder.addBucketCounts(0L);
    for (Bucket bucket : buckets) {
      builder.addBucketCounts(bucket.getCount());
      @javax.annotation.Nullable
      io.opencensus.metrics.data.Exemplar exemplar = bucket.getExemplar();
      if (exemplar != null) {
        builder.addExemplars(toProtoExemplar(exemplar));
      }
    }
  }
1149782
@VisibleForTesting
    public long[] getHashBuckets(ByteBuffer key, int hashCount, long max)
    {
        long[] hash = new long[2];
        hash(key, key.position(), key.remaining(), 0L, hash);
        long[] indexes = new long[hashCount];
        setIndexes(hash[0], hash[1], hashCount, max, indexes);
        return indexes;
    }
845143
public static <E> Distribution<E> getDistributionFromPartiallySpecifiedCounter(Counter<E> c, int numKeys){
    Distribution<E> d;
    double total = c.totalCount();
    if (total >= 1.0){
      d = getDistribution(c);
      d.numberOfKeys = numKeys;
    } else {
      d = new Distribution<E>();
      d.numberOfKeys = numKeys;
      d.counter = c;
      d.reservedMass = 1.0 - total;
    }
    return d;
  }
488667
static Partition createRangePartitionWithSubPartition(
            SqlgGraph sqlgGraph,
            AbstractLabel abstractLabel,
            String name,
            String from,
            String to,
            PartitionType partitionType,
            String partitionExpression) {

        Preconditions.checkArgument(!abstractLabel.getSchema().isSqlgSchema(), "createRangePartitionWithSubPartition may not be called for \"%s\"", Topology.SQLG_SCHEMA);
        Preconditions.checkState(abstractLabel.isRangePartition());
        Partition partition = new Partition(sqlgGraph, abstractLabel, name, from, to, partitionType, partitionExpression);
        partition.createRangePartitionOnDb();
        if (abstractLabel instanceof VertexLabel) {
            TopologyManager.addVertexLabelPartition(
                    sqlgGraph,
                    abstractLabel.getSchema().getName(),
                    abstractLabel.getName(),
                    name,
                    from,
                    to,
                    partitionType,
                    partitionExpression);
        } else {
            TopologyManager.addEdgeLabelPartition(sqlgGraph, abstractLabel, name, from, to, partitionType, partitionExpression);
        }
        partition.committed = false;
        return partition;
    }
807432
@Override
  public List<? extends OutputWriter<ByteBuffer>> createWriters(int numShards) {
    ImmutableList.Builder<SizeSegmentingGoogleCloudStorageFileWriter> result =
        new ImmutableList.Builder<>();
    for (int i = 0; i < numShards; ++i) {
      result.add(new SizeSegmentingGoogleCloudStorageFileWriter(bucket, fileNamePattern, i,
          mimeType, segmentSizeLimit));
    }
    return result.build();
  }