View Javadoc
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.internal.storage.pack;
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.lib.BitmapIndex;
52  import org.eclipse.jgit.lib.BitmapIndex.Bitmap;
53  import org.eclipse.jgit.lib.BitmapIndex.BitmapBuilder;
54  import org.eclipse.jgit.lib.Constants;
55  import org.eclipse.jgit.lib.NullProgressMonitor;
56  import org.eclipse.jgit.lib.ObjectId;
57  import org.eclipse.jgit.lib.ProgressMonitor;
58  import org.eclipse.jgit.revwalk.ObjectWalk;
59  import org.eclipse.jgit.revwalk.RevCommit;
60  import org.eclipse.jgit.revwalk.RevFlag;
61  import org.eclipse.jgit.revwalk.RevObject;
62  import org.eclipse.jgit.revwalk.RevWalk;
63  import org.eclipse.jgit.revwalk.filter.RevFilter;
64  
65  /** Helper class for PackWriter to do ObjectWalks with pack index bitmaps. */
66  final class PackWriterBitmapWalker {
67  
68  	private final ObjectWalk walker;
69  
70  	private final BitmapIndex bitmapIndex;
71  
72  	private final ProgressMonitor pm;
73  
74  	private long countOfBitmapIndexMisses;
75  
76  	PackWriterBitmapWalker(
77  			ObjectWalk walker, BitmapIndex bitmapIndex, ProgressMonitor pm) {
78  		this.walker = walker;
79  		this.bitmapIndex = bitmapIndex;
80  		this.pm = (pm == null) ? NullProgressMonitor.INSTANCE : pm;
81  	}
82  
83  	long getCountOfBitmapIndexMisses() {
84  		return countOfBitmapIndexMisses;
85  	}
86  
87  	BitmapBuilder findObjects(Iterable<? extends ObjectId> start, BitmapBuilder seen,
88  			boolean ignoreMissing)
89  			throws MissingObjectException, IncorrectObjectTypeException,
90  				   IOException {
91  		if (!ignoreMissing) {
92  			return findObjectsWalk(start, seen, false);
93  		}
94  
95  		try {
96  			return findObjectsWalk(start, seen, true);
97  		} catch (MissingObjectException ignore) {
98  			// An object reachable from one of the "start"s is missing.
99  			// Walk from the "start"s one at a time so it can be excluded.
100 		}
101 
102 		final BitmapBuilder result = bitmapIndex.newBitmapBuilder();
103 		for (ObjectId obj : start) {
104 			Bitmap bitmap = bitmapIndex.getBitmap(obj);
105 			if (bitmap != null) {
106 				result.or(bitmap);
107 			}
108 		}
109 
110 		for (ObjectId obj : start) {
111 			if (result.contains(obj)) {
112 				continue;
113 			}
114 			try {
115 				result.or(findObjectsWalk(Arrays.asList(obj), result, false));
116 			} catch (MissingObjectException ignore) {
117 				// An object reachable from this "start" is missing.
118 				//
119 				// This can happen when the client specified a "have" line
120 				// pointing to an object that is present but unreachable:
121 				// "git prune" and "git fsck" only guarantee that the object
122 				// database will continue to contain all objects reachable
123 				// from a ref and does not guarantee connectivity for other
124 				// objects in the object database.
125 				//
126 				// In this situation, skip the relevant "start" and move on
127 				// to the next one.
128 				//
129 				// TODO(czhen): Make findObjectsWalk resume the walk instead
130 				// once RevWalk and ObjectWalk support that.
131 			}
132 		}
133 		return result;
134 	}
135 
136 	private BitmapBuilder findObjectsWalk(Iterable<? extends ObjectId> start, BitmapBuilder seen,
137 			boolean ignoreMissingStart)
138 			throws MissingObjectException, IncorrectObjectTypeException,
139 			IOException {
140 		walker.reset();
141 		final BitmapBuilder bitmapResult = bitmapIndex.newBitmapBuilder();
142 
143 		for (ObjectId obj : start) {
144 			Bitmap bitmap = bitmapIndex.getBitmap(obj);
145 			if (bitmap != null)
146 				bitmapResult.or(bitmap);
147 		}
148 
149 		boolean marked = false;
150 		for (ObjectId obj : start) {
151 			try {
152 				if (!bitmapResult.contains(obj)) {
153 					walker.markStart(walker.parseAny(obj));
154 					marked = true;
155 				}
156 			} catch (MissingObjectException e) {
157 				if (ignoreMissingStart)
158 					continue;
159 				throw e;
160 			}
161 		}
162 
163 		if (marked) {
164 			if (seen == null) {
165 				walker.setRevFilter(new AddToBitmapFilter(bitmapResult));
166 			} else {
167 				walker.setRevFilter(
168 						new AddUnseenToBitmapFilter(seen, bitmapResult));
169 			}
170 
171 			while (walker.next() != null) {
172 				// Iterate through all of the commits. The BitmapRevFilter does
173 				// the work.
174 				//
175 				// filter.include returns true for commits that do not have
176 				// a bitmap in bitmapIndex and are not reachable from a
177 				// bitmap in bitmapIndex encountered earlier in the walk.
178 				// Thus the number of commits returned by next() measures how
179 				// much history was traversed without being able to make use
180 				// of bitmaps.
181 				pm.update(1);
182 				countOfBitmapIndexMisses++;
183 			}
184 
185 			RevObject ro;
186 			while ((ro = walker.nextObject()) != null) {
187 				bitmapResult.addObject(ro, ro.getType());
188 				pm.update(1);
189 			}
190 		}
191 
192 		return bitmapResult;
193 	}
194 
195 	/**
196 	 * A RevFilter that adds the visited commits to {@code bitmap} as a side
197 	 * effect.
198 	 * <p>
199 	 * When the walk hits a commit that is part of {@code bitmap}'s
200 	 * BitmapIndex, that entire bitmap is ORed into {@code bitmap} and the
201 	 * commit and its parents are marked as SEEN so that the walk does not
202 	 * have to visit its ancestors.  This ensures the walk is very short if
203 	 * there is good bitmap coverage.
204 	 */
205 	static class AddToBitmapFilter extends RevFilter {
206 		private final BitmapBuilder bitmap;
207 
208 		AddToBitmapFilter(BitmapBuilder bitmap) {
209 			this.bitmap = bitmap;
210 		}
211 
212 		@Override
213 		public final boolean include(RevWalk walker, RevCommit cmit) {
214 			Bitmap visitedBitmap;
215 
216 			if (bitmap.contains(cmit)) {
217 				// already included
218 			} else if ((visitedBitmap = bitmap.getBitmapIndex()
219 					.getBitmap(cmit)) != null) {
220 				bitmap.or(visitedBitmap);
221 			} else {
222 				bitmap.addObject(cmit, Constants.OBJ_COMMIT);
223 				return true;
224 			}
225 
226 			for (RevCommit p : cmit.getParents()) {
227 				p.add(RevFlag.SEEN);
228 			}
229 			return false;
230 		}
231 
232 		@Override
233 		public final RevFilter clone() {
234 			throw new UnsupportedOperationException();
235 		}
236 
237 		@Override
238 		public final boolean requiresCommitBody() {
239 			return false;
240 		}
241 	}
242 
243 	/**
244 	 * A RevFilter that adds the visited commits to {@code bitmap} as a side
245 	 * effect.
246 	 * <p>
247 	 * When the walk hits a commit that is part of {@code bitmap}'s
248 	 * BitmapIndex, that entire bitmap is ORed into {@code bitmap} and the
249 	 * commit and its parents are marked as SEEN so that the walk does not
250 	 * have to visit its ancestors.  This ensures the walk is very short if
251 	 * there is good bitmap coverage.
252 	 * <p>
253 	 * Commits named in {@code seen} are considered already seen.  If one is
254 	 * encountered, that commit and its parents will be marked with the SEEN
255 	 * flag to prevent the walk from visiting its ancestors.
256 	 */
257 	static class AddUnseenToBitmapFilter extends RevFilter {
258 		private final BitmapBuilder seen;
259 		private final BitmapBuilder bitmap;
260 
261 		AddUnseenToBitmapFilter(BitmapBuilder seen, BitmapBuilder bitmapResult) {
262 			this.seen = seen;
263 			this.bitmap = bitmapResult;
264 		}
265 
266 		@Override
267 		public final boolean include(RevWalk walker, RevCommit cmit) {
268 			Bitmap visitedBitmap;
269 
270 			if (seen.contains(cmit) || bitmap.contains(cmit)) {
271 				// already seen or included
272 			} else if ((visitedBitmap = bitmap.getBitmapIndex()
273 					.getBitmap(cmit)) != null) {
274 				bitmap.or(visitedBitmap);
275 			} else {
276 				bitmap.addObject(cmit, Constants.OBJ_COMMIT);
277 				return true;
278 			}
279 
280 			for (RevCommit p : cmit.getParents()) {
281 				p.add(RevFlag.SEEN);
282 			}
283 			return false;
284 		}
285 
286 		@Override
287 		public final RevFilter clone() {
288 			throw new UnsupportedOperationException();
289 		}
290 
291 		@Override
292 		public final boolean requiresCommitBody() {
293 			return false;
294 		}
295 	}
296 }