use of com.google.common.collect.SetMultimap in project molgenis by molgenis.
the class EntityManagerImpl method resolveReferences.
private List<Entity> resolveReferences(List<Attribute> resolvableAttrs, List<Entity> entities, Fetch fetch) {
// entity name --> entity ids
SetMultimap<String, Object> lazyRefEntityIdsMap = HashMultimap.create(resolvableAttrs.size(), 16);
// entity name --> attributes referring to this entity
SetMultimap<String, Attribute> refEntityAttrsMap = HashMultimap.create(resolvableAttrs.size(), 2);
// fill maps
for (Attribute attr : resolvableAttrs) {
String refEntityName = attr.getRefEntity().getId();
if (isSingleReferenceType(attr)) {
for (Entity entity : entities) {
Entity lazyRefEntity = entity.getEntity(attr.getName());
if (lazyRefEntity != null) {
lazyRefEntityIdsMap.put(refEntityName, lazyRefEntity.getIdValue());
}
}
} else if (isMultipleReferenceType(attr)) {
for (Entity entity : entities) {
Iterable<Entity> lazyRefEntities = entity.getEntities(attr.getName());
for (Entity lazyRefEntity : lazyRefEntities) {
lazyRefEntityIdsMap.put(refEntityName, lazyRefEntity.getIdValue());
}
}
}
refEntityAttrsMap.put(refEntityName, attr);
}
// batch retrieve referred entities and replace entity references with actual entities
for (Entry<String, Collection<Object>> entry : lazyRefEntityIdsMap.asMap().entrySet()) {
String refEntityName = entry.getKey();
// create a fetch for the referenced entity which is a union of the fetches defined by attributes
// referencing this entity
Set<Attribute> attrs = refEntityAttrsMap.get(refEntityName);
Fetch subFetch = createSubFetch(fetch, attrs);
// retrieve referenced entities
Stream<Entity> refEntities = dataService.findAll(refEntityName, entry.getValue().stream(), subFetch);
Map<Object, Entity> refEntitiesIdMap = refEntities.collect(Collectors.toMap(Entity::getIdValue, Function.identity()));
for (Attribute attr : attrs) {
if (isSingleReferenceType(attr)) {
String attrName = attr.getName();
for (Entity entity : entities) {
Entity lazyRefEntity = entity.getEntity(attrName);
if (lazyRefEntity != null) {
// replace lazy entity with real entity
Object refEntityId = lazyRefEntity.getIdValue();
Entity refEntity = refEntitiesIdMap.get(refEntityId);
entity.set(attrName, refEntity);
}
}
} else if (isMultipleReferenceType(attr)) {
String attrName = attr.getName();
for (Entity entity : entities) {
// replace lazy entities with real entities
Iterable<Entity> lazyRefEntities = entity.getEntities(attrName);
List<Entity> mrefEntities = stream(lazyRefEntities.spliterator(), true).map(lazyRefEntity -> {
// replace lazy entity with real entity
Object refEntityId = lazyRefEntity.getIdValue();
return refEntitiesIdMap.get(refEntityId);
}).filter(Objects::nonNull).collect(Collectors.toList());
entity.set(attrName, mrefEntities);
}
}
}
}
return entities;
}
use of com.google.common.collect.SetMultimap in project batfish by batfish.
the class CommonUtil method initPrivateIpsByPublicIp.
@VisibleForTesting
static SetMultimap<Ip, IpWildcardSetIpSpace> initPrivateIpsByPublicIp(Map<String, Configuration> configurations) {
/*
* Very hacky mapping from public IP to set of spaces of possible natted private IPs.
* Does not currently support source-nat acl.
*
* The current implementation just considers every IP in every prefix on a non-masquerading
* interface (except the local address in each such prefix) to be a possible private IP
* match for every public IP referred to by every source-nat pool on a masquerading interface.
*/
ImmutableSetMultimap.Builder<Ip, IpWildcardSetIpSpace> builder = ImmutableSetMultimap.builder();
for (Configuration c : configurations.values()) {
Collection<Interface> interfaces = c.getInterfaces().values();
Set<InterfaceAddress> nonNattedInterfaceAddresses = interfaces.stream().filter(i -> i.getSourceNats().isEmpty()).flatMap(i -> i.getAllAddresses().stream()).collect(ImmutableSet.toImmutableSet());
Set<IpWildcard> blacklist = nonNattedInterfaceAddresses.stream().map(address -> new IpWildcard(address.getIp(), Ip.ZERO)).collect(ImmutableSet.toImmutableSet());
Set<IpWildcard> whitelist = nonNattedInterfaceAddresses.stream().map(address -> new IpWildcard(address.getPrefix())).collect(ImmutableSet.toImmutableSet());
IpWildcardSetIpSpace ipSpace = IpWildcardSetIpSpace.builder().including(whitelist).excluding(blacklist).build();
interfaces.stream().flatMap(i -> i.getSourceNats().stream()).forEach(sourceNat -> {
for (long ipAsLong = sourceNat.getPoolIpFirst().asLong(); ipAsLong <= sourceNat.getPoolIpLast().asLong(); ipAsLong++) {
Ip currentPoolIp = new Ip(ipAsLong);
builder.put(currentPoolIp, ipSpace);
}
});
}
return builder.build();
}
use of com.google.common.collect.SetMultimap in project presto by prestodb.
the class TestPrestoSparkSourceDistributionSplitAssigner method testAssignSplitsToPartitionWithRandomSplitSizes.
@Test
public void testAssignSplitsToPartitionWithRandomSplitSizes() {
DataSize maxSplitDataSizePerPartition = new DataSize(2048, BYTE);
int initialPartitionCount = 3;
int minSparkInputPartitionCountForAutoTune = 2;
int maxSparkInputPartitionCountForAutoTune = 5;
int maxSplitSizeInBytes = 2048;
AtomicInteger sequenceId = new AtomicInteger();
for (int i = 0; i < 3; ++i) {
List<Long> splitSizes = new ArrayList<>(1000);
for (int j = 0; j < 1000; j++) {
splitSizes.add(ThreadLocalRandom.current().nextLong((long) (maxSplitSizeInBytes * 1.2)));
}
PrestoSparkSplitAssigner assigner = new PrestoSparkSourceDistributionSplitAssigner(new PlanNodeId("test"), createSplitSource(splitSizes), 333, maxSplitDataSizePerPartition.toBytes(), initialPartitionCount, true, minSparkInputPartitionCountForAutoTune, maxSparkInputPartitionCountForAutoTune);
HashMultimap<Integer, ScheduledSplit> actualAssignment = HashMultimap.create();
while (true) {
Optional<SetMultimap<Integer, ScheduledSplit>> assignment = assigner.getNextBatch();
if (!assignment.isPresent()) {
break;
}
actualAssignment.putAll(assignment.get());
}
long expectedSizeInBytes = splitSizes.stream().mapToLong(Long::longValue).sum();
long actualTotalSizeInBytes = actualAssignment.values().stream().mapToLong(split -> split.getSplit().getConnectorSplit().getSplitSizeInBytes().orElseThrow(() -> new IllegalArgumentException("split size is expected to be present"))).sum();
// check if all splits got assigned
assertEquals(expectedSizeInBytes, actualTotalSizeInBytes);
}
}
use of com.google.common.collect.SetMultimap in project gerrit by GerritCodeReview.
the class LabelsJson method labelsForSubmittedChange.
private Map<String, LabelWithStatus> labelsForSubmittedChange(AccountLoader accountLoader, ChangeData cd, LabelTypes labelTypes, boolean standard, boolean detailed) throws PermissionBackendException {
Set<Account.Id> allUsers = new HashSet<>();
if (detailed) {
// the latest patch set (in the next loop).
for (PatchSetApproval psa : cd.approvals().values()) {
allUsers.add(psa.accountId());
}
}
Set<String> labelNames = new HashSet<>();
SetMultimap<Account.Id, PatchSetApproval> current = MultimapBuilder.hashKeys().hashSetValues().build();
for (PatchSetApproval a : cd.currentApprovals()) {
allUsers.add(a.accountId());
Optional<LabelType> type = labelTypes.byLabel(a.labelId());
if (type.isPresent()) {
labelNames.add(type.get().getName());
// Not worth the effort to distinguish between votable/non-votable for 0
// values on closed changes, since they can't vote anyway.
current.put(a.accountId(), a);
}
}
// Since voting on merged changes is allowed all labels which apply to
// the change must be returned. All applying labels can be retrieved from
// the submit records, which is what initLabels does.
// It's not possible to only compute the labels based on the approvals
// since merged changes may not have approvals for all labels (e.g. if not
// all labels are required for submit or if the change was auto-closed due
// to direct push or if new labels were defined after the change was
// merged).
Map<String, LabelWithStatus> labels;
labels = initLabels(accountLoader, cd, labelTypes, standard);
// it wouldn't be included in the submit records.
for (String name : labelNames) {
if (!labels.containsKey(name)) {
labels.put(name, LabelWithStatus.create(new LabelInfo(), null));
}
}
labels.entrySet().stream().filter(e -> labelTypes.byLabel(e.getKey()).isPresent()).forEach(e -> setLabelValues(labelTypes.byLabel(e.getKey()).get(), e.getValue()));
for (Account.Id accountId : allUsers) {
Map<String, ApprovalInfo> byLabel = Maps.newHashMapWithExpectedSize(labels.size());
Map<String, VotingRangeInfo> pvr = Collections.emptyMap();
if (detailed) {
pvr = getPermittedVotingRanges(permittedLabels(accountId, cd));
}
for (Map.Entry<String, LabelWithStatus> entry : labels.entrySet()) {
ApprovalInfo ai = approvalInfo(accountLoader, accountId, 0, null, null, null);
byLabel.put(entry.getKey(), ai);
addApproval(entry.getValue().label(), ai);
}
for (PatchSetApproval psa : current.get(accountId)) {
Optional<LabelType> type = labelTypes.byLabel(psa.labelId());
if (!type.isPresent()) {
continue;
}
short val = psa.value();
ApprovalInfo info = byLabel.get(type.get().getName());
if (info != null) {
info.value = Integer.valueOf(val);
info.permittedVotingRange = pvr.getOrDefault(type.get().getName(), null);
info.setDate(psa.granted());
info.tag = psa.tag().orElse(null);
if (psa.postSubmit()) {
info.postSubmit = true;
}
}
if (!standard) {
continue;
}
setLabelScores(accountLoader, type.get(), labels.get(type.get().getName()), val, accountId);
}
}
return labels;
}
use of com.google.common.collect.SetMultimap in project gerrit by GerritCodeReview.
the class StalenessChecker method check.
/**
* Returns a {@link StalenessCheckResult} with structured information about staleness of the
* provided {@link com.google.gerrit.entities.Project.NameKey}.
*/
public StalenessCheckResult check(Project.NameKey project) {
ProjectData projectData = projectCache.get(project).orElseThrow(illegalState(project)).toProjectData();
ProjectIndex i = indexes.getSearchIndex();
if (i == null) {
return StalenessCheckResult.notStale();
}
Optional<FieldBundle> result = i.getRaw(project, QueryOptions.create(indexConfig, 0, 1, FIELDS));
if (!result.isPresent()) {
return StalenessCheckResult.stale("Document %s missing from index", project);
}
SetMultimap<Project.NameKey, RefState> indexedRefStates = RefState.parseStates(result.get().getValue(ProjectField.REF_STATE));
SetMultimap<Project.NameKey, RefState> currentRefStates = MultimapBuilder.hashKeys().hashSetValues().build();
projectData.tree().stream().filter(p -> p.getProject().getConfigRefState() != null).forEach(p -> currentRefStates.put(p.getProject().getNameKey(), RefState.create(RefNames.REFS_CONFIG, p.getProject().getConfigRefState())));
if (currentRefStates.equals(indexedRefStates)) {
return StalenessCheckResult.notStale();
}
return StalenessCheckResult.stale("Document has unexpected ref states (%s != %s)", currentRefStates, indexedRefStates);
}
Aggregations