1 package org.eclipse.jgit.internal.storage.pack;
2
3 import static org.eclipse.jgit.storage.pack.PackConfig.DEFAULT_BITMAP_DISTANT_COMMIT_SPAN;
4 import static org.eclipse.jgit.storage.pack.PackConfig.DEFAULT_BITMAP_RECENT_COMMIT_COUNT;
5 import static org.eclipse.jgit.storage.pack.PackConfig.DEFAULT_BITMAP_RECENT_COMMIT_SPAN;
6 import static org.junit.Assert.assertEquals;
7
8 import java.io.IOException;
9 import java.util.Collection;
10 import java.util.Collections;
11 import java.util.List;
12 import java.util.Set;
13
14 import org.eclipse.jgit.errors.IncorrectObjectTypeException;
15 import org.eclipse.jgit.errors.MissingObjectException;
16 import org.eclipse.jgit.internal.storage.file.PackBitmapIndexBuilder;
17 import org.eclipse.jgit.lib.AbbreviatedObjectId;
18 import org.eclipse.jgit.lib.AnyObjectId;
19 import org.eclipse.jgit.lib.ObjectId;
20 import org.eclipse.jgit.lib.ObjectLoader;
21 import org.eclipse.jgit.lib.ObjectReader;
22 import org.eclipse.jgit.storage.pack.PackConfig;
23 import org.junit.Test;
24
25
26 public class PackWriterBitmapPreparerTest {
27 private static class StubObjectReader extends ObjectReader {
28 @Override
29 public ObjectReader newReader() {
30 return null;
31 }
32
33 @Override
34 public Collection<ObjectId> resolve(AbbreviatedObjectId id)
35 throws IOException {
36 return null;
37 }
38
39 @Override
40 public ObjectLoader open(AnyObjectId objectId, int typeHint)
41 throws MissingObjectException, IncorrectObjectTypeException,
42 IOException {
43 return null;
44 }
45
46 @Override
47 public Set<ObjectId> getShallowCommits() throws IOException {
48 return null;
49 }
50
51 @Override
52 public void close() {
53
54 }
55 }
56
57 @Test
58 public void testNextSelectionDistanceForActiveBranch() throws Exception {
59 PackWriterBitmapPreparer preparer = newPeparer(
60 DEFAULT_BITMAP_RECENT_COMMIT_COUNT,
61 DEFAULT_BITMAP_RECENT_COMMIT_SPAN,
62 DEFAULT_BITMAP_DISTANT_COMMIT_SPAN);
63 int[][] distancesAndSpans = { { 0, 100 }, { 100, 100 }, { 10000, 100 },
64 { 20000, 100 }, { 20100, 100 }, { 20102, 102 }, { 20200, 200 },
65 { 22200, 2200 }, { 24999, 4999 }, { 25000, 5000 },
66 { 50000, 5000 }, { 1000000, 5000 }, };
67
68 for (int[] pair : distancesAndSpans) {
69 assertEquals(pair[1], preparer.nextSpan(pair[0]));
70 }
71 }
72
73 @Test
74 public void testNextSelectionDistanceWithFewerRecentCommits()
75 throws Exception {
76 PackWriterBitmapPreparer preparer = newPeparer(1000,
77 DEFAULT_BITMAP_RECENT_COMMIT_SPAN,
78 DEFAULT_BITMAP_DISTANT_COMMIT_SPAN);
79 int[][] distancesAndSpans = { { 0, 100 }, { 100, 100 }, { 1000, 100 },
80 { 1100, 100 }, { 1111, 111 }, { 2000, 1000 }, { 5999, 4999 },
81 { 6000, 5000 }, { 10000, 5000 }, { 50000, 5000 },
82 { 1000000, 5000 } };
83
84 for (int[] pair : distancesAndSpans) {
85 assertEquals(pair[1], preparer.nextSpan(pair[0]));
86 }
87 }
88
89 @Test
90 public void testNextSelectionDistanceWithSmallerRecentSpan()
91 throws Exception {
92 PackWriterBitmapPreparer preparer = newPeparer(
93 DEFAULT_BITMAP_RECENT_COMMIT_COUNT,
94 10,
95 DEFAULT_BITMAP_DISTANT_COMMIT_SPAN);
96 int[][] distancesAndSpans = { { 0, 10 }, { 100, 10 }, { 10000, 10 },
97 { 20000, 10 }, { 20010, 10 }, { 20012, 12 }, { 20050, 50 },
98 { 20200, 200 }, { 22200, 2200 }, { 24999, 4999 },
99 { 25000, 5000 }, { 50000, 5000 }, { 1000000, 5000 } };
100
101 for (int[] pair : distancesAndSpans) {
102 assertEquals(pair[1], preparer.nextSpan(pair[0]));
103 }
104 }
105
106 @Test
107 public void testNextSelectionDistanceWithSmallerDistantSpan()
108 throws Exception {
109 PackWriterBitmapPreparer preparer = newPeparer(
110 DEFAULT_BITMAP_RECENT_COMMIT_COUNT,
111 DEFAULT_BITMAP_RECENT_COMMIT_SPAN,
112 1000);
113 int[][] distancesAndSpans = { { 0, 100 }, { 100, 100 }, { 10000, 100 },
114 { 20000, 100 }, { 20100, 100 }, { 20102, 102 }, { 20200, 200 },
115 { 20999, 999 }, { 21000, 1000 }, { 22000, 1000 },
116 { 25000, 1000 }, { 50000, 1000 }, { 1000000, 1000 } };
117
118 for (int[] pair : distancesAndSpans) {
119 assertEquals(pair[1], preparer.nextSpan(pair[0]));
120 }
121 }
122
123 private PackWriterBitmapPreparer newPeparer(int recentCount, int recentSpan,
124 int distantSpan) throws IOException {
125 List<ObjectToPack> objects = Collections.emptyList();
126 Set<ObjectId> wants = Collections.emptySet();
127 PackConfig config = new PackConfig();
128 config.setBitmapRecentCommitCount(recentCount);
129 config.setBitmapRecentCommitSpan(recentSpan);
130 config.setBitmapDistantCommitSpan(distantSpan);
131 PackBitmapIndexBuilder indexBuilder = new PackBitmapIndexBuilder(
132 objects);
133 return new PackWriterBitmapPreparer(new StubObjectReader(),
134 indexBuilder, null, wants, config);
135 }
136 }