List of usage examples for com.google.common.collect Multimaps invertFrom
public static <K, V, M extends Multimap<K, V>> M invertFrom(Multimap<? extends V, ? extends K> source, M dest)
From source file:org.apache.aurora.scheduler.thrift.ReadOnlySchedulerImpl.java
private static Set<ConfigGroup> instancesToConfigGroups(Map<Integer, ITaskConfig> tasks) { Multimap<ITaskConfig, Integer> instancesByDetails = Multimaps.invertFrom(Multimaps.forMap(tasks), HashMultimap.create());/*from w w w . ja v a 2s. c o m*/ return ImmutableSet.copyOf(Iterables.transform(instancesByDetails.asMap().entrySet(), TO_GROUP)); }
From source file:com.processconfiguration.ConfigurationAlgorithm.java
/** * Identify process elements which do not occur in any possible process execution trace. * * @param definitions a BPMN model/* ww w. jav a2 s . c o m*/ * @return any elements of the model aren't connected to both a start event and an end event */ static Set<TBaseElement> findOrphans(final TDefinitions definitions) { final Set<TBaseElement> all = new HashSet<>(); // all elements final Set<TBaseElement> canStart = new HashSet<>(); // elements connected to a start event final Set<TBaseElement> canEnd = new HashSet<>(); // elements connected to an end event final Multimap<TBaseElement, TBaseElement> incomingMap = HashMultimap.create(); final Multimap<TBaseElement, TBaseElement> outgoingMap = HashMultimap.create(); // Populate incomingMap by traversing all the edges (not the nodes [except boundary events]) of the document graph definitions.accept(new TraversingVisitor(new MyTraverser() { @Override public void traverse(Configurable.Configuration aBean, Visitor aVisitor) { } }, new InheritingVisitor() { @Override public void visit(final TAssociation that) { try { TBaseElement source = ((BpmnDefinitions) definitions).findElement(that.getSourceRef()); if (source != null) { incomingMap.put(that, source); incomingMap.put(source, that); // for the purposes of marking, associations count in both directions } TBaseElement target = ((BpmnDefinitions) definitions).findElement(that.getTargetRef()); if (target != null) { incomingMap.put(target, that); incomingMap.put(that, target); // for the purposes of marking, associations count in both directions } } catch (CanoniserException e) { e.printStackTrace(); } } @Override public void visit(final TBoundaryEvent that) { try { TBaseElement activity = (TBaseElement) ((BpmnDefinitions) definitions) .findElement(that.getAttachedToRef()); if (!(activity instanceof TActivity)) { LOGGER.warning("Boundary event " + that.getId() + " attached to " + activity.getId() + " which is not an activity"); } incomingMap.put(that, activity); } catch (CanoniserException e) { e.printStackTrace(); } } @Override public void visit(final TDataAssociation that) { for (JAXBElement<Object> jeo : that.getSourceRef()) { TBaseElement source = (TBaseElement) jeo.getValue(); incomingMap.put(that, source); incomingMap.put(source, that); // for the purposes of marking, data associations count in both directions } TBaseElement target = that.getTargetRef(); if (target != null) { incomingMap.put(target, that); incomingMap.put(that, target); // for the purposes of marking, data associations count in both directions } } @Override public void visit(final TMessageFlow that) { try { TBaseElement source = ((BpmnDefinitions) definitions).findElement(that.getSourceRef()); if (source != null) { incomingMap.put(that, source); incomingMap.put(source, that); // for the purposes of marking, message flows count in both directions } TBaseElement target = ((BpmnDefinitions) definitions).findElement(that.getTargetRef()); if (target != null) { incomingMap.put(target, that); incomingMap.put(that, that); // for the purposes of marking, message flows count in both directions } } catch (CanoniserException e) { e.printStackTrace(); } } @Override public void visit(final TSequenceFlow that) { if (that.getSourceRef() != null) { incomingMap.put(that, that.getSourceRef()); } if (that.getTargetRef() != null) { incomingMap.put(that.getTargetRef(), that); } } })); // Populate outgoingMap Multimaps.invertFrom(incomingMap, outgoingMap); // Populate the sets of elements: all, canStart, canEnd definitions.accept(new TraversingVisitor(new MyTraverser() { @Override public void traverse(Configurable.Configuration aBean, Visitor aVisitor) { } }, new InheritingVisitor() { @Override public void visit(final TArtifact that) { super.visit(that); all.add(that); } @Override public void visit(final TDataAssociation that) { super.visit(that); all.add(that); } @Override public void visit(final TEndEvent that) { super.visit(that); mark((BpmnDefinitions) definitions, that, canEnd, Direction.BACKWARDS, incomingMap, outgoingMap); } @Override public void visit(final TFlowElement that) { super.visit(that); all.add(that); } @Override public void visit(final TGroup that) { // This artifact doesn't get added to "all", so no super call } // Link events are treated as if they were start events because we're // working with diagrams rather than handling processes properly @Override public void visit(final TIntermediateCatchEvent that) { super.visit(that); for (JAXBElement<? extends TEventDefinition> jed : that.getEventDefinition()) { if (jed.getValue() instanceof TLinkEventDefinition) { mark((BpmnDefinitions) definitions, that, canStart, Direction.FORWARDS, incomingMap, outgoingMap); break; } } } // Link events are treated as if they were end events because [see above] @Override public void visit(final TIntermediateThrowEvent that) { super.visit(that); for (JAXBElement<? extends TEventDefinition> jed : that.getEventDefinition()) { if (jed.getValue() instanceof TLinkEventDefinition) { mark((BpmnDefinitions) definitions, that, canEnd, Direction.BACKWARDS, incomingMap, outgoingMap); break; } } } /* TODO: support message flow pruning @Override public void visit(final TMessageFlow that) { super.visit(that); all.add(that); } */ @Override public void visit(final TStartEvent that) { super.visit(that); mark((BpmnDefinitions) definitions, that, canStart, Direction.FORWARDS, incomingMap, outgoingMap); } })); // Compose and return the set of orphan elements canStart.retainAll(canEnd); // the intersection of elements with starts and with ends all.removeAll(canStart); // elements lacking either a start or an end return all; }
From source file:com.palantir.atlasdb.cleaner.Scrubber.java
void scrubImmediately(final TransactionManager txManager, final Multimap<String, Cell> tableNameToCell, final long scrubTimestamp, final long commitTimestamp) { if (log.isInfoEnabled()) { log.info("Scrubbing a total of " + tableNameToCell.size() + " cells immediately."); }//from w ww . j a va 2s.co m // Note that if the background scrub thread is also running at the same time, it will try to scrub // the same cells as the current thread (since these cells were queued for scrubbing right before // the hard delete transaction committed; while this is unfortunate (because it means we will be // doing more work than necessary), the behavior is still correct long nextImmutableTimestamp; while ((nextImmutableTimestamp = immutableTimestampSupplier.get()) < commitTimestamp) { try { if (log.isInfoEnabled()) { log.info(String.format( "Sleeping because immutable timestamp %d has not advanced to at least commit timestamp %d", nextImmutableTimestamp, commitTimestamp)); } Thread.sleep(AtlasDbConstants.SCRUBBER_RETRY_DELAY_MILLIS); } catch (InterruptedException e) { log.error("Interrupted while waiting for immutableTimestamp to advance past commitTimestamp", e); } } List<Future<Void>> scrubFutures = Lists.newArrayList(); for (List<Entry<String, Cell>> batch : Iterables.partition(tableNameToCell.entries(), batchSizeSupplier.get())) { final Multimap<String, Cell> batchMultimap = HashMultimap.create(); for (Entry<String, Cell> e : batch) { batchMultimap.put(e.getKey(), e.getValue()); } final Callable<Void> c = new Callable<Void>() { @Override public Void call() throws Exception { if (log.isInfoEnabled()) { log.info("Scrubbing " + batchMultimap.size() + " cells immediately."); } // Here we don't need to check scrub timestamps because we guarantee that scrubImmediately is called // AFTER the transaction commits scrubCells(txManager, batchMultimap, scrubTimestamp, TransactionType.AGGRESSIVE_HARD_DELETE); Multimap<Cell, Long> cellToScrubTimestamp = HashMultimap.create(); cellToScrubTimestamp = Multimaps.invertFrom( Multimaps.index(batchMultimap.values(), Functions.constant(scrubTimestamp)), cellToScrubTimestamp); scrubberStore.markCellsAsScrubbed(cellToScrubTimestamp, batchSizeSupplier.get()); if (log.isInfoEnabled()) { log.info("Completed scrub immediately."); } return null; } }; if (!inScrubThread.get()) { scrubFutures.add(exec.submit(new Callable<Void>() { @Override public Void call() throws Exception { inScrubThread.set(true); c.call(); return null; } })); } else { try { c.call(); } catch (Exception e) { throw Throwables.throwUncheckedException(e); } } } for (Future<Void> future : scrubFutures) { try { future.get(); } catch (InterruptedException e) { throw Throwables.throwUncheckedException(e); } catch (ExecutionException e) { throw Throwables.rewrapAndThrowUncheckedException(e); } } }
From source file:com.palantir.atlasdb.keyvalue.cassandra.CQLKeyValueService.java
@Override public Map<Cell, Value> get(String tableName, Map<Cell, Long> timestampByCell) { try {/*from ww w . j a v a2s . co m*/ long firstTs = timestampByCell.values().iterator().next(); if (Iterables.all(timestampByCell.values(), Predicates.equalTo(firstTs))) { StartTsResultsCollector collector = new StartTsResultsCollector(firstTs); loadWithTs(tableName, timestampByCell.keySet(), firstTs, false, collector, readConsistency); return collector.collectedResults; } SetMultimap<Long, Cell> cellsByTs = HashMultimap.create(); Multimaps.invertFrom(Multimaps.forMap(timestampByCell), cellsByTs); Builder<Cell, Value> builder = ImmutableMap.builder(); for (long ts : cellsByTs.keySet()) { StartTsResultsCollector collector = new StartTsResultsCollector(ts); loadWithTs(tableName, cellsByTs.get(ts), ts, false, collector, readConsistency); builder.putAll(collector.collectedResults); } return builder.build(); } catch (Throwable t) { throw Throwables.throwUncheckedException(t); } }
From source file:com.palantir.atlasdb.cleaner.Scrubber.java
/** * @return number of cells read from _scrub table *///from w w w . j a v a 2s. c om private int scrubSomeCells(SortedMap<Long, Multimap<String, Cell>> scrubTimestampToTableNameToCell, final TransactionManager txManager, long maxScrubTimestamp) { // Don't call expensive toString() if trace logging is off if (log.isTraceEnabled()) { log.trace("Attempting to scrub cells: " + scrubTimestampToTableNameToCell); } if (log.isInfoEnabled()) { int numCells = 0; Set<String> tables = Sets.newHashSet(); for (Multimap<String, Cell> v : scrubTimestampToTableNameToCell.values()) { tables.addAll(v.keySet()); numCells += v.size(); } log.info("Attempting to scrub " + numCells + " cells from tables " + tables); } if (scrubTimestampToTableNameToCell.size() == 0) { return 0; // No cells left to scrub } Multimap<Long, Cell> toRemoveFromScrubQueue = HashMultimap.create(); int numCellsReadFromScrubTable = 0; List<Future<Void>> scrubFutures = Lists.newArrayList(); for (Map.Entry<Long, Multimap<String, Cell>> entry : scrubTimestampToTableNameToCell.entrySet()) { final long scrubTimestamp = entry.getKey(); final Multimap<String, Cell> tableNameToCell = entry.getValue(); numCellsReadFromScrubTable += tableNameToCell.size(); long commitTimestamp = getCommitTimestampRollBackIfNecessary(scrubTimestamp, tableNameToCell); if (commitTimestamp >= maxScrubTimestamp) { // We cannot scrub this yet because not all transactions can read this value. continue; } else if (commitTimestamp != TransactionConstants.FAILED_COMMIT_TS) { // This is CRITICAL; don't scrub if the hard delete transaction didn't actually finish // (we still remove it from the _scrub table with the call to markCellsAsScrubbed though), // or else we could cause permanent data loss if the hard delete transaction failed after // queuing cells to scrub but before successfully committing for (final List<Entry<String, Cell>> batch : Iterables.partition(tableNameToCell.entries(), batchSizeSupplier.get())) { final Multimap<String, Cell> batchMultimap = HashMultimap.create(); for (Entry<String, Cell> e : batch) { batchMultimap.put(e.getKey(), e.getValue()); } scrubFutures.add(exec.submit(new Callable<Void>() { @Override public Void call() throws Exception { scrubCells(txManager, batchMultimap, scrubTimestamp, aggressiveScrub ? TransactionType.AGGRESSIVE_HARD_DELETE : TransactionType.HARD_DELETE); return null; } })); } } toRemoveFromScrubQueue.putAll(scrubTimestamp, tableNameToCell.values()); } for (Future<Void> future : scrubFutures) { Futures.getUnchecked(future); } Multimap<Cell, Long> cellToScrubTimestamp = HashMultimap.create(); scrubberStore.markCellsAsScrubbed(Multimaps.invertFrom(toRemoveFromScrubQueue, cellToScrubTimestamp), batchSizeSupplier.get()); if (log.isTraceEnabled()) { log.trace("Finished scrubbing cells: " + scrubTimestampToTableNameToCell); } if (log.isInfoEnabled()) { Set<String> tables = Sets.newHashSet(); for (Multimap<String, Cell> v : scrubTimestampToTableNameToCell.values()) { tables.addAll(v.keySet()); } long minTimestamp = Collections.min(scrubTimestampToTableNameToCell.keySet()); long maxTimestamp = Collections.max(scrubTimestampToTableNameToCell.keySet()); log.info("Finished scrubbing " + numCellsReadFromScrubTable + " cells at " + scrubTimestampToTableNameToCell.size() + " timestamps (" + minTimestamp + "..." + maxTimestamp + ") from tables " + tables); } return numCellsReadFromScrubTable; }
From source file:org.parceler.internal.ParcelableAnalysis.java
private HashMultimap<String, ASTReference<ASTMethod>> findValueMethods(ASTType astType, Predicate<ASTMethod> filter, final Function<ASTMethod, String> nameTransformer, final Set<MethodSignature> definedMethods, final boolean declaredProperty) { return Multimaps.invertFrom( Multimaps.forMap(FluentIterable.from(astType.getMethods()).filter(new Predicate<ASTMethod>() { @Override//w ww.ja v a2 s . c o m public boolean apply(ASTMethod astMethod) { return !astMethod.isStatic() && !astMethod.isAnnotated(Transient.class) && !definedMethods.contains(new MethodSignature(astMethod)) && (declaredProperty == astMethod.isAnnotated(ParcelProperty.class)); } }).filter(filter).transform(new Function<ASTMethod, ASTReference<ASTMethod>>() { @Override public ASTReference<ASTMethod> apply(ASTMethod astMethod) { return new ASTReference<ASTMethod>(astMethod, getConverter(astMethod)); } }).toMap(new Function<ASTReference<ASTMethod>, String>() { @Override public String apply(ASTReference<ASTMethod> astMethodReference) { return nameTransformer.apply(astMethodReference.getReference()); } })), HashMultimap.<String, ASTReference<ASTMethod>>create()); }
From source file:com.palantir.atlasdb.keyvalue.cassandra.CassandraKeyValueService.java
@Override public Map<Cell, Value> get(String tableName, Map<Cell, Long> timestampByCell) { if (timestampByCell.isEmpty()) { log.info("Attempted get on '{}' table with empty cells", tableName); return ImmutableMap.of(); }/* w w w . j a va 2s . c om*/ try { Long firstTs = timestampByCell.values().iterator().next(); if (Iterables.all(timestampByCell.values(), Predicates.equalTo(firstTs))) { StartTsResultsCollector collector = new StartTsResultsCollector(firstTs); loadWithTs(tableName, timestampByCell.keySet(), firstTs, false, collector, readConsistency); return collector.collectedResults; } SetMultimap<Long, Cell> cellsByTs = Multimaps.invertFrom(Multimaps.forMap(timestampByCell), HashMultimap.<Long, Cell>create()); Builder<Cell, Value> builder = ImmutableMap.builder(); for (long ts : cellsByTs.keySet()) { StartTsResultsCollector collector = new StartTsResultsCollector(ts); loadWithTs(tableName, cellsByTs.get(ts), ts, false, collector, readConsistency); builder.putAll(collector.collectedResults); } return builder.build(); } catch (Exception e) { throw Throwables.throwUncheckedException(e); } }
From source file:org.apache.helix.model.IdealState.java
/** * Update the ideal state from a ResourceAssignment computed during a rebalance * @param assignment the new resource assignment * @param stateModelDef state model of the resource *///from ww w . j av a 2 s . c om public void updateFromAssignment(ResourceAssignment assignment, StateModelDefinition stateModelDef) { // clear all preference lists and maps _record.getMapFields().clear(); _record.getListFields().clear(); // assign a partition at a time for (PartitionId partition : assignment.getMappedPartitionIds()) { List<ParticipantId> preferenceList = new ArrayList<ParticipantId>(); Map<ParticipantId, State> participantStateMap = new HashMap<ParticipantId, State>(); // invert the map to get in state order Map<ParticipantId, State> replicaMap = assignment.getReplicaMap(partition); ListMultimap<State, ParticipantId> inverseMap = ArrayListMultimap.create(); Multimaps.invertFrom(Multimaps.forMap(replicaMap), inverseMap); // update the ideal state in order of state priorities for (State state : stateModelDef.getTypedStatesPriorityList()) { if (!state.equals(State.from(HelixDefinedState.DROPPED)) && !state.equals(State.from(HelixDefinedState.ERROR))) { List<ParticipantId> stateParticipants = inverseMap.get(state); for (ParticipantId participant : stateParticipants) { preferenceList.add(participant); participantStateMap.put(participant, state); } } } setPreferenceList(partition, preferenceList); setParticipantStateMap(partition, participantStateMap); } }
From source file:org.parceler.internal.ParcelableAnalysis.java
private HashMultimap<String, ASTReference<ASTField>> findFields(ASTType astType, final boolean declaredProperty) { return Multimaps.invertFrom( Multimaps.forMap(FluentIterable.from(astType.getFields()).filter(new Predicate<ASTField>() { @Override//from w w w . ja v a 2 s . c o m public boolean apply(ASTField astField) { return !astField.isStatic() && !astField.isAnnotated(Transient.class) && !astField.isTransient() && (declaredProperty == astField.isAnnotated(ParcelProperty.class)); } }).transform(new Function<ASTField, ASTReference<ASTField>>() { @Override public ASTReference<ASTField> apply(ASTField astField) { return new ASTReference<ASTField>(astField, getConverter(astField)); } }).toMap(new Function<ASTReference<ASTField>, String>() { @Override public String apply(ASTReference<ASTField> astFieldReference) { ASTField astField = astFieldReference.getReference(); if (astField.isAnnotated(ParcelProperty.class)) { return astField.getAnnotation(ParcelProperty.class).value(); } return astField.getName(); } })), HashMultimap.<String, ASTReference<ASTField>>create()); }
From source file:org.apache.aurora.scheduler.thrift.SchedulerThriftInterface.java
private static Set<InstanceTaskConfig> buildInitialState(Map<Integer, ITaskConfig> tasks) { // Translate tasks into instance IDs. Multimap<ITaskConfig, Integer> instancesByConfig = HashMultimap.create(); Multimaps.invertFrom(Multimaps.forMap(tasks), instancesByConfig); // Reduce instance IDs into contiguous ranges. Map<ITaskConfig, Set<Range<Integer>>> rangesByConfig = Maps.transformValues(instancesByConfig.asMap(), Numbers::toRanges);//from w ww . j ava2 s. c o m ImmutableSet.Builder<InstanceTaskConfig> builder = ImmutableSet.builder(); for (Map.Entry<ITaskConfig, Set<Range<Integer>>> entry : rangesByConfig.entrySet()) { builder.add(new InstanceTaskConfig().setTask(entry.getKey().newBuilder()) .setInstances(IRange.toBuildersSet(convertRanges(entry.getValue())))); } return builder.build(); }