Skip to content

Commit

Permalink
Consider the max occurrence of a requirement
Browse files Browse the repository at this point in the history
Currently all matching units are considered even if there are only a
lower number to use.

This adds a filtering after the initial collection phase to limit the
dependencies to the max of the requirement and also adds the
infrastructure to support filtering based on the profile properties.

relates to #3197

(cherry picked from commit a350281)
  • Loading branch information
laeubi committed Dec 11, 2023
1 parent 1b4d2e7 commit e3e3aac
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
*******************************************************************************/
package org.eclipse.tycho.p2maven;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement;
Expand All @@ -27,7 +29,6 @@
import org.eclipse.equinox.internal.p2.metadata.InstallableUnit;
import org.eclipse.equinox.p2.metadata.IInstallableUnit;
import org.eclipse.equinox.p2.metadata.IRequirement;
import org.eclipse.equinox.p2.query.CollectionResult;
import org.eclipse.equinox.p2.query.IQueryResult;
import org.eclipse.equinox.p2.query.IQueryable;
import org.eclipse.equinox.p2.query.QueryUtil;
Expand Down Expand Up @@ -76,32 +77,35 @@ public IQueryResult<IInstallableUnit> computeDependencies(Collection<IInstallabl
* @param rootIus the root {@link InstallableUnit}s to take into account
* @param avaiableIUs the {@link IQueryable} of all units that could be used for
* fulfilling a requirement
* @return the result of the slicing
* @param contextIUs context IUs that represent the the profile properties to
* consider during resolution, can be empty in which case a
* filter is always considered a match
* @return the result of the slicing, be aware that no maximum/minimum
* constraints or filters are applied as part of this computation
* @throws CoreException if there is any error
*/
public IQueryResult<IInstallableUnit> computeDirectDependencies(Collection<IInstallableUnit> rootIus,
public Map<IRequirement, Collection<IInstallableUnit>> computeDirectDependencies(
Collection<IInstallableUnit> rootIus,
IQueryable<IInstallableUnit> avaiableIUs) throws CoreException {
Collection<IInstallableUnit> result = new LinkedHashSet<>();
List<IRequirement> collect = rootIus.stream().flatMap(iu -> iu.getRequirements().stream()).filter(req -> {
for (IInstallableUnit unit : rootIus) {
if (unit.satisfies(req)) {
// self full filled requirement
return false;
}
}
return true;
}).toList();
List<IRequirement> collect = rootIus.stream().flatMap(iu -> iu.getRequirements().stream())
.filter(req -> {
for (IInstallableUnit unit : rootIus) {
if (unit.satisfies(req)) {
// self full filled requirement
return false;
}
}
return true;
}).toList();
Map<IRequirement, Collection<IInstallableUnit>> result = new LinkedHashMap<>(collect.size());
for (IInstallableUnit iu : avaiableIUs.query(QueryUtil.ALL_UNITS, new NullProgressMonitor()).toSet()) {
for (IRequirement requirement : collect) {
if (iu.satisfies(requirement)) {
result.add(iu);
// TODO remove the requirement from the set so we only collect exactly one
// provider for a requirement?
break;
result.computeIfAbsent(requirement, nil -> new ArrayList<>()).add(iu);
}
}
}
return new CollectionResult<>(result);
return result;
}

private final class TychoSlicer extends PermissiveSlicer {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.eclipse.equinox.p2.metadata.IInstallableUnit;
import org.eclipse.equinox.p2.metadata.IProvidedCapability;
import org.eclipse.equinox.p2.metadata.IRequirement;
import org.eclipse.equinox.p2.metadata.expression.IMatchExpression;
import org.eclipse.equinox.p2.publisher.eclipse.BundlesAction;
import org.eclipse.equinox.p2.query.CollectionResult;
import org.eclipse.equinox.p2.query.IQueryable;
Expand All @@ -57,7 +58,7 @@
public class MavenProjectDependencyProcessor {

private static final ProjectDependencies EMPTY_DEPENDENCIES = new ProjectDependencies(Collections.emptyList(),
Collections.emptyList());
Collections.emptyList(), Map.of(), Set.of());

private static final boolean DUMP_DATA = Boolean.getBoolean("tycho.p2.dump")
|| Boolean.getBoolean("tycho.p2.dump.dependencies");
Expand All @@ -72,13 +73,19 @@ public class MavenProjectDependencyProcessor {
* Computes the {@link ProjectDependencyClosure} of the given collection of
* projects.
*
* @param projects the projects to include in the closure
* @param session the maven session for this request
* @param projects the projects to include in the closure
* @param session the maven session for this request
* @param profilePropertiesSupplier supplier of context IUs for a project that
* represent the the profile properties to
* consider during resolution, can be empty in
* which case a filter is always considered a
* match
* @return the computed {@link ProjectDependencyClosure}
* @throws CoreException if computation failed
*/
public ProjectDependencyClosure computeProjectDependencyClosure(Collection<MavenProject> projects,
MavenSession session) throws CoreException {
MavenSession session)
throws CoreException {
Objects.requireNonNull(session);
Map<MavenProject, Collection<IInstallableUnit>> projectIUMap = generator.getInstallableUnits(projects, session);
Collection<IInstallableUnit> availableIUs = projectIUMap.values().stream().flatMap(Collection::stream)
Expand Down Expand Up @@ -144,7 +151,8 @@ private Map<MavenProject, ProjectDependencies> computeProjectDependencies(Collec
Map<MavenProject, ProjectDependencies> result = new ConcurrentHashMap<>();
projects.parallelStream().unordered().takeWhile(nil -> errors.isEmpty()).forEach(project -> {
try {
ProjectDependencies projectDependencies = computeProjectDependencies(projectIUMap.get(project),
ProjectDependencies projectDependencies = computeProjectDependencies(
Set.copyOf(projectIUMap.get(project)),
avaiableIUs);
result.put(project, projectDependencies);
if (DUMP_DATA) {
Expand Down Expand Up @@ -181,17 +189,28 @@ private Map<MavenProject, ProjectDependencies> computeProjectDependencies(Collec
* @return the collection of dependent {@link InstallableUnit}s
* @throws CoreException if computation failed
*/
private ProjectDependencies computeProjectDependencies(Collection<IInstallableUnit> projectUnits,
IQueryable<IInstallableUnit> avaiableIUs) throws CoreException {
private ProjectDependencies computeProjectDependencies(Set<IInstallableUnit> projectUnits,
IQueryable<IInstallableUnit> avaiableIUs)
throws CoreException {
if (projectUnits.isEmpty()) {
return EMPTY_DEPENDENCIES;
}
Set<IInstallableUnit> resolved = new LinkedHashSet<>(
slicer.computeDirectDependencies(projectUnits, avaiableIUs).toSet());
resolved.removeAll(projectUnits);
Map<IRequirement, Collection<IInstallableUnit>> dependencies = slicer.computeDirectDependencies(projectUnits,
avaiableIUs);
// first collect the maximum desired number
Set<IInstallableUnit> resolved = new LinkedHashSet<>();
for (Entry<IRequirement, Collection<IInstallableUnit>> entry : dependencies.entrySet()) {
IRequirement requirement = entry.getKey();
Collection<IInstallableUnit> units = entry.getValue();
List<IInstallableUnit> limit = units.stream().filter(unit -> !projectUnits.contains(unit))
.limit(requirement.getMax()).toList();
resolved.addAll(limit);
}
// now we need to filter all fragments that we are a host!
// for example SWT creates an explicit requirement to its fragments and we don't
// want them included directly
// TODO reevaluate, this is where filters come into place we probabbly no longer
// need this part!
Set<IInstallableUnit> projectFragments = new HashSet<>();
for (Iterator<IInstallableUnit> iterator = resolved.iterator(); iterator.hasNext();) {
IInstallableUnit unit = iterator.next();
Expand All @@ -200,7 +219,7 @@ private ProjectDependencies computeProjectDependencies(Collection<IInstallableUn
iterator.remove();
}
}
return new ProjectDependencies(resolved, projectFragments);
return new ProjectDependencies(resolved, projectFragments, dependencies, projectUnits);
}

private static boolean hasAnyHost(IInstallableUnit unit, Iterable<IInstallableUnit> collection) {
Expand Down Expand Up @@ -238,14 +257,27 @@ private static Stream<IRequirement> getFragmentHostRequirement(IInstallableUnit
}).filter(Objects::nonNull);
}

private static boolean isMatch(IRequirement requirement, Collection<IInstallableUnit> contextIUs) {
IMatchExpression<IInstallableUnit> filter = requirement.getFilter();
if (filter == null || contextIUs.isEmpty()) {
return true;
}
return contextIUs.stream().anyMatch(contextIU -> filter.isMatch(contextIU));
}

public static final class ProjectDependencies {

private final Collection<IInstallableUnit> dependencies;
private final Collection<IInstallableUnit> fragments;
private Map<IRequirement, Collection<IInstallableUnit>> requirementsMap;
private Set<IInstallableUnit> projectUnits;

private ProjectDependencies(Collection<IInstallableUnit> dependencies, Collection<IInstallableUnit> fragments) {
ProjectDependencies(Collection<IInstallableUnit> dependencies, Collection<IInstallableUnit> fragments,
Map<IRequirement, Collection<IInstallableUnit>> requirementsMap, Set<IInstallableUnit> projectUnits) {
this.dependencies = dependencies;
this.fragments = fragments;
this.requirementsMap = requirementsMap;
this.projectUnits = projectUnits;
}

public Collection<IInstallableUnit> getDependencies() {
Expand All @@ -256,6 +288,14 @@ public Collection<IInstallableUnit> getFragments() {
return fragments;
}

public Collection<IInstallableUnit> getDependencies(Collection<IInstallableUnit> contextIUs) {
return requirementsMap.entrySet().stream().filter(entry -> isMatch(entry.getKey(), contextIUs))
.flatMap(entry -> entry.getValue().stream().filter(unit -> !projectUnits.contains(unit))
.limit(entry.getKey().getMax()))
.distinct().toList();
}


}

public static interface ProjectDependencyClosure {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
package org.eclipse.tycho;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -88,7 +87,7 @@ public boolean isWindows() {
* Returns the target environment as map. The keys are "osgi.ws", "osgi.os", and "osgi.arch".
* This format is used by the p2 slicer to filter installable units by environments.
*
* @return a new instance of {@link HashMap} with the target environment set
* @return a new instance of {@link LinkedHashMap} with the target environment set
*/
public Map<String, String> toFilterProperties() {
//for nicer debug output, use an ordered map here
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,16 @@
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement;
import org.codehaus.plexus.logging.Logger;
import org.eclipse.equinox.internal.p2.metadata.InstallableUnit;
import org.eclipse.equinox.p2.metadata.IInstallableUnit;
import org.eclipse.tycho.ArtifactDescriptor;
import org.eclipse.tycho.ArtifactKey;
import org.eclipse.tycho.ClasspathEntry;
import org.eclipse.tycho.DefaultArtifactKey;
import org.eclipse.tycho.ExecutionEnvironmentConfiguration;
import org.eclipse.tycho.ReactorProject;
import org.eclipse.tycho.ResolvedArtifactKey;
import org.eclipse.tycho.TargetEnvironment;
import org.eclipse.tycho.TargetPlatform;
import org.eclipse.tycho.TargetPlatformService;
import org.eclipse.tycho.TychoConstants;
Expand Down Expand Up @@ -143,6 +146,25 @@ public void readExecutionEnvironmentConfiguration(ReactorProject project, Execut
}
}

public List<IInstallableUnit> getContextIUs(MavenProject project) {
TargetPlatformConfiguration configuration = getTargetPlatformConfiguration(project);
return configuration.getEnvironments().stream().map(env -> getProfileProperties(env, configuration))
.map(InstallableUnit::contextIU).toList();
}

public Map<String, String> getProfileProperties(MavenProject project, TargetEnvironment environment) {
TargetPlatformConfiguration configuration = getTargetPlatformConfiguration(project);
return getProfileProperties(environment, configuration);
}

private Map<String, String> getProfileProperties(TargetEnvironment environment,
TargetPlatformConfiguration configuration) {
Map<String, String> properties = environment.toFilterProperties();
properties.put("org.eclipse.update.install.features", "true");
properties.putAll(configuration.getProfileProperties());
return properties;
}

public TargetPlatformConfiguration getTargetPlatformConfiguration(MavenProject project) {
ReactorProject reactorProject = DefaultReactorProject.adapt(project);
return reactorProject.computeContextValue(CTX_TARGET_PLATFORM_CONFIGURATION,
Expand Down

0 comments on commit e3e3aac

Please sign in to comment.