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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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.internal.storage.pack;
  45. import org.eclipse.jgit.lib.AnyObjectId;
  46. import org.eclipse.jgit.lib.Constants;
  47. import org.eclipse.jgit.lib.ObjectId;
  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 REUSE_AS_IS = 1 << 0;
  58. private static final int DELTA_ATTEMPTED = 1 << 1;
  59. private static final int DO_NOT_DELTA = 1 << 2;
  60. private static final int EDGE = 1 << 3;
  61. private static final int ATTEMPT_DELTA_MASK = REUSE_AS_IS | DELTA_ATTEMPTED;
  62. private static final int TYPE_SHIFT = 5;
  63. private static final int EXT_SHIFT = 8;
  64. private static final int EXT_MASK = 0xf;
  65. private static final int DELTA_SHIFT = 12;
  66. private static final int NON_EXT_MASK = ~(EXT_MASK << EXT_SHIFT);
  67. private static final int NON_DELTA_MASK = 0xfff;
  68. /** Other object being packed that this will delta against. */
  69. private ObjectId deltaBase;
  70. /**
  71. * Bit field, from bit 0 to bit 31:
  72. * <ul>
  73. * <li>1 bit: canReuseAsIs</li>
  74. * <li>1 bit: deltaAttempted</li>
  75. * <li>1 bit: doNotDelta</li>
  76. * <li>1 bit: edgeObject</li>
  77. * <li>1 bit: unused</li>
  78. * <li>3 bits: type</li>
  79. * <li>4 bits: subclass flags (if any)</li>
  80. * <li>--</li>
  81. * <li>20 bits: deltaDepth</li>
  82. * </ul>
  83. */
  84. private int flags;
  85. /** Hash of the object's tree path. */
  86. private int pathHash;
  87. /** If present, deflated delta instruction stream for this object. */
  88. private DeltaCache.Ref cachedDelta;
  89. /**
  90. * Construct for the specified object id.
  91. *
  92. * @param src
  93. * object id of object for packing
  94. * @param type
  95. * real type code of the object, not its in-pack type.
  96. */
  97. public ObjectToPack(AnyObjectId src, final int type) {
  98. super(src);
  99. flags = type << TYPE_SHIFT;
  100. }
  101. /**
  102. * @return delta base object id if object is going to be packed in delta
  103. * representation; null otherwise - if going to be packed as a
  104. * whole object.
  105. */
  106. public final ObjectId getDeltaBaseId() {
  107. return deltaBase;
  108. }
  109. /**
  110. * @return delta base object to pack if object is going to be packed in
  111. * delta representation and delta is specified as object to
  112. * pack; null otherwise - if going to be packed as a whole
  113. * object or delta base is specified only as id.
  114. */
  115. public final ObjectToPack getDeltaBase() {
  116. if (deltaBase instanceof ObjectToPack)
  117. return (ObjectToPack) deltaBase;
  118. return null;
  119. }
  120. /**
  121. * Set delta base for the object. Delta base set by this method is used
  122. * by {@link PackWriter} to write object - determines its representation
  123. * in a created pack.
  124. *
  125. * @param deltaBase
  126. * delta base object or null if object should be packed as a
  127. * whole object.
  128. *
  129. */
  130. final void setDeltaBase(ObjectId deltaBase) {
  131. this.deltaBase = deltaBase;
  132. }
  133. final void setCachedDelta(DeltaCache.Ref data) {
  134. cachedDelta = data;
  135. }
  136. final DeltaCache.Ref popCachedDelta() {
  137. DeltaCache.Ref r = cachedDelta;
  138. if (r != null)
  139. cachedDelta = null;
  140. return r;
  141. }
  142. final void clearDeltaBase() {
  143. this.deltaBase = null;
  144. if (cachedDelta != null) {
  145. cachedDelta.clear();
  146. cachedDelta.enqueue();
  147. cachedDelta = null;
  148. }
  149. }
  150. /**
  151. * @return true if object is going to be written as delta; false
  152. * otherwise.
  153. */
  154. public final boolean isDeltaRepresentation() {
  155. return deltaBase != null;
  156. }
  157. /**
  158. * Check if object is already written in a pack. This information is
  159. * used to achieve delta-base precedence in a pack file.
  160. *
  161. * @return true if object is already written; false otherwise.
  162. */
  163. public final boolean isWritten() {
  164. return 1 < getOffset(); // markWantWrite sets 1.
  165. }
  166. /** @return the type of this object. */
  167. public final int getType() {
  168. return (flags >> TYPE_SHIFT) & 0x7;
  169. }
  170. final int getDeltaDepth() {
  171. return flags >>> DELTA_SHIFT;
  172. }
  173. final void setDeltaDepth(int d) {
  174. flags = (d << DELTA_SHIFT) | (flags & NON_DELTA_MASK);
  175. }
  176. final int getChainLength() {
  177. return getDeltaDepth();
  178. }
  179. final void setChainLength(int len) {
  180. setDeltaDepth(len);
  181. }
  182. final void clearChainLength() {
  183. flags &= NON_DELTA_MASK;
  184. }
  185. final boolean wantWrite() {
  186. return getOffset() == 1;
  187. }
  188. final void markWantWrite() {
  189. setOffset(1);
  190. }
  191. /**
  192. * @return true if an existing representation was selected to be reused
  193. * as-is into the pack stream.
  194. */
  195. public final boolean isReuseAsIs() {
  196. return (flags & REUSE_AS_IS) != 0;
  197. }
  198. final void setReuseAsIs() {
  199. flags |= REUSE_AS_IS;
  200. }
  201. /**
  202. * Forget the reuse information previously stored.
  203. * <p>
  204. * Implementations may subclass this method, but they must also invoke the
  205. * super version with {@code super.clearReuseAsIs()} to ensure the flag is
  206. * properly cleared for the writer.
  207. */
  208. protected void clearReuseAsIs() {
  209. flags &= ~REUSE_AS_IS;
  210. }
  211. final boolean isDoNotDelta() {
  212. return (flags & DO_NOT_DELTA) != 0;
  213. }
  214. final void setDoNotDelta() {
  215. flags |= DO_NOT_DELTA;
  216. }
  217. final boolean isEdge() {
  218. return (flags & EDGE) != 0;
  219. }
  220. final void setEdge() {
  221. flags |= EDGE;
  222. }
  223. final boolean doNotAttemptDelta() {
  224. // Do not attempt if delta attempted and object reuse.
  225. return (flags & ATTEMPT_DELTA_MASK) == ATTEMPT_DELTA_MASK;
  226. }
  227. final void setDeltaAttempted(boolean deltaAttempted) {
  228. if (deltaAttempted)
  229. flags |= DELTA_ATTEMPTED;
  230. else
  231. flags &= ~DELTA_ATTEMPTED;
  232. }
  233. /** @return the extended flags on this object, in the range [0x0, 0xf]. */
  234. protected final int getExtendedFlags() {
  235. return (flags >>> EXT_SHIFT) & EXT_MASK;
  236. }
  237. /**
  238. * Determine if a particular extended flag bit has been set.
  239. *
  240. * This implementation may be faster than calling
  241. * {@link #getExtendedFlags()} and testing the result.
  242. *
  243. * @param flag
  244. * the flag mask to test, must be between 0x0 and 0xf.
  245. * @return true if any of the bits matching the mask are non-zero.
  246. */
  247. protected final boolean isExtendedFlag(int flag) {
  248. return (flags & (flag << EXT_SHIFT)) != 0;
  249. }
  250. /**
  251. * Set an extended flag bit.
  252. *
  253. * This implementation is more efficient than getting the extended flags,
  254. * adding the bit, and setting them all back.
  255. *
  256. * @param flag
  257. * the bits to set, must be between 0x0 and 0xf.
  258. */
  259. protected final void setExtendedFlag(int flag) {
  260. flags |= (flag & EXT_MASK) << EXT_SHIFT;
  261. }
  262. /**
  263. * Clear an extended flag bit.
  264. *
  265. * This implementation is more efficient than getting the extended flags,
  266. * removing the bit, and setting them all back.
  267. *
  268. * @param flag
  269. * the bits to clear, must be between 0x0 and 0xf.
  270. */
  271. protected final void clearExtendedFlag(int flag) {
  272. flags &= ~((flag & EXT_MASK) << EXT_SHIFT);
  273. }
  274. /**
  275. * Set the extended flags used by the subclass.
  276. *
  277. * Subclass implementations may store up to 4 bits of information inside of
  278. * the internal flags field already used by the base ObjectToPack instance.
  279. *
  280. * @param extFlags
  281. * additional flag bits to store in the flags field. Due to space
  282. * constraints only values [0x0, 0xf] are permitted.
  283. */
  284. protected final void setExtendedFlags(int extFlags) {
  285. flags = ((extFlags & EXT_MASK) << EXT_SHIFT) | (flags & NON_EXT_MASK);
  286. }
  287. final int getFormat() {
  288. if (isReuseAsIs()) {
  289. if (isDeltaRepresentation())
  290. return StoredObjectRepresentation.PACK_DELTA;
  291. return StoredObjectRepresentation.PACK_WHOLE;
  292. }
  293. return StoredObjectRepresentation.FORMAT_OTHER;
  294. }
  295. // Overload weight into CRC since we don't need them at the same time.
  296. final int getWeight() {
  297. return getCRC();
  298. }
  299. final void setWeight(int weight) {
  300. setCRC(weight);
  301. }
  302. final int getPathHash() {
  303. return pathHash;
  304. }
  305. final void setPathHash(int hc) {
  306. pathHash = hc;
  307. }
  308. final int getCachedSize() {
  309. return pathHash;
  310. }
  311. final void setCachedSize(int sz) {
  312. pathHash = sz;
  313. }
  314. /**
  315. * Remember a specific representation for reuse at a later time.
  316. * <p>
  317. * Implementers should remember the representation chosen, so it can be
  318. * reused at a later time. {@link PackWriter} may invoke this method
  319. * multiple times for the same object, each time saving the current best
  320. * representation found.
  321. *
  322. * @param ref
  323. * the object representation.
  324. */
  325. public void select(StoredObjectRepresentation ref) {
  326. // Empty by default.
  327. }
  328. @SuppressWarnings("nls")
  329. @Override
  330. public String toString() {
  331. StringBuilder buf = new StringBuilder();
  332. buf.append("ObjectToPack[");
  333. buf.append(Constants.typeString(getType()));
  334. buf.append(" ");
  335. buf.append(name());
  336. if (wantWrite())
  337. buf.append(" wantWrite");
  338. if (isReuseAsIs())
  339. buf.append(" reuseAsIs");
  340. if (isDoNotDelta())
  341. buf.append(" doNotDelta");
  342. if (isEdge())
  343. buf.append(" edge");
  344. if (getDeltaDepth() > 0)
  345. buf.append(" depth=" + getDeltaDepth());
  346. if (isDeltaRepresentation()) {
  347. if (getDeltaBase() != null)
  348. buf.append(" base=inpack:" + getDeltaBase().name());
  349. else
  350. buf.append(" base=edge:" + getDeltaBaseId().name());
  351. }
  352. if (isWritten())
  353. buf.append(" offset=" + getOffset());
  354. buf.append("]");
  355. return buf.toString();
  356. }
  357. }