---------------------------------------------------------------------------- Partition Provider ----------------------------------------------------------------------------
3860
function scoresProvider(publicAPI, model) {
  publicAPI.setScores = (scores) => {
    model.scores = [].concat(scores);
    const scoreMapByValue = {};
    model.scores.forEach((score) => {
      scoreMapByValue[score.value] = score;
    });
    model.scoreMapByValue = scoreMapByValue;
    publicAPI.fireScoresChange(model.scores);
  };

  publicAPI.getScoreColor = (value) => {
    const score = model.scoreMapByValue[value];
    return score ? score.color : undefined;
  };

  publicAPI.getScoreName = (value) => {
    const score = model.scoreMapByValue[value];
    return score ? score.name : undefined;
  };
  publicAPI.getDefaultScore = () => {
    if (model.scores) {
      const index = model.scores.findIndex((score) => !!score.isDefault);
      return index === -1 ? 0 : index;
    }
    return 0;
  };
  publicAPI.setDefaultScore = (value) => {
    if (model.scores) {
      model.scores[publicAPI.getDefaultScore()].isDefault = false;
      model.scores[value].isDefault = true;
    }
  };
}
1122623
@Override
    public BatchPartitionWorkUnit createPartitionWorkUnit(PartitionPlanConfig config,
                                                          Step step,
                                                          PartitionReplyQueue partitionReplyQueue,
                                                          boolean isRemoteDispatch) {

        JSLJob partitionJobModel = ParallelStepBuilder.buildPartitionLevelJSLJob(config.getTopLevelExecutionId(),
                                                                                 config.getJobProperties(),
                                                                                 step,
                                                                                 config.getPartitionNumber());

        RuntimePartitionExecution partitionExecution = jobExecutionHelper.createPartitionExecution(config, partitionJobModel, isRemoteDispatch);

        BatchPartitionWorkUnit batchWorkUnit = new BatchPartitionWorkUnit(this, partitionExecution, config, jobExecutionStartCallbacks, jobExecutionEndCallbacks, partitionReplyQueue, tranMgr);

        registerExecutingSubWorkUnit(partitionExecution.getTopLevelExecutionId(), batchWorkUnit);

        return batchWorkUnit;
    }
964376
@Override
    public CompletableFuture<Void> createAllLeasesIfNotExists(List<String> partitionIds) {
        CompletableFuture<Void> future = null;

        // Optimization: list the blobs currently existing in the directory. If there are the
        // expected number of blobs, then we can skip doing the creates.
        int blobCount = 0;
        try {
            Iterable<ListBlobItem> leaseBlobs = this.consumerGroupDirectory.listBlobs("", true, null, this.leaseOperationOptions, null);
            Iterator<ListBlobItem> blobIterator = leaseBlobs.iterator();
            while (blobIterator.hasNext()) {
                blobCount++;
                blobIterator.next();
            }
        } catch (URISyntaxException | StorageException e) {
            TRACE_LOGGER.error(this.hostContext.withHost("Exception checking lease existence - leaseContainerName: " + this.storageContainerName + " consumerGroupName: "
                    + this.hostContext.getConsumerGroupName() + " storageBlobPrefix: " + this.storageBlobPrefix), e);
            future = new CompletableFuture<Void>();
            future.completeExceptionally(LoggingUtils.wrapException(e, EventProcessorHostActionStrings.CREATING_LEASES));
        }

        if (future == null) {
            // No error checking the list, so keep going
            if (blobCount == partitionIds.size()) {
                // All expected blobs found, so short-circuit
                future = CompletableFuture.completedFuture(null);
            } else {
                // Create the blobs in parallel
                ArrayList<CompletableFuture<CompleteLease>> createFutures = new ArrayList<CompletableFuture<CompleteLease>>();

                for (String id : partitionIds) {
                    CompletableFuture<CompleteLease> oneCreate = CompletableFuture.supplyAsync(() -> {
                        CompleteLease returnLease = null;
                        try {
                            returnLease = createLeaseIfNotExistsInternal(id, this.leaseOperationOptions);
                        } catch (URISyntaxException | IOException | StorageException e) {
                            TRACE_LOGGER.error(this.hostContext.withHostAndPartition(id,
                                    "Exception creating lease - leaseContainerName: " + this.storageContainerName + " consumerGroupName: " + this.hostContext.getConsumerGroupName()
                                            + " storageBlobPrefix: " + this.storageBlobPrefix), e);
                            throw LoggingUtils.wrapException(e, EventProcessorHostActionStrings.CREATING_LEASES);
                        }
                        return returnLease;
                    }, this.hostContext.getExecutor());
                    createFutures.add(oneCreate);
                }

                CompletableFuture<?>[] dummy = new CompletableFuture<?>[createFutures.size()];
                future = CompletableFuture.allOf(createFutures.toArray(dummy));
            }
        }

        return future;
    }
15471
@Override
    public Map<String, Optional<Partition>> getPartitionsByNames(String databaseName, String tableName, List<String> partitionNames)
    {
        requireNonNull(partitionNames, "partitionNames is null");
        if (partitionNames.isEmpty()) {
            return ImmutableMap.of();
        }

        List<Partition> partitions = batchGetPartition(databaseName, tableName, partitionNames);

        Map<String, List<String>> partitionNameToPartitionValuesMap = partitionNames.stream()
                .collect(toMap(identity(), HiveUtil::toPartitionValues));
        Map<List<String>, Partition> partitionValuesToPartitionMap = partitions.stream()
                .collect(toMap(Partition::getValues, identity()));

        ImmutableMap.Builder<String, Optional<Partition>> resultBuilder = ImmutableMap.builder();
        for (Entry<String, List<String>> entry : partitionNameToPartitionValuesMap.entrySet()) {
            Partition partition = partitionValuesToPartitionMap.get(entry.getValue());
            resultBuilder.put(entry.getKey(), Optional.ofNullable(partition));
        }
        return resultBuilder.build();
    }
639177
function partition(variable, dividers) {
  generation += 1;
  return {
    type: 'partition',
    generation,
    partition: {
      variable,
      dividers: dividers.map((divider) =>
        clone(divider, ['value', 'uncertainty', 'closeToLeft'], {
          closeToLeft: false,
        })
      ),
    },
  };
}
642391
private void removePartitionResults(QueryResult queryResult, int partitionId) {
        List<QueryResultRow> rows = queryResult.getRows();
        rows.removeIf(resultRow -> getPartitionId(resultRow) == partitionId);
    }
580249
CompletableFuture<Partition> join(
      PartitionManagementService managementService,
      ThreadContextFactory threadFactory) {
    election = managementService.getElectionService().getElectionFor(partitionId);
    server = new LogPartitionServer(
        this,
        managementService,
        config,
        threadFactory);
    client = new LogPartitionClient(this, managementService, threadFactory);
    return server.start().thenCompose(v -> client.start()).thenApply(v -> this);
  }
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;
    }
617512
@Override
  public Streamlet<R> repartition(int numPartitions,
                           SerializableBiFunction<R, Integer, List<Integer>> partitionFn) {
    checkNotNull(partitionFn, "partitionFn cannot be null");

    RemapStreamlet<R> retval = new RemapStreamlet<>(this, partitionFn);
    retval.setNumPartitions(numPartitions);
    addChild(retval);
    return retval;
  }
236727
private void createPartitions(int numPartitions) {
		this.partitions.clear();
		
		ListMemorySegmentSource memSource = new ListMemorySegmentSource(this.availableMemory);
		
		for (int i = 0; i < numPartitions; i++) {
			this.partitions.add(new InMemoryPartition<T>(this.buildSideSerializer, i, memSource, this.segmentSize, pageSizeInBits));
		}
		this.compactionMemory = new InMemoryPartition<T>(this.buildSideSerializer, -1, memSource, this.segmentSize, pageSizeInBits);
	}
42241
@Override
    public Entry<BatchPartitionWorkUnit, Future<?>> startPartition(PartitionPlanConfig partitionPlanConfig,
                                                                   Step step,
                                                                   PartitionReplyQueue partitionReplyQueue,
                                                                   boolean isRemoteDispatch) {

        BatchPartitionWorkUnit workUnit = createPartitionWorkUnit(partitionPlanConfig, step, partitionReplyQueue, isRemoteDispatch);

        Future<?> futureWork = runPartition(workUnit);

        // TODO: issue "partition.started" message ?

        return new AbstractMap.SimpleEntry<BatchPartitionWorkUnit, Future<?>>(workUnit, futureWork);
    }
291330
@VisibleForTesting
	@Override
	public void requestSubpartition(int subpartitionIndex) throws IOException, InterruptedException {
		if (partitionRequestClient == null) {
			// Create a client and request the partition
			partitionRequestClient = connectionManager
				.createPartitionRequestClient(connectionId);

			partitionRequestClient.requestSubpartition(partitionId, subpartitionIndex, this, 0);
		}
	}
255879
@Override
    public long[] validatePartitioning(long[] tableIds, byte[] hashinatorConfig) {
        ByteBuffer paramBuffer = m_ee.getParamBufferForExecuteTask(4 + (8 * tableIds.length) + 4 + hashinatorConfig.length);
        paramBuffer.putInt(tableIds.length);
        for (long tableId : tableIds) {
            paramBuffer.putLong(tableId);
        }
        paramBuffer.put(hashinatorConfig);

        ByteBuffer resultBuffer = ByteBuffer.wrap(m_ee.executeTask( TaskType.VALIDATE_PARTITIONING, paramBuffer));
        long mispartitionedRows[] = new long[tableIds.length];
        for (int ii = 0; ii < tableIds.length; ii++) {
            mispartitionedRows[ii] = resultBuffer.getLong();
        }
        return mispartitionedRows;
    }
814321
static List<PartitionDefinition> findShardPartitions(Settings settings, MappingSet mappingSet, Map<String, NodeInfo> nodes,
                                                         List<List<Map<String, Object>>> shards, Log log) {
        Mapping resolvedMapping = mappingSet == null ? null : mappingSet.getResolvedView();
        List<PartitionDefinition> partitions = new ArrayList<PartitionDefinition>(shards.size());
        for (List<Map<String, Object>> group : shards) {
            String index = null;
            int shardId = -1;
            List<String> locationList = new ArrayList<String> ();
            for (Map<String, Object> replica : group) {
                ShardInfo shard = new ShardInfo(replica);
                index = shard.getIndex();
                shardId = shard.getName();
                if (nodes.containsKey(shard.getNode())) {
                    locationList.add(nodes.get(shard.getNode()).getPublishAddress());
                }
            }
            if (index == null) {
                // Could not find shards for this partition. Continue anyway?
                if (settings.getIndexReadAllowRedStatus()) {
                    log.warn("Shard information is missing from an index and will not be reached during job execution. " +
                            "Assuming shard is unavailable and cluster is red! Continuing with read operation by " +
                            "skipping this shard! This may result in incomplete data retrieval!");
                } else {
                    throw new IllegalStateException("Could not locate shard information for one of the read indices. " +
                            "Check your cluster status to see if it is unstable!");
                }
            } else {
                PartitionDefinition partition = new PartitionDefinition(settings, resolvedMapping, index, shardId,
                        locationList.toArray(new String[0]));
                partitions.add(partition);
            }
        }
        return partitions;
    }
1176113
public void addOrAlterPartition(HiveTable table, HivePartition partition) throws IOException {
    if (!addPartitionIfNotExists(table, partition)) {
      alterPartition(table, partition);
    }
  }
1349702
private void compactPartition(final int partitionNumber) throws IOException {
		// do nothing if table was closed, parameter is invalid or no garbage exists
		if (this.closed || partitionNumber >= this.partitions.size() || this.partitions.get(partitionNumber).isCompacted()) {
			return;
		}
		// release all segments owned by compaction partition
		this.compactionMemory.clearAllMemory(availableMemory);
		this.compactionMemory.allocateSegments(1);
		this.compactionMemory.pushDownPages();
		T tempHolder = this.buildSideSerializer.createInstance();
		final int numPartitions = this.partitions.size();
		InMemoryPartition<T> partition = this.partitions.remove(partitionNumber);
		MemorySegment[] overflowSegments = partition.overflowSegments;
		long pointer;
		int pointerOffset;
		int bucketOffset;
		final int bucketsPerSegment = this.bucketsPerSegmentMask + 1;
		for (int i = 0, bucket = partitionNumber; i < this.buckets.length && bucket < this.numBuckets; i++) {
			MemorySegment segment = this.buckets[i];
			// go over all buckets in the segment belonging to the partition
			for (int k = bucket % bucketsPerSegment; k < bucketsPerSegment && bucket < this.numBuckets; k += numPartitions, bucket += numPartitions) {
				bucketOffset = k * HASH_BUCKET_SIZE;
				if((int)segment.get(bucketOffset + HEADER_PARTITION_OFFSET) != partitionNumber) {
					throw new IOException("Accessed wrong bucket! wanted: " + partitionNumber + " got: " + segment.get(bucketOffset + HEADER_PARTITION_OFFSET));
				}
				// loop over all segments that are involved in the bucket (original bucket plus overflow buckets)
				int countInSegment = segment.getInt(bucketOffset + HEADER_COUNT_OFFSET);
				int numInSegment = 0;
				pointerOffset = bucketOffset + BUCKET_POINTER_START_OFFSET;
				while (true) {
					while (numInSegment < countInSegment) {
						pointer = segment.getLong(pointerOffset);
						tempHolder = partition.readRecordAt(pointer, tempHolder);
						pointer = this.compactionMemory.appendRecord(tempHolder);
						segment.putLong(pointerOffset, pointer);
						pointerOffset += POINTER_LEN;
						numInSegment++;
					}
					// this segment is done. check if there is another chained bucket
					final long forwardPointer = segment.getLong(bucketOffset + HEADER_FORWARD_OFFSET);
					if (forwardPointer == BUCKET_FORWARD_POINTER_NOT_SET) {
						break;
					}
					final int overflowSegNum = (int) (forwardPointer >>> 32);
					segment = overflowSegments[overflowSegNum];
					bucketOffset = (int) forwardPointer;
					countInSegment = segment.getInt(bucketOffset + HEADER_COUNT_OFFSET);
					pointerOffset = bucketOffset + BUCKET_POINTER_START_OFFSET;
					numInSegment = 0;
				}
				segment = this.buckets[i];
			}
		}
		// swap partition with compaction partition
		this.compactionMemory.setPartitionNumber(partitionNumber);
		this.partitions.add(partitionNumber, compactionMemory);
		this.partitions.get(partitionNumber).overflowSegments = partition.overflowSegments;
		this.partitions.get(partitionNumber).numOverflowSegments = partition.numOverflowSegments;
		this.partitions.get(partitionNumber).nextOverflowBucket = partition.nextOverflowBucket;
		this.partitions.get(partitionNumber).setIsCompacted(true);
		//this.partitions.get(partitionNumber).pushDownPages();
		this.compactionMemory = partition;
		this.compactionMemory.resetRecordCounter();
		this.compactionMemory.setPartitionNumber(-1);
		this.compactionMemory.overflowSegments = null;
		this.compactionMemory.numOverflowSegments = 0;
		this.compactionMemory.nextOverflowBucket = 0;
		// try to allocate maximum segment count
		this.compactionMemory.clearAllMemory(this.availableMemory);
		int maxSegmentNumber = this.getMaxPartition();
		this.compactionMemory.allocateSegments(maxSegmentNumber);
		this.compactionMemory.resetRWViews();
		this.compactionMemory.pushDownPages();
	}
40707
function Partition( buffer, start, end ) {

  if( !(this instanceof Partition) )
    return new Partition( buffer, start, end )

  if( buffer != null && !Buffer.isBuffer( buffer ) )
    throw new TypeError( 'Argument must be a Buffer' )

  /** @type {Number} Partition status */
  this.status = 0x00
  /** @type {Number} Partition type identifier */
  this.type = 0x00
  /** @type {Number} Sector count */
  this.sectors = 0x00
  /** @type {Number} LBA of first sector */
  this.firstLBA = 0x00
  /** @type {Number} CHS address of first sector */
  this.firstCHS = new CHS()
  /** @type {Number} CHS address of last sector */
  this.lastCHS = new CHS()

  if( buffer != null ) {
    this.buffer = buffer.slice( start, end )
  }

}