1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 package org.eclipse.jgit.internal.storage.dfs;
45
46 import static org.eclipse.jgit.internal.storage.dfs.DfsObjDatabase.PackSource.COMPACT;
47 import static org.eclipse.jgit.internal.storage.dfs.DfsObjDatabase.PackSource.INSERT;
48 import static org.eclipse.jgit.internal.storage.pack.PackExt.PACK;
49 import static org.junit.Assert.assertEquals;
50 import static org.junit.Assert.assertTrue;
51
52 import java.io.IOException;
53
54 import org.eclipse.jgit.junit.TestRepository;
55 import org.eclipse.jgit.revwalk.RevCommit;
56 import org.junit.Before;
57 import org.junit.Test;
58
59 public class DfsPackCompacterTest {
60 private TestRepository<InMemoryRepository> git;
61 private InMemoryRepository repo;
62 private DfsObjDatabase odb;
63
64 @Before
65 public void setUp() throws IOException {
66 DfsRepositoryDescription desc = new DfsRepositoryDescription("test");
67 git = new TestRepository<>(new InMemoryRepository(desc));
68 repo = git.getRepository();
69 odb = repo.getObjectDatabase();
70 }
71
72 @Test
73 public void testEstimateCompactPackSizeInNewRepo() throws Exception {
74 RevCommit commit0 = commit().message("0").create();
75 RevCommit commit1 = commit().message("1").parent(commit0).create();
76 git.update("master", commit1);
77
78
79 long inputPacksSize = 32;
80 assertEquals(2, odb.getPacks().length);
81 for (DfsPackFile pack : odb.getPacks()) {
82 assertEquals(INSERT, pack.getPackDescription().getPackSource());
83 inputPacksSize += pack.getPackDescription().getFileSize(PACK) - 32;
84 }
85
86 compact();
87
88
89 assertEquals(1, odb.getPacks().length);
90 DfsPackFile pack = odb.getPacks()[0];
91 assertEquals(COMPACT, pack.getPackDescription().getPackSource());
92 assertEquals(inputPacksSize,
93 pack.getPackDescription().getEstimatedPackSize());
94 }
95
96 @Test
97 public void testEstimateGcPackSizeWithAnExistingGcPack() throws Exception {
98 RevCommit commit0 = commit().message("0").create();
99 RevCommit commit1 = commit().message("1").parent(commit0).create();
100 git.update("master", commit1);
101
102 compact();
103
104 RevCommit commit2 = commit().message("2").parent(commit1).create();
105 git.update("master", commit2);
106
107
108 assertEquals(2, odb.getPacks().length);
109 boolean compactPackFound = false;
110 boolean insertPackFound = false;
111 long inputPacksSize = 32;
112 for (DfsPackFile pack : odb.getPacks()) {
113 DfsPackDescription packDescription = pack.getPackDescription();
114 if (packDescription.getPackSource() == COMPACT) {
115 compactPackFound = true;
116 }
117 if (packDescription.getPackSource() == INSERT) {
118 insertPackFound = true;
119 }
120 inputPacksSize += packDescription.getFileSize(PACK) - 32;
121 }
122 assertTrue(compactPackFound);
123 assertTrue(insertPackFound);
124
125 compact();
126
127
128 DfsPackFile pack = odb.getPacks()[0];
129 assertEquals(COMPACT, pack.getPackDescription().getPackSource());
130 assertEquals(inputPacksSize,
131 pack.getPackDescription().getEstimatedPackSize());
132 }
133
134 private TestRepository<InMemoryRepository>.CommitBuilder commit() {
135 return git.commit();
136 }
137
138 private void compact() throws IOException {
139 DfsPackCompactor compactor = new DfsPackCompactor(repo);
140 compactor.autoAdd();
141 compactor.compact(null);
142 odb.clearCache();
143 }
144 }