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.

BaseSearch.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * Copyright (C) 2011, 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.storage.pack;
  44. import static org.eclipse.jgit.lib.Constants.OBJ_BLOB;
  45. import static org.eclipse.jgit.lib.Constants.OBJ_TREE;
  46. import java.io.IOException;
  47. import java.util.List;
  48. import java.util.Set;
  49. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  50. import org.eclipse.jgit.errors.MissingObjectException;
  51. import org.eclipse.jgit.lib.AnyObjectId;
  52. import org.eclipse.jgit.lib.FileMode;
  53. import org.eclipse.jgit.lib.MutableObjectId;
  54. import org.eclipse.jgit.lib.ObjectId;
  55. import org.eclipse.jgit.lib.ObjectIdSubclassMap;
  56. import org.eclipse.jgit.lib.ObjectLoader;
  57. import org.eclipse.jgit.lib.ObjectReader;
  58. import org.eclipse.jgit.lib.ProgressMonitor;
  59. import org.eclipse.jgit.revwalk.RevTree;
  60. import org.eclipse.jgit.treewalk.CanonicalTreeParser;
  61. class BaseSearch {
  62. private static final int M_BLOB = FileMode.REGULAR_FILE.getBits();
  63. private static final int M_TREE = FileMode.TREE.getBits();
  64. private final ProgressMonitor progress;
  65. private final ObjectReader reader;
  66. private final ObjectId[] baseTrees;
  67. private final ObjectIdSubclassMap<ObjectToPack> objectsMap;
  68. private final List<ObjectToPack> edgeObjects;
  69. private final IntSet alreadyProcessed;
  70. private final ObjectIdSubclassMap<TreeWithData> treeCache;
  71. private final CanonicalTreeParser parser;
  72. private final MutableObjectId idBuf;
  73. BaseSearch(ProgressMonitor countingMonitor, Set<RevTree> bases,
  74. ObjectIdSubclassMap<ObjectToPack> objects,
  75. List<ObjectToPack> edges, ObjectReader or) {
  76. progress = countingMonitor;
  77. reader = or;
  78. baseTrees = bases.toArray(new ObjectId[bases.size()]);
  79. objectsMap = objects;
  80. edgeObjects = edges;
  81. alreadyProcessed = new IntSet();
  82. treeCache = new ObjectIdSubclassMap<TreeWithData>();
  83. parser = new CanonicalTreeParser();
  84. idBuf = new MutableObjectId();
  85. }
  86. void addBase(int objectType, byte[] pathBuf, int pathLen, int pathHash)
  87. throws IOException {
  88. final int tailMode = modeForType(objectType);
  89. if (tailMode == 0)
  90. return;
  91. if (!alreadyProcessed.add(pathHash))
  92. return;
  93. if (pathLen == 0) {
  94. for (ObjectId root : baseTrees)
  95. add(root, OBJ_TREE, pathHash);
  96. return;
  97. }
  98. final int firstSlash = nextSlash(pathBuf, 0, pathLen);
  99. CHECK_BASE: for (ObjectId root : baseTrees) {
  100. int ptr = 0;
  101. int end = firstSlash;
  102. int mode = end != pathLen ? M_TREE : tailMode;
  103. parser.reset(readTree(root));
  104. while (!parser.eof()) {
  105. int cmp = parser.pathCompare(pathBuf, ptr, end, mode);
  106. if (cmp < 0) {
  107. parser.next();
  108. continue;
  109. }
  110. if (cmp > 0)
  111. continue CHECK_BASE;
  112. if (end == pathLen) {
  113. if (parser.getEntryFileMode().getObjectType() == objectType) {
  114. idBuf.fromRaw(parser.idBuffer(), parser.idOffset());
  115. add(idBuf, objectType, pathHash);
  116. }
  117. continue CHECK_BASE;
  118. }
  119. if (!FileMode.TREE.equals(parser.getEntryRawMode()))
  120. continue CHECK_BASE;
  121. ptr = end + 1;
  122. end = nextSlash(pathBuf, ptr, pathLen);
  123. mode = end != pathLen ? M_TREE : tailMode;
  124. idBuf.fromRaw(parser.idBuffer(), parser.idOffset());
  125. parser.reset(readTree(idBuf));
  126. }
  127. }
  128. }
  129. private static int modeForType(int typeCode) {
  130. switch (typeCode) {
  131. case OBJ_TREE:
  132. return M_TREE;
  133. case OBJ_BLOB:
  134. return M_BLOB;
  135. default:
  136. return 0;
  137. }
  138. }
  139. private static int nextSlash(byte[] pathBuf, int ptr, int end) {
  140. while (ptr < end && pathBuf[ptr] != '/')
  141. ptr++;
  142. return ptr;
  143. }
  144. private void add(AnyObjectId id, int objectType, int pathHash) {
  145. ObjectToPack obj = new ObjectToPack(id, objectType);
  146. obj.setEdge();
  147. obj.setPathHash(pathHash);
  148. if (objectsMap.addIfAbsent(obj) == obj) {
  149. edgeObjects.add(obj);
  150. progress.update(1);
  151. }
  152. }
  153. private byte[] readTree(AnyObjectId id) throws MissingObjectException,
  154. IncorrectObjectTypeException, IOException {
  155. TreeWithData tree = treeCache.get(id);
  156. if (tree != null)
  157. return tree.buf;
  158. ObjectLoader ldr = reader.open(id, OBJ_TREE);
  159. byte[] buf = ldr.getCachedBytes(Integer.MAX_VALUE);
  160. treeCache.add(new TreeWithData(id, buf));
  161. return buf;
  162. }
  163. private static class TreeWithData extends ObjectId {
  164. final byte[] buf;
  165. TreeWithData(AnyObjectId id, byte[] buf) {
  166. super(id);
  167. this.buf = buf;
  168. }
  169. }
  170. }