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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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. return delegate().newReader();
  128. }
  129. public void flush() throws IOException {
  130. delegate().flush();
  131. }
  132. public void close() {
  133. delegate().close();
  134. }
  135. }
  136. /** Digest to compute the name of an object. */
  137. private final MessageDigest digest;
  138. /** Temporary working buffer for streaming data through. */
  139. private byte[] tempBuffer;
  140. /** Create a new inserter for a database. */
  141. protected ObjectInserter() {
  142. digest = Constants.newMessageDigest();
  143. }
  144. /**
  145. * Obtain a temporary buffer for use by the ObjectInserter or its subclass.
  146. * <p>
  147. * This buffer is supplied by the ObjectInserter base class to itself and
  148. * its subclasses for the purposes of pulling data from a supplied
  149. * InputStream, passing it through a Deflater, or formatting the canonical
  150. * format of a small object like a small tree or commit.
  151. * <p>
  152. * <strong>This buffer IS NOT for translation such as auto-CRLF or content
  153. * filtering and must not be used for such purposes.</strong>
  154. * <p>
  155. * The returned buffer is small, around a few KiBs, and the size may change
  156. * between versions of JGit. Callers using this buffer must always check the
  157. * length of the returned array to ascertain how much space was provided.
  158. * <p>
  159. * There is a single buffer for each ObjectInserter, repeated calls to this
  160. * method will (usually) always return the same buffer. If the caller needs
  161. * more than one buffer, or needs a buffer of a larger size, it must manage
  162. * that buffer on its own.
  163. * <p>
  164. * The buffer is usually on first demand for a buffer.
  165. *
  166. * @return a temporary byte array for use by the caller.
  167. */
  168. protected byte[] buffer() {
  169. byte[] b = tempBuffer;
  170. if (b == null)
  171. tempBuffer = b = new byte[8192];
  172. return b;
  173. }
  174. /** @return digest to help compute an ObjectId */
  175. protected MessageDigest digest() {
  176. digest.reset();
  177. return digest;
  178. }
  179. /**
  180. * Compute the name of an object, without inserting it.
  181. *
  182. * @param type
  183. * type code of the object to store.
  184. * @param data
  185. * complete content of the object.
  186. * @return the name of the object.
  187. */
  188. public ObjectId idFor(int type, byte[] data) {
  189. return idFor(type, data, 0, data.length);
  190. }
  191. /**
  192. * Compute the name of an object, without inserting it.
  193. *
  194. * @param type
  195. * type code of the object to store.
  196. * @param data
  197. * complete content of the object.
  198. * @param off
  199. * first position within {@code data}.
  200. * @param len
  201. * number of bytes to copy from {@code data}.
  202. * @return the name of the object.
  203. */
  204. public ObjectId idFor(int type, byte[] data, int off, int len) {
  205. MessageDigest md = digest();
  206. md.update(Constants.encodedTypeString(type));
  207. md.update((byte) ' ');
  208. md.update(Constants.encodeASCII(len));
  209. md.update((byte) 0);
  210. md.update(data, off, len);
  211. return ObjectId.fromRaw(md.digest());
  212. }
  213. /**
  214. * Compute the name of an object, without inserting it.
  215. *
  216. * @param objectType
  217. * type code of the object to store.
  218. * @param length
  219. * number of bytes to scan from {@code in}.
  220. * @param in
  221. * stream providing the object content. The caller is responsible
  222. * for closing the stream.
  223. * @return the name of the object.
  224. * @throws IOException
  225. * the source stream could not be read.
  226. */
  227. public ObjectId idFor(int objectType, long length, InputStream in)
  228. throws IOException {
  229. MessageDigest md = digest();
  230. md.update(Constants.encodedTypeString(objectType));
  231. md.update((byte) ' ');
  232. md.update(Constants.encodeASCII(length));
  233. md.update((byte) 0);
  234. byte[] buf = buffer();
  235. while (length > 0) {
  236. int n = in.read(buf, 0, (int) Math.min(length, buf.length));
  237. if (n < 0)
  238. throw new EOFException(JGitText.get().unexpectedEndOfInput);
  239. md.update(buf, 0, n);
  240. length -= n;
  241. }
  242. return ObjectId.fromRaw(md.digest());
  243. }
  244. /**
  245. * Compute the ObjectId for the given tree without inserting it.
  246. *
  247. * @param formatter
  248. * @return the computed ObjectId
  249. */
  250. public ObjectId idFor(TreeFormatter formatter) {
  251. return formatter.computeId(this);
  252. }
  253. /**
  254. * Insert a single tree into the store, returning its unique name.
  255. *
  256. * @param formatter
  257. * the formatter containing the proposed tree's data.
  258. * @return the name of the tree object.
  259. * @throws IOException
  260. * the object could not be stored.
  261. */
  262. public final ObjectId insert(TreeFormatter formatter) throws IOException {
  263. // Delegate to the formatter, as then it can pass the raw internal
  264. // buffer back to this inserter, avoiding unnecessary data copying.
  265. //
  266. return formatter.insertTo(this);
  267. }
  268. /**
  269. * Insert a single commit into the store, returning its unique name.
  270. *
  271. * @param builder
  272. * the builder containing the proposed commit's data.
  273. * @return the name of the commit object.
  274. * @throws IOException
  275. * the object could not be stored.
  276. */
  277. public final ObjectId insert(CommitBuilder builder) throws IOException {
  278. return insert(Constants.OBJ_COMMIT, builder.build());
  279. }
  280. /**
  281. * Insert a single annotated tag into the store, returning its unique name.
  282. *
  283. * @param builder
  284. * the builder containing the proposed tag's data.
  285. * @return the name of the tag object.
  286. * @throws IOException
  287. * the object could not be stored.
  288. */
  289. public final ObjectId insert(TagBuilder builder) throws IOException {
  290. return insert(Constants.OBJ_TAG, builder.build());
  291. }
  292. /**
  293. * Insert a single object into the store, returning its unique name.
  294. *
  295. * @param type
  296. * type code of the object to store.
  297. * @param data
  298. * complete content of the object.
  299. * @return the name of the object.
  300. * @throws IOException
  301. * the object could not be stored.
  302. */
  303. public ObjectId insert(final int type, final byte[] data)
  304. throws IOException {
  305. return insert(type, data, 0, data.length);
  306. }
  307. /**
  308. * Insert a single object into the store, returning its unique name.
  309. *
  310. * @param type
  311. * type code of the object to store.
  312. * @param data
  313. * complete content of the object.
  314. * @param off
  315. * first position within {@code data}.
  316. * @param len
  317. * number of bytes to copy from {@code data}.
  318. * @return the name of the object.
  319. * @throws IOException
  320. * the object could not be stored.
  321. */
  322. public ObjectId insert(int type, byte[] data, int off, int len)
  323. throws IOException {
  324. return insert(type, len, new ByteArrayInputStream(data, off, len));
  325. }
  326. /**
  327. * Insert a single object into the store, returning its unique name.
  328. *
  329. * @param objectType
  330. * type code of the object to store.
  331. * @param length
  332. * number of bytes to copy from {@code in}.
  333. * @param in
  334. * stream providing the object content. The caller is responsible
  335. * for closing the stream.
  336. * @return the name of the object.
  337. * @throws IOException
  338. * the object could not be stored, or the source stream could
  339. * not be read.
  340. */
  341. public abstract ObjectId insert(int objectType, long length, InputStream in)
  342. throws IOException;
  343. /**
  344. * Initialize a parser to read from a pack formatted stream.
  345. *
  346. * @param in
  347. * the input stream. The stream is not closed by the parser, and
  348. * must instead be closed by the caller once parsing is complete.
  349. * @return the pack parser.
  350. * @throws IOException
  351. * the parser instance, which can be configured and then used to
  352. * parse objects into the ObjectDatabase.
  353. */
  354. public abstract PackParser newPackParser(InputStream in) throws IOException;
  355. /**
  356. * Open a reader for objects that may have been written by this inserter.
  357. * <p>
  358. * The returned reader allows the calling thread to read back recently
  359. * inserted objects without first calling {@code flush()} to make them
  360. * visible to the repository. The returned reader should only be used from
  361. * the same thread as the inserter. Objects written by this inserter may not
  362. * be visible to {@code this.newReader().newReader()}.
  363. *
  364. * @since 3.5
  365. * @return reader for any object, including an object recently inserted by
  366. * this inserter since the last flush.
  367. */
  368. public abstract ObjectReader newReader();
  369. /**
  370. * Make all inserted objects visible.
  371. * <p>
  372. * The flush may take some period of time to make the objects available to
  373. * other threads.
  374. *
  375. * @throws IOException
  376. * the flush could not be completed; objects inserted thus far
  377. * are in an indeterminate state.
  378. */
  379. public abstract void flush() throws IOException;
  380. /**
  381. * Release any resources used by this inserter.
  382. * <p>
  383. * An inserter that has been released can be used again, but may need to be
  384. * released after the subsequent usage.
  385. *
  386. * @since 4.0
  387. */
  388. @Override
  389. public abstract void close();
  390. }