Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 87484914eb7db0fbb318f37aa1c8f117fc8fde3c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*******************************************************************************
 *  Copyright (c) 2007, 2009 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.director;

import org.eclipse.equinox.p2.metadata.IInstallableUnitFragment;

import java.util.*;
import org.eclipse.equinox.internal.provisional.p2.metadata.MetadataFactory;
import org.eclipse.equinox.internal.provisional.p2.metadata.MetadataFactory.InstallableUnitDescription;
import org.eclipse.equinox.p2.engine.InstallableUnitOperand;
import org.eclipse.equinox.p2.metadata.IInstallableUnit;
import org.eclipse.equinox.p2.metadata.query.InstallableUnitQuery;

public class OperationGenerator {
	private static final IInstallableUnit NULL_IU = MetadataFactory.createResolvedInstallableUnit(MetadataFactory.createInstallableUnit(new InstallableUnitDescription()), new IInstallableUnitFragment[0]);

	public List<InstallableUnitOperand> generateOperation(Collection<IInstallableUnit> from_, Collection<IInstallableUnit> to_) {
		Collection<IInstallableUnit> intersection = new HashSet<IInstallableUnit>(from_);
		intersection.retainAll(to_);

		HashSet<IInstallableUnit> tmpFrom = new HashSet<IInstallableUnit>(from_);
		HashSet<IInstallableUnit> tmpTo = new HashSet<IInstallableUnit>(to_);
		tmpFrom.removeAll(intersection);
		tmpTo.removeAll(intersection);

		List<IInstallableUnit> from = new ArrayList<IInstallableUnit>(tmpFrom);
		Collections.sort(from);

		List<IInstallableUnit> to = new ArrayList<IInstallableUnit>(tmpTo);
		Collections.sort(to);

		ArrayList<InstallableUnitOperand> operations = new ArrayList<InstallableUnitOperand>();
		generateUpdates(from, to, operations);
		generateInstallUninstall(from, to, operations);
		generateConfigurationChanges(to_, intersection, operations);
		return operations;
	}

	//This generates operations that are causing the IUs to be reconfigured.
	private void generateConfigurationChanges(Collection<IInstallableUnit> to_, Collection<IInstallableUnit> intersection, ArrayList<InstallableUnitOperand> operations) {
		if (intersection.size() == 0)
			return;
		//We retain from each set the things that are the same.
		//Note that despite the fact that they are the same, a different CU can be attached.
		//The objects contained in the intersection are the one that were originally in the from collection.
		TreeSet<IInstallableUnit> to = new TreeSet<IInstallableUnit>(to_);
		for (IInstallableUnit fromIU : intersection) {
			IInstallableUnit toIU = to.tailSet(fromIU).first();
			generateConfigurationOperation(fromIU, toIU, operations);
		}

	}

	private void generateConfigurationOperation(IInstallableUnit fromIU, IInstallableUnit toIU, ArrayList<InstallableUnitOperand> operations) {
		List<IInstallableUnitFragment> fromFragments = fromIU.getFragments();
		List<IInstallableUnitFragment> toFragments = toIU.getFragments();
		if (fromFragments == toFragments)
			return;
		//Check to see if the two arrays are equals independently of the order of the fragments
		if (fromFragments.size() == toFragments.size() && fromFragments.containsAll(toFragments))
			return;
		operations.add(new InstallableUnitOperand(fromIU, toIU));
	}

	private void generateInstallUninstall(List<IInstallableUnit> from, List<IInstallableUnit> to, ArrayList<InstallableUnitOperand> operations) {
		int toIdx = 0;
		int fromIdx = 0;
		while (fromIdx != from.size() && toIdx != to.size()) {
			IInstallableUnit fromIU = from.get(fromIdx);
			IInstallableUnit toIU = to.get(toIdx);
			int comparison = toIU.compareTo(fromIU);
			if (comparison < 0) {
				operations.add(createInstallOperation(toIU));
				toIdx++;
			} else if (comparison == 0) {
				toIdx++;
				fromIdx++;
				//				System.out.println("same " + fromIU);
			} else {
				operations.add(createUninstallOperation(fromIU));
				fromIdx++;
			}
		}
		if (fromIdx != from.size()) {
			for (int i = fromIdx; i < from.size(); i++) {
				operations.add(createUninstallOperation(from.get(i)));
			}
		}
		if (toIdx != to.size()) {
			for (int i = toIdx; i < to.size(); i++) {
				operations.add(createInstallOperation(to.get(i)));
			}
		}
	}

	private void generateUpdates(List<IInstallableUnit> from, List<IInstallableUnit> to, ArrayList<InstallableUnitOperand> operations) {
		if (to.isEmpty() || from.isEmpty())
			return;

		Set<IInstallableUnit> processed = new HashSet<IInstallableUnit>();
		Set<IInstallableUnit> removedFromTo = new HashSet<IInstallableUnit>();

		Map<String, List<IInstallableUnit>> fromById = new HashMap<String, List<IInstallableUnit>>();
		for (IInstallableUnit iuFrom : from) {
			List<IInstallableUnit> ius = fromById.get(iuFrom.getId());
			if (ius == null) {
				ius = new ArrayList<IInstallableUnit>();
				fromById.put(iuFrom.getId(), ius);
			}
			ius.add(iuFrom);
		}

		for (int toIdx = 0; toIdx < to.size(); toIdx++) {
			IInstallableUnit iuTo = to.get(toIdx);
			if (iuTo.getId().equals(next(to, toIdx).getId())) { //This handle the case where there are multiple versions of the same IU in the target. Eg we are trying to update from A 1.0.0 to A 1.1.1 and A 1.2.2
				toIdx = skip(to, iuTo, toIdx) - 1;
				//System.out.println("Can't update " + iuTo + " because another iu with same id is in the target state");
				continue;
			}
			if (iuTo.getUpdateDescriptor() == null)
				continue;

			List<IInstallableUnit> fromIdIndexList = fromById.get(iuTo.getUpdateDescriptor().getId());
			if (fromIdIndexList == null)
				continue;

			//when the ui we update from is in the new state, skip (for example FROM is A, C, B & TO is C (update of
			InstallableUnitQuery updateQuery = new InstallableUnitQuery(iuTo.getUpdateDescriptor().getId(), iuTo.getUpdateDescriptor().getRange());
			Iterator<IInstallableUnit> updates = updateQuery.perform(fromIdIndexList.iterator()).iterator();

			if (!updates.hasNext()) { //Nothing to update from.
				continue;
			}
			IInstallableUnit iuFrom = updates.next();
			if (updates.hasNext()) { //There are multiple IUs to update from
				//System.out.println("Can't update  " + iuTo + " because there are multiple IUs to update from (" + toString(iusFrom) + ')');
				continue;
			}
			if (iuTo.equals(iuFrom)) {
				from.remove(iuFrom);
				fromIdIndexList.remove(iuFrom);
				removedFromTo.add(iuTo);
				continue;
			}
			operations.add(createUpdateOperation(iuFrom, iuTo));
			from.remove(iuFrom);
			fromIdIndexList.remove(iuFrom);
			processed.add(iuTo);
		}
		to.removeAll(processed);
		to.removeAll(removedFromTo);
	}

	private InstallableUnitOperand createUninstallOperation(IInstallableUnit iu) {
		return new InstallableUnitOperand(iu, null);
	}

	private InstallableUnitOperand createInstallOperation(IInstallableUnit iu) {
		return new InstallableUnitOperand(null, iu);
	}

	private InstallableUnitOperand createUpdateOperation(IInstallableUnit from, IInstallableUnit to) {
		return new InstallableUnitOperand(from, to);
	}

	private IInstallableUnit next(List<IInstallableUnit> l, int i) {
		i++;
		if (i >= l.size())
			return NULL_IU;
		return l.get(i);
	}

	private int skip(List<IInstallableUnit> c, IInstallableUnit id, int idx) {
		int i = idx;
		for (; i < c.size(); i++) {
			if (!id.getId().equals(c.get(i).getId()))
				return i;
		}
		return i;
	}
}

Back to the top