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.

PackWriterBitmapWalker.java 9.0KB

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