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.

DfsPackCompactor.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /*
  2. * Copyright (C) 2011, Google Inc.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.internal.storage.dfs;
  44. import static org.eclipse.jgit.internal.storage.dfs.DfsObjDatabase.PackSource.COMPACT;
  45. import static org.eclipse.jgit.internal.storage.pack.PackExt.INDEX;
  46. import static org.eclipse.jgit.internal.storage.pack.PackExt.PACK;
  47. import static org.eclipse.jgit.internal.storage.pack.StoredObjectRepresentation.PACK_DELTA;
  48. import java.io.IOException;
  49. import java.util.ArrayList;
  50. import java.util.Collections;
  51. import java.util.Comparator;
  52. import java.util.List;
  53. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  54. import org.eclipse.jgit.internal.JGitText;
  55. import org.eclipse.jgit.internal.storage.file.PackIndex;
  56. import org.eclipse.jgit.internal.storage.file.PackReverseIndex;
  57. import org.eclipse.jgit.internal.storage.pack.PackWriter;
  58. import org.eclipse.jgit.lib.AnyObjectId;
  59. import org.eclipse.jgit.lib.NullProgressMonitor;
  60. import org.eclipse.jgit.lib.ObjectId;
  61. import org.eclipse.jgit.lib.ProgressMonitor;
  62. import org.eclipse.jgit.revwalk.RevFlag;
  63. import org.eclipse.jgit.revwalk.RevObject;
  64. import org.eclipse.jgit.revwalk.RevWalk;
  65. import org.eclipse.jgit.storage.pack.PackConfig;
  66. import org.eclipse.jgit.util.BlockList;
  67. import org.eclipse.jgit.util.io.CountingOutputStream;
  68. /**
  69. * Combine several pack files into one pack.
  70. * <p>
  71. * The compactor combines several pack files together by including all objects
  72. * contained in each pack file into the same output pack. If an object appears
  73. * multiple times, it is only included once in the result. Because the new pack
  74. * is constructed by enumerating the indexes of the source packs, it is quicker
  75. * than doing a full repack of the repository, however the result is not nearly
  76. * as space efficient as new delta compression is disabled.
  77. * <p>
  78. * This method is suitable for quickly combining several packs together after
  79. * receiving a number of small fetch or push operations into a repository,
  80. * allowing the system to maintain reasonable read performance without expending
  81. * a lot of time repacking the entire repository.
  82. */
  83. public class DfsPackCompactor {
  84. private final DfsRepository repo;
  85. private final List<DfsPackFile> srcPacks;
  86. private final List<PackWriter.ObjectIdSet> exclude;
  87. private final List<DfsPackDescription> newPacks;
  88. private final List<PackWriter.Statistics> newStats;
  89. private int autoAddSize;
  90. private RevWalk rw;
  91. private RevFlag added;
  92. private RevFlag isBase;
  93. /**
  94. * Initialize a pack compactor.
  95. *
  96. * @param repository
  97. * repository objects to be packed will be read from.
  98. */
  99. public DfsPackCompactor(DfsRepository repository) {
  100. repo = repository;
  101. autoAddSize = 5 * 1024 * 1024; // 5 MiB
  102. srcPacks = new ArrayList<DfsPackFile>();
  103. exclude = new ArrayList<PackWriter.ObjectIdSet>(4);
  104. newPacks = new ArrayList<DfsPackDescription>(1);
  105. newStats = new ArrayList<PackWriter.Statistics>(1);
  106. }
  107. /**
  108. * Add a pack to be compacted.
  109. * <p>
  110. * All of the objects in this pack will be copied into the resulting pack.
  111. * The resulting pack will order objects according to the source pack's own
  112. * description ordering (which is based on creation date), and then by the
  113. * order the objects appear in the source pack.
  114. *
  115. * @param pack
  116. * a pack to combine into the resulting pack.
  117. * @return {@code this}
  118. */
  119. public DfsPackCompactor add(DfsPackFile pack) {
  120. srcPacks.add(pack);
  121. return this;
  122. }
  123. /**
  124. * Automatically select packs to be included, and add them.
  125. * <p>
  126. * Packs are selected based on size, smaller packs get included while bigger
  127. * ones are omitted.
  128. *
  129. * @return {@code this}
  130. * @throws IOException
  131. * existing packs cannot be read.
  132. */
  133. public DfsPackCompactor autoAdd() throws IOException {
  134. DfsObjDatabase objdb = repo.getObjectDatabase();
  135. for (DfsPackFile pack : objdb.getPacks()) {
  136. DfsPackDescription d = pack.getPackDescription();
  137. if (d.getFileSize(PACK) < autoAddSize)
  138. add(pack);
  139. else
  140. exclude(pack);
  141. }
  142. return this;
  143. }
  144. /**
  145. * Exclude objects from the compacted pack.
  146. *
  147. * @param set
  148. * objects to not include.
  149. * @return {@code this}.
  150. */
  151. public DfsPackCompactor exclude(PackWriter.ObjectIdSet set) {
  152. exclude.add(set);
  153. return this;
  154. }
  155. /**
  156. * Exclude objects from the compacted pack.
  157. *
  158. * @param pack
  159. * objects to not include.
  160. * @return {@code this}.
  161. * @throws IOException
  162. * pack index cannot be loaded.
  163. */
  164. public DfsPackCompactor exclude(DfsPackFile pack) throws IOException {
  165. final PackIndex idx;
  166. DfsReader ctx = (DfsReader) repo.newObjectReader();
  167. try {
  168. idx = pack.getPackIndex(ctx);
  169. } finally {
  170. ctx.release();
  171. }
  172. return exclude(new PackWriter.ObjectIdSet() {
  173. public boolean contains(AnyObjectId id) {
  174. return idx.hasObject(id);
  175. }
  176. });
  177. }
  178. /**
  179. * Compact the pack files together.
  180. *
  181. * @param pm
  182. * progress monitor to receive updates on as packing may take a
  183. * while, depending on the size of the repository.
  184. * @throws IOException
  185. * the packs cannot be compacted.
  186. */
  187. public void compact(ProgressMonitor pm) throws IOException {
  188. if (pm == null)
  189. pm = NullProgressMonitor.INSTANCE;
  190. DfsObjDatabase objdb = repo.getObjectDatabase();
  191. DfsReader ctx = (DfsReader) objdb.newReader();
  192. try {
  193. PackConfig pc = new PackConfig(repo);
  194. pc.setIndexVersion(2);
  195. pc.setDeltaCompress(false);
  196. pc.setReuseDeltas(true);
  197. pc.setReuseObjects(true);
  198. PackWriter pw = new PackWriter(pc, ctx);
  199. try {
  200. pw.setDeltaBaseAsOffset(true);
  201. pw.setReuseDeltaCommits(false);
  202. addObjectsToPack(pw, ctx, pm);
  203. if (pw.getObjectCount() == 0)
  204. return;
  205. boolean rollback = true;
  206. DfsPackDescription pack = objdb.newPack(COMPACT);
  207. try {
  208. writePack(objdb, pack, pw, pm);
  209. writeIndex(objdb, pack, pw);
  210. PackWriter.Statistics stats = pw.getStatistics();
  211. pw.release();
  212. pw = null;
  213. pack.setPackStats(stats);
  214. objdb.commitPack(Collections.singletonList(pack), toPrune());
  215. newPacks.add(pack);
  216. newStats.add(stats);
  217. rollback = false;
  218. } finally {
  219. if (rollback)
  220. objdb.rollbackPack(Collections.singletonList(pack));
  221. }
  222. } finally {
  223. if (pw != null)
  224. pw.release();
  225. }
  226. } finally {
  227. rw = null;
  228. ctx.release();
  229. }
  230. }
  231. /** @return all of the source packs that fed into this compaction. */
  232. public List<DfsPackDescription> getSourcePacks() {
  233. return toPrune();
  234. }
  235. /** @return new packs created by this compaction. */
  236. public List<DfsPackDescription> getNewPacks() {
  237. return newPacks;
  238. }
  239. /** @return statistics corresponding to the {@link #getNewPacks()}. */
  240. public List<PackWriter.Statistics> getNewPackStatistics() {
  241. return newStats;
  242. }
  243. private List<DfsPackDescription> toPrune() {
  244. int cnt = srcPacks.size();
  245. List<DfsPackDescription> all = new ArrayList<DfsPackDescription>(cnt);
  246. for (DfsPackFile pack : srcPacks)
  247. all.add(pack.getPackDescription());
  248. return all;
  249. }
  250. private void addObjectsToPack(PackWriter pw, DfsReader ctx,
  251. ProgressMonitor pm) throws IOException,
  252. IncorrectObjectTypeException {
  253. // Sort packs by description ordering, this places newer packs before
  254. // older packs, allowing the PackWriter to be handed newer objects
  255. // first and older objects last.
  256. Collections.sort(srcPacks, new Comparator<DfsPackFile>() {
  257. public int compare(DfsPackFile a, DfsPackFile b) {
  258. return a.getPackDescription().compareTo(b.getPackDescription());
  259. }
  260. });
  261. rw = new RevWalk(ctx);
  262. added = rw.newFlag("ADDED"); //$NON-NLS-1$
  263. isBase = rw.newFlag("IS_BASE"); //$NON-NLS-1$
  264. List<RevObject> baseObjects = new BlockList<RevObject>();
  265. pm.beginTask(JGitText.get().countingObjects, ProgressMonitor.UNKNOWN);
  266. for (DfsPackFile src : srcPacks) {
  267. List<ObjectIdWithOffset> want = toInclude(src, ctx);
  268. if (want.isEmpty())
  269. continue;
  270. PackReverseIndex rev = src.getReverseIdx(ctx);
  271. DfsObjectRepresentation rep = new DfsObjectRepresentation(src);
  272. for (ObjectIdWithOffset id : want) {
  273. int type = src.getObjectType(ctx, id.offset);
  274. RevObject obj = rw.lookupAny(id, type);
  275. if (obj.has(added))
  276. continue;
  277. pm.update(1);
  278. pw.addObject(obj);
  279. obj.add(added);
  280. src.representation(rep, id.offset, ctx, rev);
  281. if (rep.getFormat() != PACK_DELTA)
  282. continue;
  283. RevObject base = rw.lookupAny(rep.getDeltaBase(), type);
  284. if (!base.has(added) && !base.has(isBase)) {
  285. baseObjects.add(base);
  286. base.add(isBase);
  287. }
  288. }
  289. }
  290. for (RevObject obj : baseObjects) {
  291. if (!obj.has(added)) {
  292. pm.update(1);
  293. pw.addObject(obj);
  294. obj.add(added);
  295. }
  296. }
  297. pm.endTask();
  298. }
  299. private List<ObjectIdWithOffset> toInclude(DfsPackFile src, DfsReader ctx)
  300. throws IOException {
  301. PackIndex srcIdx = src.getPackIndex(ctx);
  302. List<ObjectIdWithOffset> want = new BlockList<ObjectIdWithOffset>(
  303. (int) srcIdx.getObjectCount());
  304. SCAN: for (PackIndex.MutableEntry ent : srcIdx) {
  305. ObjectId id = ent.toObjectId();
  306. RevObject obj = rw.lookupOrNull(id);
  307. if (obj != null && (obj.has(added) || obj.has(isBase)))
  308. continue;
  309. for (PackWriter.ObjectIdSet e : exclude)
  310. if (e.contains(id))
  311. continue SCAN;
  312. want.add(new ObjectIdWithOffset(id, ent.getOffset()));
  313. }
  314. Collections.sort(want, new Comparator<ObjectIdWithOffset>() {
  315. public int compare(ObjectIdWithOffset a, ObjectIdWithOffset b) {
  316. return Long.signum(a.offset - b.offset);
  317. }
  318. });
  319. return want;
  320. }
  321. private static void writePack(DfsObjDatabase objdb,
  322. DfsPackDescription pack,
  323. PackWriter pw, ProgressMonitor pm) throws IOException {
  324. DfsOutputStream out = objdb.writeFile(pack, PACK);
  325. try {
  326. pw.writePack(pm, pm, out);
  327. pack.addFileExt(PACK);
  328. } finally {
  329. out.close();
  330. }
  331. }
  332. private static void writeIndex(DfsObjDatabase objdb,
  333. DfsPackDescription pack,
  334. PackWriter pw) throws IOException {
  335. DfsOutputStream out = objdb.writeFile(pack, INDEX);
  336. try {
  337. CountingOutputStream cnt = new CountingOutputStream(out);
  338. pw.writeIndex(cnt);
  339. pack.addFileExt(INDEX);
  340. pack.setFileSize(INDEX, cnt.getCount());
  341. pack.setIndexVersion(pw.getIndexVersion());
  342. } finally {
  343. out.close();
  344. }
  345. }
  346. private static class ObjectIdWithOffset extends ObjectId {
  347. final long offset;
  348. ObjectIdWithOffset(AnyObjectId id, long ofs) {
  349. super(id);
  350. offset = ofs;
  351. }
  352. }
  353. }