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.Set;
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(Set<? extends ObjectId> start, BitmapBuilder seen, boolean ignoreMissingStart)
88  			throws MissingObjectException, IncorrectObjectTypeException,
89  			IOException {
90  		final BitmapBuilder bitmapResult = bitmapIndex.newBitmapBuilder();
91  
92  		for (ObjectId obj : start) {
93  			Bitmap bitmap = bitmapIndex.getBitmap(obj);
94  			if (bitmap != null)
95  				bitmapResult.or(bitmap);
96  		}
97  
98  		boolean marked = false;
99  		for (ObjectId obj : start) {
100 			try {
101 				if (!bitmapResult.contains(obj)) {
102 					walker.markStart(walker.parseAny(obj));
103 					marked = true;
104 				}
105 			} catch (MissingObjectException e) {
106 				if (ignoreMissingStart)
107 					continue;
108 				throw e;
109 			}
110 		}
111 
112 		if (marked) {
113 			if (seen == null) {
114 				walker.setRevFilter(new AddToBitmapFilter(bitmapResult));
115 			} else {
116 				walker.setRevFilter(
117 						new AddUnseenToBitmapFilter(seen, bitmapResult));
118 			}
119 
120 			while (walker.next() != null) {
121 				// Iterate through all of the commits. The BitmapRevFilter does
122 				// the work.
123 				//
124 				// filter.include returns true for commits that do not have
125 				// a bitmap in bitmapIndex and are not reachable from a
126 				// bitmap in bitmapIndex encountered earlier in the walk.
127 				// Thus the number of commits returned by next() measures how
128 				// much history was traversed without being able to make use
129 				// of bitmaps.
130 				pm.update(1);
131 				countOfBitmapIndexMisses++;
132 			}
133 
134 			RevObject ro;
135 			while ((ro = walker.nextObject()) != null) {
136 				bitmapResult.addObject(ro, ro.getType());
137 				pm.update(1);
138 			}
139 		}
140 
141 		return bitmapResult;
142 	}
143 
144 	void reset() {
145 		walker.reset();
146 	}
147 
148 	/**
149 	 * A RevFilter that adds the visited commits to {@code bitmap} as a side
150 	 * effect.
151 	 * <p>
152 	 * When the walk hits a commit that is part of {@code bitmap}'s
153 	 * BitmapIndex, that entire bitmap is ORed into {@code bitmap} and the
154 	 * commit and its parents are marked as SEEN so that the walk does not
155 	 * have to visit its ancestors.  This ensures the walk is very short if
156 	 * there is good bitmap coverage.
157 	 */
158 	static class AddToBitmapFilter extends RevFilter {
159 		private final BitmapBuilder bitmap;
160 
161 		AddToBitmapFilter(BitmapBuilder bitmap) {
162 			this.bitmap = bitmap;
163 		}
164 
165 		@Override
166 		public final boolean include(RevWalk walker, RevCommit cmit) {
167 			Bitmap visitedBitmap;
168 
169 			if (bitmap.contains(cmit)) {
170 				// already included
171 			} else if ((visitedBitmap = bitmap.getBitmapIndex()
172 					.getBitmap(cmit)) != null) {
173 				bitmap.or(visitedBitmap);
174 			} else {
175 				bitmap.addObject(cmit, Constants.OBJ_COMMIT);
176 				return true;
177 			}
178 
179 			for (RevCommit p : cmit.getParents()) {
180 				p.add(RevFlag.SEEN);
181 			}
182 			return false;
183 		}
184 
185 		@Override
186 		public final RevFilter clone() {
187 			throw new UnsupportedOperationException();
188 		}
189 
190 		@Override
191 		public final boolean requiresCommitBody() {
192 			return false;
193 		}
194 	}
195 
196 	/**
197 	 * A RevFilter that adds the visited commits to {@code bitmap} as a side
198 	 * effect.
199 	 * <p>
200 	 * When the walk hits a commit that is part of {@code bitmap}'s
201 	 * BitmapIndex, that entire bitmap is ORed into {@code bitmap} and the
202 	 * commit and its parents are marked as SEEN so that the walk does not
203 	 * have to visit its ancestors.  This ensures the walk is very short if
204 	 * there is good bitmap coverage.
205 	 * <p>
206 	 * Commits named in {@code seen} are considered already seen.  If one is
207 	 * encountered, that commit and its parents will be marked with the SEEN
208 	 * flag to prevent the walk from visiting its ancestors.
209 	 */
210 	static class AddUnseenToBitmapFilter extends RevFilter {
211 		private final BitmapBuilder seen;
212 		private final BitmapBuilder bitmap;
213 
214 		AddUnseenToBitmapFilter(BitmapBuilder seen, BitmapBuilder bitmapResult) {
215 			this.seen = seen;
216 			this.bitmap = bitmapResult;
217 		}
218 
219 		@Override
220 		public final boolean include(RevWalk walker, RevCommit cmit) {
221 			Bitmap visitedBitmap;
222 
223 			if (seen.contains(cmit) || bitmap.contains(cmit)) {
224 				// already seen or included
225 			} else if ((visitedBitmap = bitmap.getBitmapIndex()
226 					.getBitmap(cmit)) != null) {
227 				bitmap.or(visitedBitmap);
228 			} else {
229 				bitmap.addObject(cmit, Constants.OBJ_COMMIT);
230 				return true;
231 			}
232 
233 			for (RevCommit p : cmit.getParents()) {
234 				p.add(RevFlag.SEEN);
235 			}
236 			return false;
237 		}
238 
239 		@Override
240 		public final RevFilter clone() {
241 			throw new UnsupportedOperationException();
242 		}
243 
244 		@Override
245 		public final boolean requiresCommitBody() {
246 			return false;
247 		}
248 	}
249 }