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.

ObjectInserter.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /*
  2. * Copyright (C) 2007, Robin Rosenberg <robin.rosenberg@dewire.com>
  3. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
  4. * Copyright (C) 2009, Google Inc.
  5. * Copyright (C) 2010, Chris Aniszczyk <caniszczyk@gmail.com>
  6. * and other copyright owners as documented in the project's IP log.
  7. *
  8. * This program and the accompanying materials are made available
  9. * under the terms of the Eclipse Distribution License v1.0 which
  10. * accompanies this distribution, is reproduced below, and is
  11. * available at http://www.eclipse.org/org/documents/edl-v10.php
  12. *
  13. * All rights reserved.
  14. *
  15. * Redistribution and use in source and binary forms, with or
  16. * without modification, are permitted provided that the following
  17. * conditions are met:
  18. *
  19. * - Redistributions of source code must retain the above copyright
  20. * notice, this list of conditions and the following disclaimer.
  21. *
  22. * - Redistributions in binary form must reproduce the above
  23. * copyright notice, this list of conditions and the following
  24. * disclaimer in the documentation and/or other materials provided
  25. * with the distribution.
  26. *
  27. * - Neither the name of the Eclipse Foundation, Inc. nor the
  28. * names of its contributors may be used to endorse or promote
  29. * products derived from this software without specific prior
  30. * written permission.
  31. *
  32. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  33. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  34. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  35. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  36. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  37. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  38. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  39. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  40. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  41. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  42. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  43. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  44. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45. */
  46. package org.eclipse.jgit.lib;
  47. import java.io.ByteArrayInputStream;
  48. import java.io.EOFException;
  49. import java.io.IOException;
  50. import java.io.InputStream;
  51. import java.security.MessageDigest;
  52. import org.eclipse.jgit.internal.JGitText;
  53. import org.eclipse.jgit.transport.PackParser;
  54. /**
  55. * Inserts objects into an existing {@code ObjectDatabase}.
  56. * <p>
  57. * An inserter is not thread-safe. Individual threads should each obtain their
  58. * own unique inserter instance, or must arrange for locking at a higher level
  59. * to ensure the inserter is in use by no more than one thread at a time.
  60. * <p>
  61. * Objects written by an inserter may not be immediately visible for reading
  62. * after the insert method completes. Callers must invoke either
  63. * {@link #close()} or {@link #flush()} prior to updating references or
  64. * otherwise making the returned ObjectIds visible to other code.
  65. */
  66. public abstract class ObjectInserter implements AutoCloseable {
  67. /** An inserter that can be used for formatting and id generation only. */
  68. public static class Formatter extends ObjectInserter {
  69. @Override
  70. public ObjectId insert(int objectType, long length, InputStream in)
  71. throws IOException {
  72. throw new UnsupportedOperationException();
  73. }
  74. @Override
  75. public PackParser newPackParser(InputStream in) throws IOException {
  76. throw new UnsupportedOperationException();
  77. }
  78. @Override
  79. public ObjectReader newReader() {
  80. throw new UnsupportedOperationException();
  81. }
  82. @Override
  83. public void flush() throws IOException {
  84. // Do nothing.
  85. }
  86. @Override
  87. public void close() {
  88. // Do nothing.
  89. }
  90. }
  91. /** Wraps a delegate ObjectInserter. */
  92. public static abstract class Filter extends ObjectInserter {
  93. /** @return delegate ObjectInserter to handle all processing. */
  94. protected abstract ObjectInserter delegate();
  95. @Override
  96. protected byte[] buffer() {
  97. return delegate().buffer();
  98. }
  99. public ObjectId idFor(int type, byte[] data) {
  100. return delegate().idFor(type, data);
  101. }
  102. public ObjectId idFor(int type, byte[] data, int off, int len) {
  103. return delegate().idFor(type, data, off, len);
  104. }
  105. public ObjectId idFor(int objectType, long length, InputStream in)
  106. throws IOException {
  107. return delegate().idFor(objectType, length, in);
  108. }
  109. public ObjectId idFor(TreeFormatter formatter) {
  110. return delegate().idFor(formatter);
  111. }
  112. public ObjectId insert(int type, byte[] data) throws IOException {
  113. return delegate().insert(type, data);
  114. }
  115. public ObjectId insert(int type, byte[] data, int off, int len)
  116. throws IOException {
  117. return delegate().insert(type, data, off, len);
  118. }
  119. public ObjectId insert(int objectType, long length, InputStream in)
  120. throws IOException {
  121. return delegate().insert(objectType, length, in);
  122. }
  123. public PackParser newPackParser(InputStream in) throws IOException {
  124. return delegate().newPackParser(in);
  125. }
  126. public ObjectReader newReader() {
  127. final ObjectReader dr = delegate().newReader();
  128. return new ObjectReader.Filter() {
  129. @Override
  130. protected ObjectReader delegate() {
  131. return dr;
  132. }
  133. @Override
  134. public ObjectInserter getCreatedFromInserter() {
  135. return ObjectInserter.Filter.this;
  136. }
  137. };
  138. }
  139. public void flush() throws IOException {
  140. delegate().flush();
  141. }
  142. public void close() {
  143. delegate().close();
  144. }
  145. }
  146. /** Digest to compute the name of an object. */
  147. private final MessageDigest digest;
  148. /** Temporary working buffer for streaming data through. */
  149. private byte[] tempBuffer;
  150. /** Create a new inserter for a database. */
  151. protected ObjectInserter() {
  152. digest = Constants.newMessageDigest();
  153. }
  154. /**
  155. * Obtain a temporary buffer for use by the ObjectInserter or its subclass.
  156. * <p>
  157. * This buffer is supplied by the ObjectInserter base class to itself and
  158. * its subclasses for the purposes of pulling data from a supplied
  159. * InputStream, passing it through a Deflater, or formatting the canonical
  160. * format of a small object like a small tree or commit.
  161. * <p>
  162. * <strong>This buffer IS NOT for translation such as auto-CRLF or content
  163. * filtering and must not be used for such purposes.</strong>
  164. * <p>
  165. * The returned buffer is small, around a few KiBs, and the size may change
  166. * between versions of JGit. Callers using this buffer must always check the
  167. * length of the returned array to ascertain how much space was provided.
  168. * <p>
  169. * There is a single buffer for each ObjectInserter, repeated calls to this
  170. * method will (usually) always return the same buffer. If the caller needs
  171. * more than one buffer, or needs a buffer of a larger size, it must manage
  172. * that buffer on its own.
  173. * <p>
  174. * The buffer is usually on first demand for a buffer.
  175. *
  176. * @return a temporary byte array for use by the caller.
  177. */
  178. protected byte[] buffer() {
  179. byte[] b = tempBuffer;
  180. if (b == null)
  181. tempBuffer = b = new byte[8192];
  182. return b;
  183. }
  184. /** @return digest to help compute an ObjectId */
  185. protected MessageDigest digest() {
  186. digest.reset();
  187. return digest;
  188. }
  189. /**
  190. * Compute the name of an object, without inserting it.
  191. *
  192. * @param type
  193. * type code of the object to store.
  194. * @param data
  195. * complete content of the object.
  196. * @return the name of the object.
  197. */
  198. public ObjectId idFor(int type, byte[] data) {
  199. return idFor(type, data, 0, data.length);
  200. }
  201. /**
  202. * Compute the name of an object, without inserting it.
  203. *
  204. * @param type
  205. * type code of the object to store.
  206. * @param data
  207. * complete content of the object.
  208. * @param off
  209. * first position within {@code data}.
  210. * @param len
  211. * number of bytes to copy from {@code data}.
  212. * @return the name of the object.
  213. */
  214. public ObjectId idFor(int type, byte[] data, int off, int len) {
  215. MessageDigest md = digest();
  216. md.update(Constants.encodedTypeString(type));
  217. md.update((byte) ' ');
  218. md.update(Constants.encodeASCII(len));
  219. md.update((byte) 0);
  220. md.update(data, off, len);
  221. return ObjectId.fromRaw(md.digest());
  222. }
  223. /**
  224. * Compute the name of an object, without inserting it.
  225. *
  226. * @param objectType
  227. * type code of the object to store.
  228. * @param length
  229. * number of bytes to scan from {@code in}.
  230. * @param in
  231. * stream providing the object content. The caller is responsible
  232. * for closing the stream.
  233. * @return the name of the object.
  234. * @throws IOException
  235. * the source stream could not be read.
  236. */
  237. public ObjectId idFor(int objectType, long length, InputStream in)
  238. throws IOException {
  239. MessageDigest md = digest();
  240. md.update(Constants.encodedTypeString(objectType));
  241. md.update((byte) ' ');
  242. md.update(Constants.encodeASCII(length));
  243. md.update((byte) 0);
  244. byte[] buf = buffer();
  245. while (length > 0) {
  246. int n = in.read(buf, 0, (int) Math.min(length, buf.length));
  247. if (n < 0)
  248. throw new EOFException(JGitText.get().unexpectedEndOfInput);
  249. md.update(buf, 0, n);
  250. length -= n;
  251. }
  252. return ObjectId.fromRaw(md.digest());
  253. }
  254. /**
  255. * Compute the ObjectId for the given tree without inserting it.
  256. *
  257. * @param formatter
  258. * @return the computed ObjectId
  259. */
  260. public ObjectId idFor(TreeFormatter formatter) {
  261. return formatter.computeId(this);
  262. }
  263. /**
  264. * Insert a single tree into the store, returning its unique name.
  265. *
  266. * @param formatter
  267. * the formatter containing the proposed tree's data.
  268. * @return the name of the tree object.
  269. * @throws IOException
  270. * the object could not be stored.
  271. */
  272. public final ObjectId insert(TreeFormatter formatter) throws IOException {
  273. // Delegate to the formatter, as then it can pass the raw internal
  274. // buffer back to this inserter, avoiding unnecessary data copying.
  275. //
  276. return formatter.insertTo(this);
  277. }
  278. /**
  279. * Insert a single commit into the store, returning its unique name.
  280. *
  281. * @param builder
  282. * the builder containing the proposed commit's data.
  283. * @return the name of the commit object.
  284. * @throws IOException
  285. * the object could not be stored.
  286. */
  287. public final ObjectId insert(CommitBuilder builder) throws IOException {
  288. return insert(Constants.OBJ_COMMIT, builder.build());
  289. }
  290. /**
  291. * Insert a single annotated tag into the store, returning its unique name.
  292. *
  293. * @param builder
  294. * the builder containing the proposed tag's data.
  295. * @return the name of the tag object.
  296. * @throws IOException
  297. * the object could not be stored.
  298. */
  299. public final ObjectId insert(TagBuilder builder) throws IOException {
  300. return insert(Constants.OBJ_TAG, builder.build());
  301. }
  302. /**
  303. * Insert a single object into the store, returning its unique name.
  304. *
  305. * @param type
  306. * type code of the object to store.
  307. * @param data
  308. * complete content of the object.
  309. * @return the name of the object.
  310. * @throws IOException
  311. * the object could not be stored.
  312. */
  313. public ObjectId insert(final int type, final byte[] data)
  314. throws IOException {
  315. return insert(type, data, 0, data.length);
  316. }
  317. /**
  318. * Insert a single object into the store, returning its unique name.
  319. *
  320. * @param type
  321. * type code of the object to store.
  322. * @param data
  323. * complete content of the object.
  324. * @param off
  325. * first position within {@code data}.
  326. * @param len
  327. * number of bytes to copy from {@code data}.
  328. * @return the name of the object.
  329. * @throws IOException
  330. * the object could not be stored.
  331. */
  332. public ObjectId insert(int type, byte[] data, int off, int len)
  333. throws IOException {
  334. return insert(type, len, new ByteArrayInputStream(data, off, len));
  335. }
  336. /**
  337. * Insert a single object into the store, returning its unique name.
  338. *
  339. * @param objectType
  340. * type code of the object to store.
  341. * @param length
  342. * number of bytes to copy from {@code in}.
  343. * @param in
  344. * stream providing the object content. The caller is responsible
  345. * for closing the stream.
  346. * @return the name of the object.
  347. * @throws IOException
  348. * the object could not be stored, or the source stream could
  349. * not be read.
  350. */
  351. public abstract ObjectId insert(int objectType, long length, InputStream in)
  352. throws IOException;
  353. /**
  354. * Initialize a parser to read from a pack formatted stream.
  355. *
  356. * @param in
  357. * the input stream. The stream is not closed by the parser, and
  358. * must instead be closed by the caller once parsing is complete.
  359. * @return the pack parser.
  360. * @throws IOException
  361. * the parser instance, which can be configured and then used to
  362. * parse objects into the ObjectDatabase.
  363. */
  364. public abstract PackParser newPackParser(InputStream in) throws IOException;
  365. /**
  366. * Open a reader for objects that may have been written by this inserter.
  367. * <p>
  368. * The returned reader allows the calling thread to read back recently
  369. * inserted objects without first calling {@code flush()} to make them
  370. * visible to the repository. The returned reader should only be used from
  371. * the same thread as the inserter. Objects written by this inserter may not
  372. * be visible to {@code this.newReader().newReader()}.
  373. * <p>
  374. * The returned reader should return this inserter instance from {@link
  375. * ObjectReader#getCreatedFromInserter()}.
  376. *
  377. * @since 3.5
  378. * @return reader for any object, including an object recently inserted by
  379. * this inserter since the last flush.
  380. */
  381. public abstract ObjectReader newReader();
  382. /**
  383. * Make all inserted objects visible.
  384. * <p>
  385. * The flush may take some period of time to make the objects available to
  386. * other threads.
  387. *
  388. * @throws IOException
  389. * the flush could not be completed; objects inserted thus far
  390. * are in an indeterminate state.
  391. */
  392. public abstract void flush() throws IOException;
  393. /**
  394. * Release any resources used by this inserter.
  395. * <p>
  396. * An inserter that has been released can be used again, but may need to be
  397. * released after the subsequent usage.
  398. *
  399. * @since 4.0
  400. */
  401. @Override
  402. public abstract void close();
  403. }