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

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