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.

ObjectToPack.java 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * Copyright (C) 2008-2010, Google Inc.
  3. * Copyright (C) 2008, Marek Zawirski <marek.zawirski@gmail.com>
  4. * and other copyright owners as documented in the project's IP log.
  5. *
  6. * This program and the accompanying materials are made available
  7. * under the terms of the Eclipse Distribution License v1.0 which
  8. * accompanies this distribution, is reproduced below, and is
  9. * available at http://www.eclipse.org/org/documents/edl-v10.php
  10. *
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials provided
  23. * with the distribution.
  24. *
  25. * - Neither the name of the Eclipse Foundation, Inc. nor the
  26. * names of its contributors may be used to endorse or promote
  27. * products derived from this software without specific prior
  28. * written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  31. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  32. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  33. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  35. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  37. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  39. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  42. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  43. */
  44. package org.eclipse.jgit.storage.pack;
  45. import org.eclipse.jgit.lib.AnyObjectId;
  46. import org.eclipse.jgit.lib.ObjectId;
  47. import org.eclipse.jgit.revwalk.RevObject;
  48. import org.eclipse.jgit.transport.PackedObjectInfo;
  49. /**
  50. * Per-object state used by {@link PackWriter}.
  51. * <p>
  52. * {@code PackWriter} uses this class to track the things it needs to include in
  53. * the newly generated pack file, and how to efficiently obtain the raw data for
  54. * each object as they are written to the output stream.
  55. */
  56. public class ObjectToPack extends PackedObjectInfo {
  57. private static final int WANT_WRITE = 1 << 0;
  58. private static final int REUSE_AS_IS = 1 << 1;
  59. private static final int DO_NOT_DELTA = 1 << 2;
  60. private static final int TYPE_SHIFT = 5;
  61. private static final int DELTA_SHIFT = 8;
  62. private static final int NON_DELTA_MASK = 0xff;
  63. /** Other object being packed that this will delta against. */
  64. private ObjectId deltaBase;
  65. /**
  66. * Bit field, from bit 0 to bit 31:
  67. * <ul>
  68. * <li>1 bit: wantWrite</li>
  69. * <li>1 bit: canReuseAsIs</li>
  70. * <li>1 bit: doNotDelta</li>
  71. * <li>2 bits: unused</li>
  72. * <li>3 bits: type</li>
  73. * <li>--</li>
  74. * <li>24 bits: deltaDepth</li>
  75. * </ul>
  76. */
  77. private int flags;
  78. /** Hash of the object's tree path. */
  79. private int pathHash;
  80. /**
  81. * Construct for the specified object id.
  82. *
  83. * @param src
  84. * object id of object for packing
  85. * @param type
  86. * real type code of the object, not its in-pack type.
  87. */
  88. public ObjectToPack(AnyObjectId src, final int type) {
  89. super(src);
  90. flags = type << TYPE_SHIFT;
  91. }
  92. /**
  93. * Construct for the specified object.
  94. *
  95. * @param obj
  96. * identity of the object that will be packed. The object's
  97. * parsed status is undefined here. Implementers must not rely on
  98. * the object being parsed.
  99. */
  100. public ObjectToPack(RevObject obj) {
  101. this(obj, obj.getType());
  102. }
  103. /**
  104. * @return delta base object id if object is going to be packed in delta
  105. * representation; null otherwise - if going to be packed as a
  106. * whole object.
  107. */
  108. ObjectId getDeltaBaseId() {
  109. return deltaBase;
  110. }
  111. /**
  112. * @return delta base object to pack if object is going to be packed in
  113. * delta representation and delta is specified as object to
  114. * pack; null otherwise - if going to be packed as a whole
  115. * object or delta base is specified only as id.
  116. */
  117. ObjectToPack getDeltaBase() {
  118. if (deltaBase instanceof ObjectToPack)
  119. return (ObjectToPack) deltaBase;
  120. return null;
  121. }
  122. /**
  123. * Set delta base for the object. Delta base set by this method is used
  124. * by {@link PackWriter} to write object - determines its representation
  125. * in a created pack.
  126. *
  127. * @param deltaBase
  128. * delta base object or null if object should be packed as a
  129. * whole object.
  130. *
  131. */
  132. void setDeltaBase(ObjectId deltaBase) {
  133. this.deltaBase = deltaBase;
  134. }
  135. void clearDeltaBase() {
  136. this.deltaBase = null;
  137. }
  138. /**
  139. * @return true if object is going to be written as delta; false
  140. * otherwise.
  141. */
  142. boolean isDeltaRepresentation() {
  143. return deltaBase != null;
  144. }
  145. /**
  146. * Check if object is already written in a pack. This information is
  147. * used to achieve delta-base precedence in a pack file.
  148. *
  149. * @return true if object is already written; false otherwise.
  150. */
  151. boolean isWritten() {
  152. return getOffset() != 0;
  153. }
  154. int getType() {
  155. return (flags >> TYPE_SHIFT) & 0x7;
  156. }
  157. int getDeltaDepth() {
  158. return flags >>> DELTA_SHIFT;
  159. }
  160. void setDeltaDepth(int d) {
  161. flags = (d << DELTA_SHIFT) | (flags & NON_DELTA_MASK);
  162. }
  163. boolean wantWrite() {
  164. return (flags & WANT_WRITE) != 0;
  165. }
  166. void markWantWrite() {
  167. flags |= WANT_WRITE;
  168. }
  169. boolean isReuseAsIs() {
  170. return (flags & REUSE_AS_IS) != 0;
  171. }
  172. void setReuseAsIs() {
  173. flags |= REUSE_AS_IS;
  174. }
  175. void clearReuseAsIs() {
  176. flags &= ~REUSE_AS_IS;
  177. }
  178. boolean isDoNotDelta() {
  179. return (flags & DO_NOT_DELTA) != 0;
  180. }
  181. void setDoNotDelta(boolean noDelta) {
  182. if (noDelta)
  183. flags |= DO_NOT_DELTA;
  184. else
  185. flags &= ~DO_NOT_DELTA;
  186. }
  187. int getFormat() {
  188. if (isReuseAsIs()) {
  189. if (isDeltaRepresentation())
  190. return StoredObjectRepresentation.PACK_DELTA;
  191. return StoredObjectRepresentation.PACK_WHOLE;
  192. }
  193. return StoredObjectRepresentation.FORMAT_OTHER;
  194. }
  195. // Overload weight into CRC since we don't need them at the same time.
  196. int getWeight() {
  197. return getCRC();
  198. }
  199. void setWeight(int weight) {
  200. setCRC(weight);
  201. }
  202. int getPathHash() {
  203. return pathHash;
  204. }
  205. void setPathHash(int hc) {
  206. pathHash = hc;
  207. }
  208. /**
  209. * Remember a specific representation for reuse at a later time.
  210. * <p>
  211. * Implementers should remember the representation chosen, so it can be
  212. * reused at a later time. {@link PackWriter} may invoke this method
  213. * multiple times for the same object, each time saving the current best
  214. * representation found.
  215. *
  216. * @param ref
  217. * the object representation.
  218. */
  219. public void select(StoredObjectRepresentation ref) {
  220. // Empty by default.
  221. }
  222. }