Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

LargePackedDeltaObject.java 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. * Copyright (C) 2010, Google Inc.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.internal.storage.file;
  44. import java.io.BufferedInputStream;
  45. import java.io.File;
  46. import java.io.FileOutputStream;
  47. import java.io.IOException;
  48. import java.io.InputStream;
  49. import java.util.zip.DataFormatException;
  50. import java.util.zip.DeflaterOutputStream;
  51. import java.util.zip.InflaterInputStream;
  52. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  53. import org.eclipse.jgit.errors.LargeObjectException;
  54. import org.eclipse.jgit.errors.MissingObjectException;
  55. import org.eclipse.jgit.internal.storage.pack.BinaryDelta;
  56. import org.eclipse.jgit.internal.storage.pack.DeltaStream;
  57. import org.eclipse.jgit.lib.Constants;
  58. import org.eclipse.jgit.lib.ObjectId;
  59. import org.eclipse.jgit.lib.ObjectLoader;
  60. import org.eclipse.jgit.lib.ObjectStream;
  61. import org.eclipse.jgit.util.io.TeeInputStream;
  62. class LargePackedDeltaObject extends ObjectLoader {
  63. private static final long SIZE_UNKNOWN = -1;
  64. private int type;
  65. private long size;
  66. private final long objectOffset;
  67. private final long baseOffset;
  68. private final int headerLength;
  69. private final PackFile pack;
  70. private final FileObjectDatabase db;
  71. LargePackedDeltaObject(long objectOffset,
  72. long baseOffset, int headerLength, PackFile pack,
  73. FileObjectDatabase db) {
  74. this.type = Constants.OBJ_BAD;
  75. this.size = SIZE_UNKNOWN;
  76. this.objectOffset = objectOffset;
  77. this.baseOffset = baseOffset;
  78. this.headerLength = headerLength;
  79. this.pack = pack;
  80. this.db = db;
  81. }
  82. @Override
  83. public int getType() {
  84. if (type == Constants.OBJ_BAD) {
  85. WindowCursor wc = new WindowCursor(db);
  86. try {
  87. type = pack.getObjectType(wc, objectOffset);
  88. } catch (IOException packGone) {
  89. // If the pack file cannot be pinned into the cursor, it
  90. // probably was repacked recently. Go find the object
  91. // again and get the type from that location instead.
  92. //
  93. try {
  94. type = wc.open(getObjectId()).getType();
  95. } catch (IOException packGone2) {
  96. // "He's dead, Jim." We just can't discover the type
  97. // and the interface isn't supposed to be lazy here.
  98. // Report an invalid type code instead, callers will
  99. // wind up bailing out with an error at some point.
  100. }
  101. } finally {
  102. wc.release();
  103. }
  104. }
  105. return type;
  106. }
  107. @Override
  108. public long getSize() {
  109. if (size == SIZE_UNKNOWN) {
  110. WindowCursor wc = new WindowCursor(db);
  111. try {
  112. byte[] b = pack.getDeltaHeader(wc, objectOffset + headerLength);
  113. size = BinaryDelta.getResultSize(b);
  114. } catch (DataFormatException objectCorrupt) {
  115. // The zlib stream for the delta is corrupt. We probably
  116. // cannot access the object. Keep the size negative and
  117. // report that bogus result to the caller.
  118. } catch (IOException packGone) {
  119. // If the pack file cannot be pinned into the cursor, it
  120. // probably was repacked recently. Go find the object
  121. // again and get the size from that location instead.
  122. //
  123. try {
  124. size = wc.open(getObjectId()).getSize();
  125. } catch (IOException packGone2) {
  126. // "He's dead, Jim." We just can't discover the size
  127. // and the interface isn't supposed to be lazy here.
  128. // Report an invalid type code instead, callers will
  129. // wind up bailing out with an error at some point.
  130. }
  131. } finally {
  132. wc.release();
  133. }
  134. }
  135. return size;
  136. }
  137. @Override
  138. public boolean isLarge() {
  139. return true;
  140. }
  141. @Override
  142. public byte[] getCachedBytes() throws LargeObjectException {
  143. try {
  144. throw new LargeObjectException(getObjectId());
  145. } catch (IOException cannotObtainId) {
  146. LargeObjectException err = new LargeObjectException();
  147. err.initCause(cannotObtainId);
  148. throw err;
  149. }
  150. }
  151. @Override
  152. public ObjectStream openStream() throws MissingObjectException, IOException {
  153. // If the object was recently unpacked, its available loose.
  154. // The loose format is going to be faster to access than a
  155. // delta applied on top of a base. Use that whenever we can.
  156. //
  157. final ObjectId myId = getObjectId();
  158. final WindowCursor wc = new WindowCursor(db);
  159. ObjectLoader ldr = db.openLooseObject(wc, myId);
  160. if (ldr != null)
  161. return ldr.openStream();
  162. InputStream in = open(wc);
  163. in = new BufferedInputStream(in, 8192);
  164. // While we inflate the object, also deflate it back as a loose
  165. // object. This will later be cleaned up by a gc pass, but until
  166. // then we will reuse the loose form by the above code path.
  167. //
  168. int myType = getType();
  169. long mySize = getSize();
  170. final ObjectDirectoryInserter odi = db.newInserter();
  171. final File tmp = odi.newTempFile();
  172. DeflaterOutputStream dOut = odi.compress(new FileOutputStream(tmp));
  173. odi.writeHeader(dOut, myType, mySize);
  174. in = new TeeInputStream(in, dOut);
  175. return new ObjectStream.Filter(myType, mySize, in) {
  176. @Override
  177. public void close() throws IOException {
  178. super.close();
  179. odi.release();
  180. wc.release();
  181. db.insertUnpackedObject(tmp, myId, true /* force creation */);
  182. }
  183. };
  184. }
  185. private InputStream open(final WindowCursor wc)
  186. throws MissingObjectException, IOException,
  187. IncorrectObjectTypeException {
  188. InputStream delta;
  189. try {
  190. delta = new PackInputStream(pack, objectOffset + headerLength, wc);
  191. } catch (IOException packGone) {
  192. // If the pack file cannot be pinned into the cursor, it
  193. // probably was repacked recently. Go find the object
  194. // again and open the stream from that location instead.
  195. //
  196. return wc.open(getObjectId()).openStream();
  197. }
  198. delta = new InflaterInputStream(delta);
  199. final ObjectLoader base = pack.load(wc, baseOffset);
  200. DeltaStream ds = new DeltaStream(delta) {
  201. private long baseSize = SIZE_UNKNOWN;
  202. @Override
  203. protected InputStream openBase() throws IOException {
  204. InputStream in;
  205. if (base instanceof LargePackedDeltaObject)
  206. in = ((LargePackedDeltaObject) base).open(wc);
  207. else
  208. in = base.openStream();
  209. if (baseSize == SIZE_UNKNOWN) {
  210. if (in instanceof DeltaStream)
  211. baseSize = ((DeltaStream) in).getSize();
  212. else if (in instanceof ObjectStream)
  213. baseSize = ((ObjectStream) in).getSize();
  214. }
  215. return in;
  216. }
  217. @Override
  218. protected long getBaseSize() throws IOException {
  219. if (baseSize == SIZE_UNKNOWN) {
  220. // This code path should never be used as DeltaStream
  221. // is supposed to open the stream first, which would
  222. // initialize the size for us directly from the stream.
  223. baseSize = base.getSize();
  224. }
  225. return baseSize;
  226. }
  227. };
  228. if (type == Constants.OBJ_BAD) {
  229. if (!(base instanceof LargePackedDeltaObject))
  230. type = base.getType();
  231. }
  232. if (size == SIZE_UNKNOWN)
  233. size = ds.getSize();
  234. return ds;
  235. }
  236. private ObjectId getObjectId() throws IOException {
  237. return pack.findObjectForOffset(objectOffset);
  238. }
  239. }