You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BitmapWalker.java 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. package org.eclipse.jgit.revwalk;
  44. import java.io.IOException;
  45. import java.util.Arrays;
  46. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  47. import org.eclipse.jgit.errors.MissingObjectException;
  48. import org.eclipse.jgit.internal.revwalk.AddToBitmapFilter;
  49. import org.eclipse.jgit.internal.revwalk.AddUnseenToBitmapFilter;
  50. import org.eclipse.jgit.lib.AnyObjectId;
  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.NullProgressMonitor;
  55. import org.eclipse.jgit.lib.ObjectId;
  56. import org.eclipse.jgit.lib.ProgressMonitor;
  57. import org.eclipse.jgit.revwalk.filter.ObjectFilter;
  58. /**
  59. * Helper class to do ObjectWalks with pack index bitmaps.
  60. *
  61. * @since 4.10
  62. */
  63. public final class BitmapWalker {
  64. private final ObjectWalk walker;
  65. private final BitmapIndex bitmapIndex;
  66. private final ProgressMonitor pm;
  67. private long countOfBitmapIndexMisses;
  68. /**
  69. * Create a BitmapWalker.
  70. *
  71. * @param walker walker to use when traversing the object graph.
  72. * @param bitmapIndex index to obtain bitmaps from.
  73. * @param pm progress monitor to report progress on.
  74. */
  75. public BitmapWalker(
  76. ObjectWalk walker, BitmapIndex bitmapIndex, ProgressMonitor pm) {
  77. this.walker = walker;
  78. this.bitmapIndex = bitmapIndex;
  79. this.pm = (pm == null) ? NullProgressMonitor.INSTANCE : pm;
  80. }
  81. /**
  82. * Return the number of objects that had to be walked because they were not covered by a
  83. * bitmap.
  84. *
  85. * @return the number of objects that had to be walked because they were not covered by a
  86. * bitmap.
  87. */
  88. public long getCountOfBitmapIndexMisses() {
  89. return countOfBitmapIndexMisses;
  90. }
  91. /**
  92. * Return, as a bitmap, the objects reachable from the objects in start.
  93. *
  94. * @param start
  95. * the objects to start the object traversal from.
  96. * @param seen
  97. * the objects to skip if encountered during traversal.
  98. * @param ignoreMissing
  99. * true to ignore missing objects, false otherwise.
  100. * @return as a bitmap, the objects reachable from the objects in start.
  101. * @throws org.eclipse.jgit.errors.MissingObjectException
  102. * the object supplied is not available from the object
  103. * database. This usually indicates the supplied object is
  104. * invalid, but the reference was constructed during an earlier
  105. * invocation to
  106. * {@link org.eclipse.jgit.revwalk.RevWalk#lookupAny(AnyObjectId, int)}.
  107. * @throws org.eclipse.jgit.errors.IncorrectObjectTypeException
  108. * the object was not parsed yet and it was discovered during
  109. * parsing that it is not actually the type of the instance
  110. * passed in. This usually indicates the caller used the wrong
  111. * type in a
  112. * {@link org.eclipse.jgit.revwalk.RevWalk#lookupAny(AnyObjectId, int)}
  113. * call.
  114. * @throws java.io.IOException
  115. * a pack file or loose object could not be read.
  116. */
  117. public BitmapBuilder findObjects(Iterable<? extends ObjectId> start, BitmapBuilder seen,
  118. boolean ignoreMissing)
  119. throws MissingObjectException, IncorrectObjectTypeException,
  120. IOException {
  121. if (!ignoreMissing) {
  122. return findObjectsWalk(start, seen, false);
  123. }
  124. try {
  125. return findObjectsWalk(start, seen, true);
  126. } catch (MissingObjectException ignore) {
  127. // An object reachable from one of the "start"s is missing.
  128. // Walk from the "start"s one at a time so it can be excluded.
  129. }
  130. final BitmapBuilder result = bitmapIndex.newBitmapBuilder();
  131. for (ObjectId obj : start) {
  132. Bitmap bitmap = bitmapIndex.getBitmap(obj);
  133. if (bitmap != null) {
  134. result.or(bitmap);
  135. }
  136. }
  137. for (ObjectId obj : start) {
  138. if (result.contains(obj)) {
  139. continue;
  140. }
  141. try {
  142. result.or(findObjectsWalk(Arrays.asList(obj), result, false));
  143. } catch (MissingObjectException ignore) {
  144. // An object reachable from this "start" is missing.
  145. //
  146. // This can happen when the client specified a "have" line
  147. // pointing to an object that is present but unreachable:
  148. // "git prune" and "git fsck" only guarantee that the object
  149. // database will continue to contain all objects reachable
  150. // from a ref and does not guarantee connectivity for other
  151. // objects in the object database.
  152. //
  153. // In this situation, skip the relevant "start" and move on
  154. // to the next one.
  155. //
  156. // TODO(czhen): Make findObjectsWalk resume the walk instead
  157. // once RevWalk and ObjectWalk support that.
  158. }
  159. }
  160. return result;
  161. }
  162. private BitmapBuilder findObjectsWalk(Iterable<? extends ObjectId> start, BitmapBuilder seen,
  163. boolean ignoreMissingStart)
  164. throws MissingObjectException, IncorrectObjectTypeException,
  165. IOException {
  166. walker.reset();
  167. final BitmapBuilder bitmapResult = bitmapIndex.newBitmapBuilder();
  168. for (ObjectId obj : start) {
  169. Bitmap bitmap = bitmapIndex.getBitmap(obj);
  170. if (bitmap != null)
  171. bitmapResult.or(bitmap);
  172. }
  173. boolean marked = false;
  174. for (ObjectId obj : start) {
  175. try {
  176. if (!bitmapResult.contains(obj)) {
  177. walker.markStart(walker.parseAny(obj));
  178. marked = true;
  179. }
  180. } catch (MissingObjectException e) {
  181. if (ignoreMissingStart)
  182. continue;
  183. throw e;
  184. }
  185. }
  186. if (marked) {
  187. if (seen == null) {
  188. walker.setRevFilter(new AddToBitmapFilter(bitmapResult));
  189. } else {
  190. walker.setRevFilter(
  191. new AddUnseenToBitmapFilter(seen, bitmapResult));
  192. }
  193. walker.setObjectFilter(new BitmapObjectFilter(bitmapResult));
  194. while (walker.next() != null) {
  195. // Iterate through all of the commits. The BitmapRevFilter does
  196. // the work.
  197. //
  198. // filter.include returns true for commits that do not have
  199. // a bitmap in bitmapIndex and are not reachable from a
  200. // bitmap in bitmapIndex encountered earlier in the walk.
  201. // Thus the number of commits returned by next() measures how
  202. // much history was traversed without being able to make use
  203. // of bitmaps.
  204. pm.update(1);
  205. countOfBitmapIndexMisses++;
  206. }
  207. RevObject ro;
  208. while ((ro = walker.nextObject()) != null) {
  209. bitmapResult.addObject(ro, ro.getType());
  210. pm.update(1);
  211. }
  212. }
  213. return bitmapResult;
  214. }
  215. /**
  216. * Filter that excludes objects already in the given bitmap.
  217. */
  218. static class BitmapObjectFilter extends ObjectFilter {
  219. private final BitmapBuilder bitmap;
  220. BitmapObjectFilter(BitmapBuilder bitmap) {
  221. this.bitmap = bitmap;
  222. }
  223. @Override
  224. public final boolean include(ObjectWalk walker, AnyObjectId objid)
  225. throws MissingObjectException, IncorrectObjectTypeException,
  226. IOException {
  227. return !bitmap.contains(objid);
  228. }
  229. }
  230. }