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

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