1 /******************************************************************************* 2 * Copyright (c) 2008, 2017 Code 9 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 * Code 9 - initial API and implementation 14 * IBM - ongoing development 15 * Cloudsmith Inc. - query indexes 16 ******************************************************************************/ 17 package org.eclipse.equinox.p2.publisher; 18 19 import java.util.*; 20 import org.eclipse.equinox.internal.p2.metadata.IUMap; 21 import org.eclipse.equinox.internal.p2.metadata.InstallableUnit; 22 import org.eclipse.equinox.internal.p2.metadata.expression.CompoundIterator; 23 import org.eclipse.equinox.internal.p2.metadata.index.*; 24 import org.eclipse.equinox.p2.metadata.IInstallableUnit; 25 import org.eclipse.equinox.p2.metadata.Version; 26 import org.eclipse.equinox.p2.metadata.index.IIndex; 27 import org.eclipse.equinox.p2.query.IQueryResult; 28 29 public class PublisherResult extends IndexProvider<IInstallableUnit> implements IPublisherResult { 30 31 final IUMap rootIUs = new IUMap(); 32 final IUMap nonRootIUs = new IUMap(); 33 34 private IIndex<IInstallableUnit> idIndex; 35 36 @Override addIU(IInstallableUnit iu, String type)37 public void addIU(IInstallableUnit iu, String type) { 38 if (type == ROOT) 39 rootIUs.add(iu); 40 if (type == NON_ROOT) 41 nonRootIUs.add(iu); 42 } 43 44 @Override addIUs(Collection<IInstallableUnit> ius, String type)45 public void addIUs(Collection<IInstallableUnit> ius, String type) { 46 for (IInstallableUnit iu : ius) 47 addIU(iu, type); 48 } 49 50 @Override getIU(String id, Version version, String type)51 public IInstallableUnit getIU(String id, Version version, String type) { 52 if (type == null || type == ROOT) { 53 IInstallableUnit result = rootIUs.get(id, version); 54 if (result != null) 55 return result; 56 } 57 if (type == null || type == NON_ROOT) { 58 IInstallableUnit result = nonRootIUs.get(id, version); 59 if (result != null) 60 return result; 61 } 62 return null; 63 } 64 65 // TODO this method really should not be needed as it just returns the first 66 // matching IU non-deterministically. 67 @Deprecated 68 @Override getIU(String id, String type)69 public IInstallableUnit getIU(String id, String type) { 70 if (type == null || type == ROOT) { 71 IQueryResult<IInstallableUnit> ius = rootIUs.get(id); 72 if (!ius.isEmpty()) 73 return ius.iterator().next(); 74 } 75 if (type == null || type == NON_ROOT) { 76 IQueryResult<IInstallableUnit> ius = nonRootIUs.get(id); 77 if (!ius.isEmpty()) 78 return ius.iterator().next(); 79 } 80 return null; 81 } 82 83 /** 84 * Returns the IUs in this result with the given id. 85 */ 86 @Override getIUs(String id, String type)87 public Collection<IInstallableUnit> getIUs(String id, String type) { 88 if (type == null) { 89 // TODO can this be optimized? 90 ArrayList<IInstallableUnit> result = new ArrayList<>(); 91 result.addAll(rootIUs.get(id).toUnmodifiableSet()); 92 result.addAll(nonRootIUs.get(id).toUnmodifiableSet()); 93 return result; 94 } 95 if (type == ROOT) 96 return rootIUs.get(id).toUnmodifiableSet(); 97 if (type == NON_ROOT) 98 return nonRootIUs.get(id).toUnmodifiableSet(); 99 return null; 100 } 101 102 @Override merge(IPublisherResult result, int mode)103 public void merge(IPublisherResult result, int mode) { 104 switch (mode) { 105 case MERGE_MATCHING: 106 addIUs(result.getIUs(null, ROOT), ROOT); 107 addIUs(result.getIUs(null, NON_ROOT), NON_ROOT); 108 break; 109 case MERGE_ALL_ROOT: 110 addIUs(result.getIUs(null, ROOT), ROOT); 111 addIUs(result.getIUs(null, NON_ROOT), ROOT); 112 break; 113 case MERGE_ALL_NON_ROOT: 114 addIUs(result.getIUs(null, ROOT), NON_ROOT); 115 addIUs(result.getIUs(null, NON_ROOT), NON_ROOT); 116 break; 117 default: 118 break; 119 } 120 } 121 122 @Override getIndex(String memberName)123 public synchronized IIndex<IInstallableUnit> getIndex(String memberName) { 124 if (InstallableUnit.MEMBER_ID.equals(memberName)) { 125 if (idIndex == null) { 126 ArrayList<IIndex<IInstallableUnit>> indexes = new ArrayList<>(); 127 indexes.add(new IdIndex(nonRootIUs)); 128 indexes.add(new IdIndex(rootIUs)); 129 idIndex = new CompoundIndex<>(indexes); 130 } 131 return idIndex; 132 } 133 return null; 134 } 135 136 @Override everything()137 public Iterator<IInstallableUnit> everything() { 138 ArrayList<Iterator<IInstallableUnit>> iterators = new ArrayList<>(); 139 iterators.add(nonRootIUs.iterator()); 140 iterators.add(rootIUs.iterator()); 141 return new CompoundIterator<>(iterators.iterator()); 142 } 143 144 @Override getManagedProperty(Object client, String memberName, Object key)145 public Object getManagedProperty(Object client, String memberName, Object key) { 146 return null; 147 } 148 } 149