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 7.5KB

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