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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 TYPE_SHIFT = 5;
  60. private static final int DELTA_SHIFT = 8;
  61. private static final int NON_DELTA_MASK = 0xff;
  62. /** Other object being packed that this will delta against. */
  63. private ObjectId deltaBase;
  64. /**
  65. * Bit field, from bit 0 to bit 31:
  66. * <ul>
  67. * <li>1 bit: wantWrite</li>
  68. * <li>1 bit: canReuseAsIs</li>
  69. * <li>3 bits: unused</li>
  70. * <li>3 bits: type</li>
  71. * <li>--</li>
  72. * <li>24 bits: deltaDepth</li>
  73. * </ul>
  74. */
  75. private int flags;
  76. /**
  77. * Construct for the specified object id.
  78. *
  79. * @param src
  80. * object id of object for packing
  81. * @param type
  82. * real type code of the object, not its in-pack type.
  83. */
  84. public ObjectToPack(AnyObjectId src, final int type) {
  85. super(src);
  86. flags = type << TYPE_SHIFT;
  87. }
  88. /**
  89. * Construct for the specified object.
  90. *
  91. * @param obj
  92. * identity of the object that will be packed. The object's
  93. * parsed status is undefined here. Implementers must not rely on
  94. * the object being parsed.
  95. */
  96. public ObjectToPack(RevObject obj) {
  97. this(obj, obj.getType());
  98. }
  99. /**
  100. * @return delta base object id if object is going to be packed in delta
  101. * representation; null otherwise - if going to be packed as a
  102. * whole object.
  103. */
  104. ObjectId getDeltaBaseId() {
  105. return deltaBase;
  106. }
  107. /**
  108. * @return delta base object to pack if object is going to be packed in
  109. * delta representation and delta is specified as object to
  110. * pack; null otherwise - if going to be packed as a whole
  111. * object or delta base is specified only as id.
  112. */
  113. ObjectToPack getDeltaBase() {
  114. if (deltaBase instanceof ObjectToPack)
  115. return (ObjectToPack) deltaBase;
  116. return null;
  117. }
  118. /**
  119. * Set delta base for the object. Delta base set by this method is used
  120. * by {@link PackWriter} to write object - determines its representation
  121. * in a created pack.
  122. *
  123. * @param deltaBase
  124. * delta base object or null if object should be packed as a
  125. * whole object.
  126. *
  127. */
  128. void setDeltaBase(ObjectId deltaBase) {
  129. this.deltaBase = deltaBase;
  130. }
  131. void clearDeltaBase() {
  132. this.deltaBase = null;
  133. }
  134. /**
  135. * @return true if object is going to be written as delta; false
  136. * otherwise.
  137. */
  138. boolean isDeltaRepresentation() {
  139. return deltaBase != null;
  140. }
  141. /**
  142. * Check if object is already written in a pack. This information is
  143. * used to achieve delta-base precedence in a pack file.
  144. *
  145. * @return true if object is already written; false otherwise.
  146. */
  147. boolean isWritten() {
  148. return getOffset() != 0;
  149. }
  150. int getType() {
  151. return (flags >> TYPE_SHIFT) & 0x7;
  152. }
  153. int getDeltaDepth() {
  154. return flags >>> DELTA_SHIFT;
  155. }
  156. void updateDeltaDepth() {
  157. final int d;
  158. if (deltaBase instanceof ObjectToPack)
  159. d = ((ObjectToPack) deltaBase).getDeltaDepth() + 1;
  160. else if (deltaBase != null)
  161. d = 1;
  162. else
  163. d = 0;
  164. flags = (d << DELTA_SHIFT) | (flags & NON_DELTA_MASK);
  165. }
  166. boolean wantWrite() {
  167. return (flags & WANT_WRITE) != 0;
  168. }
  169. void markWantWrite() {
  170. flags |= WANT_WRITE;
  171. }
  172. boolean isReuseAsIs() {
  173. return (flags & REUSE_AS_IS) != 0;
  174. }
  175. void setReuseAsIs() {
  176. flags |= REUSE_AS_IS;
  177. }
  178. void clearReuseAsIs() {
  179. flags &= ~REUSE_AS_IS;
  180. }
  181. int getFormat() {
  182. if (isReuseAsIs()) {
  183. if (isDeltaRepresentation())
  184. return StoredObjectRepresentation.PACK_DELTA;
  185. return StoredObjectRepresentation.PACK_WHOLE;
  186. }
  187. return StoredObjectRepresentation.FORMAT_OTHER;
  188. }
  189. // Overload weight into CRC since we don't need them at the same time.
  190. int getWeight() {
  191. return getCRC();
  192. }
  193. void setWeight(int weight) {
  194. setCRC(weight);
  195. }
  196. /**
  197. * Remember a specific representation for reuse at a later time.
  198. * <p>
  199. * Implementers should remember the representation chosen, so it can be
  200. * reused at a later time. {@link PackWriter} may invoke this method
  201. * multiple times for the same object, each time saving the current best
  202. * representation found.
  203. *
  204. * @param ref
  205. * the object representation.
  206. */
  207. public void select(StoredObjectRepresentation ref) {
  208. // Empty by default.
  209. }
  210. }