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.

PackWriter.java 79KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543
  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 static org.eclipse.jgit.internal.storage.pack.StoredObjectRepresentation.PACK_DELTA;
  46. import static org.eclipse.jgit.internal.storage.pack.StoredObjectRepresentation.PACK_WHOLE;
  47. import static org.eclipse.jgit.lib.Constants.OBJECT_ID_LENGTH;
  48. import static org.eclipse.jgit.lib.Constants.OBJ_BLOB;
  49. import static org.eclipse.jgit.lib.Constants.OBJ_COMMIT;
  50. import static org.eclipse.jgit.lib.Constants.OBJ_TAG;
  51. import static org.eclipse.jgit.lib.Constants.OBJ_TREE;
  52. import java.io.IOException;
  53. import java.io.OutputStream;
  54. import java.lang.ref.WeakReference;
  55. import java.security.MessageDigest;
  56. import java.text.MessageFormat;
  57. import java.util.ArrayList;
  58. import java.util.Arrays;
  59. import java.util.Collection;
  60. import java.util.Collections;
  61. import java.util.Comparator;
  62. import java.util.HashSet;
  63. import java.util.Iterator;
  64. import java.util.List;
  65. import java.util.Map;
  66. import java.util.NoSuchElementException;
  67. import java.util.Set;
  68. import java.util.concurrent.ConcurrentHashMap;
  69. import java.util.concurrent.ExecutionException;
  70. import java.util.concurrent.Executor;
  71. import java.util.concurrent.ExecutorService;
  72. import java.util.concurrent.Executors;
  73. import java.util.concurrent.Future;
  74. import java.util.concurrent.TimeUnit;
  75. import java.util.zip.CRC32;
  76. import java.util.zip.CheckedOutputStream;
  77. import java.util.zip.Deflater;
  78. import java.util.zip.DeflaterOutputStream;
  79. import org.eclipse.jgit.annotations.NonNull;
  80. import org.eclipse.jgit.errors.CorruptObjectException;
  81. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  82. import org.eclipse.jgit.errors.LargeObjectException;
  83. import org.eclipse.jgit.errors.MissingObjectException;
  84. import org.eclipse.jgit.errors.StoredObjectRepresentationNotAvailableException;
  85. import org.eclipse.jgit.internal.JGitText;
  86. import org.eclipse.jgit.internal.storage.file.PackBitmapIndexBuilder;
  87. import org.eclipse.jgit.internal.storage.file.PackBitmapIndexWriterV1;
  88. import org.eclipse.jgit.internal.storage.file.PackIndexWriter;
  89. import org.eclipse.jgit.lib.AnyObjectId;
  90. import org.eclipse.jgit.lib.AsyncObjectSizeQueue;
  91. import org.eclipse.jgit.lib.BatchingProgressMonitor;
  92. import org.eclipse.jgit.lib.BitmapIndex;
  93. import org.eclipse.jgit.lib.BitmapIndex.BitmapBuilder;
  94. import org.eclipse.jgit.lib.BitmapObject;
  95. import org.eclipse.jgit.lib.Constants;
  96. import org.eclipse.jgit.lib.NullProgressMonitor;
  97. import org.eclipse.jgit.lib.ObjectId;
  98. import org.eclipse.jgit.lib.ObjectIdOwnerMap;
  99. import org.eclipse.jgit.lib.ObjectIdSet;
  100. import org.eclipse.jgit.lib.ObjectLoader;
  101. import org.eclipse.jgit.lib.ObjectReader;
  102. import org.eclipse.jgit.lib.ProgressMonitor;
  103. import org.eclipse.jgit.lib.Repository;
  104. import org.eclipse.jgit.lib.ThreadSafeProgressMonitor;
  105. import org.eclipse.jgit.revwalk.AsyncRevObjectQueue;
  106. import org.eclipse.jgit.revwalk.DepthWalk;
  107. import org.eclipse.jgit.revwalk.ObjectWalk;
  108. import org.eclipse.jgit.revwalk.RevCommit;
  109. import org.eclipse.jgit.revwalk.RevFlag;
  110. import org.eclipse.jgit.revwalk.RevObject;
  111. import org.eclipse.jgit.revwalk.RevSort;
  112. import org.eclipse.jgit.revwalk.RevTag;
  113. import org.eclipse.jgit.revwalk.RevTree;
  114. import org.eclipse.jgit.storage.pack.PackConfig;
  115. import org.eclipse.jgit.storage.pack.PackStatistics;
  116. import org.eclipse.jgit.transport.ObjectCountCallback;
  117. import org.eclipse.jgit.transport.WriteAbortedException;
  118. import org.eclipse.jgit.util.BlockList;
  119. import org.eclipse.jgit.util.TemporaryBuffer;
  120. /**
  121. * <p>
  122. * PackWriter class is responsible for generating pack files from specified set
  123. * of objects from repository. This implementation produce pack files in format
  124. * version 2.
  125. * </p>
  126. * <p>
  127. * Source of objects may be specified in two ways:
  128. * <ul>
  129. * <li>(usually) by providing sets of interesting and uninteresting objects in
  130. * repository - all interesting objects and their ancestors except uninteresting
  131. * objects and their ancestors will be included in pack, or</li>
  132. * <li>by providing iterator of {@link RevObject} specifying exact list and
  133. * order of objects in pack</li>
  134. * </ul>
  135. * <p>
  136. * Typical usage consists of creating an instance, configuring options,
  137. * preparing the list of objects by calling {@link #preparePack(Iterator)} or
  138. * {@link #preparePack(ProgressMonitor, Set, Set)}, and streaming with
  139. * {@link #writePack(ProgressMonitor, ProgressMonitor, OutputStream)}. If the
  140. * pack is being stored as a file the matching index can be written out after
  141. * writing the pack by {@link #writeIndex(OutputStream)}. An optional bitmap
  142. * index can be made by calling {@link #prepareBitmapIndex(ProgressMonitor)}
  143. * followed by {@link #writeBitmapIndex(OutputStream)}.
  144. * </p>
  145. * <p>
  146. * Class provide set of configurable options and {@link ProgressMonitor}
  147. * support, as operations may take a long time for big repositories. Deltas
  148. * searching algorithm is <b>NOT IMPLEMENTED</b> yet - this implementation
  149. * relies only on deltas and objects reuse.
  150. * </p>
  151. * <p>
  152. * This class is not thread safe. It is intended to be used in one thread as a
  153. * single pass to produce one pack. Invoking methods multiple times or out of
  154. * order is not supported as internal data structures are destroyed during
  155. * certain phases to save memory when packing large repositories.
  156. * </p>
  157. */
  158. public class PackWriter implements AutoCloseable {
  159. private static final int PACK_VERSION_GENERATED = 2;
  160. /** Empty set of objects for {@code preparePack()}. */
  161. public static final Set<ObjectId> NONE = Collections.emptySet();
  162. private static final Map<WeakReference<PackWriter>, Boolean> instances =
  163. new ConcurrentHashMap<>();
  164. private static final Iterable<PackWriter> instancesIterable = new Iterable<PackWriter>() {
  165. @Override
  166. public Iterator<PackWriter> iterator() {
  167. return new Iterator<PackWriter>() {
  168. private final Iterator<WeakReference<PackWriter>> it =
  169. instances.keySet().iterator();
  170. private PackWriter next;
  171. @Override
  172. public boolean hasNext() {
  173. if (next != null)
  174. return true;
  175. while (it.hasNext()) {
  176. WeakReference<PackWriter> ref = it.next();
  177. next = ref.get();
  178. if (next != null)
  179. return true;
  180. it.remove();
  181. }
  182. return false;
  183. }
  184. @Override
  185. public PackWriter next() {
  186. if (hasNext()) {
  187. PackWriter result = next;
  188. next = null;
  189. return result;
  190. }
  191. throw new NoSuchElementException();
  192. }
  193. @Override
  194. public void remove() {
  195. throw new UnsupportedOperationException();
  196. }
  197. };
  198. }
  199. };
  200. /** @return all allocated, non-released PackWriters instances. */
  201. public static Iterable<PackWriter> getInstances() {
  202. return instancesIterable;
  203. }
  204. @SuppressWarnings("unchecked")
  205. BlockList<ObjectToPack> objectsLists[] = new BlockList[OBJ_TAG + 1];
  206. {
  207. objectsLists[OBJ_COMMIT] = new BlockList<>();
  208. objectsLists[OBJ_TREE] = new BlockList<>();
  209. objectsLists[OBJ_BLOB] = new BlockList<>();
  210. objectsLists[OBJ_TAG] = new BlockList<>();
  211. }
  212. private ObjectIdOwnerMap<ObjectToPack> objectsMap = new ObjectIdOwnerMap<>();
  213. // edge objects for thin packs
  214. private List<ObjectToPack> edgeObjects = new BlockList<>();
  215. // Objects the client is known to have already.
  216. private BitmapBuilder haveObjects;
  217. private List<CachedPack> cachedPacks = new ArrayList<>(2);
  218. private Set<ObjectId> tagTargets = NONE;
  219. private Set<? extends ObjectId> excludeFromBitmapSelection = NONE;
  220. private ObjectIdSet[] excludeInPacks;
  221. private ObjectIdSet excludeInPackLast;
  222. private Deflater myDeflater;
  223. private final ObjectReader reader;
  224. /** {@link #reader} recast to the reuse interface, if it supports it. */
  225. private final ObjectReuseAsIs reuseSupport;
  226. final PackConfig config;
  227. private final PackStatistics.Accumulator stats;
  228. private final MutableState state;
  229. private final WeakReference<PackWriter> selfRef;
  230. private PackStatistics.ObjectType.Accumulator typeStats;
  231. private List<ObjectToPack> sortedByName;
  232. private byte packcsum[];
  233. private boolean deltaBaseAsOffset;
  234. private boolean reuseDeltas;
  235. private boolean reuseDeltaCommits;
  236. private boolean reuseValidate;
  237. private boolean thin;
  238. private boolean useCachedPacks;
  239. private boolean useBitmaps;
  240. private boolean ignoreMissingUninteresting = true;
  241. private boolean pruneCurrentObjectList;
  242. private boolean shallowPack;
  243. private boolean canBuildBitmaps;
  244. private boolean indexDisabled;
  245. private int depth;
  246. private Collection<? extends ObjectId> unshallowObjects;
  247. private PackBitmapIndexBuilder writeBitmaps;
  248. private CRC32 crc32;
  249. private ObjectCountCallback callback;
  250. /**
  251. * Create writer for specified repository.
  252. * <p>
  253. * Objects for packing are specified in {@link #preparePack(Iterator)} or
  254. * {@link #preparePack(ProgressMonitor, Set, Set)}.
  255. *
  256. * @param repo
  257. * repository where objects are stored.
  258. */
  259. public PackWriter(final Repository repo) {
  260. this(repo, repo.newObjectReader());
  261. }
  262. /**
  263. * Create a writer to load objects from the specified reader.
  264. * <p>
  265. * Objects for packing are specified in {@link #preparePack(Iterator)} or
  266. * {@link #preparePack(ProgressMonitor, Set, Set)}.
  267. *
  268. * @param reader
  269. * reader to read from the repository with.
  270. */
  271. public PackWriter(final ObjectReader reader) {
  272. this(new PackConfig(), reader);
  273. }
  274. /**
  275. * Create writer for specified repository.
  276. * <p>
  277. * Objects for packing are specified in {@link #preparePack(Iterator)} or
  278. * {@link #preparePack(ProgressMonitor, Set, Set)}.
  279. *
  280. * @param repo
  281. * repository where objects are stored.
  282. * @param reader
  283. * reader to read from the repository with.
  284. */
  285. public PackWriter(final Repository repo, final ObjectReader reader) {
  286. this(new PackConfig(repo), reader);
  287. }
  288. /**
  289. * Create writer with a specified configuration.
  290. * <p>
  291. * Objects for packing are specified in {@link #preparePack(Iterator)} or
  292. * {@link #preparePack(ProgressMonitor, Set, Set)}.
  293. *
  294. * @param config
  295. * configuration for the pack writer.
  296. * @param reader
  297. * reader to read from the repository with.
  298. */
  299. public PackWriter(final PackConfig config, final ObjectReader reader) {
  300. this.config = config;
  301. this.reader = reader;
  302. if (reader instanceof ObjectReuseAsIs)
  303. reuseSupport = ((ObjectReuseAsIs) reader);
  304. else
  305. reuseSupport = null;
  306. deltaBaseAsOffset = config.isDeltaBaseAsOffset();
  307. reuseDeltas = config.isReuseDeltas();
  308. reuseValidate = true; // be paranoid by default
  309. stats = new PackStatistics.Accumulator();
  310. state = new MutableState();
  311. selfRef = new WeakReference<>(this);
  312. instances.put(selfRef, Boolean.TRUE);
  313. }
  314. /**
  315. * Set the {@code ObjectCountCallback}.
  316. * <p>
  317. * It should be set before calling
  318. * {@link #writePack(ProgressMonitor, ProgressMonitor, OutputStream)}.
  319. *
  320. * @param callback
  321. * the callback to set
  322. *
  323. * @return this object for chaining.
  324. */
  325. public PackWriter setObjectCountCallback(ObjectCountCallback callback) {
  326. this.callback = callback;
  327. return this;
  328. }
  329. /**
  330. * Records the set of shallow commits in the client.
  331. *
  332. * @param clientShallowCommits
  333. * the shallow commits in the client
  334. */
  335. public void setClientShallowCommits(Set<ObjectId> clientShallowCommits) {
  336. stats.clientShallowCommits = Collections
  337. .unmodifiableSet(new HashSet<>(clientShallowCommits));
  338. }
  339. /**
  340. * Check whether writer can store delta base as an offset (new style
  341. * reducing pack size) or should store it as an object id (legacy style,
  342. * compatible with old readers).
  343. *
  344. * Default setting: {@value PackConfig#DEFAULT_DELTA_BASE_AS_OFFSET}
  345. *
  346. * @return true if delta base is stored as an offset; false if it is stored
  347. * as an object id.
  348. */
  349. public boolean isDeltaBaseAsOffset() {
  350. return deltaBaseAsOffset;
  351. }
  352. /**
  353. * Set writer delta base format. Delta base can be written as an offset in a
  354. * pack file (new approach reducing file size) or as an object id (legacy
  355. * approach, compatible with old readers).
  356. *
  357. * Default setting: {@value PackConfig#DEFAULT_DELTA_BASE_AS_OFFSET}
  358. *
  359. * @param deltaBaseAsOffset
  360. * boolean indicating whether delta base can be stored as an
  361. * offset.
  362. */
  363. public void setDeltaBaseAsOffset(boolean deltaBaseAsOffset) {
  364. this.deltaBaseAsOffset = deltaBaseAsOffset;
  365. }
  366. /**
  367. * Check if the writer will reuse commits that are already stored as deltas.
  368. *
  369. * @return true if the writer would reuse commits stored as deltas, assuming
  370. * delta reuse is already enabled.
  371. */
  372. public boolean isReuseDeltaCommits() {
  373. return reuseDeltaCommits;
  374. }
  375. /**
  376. * Set the writer to reuse existing delta versions of commits.
  377. *
  378. * @param reuse
  379. * if true, the writer will reuse any commits stored as deltas.
  380. * By default the writer does not reuse delta commits.
  381. */
  382. public void setReuseDeltaCommits(boolean reuse) {
  383. reuseDeltaCommits = reuse;
  384. }
  385. /**
  386. * Check if the writer validates objects before copying them.
  387. *
  388. * @return true if validation is enabled; false if the reader will handle
  389. * object validation as a side-effect of it consuming the output.
  390. */
  391. public boolean isReuseValidatingObjects() {
  392. return reuseValidate;
  393. }
  394. /**
  395. * Enable (or disable) object validation during packing.
  396. *
  397. * @param validate
  398. * if true the pack writer will validate an object before it is
  399. * put into the output. This additional validation work may be
  400. * necessary to avoid propagating corruption from one local pack
  401. * file to another local pack file.
  402. */
  403. public void setReuseValidatingObjects(boolean validate) {
  404. reuseValidate = validate;
  405. }
  406. /** @return true if this writer is producing a thin pack. */
  407. public boolean isThin() {
  408. return thin;
  409. }
  410. /**
  411. * @param packthin
  412. * a boolean indicating whether writer may pack objects with
  413. * delta base object not within set of objects to pack, but
  414. * belonging to party repository (uninteresting/boundary) as
  415. * determined by set; this kind of pack is used only for
  416. * transport; true - to produce thin pack, false - otherwise.
  417. */
  418. public void setThin(final boolean packthin) {
  419. thin = packthin;
  420. }
  421. /** @return true to reuse cached packs. If true index creation isn't available. */
  422. public boolean isUseCachedPacks() {
  423. return useCachedPacks;
  424. }
  425. /**
  426. * @param useCached
  427. * if set to true and a cached pack is present, it will be
  428. * appended onto the end of a thin-pack, reducing the amount of
  429. * working set space and CPU used by PackWriter. Enabling this
  430. * feature prevents PackWriter from creating an index for the
  431. * newly created pack, so its only suitable for writing to a
  432. * network client, where the client will make the index.
  433. */
  434. public void setUseCachedPacks(boolean useCached) {
  435. useCachedPacks = useCached;
  436. }
  437. /** @return true to use bitmaps for ObjectWalks, if available. */
  438. public boolean isUseBitmaps() {
  439. return useBitmaps;
  440. }
  441. /**
  442. * @param useBitmaps
  443. * if set to true, bitmaps will be used when preparing a pack.
  444. */
  445. public void setUseBitmaps(boolean useBitmaps) {
  446. this.useBitmaps = useBitmaps;
  447. }
  448. /** @return true if the index file cannot be created by this PackWriter. */
  449. public boolean isIndexDisabled() {
  450. return indexDisabled || !cachedPacks.isEmpty();
  451. }
  452. /**
  453. * @param noIndex
  454. * true to disable creation of the index file.
  455. */
  456. public void setIndexDisabled(boolean noIndex) {
  457. this.indexDisabled = noIndex;
  458. }
  459. /**
  460. * @return true to ignore objects that are uninteresting and also not found
  461. * on local disk; false to throw a {@link MissingObjectException}
  462. * out of {@link #preparePack(ProgressMonitor, Set, Set)} if an
  463. * uninteresting object is not in the source repository. By default,
  464. * true, permitting gracefully ignoring of uninteresting objects.
  465. */
  466. public boolean isIgnoreMissingUninteresting() {
  467. return ignoreMissingUninteresting;
  468. }
  469. /**
  470. * @param ignore
  471. * true if writer should ignore non existing uninteresting
  472. * objects during construction set of objects to pack; false
  473. * otherwise - non existing uninteresting objects may cause
  474. * {@link MissingObjectException}
  475. */
  476. public void setIgnoreMissingUninteresting(final boolean ignore) {
  477. ignoreMissingUninteresting = ignore;
  478. }
  479. /**
  480. * Set the tag targets that should be hoisted earlier during packing.
  481. * <p>
  482. * Callers may put objects into this set before invoking any of the
  483. * preparePack methods to influence where an annotated tag's target is
  484. * stored within the resulting pack. Typically these will be clustered
  485. * together, and hoisted earlier in the file even if they are ancient
  486. * revisions, allowing readers to find tag targets with better locality.
  487. *
  488. * @param objects
  489. * objects that annotated tags point at.
  490. */
  491. public void setTagTargets(Set<ObjectId> objects) {
  492. tagTargets = objects;
  493. }
  494. /**
  495. * Configure this pack for a shallow clone.
  496. *
  497. * @param depth
  498. * maximum depth of history to return. 1 means return only the
  499. * "wants".
  500. * @param unshallow
  501. * objects which used to be shallow on the client, but are being
  502. * extended as part of this fetch
  503. */
  504. public void setShallowPack(int depth,
  505. Collection<? extends ObjectId> unshallow) {
  506. this.shallowPack = true;
  507. this.depth = depth;
  508. this.unshallowObjects = unshallow;
  509. }
  510. /**
  511. * Returns objects number in a pack file that was created by this writer.
  512. *
  513. * @return number of objects in pack.
  514. * @throws IOException
  515. * a cached pack cannot supply its object count.
  516. */
  517. public long getObjectCount() throws IOException {
  518. if (stats.totalObjects == 0) {
  519. long objCnt = 0;
  520. objCnt += objectsLists[OBJ_COMMIT].size();
  521. objCnt += objectsLists[OBJ_TREE].size();
  522. objCnt += objectsLists[OBJ_BLOB].size();
  523. objCnt += objectsLists[OBJ_TAG].size();
  524. for (CachedPack pack : cachedPacks)
  525. objCnt += pack.getObjectCount();
  526. return objCnt;
  527. }
  528. return stats.totalObjects;
  529. }
  530. /**
  531. * Returns the object ids in the pack file that was created by this writer.
  532. * <p>
  533. * This method can only be invoked after
  534. * {@link #writePack(ProgressMonitor, ProgressMonitor, OutputStream)} has
  535. * been invoked and completed successfully.
  536. *
  537. * @return set of objects in pack.
  538. * @throws IOException
  539. * a cached pack cannot supply its object ids.
  540. */
  541. public ObjectIdOwnerMap<ObjectIdOwnerMap.Entry> getObjectSet()
  542. throws IOException {
  543. if (!cachedPacks.isEmpty())
  544. throw new IOException(
  545. JGitText.get().cachedPacksPreventsListingObjects);
  546. if (writeBitmaps != null) {
  547. return writeBitmaps.getObjectSet();
  548. }
  549. ObjectIdOwnerMap<ObjectIdOwnerMap.Entry> r = new ObjectIdOwnerMap<>();
  550. for (BlockList<ObjectToPack> objList : objectsLists) {
  551. if (objList != null) {
  552. for (ObjectToPack otp : objList)
  553. r.add(new ObjectIdOwnerMap.Entry(otp) {
  554. // A new entry that copies the ObjectId
  555. });
  556. }
  557. }
  558. return r;
  559. }
  560. /**
  561. * Add a pack index whose contents should be excluded from the result.
  562. *
  563. * @param idx
  564. * objects in this index will not be in the output pack.
  565. */
  566. public void excludeObjects(ObjectIdSet idx) {
  567. if (excludeInPacks == null) {
  568. excludeInPacks = new ObjectIdSet[] { idx };
  569. excludeInPackLast = idx;
  570. } else {
  571. int cnt = excludeInPacks.length;
  572. ObjectIdSet[] newList = new ObjectIdSet[cnt + 1];
  573. System.arraycopy(excludeInPacks, 0, newList, 0, cnt);
  574. newList[cnt] = idx;
  575. excludeInPacks = newList;
  576. }
  577. }
  578. /**
  579. * Prepare the list of objects to be written to the pack stream.
  580. * <p>
  581. * Iterator <b>exactly</b> determines which objects are included in a pack
  582. * and order they appear in pack (except that objects order by type is not
  583. * needed at input). This order should conform general rules of ordering
  584. * objects in git - by recency and path (type and delta-base first is
  585. * internally secured) and responsibility for guaranteeing this order is on
  586. * a caller side. Iterator must return each id of object to write exactly
  587. * once.
  588. * </p>
  589. *
  590. * @param objectsSource
  591. * iterator of object to store in a pack; order of objects within
  592. * each type is important, ordering by type is not needed;
  593. * allowed types for objects are {@link Constants#OBJ_COMMIT},
  594. * {@link Constants#OBJ_TREE}, {@link Constants#OBJ_BLOB} and
  595. * {@link Constants#OBJ_TAG}; objects returned by iterator may be
  596. * later reused by caller as object id and type are internally
  597. * copied in each iteration.
  598. * @throws IOException
  599. * when some I/O problem occur during reading objects.
  600. */
  601. public void preparePack(@NonNull Iterator<RevObject> objectsSource)
  602. throws IOException {
  603. while (objectsSource.hasNext()) {
  604. addObject(objectsSource.next());
  605. }
  606. }
  607. /**
  608. * Prepare the list of objects to be written to the pack stream.
  609. * <p>
  610. * Basing on these 2 sets, another set of objects to put in a pack file is
  611. * created: this set consists of all objects reachable (ancestors) from
  612. * interesting objects, except uninteresting objects and their ancestors.
  613. * This method uses class {@link ObjectWalk} extensively to find out that
  614. * appropriate set of output objects and their optimal order in output pack.
  615. * Order is consistent with general git in-pack rules: sort by object type,
  616. * recency, path and delta-base first.
  617. * </p>
  618. *
  619. * @param countingMonitor
  620. * progress during object enumeration.
  621. * @param want
  622. * collection of objects to be marked as interesting (start
  623. * points of graph traversal). Must not be {@code null}.
  624. * @param have
  625. * collection of objects to be marked as uninteresting (end
  626. * points of graph traversal). Pass {@link #NONE} if all objects
  627. * reachable from {@code want} are desired, such as when serving
  628. * a clone.
  629. * @throws IOException
  630. * when some I/O problem occur during reading objects.
  631. */
  632. public void preparePack(ProgressMonitor countingMonitor,
  633. @NonNull Set<? extends ObjectId> want,
  634. @NonNull Set<? extends ObjectId> have) throws IOException {
  635. preparePack(countingMonitor, want, have, NONE, NONE);
  636. }
  637. /**
  638. * Prepare the list of objects to be written to the pack stream.
  639. * <p>
  640. * Like {@link #preparePack(ProgressMonitor, Set, Set)} but also allows
  641. * specifying commits that should not be walked past ("shallow" commits).
  642. * The caller is responsible for filtering out commits that should not be
  643. * shallow any more ("unshallow" commits as in {@link #setShallowPack}) from
  644. * the shallow set.
  645. *
  646. * @param countingMonitor
  647. * progress during object enumeration.
  648. * @param want
  649. * objects of interest, ancestors of which will be included in
  650. * the pack. Must not be {@code null}.
  651. * @param have
  652. * objects whose ancestors (up to and including {@code shallow}
  653. * commits) do not need to be included in the pack because they
  654. * are already available from elsewhere. Must not be
  655. * {@code null}.
  656. * @param shallow
  657. * commits indicating the boundary of the history marked with
  658. * {@code have}. Shallow commits have parents but those parents
  659. * are considered not to be already available. Parents of
  660. * {@code shallow} commits and earlier generations will be
  661. * included in the pack if requested by {@code want}. Must not be
  662. * {@code null}.
  663. * @throws IOException
  664. * an I/O problem occurred while reading objects.
  665. */
  666. public void preparePack(ProgressMonitor countingMonitor,
  667. @NonNull Set<? extends ObjectId> want,
  668. @NonNull Set<? extends ObjectId> have,
  669. @NonNull Set<? extends ObjectId> shallow) throws IOException {
  670. preparePack(countingMonitor, want, have, shallow, NONE);
  671. }
  672. /**
  673. * Prepare the list of objects to be written to the pack stream.
  674. * <p>
  675. * Like {@link #preparePack(ProgressMonitor, Set, Set)} but also allows
  676. * specifying commits that should not be walked past ("shallow" commits).
  677. * The caller is responsible for filtering out commits that should not be
  678. * shallow any more ("unshallow" commits as in {@link #setShallowPack}) from
  679. * the shallow set.
  680. *
  681. * @param countingMonitor
  682. * progress during object enumeration.
  683. * @param want
  684. * objects of interest, ancestors of which will be included in
  685. * the pack. Must not be {@code null}.
  686. * @param have
  687. * objects whose ancestors (up to and including {@code shallow}
  688. * commits) do not need to be included in the pack because they
  689. * are already available from elsewhere. Must not be
  690. * {@code null}.
  691. * @param shallow
  692. * commits indicating the boundary of the history marked with
  693. * {@code have}. Shallow commits have parents but those parents
  694. * are considered not to be already available. Parents of
  695. * {@code shallow} commits and earlier generations will be
  696. * included in the pack if requested by {@code want}. Must not be
  697. * {@code null}.
  698. * @param noBitmaps
  699. * collection of objects to be excluded from bitmap commit
  700. * selection.
  701. * @throws IOException
  702. * an I/O problem occurred while reading objects.
  703. */
  704. public void preparePack(ProgressMonitor countingMonitor,
  705. @NonNull Set<? extends ObjectId> want,
  706. @NonNull Set<? extends ObjectId> have,
  707. @NonNull Set<? extends ObjectId> shallow,
  708. @NonNull Set<? extends ObjectId> noBitmaps) throws IOException {
  709. try (ObjectWalk ow = getObjectWalk()) {
  710. ow.assumeShallow(shallow);
  711. preparePack(countingMonitor, ow, want, have, noBitmaps);
  712. }
  713. }
  714. private ObjectWalk getObjectWalk() {
  715. return shallowPack ? new DepthWalk.ObjectWalk(reader, depth - 1)
  716. : new ObjectWalk(reader);
  717. }
  718. /**
  719. * Prepare the list of objects to be written to the pack stream.
  720. * <p>
  721. * Basing on these 2 sets, another set of objects to put in a pack file is
  722. * created: this set consists of all objects reachable (ancestors) from
  723. * interesting objects, except uninteresting objects and their ancestors.
  724. * This method uses class {@link ObjectWalk} extensively to find out that
  725. * appropriate set of output objects and their optimal order in output pack.
  726. * Order is consistent with general git in-pack rules: sort by object type,
  727. * recency, path and delta-base first.
  728. * </p>
  729. *
  730. * @param countingMonitor
  731. * progress during object enumeration.
  732. * @param walk
  733. * ObjectWalk to perform enumeration.
  734. * @param interestingObjects
  735. * collection of objects to be marked as interesting (start
  736. * points of graph traversal). Must not be {@code null}.
  737. * @param uninterestingObjects
  738. * collection of objects to be marked as uninteresting (end
  739. * points of graph traversal). Pass {@link #NONE} if all objects
  740. * reachable from {@code want} are desired, such as when serving
  741. * a clone.
  742. * @param noBitmaps
  743. * collection of objects to be excluded from bitmap commit
  744. * selection.
  745. * @throws IOException
  746. * when some I/O problem occur during reading objects.
  747. */
  748. public void preparePack(ProgressMonitor countingMonitor,
  749. @NonNull ObjectWalk walk,
  750. @NonNull Set<? extends ObjectId> interestingObjects,
  751. @NonNull Set<? extends ObjectId> uninterestingObjects,
  752. @NonNull Set<? extends ObjectId> noBitmaps)
  753. throws IOException {
  754. if (countingMonitor == null)
  755. countingMonitor = NullProgressMonitor.INSTANCE;
  756. if (shallowPack && !(walk instanceof DepthWalk.ObjectWalk))
  757. throw new IllegalArgumentException(
  758. JGitText.get().shallowPacksRequireDepthWalk);
  759. findObjectsToPack(countingMonitor, walk, interestingObjects,
  760. uninterestingObjects, noBitmaps);
  761. }
  762. /**
  763. * Determine if the pack file will contain the requested object.
  764. *
  765. * @param id
  766. * the object to test the existence of.
  767. * @return true if the object will appear in the output pack file.
  768. * @throws IOException
  769. * a cached pack cannot be examined.
  770. */
  771. public boolean willInclude(final AnyObjectId id) throws IOException {
  772. ObjectToPack obj = objectsMap.get(id);
  773. return obj != null && !obj.isEdge();
  774. }
  775. /**
  776. * Lookup the ObjectToPack object for a given ObjectId.
  777. *
  778. * @param id
  779. * the object to find in the pack.
  780. * @return the object we are packing, or null.
  781. */
  782. public ObjectToPack get(AnyObjectId id) {
  783. ObjectToPack obj = objectsMap.get(id);
  784. return obj != null && !obj.isEdge() ? obj : null;
  785. }
  786. /**
  787. * Computes SHA-1 of lexicographically sorted objects ids written in this
  788. * pack, as used to name a pack file in repository.
  789. *
  790. * @return ObjectId representing SHA-1 name of a pack that was created.
  791. */
  792. public ObjectId computeName() {
  793. final byte[] buf = new byte[OBJECT_ID_LENGTH];
  794. final MessageDigest md = Constants.newMessageDigest();
  795. for (ObjectToPack otp : sortByName()) {
  796. otp.copyRawTo(buf, 0);
  797. md.update(buf, 0, OBJECT_ID_LENGTH);
  798. }
  799. return ObjectId.fromRaw(md.digest());
  800. }
  801. /**
  802. * Returns the index format version that will be written.
  803. * <p>
  804. * This method can only be invoked after
  805. * {@link #writePack(ProgressMonitor, ProgressMonitor, OutputStream)} has
  806. * been invoked and completed successfully.
  807. *
  808. * @return the index format version.
  809. */
  810. public int getIndexVersion() {
  811. int indexVersion = config.getIndexVersion();
  812. if (indexVersion <= 0) {
  813. for (BlockList<ObjectToPack> objs : objectsLists)
  814. indexVersion = Math.max(indexVersion,
  815. PackIndexWriter.oldestPossibleFormat(objs));
  816. }
  817. return indexVersion;
  818. }
  819. /**
  820. * Create an index file to match the pack file just written.
  821. * <p>
  822. * Called after
  823. * {@link #writePack(ProgressMonitor, ProgressMonitor, OutputStream)}.
  824. * <p>
  825. * Writing an index is only required for local pack storage. Packs sent on
  826. * the network do not need to create an index.
  827. *
  828. * @param indexStream
  829. * output for the index data. Caller is responsible for closing
  830. * this stream.
  831. * @throws IOException
  832. * the index data could not be written to the supplied stream.
  833. */
  834. public void writeIndex(final OutputStream indexStream) throws IOException {
  835. if (isIndexDisabled())
  836. throw new IOException(JGitText.get().cachedPacksPreventsIndexCreation);
  837. long writeStart = System.currentTimeMillis();
  838. final PackIndexWriter iw = PackIndexWriter.createVersion(
  839. indexStream, getIndexVersion());
  840. iw.write(sortByName(), packcsum);
  841. stats.timeWriting += System.currentTimeMillis() - writeStart;
  842. }
  843. /**
  844. * Create a bitmap index file to match the pack file just written.
  845. * <p>
  846. * Called after {@link #prepareBitmapIndex(ProgressMonitor)}.
  847. *
  848. * @param bitmapIndexStream
  849. * output for the bitmap index data. Caller is responsible for
  850. * closing this stream.
  851. * @throws IOException
  852. * the index data could not be written to the supplied stream.
  853. */
  854. public void writeBitmapIndex(final OutputStream bitmapIndexStream)
  855. throws IOException {
  856. if (writeBitmaps == null)
  857. throw new IOException(JGitText.get().bitmapsMustBePrepared);
  858. long writeStart = System.currentTimeMillis();
  859. final PackBitmapIndexWriterV1 iw = new PackBitmapIndexWriterV1(bitmapIndexStream);
  860. iw.write(writeBitmaps, packcsum);
  861. stats.timeWriting += System.currentTimeMillis() - writeStart;
  862. }
  863. private List<ObjectToPack> sortByName() {
  864. if (sortedByName == null) {
  865. int cnt = 0;
  866. cnt += objectsLists[OBJ_COMMIT].size();
  867. cnt += objectsLists[OBJ_TREE].size();
  868. cnt += objectsLists[OBJ_BLOB].size();
  869. cnt += objectsLists[OBJ_TAG].size();
  870. sortedByName = new BlockList<>(cnt);
  871. sortedByName.addAll(objectsLists[OBJ_COMMIT]);
  872. sortedByName.addAll(objectsLists[OBJ_TREE]);
  873. sortedByName.addAll(objectsLists[OBJ_BLOB]);
  874. sortedByName.addAll(objectsLists[OBJ_TAG]);
  875. Collections.sort(sortedByName);
  876. }
  877. return sortedByName;
  878. }
  879. private void beginPhase(PackingPhase phase, ProgressMonitor monitor,
  880. long cnt) {
  881. state.phase = phase;
  882. String task;
  883. switch (phase) {
  884. case COUNTING:
  885. task = JGitText.get().countingObjects;
  886. break;
  887. case GETTING_SIZES:
  888. task = JGitText.get().searchForSizes;
  889. break;
  890. case FINDING_SOURCES:
  891. task = JGitText.get().searchForReuse;
  892. break;
  893. case COMPRESSING:
  894. task = JGitText.get().compressingObjects;
  895. break;
  896. case WRITING:
  897. task = JGitText.get().writingObjects;
  898. break;
  899. case BUILDING_BITMAPS:
  900. task = JGitText.get().buildingBitmaps;
  901. break;
  902. default:
  903. throw new IllegalArgumentException(
  904. MessageFormat.format(JGitText.get().illegalPackingPhase, phase));
  905. }
  906. monitor.beginTask(task, (int) cnt);
  907. }
  908. private void endPhase(ProgressMonitor monitor) {
  909. monitor.endTask();
  910. }
  911. /**
  912. * Write the prepared pack to the supplied stream.
  913. * <p>
  914. * Called after
  915. * {@link #preparePack(ProgressMonitor, ObjectWalk, Set, Set, Set)} or
  916. * {@link #preparePack(ProgressMonitor, Set, Set)}.
  917. * <p>
  918. * Performs delta search if enabled and writes the pack stream.
  919. * <p>
  920. * All reused objects data checksum (Adler32/CRC32) is computed and
  921. * validated against existing checksum.
  922. *
  923. * @param compressMonitor
  924. * progress monitor to report object compression work.
  925. * @param writeMonitor
  926. * progress monitor to report the number of objects written.
  927. * @param packStream
  928. * output stream of pack data. The stream should be buffered by
  929. * the caller. The caller is responsible for closing the stream.
  930. * @throws IOException
  931. * an error occurred reading a local object's data to include in
  932. * the pack, or writing compressed object data to the output
  933. * stream.
  934. * @throws WriteAbortedException
  935. * the write operation is aborted by {@link ObjectCountCallback}
  936. * .
  937. */
  938. public void writePack(ProgressMonitor compressMonitor,
  939. ProgressMonitor writeMonitor, OutputStream packStream)
  940. throws IOException {
  941. if (compressMonitor == null)
  942. compressMonitor = NullProgressMonitor.INSTANCE;
  943. if (writeMonitor == null)
  944. writeMonitor = NullProgressMonitor.INSTANCE;
  945. excludeInPacks = null;
  946. excludeInPackLast = null;
  947. boolean needSearchForReuse = reuseSupport != null && (
  948. reuseDeltas
  949. || config.isReuseObjects()
  950. || !cachedPacks.isEmpty());
  951. if (compressMonitor instanceof BatchingProgressMonitor) {
  952. long delay = 1000;
  953. if (needSearchForReuse && config.isDeltaCompress())
  954. delay = 500;
  955. ((BatchingProgressMonitor) compressMonitor).setDelayStart(
  956. delay,
  957. TimeUnit.MILLISECONDS);
  958. }
  959. if (needSearchForReuse)
  960. searchForReuse(compressMonitor);
  961. if (config.isDeltaCompress())
  962. searchForDeltas(compressMonitor);
  963. crc32 = new CRC32();
  964. final PackOutputStream out = new PackOutputStream(
  965. writeMonitor,
  966. isIndexDisabled()
  967. ? packStream
  968. : new CheckedOutputStream(packStream, crc32),
  969. this);
  970. long objCnt = getObjectCount();
  971. stats.totalObjects = objCnt;
  972. if (callback != null)
  973. callback.setObjectCount(objCnt);
  974. beginPhase(PackingPhase.WRITING, writeMonitor, objCnt);
  975. long writeStart = System.currentTimeMillis();
  976. try {
  977. out.writeFileHeader(PACK_VERSION_GENERATED, objCnt);
  978. out.flush();
  979. writeObjects(out);
  980. if (!edgeObjects.isEmpty() || !cachedPacks.isEmpty()) {
  981. for (PackStatistics.ObjectType.Accumulator typeStat : stats.objectTypes) {
  982. if (typeStat == null)
  983. continue;
  984. stats.thinPackBytes += typeStat.bytes;
  985. }
  986. }
  987. stats.reusedPacks = Collections.unmodifiableList(cachedPacks);
  988. for (CachedPack pack : cachedPacks) {
  989. long deltaCnt = pack.getDeltaCount();
  990. stats.reusedObjects += pack.getObjectCount();
  991. stats.reusedDeltas += deltaCnt;
  992. stats.totalDeltas += deltaCnt;
  993. reuseSupport.copyPackAsIs(out, pack);
  994. }
  995. writeChecksum(out);
  996. out.flush();
  997. } finally {
  998. stats.timeWriting = System.currentTimeMillis() - writeStart;
  999. stats.depth = depth;
  1000. for (PackStatistics.ObjectType.Accumulator typeStat : stats.objectTypes) {
  1001. if (typeStat == null)
  1002. continue;
  1003. typeStat.cntDeltas += typeStat.reusedDeltas;
  1004. stats.reusedObjects += typeStat.reusedObjects;
  1005. stats.reusedDeltas += typeStat.reusedDeltas;
  1006. stats.totalDeltas += typeStat.cntDeltas;
  1007. }
  1008. }
  1009. stats.totalBytes = out.length();
  1010. reader.close();
  1011. endPhase(writeMonitor);
  1012. }
  1013. /**
  1014. * @return description of what this PackWriter did in order to create the
  1015. * final pack stream. This should only be invoked after the calls to
  1016. * create the pack/index/bitmap have completed.
  1017. */
  1018. public PackStatistics getStatistics() {
  1019. return new PackStatistics(stats);
  1020. }
  1021. /** @return snapshot of the current state of this PackWriter. */
  1022. public State getState() {
  1023. return state.snapshot();
  1024. }
  1025. /**
  1026. * Release all resources used by this writer.
  1027. */
  1028. @Override
  1029. public void close() {
  1030. reader.close();
  1031. if (myDeflater != null) {
  1032. myDeflater.end();
  1033. myDeflater = null;
  1034. }
  1035. instances.remove(selfRef);
  1036. }
  1037. private void searchForReuse(ProgressMonitor monitor) throws IOException {
  1038. long cnt = 0;
  1039. cnt += objectsLists[OBJ_COMMIT].size();
  1040. cnt += objectsLists[OBJ_TREE].size();
  1041. cnt += objectsLists[OBJ_BLOB].size();
  1042. cnt += objectsLists[OBJ_TAG].size();
  1043. long start = System.currentTimeMillis();
  1044. beginPhase(PackingPhase.FINDING_SOURCES, monitor, cnt);
  1045. if (cnt <= 4096) {
  1046. // For small object counts, do everything as one list.
  1047. BlockList<ObjectToPack> tmp = new BlockList<>((int) cnt);
  1048. tmp.addAll(objectsLists[OBJ_TAG]);
  1049. tmp.addAll(objectsLists[OBJ_COMMIT]);
  1050. tmp.addAll(objectsLists[OBJ_TREE]);
  1051. tmp.addAll(objectsLists[OBJ_BLOB]);
  1052. searchForReuse(monitor, tmp);
  1053. if (pruneCurrentObjectList) {
  1054. // If the list was pruned, we need to re-prune the main lists.
  1055. pruneEdgesFromObjectList(objectsLists[OBJ_COMMIT]);
  1056. pruneEdgesFromObjectList(objectsLists[OBJ_TREE]);
  1057. pruneEdgesFromObjectList(objectsLists[OBJ_BLOB]);
  1058. pruneEdgesFromObjectList(objectsLists[OBJ_TAG]);
  1059. }
  1060. } else {
  1061. searchForReuse(monitor, objectsLists[OBJ_TAG]);
  1062. searchForReuse(monitor, objectsLists[OBJ_COMMIT]);
  1063. searchForReuse(monitor, objectsLists[OBJ_TREE]);
  1064. searchForReuse(monitor, objectsLists[OBJ_BLOB]);
  1065. }
  1066. endPhase(monitor);
  1067. stats.timeSearchingForReuse = System.currentTimeMillis() - start;
  1068. if (config.isReuseDeltas() && config.getCutDeltaChains()) {
  1069. cutDeltaChains(objectsLists[OBJ_TREE]);
  1070. cutDeltaChains(objectsLists[OBJ_BLOB]);
  1071. }
  1072. }
  1073. private void searchForReuse(ProgressMonitor monitor, List<ObjectToPack> list)
  1074. throws IOException, MissingObjectException {
  1075. pruneCurrentObjectList = false;
  1076. reuseSupport.selectObjectRepresentation(this, monitor, list);
  1077. if (pruneCurrentObjectList)
  1078. pruneEdgesFromObjectList(list);
  1079. }
  1080. private void cutDeltaChains(BlockList<ObjectToPack> list)
  1081. throws IOException {
  1082. int max = config.getMaxDeltaDepth();
  1083. for (int idx = list.size() - 1; idx >= 0; idx--) {
  1084. int d = 0;
  1085. ObjectToPack b = list.get(idx).getDeltaBase();
  1086. while (b != null) {
  1087. if (d < b.getChainLength())
  1088. break;
  1089. b.setChainLength(++d);
  1090. if (d >= max && b.isDeltaRepresentation()) {
  1091. reselectNonDelta(b);
  1092. break;
  1093. }
  1094. b = b.getDeltaBase();
  1095. }
  1096. }
  1097. if (config.isDeltaCompress()) {
  1098. for (ObjectToPack otp : list)
  1099. otp.clearChainLength();
  1100. }
  1101. }
  1102. private void searchForDeltas(ProgressMonitor monitor)
  1103. throws MissingObjectException, IncorrectObjectTypeException,
  1104. IOException {
  1105. // Commits and annotated tags tend to have too many differences to
  1106. // really benefit from delta compression. Consequently just don't
  1107. // bother examining those types here.
  1108. //
  1109. ObjectToPack[] list = new ObjectToPack[
  1110. objectsLists[OBJ_TREE].size()
  1111. + objectsLists[OBJ_BLOB].size()
  1112. + edgeObjects.size()];
  1113. int cnt = 0;
  1114. cnt = findObjectsNeedingDelta(list, cnt, OBJ_TREE);
  1115. cnt = findObjectsNeedingDelta(list, cnt, OBJ_BLOB);
  1116. if (cnt == 0)
  1117. return;
  1118. int nonEdgeCnt = cnt;
  1119. // Queue up any edge objects that we might delta against. We won't
  1120. // be sending these as we assume the other side has them, but we need
  1121. // them in the search phase below.
  1122. //
  1123. for (ObjectToPack eo : edgeObjects) {
  1124. eo.setWeight(0);
  1125. list[cnt++] = eo;
  1126. }
  1127. // Compute the sizes of the objects so we can do a proper sort.
  1128. // We let the reader skip missing objects if it chooses. For
  1129. // some readers this can be a huge win. We detect missing objects
  1130. // by having set the weights above to 0 and allowing the delta
  1131. // search code to discover the missing object and skip over it, or
  1132. // abort with an exception if we actually had to have it.
  1133. //
  1134. final long sizingStart = System.currentTimeMillis();
  1135. beginPhase(PackingPhase.GETTING_SIZES, monitor, cnt);
  1136. AsyncObjectSizeQueue<ObjectToPack> sizeQueue = reader.getObjectSize(
  1137. Arrays.<ObjectToPack> asList(list).subList(0, cnt), false);
  1138. try {
  1139. final long limit = Math.min(
  1140. config.getBigFileThreshold(),
  1141. Integer.MAX_VALUE);
  1142. for (;;) {
  1143. try {
  1144. if (!sizeQueue.next())
  1145. break;
  1146. } catch (MissingObjectException notFound) {
  1147. monitor.update(1);
  1148. if (ignoreMissingUninteresting) {
  1149. ObjectToPack otp = sizeQueue.getCurrent();
  1150. if (otp != null && otp.isEdge()) {
  1151. otp.setDoNotDelta();
  1152. continue;
  1153. }
  1154. otp = objectsMap.get(notFound.getObjectId());
  1155. if (otp != null && otp.isEdge()) {
  1156. otp.setDoNotDelta();
  1157. continue;
  1158. }
  1159. }
  1160. throw notFound;
  1161. }
  1162. ObjectToPack otp = sizeQueue.getCurrent();
  1163. if (otp == null)
  1164. otp = objectsMap.get(sizeQueue.getObjectId());
  1165. long sz = sizeQueue.getSize();
  1166. if (DeltaIndex.BLKSZ < sz && sz < limit)
  1167. otp.setWeight((int) sz);
  1168. else
  1169. otp.setDoNotDelta(); // too small, or too big
  1170. monitor.update(1);
  1171. }
  1172. } finally {
  1173. sizeQueue.release();
  1174. }
  1175. endPhase(monitor);
  1176. stats.timeSearchingForSizes = System.currentTimeMillis() - sizingStart;
  1177. // Sort the objects by path hash so like files are near each other,
  1178. // and then by size descending so that bigger files are first. This
  1179. // applies "Linus' Law" which states that newer files tend to be the
  1180. // bigger ones, because source files grow and hardly ever shrink.
  1181. //
  1182. Arrays.sort(list, 0, cnt, new Comparator<ObjectToPack>() {
  1183. @Override
  1184. public int compare(ObjectToPack a, ObjectToPack b) {
  1185. int cmp = (a.isDoNotDelta() ? 1 : 0)
  1186. - (b.isDoNotDelta() ? 1 : 0);
  1187. if (cmp != 0)
  1188. return cmp;
  1189. cmp = a.getType() - b.getType();
  1190. if (cmp != 0)
  1191. return cmp;
  1192. cmp = (a.getPathHash() >>> 1) - (b.getPathHash() >>> 1);
  1193. if (cmp != 0)
  1194. return cmp;
  1195. cmp = (a.getPathHash() & 1) - (b.getPathHash() & 1);
  1196. if (cmp != 0)
  1197. return cmp;
  1198. cmp = (a.isEdge() ? 0 : 1) - (b.isEdge() ? 0 : 1);
  1199. if (cmp != 0)
  1200. return cmp;
  1201. return b.getWeight() - a.getWeight();
  1202. }
  1203. });
  1204. // Above we stored the objects we cannot delta onto the end.
  1205. // Remove them from the list so we don't waste time on them.
  1206. while (0 < cnt && list[cnt - 1].isDoNotDelta()) {
  1207. if (!list[cnt - 1].isEdge())
  1208. nonEdgeCnt--;
  1209. cnt--;
  1210. }
  1211. if (cnt == 0)
  1212. return;
  1213. final long searchStart = System.currentTimeMillis();
  1214. searchForDeltas(monitor, list, cnt);
  1215. stats.deltaSearchNonEdgeObjects = nonEdgeCnt;
  1216. stats.timeCompressing = System.currentTimeMillis() - searchStart;
  1217. for (int i = 0; i < cnt; i++)
  1218. if (!list[i].isEdge() && list[i].isDeltaRepresentation())
  1219. stats.deltasFound++;
  1220. }
  1221. private int findObjectsNeedingDelta(ObjectToPack[] list, int cnt, int type) {
  1222. for (ObjectToPack otp : objectsLists[type]) {
  1223. if (otp.isDoNotDelta()) // delta is disabled for this path
  1224. continue;
  1225. if (otp.isDeltaRepresentation()) // already reusing a delta
  1226. continue;
  1227. otp.setWeight(0);
  1228. list[cnt++] = otp;
  1229. }
  1230. return cnt;
  1231. }
  1232. private void reselectNonDelta(ObjectToPack otp) throws IOException {
  1233. otp.clearDeltaBase();
  1234. otp.clearReuseAsIs();
  1235. boolean old = reuseDeltas;
  1236. reuseDeltas = false;
  1237. reuseSupport.selectObjectRepresentation(this,
  1238. NullProgressMonitor.INSTANCE,
  1239. Collections.singleton(otp));
  1240. reuseDeltas = old;
  1241. }
  1242. private void searchForDeltas(final ProgressMonitor monitor,
  1243. final ObjectToPack[] list, final int cnt)
  1244. throws MissingObjectException, IncorrectObjectTypeException,
  1245. LargeObjectException, IOException {
  1246. int threads = config.getThreads();
  1247. if (threads == 0)
  1248. threads = Runtime.getRuntime().availableProcessors();
  1249. if (threads <= 1 || cnt <= config.getDeltaSearchWindowSize())
  1250. singleThreadDeltaSearch(monitor, list, cnt);
  1251. else
  1252. parallelDeltaSearch(monitor, list, cnt, threads);
  1253. }
  1254. private void singleThreadDeltaSearch(ProgressMonitor monitor,
  1255. ObjectToPack[] list, int cnt) throws IOException {
  1256. long totalWeight = 0;
  1257. for (int i = 0; i < cnt; i++) {
  1258. ObjectToPack o = list[i];
  1259. totalWeight += DeltaTask.getAdjustedWeight(o);
  1260. }
  1261. long bytesPerUnit = 1;
  1262. while (DeltaTask.MAX_METER <= (totalWeight / bytesPerUnit))
  1263. bytesPerUnit <<= 10;
  1264. int cost = (int) (totalWeight / bytesPerUnit);
  1265. if (totalWeight % bytesPerUnit != 0)
  1266. cost++;
  1267. beginPhase(PackingPhase.COMPRESSING, monitor, cost);
  1268. new DeltaWindow(config, new DeltaCache(config), reader,
  1269. monitor, bytesPerUnit,
  1270. list, 0, cnt).search();
  1271. endPhase(monitor);
  1272. }
  1273. private void parallelDeltaSearch(ProgressMonitor monitor,
  1274. ObjectToPack[] list, int cnt, int threads) throws IOException {
  1275. DeltaCache dc = new ThreadSafeDeltaCache(config);
  1276. ThreadSafeProgressMonitor pm = new ThreadSafeProgressMonitor(monitor);
  1277. DeltaTask.Block taskBlock = new DeltaTask.Block(threads, config,
  1278. reader, dc, pm,
  1279. list, 0, cnt);
  1280. taskBlock.partitionTasks();
  1281. beginPhase(PackingPhase.COMPRESSING, monitor, taskBlock.cost());
  1282. pm.startWorkers(taskBlock.tasks.size());
  1283. Executor executor = config.getExecutor();
  1284. final List<Throwable> errors =
  1285. Collections.synchronizedList(new ArrayList<Throwable>(threads));
  1286. if (executor instanceof ExecutorService) {
  1287. // Caller supplied us a service, use it directly.
  1288. runTasks((ExecutorService) executor, pm, taskBlock, errors);
  1289. } else if (executor == null) {
  1290. // Caller didn't give us a way to run the tasks, spawn up a
  1291. // temporary thread pool and make sure it tears down cleanly.
  1292. ExecutorService pool = Executors.newFixedThreadPool(threads);
  1293. try {
  1294. runTasks(pool, pm, taskBlock, errors);
  1295. } finally {
  1296. pool.shutdown();
  1297. for (;;) {
  1298. try {
  1299. if (pool.awaitTermination(60, TimeUnit.SECONDS))
  1300. break;
  1301. } catch (InterruptedException e) {
  1302. throw new IOException(
  1303. JGitText.get().packingCancelledDuringObjectsWriting);
  1304. }
  1305. }
  1306. }
  1307. } else {
  1308. // The caller gave us an executor, but it might not do
  1309. // asynchronous execution. Wrap everything and hope it
  1310. // can schedule these for us.
  1311. for (final DeltaTask task : taskBlock.tasks) {
  1312. executor.execute(new Runnable() {
  1313. @Override
  1314. public void run() {
  1315. try {
  1316. task.call();
  1317. } catch (Throwable failure) {
  1318. errors.add(failure);
  1319. }
  1320. }
  1321. });
  1322. }
  1323. try {
  1324. pm.waitForCompletion();
  1325. } catch (InterruptedException ie) {
  1326. // We can't abort the other tasks as we have no handle.
  1327. // Cross our fingers and just break out anyway.
  1328. //
  1329. throw new IOException(
  1330. JGitText.get().packingCancelledDuringObjectsWriting);
  1331. }
  1332. }
  1333. // If any task threw an error, try to report it back as
  1334. // though we weren't using a threaded search algorithm.
  1335. //
  1336. if (!errors.isEmpty()) {
  1337. Throwable err = errors.get(0);
  1338. if (err instanceof Error)
  1339. throw (Error) err;
  1340. if (err instanceof RuntimeException)
  1341. throw (RuntimeException) err;
  1342. if (err instanceof IOException)
  1343. throw (IOException) err;
  1344. IOException fail = new IOException(err.getMessage());
  1345. fail.initCause(err);
  1346. throw fail;
  1347. }
  1348. endPhase(monitor);
  1349. }
  1350. private static void runTasks(ExecutorService pool,
  1351. ThreadSafeProgressMonitor pm,
  1352. DeltaTask.Block tb, List<Throwable> errors) throws IOException {
  1353. List<Future<?>> futures = new ArrayList<>(tb.tasks.size());
  1354. for (DeltaTask task : tb.tasks)
  1355. futures.add(pool.submit(task));
  1356. try {
  1357. pm.waitForCompletion();
  1358. for (Future<?> f : futures) {
  1359. try {
  1360. f.get();
  1361. } catch (ExecutionException failed) {
  1362. errors.add(failed.getCause());
  1363. }
  1364. }
  1365. } catch (InterruptedException ie) {
  1366. for (Future<?> f : futures)
  1367. f.cancel(true);
  1368. throw new IOException(
  1369. JGitText.get().packingCancelledDuringObjectsWriting);
  1370. }
  1371. }
  1372. private void writeObjects(PackOutputStream out) throws IOException {
  1373. writeObjects(out, objectsLists[OBJ_COMMIT]);
  1374. writeObjects(out, objectsLists[OBJ_TAG]);
  1375. writeObjects(out, objectsLists[OBJ_TREE]);
  1376. writeObjects(out, objectsLists[OBJ_BLOB]);
  1377. }
  1378. private void writeObjects(PackOutputStream out, List<ObjectToPack> list)
  1379. throws IOException {
  1380. if (list.isEmpty())
  1381. return;
  1382. typeStats = stats.objectTypes[list.get(0).getType()];
  1383. long beginOffset = out.length();
  1384. if (reuseSupport != null) {
  1385. reuseSupport.writeObjects(out, list);
  1386. } else {
  1387. for (ObjectToPack otp : list)
  1388. out.writeObject(otp);
  1389. }
  1390. typeStats.bytes += out.length() - beginOffset;
  1391. typeStats.cntObjects = list.size();
  1392. }
  1393. void writeObject(PackOutputStream out, ObjectToPack otp) throws IOException {
  1394. if (!otp.isWritten())
  1395. writeObjectImpl(out, otp);
  1396. }
  1397. private void writeObjectImpl(PackOutputStream out, ObjectToPack otp)
  1398. throws IOException {
  1399. if (otp.wantWrite()) {
  1400. // A cycle exists in this delta chain. This should only occur if a
  1401. // selected object representation disappeared during writing
  1402. // (for example due to a concurrent repack) and a different base
  1403. // was chosen, forcing a cycle. Select something other than a
  1404. // delta, and write this object.
  1405. reselectNonDelta(otp);
  1406. }
  1407. otp.markWantWrite();
  1408. while (otp.isReuseAsIs()) {
  1409. writeBase(out, otp.getDeltaBase());
  1410. if (otp.isWritten())
  1411. return; // Delta chain cycle caused this to write already.
  1412. crc32.reset();
  1413. otp.setOffset(out.length());
  1414. try {
  1415. reuseSupport.copyObjectAsIs(out, otp, reuseValidate);
  1416. out.endObject();
  1417. otp.setCRC((int) crc32.getValue());
  1418. typeStats.reusedObjects++;
  1419. if (otp.isDeltaRepresentation()) {
  1420. typeStats.reusedDeltas++;
  1421. typeStats.deltaBytes += out.length() - otp.getOffset();
  1422. }
  1423. return;
  1424. } catch (StoredObjectRepresentationNotAvailableException gone) {
  1425. if (otp.getOffset() == out.length()) {
  1426. otp.setOffset(0);
  1427. otp.clearDeltaBase();
  1428. otp.clearReuseAsIs();
  1429. reuseSupport.selectObjectRepresentation(this,
  1430. NullProgressMonitor.INSTANCE,
  1431. Collections.singleton(otp));
  1432. continue;
  1433. } else {
  1434. // Object writing already started, we cannot recover.
  1435. //
  1436. CorruptObjectException coe;
  1437. coe = new CorruptObjectException(otp, ""); //$NON-NLS-1$
  1438. coe.initCause(gone);
  1439. throw coe;
  1440. }
  1441. }
  1442. }
  1443. // If we reached here, reuse wasn't possible.
  1444. //
  1445. if (otp.isDeltaRepresentation())
  1446. writeDeltaObjectDeflate(out, otp);
  1447. else
  1448. writeWholeObjectDeflate(out, otp);
  1449. out.endObject();
  1450. otp.setCRC((int) crc32.getValue());
  1451. }
  1452. private void writeBase(PackOutputStream out, ObjectToPack base)
  1453. throws IOException {
  1454. if (base != null && !base.isWritten() && !base.isEdge())
  1455. writeObjectImpl(out, base);
  1456. }
  1457. private void writeWholeObjectDeflate(PackOutputStream out,
  1458. final ObjectToPack otp) throws IOException {
  1459. final Deflater deflater = deflater();
  1460. final ObjectLoader ldr = reader.open(otp, otp.getType());
  1461. crc32.reset();
  1462. otp.setOffset(out.length());
  1463. out.writeHeader(otp, ldr.getSize());
  1464. deflater.reset();
  1465. DeflaterOutputStream dst = new DeflaterOutputStream(out, deflater);
  1466. ldr.copyTo(dst);
  1467. dst.finish();
  1468. }
  1469. private void writeDeltaObjectDeflate(PackOutputStream out,
  1470. final ObjectToPack otp) throws IOException {
  1471. writeBase(out, otp.getDeltaBase());
  1472. crc32.reset();
  1473. otp.setOffset(out.length());
  1474. DeltaCache.Ref ref = otp.popCachedDelta();
  1475. if (ref != null) {
  1476. byte[] zbuf = ref.get();
  1477. if (zbuf != null) {
  1478. out.writeHeader(otp, otp.getCachedSize());
  1479. out.write(zbuf);
  1480. typeStats.cntDeltas++;
  1481. typeStats.deltaBytes += out.length() - otp.getOffset();
  1482. return;
  1483. }
  1484. }
  1485. try (TemporaryBuffer.Heap delta = delta(otp)) {
  1486. out.writeHeader(otp, delta.length());
  1487. Deflater deflater = deflater();
  1488. deflater.reset();
  1489. DeflaterOutputStream dst = new DeflaterOutputStream(out, deflater);
  1490. delta.writeTo(dst, null);
  1491. dst.finish();
  1492. }
  1493. typeStats.cntDeltas++;
  1494. typeStats.deltaBytes += out.length() - otp.getOffset();
  1495. }
  1496. private TemporaryBuffer.Heap delta(final ObjectToPack otp)
  1497. throws IOException {
  1498. DeltaIndex index = new DeltaIndex(buffer(otp.getDeltaBaseId()));
  1499. byte[] res = buffer(otp);
  1500. // We never would have proposed this pair if the delta would be
  1501. // larger than the unpacked version of the object. So using it
  1502. // as our buffer limit is valid: we will never reach it.
  1503. //
  1504. TemporaryBuffer.Heap delta = new TemporaryBuffer.Heap(res.length);
  1505. index.encode(delta, res);
  1506. return delta;
  1507. }
  1508. private byte[] buffer(AnyObjectId objId) throws IOException {
  1509. return buffer(config, reader, objId);
  1510. }
  1511. static byte[] buffer(PackConfig config, ObjectReader or, AnyObjectId objId)
  1512. throws IOException {
  1513. // PackWriter should have already pruned objects that
  1514. // are above the big file threshold, so our chances of
  1515. // the object being below it are very good. We really
  1516. // shouldn't be here, unless the implementation is odd.
  1517. return or.open(objId).getCachedBytes(config.getBigFileThreshold());
  1518. }
  1519. private Deflater deflater() {
  1520. if (myDeflater == null)
  1521. myDeflater = new Deflater(config.getCompressionLevel());
  1522. return myDeflater;
  1523. }
  1524. private void writeChecksum(PackOutputStream out) throws IOException {
  1525. packcsum = out.getDigest();
  1526. out.write(packcsum);
  1527. }
  1528. private void findObjectsToPack(@NonNull ProgressMonitor countingMonitor,
  1529. @NonNull ObjectWalk walker, @NonNull Set<? extends ObjectId> want,
  1530. @NonNull Set<? extends ObjectId> have,
  1531. @NonNull Set<? extends ObjectId> noBitmaps) throws IOException {
  1532. final long countingStart = System.currentTimeMillis();
  1533. beginPhase(PackingPhase.COUNTING, countingMonitor, ProgressMonitor.UNKNOWN);
  1534. stats.interestingObjects = Collections.unmodifiableSet(new HashSet<ObjectId>(want));
  1535. stats.uninterestingObjects = Collections.unmodifiableSet(new HashSet<ObjectId>(have));
  1536. excludeFromBitmapSelection = noBitmaps;
  1537. canBuildBitmaps = config.isBuildBitmaps()
  1538. && !shallowPack
  1539. && have.isEmpty()
  1540. && (excludeInPacks == null || excludeInPacks.length == 0);
  1541. if (!shallowPack && useBitmaps) {
  1542. BitmapIndex bitmapIndex = reader.getBitmapIndex();
  1543. if (bitmapIndex != null) {
  1544. PackWriterBitmapWalker bitmapWalker = new PackWriterBitmapWalker(
  1545. walker, bitmapIndex, countingMonitor);
  1546. findObjectsToPackUsingBitmaps(bitmapWalker, want, have);
  1547. endPhase(countingMonitor);
  1548. stats.timeCounting = System.currentTimeMillis() - countingStart;
  1549. stats.bitmapIndexMisses = bitmapWalker.getCountOfBitmapIndexMisses();
  1550. return;
  1551. }
  1552. }
  1553. List<ObjectId> all = new ArrayList<>(want.size() + have.size());
  1554. all.addAll(want);
  1555. all.addAll(have);
  1556. final RevFlag include = walker.newFlag("include"); //$NON-NLS-1$
  1557. final RevFlag added = walker.newFlag("added"); //$NON-NLS-1$
  1558. walker.carry(include);
  1559. int haveEst = have.size();
  1560. if (have.isEmpty()) {
  1561. walker.sort(RevSort.COMMIT_TIME_DESC);
  1562. } else {
  1563. walker.sort(RevSort.TOPO);
  1564. if (thin)
  1565. walker.sort(RevSort.BOUNDARY, true);
  1566. }
  1567. List<RevObject> wantObjs = new ArrayList<>(want.size());
  1568. List<RevObject> haveObjs = new ArrayList<>(haveEst);
  1569. List<RevTag> wantTags = new ArrayList<>(want.size());
  1570. // Retrieve the RevWalk's versions of "want" and "have" objects to
  1571. // maintain any state previously set in the RevWalk.
  1572. AsyncRevObjectQueue q = walker.parseAny(all, true);
  1573. try {
  1574. for (;;) {
  1575. try {
  1576. RevObject o = q.next();
  1577. if (o == null)
  1578. break;
  1579. if (have.contains(o))
  1580. haveObjs.add(o);
  1581. if (want.contains(o)) {
  1582. o.add(include);
  1583. wantObjs.add(o);
  1584. if (o instanceof RevTag)
  1585. wantTags.add((RevTag) o);
  1586. }
  1587. } catch (MissingObjectException e) {
  1588. if (ignoreMissingUninteresting
  1589. && have.contains(e.getObjectId()))
  1590. continue;
  1591. throw e;
  1592. }
  1593. }
  1594. } finally {
  1595. q.release();
  1596. }
  1597. if (!wantTags.isEmpty()) {
  1598. all = new ArrayList<>(wantTags.size());
  1599. for (RevTag tag : wantTags)
  1600. all.add(tag.getObject());
  1601. q = walker.parseAny(all, true);
  1602. try {
  1603. while (q.next() != null) {
  1604. // Just need to pop the queue item to parse the object.
  1605. }
  1606. } finally {
  1607. q.release();
  1608. }
  1609. }
  1610. if (walker instanceof DepthWalk.ObjectWalk) {
  1611. DepthWalk.ObjectWalk depthWalk = (DepthWalk.ObjectWalk) walker;
  1612. for (RevObject obj : wantObjs) {
  1613. depthWalk.markRoot(obj);
  1614. }
  1615. // Mark the tree objects associated with "have" commits as
  1616. // uninteresting to avoid writing redundant blobs. A normal RevWalk
  1617. // lazily propagates the "uninteresting" state from a commit to its
  1618. // tree during the walk, but DepthWalks can terminate early so
  1619. // preemptively propagate that state here.
  1620. for (RevObject obj : haveObjs) {
  1621. if (obj instanceof RevCommit) {
  1622. RevTree t = ((RevCommit) obj).getTree();
  1623. depthWalk.markUninteresting(t);
  1624. }
  1625. }
  1626. if (unshallowObjects != null) {
  1627. for (ObjectId id : unshallowObjects) {
  1628. depthWalk.markUnshallow(walker.parseAny(id));
  1629. }
  1630. }
  1631. } else {
  1632. for (RevObject obj : wantObjs)
  1633. walker.markStart(obj);
  1634. }
  1635. for (RevObject obj : haveObjs)
  1636. walker.markUninteresting(obj);
  1637. final int maxBases = config.getDeltaSearchWindowSize();
  1638. Set<RevTree> baseTrees = new HashSet<>();
  1639. BlockList<RevCommit> commits = new BlockList<>();
  1640. Set<ObjectId> roots = new HashSet<>();
  1641. RevCommit c;
  1642. while ((c = walker.next()) != null) {
  1643. if (exclude(c))
  1644. continue;
  1645. if (c.has(RevFlag.UNINTERESTING)) {
  1646. if (baseTrees.size() <= maxBases)
  1647. baseTrees.add(c.getTree());
  1648. continue;
  1649. }
  1650. commits.add(c);
  1651. if (c.getParentCount() == 0) {
  1652. roots.add(c.copy());
  1653. }
  1654. countingMonitor.update(1);
  1655. }
  1656. stats.rootCommits = Collections.unmodifiableSet(roots);
  1657. if (shallowPack) {
  1658. for (RevCommit cmit : commits) {
  1659. addObject(cmit, 0);
  1660. }
  1661. } else {
  1662. int commitCnt = 0;
  1663. boolean putTagTargets = false;
  1664. for (RevCommit cmit : commits) {
  1665. if (!cmit.has(added)) {
  1666. cmit.add(added);
  1667. addObject(cmit, 0);
  1668. commitCnt++;
  1669. }
  1670. for (int i = 0; i < cmit.getParentCount(); i++) {
  1671. RevCommit p = cmit.getParent(i);
  1672. if (!p.has(added) && !p.has(RevFlag.UNINTERESTING)
  1673. && !exclude(p)) {
  1674. p.add(added);
  1675. addObject(p, 0);
  1676. commitCnt++;
  1677. }
  1678. }
  1679. if (!putTagTargets && 4096 < commitCnt) {
  1680. for (ObjectId id : tagTargets) {
  1681. RevObject obj = walker.lookupOrNull(id);
  1682. if (obj instanceof RevCommit
  1683. && obj.has(include)
  1684. && !obj.has(RevFlag.UNINTERESTING)
  1685. && !obj.has(added)) {
  1686. obj.add(added);
  1687. addObject(obj, 0);
  1688. }
  1689. }
  1690. putTagTargets = true;
  1691. }
  1692. }
  1693. }
  1694. commits = null;
  1695. if (thin && !baseTrees.isEmpty()) {
  1696. BaseSearch bases = new BaseSearch(countingMonitor, baseTrees, //
  1697. objectsMap, edgeObjects, reader);
  1698. RevObject o;
  1699. while ((o = walker.nextObject()) != null) {
  1700. if (o.has(RevFlag.UNINTERESTING))
  1701. continue;
  1702. if (exclude(o))
  1703. continue;
  1704. int pathHash = walker.getPathHashCode();
  1705. byte[] pathBuf = walker.getPathBuffer();
  1706. int pathLen = walker.getPathLength();
  1707. bases.addBase(o.getType(), pathBuf, pathLen, pathHash);
  1708. addObject(o, pathHash);
  1709. countingMonitor.update(1);
  1710. }
  1711. } else {
  1712. RevObject o;
  1713. while ((o = walker.nextObject()) != null) {
  1714. if (o.has(RevFlag.UNINTERESTING))
  1715. continue;
  1716. if (exclude(o))
  1717. continue;
  1718. addObject(o, walker.getPathHashCode());
  1719. countingMonitor.update(1);
  1720. }
  1721. }
  1722. for (CachedPack pack : cachedPacks)
  1723. countingMonitor.update((int) pack.getObjectCount());
  1724. endPhase(countingMonitor);
  1725. stats.timeCounting = System.currentTimeMillis() - countingStart;
  1726. stats.bitmapIndexMisses = -1;
  1727. }
  1728. private void findObjectsToPackUsingBitmaps(
  1729. PackWriterBitmapWalker bitmapWalker, Set<? extends ObjectId> want,
  1730. Set<? extends ObjectId> have)
  1731. throws MissingObjectException, IncorrectObjectTypeException,
  1732. IOException {
  1733. BitmapBuilder haveBitmap = bitmapWalker.findObjects(have, null, true);
  1734. BitmapBuilder wantBitmap = bitmapWalker.findObjects(want, haveBitmap,
  1735. false);
  1736. BitmapBuilder needBitmap = wantBitmap.andNot(haveBitmap);
  1737. if (useCachedPacks && reuseSupport != null && !reuseValidate
  1738. && (excludeInPacks == null || excludeInPacks.length == 0))
  1739. cachedPacks.addAll(
  1740. reuseSupport.getCachedPacksAndUpdate(needBitmap));
  1741. for (BitmapObject obj : needBitmap) {
  1742. ObjectId objectId = obj.getObjectId();
  1743. if (exclude(objectId)) {
  1744. needBitmap.remove(objectId);
  1745. continue;
  1746. }
  1747. addObject(objectId, obj.getType(), 0);
  1748. }
  1749. if (thin)
  1750. haveObjects = haveBitmap;
  1751. }
  1752. private static void pruneEdgesFromObjectList(List<ObjectToPack> list) {
  1753. final int size = list.size();
  1754. int src = 0;
  1755. int dst = 0;
  1756. for (; src < size; src++) {
  1757. ObjectToPack obj = list.get(src);
  1758. if (obj.isEdge())
  1759. continue;
  1760. if (dst != src)
  1761. list.set(dst, obj);
  1762. dst++;
  1763. }
  1764. while (dst < list.size())
  1765. list.remove(list.size() - 1);
  1766. }
  1767. /**
  1768. * Include one object to the output file.
  1769. * <p>
  1770. * Objects are written in the order they are added. If the same object is
  1771. * added twice, it may be written twice, creating a larger than necessary
  1772. * file.
  1773. *
  1774. * @param object
  1775. * the object to add.
  1776. * @throws IncorrectObjectTypeException
  1777. * the object is an unsupported type.
  1778. */
  1779. public void addObject(final RevObject object)
  1780. throws IncorrectObjectTypeException {
  1781. if (!exclude(object))
  1782. addObject(object, 0);
  1783. }
  1784. private void addObject(final RevObject object, final int pathHashCode) {
  1785. addObject(object, object.getType(), pathHashCode);
  1786. }
  1787. private void addObject(
  1788. final AnyObjectId src, final int type, final int pathHashCode) {
  1789. final ObjectToPack otp;
  1790. if (reuseSupport != null)
  1791. otp = reuseSupport.newObjectToPack(src, type);
  1792. else
  1793. otp = new ObjectToPack(src, type);
  1794. otp.setPathHash(pathHashCode);
  1795. objectsLists[type].add(otp);
  1796. objectsMap.add(otp);
  1797. }
  1798. private boolean exclude(AnyObjectId objectId) {
  1799. if (excludeInPacks == null)
  1800. return false;
  1801. if (excludeInPackLast.contains(objectId))
  1802. return true;
  1803. for (ObjectIdSet idx : excludeInPacks) {
  1804. if (idx.contains(objectId)) {
  1805. excludeInPackLast = idx;
  1806. return true;
  1807. }
  1808. }
  1809. return false;
  1810. }
  1811. /**
  1812. * Select an object representation for this writer.
  1813. * <p>
  1814. * An {@link ObjectReader} implementation should invoke this method once for
  1815. * each representation available for an object, to allow the writer to find
  1816. * the most suitable one for the output.
  1817. *
  1818. * @param otp
  1819. * the object being packed.
  1820. * @param next
  1821. * the next available representation from the repository.
  1822. */
  1823. public void select(ObjectToPack otp, StoredObjectRepresentation next) {
  1824. int nFmt = next.getFormat();
  1825. if (!cachedPacks.isEmpty()) {
  1826. if (otp.isEdge())
  1827. return;
  1828. if ((nFmt == PACK_WHOLE) | (nFmt == PACK_DELTA)) {
  1829. for (CachedPack pack : cachedPacks) {
  1830. if (pack.hasObject(otp, next)) {
  1831. otp.setEdge();
  1832. otp.clearDeltaBase();
  1833. otp.clearReuseAsIs();
  1834. pruneCurrentObjectList = true;
  1835. return;
  1836. }
  1837. }
  1838. }
  1839. }
  1840. if (nFmt == PACK_DELTA && reuseDeltas && reuseDeltaFor(otp)) {
  1841. ObjectId baseId = next.getDeltaBase();
  1842. ObjectToPack ptr = objectsMap.get(baseId);
  1843. if (ptr != null && !ptr.isEdge()) {
  1844. otp.setDeltaBase(ptr);
  1845. otp.setReuseAsIs();
  1846. } else if (thin && have(ptr, baseId)) {
  1847. otp.setDeltaBase(baseId);
  1848. otp.setReuseAsIs();
  1849. } else {
  1850. otp.clearDeltaBase();
  1851. otp.clearReuseAsIs();
  1852. }
  1853. } else if (nFmt == PACK_WHOLE && config.isReuseObjects()) {
  1854. int nWeight = next.getWeight();
  1855. if (otp.isReuseAsIs() && !otp.isDeltaRepresentation()) {
  1856. // We've chosen another PACK_WHOLE format for this object,
  1857. // choose the one that has the smaller compressed size.
  1858. //
  1859. if (otp.getWeight() <= nWeight)
  1860. return;
  1861. }
  1862. otp.clearDeltaBase();
  1863. otp.setReuseAsIs();
  1864. otp.setWeight(nWeight);
  1865. } else {
  1866. otp.clearDeltaBase();
  1867. otp.clearReuseAsIs();
  1868. }
  1869. otp.setDeltaAttempted(reuseDeltas & next.wasDeltaAttempted());
  1870. otp.select(next);
  1871. }
  1872. private final boolean have(ObjectToPack ptr, AnyObjectId objectId) {
  1873. return (ptr != null && ptr.isEdge())
  1874. || (haveObjects != null && haveObjects.contains(objectId));
  1875. }
  1876. /**
  1877. * Prepares the bitmaps to be written to the bitmap index file.
  1878. * <p>
  1879. * Bitmaps can be used to speed up fetches and clones by storing the entire
  1880. * object graph at selected commits. Writing a bitmap index is an optional
  1881. * feature that not all pack users may require.
  1882. * <p>
  1883. * Called after {@link #writeIndex(OutputStream)}.
  1884. * <p>
  1885. * To reduce memory internal state is cleared during this method, rendering
  1886. * the PackWriter instance useless for anything further than a call to write
  1887. * out the new bitmaps with {@link #writeBitmapIndex(OutputStream)}.
  1888. *
  1889. * @param pm
  1890. * progress monitor to report bitmap building work.
  1891. * @return whether a bitmap index may be written.
  1892. * @throws IOException
  1893. * when some I/O problem occur during reading objects.
  1894. */
  1895. public boolean prepareBitmapIndex(ProgressMonitor pm) throws IOException {
  1896. if (!canBuildBitmaps || getObjectCount() > Integer.MAX_VALUE
  1897. || !cachedPacks.isEmpty())
  1898. return false;
  1899. if (pm == null)
  1900. pm = NullProgressMonitor.INSTANCE;
  1901. int numCommits = objectsLists[OBJ_COMMIT].size();
  1902. List<ObjectToPack> byName = sortByName();
  1903. sortedByName = null;
  1904. objectsLists = null;
  1905. objectsMap = null;
  1906. writeBitmaps = new PackBitmapIndexBuilder(byName);
  1907. byName = null;
  1908. PackWriterBitmapPreparer bitmapPreparer = new PackWriterBitmapPreparer(
  1909. reader, writeBitmaps, pm, stats.interestingObjects, config);
  1910. Collection<PackWriterBitmapPreparer.BitmapCommit> selectedCommits = bitmapPreparer
  1911. .selectCommits(numCommits, excludeFromBitmapSelection);
  1912. beginPhase(PackingPhase.BUILDING_BITMAPS, pm, selectedCommits.size());
  1913. PackWriterBitmapWalker walker = bitmapPreparer.newBitmapWalker();
  1914. AnyObjectId last = null;
  1915. for (PackWriterBitmapPreparer.BitmapCommit cmit : selectedCommits) {
  1916. if (!cmit.isReuseWalker()) {
  1917. walker = bitmapPreparer.newBitmapWalker();
  1918. }
  1919. BitmapBuilder bitmap = walker.findObjects(
  1920. Collections.singleton(cmit), null, false);
  1921. if (last != null && cmit.isReuseWalker() && !bitmap.contains(last))
  1922. throw new IllegalStateException(MessageFormat.format(
  1923. JGitText.get().bitmapMissingObject, cmit.name(),
  1924. last.name()));
  1925. last = cmit;
  1926. writeBitmaps.addBitmap(cmit, bitmap.build(), cmit.getFlags());
  1927. pm.update(1);
  1928. }
  1929. endPhase(pm);
  1930. return true;
  1931. }
  1932. private boolean reuseDeltaFor(ObjectToPack otp) {
  1933. int type = otp.getType();
  1934. if ((type & 2) != 0) // OBJ_TREE(2) or OBJ_BLOB(3)
  1935. return true;
  1936. if (type == OBJ_COMMIT)
  1937. return reuseDeltaCommits;
  1938. if (type == OBJ_TAG)
  1939. return false;
  1940. return true;
  1941. }
  1942. /**
  1943. * Summary of how PackWriter created the pack.
  1944. *
  1945. * @deprecated Use {@link PackStatistics} instead.
  1946. */
  1947. @Deprecated
  1948. public static class Statistics {
  1949. /** Statistics about a single class of object. */
  1950. public static class ObjectType {
  1951. // All requests are forwarded to this object.
  1952. private PackStatistics.ObjectType objectType;
  1953. /**
  1954. * Wraps an
  1955. * {@link org.eclipse.jgit.storage.pack.PackStatistics.ObjectType}
  1956. * instance to maintain backwards compatibility with existing API.
  1957. *
  1958. * @param type
  1959. * the wrapped instance
  1960. */
  1961. public ObjectType(PackStatistics.ObjectType type) {
  1962. objectType = type;
  1963. }
  1964. /**
  1965. * @return total number of objects output. This total includes the
  1966. * value of {@link #getDeltas()}.
  1967. */
  1968. public long getObjects() {
  1969. return objectType.getObjects();
  1970. }
  1971. /**
  1972. * @return total number of deltas output. This may be lower than the
  1973. * actual number of deltas if a cached pack was reused.
  1974. */
  1975. public long getDeltas() {
  1976. return objectType.getDeltas();
  1977. }
  1978. /**
  1979. * @return number of objects whose existing representation was
  1980. * reused in the output. This count includes
  1981. * {@link #getReusedDeltas()}.
  1982. */
  1983. public long getReusedObjects() {
  1984. return objectType.getReusedObjects();
  1985. }
  1986. /**
  1987. * @return number of deltas whose existing representation was reused
  1988. * in the output, as their base object was also output or
  1989. * was assumed present for a thin pack. This may be lower
  1990. * than the actual number of reused deltas if a cached pack
  1991. * was reused.
  1992. */
  1993. public long getReusedDeltas() {
  1994. return objectType.getReusedDeltas();
  1995. }
  1996. /**
  1997. * @return total number of bytes written. This size includes the
  1998. * object headers as well as the compressed data. This size
  1999. * also includes all of {@link #getDeltaBytes()}.
  2000. */
  2001. public long getBytes() {
  2002. return objectType.getBytes();
  2003. }
  2004. /**
  2005. * @return number of delta bytes written. This size includes the
  2006. * object headers for the delta objects.
  2007. */
  2008. public long getDeltaBytes() {
  2009. return objectType.getDeltaBytes();
  2010. }
  2011. }
  2012. // All requests are forwarded to this object.
  2013. private PackStatistics statistics;
  2014. /**
  2015. * Wraps a {@link PackStatistics} object to maintain backwards
  2016. * compatibility with existing API.
  2017. *
  2018. * @param stats
  2019. * the wrapped PackStatitics object
  2020. */
  2021. public Statistics(PackStatistics stats) {
  2022. statistics = stats;
  2023. }
  2024. /**
  2025. * @return unmodifiable collection of objects to be included in the
  2026. * pack. May be null if the pack was hand-crafted in a unit
  2027. * test.
  2028. */
  2029. public Set<ObjectId> getInterestingObjects() {
  2030. return statistics.getInterestingObjects();
  2031. }
  2032. /**
  2033. * @return unmodifiable collection of objects that should be excluded
  2034. * from the pack, as the peer that will receive the pack already
  2035. * has these objects.
  2036. */
  2037. public Set<ObjectId> getUninterestingObjects() {
  2038. return statistics.getUninterestingObjects();
  2039. }
  2040. /**
  2041. * @return unmodifiable collection of the cached packs that were reused
  2042. * in the output, if any were selected for reuse.
  2043. */
  2044. public Collection<CachedPack> getReusedPacks() {
  2045. return statistics.getReusedPacks();
  2046. }
  2047. /**
  2048. * @return number of objects in the output pack that went through the
  2049. * delta search process in order to find a potential delta base.
  2050. */
  2051. public int getDeltaSearchNonEdgeObjects() {
  2052. return statistics.getDeltaSearchNonEdgeObjects();
  2053. }
  2054. /**
  2055. * @return number of objects in the output pack that went through delta
  2056. * base search and found a suitable base. This is a subset of
  2057. * {@link #getDeltaSearchNonEdgeObjects()}.
  2058. */
  2059. public int getDeltasFound() {
  2060. return statistics.getDeltasFound();
  2061. }
  2062. /**
  2063. * @return total number of objects output. This total includes the value
  2064. * of {@link #getTotalDeltas()}.
  2065. */
  2066. public long getTotalObjects() {
  2067. return statistics.getTotalObjects();
  2068. }
  2069. /**
  2070. * @return the count of objects that needed to be discovered through an
  2071. * object walk because they were not found in bitmap indices.
  2072. * Returns -1 if no bitmap indices were found.
  2073. */
  2074. public long getBitmapIndexMisses() {
  2075. return statistics.getBitmapIndexMisses();
  2076. }
  2077. /**
  2078. * @return total number of deltas output. This may be lower than the
  2079. * actual number of deltas if a cached pack was reused.
  2080. */
  2081. public long getTotalDeltas() {
  2082. return statistics.getTotalDeltas();
  2083. }
  2084. /**
  2085. * @return number of objects whose existing representation was reused in
  2086. * the output. This count includes {@link #getReusedDeltas()}.
  2087. */
  2088. public long getReusedObjects() {
  2089. return statistics.getReusedObjects();
  2090. }
  2091. /**
  2092. * @return number of deltas whose existing representation was reused in
  2093. * the output, as their base object was also output or was
  2094. * assumed present for a thin pack. This may be lower than the
  2095. * actual number of reused deltas if a cached pack was reused.
  2096. */
  2097. public long getReusedDeltas() {
  2098. return statistics.getReusedDeltas();
  2099. }
  2100. /**
  2101. * @return total number of bytes written. This size includes the pack
  2102. * header, trailer, thin pack, and reused cached pack(s).
  2103. */
  2104. public long getTotalBytes() {
  2105. return statistics.getTotalBytes();
  2106. }
  2107. /**
  2108. * @return size of the thin pack in bytes, if a thin pack was generated.
  2109. * A thin pack is created when the client already has objects
  2110. * and some deltas are created against those objects, or if a
  2111. * cached pack is being used and some deltas will reference
  2112. * objects in the cached pack. This size does not include the
  2113. * pack header or trailer.
  2114. */
  2115. public long getThinPackBytes() {
  2116. return statistics.getThinPackBytes();
  2117. }
  2118. /**
  2119. * @param typeCode
  2120. * object type code, e.g. OBJ_COMMIT or OBJ_TREE.
  2121. * @return information about this type of object in the pack.
  2122. */
  2123. public ObjectType byObjectType(int typeCode) {
  2124. return new ObjectType(statistics.byObjectType(typeCode));
  2125. }
  2126. /** @return true if the resulting pack file was a shallow pack. */
  2127. public boolean isShallow() {
  2128. return statistics.isShallow();
  2129. }
  2130. /** @return depth (in commits) the pack includes if shallow. */
  2131. public int getDepth() {
  2132. return statistics.getDepth();
  2133. }
  2134. /**
  2135. * @return time in milliseconds spent enumerating the objects that need
  2136. * to be included in the output. This time includes any restarts
  2137. * that occur when a cached pack is selected for reuse.
  2138. */
  2139. public long getTimeCounting() {
  2140. return statistics.getTimeCounting();
  2141. }
  2142. /**
  2143. * @return time in milliseconds spent matching existing representations
  2144. * against objects that will be transmitted, or that the client
  2145. * can be assumed to already have.
  2146. */
  2147. public long getTimeSearchingForReuse() {
  2148. return statistics.getTimeSearchingForReuse();
  2149. }
  2150. /**
  2151. * @return time in milliseconds spent finding the sizes of all objects
  2152. * that will enter the delta compression search window. The
  2153. * sizes need to be known to better match similar objects
  2154. * together and improve delta compression ratios.
  2155. */
  2156. public long getTimeSearchingForSizes() {
  2157. return statistics.getTimeSearchingForSizes();
  2158. }
  2159. /**
  2160. * @return time in milliseconds spent on delta compression. This is
  2161. * observed wall-clock time and does not accurately track CPU
  2162. * time used when multiple threads were used to perform the
  2163. * delta compression.
  2164. */
  2165. public long getTimeCompressing() {
  2166. return statistics.getTimeCompressing();
  2167. }
  2168. /**
  2169. * @return time in milliseconds spent writing the pack output, from
  2170. * start of header until end of trailer. The transfer speed can
  2171. * be approximated by dividing {@link #getTotalBytes()} by this
  2172. * value.
  2173. */
  2174. public long getTimeWriting() {
  2175. return statistics.getTimeWriting();
  2176. }
  2177. /** @return total time spent processing this pack. */
  2178. public long getTimeTotal() {
  2179. return statistics.getTimeTotal();
  2180. }
  2181. /**
  2182. * @return get the average output speed in terms of bytes-per-second.
  2183. * {@code getTotalBytes() / (getTimeWriting() / 1000.0)}.
  2184. */
  2185. public double getTransferRate() {
  2186. return statistics.getTransferRate();
  2187. }
  2188. /** @return formatted message string for display to clients. */
  2189. public String getMessage() {
  2190. return statistics.getMessage();
  2191. }
  2192. }
  2193. private class MutableState {
  2194. /** Estimated size of a single ObjectToPack instance. */
  2195. // Assume 64-bit pointers, since this is just an estimate.
  2196. private static final long OBJECT_TO_PACK_SIZE =
  2197. (2 * 8) // Object header
  2198. + (2 * 8) + (2 * 8) // ObjectToPack fields
  2199. + (8 + 8) // PackedObjectInfo fields
  2200. + 8 // ObjectIdOwnerMap fields
  2201. + 40 // AnyObjectId fields
  2202. + 8; // Reference in BlockList
  2203. private final long totalDeltaSearchBytes;
  2204. private volatile PackingPhase phase;
  2205. MutableState() {
  2206. phase = PackingPhase.COUNTING;
  2207. if (config.isDeltaCompress()) {
  2208. int threads = config.getThreads();
  2209. if (threads <= 0)
  2210. threads = Runtime.getRuntime().availableProcessors();
  2211. totalDeltaSearchBytes = (threads * config.getDeltaSearchMemoryLimit())
  2212. + config.getBigFileThreshold();
  2213. } else
  2214. totalDeltaSearchBytes = 0;
  2215. }
  2216. State snapshot() {
  2217. long objCnt = 0;
  2218. BlockList<ObjectToPack>[] lists = objectsLists;
  2219. if (lists != null) {
  2220. objCnt += lists[OBJ_COMMIT].size();
  2221. objCnt += lists[OBJ_TREE].size();
  2222. objCnt += lists[OBJ_BLOB].size();
  2223. objCnt += lists[OBJ_TAG].size();
  2224. // Exclude CachedPacks.
  2225. }
  2226. long bytesUsed = OBJECT_TO_PACK_SIZE * objCnt;
  2227. PackingPhase curr = phase;
  2228. if (curr == PackingPhase.COMPRESSING)
  2229. bytesUsed += totalDeltaSearchBytes;
  2230. return new State(curr, bytesUsed);
  2231. }
  2232. }
  2233. /** Possible states that a PackWriter can be in. */
  2234. public static enum PackingPhase {
  2235. /** Counting objects phase. */
  2236. COUNTING,
  2237. /** Getting sizes phase. */
  2238. GETTING_SIZES,
  2239. /** Finding sources phase. */
  2240. FINDING_SOURCES,
  2241. /** Compressing objects phase. */
  2242. COMPRESSING,
  2243. /** Writing objects phase. */
  2244. WRITING,
  2245. /** Building bitmaps phase. */
  2246. BUILDING_BITMAPS;
  2247. }
  2248. /** Summary of the current state of a PackWriter. */
  2249. public class State {
  2250. private final PackingPhase phase;
  2251. private final long bytesUsed;
  2252. State(PackingPhase phase, long bytesUsed) {
  2253. this.phase = phase;
  2254. this.bytesUsed = bytesUsed;
  2255. }
  2256. /** @return the PackConfig used to build the writer. */
  2257. public PackConfig getConfig() {
  2258. return config;
  2259. }
  2260. /** @return the current phase of the writer. */
  2261. public PackingPhase getPhase() {
  2262. return phase;
  2263. }
  2264. /** @return an estimate of the total memory used by the writer. */
  2265. public long estimateBytesUsed() {
  2266. return bytesUsed;
  2267. }
  2268. @SuppressWarnings("nls")
  2269. @Override
  2270. public String toString() {
  2271. return "PackWriter.State[" + phase + ", memory=" + bytesUsed + "]";
  2272. }
  2273. }
  2274. }