You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DfsInserterTest.java 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. package org.eclipse.jgit.internal.storage.dfs;
  44. import static org.junit.Assert.assertEquals;
  45. import static org.junit.Assert.assertFalse;
  46. import static org.junit.Assert.assertSame;
  47. import static org.junit.Assert.assertTrue;
  48. import java.io.IOException;
  49. import java.nio.ByteBuffer;
  50. import java.util.Arrays;
  51. import java.util.Collection;
  52. import java.util.HashSet;
  53. import java.util.List;
  54. import java.util.Set;
  55. import java.util.zip.Deflater;
  56. import org.eclipse.jgit.internal.storage.dfs.DfsObjDatabase.PackSource;
  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. public class DfsInserterTest {
  71. InMemoryRepository db;
  72. @Before
  73. public void setUp() {
  74. db = new InMemoryRepository(new DfsRepositoryDescription("test"));
  75. }
  76. @Test
  77. public void testInserterDiscardsPack() throws IOException {
  78. try (ObjectInserter ins = db.newObjectInserter()) {
  79. ins.insert(Constants.OBJ_BLOB, Constants.encode("foo"));
  80. ins.insert(Constants.OBJ_BLOB, Constants.encode("bar"));
  81. assertEquals(0, db.getObjectDatabase().listPacks().size());
  82. }
  83. assertEquals(0, db.getObjectDatabase().listPacks().size());
  84. }
  85. @Test
  86. public void testReadFromInserterSmallObjects() throws IOException {
  87. ObjectInserter ins = db.newObjectInserter();
  88. ObjectId id1 = ins.insert(Constants.OBJ_BLOB, Constants.encode("foo"));
  89. ObjectId id2 = ins.insert(Constants.OBJ_BLOB, Constants.encode("bar"));
  90. assertEquals(0, db.getObjectDatabase().listPacks().size());
  91. ObjectReader reader = ins.newReader();
  92. assertSame(ins, reader.getCreatedFromInserter());
  93. assertEquals("foo", readString(reader.open(id1)));
  94. assertEquals("bar", readString(reader.open(id2)));
  95. assertEquals(0, db.getObjectDatabase().listPacks().size());
  96. ins.flush();
  97. assertEquals(1, db.getObjectDatabase().listPacks().size());
  98. }
  99. @Test
  100. public void testReadFromInserterLargerObjects() throws IOException {
  101. db.getObjectDatabase().getReaderOptions().setStreamFileThreshold(512);
  102. DfsBlockCache.reconfigure(new DfsBlockCacheConfig()
  103. .setBlockSize(512)
  104. .setBlockLimit(2048));
  105. byte[] data = new TestRng(JGitTestUtil.getName()).nextBytes(8192);
  106. DfsInserter ins = (DfsInserter) db.newObjectInserter();
  107. ins.setCompressionLevel(Deflater.NO_COMPRESSION);
  108. ObjectId id1 = ins.insert(Constants.OBJ_BLOB, data);
  109. assertEquals(0, db.getObjectDatabase().listPacks().size());
  110. ObjectReader reader = ins.newReader();
  111. assertSame(ins, reader.getCreatedFromInserter());
  112. assertTrue(Arrays.equals(data, readStream(reader.open(id1))));
  113. assertEquals(0, db.getObjectDatabase().listPacks().size());
  114. ins.flush();
  115. List<DfsPackDescription> packs = db.getObjectDatabase().listPacks();
  116. assertEquals(1, packs.size());
  117. assertTrue(packs.get(0).getFileSize(PackExt.PACK) > 2048);
  118. }
  119. @Test
  120. public void testReadFromFallback() throws IOException {
  121. ObjectInserter ins = db.newObjectInserter();
  122. ObjectId id1 = ins.insert(Constants.OBJ_BLOB, Constants.encode("foo"));
  123. ins.flush();
  124. ObjectId id2 = ins.insert(Constants.OBJ_BLOB, Constants.encode("bar"));
  125. assertEquals(1, db.getObjectDatabase().listPacks().size());
  126. ObjectReader reader = ins.newReader();
  127. assertSame(ins, reader.getCreatedFromInserter());
  128. assertEquals("foo", readString(reader.open(id1)));
  129. assertEquals("bar", readString(reader.open(id2)));
  130. assertEquals(1, db.getObjectDatabase().listPacks().size());
  131. ins.flush();
  132. assertEquals(2, db.getObjectDatabase().listPacks().size());
  133. }
  134. @Test
  135. public void testReaderResolve() throws IOException {
  136. ObjectInserter ins = db.newObjectInserter();
  137. ObjectId id1 = ins.insert(Constants.OBJ_BLOB, Constants.encode("foo"));
  138. ins.flush();
  139. ObjectId id2 = ins.insert(Constants.OBJ_BLOB, Constants.encode("bar"));
  140. String abbr1 = ObjectId.toString(id1).substring(0, 4);
  141. String abbr2 = ObjectId.toString(id2).substring(0, 4);
  142. assertFalse(abbr1.equals(abbr2));
  143. ObjectReader reader = ins.newReader();
  144. assertSame(ins, reader.getCreatedFromInserter());
  145. Collection<ObjectId> objs;
  146. objs = reader.resolve(AbbreviatedObjectId.fromString(abbr1));
  147. assertEquals(1, objs.size());
  148. assertEquals(id1, objs.iterator().next());
  149. objs = reader.resolve(AbbreviatedObjectId.fromString(abbr2));
  150. assertEquals(1, objs.size());
  151. assertEquals(id2, objs.iterator().next());
  152. }
  153. @Test
  154. public void testGarbageSelectivelyVisible() throws IOException {
  155. ObjectInserter ins = db.newObjectInserter();
  156. ObjectId fooId = ins.insert(Constants.OBJ_BLOB, Constants.encode("foo"));
  157. ins.flush();
  158. assertEquals(1, db.getObjectDatabase().listPacks().size());
  159. // Make pack 0 garbage.
  160. db.getObjectDatabase().listPacks().get(0).setPackSource(PackSource.UNREACHABLE_GARBAGE);
  161. // Default behavior should be that the database has foo, because we allow garbage objects.
  162. assertTrue(db.getObjectDatabase().has(fooId));
  163. // But we should not be able to see it if we pass the right args.
  164. assertFalse(db.getObjectDatabase().has(fooId, true));
  165. }
  166. @Test
  167. public void testInserterIgnoresUnreachable() throws IOException {
  168. ObjectInserter ins = db.newObjectInserter();
  169. ObjectId fooId = ins.insert(Constants.OBJ_BLOB, Constants.encode("foo"));
  170. ins.flush();
  171. assertEquals(1, db.getObjectDatabase().listPacks().size());
  172. // Make pack 0 garbage.
  173. db.getObjectDatabase().listPacks().get(0).setPackSource(PackSource.UNREACHABLE_GARBAGE);
  174. // We shouldn't be able to see foo because it's garbage.
  175. assertFalse(db.getObjectDatabase().has(fooId, true));
  176. // But if we re-insert foo, it should become visible again.
  177. ins.insert(Constants.OBJ_BLOB, Constants.encode("foo"));
  178. ins.flush();
  179. assertTrue(db.getObjectDatabase().has(fooId, true));
  180. // Verify that we have a foo in both packs, and 1 of them is garbage.
  181. DfsReader reader = new DfsReader(db.getObjectDatabase());
  182. DfsPackFile packs[] = db.getObjectDatabase().getPacks();
  183. Set<PackSource> pack_sources = new HashSet<>();
  184. assertEquals(2, packs.length);
  185. pack_sources.add(packs[0].getPackDescription().getPackSource());
  186. pack_sources.add(packs[1].getPackDescription().getPackSource());
  187. assertTrue(packs[0].hasObject(reader, fooId));
  188. assertTrue(packs[1].hasObject(reader, fooId));
  189. assertTrue(pack_sources.contains(PackSource.UNREACHABLE_GARBAGE));
  190. assertTrue(pack_sources.contains(PackSource.INSERT));
  191. }
  192. @Test
  193. public void testNoCheckExisting() throws IOException {
  194. byte[] contents = Constants.encode("foo");
  195. ObjectId fooId;
  196. try (ObjectInserter ins = db.newObjectInserter()) {
  197. fooId = ins.insert(Constants.OBJ_BLOB, contents);
  198. ins.flush();
  199. }
  200. assertEquals(1, db.getObjectDatabase().listPacks().size());
  201. try (ObjectInserter ins = db.newObjectInserter()) {
  202. ((DfsInserter) ins).checkExisting(false);
  203. assertEquals(fooId, ins.insert(Constants.OBJ_BLOB, contents));
  204. ins.flush();
  205. }
  206. assertEquals(2, db.getObjectDatabase().listPacks().size());
  207. // Verify that we have a foo in both INSERT packs.
  208. DfsReader reader = new DfsReader(db.getObjectDatabase());
  209. DfsPackFile packs[] = db.getObjectDatabase().getPacks();
  210. assertEquals(2, packs.length);
  211. DfsPackFile p1 = packs[0];
  212. assertEquals(PackSource.INSERT, p1.getPackDescription().getPackSource());
  213. assertTrue(p1.hasObject(reader, fooId));
  214. DfsPackFile p2 = packs[1];
  215. assertEquals(PackSource.INSERT, p2.getPackDescription().getPackSource());
  216. assertTrue(p2.hasObject(reader, fooId));
  217. }
  218. private static String readString(ObjectLoader loader) throws IOException {
  219. return RawParseUtils.decode(readStream(loader));
  220. }
  221. private static byte[] readStream(ObjectLoader loader) throws IOException {
  222. ByteBuffer bb = IO.readWholeStream(loader.openStream(), 64);
  223. byte[] buf = new byte[bb.remaining()];
  224. bb.get(buf);
  225. return buf;
  226. }
  227. }