View Javadoc
1   /*
2    * Copyright (C) 2017, Google Inc. 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  
11  package org.eclipse.jgit.internal.storage.dfs;
12  
13  import static org.eclipse.jgit.internal.storage.dfs.DfsObjDatabase.PackSource.COMPACT;
14  import static org.eclipse.jgit.internal.storage.dfs.DfsObjDatabase.PackSource.INSERT;
15  import static org.eclipse.jgit.internal.storage.pack.PackExt.PACK;
16  import static org.junit.Assert.assertEquals;
17  import static org.junit.Assert.assertTrue;
18  
19  import java.io.IOException;
20  
21  import org.eclipse.jgit.junit.TestRepository;
22  import org.eclipse.jgit.revwalk.RevCommit;
23  import org.junit.Before;
24  import org.junit.Test;
25  
26  public class DfsPackCompacterTest {
27  	private TestRepository<InMemoryRepository> git;
28  	private InMemoryRepository repo;
29  	private DfsObjDatabase odb;
30  
31  	@Before
32  	public void setUp() throws IOException {
33  		DfsRepositoryDescription desc = new DfsRepositoryDescription("test");
34  		git = new TestRepository<>(new InMemoryRepository(desc));
35  		repo = git.getRepository();
36  		odb = repo.getObjectDatabase();
37  	}
38  
39  	@Test
40  	public void testEstimateCompactPackSizeInNewRepo() throws Exception {
41  		RevCommit commit0 = commit().message("0").create();
42  		RevCommit commit1 = commit().message("1").parent(commit0).create();
43  		git.update("master", commit1);
44  
45  		// Packs start out as INSERT.
46  		long inputPacksSize = 32;
47  		assertEquals(2, odb.getPacks().length);
48  		for (DfsPackFile pack : odb.getPacks()) {
49  			assertEquals(INSERT, pack.getPackDescription().getPackSource());
50  			inputPacksSize += pack.getPackDescription().getFileSize(PACK) - 32;
51  		}
52  
53  		compact();
54  
55  		// INSERT packs are compacted into a single COMPACT pack.
56  		assertEquals(1, odb.getPacks().length);
57  		DfsPackFile pack = odb.getPacks()[0];
58  		assertEquals(COMPACT, pack.getPackDescription().getPackSource());
59  		assertEquals(inputPacksSize,
60  				pack.getPackDescription().getEstimatedPackSize());
61  	}
62  
63  	@Test
64  	public void testEstimateGcPackSizeWithAnExistingGcPack() throws Exception {
65  		RevCommit commit0 = commit().message("0").create();
66  		RevCommit commit1 = commit().message("1").parent(commit0).create();
67  		git.update("master", commit1);
68  
69  		compact();
70  
71  		RevCommit commit2 = commit().message("2").parent(commit1).create();
72  		git.update("master", commit2);
73  
74  		// There will be one INSERT pack and one COMPACT pack.
75  		assertEquals(2, odb.getPacks().length);
76  		boolean compactPackFound = false;
77  		boolean insertPackFound = false;
78  		long inputPacksSize = 32;
79  		for (DfsPackFile pack : odb.getPacks()) {
80  			DfsPackDescription packDescription = pack.getPackDescription();
81  			if (packDescription.getPackSource() == COMPACT) {
82  				compactPackFound = true;
83  			}
84  			if (packDescription.getPackSource() == INSERT) {
85  				insertPackFound = true;
86  			}
87  			inputPacksSize += packDescription.getFileSize(PACK) - 32;
88  		}
89  		assertTrue(compactPackFound);
90  		assertTrue(insertPackFound);
91  
92  		compact();
93  
94  		// INSERT pack is combined into the COMPACT pack.
95  		DfsPackFile pack = odb.getPacks()[0];
96  		assertEquals(COMPACT, pack.getPackDescription().getPackSource());
97  		assertEquals(inputPacksSize,
98  				pack.getPackDescription().getEstimatedPackSize());
99  	}
100 
101 	private TestRepository<InMemoryRepository>.CommitBuilder commit() {
102 		return git.commit();
103 	}
104 
105 	private void compact() throws IOException {
106 		DfsPackCompactor compactor = new DfsPackCompactor(repo);
107 		compactor.autoAdd();
108 		compactor.compact(null);
109 		odb.clearCache();
110 	}
111 }