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

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