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

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