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.Constants;
53  import org.eclipse.jgit.lib.NullProgressMonitor;
54  import org.eclipse.jgit.lib.ObjectId;
55  import org.eclipse.jgit.lib.BitmapIndex.Bitmap;
56  import org.eclipse.jgit.lib.BitmapIndex.BitmapBuilder;
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 			BitmapRevFilter filter = newRevFilter(seen, bitmapResult);
114 			walker.setRevFilter(filter);
115 
116 			while (walker.next() != null) {
117 				// Iterate through all of the commits. The BitmapRevFilter does
118 				// the work.
119 				pm.update(1);
120 			}
121 
122 			RevObject ro;
123 			while ((ro = walker.nextObject()) != null) {
124 				bitmapResult.add(ro, ro.getType());
125 				pm.update(1);
126 			}
127 			countOfBitmapIndexMisses += filter.getCountOfLoadedCommits();
128 		}
129 
130 		return bitmapResult;
131 	}
132 
133 	void reset() {
134 		walker.reset();
135 	}
136 
137 	static BitmapRevFilter newRevFilter(
138 			final BitmapBuilder seen, final BitmapBuilder bitmapResult) {
139 		if (seen != null) {
140 			return new BitmapRevFilter() {
141 				protected boolean load(RevCommit cmit) {
142 					if (seen.contains(cmit))
143 						return false;
144 					return bitmapResult.add(cmit, Constants.OBJ_COMMIT);
145 				}
146 			};
147 		}
148 		return new BitmapRevFilter() {
149 			@Override
150 			protected boolean load(RevCommit cmit) {
151 				return bitmapResult.add(cmit, Constants.OBJ_COMMIT);
152 			}
153 		};
154 	}
155 
156 	static abstract class BitmapRevFilter extends RevFilter {
157 		private long countOfLoadedCommits;
158 
159 		protected abstract boolean load(RevCommit cmit);
160 
161 		@Override
162 		public final boolean include(RevWalk walker, RevCommit cmit) {
163 			if (load(cmit)) {
164 				countOfLoadedCommits++;
165 				return true;
166 			}
167 			for (RevCommit p : cmit.getParents())
168 				p.add(RevFlag.SEEN);
169 			return false;
170 		}
171 
172 		@Override
173 		public final RevFilter clone() {
174 			return this;
175 		}
176 
177 		@Override
178 		public final boolean requiresCommitBody() {
179 			return false;
180 		}
181 
182 		long getCountOfLoadedCommits() {
183 			return countOfLoadedCommits;
184 		}
185 	}
186 }