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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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 void flush() throws IOException {
  79. // Do nothing.
  80. }
  81. @Override
  82. public void release() {
  83. // Do nothing.
  84. }
  85. }
  86. /** Digest to compute the name of an object. */
  87. private final MessageDigest digest;
  88. /** Temporary working buffer for streaming data through. */
  89. private byte[] tempBuffer;
  90. /** Create a new inserter for a database. */
  91. protected ObjectInserter() {
  92. digest = Constants.newMessageDigest();
  93. }
  94. /**
  95. * Obtain a temporary buffer for use by the ObjectInserter or its subclass.
  96. * <p>
  97. * This buffer is supplied by the ObjectInserter base class to itself and
  98. * its subclasses for the purposes of pulling data from a supplied
  99. * InputStream, passing it through a Deflater, or formatting the canonical
  100. * format of a small object like a small tree or commit.
  101. * <p>
  102. * <strong>This buffer IS NOT for translation such as auto-CRLF or content
  103. * filtering and must not be used for such purposes.</strong>
  104. * <p>
  105. * The returned buffer is small, around a few KiBs, and the size may change
  106. * between versions of JGit. Callers using this buffer must always check the
  107. * length of the returned array to ascertain how much space was provided.
  108. * <p>
  109. * There is a single buffer for each ObjectInserter, repeated calls to this
  110. * method will (usually) always return the same buffer. If the caller needs
  111. * more than one buffer, or needs a buffer of a larger size, it must manage
  112. * that buffer on its own.
  113. * <p>
  114. * The buffer is usually on first demand for a buffer.
  115. *
  116. * @return a temporary byte array for use by the caller.
  117. */
  118. protected byte[] buffer() {
  119. byte[] b = tempBuffer;
  120. if (b == null)
  121. tempBuffer = b = new byte[8192];
  122. return b;
  123. }
  124. /** @return digest to help compute an ObjectId */
  125. protected MessageDigest digest() {
  126. digest.reset();
  127. return digest;
  128. }
  129. /**
  130. * Compute the name of an object, without inserting it.
  131. *
  132. * @param type
  133. * type code of the object to store.
  134. * @param data
  135. * complete content of the object.
  136. * @return the name of the object.
  137. */
  138. public ObjectId idFor(int type, byte[] data) {
  139. return idFor(type, data, 0, data.length);
  140. }
  141. /**
  142. * Compute the name of an object, without inserting it.
  143. *
  144. * @param type
  145. * type code of the object to store.
  146. * @param data
  147. * complete content of the object.
  148. * @param off
  149. * first position within {@code data}.
  150. * @param len
  151. * number of bytes to copy from {@code data}.
  152. * @return the name of the object.
  153. */
  154. public ObjectId idFor(int type, byte[] data, int off, int len) {
  155. MessageDigest md = digest();
  156. md.update(Constants.encodedTypeString(type));
  157. md.update((byte) ' ');
  158. md.update(Constants.encodeASCII(len));
  159. md.update((byte) 0);
  160. md.update(data, off, len);
  161. return ObjectId.fromRaw(md.digest());
  162. }
  163. /**
  164. * Compute the name of an object, without inserting it.
  165. *
  166. * @param objectType
  167. * type code of the object to store.
  168. * @param length
  169. * number of bytes to scan from {@code in}.
  170. * @param in
  171. * stream providing the object content. The caller is responsible
  172. * for closing the stream.
  173. * @return the name of the object.
  174. * @throws IOException
  175. * the source stream could not be read.
  176. */
  177. public ObjectId idFor(int objectType, long length, InputStream in)
  178. throws IOException {
  179. MessageDigest md = digest();
  180. md.update(Constants.encodedTypeString(objectType));
  181. md.update((byte) ' ');
  182. md.update(Constants.encodeASCII(length));
  183. md.update((byte) 0);
  184. byte[] buf = buffer();
  185. while (length > 0) {
  186. int n = in.read(buf, 0, (int) Math.min(length, buf.length));
  187. if (n < 0)
  188. throw new EOFException("Unexpected end of input");
  189. md.update(buf, 0, n);
  190. length -= n;
  191. }
  192. return ObjectId.fromRaw(md.digest());
  193. }
  194. /**
  195. * Compute the ObjectId for the given tree without inserting it.
  196. *
  197. * @param formatter
  198. * @return the computed ObjectId
  199. */
  200. public ObjectId idFor(TreeFormatter formatter) {
  201. return formatter.computeId(this);
  202. }
  203. /**
  204. * Insert a single tree into the store, returning its unique name.
  205. *
  206. * @param formatter
  207. * the formatter containing the proposed tree's data.
  208. * @return the name of the tree object.
  209. * @throws IOException
  210. * the object could not be stored.
  211. */
  212. public final ObjectId insert(TreeFormatter formatter) throws IOException {
  213. // Delegate to the formatter, as then it can pass the raw internal
  214. // buffer back to this inserter, avoiding unnecessary data copying.
  215. //
  216. return formatter.insertTo(this);
  217. }
  218. /**
  219. * Insert a single commit into the store, returning its unique name.
  220. *
  221. * @param builder
  222. * the builder containing the proposed commit's data.
  223. * @return the name of the commit object.
  224. * @throws IOException
  225. * the object could not be stored.
  226. */
  227. public final ObjectId insert(CommitBuilder builder) throws IOException {
  228. return insert(Constants.OBJ_COMMIT, builder.build());
  229. }
  230. /**
  231. * Insert a single annotated tag into the store, returning its unique name.
  232. *
  233. * @param builder
  234. * the builder containing the proposed tag's data.
  235. * @return the name of the tag object.
  236. * @throws IOException
  237. * the object could not be stored.
  238. */
  239. public final ObjectId insert(TagBuilder builder) throws IOException {
  240. return insert(Constants.OBJ_TAG, builder.build());
  241. }
  242. /**
  243. * Insert a single object into the store, returning its unique name.
  244. *
  245. * @param type
  246. * type code of the object to store.
  247. * @param data
  248. * complete content of the object.
  249. * @return the name of the object.
  250. * @throws IOException
  251. * the object could not be stored.
  252. */
  253. public ObjectId insert(final int type, final byte[] data)
  254. throws IOException {
  255. return insert(type, data, 0, data.length);
  256. }
  257. /**
  258. * Insert a single object into the store, returning its unique name.
  259. *
  260. * @param type
  261. * type code of the object to store.
  262. * @param data
  263. * complete content of the object.
  264. * @param off
  265. * first position within {@code data}.
  266. * @param len
  267. * number of bytes to copy from {@code data}.
  268. * @return the name of the object.
  269. * @throws IOException
  270. * the object could not be stored.
  271. */
  272. public ObjectId insert(int type, byte[] data, int off, int len)
  273. throws IOException {
  274. return insert(type, len, new ByteArrayInputStream(data, off, len));
  275. }
  276. /**
  277. * Insert a single object into the store, returning its unique name.
  278. *
  279. * @param objectType
  280. * type code of the object to store.
  281. * @param length
  282. * number of bytes to copy from {@code in}.
  283. * @param in
  284. * stream providing the object content. The caller is responsible
  285. * for closing the stream.
  286. * @return the name of the object.
  287. * @throws IOException
  288. * the object could not be stored, or the source stream could
  289. * not be read.
  290. */
  291. public abstract ObjectId insert(int objectType, long length, InputStream in)
  292. throws IOException;
  293. /**
  294. * Initialize a parser to read from a pack formatted stream.
  295. *
  296. * @param in
  297. * the input stream. The stream is not closed by the parser, and
  298. * must instead be closed by the caller once parsing is complete.
  299. * @return the pack parser.
  300. * @throws IOException
  301. * the parser instance, which can be configured and then used to
  302. * parse objects into the ObjectDatabase.
  303. */
  304. public abstract PackParser newPackParser(InputStream in) throws IOException;
  305. /**
  306. * Make all inserted objects visible.
  307. * <p>
  308. * The flush may take some period of time to make the objects available to
  309. * other threads.
  310. *
  311. * @throws IOException
  312. * the flush could not be completed; objects inserted thus far
  313. * are in an indeterminate state.
  314. */
  315. public abstract void flush() throws IOException;
  316. /**
  317. * Release any resources used by this inserter.
  318. * <p>
  319. * An inserter that has been released can be used again, but may need to be
  320. * released after the subsequent usage.
  321. */
  322. public abstract void release();
  323. }