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.

FileReftableStackTest.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Copyright (C) 2019 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.file;
  11. import static org.eclipse.jgit.lib.Ref.Storage.PACKED;
  12. import static org.junit.Assert.assertEquals;
  13. import static org.junit.Assert.assertTrue;
  14. import java.io.File;
  15. import java.io.FileNotFoundException;
  16. import java.io.IOException;
  17. import java.util.Arrays;
  18. import java.util.Collections;
  19. import java.util.List;
  20. import java.util.stream.Collectors;
  21. import org.eclipse.jgit.internal.storage.file.FileReftableStack.Segment;
  22. import org.eclipse.jgit.internal.storage.reftable.MergedReftable;
  23. import org.eclipse.jgit.internal.storage.reftable.RefCursor;
  24. import org.eclipse.jgit.lib.Config;
  25. import org.eclipse.jgit.lib.ObjectId;
  26. import org.eclipse.jgit.lib.ObjectIdRef;
  27. import org.eclipse.jgit.lib.Ref;
  28. import org.eclipse.jgit.util.FileUtils;
  29. import org.junit.After;
  30. import org.junit.Before;
  31. import org.junit.Rule;
  32. import org.junit.Test;
  33. import org.junit.rules.ExpectedException;
  34. public class FileReftableStackTest {
  35. private static Ref newRef(String name, ObjectId id) {
  36. return new ObjectIdRef.PeeledNonTag(PACKED, name, id);
  37. }
  38. private File reftableDir;
  39. @Before
  40. public void setup() throws Exception {
  41. reftableDir = FileUtils.createTempDir("rtstack", "", null);
  42. }
  43. @After
  44. public void tearDown() throws Exception {
  45. if (reftableDir != null) {
  46. FileUtils.delete(reftableDir, FileUtils.RECURSIVE);
  47. }
  48. }
  49. void writeBranches(FileReftableStack stack, String template, int start,
  50. int N) throws IOException {
  51. for (int i = 0; i < N; i++) {
  52. while (true) {
  53. final long next = stack.getMergedReftable().maxUpdateIndex()
  54. + 1;
  55. String name = String.format(template,
  56. Integer.valueOf(start + i));
  57. Ref r = newRef(name, ObjectId.zeroId());
  58. boolean ok = stack.addReftable(rw -> {
  59. rw.setMinUpdateIndex(next).setMaxUpdateIndex(next).begin()
  60. .writeRef(r);
  61. });
  62. if (ok) {
  63. break;
  64. }
  65. }
  66. }
  67. }
  68. public void testCompaction(int N) throws Exception {
  69. try (FileReftableStack stack = new FileReftableStack(
  70. new File(reftableDir, "refs"), reftableDir, null,
  71. () -> new Config())) {
  72. writeBranches(stack, "refs/heads/branch%d", 0, N);
  73. MergedReftable table = stack.getMergedReftable();
  74. for (int i = 1; i < N; i++) {
  75. String name = String.format("refs/heads/branch%d",
  76. Integer.valueOf(i));
  77. RefCursor c = table.seekRef(name);
  78. assertTrue(c.next());
  79. assertEquals(ObjectId.zeroId(), c.getRef().getObjectId());
  80. }
  81. List<String> files = Arrays.asList(reftableDir.listFiles()).stream()
  82. .map(File::getName).collect(Collectors.toList());
  83. Collections.sort(files);
  84. assertTrue(files.size() < 20);
  85. FileReftableStack.CompactionStats stats = stack.getStats();
  86. assertEquals(0, stats.failed);
  87. assertTrue(stats.attempted < N);
  88. assertTrue(stats.refCount < FileReftableStack.log(N) * N);
  89. }
  90. }
  91. @Test
  92. public void testCompaction9() throws Exception {
  93. testCompaction(9);
  94. }
  95. @Test
  96. public void testCompaction1024() throws Exception {
  97. testCompaction(1024);
  98. }
  99. @Rule
  100. public final ExpectedException thrown = ExpectedException.none();
  101. @SuppressWarnings({ "resource", "unused" })
  102. @Test
  103. public void missingReftable() throws Exception {
  104. try (FileReftableStack stack = new FileReftableStack(
  105. new File(reftableDir, "refs"), reftableDir, null,
  106. () -> new Config())) {
  107. outer: for (int i = 0; i < 10; i++) {
  108. final long next = stack.getMergedReftable().maxUpdateIndex()
  109. + 1;
  110. String name = String.format("branch%d", Integer.valueOf(i));
  111. Ref r = newRef(name, ObjectId.zeroId());
  112. boolean ok = stack.addReftable(rw -> {
  113. rw.setMinUpdateIndex(next).setMaxUpdateIndex(next).begin()
  114. .writeRef(r);
  115. });
  116. assertTrue(ok);
  117. List<File> files = Arrays.asList(reftableDir.listFiles());
  118. for (int j = 0; j < files.size(); j++) {
  119. File f = files.get(j);
  120. if (f.getName().endsWith(".ref")) {
  121. assertTrue(f.delete());
  122. break outer;
  123. }
  124. }
  125. }
  126. }
  127. thrown.expect(FileNotFoundException.class);
  128. new FileReftableStack(new File(reftableDir, "refs"), reftableDir, null,
  129. () -> new Config());
  130. }
  131. @Test
  132. public void testSegments() {
  133. long in[] = { 1024, 1024, 1536, 100, 64, 50, 25, 24 };
  134. List<Segment> got = FileReftableStack.segmentSizes(in);
  135. Segment want[] = { new Segment(0, 3, 10, 3584),
  136. new Segment(3, 5, 6, 164), new Segment(5, 6, 5, 50),
  137. new Segment(6, 8, 4, 49), };
  138. assertEquals(got.size(), want.length);
  139. for (int i = 0; i < want.length; i++) {
  140. assertTrue(want[i].equals(got.get(i)));
  141. }
  142. }
  143. @Test
  144. public void testLog2() throws Exception {
  145. assertEquals(10, FileReftableStack.log(1024));
  146. assertEquals(10, FileReftableStack.log(1025));
  147. assertEquals(10, FileReftableStack.log(2047));
  148. }
  149. }