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.

WindowCursor.java 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. * Copyright (C) 2008-2009, Google Inc.
  3. * Copyright (C) 2006-2008, Shawn O. Pearce <spearce@spearce.org>
  4. * and other copyright owners as documented in the project's IP log.
  5. *
  6. * This program and the accompanying materials are made available
  7. * under the terms of the Eclipse Distribution License v1.0 which
  8. * accompanies this distribution, is reproduced below, and is
  9. * available at http://www.eclipse.org/org/documents/edl-v10.php
  10. *
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials provided
  23. * with the distribution.
  24. *
  25. * - Neither the name of the Eclipse Foundation, Inc. nor the
  26. * names of its contributors may be used to endorse or promote
  27. * products derived from this software without specific prior
  28. * written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  31. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  32. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  33. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  35. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  37. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  39. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  42. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  43. */
  44. package org.eclipse.jgit.storage.file;
  45. import java.io.IOException;
  46. import java.util.Collection;
  47. import java.util.Collections;
  48. import java.util.HashSet;
  49. import java.util.zip.DataFormatException;
  50. import java.util.zip.Inflater;
  51. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  52. import org.eclipse.jgit.errors.MissingObjectException;
  53. import org.eclipse.jgit.errors.StoredObjectRepresentationNotAvailableException;
  54. import org.eclipse.jgit.lib.AbbreviatedObjectId;
  55. import org.eclipse.jgit.lib.AnyObjectId;
  56. import org.eclipse.jgit.lib.Constants;
  57. import org.eclipse.jgit.lib.InflaterCache;
  58. import org.eclipse.jgit.lib.ObjectId;
  59. import org.eclipse.jgit.lib.ObjectLoader;
  60. import org.eclipse.jgit.lib.ObjectReader;
  61. import org.eclipse.jgit.lib.ProgressMonitor;
  62. import org.eclipse.jgit.revwalk.RevObject;
  63. import org.eclipse.jgit.storage.pack.ObjectReuseAsIs;
  64. import org.eclipse.jgit.storage.pack.ObjectToPack;
  65. import org.eclipse.jgit.storage.pack.PackOutputStream;
  66. import org.eclipse.jgit.storage.pack.PackWriter;
  67. /** Active handle to a ByteWindow. */
  68. final class WindowCursor extends ObjectReader implements ObjectReuseAsIs {
  69. /** Temporary buffer large enough for at least one raw object id. */
  70. final byte[] tempId = new byte[Constants.OBJECT_ID_LENGTH];
  71. private Inflater inf;
  72. private ByteWindow window;
  73. final FileObjectDatabase db;
  74. WindowCursor(FileObjectDatabase db) {
  75. this.db = db;
  76. }
  77. @Override
  78. public ObjectReader newReader() {
  79. return new WindowCursor(db);
  80. }
  81. @Override
  82. public Collection<ObjectId> resolve(AbbreviatedObjectId id)
  83. throws IOException {
  84. if (id.isComplete())
  85. return Collections.singleton(id.toObjectId());
  86. HashSet<ObjectId> matches = new HashSet<ObjectId>(4);
  87. db.resolve(matches, id);
  88. return matches;
  89. }
  90. public boolean has(AnyObjectId objectId) throws IOException {
  91. return db.has(objectId);
  92. }
  93. public ObjectLoader open(AnyObjectId objectId, int typeHint)
  94. throws MissingObjectException, IncorrectObjectTypeException,
  95. IOException {
  96. final ObjectLoader ldr = db.openObject(this, objectId);
  97. if (ldr == null) {
  98. if (typeHint == OBJ_ANY)
  99. throw new MissingObjectException(objectId.copy(), "unknown");
  100. throw new MissingObjectException(objectId.copy(), typeHint);
  101. }
  102. if (typeHint != OBJ_ANY && ldr.getType() != typeHint)
  103. throw new IncorrectObjectTypeException(objectId.copy(), typeHint);
  104. return ldr;
  105. }
  106. public long getObjectSize(AnyObjectId objectId, int typeHint)
  107. throws MissingObjectException, IncorrectObjectTypeException,
  108. IOException {
  109. long sz = db.getObjectSize(this, objectId);
  110. if (sz < 0) {
  111. if (typeHint == OBJ_ANY)
  112. throw new MissingObjectException(objectId.copy(), "unknown");
  113. throw new MissingObjectException(objectId.copy(), typeHint);
  114. }
  115. return sz;
  116. }
  117. public LocalObjectToPack newObjectToPack(RevObject obj) {
  118. return new LocalObjectToPack(obj);
  119. }
  120. public void selectObjectRepresentation(PackWriter packer,
  121. ProgressMonitor monitor, Iterable<ObjectToPack> objects)
  122. throws IOException, MissingObjectException {
  123. for (ObjectToPack otp : objects) {
  124. db.selectObjectRepresentation(packer, otp, this);
  125. monitor.update(1);
  126. }
  127. }
  128. public void copyObjectAsIs(PackOutputStream out, ObjectToPack otp)
  129. throws IOException, StoredObjectRepresentationNotAvailableException {
  130. LocalObjectToPack src = (LocalObjectToPack) otp;
  131. src.pack.copyAsIs(out, src, this);
  132. }
  133. public void writeObjects(PackOutputStream out, Iterable<ObjectToPack> list)
  134. throws IOException {
  135. for (ObjectToPack otp : list)
  136. out.writeObject(otp);
  137. }
  138. /**
  139. * Copy bytes from the window to a caller supplied buffer.
  140. *
  141. * @param pack
  142. * the file the desired window is stored within.
  143. * @param position
  144. * position within the file to read from.
  145. * @param dstbuf
  146. * destination buffer to copy into.
  147. * @param dstoff
  148. * offset within <code>dstbuf</code> to start copying into.
  149. * @param cnt
  150. * number of bytes to copy. This value may exceed the number of
  151. * bytes remaining in the window starting at offset
  152. * <code>pos</code>.
  153. * @return number of bytes actually copied; this may be less than
  154. * <code>cnt</code> if <code>cnt</code> exceeded the number of bytes
  155. * available.
  156. * @throws IOException
  157. * this cursor does not match the provider or id and the proper
  158. * window could not be acquired through the provider's cache.
  159. */
  160. int copy(final PackFile pack, long position, final byte[] dstbuf,
  161. int dstoff, final int cnt) throws IOException {
  162. final long length = pack.length;
  163. int need = cnt;
  164. while (need > 0 && position < length) {
  165. pin(pack, position);
  166. final int r = window.copy(position, dstbuf, dstoff, need);
  167. position += r;
  168. dstoff += r;
  169. need -= r;
  170. }
  171. return cnt - need;
  172. }
  173. /**
  174. * Inflate a region of the pack starting at {@code position}.
  175. *
  176. * @param pack
  177. * the file the desired window is stored within.
  178. * @param position
  179. * position within the file to read from.
  180. * @param dstbuf
  181. * destination buffer the inflater should output decompressed
  182. * data to.
  183. * @param dstoff
  184. * current offset within <code>dstbuf</code> to inflate into.
  185. * @return updated <code>dstoff</code> based on the number of bytes
  186. * successfully inflated into <code>dstbuf</code>.
  187. * @throws IOException
  188. * this cursor does not match the provider or id and the proper
  189. * window could not be acquired through the provider's cache.
  190. * @throws DataFormatException
  191. * the inflater encountered an invalid chunk of data. Data
  192. * stream corruption is likely.
  193. */
  194. int inflate(final PackFile pack, long position, final byte[] dstbuf,
  195. int dstoff) throws IOException, DataFormatException {
  196. prepareInflater();
  197. pin(pack, position);
  198. position += window.setInput(position, inf);
  199. do {
  200. int n = inf.inflate(dstbuf, dstoff, dstbuf.length - dstoff);
  201. if (n == 0) {
  202. if (inf.needsInput()) {
  203. pin(pack, position);
  204. position += window.setInput(position, inf);
  205. } else if (inf.finished())
  206. return dstoff;
  207. else
  208. throw new DataFormatException();
  209. }
  210. dstoff += n;
  211. } while (dstoff < dstbuf.length);
  212. return dstoff;
  213. }
  214. ByteArrayWindow quickCopy(PackFile p, long pos, long cnt)
  215. throws IOException {
  216. pin(p, pos);
  217. if (window instanceof ByteArrayWindow
  218. && window.contains(p, pos + (cnt - 1)))
  219. return (ByteArrayWindow) window;
  220. return null;
  221. }
  222. Inflater inflater() {
  223. prepareInflater();
  224. return inf;
  225. }
  226. private void prepareInflater() {
  227. if (inf == null)
  228. inf = InflaterCache.get();
  229. else
  230. inf.reset();
  231. }
  232. void pin(final PackFile pack, final long position)
  233. throws IOException {
  234. final ByteWindow w = window;
  235. if (w == null || !w.contains(pack, position)) {
  236. // If memory is low, we may need what is in our window field to
  237. // be cleaned up by the GC during the get for the next window.
  238. // So we always clear it, even though we are just going to set
  239. // it again.
  240. //
  241. window = null;
  242. window = WindowCache.get(pack, position);
  243. }
  244. }
  245. int getStreamFileThreshold() {
  246. return WindowCache.getStreamFileThreshold();
  247. }
  248. /** Release the current window cursor. */
  249. public void release() {
  250. window = null;
  251. try {
  252. InflaterCache.release(inf);
  253. } finally {
  254. inf = null;
  255. }
  256. }
  257. }