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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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.ByteArrayOutputStream;
  49. import java.io.EOFException;
  50. import java.io.IOException;
  51. import java.io.InputStream;
  52. import java.security.MessageDigest;
  53. import java.text.MessageFormat;
  54. import org.eclipse.jgit.JGitText;
  55. import org.eclipse.jgit.errors.ObjectWritingException;
  56. /**
  57. * Inserts objects into an existing {@code ObjectDatabase}.
  58. * <p>
  59. * An inserter is not thread-safe. Individual threads should each obtain their
  60. * own unique inserter instance, or must arrange for locking at a higher level
  61. * to ensure the inserter is in use by no more than one thread at a time.
  62. * <p>
  63. * Objects written by an inserter may not be immediately visible for reading
  64. * after the insert method completes. Callers must invoke either
  65. * {@link #release()} or {@link #flush()} prior to updating references or
  66. * otherwise making the returned ObjectIds visible to other code.
  67. */
  68. public abstract class ObjectInserter {
  69. /** An inserter that can be used for formatting and id generation only. */
  70. public static class Formatter extends ObjectInserter {
  71. @Override
  72. public ObjectId insert(int objectType, long length, InputStream in)
  73. throws IOException {
  74. throw new UnsupportedOperationException();
  75. }
  76. @Override
  77. public void flush() throws IOException {
  78. // Do nothing.
  79. }
  80. @Override
  81. public void release() {
  82. // Do nothing.
  83. }
  84. }
  85. /** Digest to compute the name of an object. */
  86. private final MessageDigest digest;
  87. /** Temporary working buffer for streaming data through. */
  88. private byte[] tempBuffer;
  89. /** Create a new inserter for a database. */
  90. protected ObjectInserter() {
  91. digest = Constants.newMessageDigest();
  92. }
  93. /** @return a temporary byte array for use by the caller. */
  94. protected byte[] buffer() {
  95. if (tempBuffer == null)
  96. tempBuffer = new byte[8192];
  97. return tempBuffer;
  98. }
  99. /** @return digest to help compute an ObjectId */
  100. protected MessageDigest digest() {
  101. digest.reset();
  102. return digest;
  103. }
  104. /**
  105. * Compute the name of an object, without inserting it.
  106. *
  107. * @param type
  108. * type code of the object to store.
  109. * @param data
  110. * complete content of the object.
  111. * @return the name of the object.
  112. */
  113. public ObjectId idFor(int type, byte[] data) {
  114. return idFor(type, data, 0, data.length);
  115. }
  116. /**
  117. * Compute the name of an object, without inserting it.
  118. *
  119. * @param type
  120. * type code of the object to store.
  121. * @param data
  122. * complete content of the object.
  123. * @param off
  124. * first position within {@code data}.
  125. * @param len
  126. * number of bytes to copy from {@code data}.
  127. * @return the name of the object.
  128. */
  129. public ObjectId idFor(int type, byte[] data, int off, int len) {
  130. MessageDigest md = digest();
  131. md.update(Constants.encodedTypeString(type));
  132. md.update((byte) ' ');
  133. md.update(Constants.encodeASCII(len));
  134. md.update((byte) 0);
  135. md.update(data, off, len);
  136. return ObjectId.fromRaw(md.digest());
  137. }
  138. /**
  139. * Compute the name of an object, without inserting it.
  140. *
  141. * @param objectType
  142. * type code of the object to store.
  143. * @param length
  144. * number of bytes to scan from {@code in}.
  145. * @param in
  146. * stream providing the object content. The caller is responsible
  147. * for closing the stream.
  148. * @return the name of the object.
  149. * @throws IOException
  150. * the source stream could not be read.
  151. */
  152. public ObjectId idFor(int objectType, long length, InputStream in)
  153. throws IOException {
  154. MessageDigest md = digest();
  155. md.update(Constants.encodedTypeString(objectType));
  156. md.update((byte) ' ');
  157. md.update(Constants.encodeASCII(length));
  158. md.update((byte) 0);
  159. byte[] buf = buffer();
  160. while (length > 0) {
  161. int n = in.read(buf, 0, (int) Math.min(length, buf.length));
  162. if (n < 0)
  163. throw new EOFException("Unexpected end of input");
  164. md.update(buf, 0, n);
  165. length -= n;
  166. }
  167. return ObjectId.fromRaw(md.digest());
  168. }
  169. /**
  170. * Insert a single commit into the store, returning its unique name.
  171. *
  172. * As a side effect, {@link CommitBuilder#getCommitId()} will also be
  173. * populated with the returned ObjectId.
  174. *
  175. * @param builder
  176. * the builder containing the proposed commit's data.
  177. * @return the name of the commit object.
  178. * @throws IOException
  179. * the object could not be stored.
  180. */
  181. public final ObjectId insert(CommitBuilder builder) throws IOException {
  182. return insert(Constants.OBJ_COMMIT, builder.format(this));
  183. }
  184. /**
  185. * Insert a single annotated tag into the store, returning its unique name.
  186. *
  187. * As a side effect, {@link TagBuilder#getTagId()} will also be populated
  188. * with the returned ObjectId.
  189. *
  190. * @param builder
  191. * the builder containing the proposed tag's data.
  192. * @return the name of the tag object.
  193. * @throws IOException
  194. * the object could not be stored.
  195. */
  196. public final ObjectId insert(TagBuilder builder) throws IOException {
  197. return insert(Constants.OBJ_TAG, builder.format(this));
  198. }
  199. /**
  200. * Insert a single object into the store, returning its unique name.
  201. *
  202. * @param type
  203. * type code of the object to store.
  204. * @param data
  205. * complete content of the object.
  206. * @return the name of the object.
  207. * @throws IOException
  208. * the object could not be stored.
  209. */
  210. public ObjectId insert(final int type, final byte[] data)
  211. throws IOException {
  212. return insert(type, data, 0, data.length);
  213. }
  214. /**
  215. * Insert a single object into the store, returning its unique name.
  216. *
  217. * @param type
  218. * type code of the object to store.
  219. * @param data
  220. * complete content of the object.
  221. * @param off
  222. * first position within {@code data}.
  223. * @param len
  224. * number of bytes to copy from {@code data}.
  225. * @return the name of the object.
  226. * @throws IOException
  227. * the object could not be stored.
  228. */
  229. public ObjectId insert(int type, byte[] data, int off, int len)
  230. throws IOException {
  231. return insert(type, len, new ByteArrayInputStream(data, off, len));
  232. }
  233. /**
  234. * Insert a single object into the store, returning its unique name.
  235. *
  236. * @param objectType
  237. * type code of the object to store.
  238. * @param length
  239. * number of bytes to copy from {@code in}.
  240. * @param in
  241. * stream providing the object content. The caller is responsible
  242. * for closing the stream.
  243. * @return the name of the object.
  244. * @throws IOException
  245. * the object could not be stored, or the source stream could
  246. * not be read.
  247. */
  248. public abstract ObjectId insert(int objectType, long length, InputStream in)
  249. throws IOException;
  250. /**
  251. * Make all inserted objects visible.
  252. * <p>
  253. * The flush may take some period of time to make the objects available to
  254. * other threads.
  255. *
  256. * @throws IOException
  257. * the flush could not be completed; objects inserted thus far
  258. * are in an indeterminate state.
  259. */
  260. public abstract void flush() throws IOException;
  261. /**
  262. * Release any resources used by this inserter.
  263. * <p>
  264. * An inserter that has been released can be used again, but may need to be
  265. * released after the subsequent usage.
  266. */
  267. public abstract void release();
  268. /**
  269. * Format a Tree in canonical format.
  270. *
  271. * @param tree
  272. * the tree object to format
  273. * @return canonical encoding of the tree object.
  274. * @throws IOException
  275. * the tree cannot be loaded, or its not in a writable state.
  276. */
  277. public final byte[] format(Tree tree) throws IOException {
  278. ByteArrayOutputStream o = new ByteArrayOutputStream();
  279. for (TreeEntry e : tree.members()) {
  280. ObjectId id = e.getId();
  281. if (id == null)
  282. throw new ObjectWritingException(MessageFormat.format(JGitText
  283. .get().objectAtPathDoesNotHaveId, e.getFullName()));
  284. e.getMode().copyTo(o);
  285. o.write(' ');
  286. o.write(e.getNameUTF8());
  287. o.write(0);
  288. id.copyRawTo(o);
  289. }
  290. return o.toByteArray();
  291. }
  292. }