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.

ObjectReader.java 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  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.lib;
  44. import java.io.IOException;
  45. import java.util.ArrayList;
  46. import java.util.Collection;
  47. import java.util.Iterator;
  48. import java.util.List;
  49. import java.util.Set;
  50. import org.eclipse.jgit.annotations.Nullable;
  51. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  52. import org.eclipse.jgit.errors.MissingObjectException;
  53. /**
  54. * Reads an {@link org.eclipse.jgit.lib.ObjectDatabase} for a single thread.
  55. * <p>
  56. * Readers that can support efficient reuse of pack encoded objects should also
  57. * implement the companion interface
  58. * {@link org.eclipse.jgit.internal.storage.pack.ObjectReuseAsIs}.
  59. */
  60. public abstract class ObjectReader implements AutoCloseable {
  61. /** Type hint indicating the caller doesn't know the type. */
  62. public static final int OBJ_ANY = -1;
  63. /**
  64. * The threshold at which a file will be streamed rather than loaded
  65. * entirely into memory.
  66. * @since 4.6
  67. */
  68. protected int streamFileThreshold;
  69. /**
  70. * Construct a new reader from the same data.
  71. * <p>
  72. * Applications can use this method to build a new reader from the same data
  73. * source, but for an different thread.
  74. *
  75. * @return a brand new reader, using the same data source.
  76. */
  77. public abstract ObjectReader newReader();
  78. /**
  79. * Obtain a unique abbreviation (prefix) of an object SHA-1.
  80. *
  81. * This method uses a reasonable default for the minimum length. Callers who
  82. * don't care about the minimum length should prefer this method.
  83. *
  84. * The returned abbreviation would expand back to the argument ObjectId when
  85. * passed to {@link #resolve(AbbreviatedObjectId)}, assuming no new objects
  86. * are added to this repository between calls.
  87. *
  88. * @param objectId
  89. * object identity that needs to be abbreviated.
  90. * @return SHA-1 abbreviation.
  91. * @throws java.io.IOException
  92. * the object store cannot be read.
  93. */
  94. public AbbreviatedObjectId abbreviate(AnyObjectId objectId)
  95. throws IOException {
  96. return abbreviate(objectId, 7);
  97. }
  98. /**
  99. * Obtain a unique abbreviation (prefix) of an object SHA-1.
  100. *
  101. * The returned abbreviation would expand back to the argument ObjectId when
  102. * passed to {@link #resolve(AbbreviatedObjectId)}, assuming no new objects
  103. * are added to this repository between calls.
  104. *
  105. * The default implementation of this method abbreviates the id to the
  106. * minimum length, then resolves it to see if there are multiple results.
  107. * When multiple results are found, the length is extended by 1 and resolve
  108. * is tried again.
  109. *
  110. * @param objectId
  111. * object identity that needs to be abbreviated.
  112. * @param len
  113. * minimum length of the abbreviated string. Must be in the range
  114. * [2, {@value Constants#OBJECT_ID_STRING_LENGTH}].
  115. * @return SHA-1 abbreviation. If no matching objects exist in the
  116. * repository, the abbreviation will match the minimum length.
  117. * @throws java.io.IOException
  118. * the object store cannot be read.
  119. */
  120. public AbbreviatedObjectId abbreviate(AnyObjectId objectId, int len)
  121. throws IOException {
  122. if (len == Constants.OBJECT_ID_STRING_LENGTH)
  123. return AbbreviatedObjectId.fromObjectId(objectId);
  124. AbbreviatedObjectId abbrev = objectId.abbreviate(len);
  125. Collection<ObjectId> matches = resolve(abbrev);
  126. while (1 < matches.size() && len < Constants.OBJECT_ID_STRING_LENGTH) {
  127. abbrev = objectId.abbreviate(++len);
  128. List<ObjectId> n = new ArrayList<>(8);
  129. for (ObjectId candidate : matches) {
  130. if (abbrev.prefixCompare(candidate) == 0)
  131. n.add(candidate);
  132. }
  133. if (1 < n.size())
  134. matches = n;
  135. else
  136. matches = resolve(abbrev);
  137. }
  138. return abbrev;
  139. }
  140. /**
  141. * Resolve an abbreviated ObjectId to its full form.
  142. *
  143. * This method searches for an ObjectId that begins with the abbreviation,
  144. * and returns at least some matching candidates.
  145. *
  146. * If the returned collection is empty, no objects start with this
  147. * abbreviation. The abbreviation doesn't belong to this repository, or the
  148. * repository lacks the necessary objects to complete it.
  149. *
  150. * If the collection contains exactly one member, the abbreviation is
  151. * (currently) unique within this database. There is a reasonably high
  152. * probability that the returned id is what was previously abbreviated.
  153. *
  154. * If the collection contains 2 or more members, the abbreviation is not
  155. * unique. In this case the implementation is only required to return at
  156. * least 2 candidates to signal the abbreviation has conflicts. User
  157. * friendly implementations should return as many candidates as reasonably
  158. * possible, as the caller may be able to disambiguate further based on
  159. * context. However since databases can be very large (e.g. 10 million
  160. * objects) returning 625,000 candidates for the abbreviation "0" is simply
  161. * unreasonable, so implementors should draw the line at around 256 matches.
  162. *
  163. * @param id
  164. * abbreviated id to resolve to a complete identity. The
  165. * abbreviation must have a length of at least 2.
  166. * @return candidates that begin with the abbreviated identity.
  167. * @throws java.io.IOException
  168. * the object store cannot be read.
  169. */
  170. public abstract Collection<ObjectId> resolve(AbbreviatedObjectId id)
  171. throws IOException;
  172. /**
  173. * Does the requested object exist in this database?
  174. *
  175. * @param objectId
  176. * identity of the object to test for existence of.
  177. * @return true if the specified object is stored in this database.
  178. * @throws java.io.IOException
  179. * the object store cannot be accessed.
  180. */
  181. public boolean has(AnyObjectId objectId) throws IOException {
  182. return has(objectId, OBJ_ANY);
  183. }
  184. /**
  185. * Does the requested object exist in this database?
  186. *
  187. * @param objectId
  188. * identity of the object to test for existence of.
  189. * @param typeHint
  190. * hint about the type of object being requested, e.g.
  191. * {@link org.eclipse.jgit.lib.Constants#OBJ_BLOB};
  192. * {@link #OBJ_ANY} if the object type is not known, or does not
  193. * matter to the caller.
  194. * @return true if the specified object is stored in this database.
  195. * @throws IncorrectObjectTypeException
  196. * typeHint was not OBJ_ANY, and the object's actual type does
  197. * not match typeHint.
  198. * @throws java.io.IOException
  199. * the object store cannot be accessed.
  200. */
  201. public boolean has(AnyObjectId objectId, int typeHint) throws IOException {
  202. try {
  203. open(objectId, typeHint);
  204. return true;
  205. } catch (MissingObjectException notFound) {
  206. return false;
  207. }
  208. }
  209. /**
  210. * Open an object from this database.
  211. *
  212. * @param objectId
  213. * identity of the object to open.
  214. * @return a {@link org.eclipse.jgit.lib.ObjectLoader} for accessing the
  215. * object.
  216. * @throws org.eclipse.jgit.errors.MissingObjectException
  217. * the object does not exist.
  218. * @throws java.io.IOException
  219. * the object store cannot be accessed.
  220. */
  221. public ObjectLoader open(AnyObjectId objectId)
  222. throws MissingObjectException, IOException {
  223. return open(objectId, OBJ_ANY);
  224. }
  225. /**
  226. * Open an object from this database.
  227. *
  228. * @param objectId
  229. * identity of the object to open.
  230. * @param typeHint
  231. * hint about the type of object being requested, e.g.
  232. * {@link org.eclipse.jgit.lib.Constants#OBJ_BLOB};
  233. * {@link #OBJ_ANY} if the object type is not known, or does not
  234. * matter to the caller.
  235. * @return a {@link org.eclipse.jgit.lib.ObjectLoader} for accessing the
  236. * object.
  237. * @throws org.eclipse.jgit.errors.MissingObjectException
  238. * the object does not exist.
  239. * @throws org.eclipse.jgit.errors.IncorrectObjectTypeException
  240. * typeHint was not OBJ_ANY, and the object's actual type does
  241. * not match typeHint.
  242. * @throws java.io.IOException
  243. * the object store cannot be accessed.
  244. */
  245. public abstract ObjectLoader open(AnyObjectId objectId, int typeHint)
  246. throws MissingObjectException, IncorrectObjectTypeException,
  247. IOException;
  248. /**
  249. * Returns IDs for those commits which should be considered as shallow.
  250. *
  251. * @return IDs of shallow commits
  252. * @throws java.io.IOException
  253. */
  254. public abstract Set<ObjectId> getShallowCommits() throws IOException;
  255. /**
  256. * Asynchronous object opening.
  257. *
  258. * @param objectIds
  259. * objects to open from the object store. The supplied collection
  260. * must not be modified until the queue has finished.
  261. * @param reportMissing
  262. * if true missing objects are reported by calling failure with a
  263. * MissingObjectException. This may be more expensive for the
  264. * implementation to guarantee. If false the implementation may
  265. * choose to report MissingObjectException, or silently skip over
  266. * the object with no warning.
  267. * @return queue to read the objects from.
  268. */
  269. public <T extends ObjectId> AsyncObjectLoaderQueue<T> open(
  270. Iterable<T> objectIds, final boolean reportMissing) {
  271. final Iterator<T> idItr = objectIds.iterator();
  272. return new AsyncObjectLoaderQueue<T>() {
  273. private T cur;
  274. @Override
  275. public boolean next() throws MissingObjectException, IOException {
  276. if (idItr.hasNext()) {
  277. cur = idItr.next();
  278. return true;
  279. }
  280. return false;
  281. }
  282. @Override
  283. public T getCurrent() {
  284. return cur;
  285. }
  286. @Override
  287. public ObjectId getObjectId() {
  288. return cur;
  289. }
  290. @Override
  291. public ObjectLoader open() throws IOException {
  292. return ObjectReader.this.open(cur, OBJ_ANY);
  293. }
  294. @Override
  295. public boolean cancel(boolean mayInterruptIfRunning) {
  296. return true;
  297. }
  298. @Override
  299. public void release() {
  300. // Since we are sequential by default, we don't
  301. // have any state to clean up if we terminate early.
  302. }
  303. };
  304. }
  305. /**
  306. * Get only the size of an object.
  307. * <p>
  308. * The default implementation of this method opens an ObjectLoader.
  309. * Databases are encouraged to override this if a faster access method is
  310. * available to them.
  311. *
  312. * @param objectId
  313. * identity of the object to open.
  314. * @param typeHint
  315. * hint about the type of object being requested, e.g.
  316. * {@link org.eclipse.jgit.lib.Constants#OBJ_BLOB};
  317. * {@link #OBJ_ANY} if the object type is not known, or does not
  318. * matter to the caller.
  319. * @return size of object in bytes.
  320. * @throws org.eclipse.jgit.errors.MissingObjectException
  321. * the object does not exist.
  322. * @throws org.eclipse.jgit.errors.IncorrectObjectTypeException
  323. * typeHint was not OBJ_ANY, and the object's actual type does
  324. * not match typeHint.
  325. * @throws java.io.IOException
  326. * the object store cannot be accessed.
  327. */
  328. public long getObjectSize(AnyObjectId objectId, int typeHint)
  329. throws MissingObjectException, IncorrectObjectTypeException,
  330. IOException {
  331. return open(objectId, typeHint).getSize();
  332. }
  333. /**
  334. * Asynchronous object size lookup.
  335. *
  336. * @param objectIds
  337. * objects to get the size of from the object store. The supplied
  338. * collection must not be modified until the queue has finished.
  339. * @param reportMissing
  340. * if true missing objects are reported by calling failure with a
  341. * MissingObjectException. This may be more expensive for the
  342. * implementation to guarantee. If false the implementation may
  343. * choose to report MissingObjectException, or silently skip over
  344. * the object with no warning.
  345. * @return queue to read object sizes from.
  346. */
  347. public <T extends ObjectId> AsyncObjectSizeQueue<T> getObjectSize(
  348. Iterable<T> objectIds, final boolean reportMissing) {
  349. final Iterator<T> idItr = objectIds.iterator();
  350. return new AsyncObjectSizeQueue<T>() {
  351. private T cur;
  352. private long sz;
  353. @Override
  354. public boolean next() throws MissingObjectException, IOException {
  355. if (idItr.hasNext()) {
  356. cur = idItr.next();
  357. sz = getObjectSize(cur, OBJ_ANY);
  358. return true;
  359. }
  360. return false;
  361. }
  362. @Override
  363. public T getCurrent() {
  364. return cur;
  365. }
  366. @Override
  367. public ObjectId getObjectId() {
  368. return cur;
  369. }
  370. @Override
  371. public long getSize() {
  372. return sz;
  373. }
  374. @Override
  375. public boolean cancel(boolean mayInterruptIfRunning) {
  376. return true;
  377. }
  378. @Override
  379. public void release() {
  380. // Since we are sequential by default, we don't
  381. // have any state to clean up if we terminate early.
  382. }
  383. };
  384. }
  385. /**
  386. * Advise the reader to avoid unreachable objects.
  387. * <p>
  388. * While enabled the reader will skip over anything previously proven to be
  389. * unreachable. This may be dangerous in the face of concurrent writes.
  390. *
  391. * @param avoid
  392. * true to avoid unreachable objects.
  393. * @since 3.0
  394. */
  395. public void setAvoidUnreachableObjects(boolean avoid) {
  396. // Do nothing by default.
  397. }
  398. /**
  399. * An index that can be used to speed up ObjectWalks.
  400. *
  401. * @return the index or null if one does not exist.
  402. * @throws java.io.IOException
  403. * when the index fails to load
  404. * @since 3.0
  405. */
  406. public BitmapIndex getBitmapIndex() throws IOException {
  407. return null;
  408. }
  409. /**
  410. * Get the {@link org.eclipse.jgit.lib.ObjectInserter} from which this
  411. * reader was created using {@code inserter.newReader()}
  412. *
  413. * @return the {@link org.eclipse.jgit.lib.ObjectInserter} from which this
  414. * reader was created using {@code inserter.newReader()}, or null if
  415. * this reader was not created from an inserter.
  416. * @since 4.4
  417. */
  418. @Nullable
  419. public ObjectInserter getCreatedFromInserter() {
  420. return null;
  421. }
  422. /**
  423. * {@inheritDoc}
  424. * <p>
  425. * Release any resources used by this reader.
  426. * <p>
  427. * A reader that has been released can be used again, but may need to be
  428. * released after the subsequent usage.
  429. *
  430. * @since 4.0
  431. */
  432. @Override
  433. public abstract void close();
  434. /**
  435. * Sets the threshold at which a file will be streamed rather than loaded
  436. * entirely into memory
  437. *
  438. * @param threshold
  439. * the new threshold
  440. * @since 4.6
  441. */
  442. public void setStreamFileThreshold(int threshold) {
  443. streamFileThreshold = threshold;
  444. }
  445. /**
  446. * Returns the threshold at which a file will be streamed rather than loaded
  447. * entirely into memory
  448. *
  449. * @return the threshold in bytes
  450. * @since 4.6
  451. */
  452. public int getStreamFileThreshold() {
  453. return streamFileThreshold;
  454. }
  455. /**
  456. * Wraps a delegate ObjectReader.
  457. *
  458. * @since 4.4
  459. */
  460. public static abstract class Filter extends ObjectReader {
  461. /**
  462. * @return delegate ObjectReader to handle all processing.
  463. * @since 4.4
  464. */
  465. protected abstract ObjectReader delegate();
  466. @Override
  467. public ObjectReader newReader() {
  468. return delegate().newReader();
  469. }
  470. @Override
  471. public AbbreviatedObjectId abbreviate(AnyObjectId objectId)
  472. throws IOException {
  473. return delegate().abbreviate(objectId);
  474. }
  475. @Override
  476. public AbbreviatedObjectId abbreviate(AnyObjectId objectId, int len)
  477. throws IOException {
  478. return delegate().abbreviate(objectId, len);
  479. }
  480. @Override
  481. public Collection<ObjectId> resolve(AbbreviatedObjectId id)
  482. throws IOException {
  483. return delegate().resolve(id);
  484. }
  485. @Override
  486. public boolean has(AnyObjectId objectId) throws IOException {
  487. return delegate().has(objectId);
  488. }
  489. @Override
  490. public boolean has(AnyObjectId objectId, int typeHint) throws IOException {
  491. return delegate().has(objectId, typeHint);
  492. }
  493. @Override
  494. public ObjectLoader open(AnyObjectId objectId)
  495. throws MissingObjectException, IOException {
  496. return delegate().open(objectId);
  497. }
  498. @Override
  499. public ObjectLoader open(AnyObjectId objectId, int typeHint)
  500. throws MissingObjectException, IncorrectObjectTypeException,
  501. IOException {
  502. return delegate().open(objectId, typeHint);
  503. }
  504. @Override
  505. public Set<ObjectId> getShallowCommits() throws IOException {
  506. return delegate().getShallowCommits();
  507. }
  508. @Override
  509. public <T extends ObjectId> AsyncObjectLoaderQueue<T> open(
  510. Iterable<T> objectIds, boolean reportMissing) {
  511. return delegate().open(objectIds, reportMissing);
  512. }
  513. @Override
  514. public long getObjectSize(AnyObjectId objectId, int typeHint)
  515. throws MissingObjectException, IncorrectObjectTypeException,
  516. IOException {
  517. return delegate().getObjectSize(objectId, typeHint);
  518. }
  519. @Override
  520. public <T extends ObjectId> AsyncObjectSizeQueue<T> getObjectSize(
  521. Iterable<T> objectIds, boolean reportMissing) {
  522. return delegate().getObjectSize(objectIds, reportMissing);
  523. }
  524. @Override
  525. public void setAvoidUnreachableObjects(boolean avoid) {
  526. delegate().setAvoidUnreachableObjects(avoid);
  527. }
  528. @Override
  529. public BitmapIndex getBitmapIndex() throws IOException {
  530. return delegate().getBitmapIndex();
  531. }
  532. @Override
  533. @Nullable
  534. public ObjectInserter getCreatedFromInserter() {
  535. return delegate().getCreatedFromInserter();
  536. }
  537. @Override
  538. public void close() {
  539. delegate().close();
  540. }
  541. }
  542. }