1 /*
2 * Copyright (C) 2012, Google Inc.
3 * and other copyright owners as documented in the project's IP log.
4 *
5 * This program and the accompanying materials are made available
6 * under the terms of the Eclipse Distribution License v1.0 which
7 * accompanies this distribution, is reproduced below, and is
8 * available at http://www.eclipse.org/org/documents/edl-v10.php
9 *
10 * All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
14 * conditions are met:
15 *
16 * - Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 *
19 * - Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials provided
22 * with the distribution.
23 *
24 * - Neither the name of the Eclipse Foundation, Inc. nor the
25 * names of its contributors may be used to endorse or promote
26 * products derived from this software without specific prior
27 * written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 */
43
44 package org.eclipse.jgit.revwalk;
45
46 import java.io.IOException;
47 import java.util.Arrays;
48
49 import org.eclipse.jgit.errors.IncorrectObjectTypeException;
50 import org.eclipse.jgit.errors.MissingObjectException;
51 import org.eclipse.jgit.internal.revwalk.AddToBitmapFilter;
52 import org.eclipse.jgit.internal.revwalk.AddUnseenToBitmapFilter;
53 import org.eclipse.jgit.lib.AnyObjectId;
54 import org.eclipse.jgit.lib.BitmapIndex;
55 import org.eclipse.jgit.lib.BitmapIndex.Bitmap;
56 import org.eclipse.jgit.lib.BitmapIndex.BitmapBuilder;
57 import org.eclipse.jgit.lib.NullProgressMonitor;
58 import org.eclipse.jgit.lib.ObjectId;
59 import org.eclipse.jgit.lib.ProgressMonitor;
60 import org.eclipse.jgit.revwalk.filter.ObjectFilter;
61
62 /**
63 * Helper class to do ObjectWalks with pack index bitmaps.
64 *
65 * @since 4.10
66 */
67 public final class BitmapWalker {
68
69 private final ObjectWalk walker;
70
71 private final BitmapIndex bitmapIndex;
72
73 private final ProgressMonitor pm;
74
75 private long countOfBitmapIndexMisses;
76
77 /**
78 * Create a BitmapWalker.
79 *
80 * @param walker walker to use when traversing the object graph.
81 * @param bitmapIndex index to obtain bitmaps from.
82 * @param pm progress monitor to report progress on.
83 */
84 public BitmapWalker(
85 ObjectWalk walker, BitmapIndex bitmapIndex, ProgressMonitor pm) {
86 this.walker = walker;
87 this.bitmapIndex = bitmapIndex;
88 this.pm = (pm == null) ? NullProgressMonitor.INSTANCE : pm;
89 }
90
91 /**
92 * Return the number of objects that had to be walked because they were not covered by a
93 * bitmap.
94 *
95 * @return the number of objects that had to be walked because they were not covered by a
96 * bitmap.
97 */
98 public long getCountOfBitmapIndexMisses() {
99 return countOfBitmapIndexMisses;
100 }
101
102 /**
103 * Return, as a bitmap, the objects reachable from the objects in start.
104 *
105 * @param start
106 * the objects to start the object traversal from.
107 * @param seen
108 * the objects to skip if encountered during traversal.
109 * @param ignoreMissing
110 * true to ignore missing objects, false otherwise.
111 * @return as a bitmap, the objects reachable from the objects in start.
112 * @throws org.eclipse.jgit.errors.MissingObjectException
113 * the object supplied is not available from the object
114 * database. This usually indicates the supplied object is
115 * invalid, but the reference was constructed during an earlier
116 * invocation to
117 * {@link org.eclipse.jgit.revwalk.RevWalk#lookupAny(AnyObjectId, int)}.
118 * @throws org.eclipse.jgit.errors.IncorrectObjectTypeException
119 * the object was not parsed yet and it was discovered during
120 * parsing that it is not actually the type of the instance
121 * passed in. This usually indicates the caller used the wrong
122 * type in a
123 * {@link org.eclipse.jgit.revwalk.RevWalk#lookupAny(AnyObjectId, int)}
124 * call.
125 * @throws java.io.IOException
126 * a pack file or loose object could not be read.
127 */
128 public BitmapBuilder findObjects(Iterable<? extends ObjectId> start, BitmapBuilder seen,
129 boolean ignoreMissing)
130 throws MissingObjectException, IncorrectObjectTypeException,
131 IOException {
132 if (!ignoreMissing) {
133 return findObjectsWalk(start, seen, false);
134 }
135
136 try {
137 return findObjectsWalk(start, seen, true);
138 } catch (MissingObjectException ignore) {
139 // An object reachable from one of the "start"s is missing.
140 // Walk from the "start"s one at a time so it can be excluded.
141 }
142
143 final BitmapBuilder result = bitmapIndex.newBitmapBuilder();
144 for (ObjectId obj : start) {
145 Bitmap bitmap = bitmapIndex.getBitmap(obj);
146 if (bitmap != null) {
147 result.or(bitmap);
148 }
149 }
150
151 for (ObjectId obj : start) {
152 if (result.contains(obj)) {
153 continue;
154 }
155 try {
156 result.or(findObjectsWalk(Arrays.asList(obj), result, false));
157 } catch (MissingObjectException ignore) {
158 // An object reachable from this "start" is missing.
159 //
160 // This can happen when the client specified a "have" line
161 // pointing to an object that is present but unreachable:
162 // "git prune" and "git fsck" only guarantee that the object
163 // database will continue to contain all objects reachable
164 // from a ref and does not guarantee connectivity for other
165 // objects in the object database.
166 //
167 // In this situation, skip the relevant "start" and move on
168 // to the next one.
169 //
170 // TODO(czhen): Make findObjectsWalk resume the walk instead
171 // once RevWalk and ObjectWalk support that.
172 }
173 }
174 return result;
175 }
176
177 private BitmapBuilder findObjectsWalk(Iterable<? extends ObjectId> start, BitmapBuilder seen,
178 boolean ignoreMissingStart)
179 throws MissingObjectException, IncorrectObjectTypeException,
180 IOException {
181 walker.reset();
182 final BitmapBuilder bitmapResult = bitmapIndex.newBitmapBuilder();
183
184 for (ObjectId obj : start) {
185 Bitmap bitmap = bitmapIndex.getBitmap(obj);
186 if (bitmap != null)
187 bitmapResult.or(bitmap);
188 }
189
190 boolean marked = false;
191 for (ObjectId obj : start) {
192 try {
193 if (!bitmapResult.contains(obj)) {
194 walker.markStart(walker.parseAny(obj));
195 marked = true;
196 }
197 } catch (MissingObjectException e) {
198 if (ignoreMissingStart)
199 continue;
200 throw e;
201 }
202 }
203
204 if (marked) {
205 if (seen == null) {
206 walker.setRevFilter(new AddToBitmapFilter(bitmapResult));
207 } else {
208 walker.setRevFilter(
209 new AddUnseenToBitmapFilter(seen, bitmapResult));
210 }
211 walker.setObjectFilter(new BitmapObjectFilter(bitmapResult));
212
213 while (walker.next() != null) {
214 // Iterate through all of the commits. The BitmapRevFilter does
215 // the work.
216 //
217 // filter.include returns true for commits that do not have
218 // a bitmap in bitmapIndex and are not reachable from a
219 // bitmap in bitmapIndex encountered earlier in the walk.
220 // Thus the number of commits returned by next() measures how
221 // much history was traversed without being able to make use
222 // of bitmaps.
223 pm.update(1);
224 countOfBitmapIndexMisses++;
225 }
226
227 RevObject ro;
228 while ((ro = walker.nextObject()) != null) {
229 bitmapResult.addObject(ro, ro.getType());
230 pm.update(1);
231 }
232 }
233
234 return bitmapResult;
235 }
236
237 /**
238 * Filter that excludes objects already in the given bitmap.
239 */
240 static class BitmapObjectFilter extends ObjectFilter {
241 private final BitmapBuilder bitmap;
242
243 BitmapObjectFilter(BitmapBuilder bitmap) {
244 this.bitmap = bitmap;
245 }
246
247 @Override
248 public final boolean include(ObjectWalk walker, AnyObjectId objid)
249 throws MissingObjectException, IncorrectObjectTypeException,
250 IOException {
251 return !bitmap.contains(objid);
252 }
253 }
254 }