View Javadoc
1   /*
2    * Copyright (C) 2017, Google Inc.
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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  		// Packs start out as INSERT.
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  		// INSERT packs are compacted into a single COMPACT pack.
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 		// There will be one INSERT pack and one COMPACT pack.
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 		// INSERT pack is combined into the COMPACT pack.
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 }