Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Hallgren2010-02-19 07:13:19 -0500
committerThomas Hallgren2010-02-19 07:13:19 -0500
commit3a4abd5c6deca5395ea24bb742ab53fa21427f85 (patch)
tree9eb424ba5b281990970f584088d4ceef5bf05111 /bundles/org.eclipse.equinox.p2.artifact.repository
parent273e205c6904675b1b1314b6f4df5f9e962735b5 (diff)
downloadrt.equinox.p2-3a4abd5c6deca5395ea24bb742ab53fa21427f85.tar.gz
rt.equinox.p2-3a4abd5c6deca5395ea24bb742ab53fa21427f85.tar.xz
rt.equinox.p2-3a4abd5c6deca5395ea24bb742ab53fa21427f85.zip
302201 : Unify the two query approaches used in p2
Diffstat (limited to 'bundles/org.eclipse.equinox.p2.artifact.repository')
-rw-r--r--bundles/org.eclipse.equinox.p2.artifact.repository/src/org/eclipse/equinox/internal/p2/artifact/repository/FlatteningIterator.java64
-rw-r--r--bundles/org.eclipse.equinox.p2.artifact.repository/src/org/eclipse/equinox/internal/p2/artifact/repository/simple/KeyIndex.java84
-rw-r--r--bundles/org.eclipse.equinox.p2.artifact.repository/src/org/eclipse/equinox/internal/p2/artifact/repository/simple/SimpleArtifactRepository.java34
3 files changed, 111 insertions, 71 deletions
diff --git a/bundles/org.eclipse.equinox.p2.artifact.repository/src/org/eclipse/equinox/internal/p2/artifact/repository/FlatteningIterator.java b/bundles/org.eclipse.equinox.p2.artifact.repository/src/org/eclipse/equinox/internal/p2/artifact/repository/FlatteningIterator.java
deleted file mode 100644
index 1035f1420..000000000
--- a/bundles/org.eclipse.equinox.p2.artifact.repository/src/org/eclipse/equinox/internal/p2/artifact/repository/FlatteningIterator.java
+++ /dev/null
@@ -1,64 +0,0 @@
-/*******************************************************************************
- * Copyright (c) 2009, 2010 IBM Corporation and others.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * IBM Corporation - initial API and implementation
- *******************************************************************************/
-
-package org.eclipse.equinox.internal.p2.artifact.repository;
-
-import java.util.*;
-
-/**
- * An iterator over values that are provided by iterating over collections.
- */
-public class FlatteningIterator<T> implements Iterator<T> {
- private static final Object NO_ELEMENT = new Object();
- private final Iterator<? extends Collection<T>> collectionIterator;
- private Iterator<T> currentIterator;
-
- private T nextObject = noElement();
-
- public FlatteningIterator(Iterator<? extends Collection<T>> collectionIterator) {
- this.collectionIterator = collectionIterator;
- }
-
- public boolean hasNext() {
- return positionNext();
- }
-
- public T next() {
- if (!positionNext())
- throw new NoSuchElementException();
-
- T nxt = nextObject;
- nextObject = noElement();
- return nxt;
- }
-
- public void remove() {
- throw new UnsupportedOperationException();
- }
-
- private boolean positionNext() {
- if (nextObject != NO_ELEMENT)
- return true;
-
- while (currentIterator == null || !currentIterator.hasNext()) {
- if (!collectionIterator.hasNext())
- return false;
- currentIterator = collectionIterator.next().iterator();
- }
- nextObject = currentIterator.next();
- return true;
- }
-
- @SuppressWarnings("unchecked")
- private static <T> T noElement() {
- return (T) NO_ELEMENT;
- }
-} \ No newline at end of file
diff --git a/bundles/org.eclipse.equinox.p2.artifact.repository/src/org/eclipse/equinox/internal/p2/artifact/repository/simple/KeyIndex.java b/bundles/org.eclipse.equinox.p2.artifact.repository/src/org/eclipse/equinox/internal/p2/artifact/repository/simple/KeyIndex.java
new file mode 100644
index 000000000..445543289
--- /dev/null
+++ b/bundles/org.eclipse.equinox.p2.artifact.repository/src/org/eclipse/equinox/internal/p2/artifact/repository/simple/KeyIndex.java
@@ -0,0 +1,84 @@
+/*******************************************************************************
+ * Copyright (c) 2010 Cloudsmith Inc. and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Cloudsmith Inc. - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.equinox.internal.p2.artifact.repository.simple;
+
+import java.util.*;
+import org.eclipse.equinox.internal.p2.core.helpers.CollectionUtils;
+import org.eclipse.equinox.internal.p2.metadata.ArtifactKey;
+import org.eclipse.equinox.internal.p2.metadata.index.Index;
+import org.eclipse.equinox.p2.metadata.IArtifactKey;
+import org.eclipse.equinox.p2.metadata.expression.IEvaluationContext;
+import org.eclipse.equinox.p2.metadata.expression.IExpression;
+
+/**
+ * An index that maps id to IArtifactKey
+ */
+public class KeyIndex extends Index<IArtifactKey> {
+ // Memory conserving map. Stores either String -> IArtifactKey
+ // or String -> IArtifactKey[]
+ // A stored array is always length >= 2.
+ private final Map<String, Object> artifactMap;
+
+ public KeyIndex(Collection<IArtifactKey> artifactKeys) {
+ artifactMap = new HashMap<String, Object>(artifactKeys.size());
+ for (IArtifactKey ak : artifactKeys) {
+ Object prev = artifactMap.put(ak.getId(), ak);
+ if (prev != null) {
+ if (prev instanceof IArtifactKey)
+ artifactMap.put(ak.getId(), new IArtifactKey[] {(IArtifactKey) prev, ak});
+ else {
+ IArtifactKey[] prevArr = (IArtifactKey[]) prev;
+ IArtifactKey[] nxtArr = new IArtifactKey[prevArr.length + 1];
+ System.arraycopy(prevArr, 0, nxtArr, 0, prevArr.length);
+ nxtArr[prevArr.length] = ak;
+ artifactMap.put(ak.getId(), nxtArr);
+ }
+ }
+ }
+ }
+
+ public Iterator<IArtifactKey> getCandidates(IEvaluationContext ctx, IExpression variable, IExpression booleanExpr) {
+ Object queriedKeys = getQueriedIDs(ctx, variable, ArtifactKey.MEMBER_ID, booleanExpr, null);
+ if (queriedKeys == null)
+ return null;
+
+ Collection<IArtifactKey> collector = null;
+ if (queriedKeys.getClass().isArray()) {
+ Object[] keyArr = (Object[]) queriedKeys;
+ int idx = keyArr.length;
+ while (--idx >= 0) {
+ Object v = artifactMap.get(keyArr[idx]);
+ if (v == null)
+ continue;
+ if (collector == null)
+ collector = new ArrayList<IArtifactKey>();
+ if (v instanceof IArtifactKey)
+ collector.add((IArtifactKey) v);
+ else {
+ IArtifactKey[] akArr = (IArtifactKey[]) v;
+ for (IArtifactKey ak : akArr)
+ collector.add(ak);
+ }
+ }
+ if (collector == null)
+ collector = CollectionUtils.emptySet();
+ } else {
+ Object v = artifactMap.get(queriedKeys);
+ if (v == null)
+ collector = CollectionUtils.emptySet();
+ else if (v instanceof IArtifactKey)
+ collector = Collections.singleton((IArtifactKey) v);
+ else
+ collector = CollectionUtils.unmodifiableList((IArtifactKey[]) v);
+ }
+ return collector.iterator();
+ }
+}
diff --git a/bundles/org.eclipse.equinox.p2.artifact.repository/src/org/eclipse/equinox/internal/p2/artifact/repository/simple/SimpleArtifactRepository.java b/bundles/org.eclipse.equinox.p2.artifact.repository/src/org/eclipse/equinox/internal/p2/artifact/repository/simple/SimpleArtifactRepository.java
index 674df3b9d..42d5acbf2 100644
--- a/bundles/org.eclipse.equinox.p2.artifact.repository/src/org/eclipse/equinox/internal/p2/artifact/repository/simple/SimpleArtifactRepository.java
+++ b/bundles/org.eclipse.equinox.p2.artifact.repository/src/org/eclipse/equinox/internal/p2/artifact/repository/simple/SimpleArtifactRepository.java
@@ -24,6 +24,8 @@ import org.eclipse.equinox.internal.p2.artifact.processors.md5.MD5Verifier;
import org.eclipse.equinox.internal.p2.artifact.repository.*;
import org.eclipse.equinox.internal.p2.artifact.repository.Messages;
import org.eclipse.equinox.internal.p2.core.helpers.FileUtils;
+import org.eclipse.equinox.internal.p2.metadata.ArtifactKey;
+import org.eclipse.equinox.internal.p2.metadata.expression.CompoundIterator;
import org.eclipse.equinox.internal.p2.repository.RepositoryTransport;
import org.eclipse.equinox.internal.p2.repository.Transport;
import org.eclipse.equinox.internal.provisional.p2.artifact.repository.processing.*;
@@ -31,6 +33,7 @@ import org.eclipse.equinox.internal.provisional.p2.repository.IStateful;
import org.eclipse.equinox.p2.core.IProvisioningAgent;
import org.eclipse.equinox.p2.core.ProvisionException;
import org.eclipse.equinox.p2.metadata.IArtifactKey;
+import org.eclipse.equinox.p2.metadata.index.*;
import org.eclipse.equinox.p2.query.*;
import org.eclipse.equinox.p2.repository.IRepository;
import org.eclipse.equinox.p2.repository.artifact.*;
@@ -38,7 +41,7 @@ import org.eclipse.equinox.p2.repository.artifact.spi.AbstractArtifactRepository
import org.eclipse.equinox.p2.repository.artifact.spi.ArtifactDescriptor;
import org.eclipse.osgi.util.NLS;
-public class SimpleArtifactRepository extends AbstractArtifactRepository implements IArtifactRepository, IFileArtifactRepository {
+public class SimpleArtifactRepository extends AbstractArtifactRepository implements IArtifactRepository, IFileArtifactRepository, IIndexProvider<IArtifactKey> {
/**
* A boolean property controlling whether mirroring is enabled.
*/
@@ -204,9 +207,10 @@ public class SimpleArtifactRepository extends AbstractArtifactRepository impleme
/**
* Map<IArtifactKey,List<IArtifactDescriptor>> containing the index of artifacts in the repository.
*/
- protected Map<IArtifactKey, List<IArtifactDescriptor>> artifactMap = new HashMap<IArtifactKey, List<IArtifactDescriptor>>();
+ protected final Map<IArtifactKey, List<IArtifactDescriptor>> artifactMap = new HashMap<IArtifactKey, List<IArtifactDescriptor>>();
private transient BlobStore blobStore;
transient private Mapper mapper = new Mapper();
+ private KeyIndex keyIndex;
static final private String PUBLISH_PACK_FILES_AS_SIBLINGS = "publishPackFilesAsSiblings"; //$NON-NLS-1$
@@ -271,6 +275,7 @@ public class SimpleArtifactRepository extends AbstractArtifactRepository impleme
if (descriptors == null) {
descriptors = new ArrayList<IArtifactDescriptor>();
artifactMap.put(key, descriptors);
+ keyIndex = null;
}
descriptors.add(descriptor);
}
@@ -282,8 +287,10 @@ public class SimpleArtifactRepository extends AbstractArtifactRepository impleme
return;
descriptors.remove(descriptor);
- if (descriptors.isEmpty())
+ if (descriptors.isEmpty()) {
artifactMap.remove(key);
+ keyIndex = null;
+ }
}
public SimpleArtifactRepository(IProvisioningAgent agent, String repositoryName, URI location, Map<String, String> properties) {
@@ -906,7 +913,7 @@ public class SimpleArtifactRepository extends AbstractArtifactRepository impleme
public void save() {
if (disableSave)
return;
- boolean compress = "true".equalsIgnoreCase((String) properties.get(PROP_COMPRESSED)); //$NON-NLS-1$
+ boolean compress = "true".equalsIgnoreCase(properties.get(PROP_COMPRESSED)); //$NON-NLS-1$
save(compress);
}
@@ -988,13 +995,17 @@ public class SimpleArtifactRepository extends AbstractArtifactRepository impleme
return new IQueryable<IArtifactDescriptor>() {
public IQueryResult<IArtifactDescriptor> query(IQuery<IArtifactDescriptor> query, IProgressMonitor monitor) {
- return query.perform(new FlatteningIterator<IArtifactDescriptor>(descs.iterator()));
+ return query.perform(new CompoundIterator<IArtifactDescriptor>(descs.iterator()));
}
};
}
- public IQueryResult<IArtifactKey> query(IQuery<IArtifactKey> query, IProgressMonitor monitor) {
- return query.perform(artifactMap.keySet().iterator());
+ public synchronized IQueryResult<IArtifactKey> query(IQuery<IArtifactKey> query, IProgressMonitor monitor) {
+ return query instanceof IQueryWithIndex<?> ? ((IQueryWithIndex<IArtifactKey>) query).perform(this) : query.perform(artifactMap.keySet().iterator());
+ }
+
+ public Iterator<IArtifactKey> everything() {
+ return artifactMap.keySet().iterator();
}
public IStatus executeBatch(Runnable runnable) {
@@ -1021,4 +1032,13 @@ public class SimpleArtifactRepository extends AbstractArtifactRepository impleme
result = Status.OK_STATUS;
return result;
}
+
+ public synchronized IIndex<IArtifactKey> getIndex(String memberName) {
+ if (ArtifactKey.MEMBER_ID.equals(memberName)) {
+ if (keyIndex == null)
+ keyIndex = new KeyIndex(artifactMap.keySet());
+ return keyIndex;
+ }
+ return null;
+ }
}

Back to the top