use of org.apache.commons.lang3.tuple.MutablePair in project pinot by linkedin.
the class Cube method sortDimensionOrder.
/**
* Sort dimensions according to their cost, which is the sum of the error for aggregating all its children rows.
* Dimensions with larger error is sorted in the front of the list.
* The order among the dimensions that belong to the same hierarchical group will be maintained. An example of
* a hierarchical group {continent, country}. The cost of a group is the average of member costs.
* @throws Exception An exception is thrown if OLAP database cannot be connected.
*/
private static Dimensions sortDimensionOrder(List<DimNameValueCostEntry> costSet, Dimensions dimensions, int topDimension, List<List<String>> hierarchy) throws Exception {
List<MutablePair<String, Double>> dimensionCostPairs = new ArrayList<>();
Map<String, Double> dimNameToCost = new HashMap<>();
for (DimNameValueCostEntry dimNameValueCostEntry : costSet) {
double cost = dimNameValueCostEntry.getCost();
if (dimNameToCost.containsKey(dimNameValueCostEntry.getDimName())) {
cost += dimNameToCost.get(dimNameValueCostEntry.getDimName());
}
dimNameToCost.put(dimNameValueCostEntry.getDimName(), cost);
}
// Given one dimension name D, returns the hierarchical dimension to which D belong.
Map<String, HierarchicalDimension> hierarchicalDimensionMap = new HashMap<>();
Set<String> availableDimensionKeySet = new HashSet<>(dimensions.allDimensions());
// dimensions of the dataset.
for (List<String> suggestedHierarchyList : hierarchy) {
if (suggestedHierarchyList == null || suggestedHierarchyList.size() < 2) {
continue;
}
List<String> actualHierarchy = new ArrayList<>();
for (String dimension : suggestedHierarchyList) {
if (availableDimensionKeySet.contains(dimension)) {
actualHierarchy.add(dimension);
}
}
if (actualHierarchy.size() > 1) {
HierarchicalDimension hierarchicalDimension = new HierarchicalDimension();
hierarchicalDimension.hierarchy = actualHierarchy;
for (String dimension : actualHierarchy) {
hierarchicalDimensionMap.put(dimension, hierarchicalDimension);
}
hierarchicalDimension.index = dimensionCostPairs.size();
dimensionCostPairs.add(new MutablePair<>(actualHierarchy.get(0), .0));
}
}
// cost among all the children in that hierarchy.
for (int i = 0; i < dimensions.size(); ++i) {
String dimension = dimensions.get(i);
double cost = 0d;
if (dimNameToCost.containsKey(dimension)) {
cost += dimNameToCost.get(dimension);
}
if (hierarchicalDimensionMap.containsKey(dimension)) {
HierarchicalDimension hierarchicalDimension = hierarchicalDimensionMap.get(dimension);
MutablePair<String, Double> costOfDimensionPair = dimensionCostPairs.get(hierarchicalDimension.index);
// The max cost of children will be the cost of a group
costOfDimensionPair.right = Math.max(cost, costOfDimensionPair.right);
} else {
// The dimension does not belong to any hierarchy
MutablePair<String, Double> costOfDimensionPair = new MutablePair<>(dimension, cost);
dimensionCostPairs.add(costOfDimensionPair);
}
}
// Sort dimensions according to their costs in a descending order
dimensionCostPairs.sort((new DimensionCostPairSorter()).reversed());
// If there exists a huge gap (e.g., 1/10 of cost) between two cost pairs, then we chop of the dimensions because
// pairs with small costs does not provide useful information
// Invariance to keep: cutOffPairIdx <= number of dimensionCostPairs
int cutOffPairIdx = 1;
if (dimensionCostPairs.size() > 1) {
double cutOffCost = dimensionCostPairs.get(0).getRight() / 10d;
for (; cutOffPairIdx < dimensionCostPairs.size(); ++cutOffPairIdx) {
double curCost = dimensionCostPairs.get(cutOffPairIdx).getRight();
if (Double.compare(cutOffCost, curCost) > 0) {
break;
}
}
} else {
cutOffPairIdx = 0;
}
// Create a new Dimension instance whose dimensions follow the calculated order
ArrayList<String> newDimensions = new ArrayList<>();
int pairIdx = 0;
for (MutablePair<String, Double> dimensionCostPair : dimensionCostPairs) {
StringBuilder sb = new StringBuilder(" Dimension: ");
if (hierarchicalDimensionMap.containsKey(dimensionCostPair.getLeft())) {
HierarchicalDimension hierarchicalDimension = hierarchicalDimensionMap.get(dimensionCostPair.getLeft());
if (pairIdx <= cutOffPairIdx) {
newDimensions.addAll(hierarchicalDimension.hierarchy);
}
sb.append(hierarchicalDimension.hierarchy);
} else {
// The dimension does not belong to any hierarchy
if (pairIdx <= cutOffPairIdx) {
newDimensions.add(dimensionCostPair.getLeft());
}
sb.append(dimensionCostPair.getLeft());
}
sb.append(", Cost: ");
sb.append(dimensionCostPair.getRight());
LOG.info(sb.toString());
++pairIdx;
}
return new Dimensions(newDimensions.subList(0, Math.min(topDimension, newDimensions.size())));
}
use of org.apache.commons.lang3.tuple.MutablePair in project PneumaticCraft by MineMaarten.
the class TileEntityPneumaticBase method disperseAir.
private void disperseAir(List<Pair<ForgeDirection, IAirHandler>> teList) {
boolean shouldRepeat = false;
List<Pair<Integer, Integer>> dispersion = new ArrayList<Pair<Integer, Integer>>();
do {
shouldRepeat = false;
//Add up every volume and air.
int totalVolume = getVolume();
int totalAir = currentAir;
for (Pair<ForgeDirection, IAirHandler> entry : teList) {
IAirHandler airHandler = entry.getValue();
totalVolume += airHandler.getVolume();
totalAir += airHandler.getCurrentAir(entry.getKey().getOpposite());
}
//Only go push based, ignore any machines that have a higher pressure than this block.
Iterator<Pair<ForgeDirection, IAirHandler>> iterator = teList.iterator();
while (iterator.hasNext()) {
Pair<ForgeDirection, IAirHandler> entry = iterator.next();
IAirHandler airHandler = entry.getValue();
//Calculate the total air the machine is going to get.
int totalMachineAir = (int) ((long) totalAir * airHandler.getVolume() / totalVolume);
int airDispersed = totalMachineAir - airHandler.getCurrentAir(entry.getKey().getOpposite());
if (airDispersed < 0) {
iterator.remove();
shouldRepeat = true;
dispersion.clear();
break;
} else {
dispersion.add(new MutablePair(getMaxDispersion(entry.getKey()), airDispersed));
}
}
} while (shouldRepeat);
int toBeDivided = 0;
int receivers = dispersion.size();
for (Pair<Integer, Integer> disp : dispersion) {
if (disp.getValue() > disp.getKey()) {
//Any air that wants to go to a neighbor, but can't (because of regulator module) gives back its air.
toBeDivided += disp.getValue() - disp.getKey();
disp.setValue(disp.getKey());
receivers--;
}
}
while (toBeDivided >= receivers && receivers > 0) {
//try to give every receiver an equal part of the to be divided air.
int dividedValue = toBeDivided / receivers;
for (Pair<Integer, Integer> disp : dispersion) {
int maxTransfer = disp.getKey() - disp.getValue();
if (maxTransfer > 0) {
if (maxTransfer <= dividedValue) {
//next step this receiver won't be able to receive any air.
receivers--;
}
//cap it at the max it can have.
int transfered = Math.min(dividedValue, maxTransfer);
disp.setValue(disp.getValue() + transfered);
toBeDivided -= transfered;
} else {
receivers--;
}
}
}
for (int i = 0; i < teList.size(); i++) {
IAirHandler neighbor = teList.get(i).getValue();
int transferedAir = dispersion.get(i).getValue();
onAirDispersion(transferedAir, teList.get(i).getKey());
neighbor.addAir(transferedAir, teList.get(i).getKey().getOpposite());
addAir(-transferedAir, teList.get(i).getKey());
}
}
use of org.apache.commons.lang3.tuple.MutablePair in project DataX by alibaba.
the class HdfsHelper method transportOneRecord.
public static MutablePair<Text, Boolean> transportOneRecord(Record record, char fieldDelimiter, List<Configuration> columnsConfiguration, TaskPluginCollector taskPluginCollector) {
MutablePair<List<Object>, Boolean> transportResultList = transportOneRecord(record, columnsConfiguration, taskPluginCollector);
//保存<转换后的数据,是否是脏数据>
MutablePair<Text, Boolean> transportResult = new MutablePair<Text, Boolean>();
transportResult.setRight(false);
if (null != transportResultList) {
Text recordResult = new Text(StringUtils.join(transportResultList.getLeft(), fieldDelimiter));
transportResult.setRight(transportResultList.getRight());
transportResult.setLeft(recordResult);
}
return transportResult;
}
use of org.apache.commons.lang3.tuple.MutablePair in project pulsar by yahoo.
the class Consumer method sendMessages.
/**
* Dispatch a list of entries to the consumer.
*
* @return a promise that can be use to track when all the data has been written into the socket
*/
public Pair<ChannelPromise, Integer> sendMessages(final List<Entry> entries) {
final ChannelHandlerContext ctx = cnx.ctx();
final MutablePair<ChannelPromise, Integer> sentMessages = new MutablePair<ChannelPromise, Integer>();
final ChannelPromise writePromise = ctx.newPromise();
sentMessages.setLeft(writePromise);
if (entries.isEmpty()) {
if (log.isDebugEnabled()) {
log.debug("[{}] List of messages is empty, triggering write future immediately for consumerId {}", subscription, consumerId);
}
writePromise.setSuccess();
sentMessages.setRight(0);
return sentMessages;
}
try {
sentMessages.setRight(updatePermitsAndPendingAcks(entries));
} catch (PulsarServerException pe) {
log.warn("[{}] [{}] consumer doesn't support batch-message {}", subscription, consumerId, cnx.getRemoteEndpointProtocolVersion());
subscription.markTopicWithBatchMessagePublished();
sentMessages.setRight(0);
// disconnect consumer: it will update dispatcher's availablePermits and resend pendingAck-messages of this
// consumer to other consumer
disconnect();
return sentMessages;
}
ctx.channel().eventLoop().execute(() -> {
for (int i = 0; i < entries.size(); i++) {
Entry entry = entries.get(i);
PositionImpl pos = (PositionImpl) entry.getPosition();
MessageIdData.Builder messageIdBuilder = MessageIdData.newBuilder();
MessageIdData messageId = messageIdBuilder.setLedgerId(pos.getLedgerId()).setEntryId(pos.getEntryId()).build();
ByteBuf metadataAndPayload = entry.getDataBuffer();
// skip checksum by incrementing reader-index if consumer-client doesn't support checksum verification
if (cnx.getRemoteEndpointProtocolVersion() < ProtocolVersion.v6.getNumber()) {
readChecksum(metadataAndPayload);
}
// stats
msgOut.recordEvent(metadataAndPayload.readableBytes());
if (log.isDebugEnabled()) {
log.debug("[{}] Sending message to consumerId {}, entry id {}", subscription, consumerId, pos.getEntryId());
}
// We only want to pass the "real" promise on the last entry written
ChannelPromise promise = ctx.voidPromise();
if (i == (entries.size() - 1)) {
promise = writePromise;
}
ctx.write(Commands.newMessage(consumerId, messageId, metadataAndPayload), promise);
messageId.recycle();
messageIdBuilder.recycle();
}
ctx.flush();
});
return sentMessages;
}
use of org.apache.commons.lang3.tuple.MutablePair in project apex-core by apache.
the class BlacklistBasedResourceRequestHandler method recreateContainerRequest.
private void recreateContainerRequest(Map<StreamingContainerAgent.ContainerStartRequest, MutablePair<Integer, ContainerRequest>> requestedResources, int loopCounter, ResourceRequestHandler resourceRequestor, List<ContainerRequest> removedContainerRequests) {
for (Map.Entry<StreamingContainerAgent.ContainerStartRequest, MutablePair<Integer, ContainerRequest>> entry : requestedResources.entrySet()) {
if ((loopCounter - entry.getValue().getKey()) > NUMBER_MISSED_HEARTBEATS) {
StreamingContainerAgent.ContainerStartRequest csr = entry.getKey();
removedContainerRequests.add(entry.getValue().getRight());
ContainerRequest cr = resourceRequestor.createContainerRequest(csr, false);
if (cr.getNodes() != null && !cr.getNodes().isEmpty()) {
addHostSpecificRequest(csr, cr);
} else {
otherContainerRequests.put(cr, csr);
}
}
}
}
Aggregations