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.

DfsPackDescriptionTest.java 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. * Copyright (C) 2018, Google LLC. 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.GC;
  13. import static org.eclipse.jgit.internal.storage.dfs.DfsObjDatabase.PackSource.GC_REST;
  14. import static org.eclipse.jgit.internal.storage.dfs.DfsObjDatabase.PackSource.GC_TXN;
  15. import static org.eclipse.jgit.internal.storage.dfs.DfsObjDatabase.PackSource.INSERT;
  16. import static org.eclipse.jgit.internal.storage.dfs.DfsObjDatabase.PackSource.RECEIVE;
  17. import static org.eclipse.jgit.internal.storage.dfs.DfsObjDatabase.PackSource.UNREACHABLE_GARBAGE;
  18. import static org.eclipse.jgit.internal.storage.pack.PackExt.INDEX;
  19. import static org.eclipse.jgit.internal.storage.pack.PackExt.PACK;
  20. import static org.junit.Assert.assertEquals;
  21. import java.util.Comparator;
  22. import java.util.concurrent.atomic.AtomicInteger;
  23. import org.eclipse.jgit.internal.storage.dfs.DfsObjDatabase.PackSource;
  24. import org.junit.Before;
  25. import org.junit.Test;
  26. public final class DfsPackDescriptionTest {
  27. private AtomicInteger counter;
  28. @Before
  29. public void setUp() {
  30. counter = new AtomicInteger();
  31. }
  32. @Test
  33. public void objectLookupComparatorEqual() throws Exception {
  34. DfsPackDescription a = create(RECEIVE);
  35. a.setFileSize(PACK, 1);
  36. a.setFileSize(INDEX, 1);
  37. a.setLastModified(1);
  38. a.setObjectCount(1);
  39. a.setMaxUpdateIndex(1);
  40. DfsPackDescription b = create(INSERT);
  41. b.setFileSize(PACK, 1);
  42. b.setFileSize(INDEX, 2);
  43. b.setLastModified(1);
  44. b.setObjectCount(1);
  45. b.setMaxUpdateIndex(2);
  46. assertComparesEqual(DfsPackDescription.objectLookupComparator(), a, b);
  47. }
  48. @Test
  49. public void objectLookupComparatorPackSource() throws Exception {
  50. DfsPackDescription a = create(COMPACT);
  51. a.setFileSize(PACK, 2);
  52. a.setLastModified(1);
  53. a.setObjectCount(2);
  54. DfsPackDescription b = create(GC);
  55. b.setFileSize(PACK, 1);
  56. b.setLastModified(2);
  57. b.setObjectCount(1);
  58. assertComparesLessThan(DfsPackDescription.objectLookupComparator(), a, b);
  59. }
  60. @Test
  61. public void objectLookupComparatorCustomPackSourceComparator()
  62. throws Exception {
  63. DfsPackDescription a = create(GC);
  64. DfsPackDescription b = create(COMPACT);
  65. assertComparesLessThan(DfsPackDescription.objectLookupComparator(), b, a);
  66. assertComparesLessThan(
  67. DfsPackDescription.objectLookupComparator(
  68. new PackSource.ComparatorBuilder()
  69. .add(GC)
  70. .add(INSERT, RECEIVE, GC_REST, GC_TXN, UNREACHABLE_GARBAGE)
  71. .add(COMPACT)
  72. .build()),
  73. a, b);
  74. }
  75. @Test
  76. public void objectLookupComparatorGcFileSize() throws Exception {
  77. // a is older and smaller.
  78. DfsPackDescription a = create(GC_REST);
  79. a.setFileSize(PACK, 100);
  80. a.setLastModified(1);
  81. a.setObjectCount(2);
  82. // b is newer and larger.
  83. DfsPackDescription b = create(GC_REST);
  84. b.setFileSize(PACK, 200);
  85. b.setLastModified(2);
  86. b.setObjectCount(1);
  87. // Since they have the same GC type, tiebreaker is size, and a comes first.
  88. assertComparesLessThan(DfsPackDescription.objectLookupComparator(), a, b);
  89. }
  90. @Test
  91. public void objectLookupComparatorNonGcLastModified()
  92. throws Exception {
  93. // a is older and smaller.
  94. DfsPackDescription a = create(INSERT);
  95. a.setFileSize(PACK, 100);
  96. a.setLastModified(1);
  97. a.setObjectCount(2);
  98. // b is newer and larger.
  99. DfsPackDescription b = create(INSERT);
  100. b.setFileSize(PACK, 200);
  101. b.setLastModified(2);
  102. b.setObjectCount(1);
  103. // Since they have the same type but not GC, tiebreaker is last modified,
  104. // and b comes first.
  105. assertComparesLessThan(DfsPackDescription.objectLookupComparator(), b, a);
  106. }
  107. @Test
  108. public void objectLookupComparatorObjectCount() throws Exception {
  109. DfsPackDescription a = create(INSERT);
  110. a.setObjectCount(1);
  111. DfsPackDescription b = create(INSERT);
  112. b.setObjectCount(2);
  113. assertComparesLessThan(DfsPackDescription.objectLookupComparator(), a, b);
  114. }
  115. @Test
  116. public void reftableComparatorEqual() throws Exception {
  117. DfsPackDescription a = create(INSERT);
  118. a.setFileSize(PACK, 100);
  119. a.setObjectCount(1);
  120. DfsPackDescription b = create(INSERT);
  121. b.setFileSize(PACK, 200);
  122. a.setObjectCount(2);
  123. assertComparesEqual(DfsPackDescription.reftableComparator(), a, b);
  124. }
  125. @Test
  126. public void reftableComparatorPackSource() throws Exception {
  127. DfsPackDescription a = create(INSERT);
  128. a.setMaxUpdateIndex(1);
  129. a.setLastModified(1);
  130. DfsPackDescription b = create(GC);
  131. b.setMaxUpdateIndex(2);
  132. b.setLastModified(2);
  133. assertComparesLessThan(DfsPackDescription.reftableComparator(), b, a);
  134. }
  135. @Test
  136. public void reftableComparatorMaxUpdateIndex() throws Exception {
  137. DfsPackDescription a = create(INSERT);
  138. a.setMaxUpdateIndex(1);
  139. a.setLastModified(2);
  140. DfsPackDescription b = create(INSERT);
  141. b.setMaxUpdateIndex(2);
  142. b.setLastModified(1);
  143. assertComparesLessThan(DfsPackDescription.reftableComparator(), a, b);
  144. }
  145. @Test
  146. public void reftableComparatorLastModified() throws Exception {
  147. DfsPackDescription a = create(INSERT);
  148. a.setLastModified(1);
  149. DfsPackDescription b = create(INSERT);
  150. b.setLastModified(2);
  151. assertComparesLessThan(DfsPackDescription.reftableComparator(), a, b);
  152. }
  153. @Test
  154. public void reuseComparatorEqual() throws Exception {
  155. DfsPackDescription a = create(RECEIVE);
  156. a.setFileSize(PACK, 1);
  157. a.setFileSize(INDEX, 1);
  158. a.setLastModified(1);
  159. a.setObjectCount(1);
  160. a.setMaxUpdateIndex(1);
  161. DfsPackDescription b = create(INSERT);
  162. b.setFileSize(PACK, 2);
  163. b.setFileSize(INDEX, 2);
  164. b.setLastModified(2);
  165. b.setObjectCount(2);
  166. b.setMaxUpdateIndex(2);
  167. assertComparesEqual(DfsPackDescription.reuseComparator(), a, b);
  168. }
  169. @Test
  170. public void reuseComparatorGcPackSize() throws Exception {
  171. DfsPackDescription a = create(GC_REST);
  172. a.setFileSize(PACK, 1);
  173. a.setFileSize(INDEX, 1);
  174. a.setLastModified(2);
  175. a.setObjectCount(1);
  176. a.setMaxUpdateIndex(1);
  177. DfsPackDescription b = create(GC_REST);
  178. b.setFileSize(PACK, 2);
  179. b.setFileSize(INDEX, 2);
  180. b.setLastModified(1);
  181. b.setObjectCount(2);
  182. b.setMaxUpdateIndex(2);
  183. assertComparesLessThan(DfsPackDescription.reuseComparator(), b, a);
  184. }
  185. private DfsPackDescription create(PackSource source) {
  186. return new DfsPackDescription(
  187. new DfsRepositoryDescription("repo"),
  188. "pack_" + counter.incrementAndGet(),
  189. source);
  190. }
  191. private static <T> void assertComparesEqual(
  192. Comparator<T> comparator, T o1, T o2) {
  193. assertEquals(
  194. "first object must compare equal to itself",
  195. 0, comparator.compare(o1, o1));
  196. assertEquals(
  197. "second object must compare equal to itself",
  198. 0, comparator.compare(o2, o2));
  199. assertEquals(
  200. "first object must compare equal to second object",
  201. 0, comparator.compare(o1, o2));
  202. }
  203. private static <T> void assertComparesLessThan(
  204. Comparator<T> comparator, T o1, T o2) {
  205. assertEquals(
  206. "first object must compare equal to itself",
  207. 0, comparator.compare(o1, o1));
  208. assertEquals(
  209. "second object must compare equal to itself",
  210. 0, comparator.compare(o2, o2));
  211. assertEquals(
  212. "first object must compare less than second object",
  213. -1, comparator.compare(o1, o2));
  214. }
  215. }