Skip to content

Commit

Permalink
Add IQueryable.contains(T) method and implement overrides where suitable
Browse files Browse the repository at this point in the history
This methods provides a more convenient and in most cases faster way to
test if a queryable (e.g. a repository) contains an element, compared to
querying the queryable/repository for it.
IArtifactRepository already has a contains() method so this effectively
only adds it for IMetadataRepository and generalizes it for all
IQueryable sub-types.

Co-authored-by: Christoph Läubrich <laeubi@laeubi-soft.de>
  • Loading branch information
HannesWell and laeubi committed Sep 11, 2023
1 parent d86047f commit ace64cc
Show file tree
Hide file tree
Showing 27 changed files with 198 additions and 50 deletions.
4 changes: 2 additions & 2 deletions bundles/org.eclipse.equinox.p2.director/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: %pluginName
Bundle-SymbolicName: org.eclipse.equinox.p2.director;singleton:=true
Bundle-Version: 2.6.100.qualifier
Bundle-Version: 2.6.200.qualifier
Bundle-Activator: org.eclipse.equinox.internal.p2.director.DirectorActivator
Bundle-Vendor: %providerName
Bundle-Localization: plugin
Expand All @@ -23,7 +23,7 @@ Export-Package: org.eclipse.equinox.internal.p2.director;
org.eclipse.equinox.p2.planner;version="2.0.0"
Require-Bundle: org.eclipse.equinox.common;bundle-version="[3.3.0,4.0.0)",
org.eclipse.core.jobs;bundle-version="[3.3.0,4.0.0)",
org.eclipse.equinox.p2.metadata;bundle-version="[2.0.0,3.0.0)",
org.eclipse.equinox.p2.metadata;bundle-version="[2.8.0,3.0.0)",
org.sat4j.core;bundle-version="[2.3.5,3.0.0)",
org.sat4j.pb;bundle-version="[2.3.5,3.0.0)"
Bundle-RequiredExecutionEnvironment: JavaSE-17
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ public Iterator<IInstallableUnit> everything() {
return dataSet.iterator();
}

@Override
public boolean contains(IInstallableUnit element) {
return dataSet.contains(element);
}

@Override
public synchronized IIndex<IInstallableUnit> getIndex(String memberName) {
if (InstallableUnit.MEMBER_PROVIDED_CAPABILITIES.equals(memberName)) {
Expand Down
4 changes: 2 additions & 2 deletions bundles/org.eclipse.equinox.p2.engine/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: %pluginName
Bundle-SymbolicName: org.eclipse.equinox.p2.engine;singleton:=true
Bundle-Version: 2.8.100.qualifier
Bundle-Version: 2.9.0.qualifier
Bundle-Activator: org.eclipse.equinox.internal.p2.engine.EngineActivator
Bundle-Vendor: %providerName
Bundle-Localization: plugin
Expand Down Expand Up @@ -50,7 +50,7 @@ Import-Package: javax.xml.parsers,
org.eclipse.equinox.p2.metadata;version="[2.4.0,3.0.0)",
org.eclipse.equinox.p2.metadata.expression;version="[2.0.0,3.0.0)",
org.eclipse.equinox.p2.metadata.index;version="[2.0.0,3.0.0)",
org.eclipse.equinox.p2.query;version="[2.0.0,3.0.0)",
org.eclipse.equinox.p2.query;version="[2.1.0,3.0.0)",
org.eclipse.equinox.p2.repository;version="[2.0.0,3.0.0)",
org.eclipse.equinox.p2.repository.artifact;version="[2.0.0,3.0.0)",
org.eclipse.equinox.p2.repository.artifact.spi;version="[2.0.0,3.0.0)",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,11 @@ public Iterator<IInstallableUnit> everything() {
return ius.iterator();
}

@Override
public boolean contains(IInstallableUnit element) {
return ius.contains(element);
}

@Override
public Object getManagedProperty(Object client, String memberName, Object key) {
if (!(client instanceof IInstallableUnit))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@
import org.eclipse.equinox.p2.core.ProvisionException;
import org.eclipse.equinox.p2.engine.IProfile;
import org.eclipse.equinox.p2.metadata.IInstallableUnit;
import org.eclipse.equinox.p2.query.IQuery;
import org.eclipse.equinox.p2.query.IQueryResult;
import org.eclipse.equinox.p2.query.*;
import org.eclipse.equinox.p2.repository.IRepository;
import org.eclipse.equinox.p2.repository.IRepositoryReference;
import org.eclipse.equinox.p2.repository.metadata.spi.AbstractMetadataRepository;
Expand Down Expand Up @@ -172,6 +171,11 @@ public IQueryResult<IInstallableUnit> query(IQuery<IInstallableUnit> query, IPro
return profile.query(query, monitor);
}

@Override
public boolean contains(IInstallableUnit element) {
return !profile.query(QueryUtil.createIUQuery(element), null).isEmpty();
}

private static IProfile getProfile(IProvisioningAgent agent, URI location) throws ProvisionException {
if (!FILE_SCHEME.equalsIgnoreCase(location.getScheme()))
fail(location, ProvisionException.REPOSITORY_NOT_FOUND);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.equinox.internal.p2.engine;

import java.util.*;
import java.util.stream.Stream;
import org.eclipse.core.runtime.*;
import org.eclipse.equinox.p2.engine.*;
import org.eclipse.equinox.p2.metadata.IInstallableUnit;
Expand Down Expand Up @@ -90,21 +91,28 @@ public QueryablePlan(boolean add) {
this.addition = add;
}

private Stream<IInstallableUnit> installableUnits() {
return operands.stream() //
.filter(InstallableUnitOperand.class::isInstance).map(InstallableUnitOperand.class::cast)
.map(addition ? InstallableUnitOperand::second : InstallableUnitOperand::first);
}

@Override
public IQueryResult<IInstallableUnit> query(IQuery<IInstallableUnit> query, IProgressMonitor monitor) {
if (operands == null || status.getSeverity() == IStatus.ERROR)
if (operands.isEmpty() || status.getSeverity() == IStatus.ERROR) {
return Collector.emptyCollector();
Collection<IInstallableUnit> list = new ArrayList<>();
for (Operand operand : operands) {
if (!(operand instanceof InstallableUnitOperand))
continue;
InstallableUnitOperand op = ((InstallableUnitOperand) operand);
IInstallableUnit iu = addition ? op.second() : op.first();
if (iu != null)
list.add(iu);
}
Stream<IInstallableUnit> list = installableUnits();
return query.perform(list.iterator());
}

@Override
public boolean contains(IInstallableUnit element) {
if (operands.isEmpty() || status.getSeverity() == IStatus.ERROR) {
return false;
}
return installableUnits().anyMatch(iu -> Objects.equals(iu, element));
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
public class ProvisioningContext {
private IProvisioningAgent agent;
private URI[] artifactRepositories; //artifact repositories to consult
private final List<IInstallableUnit> extraIUs = Collections.synchronizedList(new ArrayList<IInstallableUnit>());
private final List<IInstallableUnit> extraIUs = Collections.synchronizedList(new ArrayList<>());
private URI[] metadataRepositories; //metadata repositories to consult
private final Map<String, String> properties = new HashMap<>();
private Map<String, URI> referencedArtifactRepositories = null;
Expand All @@ -64,6 +64,11 @@ class ArtifactRepositoryQueryable implements IQueryable<IArtifactRepository> {
public IQueryResult<IArtifactRepository> query(IQuery<IArtifactRepository> query, IProgressMonitor mon) {
return query.perform(repositories.listIterator());
}

@Override
public boolean contains(IArtifactRepository element) {
return repositories.contains(element);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: %pluginName
Bundle-SymbolicName: org.eclipse.equinox.p2.extensionlocation;singleton:=true
Bundle-Version: 1.5.100.qualifier
Bundle-Version: 1.5.200.qualifier
Bundle-Activator: org.eclipse.equinox.internal.p2.extensionlocation.Activator
Bundle-Vendor: %providerName
Bundle-Localization: plugin
Export-Package: org.eclipse.equinox.internal.p2.extensionlocation;x-friends:="org.eclipse.equinox.p2.reconciler.dropins,org.eclipse.equinox.p2.ui,org.eclipse.equinox.p2.ui.importexport"
Require-Bundle: org.eclipse.equinox.common;bundle-version="[3.5.0,4.0.0)",
org.eclipse.equinox.p2.metadata
org.eclipse.equinox.p2.metadata;bundle-version="[2.8.0,3.0.0)"
Bundle-RequiredExecutionEnvironment: JavaSE-17
Bundle-ActivationPolicy: lazy
Import-Package: org.eclipse.equinox.internal.p2.artifact.repository.simple,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
*
* Contributors:
* IBM Corporation - initial API and implementation
* Code 9 - ongoing development
Expand Down Expand Up @@ -51,7 +51,7 @@ public static URI getLocalRepositoryLocation(URI location) {
}

/*
* Constructor for the class. Return a new extension location repository based on the
* Constructor for the class. Return a new extension location repository based on the
* given location and specified nested repo.
*/
public ExtensionLocationMetadataRepository(IProvisioningAgent agent, URI location, IMetadataRepository repository, IProgressMonitor monitor) throws ProvisionException {
Expand Down Expand Up @@ -112,6 +112,11 @@ public IQueryResult<IInstallableUnit> query(IQuery<IInstallableUnit> query, IPro
return metadataRepository.query(query, monitor);
}

@Override
public boolean contains(IInstallableUnit element) {
return metadataRepository.contains(element);
}

public static void validate(URI location, IProgressMonitor monitor) throws ProvisionException {
File base = getBaseDirectory(location);
if (new File(base, EXTENSION_LOCATION).exists() || location.getPath().endsWith(EXTENSION_LOCATION))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: %pluginName
Bundle-SymbolicName: org.eclipse.equinox.p2.metadata.repository;singleton:=true
Bundle-Version: 1.5.100.qualifier
Bundle-Version: 1.5.200.qualifier
Bundle-Vendor: %providerName
Bundle-Localization: plugin
Export-Package: org.eclipse.equinox.internal.p2.metadata.repository;
Expand Down Expand Up @@ -35,7 +35,7 @@ Import-Package: javax.xml.parsers,
org.eclipse.equinox.p2.metadata;version="[2.4.0,3.0.0)",
org.eclipse.equinox.p2.metadata.expression;version="[2.0.0,3.0.0)",
org.eclipse.equinox.p2.metadata.index;version="[2.0.0,3.0.0)",
org.eclipse.equinox.p2.query;version="[2.0.0,3.0.0)",
org.eclipse.equinox.p2.query;version="[2.1.0,3.0.0)",
org.eclipse.equinox.p2.repository;version="[2.0.0,3.0.0)",
org.eclipse.equinox.p2.repository.metadata;version="[2.0.0,3.0.0)",
org.eclipse.equinox.p2.repository.metadata.spi;version="[2.0.0,3.0.0)",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,16 @@ public IQueryResult<IInstallableUnit> query(IQuery<IInstallableUnit> query, IPro
}
}

@Override
public boolean contains(IInstallableUnit element) {
for (IMetadataRepository repository : loadedRepos) {
if (repository.contains(element)) {
return true;
}
}
return false;
}

//successfully loaded repo will be added to the list repositoriesToBeRemovedOnFailure if the list is not null and the repo wasn't previously loaded
private void addChild(URI childURI, boolean save, IProgressMonitor monitor, boolean propagateException, List<URI> repositoriesToBeRemovedOnFailure) throws ProvisionException {
SubMonitor sub = SubMonitor.convert(monitor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,11 @@ public IQueryResult<IInstallableUnit> query(IQuery<IInstallableUnit> query, IPro
return IndexProvider.query(this, query, monitor);
}

@Override
public boolean contains(IInstallableUnit element) {
return units.contains(element);
}

@Override
public synchronized Iterator<IInstallableUnit> everything() {
snapshotNeeded = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ public synchronized IQueryResult<IInstallableUnit> query(IQuery<IInstallableUnit
return IndexProvider.query(this, query, monitor);
}

@Override
public boolean contains(IInstallableUnit element) {
return units.contains(element);
}

@Override
public synchronized IIndex<IInstallableUnit> getIndex(String memberName) {
if (InstallableUnit.MEMBER_ID.equals(memberName)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,12 @@
</message_arguments>
</filter>
</resource>
<resource path="src/org/eclipse/equinox/p2/query/IQueryable.java" type="org.eclipse.equinox.p2.query.IQueryable">
<filter id="404000815">
<message_arguments>
<message_argument value="org.eclipse.equinox.p2.query.IQueryable"/>
<message_argument value="contains(T)"/>
</message_arguments>
</filter>
</resource>
</component>
4 changes: 2 additions & 2 deletions bundles/org.eclipse.equinox.p2.metadata/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: %pluginName
Bundle-SymbolicName: org.eclipse.equinox.p2.metadata;singleton:=true
Bundle-Version: 2.7.100.qualifier
Bundle-Version: 2.8.0.qualifier

Check warning on line 5 in bundles/org.eclipse.equinox.p2.metadata/META-INF/MANIFEST.MF

View workflow job for this annotation

GitHub Actions / build / Verify Linux

The minor version should be the same for version 2.8.0, since no new APIs have been added since version 2.7.100

Check warning on line 5 in bundles/org.eclipse.equinox.p2.metadata/META-INF/MANIFEST.MF

View workflow job for this annotation

GitHub Actions / build / Verify MacOS

The minor version should be the same for version 2.8.0, since no new APIs have been added since version 2.7.100
Bundle-Vendor: %providerName
Bundle-Localization: plugin
Export-Package: org.eclipse.equinox.internal.p2.metadata;
Expand Down Expand Up @@ -78,7 +78,7 @@ Export-Package: org.eclipse.equinox.internal.p2.metadata;
org.eclipse.equinox.p2.metadata;version="2.4.0",
org.eclipse.equinox.p2.metadata.expression;version="2.0.0",
org.eclipse.equinox.p2.metadata.index;version="2.0.0",
org.eclipse.equinox.p2.query;version="2.0.0"
org.eclipse.equinox.p2.query;version="2.1.0"
Require-Bundle: org.eclipse.equinox.common,
org.eclipse.equinox.p2.core;bundle-version="[2.0.0,3.0.0)"
Import-Package: org.eclipse.osgi.service.localization;version="1.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.equinox.internal.p2.metadata.InstallableUnit;
import org.eclipse.equinox.internal.p2.metadata.expression.CompoundIterator;
Expand All @@ -31,8 +32,9 @@
import org.eclipse.equinox.p2.metadata.index.IIndexProvider;

/**
* A queryable that holds a number of other IQueryables and provides
* a mechanism for querying the entire set.
* A queryable that holds a number of other IQueryables and provides a mechanism
* for querying the entire set.
*
* @since 2.0
*/
public final class CompoundQueryable<T> extends IndexProvider<T> {
Expand Down Expand Up @@ -75,7 +77,7 @@ public CompoundQueryable(IQueryable<T>[] queryables) {
*/
@SuppressWarnings("unchecked")
CompoundQueryable(IQueryable<T> query1, IQueryable<T> query2) {
this(new IQueryable[] {query1, query2});
this(new IQueryable[] { query1, query2 });
}

@Override
Expand All @@ -98,7 +100,7 @@ public IIndex<T> getIndex(String memberName) {
// Nobody had an index for this member
return null;

ArrayList<IIndex<T>> indexes = new ArrayList<>(queryables.length);
List<IIndex<T>> indexes = new ArrayList<>(queryables.length);
for (IQueryable<T> queryable : queryables) {
if (queryable instanceof IIndexProvider<?>) {
@SuppressWarnings("unchecked")
Expand All @@ -118,17 +120,27 @@ public IIndex<T> getIndex(String memberName) {
@Override
public Iterator<T> everything() {
if (queryables.length == 0)
return Collections.<T> emptySet().iterator();
return Collections.emptyIterator();

if (queryables.length == 1)
return getIteratorFromQueryable(queryables[0]);

ArrayList<Iterator<T>> iterators = new ArrayList<>(queryables.length);
List<Iterator<T>> iterators = new ArrayList<>(queryables.length);
for (IQueryable<T> queryable : queryables)
iterators.add(getIteratorFromQueryable(queryable));
return new CompoundIterator<>(iterators.iterator());
}

@Override
public boolean contains(T element) {
for (IQueryable<T> queryable : queryables) {
if (queryable.contains(element)) {
return true;
}
}
return false;
}

@Override
public Object getManagedProperty(Object client, String memberName, Object key) {
for (IQueryable<T> queryable : queryables) {
Expand Down Expand Up @@ -164,7 +176,7 @@ public IExpression getExpression() {
}

Iterator<T> getCapturedIterator() {
return capturedIterator == null ? Collections.<T> emptySet().iterator() : capturedIterator;
return capturedIterator == null ? Collections.emptyIterator() : capturedIterator;
}
}

Expand Down
Loading

0 comments on commit ace64cc

Please sign in to comment.