| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2009, 2023 EclipseSource and others. |
| 3 |
* |
| 4 |
* This |
| 5 |
* program and the accompanying materials are made available under the terms of |
| 6 |
* the Eclipse Public License 2.0 which accompanies this distribution, and is |
| 7 |
* available at |
| 8 |
* https://www.eclipse.org/legal/epl-2.0/ |
| 9 |
* |
| 10 |
* SPDX-License-Identifier: EPL-2.0 |
| 11 |
* |
| 12 |
* Contributors: |
| 13 |
* EclipseSource - initial API and implementation |
| 14 |
******************************************************************************/ |
| 15 |
package org.eclipse.equinox.p2.query; |
| 16 |
|
| 17 |
import java.util.ArrayList; |
| 18 |
import java.util.Arrays; |
| 19 |
import java.util.Collection; |
| 20 |
import java.util.Collections; |
| 21 |
import java.util.Iterator; |
| 22 |
import java.util.List; |
| 23 |
import org.eclipse.core.runtime.NullProgressMonitor; |
| 24 |
import org.eclipse.equinox.internal.p2.metadata.InstallableUnit; |
| 25 |
import org.eclipse.equinox.internal.p2.metadata.expression.CompoundIterator; |
| 26 |
import org.eclipse.equinox.internal.p2.metadata.index.CompoundIndex; |
| 27 |
import org.eclipse.equinox.internal.p2.metadata.index.IndexProvider; |
| 28 |
import org.eclipse.equinox.p2.metadata.IInstallableUnit; |
| 29 |
import org.eclipse.equinox.p2.metadata.KeyWithLocale; |
| 30 |
import org.eclipse.equinox.p2.metadata.expression.IEvaluationContext; |
| 31 |
import org.eclipse.equinox.p2.metadata.expression.IExpression; |
| 32 |
import org.eclipse.equinox.p2.metadata.index.IIndex; |
| 33 |
import org.eclipse.equinox.p2.metadata.index.IIndexProvider; |
| 34 |
|
| 35 |
/** |
| 36 |
* A queryable that holds a number of other IQueryables and provides a mechanism |
| 37 |
* for querying the entire set. |
| 38 |
* |
| 39 |
* @since 2.0 |
| 40 |
*/ |
| 41 |
public final class CompoundQueryable<T> extends IndexProvider<T> { |
| 42 |
|
| 43 |
static class PassThroughIndex<T> implements IIndex<T> { |
| 44 |
private final Iterator<T> iterator; |
| 45 |
|
| 46 |
public PassThroughIndex(Iterator<T> iterator) { |
| 47 |
this.iterator = iterator; |
| 48 |
} |
| 49 |
|
| 50 |
@Override |
| 51 |
public Iterator<T> getCandidates(IEvaluationContext ctx, IExpression variable, IExpression booleanExpr) { |
| 52 |
return iterator; |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
private Collection<? extends IQueryable<T>> queryables; |
| 57 |
|
| 58 |
public CompoundQueryable(IQueryable<T>[] queryables) { |
| 59 |
this.queryables = Arrays.asList(queryables); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Creates a queryable that combines the given collection of input queryables |
| 64 |
* |
| 65 |
* @param queryables The collection of queryables to be combined |
| 66 |
* @since 2.8 |
| 67 |
*/ |
| 68 |
public CompoundQueryable(Collection<? extends IQueryable<T>> queryables) { |
| 69 |
this.queryables = List.copyOf(queryables); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Creates a queryable that combines the two provided input queryables |
| 74 |
* |
| 75 |
* @param query1 The first queryable |
| 76 |
* @param query2 The second queryable |
| 77 |
*/ |
| 78 |
CompoundQueryable(IQueryable<T> query1, IQueryable<T> query2) { |
| 79 |
this(List.of(query1, query2)); |
| 80 |
} |
| 81 |
|
| 82 |
@Override |
| 83 |
public IIndex<T> getIndex(String memberName) { |
| 84 |
// Check that at least one of the queryable can present an index |
| 85 |
// for the given member. |
| 86 |
boolean found = queryables.stream().filter(IIndexProvider.class::isInstance).map(IIndexProvider.class::cast) |
| 87 |
.anyMatch(ip -> ip.getIndex(memberName) != null); |
| 88 |
if (!found) { |
| 89 |
// Nobody had an index for this member |
| 90 |
return null; |
| 91 |
} |
| 92 |
List<IIndex<T>> indexes = new ArrayList<>(queryables.size()); |
| 93 |
for (IQueryable<T> queryable : queryables) { |
| 94 |
if (queryable instanceof IIndexProvider<?>) { |
| 95 |
@SuppressWarnings("unchecked") |
| 96 |
IIndexProvider<T> ip = (IIndexProvider<T>) queryable; |
| 97 |
IIndex<T> index = ip.getIndex(memberName); |
| 98 |
if (index != null) { |
| 99 |
indexes.add(index); |
| 100 |
} else { |
| 101 |
indexes.add(new PassThroughIndex<>(ip.everything())); |
| 102 |
} |
| 103 |
} else { |
| 104 |
indexes.add(new PassThroughIndex<>(getIteratorFromQueryable(queryable))); |
| 105 |
} |
| 106 |
} |
| 107 |
return indexes.size() == 1 ? indexes.get(0) : new CompoundIndex<>(indexes); |
| 108 |
} |
| 109 |
|
| 110 |
@Override |
| 111 |
public Iterator<T> everything() { |
| 112 |
if (queryables.isEmpty()) { |
| 113 |
return Collections.emptyIterator(); |
| 114 |
} |
| 115 |
if (queryables.size() == 1) { |
| 116 |
return getIteratorFromQueryable(queryables.iterator().next()); |
| 117 |
} |
| 118 |
List<Iterator<T>> iterators = new ArrayList<>(queryables.size()); |
| 119 |
for (IQueryable<T> queryable : queryables) { |
| 120 |
iterators.add(getIteratorFromQueryable(queryable)); |
| 121 |
} |
| 122 |
return new CompoundIterator<>(iterators.iterator()); |
| 123 |
} |
| 124 |
|
| 125 |
@Override |
| 126 |
public boolean contains(T element) { |
| 127 |
for (IQueryable<T> queryable : queryables) { |
| 128 |
if (queryable.contains(element)) { |
| 129 |
return true; |
| 130 |
} |
| 131 |
} |
| 132 |
return false; |
| 133 |
} |
| 134 |
|
| 135 |
@Override |
| 136 |
public Object getManagedProperty(Object client, String memberName, Object key) { |
| 137 |
for (IQueryable<T> queryable : queryables) { |
| 138 |
if (queryable instanceof IIndexProvider<?>) { |
| 139 |
@SuppressWarnings("unchecked") |
| 140 |
IIndexProvider<T> ip = (IIndexProvider<T>) queryable; |
| 141 |
Object value = ip.getManagedProperty(client, memberName, key); |
| 142 |
if (value != null) { |
| 143 |
return value; |
| 144 |
} |
| 145 |
} |
| 146 |
} |
| 147 |
// When asked for translatedProperties we should return from the IU when the |
| 148 |
// property is not found. |
| 149 |
if (client instanceof IInstallableUnit iu && memberName.equals(InstallableUnit.MEMBER_TRANSLATED_PROPERTIES)) { |
| 150 |
return key instanceof KeyWithLocale keyWithLocal ? iu.getProperty(keyWithLocal.getKey()) |
| 151 |
: iu.getProperty(key.toString()); |
| 152 |
} |
| 153 |
return null; |
| 154 |
} |
| 155 |
|
| 156 |
static class IteratorCapture<T> implements IQuery<T> { |
| 157 |
private Iterator<T> capturedIterator; |
| 158 |
|
| 159 |
@Override |
| 160 |
public IQueryResult<T> perform(Iterator<T> iterator) { |
| 161 |
capturedIterator = iterator; |
| 162 |
return Collector.emptyCollector(); |
| 163 |
} |
| 164 |
|
| 165 |
@Override |
| 166 |
public IExpression getExpression() { |
| 167 |
return null; |
| 168 |
} |
| 169 |
|
| 170 |
Iterator<T> getCapturedIterator() { |
| 171 |
return capturedIterator == null ? Collections.emptyIterator() : capturedIterator; |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
private static <T> Iterator<T> getIteratorFromQueryable(IQueryable<T> queryable) { |
| 176 |
if (queryable instanceof IIndexProvider<?>) { |
| 177 |
@SuppressWarnings("unchecked") |
| 178 |
IIndexProvider<T> ip = (IIndexProvider<T>) queryable; |
| 179 |
return ip.everything(); |
| 180 |
} |
| 181 |
IteratorCapture<T> capture = new IteratorCapture<>(); |
| 182 |
queryable.query(capture, new NullProgressMonitor()); |
| 183 |
return capture.getCapturedIterator(); |
| 184 |
} |
| 185 |
} |