Debian Codegraph @

core/java/android/content/pm/split/SplitDependencyLoader.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package android.content.pm.split;
17
18 import android.annotation.IntRange;
19 import android.annotation.NonNull;
20 import android.content.pm.parsing.PackageLite;
21 import android.util.IntArray;
22 import android.util.SparseArray;
23
24 import libcore.util.EmptyArray;
25
26 import java.util.Arrays;
27 import java.util.BitSet;
28
29 /**
30 * A helper class that implements the dependency tree traversal for splits. Callbacks
31 * are implemented by subclasses to notify whether a split has already been constructed
32 * and is cached, and to actually create the split requested.
33 *
34 * This helper is meant to be subclassed so as to reduce the number of allocations
35 * needed to make use of it.
36 *
37 * All inputs and outputs are assumed to be indices into an array of splits.
38 *
39 * @hide
40 */
41 public abstract class SplitDependencyLoader<E extends Exception> {
42 private final @NonNull SparseArray<int[]> mDependencies;
43
44 /**
45 * Construct a new SplitDependencyLoader. Meant to be called from the
46 * subclass constructor.
47 * @param dependencies The dependency tree of splits.
48 */
49 protected SplitDependencyLoader(@NonNull SparseArray<int[]> dependencies) {
50 mDependencies = dependencies;
51 }
52
53 /**
54 * Traverses the dependency tree and constructs any splits that are not already
55 * cached. This routine short-circuits and skips the creation of splits closer to the
56 * root if they are cached, as reported by the subclass implementation of
57 * {@link #isSplitCached(int)}. The construction of splits is delegated to the subclass
58 * implementation of {@link #constructSplit(int, int[], int)}.
59 * @param splitIdx The index of the split to load. 0 represents the base Application.
60 */
61 protected void loadDependenciesForSplit(@IntRange(from = 0) int splitIdx) throws E {
62 // Quick check before any allocations are done.
63 if (isSplitCached(splitIdx)) {
64 return;
65 }
66
67 // Special case the base, since it has no dependencies.
68 if (splitIdx == 0) {
69 final int[] configSplitIndices = collectConfigSplitIndices(0);
70 constructSplit(0, configSplitIndices, -1);
71 return;
72 }
73
74 // Build up the dependency hierarchy.
75 final IntArray linearDependencies = new IntArray();
76 linearDependencies.add(splitIdx);
77
78 // Collect all the dependencies that need to be constructed.
79 // They will be listed from leaf to root.
80 while (true) {
81 // Only follow the first index into the array. The others are config splits and
82 // get loaded with the split.
83 final int[] deps = mDependencies.get(splitIdx);
84 if (deps != null && deps.length > 0) {
85 splitIdx = deps[0];
86 } else {
87 splitIdx = -1;
88 }
89
90 if (splitIdx < 0 || isSplitCached(splitIdx)) {
91 break;
92 }
93
94 linearDependencies.add(splitIdx);
95 }
96
97 // Visit each index, from right to left (root to leaf).
98 int parentIdx = splitIdx;
99 for (int i = linearDependencies.size() - 1; i >= 0; i--) {
100 final int idx = linearDependencies.get(i);
101 final int[] configSplitIndices = collectConfigSplitIndices(idx);
102 constructSplit(idx, configSplitIndices, parentIdx);
103 parentIdx = idx;
104 }
105 }
106
107 private @NonNull int[] collectConfigSplitIndices(int splitIdx) {
108 // The config splits appear after the first element.
109 final int[] deps = mDependencies.get(splitIdx);
110 if (deps == null || deps.length <= 1) {
111 return EmptyArray.INT;
112 }
113 return Arrays.copyOfRange(deps, 1, deps.length);
114 }
115
116 /**
117 * Subclass to report whether the split at `splitIdx` is cached and need not be constructed.
118 * It is assumed that if `splitIdx` is cached, any parent of `splitIdx` is also cached.
119 * @param splitIdx The index of the split to check for in the cache.
120 * @return true if the split is cached and does not need to be constructed.
121 */
122 protected abstract boolean isSplitCached(@IntRange(from = 0) int splitIdx);
123
124 /**
125 * Subclass to construct a split at index `splitIdx` with parent split `parentSplitIdx`.
126 * The result is expected to be cached by the subclass in its own structures.
127 * @param splitIdx The index of the split to construct. 0 represents the base Application.
128 * @param configSplitIndices The array of configuration splits to load along with this split.
129 * May be empty (length == 0) but never null.
130 * @param parentSplitIdx The index of the parent split. -1 if there is no parent.
131 * @throws E Subclass defined exception representing failure to construct a split.
132 */
133 protected abstract void constructSplit(@IntRange(from = 0) int splitIdx,
134 @NonNull @IntRange(from = 1) int[] configSplitIndices,
135 @IntRange(from = -1) int parentSplitIdx) throws E;
136
137 public static class IllegalDependencyException extends Exception {
138 private IllegalDependencyException(String message) {
139 super(message);
140 }
141 }
142
143 private static int[] append(int[] src, int elem) {
144 if (src == null) {
145 return new int[] { elem };
146 }
147 int[] dst = Arrays.copyOf(src, src.length + 1);
148 dst[src.length] = elem;
149 return dst;
150 }
151
152 /**
153 * Build the split dependency tree by the given package
154 *
155 * @param pkg The package to retrieve the dependency tree
156 * @return The dependency tree of splits
157 * @throws IllegalDependencyException if the requires split is missing, targets split is
158 * missing, it declares itself as configuration split for a non-feature split, or
159 * cycle detected in split dependencies.
160 */
161 public static @NonNull SparseArray<int[]> createDependenciesFromPackage(PackageLite pkg)
162 throws IllegalDependencyException {
163 // The data structure that holds the dependencies. In ParsingPackageUtils, splits are
164 // stored in their own array, separate from the base. We treat all paths as equals, so
165 // we need to insert the base as index 0, and shift all other splits.
166 final SparseArray<int[]> splitDependencies = new SparseArray<>();
167
168 // The base depends on nothing.
169 splitDependencies.put(0, new int[] {-1});
170
171 // First write out the <uses-split> dependencies. These must appear first in the
172 // array of ints, as is convention in this class.
173 for (int splitIdx = 0; splitIdx < pkg.getSplitNames().length; splitIdx++) {
174 if (!pkg.getIsFeatureSplits()[splitIdx]) {
175 // Non-feature splits don't have dependencies.
176 continue;
177 }
178
179 // Implicit dependency on the base.
180 final int targetIdx;
181 final String splitDependency = pkg.getUsesSplitNames()[splitIdx];
182 if (splitDependency != null) {
183 final int depIdx = Arrays.binarySearch(pkg.getSplitNames(), splitDependency);
184 if (depIdx < 0) {
185 throw new IllegalDependencyException("Split '" + pkg.getSplitNames()[splitIdx]
186 + "' requires split '" + splitDependency + "', which is missing.");
187 }
188 targetIdx = depIdx + 1;
189 } else {
190 // Implicitly depend on the base.
191 targetIdx = 0;
192 }
193 splitDependencies.put(splitIdx + 1, new int[] {targetIdx});
194 }
195
196 // Write out the configForSplit reverse-dependencies. These appear after the <uses-split>
197 // dependencies and are considered leaves.
198 //
199 // At this point, all splits in splitDependencies have the first element in their array set.
200 for (int splitIdx = 0, size = pkg.getSplitNames().length; splitIdx < size; splitIdx++) {
201 if (pkg.getIsFeatureSplits()[splitIdx]) {
202 // Feature splits are not configForSplits.
203 continue;
204 }
205
206 // Implicit feature for the base.
207 final int targetSplitIdx;
208 final String configForSplit = pkg.getConfigForSplit()[splitIdx];
209 if (configForSplit != null) {
210 final int depIdx = Arrays.binarySearch(pkg.getSplitNames(), configForSplit);
211 if (depIdx < 0) {
212 throw new IllegalDependencyException("Split '" + pkg.getSplitNames()[splitIdx]
213 + "' targets split '" + configForSplit + "', which is missing.");
214 }
215
216 if (!pkg.getIsFeatureSplits()[depIdx]) {
217 throw new IllegalDependencyException("Split '" + pkg.getSplitNames()[splitIdx]
218 + "' declares itself as configuration split for a non-feature split '"
219 + pkg.getSplitNames()[depIdx] + "'");
220 }
221 targetSplitIdx = depIdx + 1;
222 } else {
223 targetSplitIdx = 0;
224 }
225 splitDependencies.put(targetSplitIdx,
226 append(splitDependencies.get(targetSplitIdx), splitIdx + 1));
227 }
228
229 // Verify that there are no cycles.
230 final BitSet bitset = new BitSet();
231 for (int i = 0, size = splitDependencies.size(); i < size; i++) {
232 int splitIdx = splitDependencies.keyAt(i);
233
234 bitset.clear();
235 while (splitIdx != -1) {
236 // Check if this split has been visited yet.
237 if (bitset.get(splitIdx)) {
238 throw new IllegalDependencyException("Cycle detected in split dependencies.");
239 }
240
241 // Mark the split so that if we visit it again, we no there is a cycle.
242 bitset.set(splitIdx);
243
244 // Follow the first dependency only, the others are leaves by definition.
245 final int[] deps = splitDependencies.get(splitIdx);
246 splitIdx = deps != null ? deps[0] : -1;
247 }
248 }
249 return splitDependencies;
250 }
251 }