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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
/*
* Copyright (C) 2017, Google Inc. and others
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0 which is available at
* https://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
package org.eclipse.jgit.internal.storage.dfs;
import static org.eclipse.jgit.internal.storage.dfs.DfsObjDatabase.PackSource.COMPACT;
import static org.eclipse.jgit.internal.storage.dfs.DfsObjDatabase.PackSource.INSERT;
import static org.eclipse.jgit.internal.storage.pack.PackExt.OBJECT_SIZE_INDEX;
import static org.eclipse.jgit.internal.storage.pack.PackExt.PACK;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.io.IOException;
import java.util.Arrays;
import java.util.Optional;
import org.eclipse.jgit.junit.TestRepository;
import org.eclipse.jgit.lib.ConfigConstants;
import org.eclipse.jgit.revwalk.RevCommit;
import org.junit.Before;
import org.junit.Test;
public class DfsPackCompacterTest {
private TestRepository<InMemoryRepository> git;
private InMemoryRepository repo;
private DfsObjDatabase odb;
@Before
public void setUp() throws IOException {
DfsRepositoryDescription desc = new DfsRepositoryDescription("test");
git = new TestRepository<>(new InMemoryRepository(desc));
repo = git.getRepository();
odb = repo.getObjectDatabase();
}
@Test
public void testEstimateCompactPackSizeInNewRepo() throws Exception {
RevCommit commit0 = commit().message("0").create();
RevCommit commit1 = commit().message("1").parent(commit0).create();
git.update("master", commit1);
// Packs start out as INSERT.
long inputPacksSize = 32;
assertEquals(2, odb.getPacks().length);
for (DfsPackFile pack : odb.getPacks()) {
assertEquals(INSERT, pack.getPackDescription().getPackSource());
inputPacksSize += pack.getPackDescription().getFileSize(PACK) - 32;
}
compact();
// INSERT packs are compacted into a single COMPACT pack.
assertEquals(1, odb.getPacks().length);
DfsPackFile pack = odb.getPacks()[0];
assertEquals(COMPACT, pack.getPackDescription().getPackSource());
assertEquals(inputPacksSize,
pack.getPackDescription().getEstimatedPackSize());
}
@Test
public void testEstimateGcPackSizeWithAnExistingGcPack() throws Exception {
RevCommit commit0 = commit().message("0").create();
RevCommit commit1 = commit().message("1").parent(commit0).create();
git.update("master", commit1);
compact();
RevCommit commit2 = commit().message("2").parent(commit1).create();
git.update("master", commit2);
// There will be one INSERT pack and one COMPACT pack.
assertEquals(2, odb.getPacks().length);
boolean compactPackFound = false;
boolean insertPackFound = false;
long inputPacksSize = 32;
for (DfsPackFile pack : odb.getPacks()) {
DfsPackDescription packDescription = pack.getPackDescription();
if (packDescription.getPackSource() == COMPACT) {
compactPackFound = true;
}
if (packDescription.getPackSource() == INSERT) {
insertPackFound = true;
}
inputPacksSize += packDescription.getFileSize(PACK) - 32;
}
assertTrue(compactPackFound);
assertTrue(insertPackFound);
compact();
// INSERT pack is combined into the COMPACT pack.
DfsPackFile pack = odb.getPacks()[0];
assertEquals(COMPACT, pack.getPackDescription().getPackSource());
assertEquals(inputPacksSize,
pack.getPackDescription().getEstimatedPackSize());
}
@Test
public void testObjectSizeIndexWritten() throws Exception {
writeObjectSizeIndex(repo, true);
RevCommit commit0 = commit().message("0").create();
RevCommit commit1 = commit().message("1").parent(commit0).create();
git.update("master", commit1);
compact();
Optional<DfsPackFile> compactPack = Arrays.stream(odb.getPacks())
.filter(pack -> pack.getPackDescription()
.getPackSource() == COMPACT)
.findFirst();
assertTrue(compactPack.isPresent());
assertTrue(compactPack.get().getPackDescription().hasFileExt(OBJECT_SIZE_INDEX));
}
@Test
public void testObjectSizeIndexNotWritten() throws Exception {
writeObjectSizeIndex(repo, false);
RevCommit commit0 = commit().message("0").create();
RevCommit commit1 = commit().message("1").parent(commit0).create();
git.update("master", commit1);
compact();
Optional<DfsPackFile> compactPack = Arrays.stream(odb.getPacks())
.filter(pack -> pack.getPackDescription()
.getPackSource() == COMPACT)
.findFirst();
assertTrue(compactPack.isPresent());
assertFalse(compactPack.get().getPackDescription().hasFileExt(OBJECT_SIZE_INDEX));
}
private TestRepository<InMemoryRepository>.CommitBuilder commit() {
return git.commit();
}
private void compact() throws IOException {
DfsPackCompactor compactor = new DfsPackCompactor(repo);
compactor.autoAdd();
compactor.compact(null);
odb.clearCache();
}
private static void writeObjectSizeIndex(DfsRepository repo, boolean should) {
repo.getConfig().setInt(ConfigConstants.CONFIG_PACK_SECTION, null,
ConfigConstants.CONFIG_KEY_MIN_BYTES_OBJ_SIZE_INDEX, should ? 0 : -1);
}
}
|