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.

ReftableCompactorTest.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. package org.eclipse.jgit.internal.storage.reftable;
  44. import static org.eclipse.jgit.lib.Constants.OBJECT_ID_LENGTH;
  45. import static org.eclipse.jgit.lib.Ref.Storage.NEW;
  46. import static org.eclipse.jgit.lib.Ref.Storage.PACKED;
  47. import static org.junit.Assert.assertEquals;
  48. import static org.junit.Assert.assertFalse;
  49. import static org.junit.Assert.assertTrue;
  50. import java.io.ByteArrayOutputStream;
  51. import java.io.IOException;
  52. import java.util.ArrayList;
  53. import java.util.Arrays;
  54. import java.util.List;
  55. import org.eclipse.jgit.internal.storage.io.BlockSource;
  56. import org.eclipse.jgit.internal.storage.reftable.ReftableWriter.Stats;
  57. import org.eclipse.jgit.lib.ObjectId;
  58. import org.eclipse.jgit.lib.ObjectIdRef;
  59. import org.eclipse.jgit.lib.Ref;
  60. import org.junit.Test;
  61. public class ReftableCompactorTest {
  62. private static final String MASTER = "refs/heads/master";
  63. private static final String NEXT = "refs/heads/next";
  64. @Test
  65. public void noTables() throws IOException {
  66. ReftableCompactor compactor;
  67. try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
  68. compactor = new ReftableCompactor(out);
  69. compactor.compact();
  70. }
  71. Stats stats = compactor.getStats();
  72. assertEquals(0, stats.minUpdateIndex());
  73. assertEquals(0, stats.maxUpdateIndex());
  74. assertEquals(0, stats.refCount());
  75. }
  76. @Test
  77. public void oneTable() throws IOException {
  78. byte[] inTab;
  79. try (ByteArrayOutputStream inBuf = new ByteArrayOutputStream()) {
  80. ReftableWriter writer = new ReftableWriter(inBuf)
  81. .setMinUpdateIndex(0)
  82. .setMaxUpdateIndex(0)
  83. .begin();
  84. writer.writeRef(ref(MASTER, 1));
  85. writer.finish();
  86. inTab = inBuf.toByteArray();
  87. }
  88. byte[] outTab;
  89. ReftableCompactor compactor;
  90. try (ByteArrayOutputStream outBuf = new ByteArrayOutputStream()) {
  91. compactor = new ReftableCompactor(outBuf);
  92. List<ReftableReader> readers = new ArrayList<>();
  93. readers.add(read(inTab));
  94. compactor.addAll(readers);
  95. compactor.compact();
  96. outTab = outBuf.toByteArray();
  97. }
  98. Stats stats = compactor.getStats();
  99. assertEquals(0, stats.minUpdateIndex());
  100. assertEquals(0, stats.maxUpdateIndex());
  101. assertEquals(1, stats.refCount());
  102. ReftableReader rr = read(outTab);
  103. try (RefCursor rc = rr.allRefs()) {
  104. assertTrue(rc.next());
  105. assertEquals(MASTER, rc.getRef().getName());
  106. assertEquals(id(1), rc.getRef().getObjectId());
  107. assertEquals(0, rc.getRef().getUpdateIndex());
  108. }
  109. }
  110. @Test
  111. public void twoTablesOneRef() throws IOException {
  112. byte[] inTab1;
  113. try (ByteArrayOutputStream inBuf = new ByteArrayOutputStream()) {
  114. ReftableWriter writer = new ReftableWriter(inBuf)
  115. .setMinUpdateIndex(0)
  116. .setMaxUpdateIndex(0)
  117. .begin();
  118. writer.writeRef(ref(MASTER, 1));
  119. writer.finish();
  120. inTab1 = inBuf.toByteArray();
  121. }
  122. byte[] inTab2;
  123. try (ByteArrayOutputStream inBuf = new ByteArrayOutputStream()) {
  124. ReftableWriter writer = new ReftableWriter(inBuf)
  125. .setMinUpdateIndex(1)
  126. .setMaxUpdateIndex(1)
  127. .begin();
  128. writer.writeRef(ref(MASTER, 2));
  129. writer.finish();
  130. inTab2 = inBuf.toByteArray();
  131. }
  132. byte[] outTab;
  133. ReftableCompactor compactor;
  134. try (ByteArrayOutputStream outBuf = new ByteArrayOutputStream()) {
  135. compactor = new ReftableCompactor(outBuf);
  136. compactor.addAll(Arrays.asList(read(inTab1), read(inTab2)));
  137. compactor.compact();
  138. outTab = outBuf.toByteArray();
  139. }
  140. Stats stats = compactor.getStats();
  141. assertEquals(0, stats.minUpdateIndex());
  142. assertEquals(1, stats.maxUpdateIndex());
  143. assertEquals(1, stats.refCount());
  144. ReftableReader rr = read(outTab);
  145. try (RefCursor rc = rr.allRefs()) {
  146. assertTrue(rc.next());
  147. assertEquals(MASTER, rc.getRef().getName());
  148. assertEquals(id(2), rc.getRef().getObjectId());
  149. assertEquals(1, rc.getRef().getUpdateIndex());
  150. }
  151. }
  152. @Test
  153. public void twoTablesTwoRefs() throws IOException {
  154. byte[] inTab1;
  155. try (ByteArrayOutputStream inBuf = new ByteArrayOutputStream()) {
  156. ReftableWriter writer = new ReftableWriter(inBuf)
  157. .setMinUpdateIndex(0)
  158. .setMaxUpdateIndex(0)
  159. .begin();
  160. writer.writeRef(ref(MASTER, 1));
  161. writer.writeRef(ref(NEXT, 2));
  162. writer.finish();
  163. inTab1 = inBuf.toByteArray();
  164. }
  165. byte[] inTab2;
  166. try (ByteArrayOutputStream inBuf = new ByteArrayOutputStream()) {
  167. ReftableWriter writer = new ReftableWriter(inBuf)
  168. .setMinUpdateIndex(1)
  169. .setMaxUpdateIndex(1)
  170. .begin();
  171. writer.writeRef(ref(MASTER, 3));
  172. writer.finish();
  173. inTab2 = inBuf.toByteArray();
  174. }
  175. byte[] outTab;
  176. ReftableCompactor compactor;
  177. try (ByteArrayOutputStream outBuf = new ByteArrayOutputStream()) {
  178. compactor = new ReftableCompactor(outBuf);
  179. compactor.addAll(Arrays.asList(read(inTab1), read(inTab2)));
  180. compactor.compact();
  181. outTab = outBuf.toByteArray();
  182. }
  183. Stats stats = compactor.getStats();
  184. assertEquals(0, stats.minUpdateIndex());
  185. assertEquals(1, stats.maxUpdateIndex());
  186. assertEquals(2, stats.refCount());
  187. ReftableReader rr = read(outTab);
  188. try (RefCursor rc = rr.allRefs()) {
  189. assertTrue(rc.next());
  190. assertEquals(MASTER, rc.getRef().getName());
  191. assertEquals(id(3), rc.getRef().getObjectId());
  192. assertEquals(1, rc.getRef().getUpdateIndex());
  193. assertTrue(rc.next());
  194. assertEquals(NEXT, rc.getRef().getName());
  195. assertEquals(id(2), rc.getRef().getObjectId());
  196. assertEquals(0, rc.getRef().getUpdateIndex());
  197. }
  198. }
  199. @Test
  200. public void twoTablesIncludeOneDelete() throws IOException {
  201. byte[] inTab1;
  202. try (ByteArrayOutputStream inBuf = new ByteArrayOutputStream()) {
  203. ReftableWriter writer = new ReftableWriter(inBuf)
  204. .setMinUpdateIndex(0)
  205. .setMaxUpdateIndex(0)
  206. .begin();
  207. writer.writeRef(ref(MASTER, 1));
  208. writer.finish();
  209. inTab1 = inBuf.toByteArray();
  210. }
  211. byte[] inTab2;
  212. try (ByteArrayOutputStream inBuf = new ByteArrayOutputStream()) {
  213. ReftableWriter writer = new ReftableWriter(inBuf)
  214. .setMinUpdateIndex(1)
  215. .setMaxUpdateIndex(1)
  216. .begin();
  217. writer.writeRef(tombstone(MASTER));
  218. writer.finish();
  219. inTab2 = inBuf.toByteArray();
  220. }
  221. byte[] outTab;
  222. ReftableCompactor compactor;
  223. try (ByteArrayOutputStream outBuf = new ByteArrayOutputStream()) {
  224. compactor = new ReftableCompactor(outBuf);
  225. compactor.setIncludeDeletes(true);
  226. compactor.addAll(Arrays.asList(read(inTab1), read(inTab2)));
  227. compactor.compact();
  228. outTab = outBuf.toByteArray();
  229. }
  230. Stats stats = compactor.getStats();
  231. assertEquals(0, stats.minUpdateIndex());
  232. assertEquals(1, stats.maxUpdateIndex());
  233. assertEquals(1, stats.refCount());
  234. ReftableReader rr = read(outTab);
  235. try (RefCursor rc = rr.allRefs()) {
  236. assertFalse(rc.next());
  237. }
  238. }
  239. @Test
  240. public void twoTablesNotIncludeOneDelete() throws IOException {
  241. byte[] inTab1;
  242. try (ByteArrayOutputStream inBuf = new ByteArrayOutputStream()) {
  243. ReftableWriter writer = new ReftableWriter(inBuf)
  244. .setMinUpdateIndex(0)
  245. .setMaxUpdateIndex(0)
  246. .begin();
  247. writer.writeRef(ref(MASTER, 1));
  248. writer.finish();
  249. inTab1 = inBuf.toByteArray();
  250. }
  251. byte[] inTab2;
  252. try (ByteArrayOutputStream inBuf = new ByteArrayOutputStream()) {
  253. ReftableWriter writer = new ReftableWriter(inBuf)
  254. .setMinUpdateIndex(1)
  255. .setMaxUpdateIndex(1)
  256. .begin();
  257. writer.writeRef(tombstone(MASTER));
  258. writer.finish();
  259. inTab2 = inBuf.toByteArray();
  260. }
  261. byte[] outTab;
  262. ReftableCompactor compactor;
  263. try (ByteArrayOutputStream outBuf = new ByteArrayOutputStream()) {
  264. compactor = new ReftableCompactor(outBuf);
  265. compactor.setIncludeDeletes(false);
  266. compactor.addAll(Arrays.asList(read(inTab1), read(inTab2)));
  267. compactor.compact();
  268. outTab = outBuf.toByteArray();
  269. }
  270. Stats stats = compactor.getStats();
  271. assertEquals(0, stats.minUpdateIndex());
  272. assertEquals(1, stats.maxUpdateIndex());
  273. assertEquals(0, stats.refCount());
  274. ReftableReader rr = read(outTab);
  275. try (RefCursor rc = rr.allRefs()) {
  276. assertFalse(rc.next());
  277. }
  278. }
  279. private static Ref ref(String name, int id) {
  280. return new ObjectIdRef.PeeledNonTag(PACKED, name, id(id));
  281. }
  282. private static Ref tombstone(String name) {
  283. return new ObjectIdRef.Unpeeled(NEW, name, null);
  284. }
  285. private static ObjectId id(int i) {
  286. byte[] buf = new byte[OBJECT_ID_LENGTH];
  287. buf[0] = (byte) (i & 0xff);
  288. buf[1] = (byte) ((i >>> 8) & 0xff);
  289. buf[2] = (byte) ((i >>> 16) & 0xff);
  290. buf[3] = (byte) (i >>> 24);
  291. return ObjectId.fromRaw(buf);
  292. }
  293. private static ReftableReader read(byte[] table) {
  294. return new ReftableReader(BlockSource.from(table));
  295. }
  296. }