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 12, 2023
1 parent 702161f commit 1944cf6
Show file tree
Hide file tree
Showing 27 changed files with 214 additions and 61 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
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2008, 2017 IBM Corporation and others.
* Copyright (c) 2008, 2023 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down 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.200.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
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2007, 2018 IBM Corporation and others.
* Copyright (c) 2007, 2023 IBM Corporation and others.
*
* This
* program and the accompanying materials are made available under the terms of
Expand Down 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
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2009, 2017 IBM Corporation and others.
* Copyright (c) 2009, 2023 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -172,6 +172,11 @@ public IQueryResult<IInstallableUnit> query(IQuery<IInstallableUnit> query, IPro
return profile.query(query, monitor);
}

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

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
@@ -1,19 +1,20 @@
/*******************************************************************************
* Copyright (c) 2007, 2017 IBM Corporation and others.
* Copyright (c) 2007, 2023 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* 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,20 +91,28 @@ public QueryablePlan(boolean add) {
this.addition = add;
}

private Stream<IInstallableUnit> installableUnits() {
if (operands.isEmpty() || status.getSeverity() == IStatus.ERROR) {
return Stream.empty();
}
return operands.stream() //
.filter(InstallableUnitOperand.class::isInstance).map(InstallableUnitOperand.class::cast)
.map(addition ? InstallableUnitOperand::second : InstallableUnitOperand::first)
.filter(Objects::nonNull);
}

@Override
public IQueryResult<IInstallableUnit> query(IQuery<IInstallableUnit> query, IProgressMonitor monitor) {
if (operands == null || status.getSeverity() == IStatus.ERROR)
Iterator<IInstallableUnit> units = installableUnits().iterator();
if (!units.hasNext()) {
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);
}
return query.perform(list.iterator());
return query.perform(units);
}

@Override
public boolean contains(IInstallableUnit element) {
return installableUnits().anyMatch(iu -> Objects.equals(iu, element));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2008, 2017 IBM Corporation and others.
* Copyright (c) 2008, 2023 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -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
@@ -1,13 +1,13 @@
/*******************************************************************************
* Copyright (c) 2008, 2017 IBM Corporation and others.
* Copyright (c) 2008, 2023 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* 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
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2008, 2017 IBM Corporation and others.
* Copyright (c) 2008, 2023 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down 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
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2007, 2020 IBM Corporation and others.
* Copyright (c) 2007, 2023 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down 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
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2007, 2017 IBM Corporation and others.
* Copyright (c) 2007, 2023 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down 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 @@ -16,4 +16,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>
Original file line number Diff line number Diff line change
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
Loading

0 comments on commit 1944cf6

Please sign in to comment.