View Javadoc
1   /*
2    * Copyright (C) 2013, 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.junit.Assert.assertEquals;
47  import static org.junit.Assert.assertFalse;
48  import static org.junit.Assert.assertTrue;
49  
50  import java.io.IOException;
51  import java.nio.ByteBuffer;
52  import java.util.Arrays;
53  import java.util.Collection;
54  import java.util.List;
55  import java.util.zip.Deflater;
56  
57  import org.eclipse.jgit.internal.storage.pack.PackExt;
58  import org.eclipse.jgit.junit.JGitTestUtil;
59  import org.eclipse.jgit.junit.TestRng;
60  import org.eclipse.jgit.lib.AbbreviatedObjectId;
61  import org.eclipse.jgit.lib.Constants;
62  import org.eclipse.jgit.lib.ObjectId;
63  import org.eclipse.jgit.lib.ObjectInserter;
64  import org.eclipse.jgit.lib.ObjectLoader;
65  import org.eclipse.jgit.lib.ObjectReader;
66  import org.eclipse.jgit.util.IO;
67  import org.eclipse.jgit.util.RawParseUtils;
68  import org.junit.Before;
69  import org.junit.Test;
70  
71  public class DfsInserterTest {
72  	InMemoryRepository db;
73  
74  	@Before
75  	public void setUp() {
76  		db = new InMemoryRepository(new DfsRepositoryDescription("test"));
77  	}
78  
79  	@Test
80  	public void testInserterDiscardsPack() throws IOException {
81  		try (ObjectInserter ins = db.newObjectInserter()) {
82  			ins.insert(Constants.OBJ_BLOB, Constants.encode("foo"));
83  			ins.insert(Constants.OBJ_BLOB, Constants.encode("bar"));
84  			assertEquals(0, db.getObjectDatabase().listPacks().size());
85  		}
86  		assertEquals(0, db.getObjectDatabase().listPacks().size());
87  	}
88  
89  	@Test
90  	public void testReadFromInserterSmallObjects() throws IOException {
91  		ObjectInserter ins = db.newObjectInserter();
92  		ObjectId id1 = ins.insert(Constants.OBJ_BLOB, Constants.encode("foo"));
93  		ObjectId id2 = ins.insert(Constants.OBJ_BLOB, Constants.encode("bar"));
94  		assertEquals(0, db.getObjectDatabase().listPacks().size());
95  
96  		ObjectReader reader = ins.newReader();
97  		assertEquals("foo", readString(reader.open(id1)));
98  		assertEquals("bar", readString(reader.open(id2)));
99  		assertEquals(0, db.getObjectDatabase().listPacks().size());
100 		ins.flush();
101 		assertEquals(1, db.getObjectDatabase().listPacks().size());
102 	}
103 
104 	@Test
105 	public void testReadFromInserterLargerObjects() throws IOException {
106 		db.getObjectDatabase().getReaderOptions().setStreamFileThreshold(512);
107 		DfsBlockCache.reconfigure(new DfsBlockCacheConfig()
108 			.setBlockSize(512)
109 			.setBlockLimit(2048));
110 
111 		byte[] data = new TestRng(JGitTestUtil.getName()).nextBytes(8192);
112 		DfsInserter ins = (DfsInserter) db.newObjectInserter();
113 		ins.setCompressionLevel(Deflater.NO_COMPRESSION);
114 		ObjectId id1 = ins.insert(Constants.OBJ_BLOB, data);
115 		assertEquals(0, db.getObjectDatabase().listPacks().size());
116 
117 		ObjectReader reader = ins.newReader();
118 		assertTrue(Arrays.equals(data, readStream(reader.open(id1))));
119 		assertEquals(0, db.getObjectDatabase().listPacks().size());
120 		ins.flush();
121 
122 		List<DfsPackDescription> packs = db.getObjectDatabase().listPacks();
123 		assertEquals(1, packs.size());
124 		assertTrue(packs.get(0).getFileSize(PackExt.PACK) > 2048);
125 	}
126 
127 	@Test
128 	public void testReadFromFallback() throws IOException {
129 		ObjectInserter ins = db.newObjectInserter();
130 		ObjectId id1 = ins.insert(Constants.OBJ_BLOB, Constants.encode("foo"));
131 		ins.flush();
132 		ObjectId id2 = ins.insert(Constants.OBJ_BLOB, Constants.encode("bar"));
133 		assertEquals(1, db.getObjectDatabase().listPacks().size());
134 
135 		ObjectReader reader = ins.newReader();
136 		assertEquals("foo", readString(reader.open(id1)));
137 		assertEquals("bar", readString(reader.open(id2)));
138 		assertEquals(1, db.getObjectDatabase().listPacks().size());
139 		ins.flush();
140 		assertEquals(2, db.getObjectDatabase().listPacks().size());
141 	}
142 
143 	@Test
144 	public void testReaderResolve() throws IOException {
145 		ObjectInserter ins = db.newObjectInserter();
146 		ObjectId id1 = ins.insert(Constants.OBJ_BLOB, Constants.encode("foo"));
147 		ins.flush();
148 		ObjectId id2 = ins.insert(Constants.OBJ_BLOB, Constants.encode("bar"));
149 		String abbr1 = ObjectId.toString(id1).substring(0, 4);
150 		String abbr2 = ObjectId.toString(id2).substring(0, 4);
151 		assertFalse(abbr1.equals(abbr2));
152 
153 		ObjectReader reader = ins.newReader();
154 		Collection<ObjectId> objs;
155 		objs = reader.resolve(AbbreviatedObjectId.fromString(abbr1));
156 		assertEquals(1, objs.size());
157 		assertEquals(id1, objs.iterator().next());
158 
159 		objs = reader.resolve(AbbreviatedObjectId.fromString(abbr2));
160 		assertEquals(1, objs.size());
161 		assertEquals(id2, objs.iterator().next());
162 	}
163 
164 	private static String readString(ObjectLoader loader) throws IOException {
165 		return RawParseUtils.decode(readStream(loader));
166 	}
167 
168 	private static byte[] readStream(ObjectLoader loader) throws IOException {
169 		ByteBuffer bb = IO.readWholeStream(loader.openStream(), 64);
170 		byte[] buf = new byte[bb.remaining()];
171 		bb.get(buf);
172 		return buf;
173 	}
174 }