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 15KB

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