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.

DfsPackCompacterTest.java 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Copyright (C) 2017, Google Inc. and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.internal.storage.dfs;
  11. import static org.eclipse.jgit.internal.storage.dfs.DfsObjDatabase.PackSource.COMPACT;
  12. import static org.eclipse.jgit.internal.storage.dfs.DfsObjDatabase.PackSource.INSERT;
  13. import static org.eclipse.jgit.internal.storage.pack.PackExt.PACK;
  14. import static org.junit.Assert.assertEquals;
  15. import static org.junit.Assert.assertTrue;
  16. import java.io.IOException;
  17. import org.eclipse.jgit.junit.TestRepository;
  18. import org.eclipse.jgit.revwalk.RevCommit;
  19. import org.junit.Before;
  20. import org.junit.Test;
  21. public class DfsPackCompacterTest {
  22. private TestRepository<InMemoryRepository> git;
  23. private InMemoryRepository repo;
  24. private DfsObjDatabase odb;
  25. @Before
  26. public void setUp() throws IOException {
  27. DfsRepositoryDescription desc = new DfsRepositoryDescription("test");
  28. git = new TestRepository<>(new InMemoryRepository(desc));
  29. repo = git.getRepository();
  30. odb = repo.getObjectDatabase();
  31. }
  32. @Test
  33. public void testEstimateCompactPackSizeInNewRepo() throws Exception {
  34. RevCommit commit0 = commit().message("0").create();
  35. RevCommit commit1 = commit().message("1").parent(commit0).create();
  36. git.update("master", commit1);
  37. // Packs start out as INSERT.
  38. long inputPacksSize = 32;
  39. assertEquals(2, odb.getPacks().length);
  40. for (DfsPackFile pack : odb.getPacks()) {
  41. assertEquals(INSERT, pack.getPackDescription().getPackSource());
  42. inputPacksSize += pack.getPackDescription().getFileSize(PACK) - 32;
  43. }
  44. compact();
  45. // INSERT packs are compacted into a single COMPACT pack.
  46. assertEquals(1, odb.getPacks().length);
  47. DfsPackFile pack = odb.getPacks()[0];
  48. assertEquals(COMPACT, pack.getPackDescription().getPackSource());
  49. assertEquals(inputPacksSize,
  50. pack.getPackDescription().getEstimatedPackSize());
  51. }
  52. @Test
  53. public void testEstimateGcPackSizeWithAnExistingGcPack() throws Exception {
  54. RevCommit commit0 = commit().message("0").create();
  55. RevCommit commit1 = commit().message("1").parent(commit0).create();
  56. git.update("master", commit1);
  57. compact();
  58. RevCommit commit2 = commit().message("2").parent(commit1).create();
  59. git.update("master", commit2);
  60. // There will be one INSERT pack and one COMPACT pack.
  61. assertEquals(2, odb.getPacks().length);
  62. boolean compactPackFound = false;
  63. boolean insertPackFound = false;
  64. long inputPacksSize = 32;
  65. for (DfsPackFile pack : odb.getPacks()) {
  66. DfsPackDescription packDescription = pack.getPackDescription();
  67. if (packDescription.getPackSource() == COMPACT) {
  68. compactPackFound = true;
  69. }
  70. if (packDescription.getPackSource() == INSERT) {
  71. insertPackFound = true;
  72. }
  73. inputPacksSize += packDescription.getFileSize(PACK) - 32;
  74. }
  75. assertTrue(compactPackFound);
  76. assertTrue(insertPackFound);
  77. compact();
  78. // INSERT pack is combined into the COMPACT pack.
  79. DfsPackFile pack = odb.getPacks()[0];
  80. assertEquals(COMPACT, pack.getPackDescription().getPackSource());
  81. assertEquals(inputPacksSize,
  82. pack.getPackDescription().getEstimatedPackSize());
  83. }
  84. private TestRepository<InMemoryRepository>.CommitBuilder commit() {
  85. return git.commit();
  86. }
  87. private void compact() throws IOException {
  88. DfsPackCompactor compactor = new DfsPackCompactor(repo);
  89. compactor.autoAdd();
  90. compactor.compact(null);
  91. odb.clearCache();
  92. }
  93. }