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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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.List;
  50. import java.util.zip.DataFormatException;
  51. import java.util.zip.Inflater;
  52. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  53. import org.eclipse.jgit.errors.MissingObjectException;
  54. import org.eclipse.jgit.errors.StoredObjectRepresentationNotAvailableException;
  55. import org.eclipse.jgit.lib.AbbreviatedObjectId;
  56. import org.eclipse.jgit.lib.AnyObjectId;
  57. import org.eclipse.jgit.lib.Constants;
  58. import org.eclipse.jgit.lib.InflaterCache;
  59. import org.eclipse.jgit.lib.ObjectId;
  60. import org.eclipse.jgit.lib.ObjectLoader;
  61. import org.eclipse.jgit.lib.ObjectReader;
  62. import org.eclipse.jgit.lib.ProgressMonitor;
  63. import org.eclipse.jgit.revwalk.RevObject;
  64. import org.eclipse.jgit.storage.pack.ObjectReuseAsIs;
  65. import org.eclipse.jgit.storage.pack.ObjectToPack;
  66. import org.eclipse.jgit.storage.pack.PackOutputStream;
  67. import org.eclipse.jgit.storage.pack.PackWriter;
  68. /** Active handle to a ByteWindow. */
  69. final class WindowCursor extends ObjectReader implements ObjectReuseAsIs {
  70. /** Temporary buffer large enough for at least one raw object id. */
  71. final byte[] tempId = new byte[Constants.OBJECT_ID_LENGTH];
  72. private Inflater inf;
  73. private ByteWindow window;
  74. final FileObjectDatabase db;
  75. WindowCursor(FileObjectDatabase db) {
  76. this.db = db;
  77. }
  78. @Override
  79. public ObjectReader newReader() {
  80. return new WindowCursor(db);
  81. }
  82. @Override
  83. public Collection<ObjectId> resolve(AbbreviatedObjectId id)
  84. throws IOException {
  85. if (id.isComplete())
  86. return Collections.singleton(id.toObjectId());
  87. HashSet<ObjectId> matches = new HashSet<ObjectId>(4);
  88. db.resolve(matches, id);
  89. return matches;
  90. }
  91. public boolean has(AnyObjectId objectId) throws IOException {
  92. return db.has(objectId);
  93. }
  94. public ObjectLoader open(AnyObjectId objectId, int typeHint)
  95. throws MissingObjectException, IncorrectObjectTypeException,
  96. IOException {
  97. final ObjectLoader ldr = db.openObject(this, objectId);
  98. if (ldr == null) {
  99. if (typeHint == OBJ_ANY)
  100. throw new MissingObjectException(objectId.copy(), "unknown");
  101. throw new MissingObjectException(objectId.copy(), typeHint);
  102. }
  103. if (typeHint != OBJ_ANY && ldr.getType() != typeHint)
  104. throw new IncorrectObjectTypeException(objectId.copy(), typeHint);
  105. return ldr;
  106. }
  107. public long getObjectSize(AnyObjectId objectId, int typeHint)
  108. throws MissingObjectException, IncorrectObjectTypeException,
  109. IOException {
  110. long sz = db.getObjectSize(this, objectId);
  111. if (sz < 0) {
  112. if (typeHint == OBJ_ANY)
  113. throw new MissingObjectException(objectId.copy(), "unknown");
  114. throw new MissingObjectException(objectId.copy(), typeHint);
  115. }
  116. return sz;
  117. }
  118. public LocalObjectToPack newObjectToPack(RevObject obj) {
  119. return new LocalObjectToPack(obj);
  120. }
  121. public void selectObjectRepresentation(PackWriter packer,
  122. ProgressMonitor monitor, Iterable<ObjectToPack> objects)
  123. throws IOException, MissingObjectException {
  124. for (ObjectToPack otp : objects) {
  125. db.selectObjectRepresentation(packer, otp, this);
  126. monitor.update(1);
  127. }
  128. }
  129. public void copyObjectAsIs(PackOutputStream out, ObjectToPack otp)
  130. throws IOException, StoredObjectRepresentationNotAvailableException {
  131. LocalObjectToPack src = (LocalObjectToPack) otp;
  132. src.pack.copyAsIs(out, src, this);
  133. }
  134. public void writeObjects(PackOutputStream out, List<ObjectToPack> list)
  135. throws IOException {
  136. for (ObjectToPack otp : list)
  137. out.writeObject(otp);
  138. }
  139. /**
  140. * Copy bytes from the window to a caller supplied buffer.
  141. *
  142. * @param pack
  143. * the file the desired window is stored within.
  144. * @param position
  145. * position within the file to read from.
  146. * @param dstbuf
  147. * destination buffer to copy into.
  148. * @param dstoff
  149. * offset within <code>dstbuf</code> to start copying into.
  150. * @param cnt
  151. * number of bytes to copy. This value may exceed the number of
  152. * bytes remaining in the window starting at offset
  153. * <code>pos</code>.
  154. * @return number of bytes actually copied; this may be less than
  155. * <code>cnt</code> if <code>cnt</code> exceeded the number of bytes
  156. * available.
  157. * @throws IOException
  158. * this cursor does not match the provider or id and the proper
  159. * window could not be acquired through the provider's cache.
  160. */
  161. int copy(final PackFile pack, long position, final byte[] dstbuf,
  162. int dstoff, final int cnt) throws IOException {
  163. final long length = pack.length;
  164. int need = cnt;
  165. while (need > 0 && position < length) {
  166. pin(pack, position);
  167. final int r = window.copy(position, dstbuf, dstoff, need);
  168. position += r;
  169. dstoff += r;
  170. need -= r;
  171. }
  172. return cnt - need;
  173. }
  174. /**
  175. * Inflate a region of the pack starting at {@code position}.
  176. *
  177. * @param pack
  178. * the file the desired window is stored within.
  179. * @param position
  180. * position within the file to read from.
  181. * @param dstbuf
  182. * destination buffer the inflater should output decompressed
  183. * data to.
  184. * @param dstoff
  185. * current offset within <code>dstbuf</code> to inflate into.
  186. * @return updated <code>dstoff</code> based on the number of bytes
  187. * successfully inflated into <code>dstbuf</code>.
  188. * @throws IOException
  189. * this cursor does not match the provider or id and the proper
  190. * window could not be acquired through the provider's cache.
  191. * @throws DataFormatException
  192. * the inflater encountered an invalid chunk of data. Data
  193. * stream corruption is likely.
  194. */
  195. int inflate(final PackFile pack, long position, final byte[] dstbuf,
  196. int dstoff) throws IOException, DataFormatException {
  197. prepareInflater();
  198. pin(pack, position);
  199. position += window.setInput(position, inf);
  200. do {
  201. int n = inf.inflate(dstbuf, dstoff, dstbuf.length - dstoff);
  202. if (n == 0) {
  203. if (inf.needsInput()) {
  204. pin(pack, position);
  205. position += window.setInput(position, inf);
  206. } else if (inf.finished())
  207. return dstoff;
  208. else
  209. throw new DataFormatException();
  210. }
  211. dstoff += n;
  212. } while (dstoff < dstbuf.length);
  213. return dstoff;
  214. }
  215. ByteArrayWindow quickCopy(PackFile p, long pos, long cnt)
  216. throws IOException {
  217. pin(p, pos);
  218. if (window instanceof ByteArrayWindow
  219. && window.contains(p, pos + (cnt - 1)))
  220. return (ByteArrayWindow) window;
  221. return null;
  222. }
  223. Inflater inflater() {
  224. prepareInflater();
  225. return inf;
  226. }
  227. private void prepareInflater() {
  228. if (inf == null)
  229. inf = InflaterCache.get();
  230. else
  231. inf.reset();
  232. }
  233. void pin(final PackFile pack, final long position)
  234. throws IOException {
  235. final ByteWindow w = window;
  236. if (w == null || !w.contains(pack, position)) {
  237. // If memory is low, we may need what is in our window field to
  238. // be cleaned up by the GC during the get for the next window.
  239. // So we always clear it, even though we are just going to set
  240. // it again.
  241. //
  242. window = null;
  243. window = WindowCache.get(pack, position);
  244. }
  245. }
  246. int getStreamFileThreshold() {
  247. return WindowCache.getStreamFileThreshold();
  248. }
  249. /** Release the current window cursor. */
  250. public void release() {
  251. window = null;
  252. try {
  253. InflaterCache.release(inf);
  254. } finally {
  255. inf = null;
  256. }
  257. }
  258. }