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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /*
  2. * Copyright (C) 2008-2009, Google Inc.
  3. * Copyright (C) 2006-2008, Shawn O. Pearce <spearce@spearce.org> and others
  4. *
  5. * This program and the accompanying materials are made available under the
  6. * terms of the Eclipse Distribution License v. 1.0 which is available at
  7. * https://www.eclipse.org/org/documents/edl-v10.php.
  8. *
  9. * SPDX-License-Identifier: BSD-3-Clause
  10. */
  11. package org.eclipse.jgit.internal.storage.file;
  12. import java.io.IOException;
  13. import java.util.Collection;
  14. import java.util.Collections;
  15. import java.util.HashSet;
  16. import java.util.List;
  17. import java.util.Set;
  18. import java.util.zip.DataFormatException;
  19. import java.util.zip.Inflater;
  20. import org.eclipse.jgit.annotations.Nullable;
  21. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  22. import org.eclipse.jgit.errors.MissingObjectException;
  23. import org.eclipse.jgit.errors.StoredObjectRepresentationNotAvailableException;
  24. import org.eclipse.jgit.errors.StoredPackRepresentationNotAvailableException;
  25. import org.eclipse.jgit.internal.JGitText;
  26. import org.eclipse.jgit.internal.storage.pack.CachedPack;
  27. import org.eclipse.jgit.internal.storage.pack.ObjectReuseAsIs;
  28. import org.eclipse.jgit.internal.storage.pack.ObjectToPack;
  29. import org.eclipse.jgit.internal.storage.pack.PackOutputStream;
  30. import org.eclipse.jgit.internal.storage.pack.PackWriter;
  31. import org.eclipse.jgit.lib.AbbreviatedObjectId;
  32. import org.eclipse.jgit.lib.AnyObjectId;
  33. import org.eclipse.jgit.lib.BitmapIndex;
  34. import org.eclipse.jgit.lib.BitmapIndex.BitmapBuilder;
  35. import org.eclipse.jgit.lib.Constants;
  36. import org.eclipse.jgit.lib.InflaterCache;
  37. import org.eclipse.jgit.lib.ObjectId;
  38. import org.eclipse.jgit.lib.ObjectInserter;
  39. import org.eclipse.jgit.lib.ObjectLoader;
  40. import org.eclipse.jgit.lib.ObjectReader;
  41. import org.eclipse.jgit.lib.ProgressMonitor;
  42. /** Active handle to a ByteWindow. */
  43. final class WindowCursor extends ObjectReader implements ObjectReuseAsIs {
  44. /** Temporary buffer large enough for at least one raw object id. */
  45. final byte[] tempId = new byte[Constants.OBJECT_ID_LENGTH];
  46. private Inflater inf;
  47. private ByteWindow window;
  48. private DeltaBaseCache baseCache;
  49. @Nullable
  50. private final ObjectInserter createdFromInserter;
  51. final FileObjectDatabase db;
  52. WindowCursor(FileObjectDatabase db) {
  53. this.db = db;
  54. this.createdFromInserter = null;
  55. this.streamFileThreshold = WindowCache.getStreamFileThreshold();
  56. }
  57. WindowCursor(FileObjectDatabase db,
  58. @Nullable ObjectDirectoryInserter createdFromInserter) {
  59. this.db = db;
  60. this.createdFromInserter = createdFromInserter;
  61. this.streamFileThreshold = WindowCache.getStreamFileThreshold();
  62. }
  63. DeltaBaseCache getDeltaBaseCache() {
  64. if (baseCache == null)
  65. baseCache = new DeltaBaseCache();
  66. return baseCache;
  67. }
  68. /** {@inheritDoc} */
  69. @Override
  70. public ObjectReader newReader() {
  71. return new WindowCursor(db);
  72. }
  73. /** {@inheritDoc} */
  74. @Override
  75. public BitmapIndex getBitmapIndex() throws IOException {
  76. for (Pack pack : db.getPacks()) {
  77. PackBitmapIndex index = pack.getBitmapIndex();
  78. if (index != null)
  79. return new BitmapIndexImpl(index);
  80. }
  81. return null;
  82. }
  83. /** {@inheritDoc} */
  84. @Override
  85. public Collection<CachedPack> getCachedPacksAndUpdate(
  86. BitmapBuilder needBitmap) throws IOException {
  87. for (Pack pack : db.getPacks()) {
  88. PackBitmapIndex index = pack.getBitmapIndex();
  89. if (needBitmap.removeAllOrNone(index))
  90. return Collections.<CachedPack> singletonList(
  91. new LocalCachedPack(Collections.singletonList(pack)));
  92. }
  93. return Collections.emptyList();
  94. }
  95. /** {@inheritDoc} */
  96. @Override
  97. public Collection<ObjectId> resolve(AbbreviatedObjectId id)
  98. throws IOException {
  99. if (id.isComplete())
  100. return Collections.singleton(id.toObjectId());
  101. HashSet<ObjectId> matches = new HashSet<>(4);
  102. db.resolve(matches, id);
  103. return matches;
  104. }
  105. /** {@inheritDoc} */
  106. @Override
  107. public boolean has(AnyObjectId objectId) throws IOException {
  108. return db.has(objectId);
  109. }
  110. /** {@inheritDoc} */
  111. @Override
  112. public ObjectLoader open(AnyObjectId objectId, int typeHint)
  113. throws MissingObjectException, IncorrectObjectTypeException,
  114. IOException {
  115. final ObjectLoader ldr = db.openObject(this, objectId);
  116. if (ldr == null) {
  117. if (typeHint == OBJ_ANY)
  118. throw new MissingObjectException(objectId.copy(),
  119. JGitText.get().unknownObjectType2);
  120. throw new MissingObjectException(objectId.copy(), typeHint);
  121. }
  122. if (typeHint != OBJ_ANY && ldr.getType() != typeHint)
  123. throw new IncorrectObjectTypeException(objectId.copy(), typeHint);
  124. return ldr;
  125. }
  126. /** {@inheritDoc} */
  127. @Override
  128. public Set<ObjectId> getShallowCommits() throws IOException {
  129. return db.getShallowCommits();
  130. }
  131. /** {@inheritDoc} */
  132. @Override
  133. public long getObjectSize(AnyObjectId objectId, int typeHint)
  134. throws MissingObjectException, IncorrectObjectTypeException,
  135. IOException {
  136. long sz = db.getObjectSize(this, objectId);
  137. if (sz < 0) {
  138. if (typeHint == OBJ_ANY)
  139. throw new MissingObjectException(objectId.copy(),
  140. JGitText.get().unknownObjectType2);
  141. throw new MissingObjectException(objectId.copy(), typeHint);
  142. }
  143. return sz;
  144. }
  145. /** {@inheritDoc} */
  146. @Override
  147. public LocalObjectToPack newObjectToPack(AnyObjectId objectId, int type) {
  148. return new LocalObjectToPack(objectId, type);
  149. }
  150. /** {@inheritDoc} */
  151. @Override
  152. public void selectObjectRepresentation(PackWriter packer,
  153. ProgressMonitor monitor, Iterable<ObjectToPack> objects)
  154. throws IOException, MissingObjectException {
  155. for (ObjectToPack otp : objects) {
  156. db.selectObjectRepresentation(packer, otp, this);
  157. monitor.update(1);
  158. }
  159. }
  160. /** {@inheritDoc} */
  161. @Override
  162. public void copyObjectAsIs(PackOutputStream out, ObjectToPack otp,
  163. boolean validate) throws IOException,
  164. StoredObjectRepresentationNotAvailableException {
  165. LocalObjectToPack src = (LocalObjectToPack) otp;
  166. src.pack.copyAsIs(out, src, validate, this);
  167. }
  168. /** {@inheritDoc} */
  169. @Override
  170. public void writeObjects(PackOutputStream out, List<ObjectToPack> list)
  171. throws IOException {
  172. for (ObjectToPack otp : list)
  173. out.writeObject(otp);
  174. }
  175. /**
  176. * Copy bytes from the window to a caller supplied buffer.
  177. *
  178. * @param pack
  179. * the file the desired window is stored within.
  180. * @param position
  181. * position within the file to read from.
  182. * @param dstbuf
  183. * destination buffer to copy into.
  184. * @param dstoff
  185. * offset within <code>dstbuf</code> to start copying into.
  186. * @param cnt
  187. * number of bytes to copy. This value may exceed the number of
  188. * bytes remaining in the window starting at offset
  189. * <code>pos</code>.
  190. * @return number of bytes actually copied; this may be less than
  191. * <code>cnt</code> if <code>cnt</code> exceeded the number of bytes
  192. * available.
  193. * @throws IOException
  194. * this cursor does not match the provider or id and the proper
  195. * window could not be acquired through the provider's cache.
  196. */
  197. int copy(final Pack pack, long position, final byte[] dstbuf,
  198. int dstoff, final int cnt) throws IOException {
  199. final long length = pack.length;
  200. int need = cnt;
  201. while (need > 0 && position < length) {
  202. pin(pack, position);
  203. final int r = window.copy(position, dstbuf, dstoff, need);
  204. position += r;
  205. dstoff += r;
  206. need -= r;
  207. }
  208. return cnt - need;
  209. }
  210. /** {@inheritDoc} */
  211. @Override
  212. public void copyPackAsIs(PackOutputStream out, CachedPack pack)
  213. throws IOException, StoredPackRepresentationNotAvailableException {
  214. ((LocalCachedPack) pack).copyAsIs(out, this);
  215. }
  216. void copyPackAsIs(final Pack pack, final long length,
  217. final PackOutputStream out) throws IOException, StoredPackRepresentationNotAvailableException {
  218. long position = 12;
  219. long remaining = length - (12 + 20);
  220. while (0 < remaining) {
  221. try {
  222. pin(pack, position);
  223. int ptr = (int) (position - window.start);
  224. int n = (int) Math.min(window.size() - ptr, remaining);
  225. window.write(out, position, n);
  226. position += n;
  227. remaining -= n;
  228. } catch (Exception e) {
  229. throw new StoredPackRepresentationNotAvailableException(pack,
  230. e);
  231. }
  232. }
  233. }
  234. /**
  235. * Inflate a region of the pack starting at {@code position}.
  236. *
  237. * @param pack
  238. * the file the desired window is stored within.
  239. * @param position
  240. * position within the file to read from.
  241. * @param dstbuf
  242. * destination buffer the inflater should output decompressed
  243. * data to. Must be large enough to store the entire stream,
  244. * unless headerOnly is true.
  245. * @param headerOnly
  246. * if true the caller wants only {@code dstbuf.length} bytes.
  247. * @return number of bytes inflated into <code>dstbuf</code>.
  248. * @throws IOException
  249. * this cursor does not match the provider or id and the proper
  250. * window could not be acquired through the provider's cache.
  251. * @throws DataFormatException
  252. * the inflater encountered an invalid chunk of data. Data
  253. * stream corruption is likely.
  254. */
  255. int inflate(final Pack pack, long position, final byte[] dstbuf,
  256. boolean headerOnly) throws IOException, DataFormatException {
  257. prepareInflater();
  258. pin(pack, position);
  259. position += window.setInput(position, inf);
  260. for (int dstoff = 0;;) {
  261. int n = inf.inflate(dstbuf, dstoff, dstbuf.length - dstoff);
  262. dstoff += n;
  263. if (inf.finished() || (headerOnly && dstoff == dstbuf.length))
  264. return dstoff;
  265. if (inf.needsInput()) {
  266. pin(pack, position);
  267. position += window.setInput(position, inf);
  268. } else if (n == 0)
  269. throw new DataFormatException();
  270. }
  271. }
  272. ByteArrayWindow quickCopy(Pack p, long pos, long cnt)
  273. throws IOException {
  274. pin(p, pos);
  275. if (window instanceof ByteArrayWindow
  276. && window.contains(p, pos + (cnt - 1)))
  277. return (ByteArrayWindow) window;
  278. return null;
  279. }
  280. Inflater inflater() {
  281. prepareInflater();
  282. return inf;
  283. }
  284. private void prepareInflater() {
  285. if (inf == null)
  286. inf = InflaterCache.get();
  287. else
  288. inf.reset();
  289. }
  290. void pin(Pack pack, long position)
  291. throws IOException {
  292. final ByteWindow w = window;
  293. if (w == null || !w.contains(pack, position)) {
  294. // If memory is low, we may need what is in our window field to
  295. // be cleaned up by the GC during the get for the next window.
  296. // So we always clear it, even though we are just going to set
  297. // it again.
  298. //
  299. window = null;
  300. window = WindowCache.get(pack, position);
  301. }
  302. }
  303. /** {@inheritDoc} */
  304. @Override
  305. @Nullable
  306. public ObjectInserter getCreatedFromInserter() {
  307. return createdFromInserter;
  308. }
  309. /**
  310. * {@inheritDoc}
  311. * <p>
  312. * Release the current window cursor.
  313. */
  314. @Override
  315. public void close() {
  316. window = null;
  317. baseCache = null;
  318. try {
  319. InflaterCache.release(inf);
  320. } finally {
  321. inf = null;
  322. }
  323. }
  324. }