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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  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. } else {
  280. return false;
  281. }
  282. }
  283. @Override
  284. public T getCurrent() {
  285. return cur;
  286. }
  287. @Override
  288. public ObjectId getObjectId() {
  289. return cur;
  290. }
  291. @Override
  292. public ObjectLoader open() throws IOException {
  293. return ObjectReader.this.open(cur, OBJ_ANY);
  294. }
  295. @Override
  296. public boolean cancel(boolean mayInterruptIfRunning) {
  297. return true;
  298. }
  299. @Override
  300. public void release() {
  301. // Since we are sequential by default, we don't
  302. // have any state to clean up if we terminate early.
  303. }
  304. };
  305. }
  306. /**
  307. * Get only the size of an object.
  308. * <p>
  309. * The default implementation of this method opens an ObjectLoader.
  310. * Databases are encouraged to override this if a faster access method is
  311. * available to them.
  312. *
  313. * @param objectId
  314. * identity of the object to open.
  315. * @param typeHint
  316. * hint about the type of object being requested, e.g.
  317. * {@link org.eclipse.jgit.lib.Constants#OBJ_BLOB};
  318. * {@link #OBJ_ANY} if the object type is not known, or does not
  319. * matter to the caller.
  320. * @return size of object in bytes.
  321. * @throws org.eclipse.jgit.errors.MissingObjectException
  322. * the object does not exist.
  323. * @throws org.eclipse.jgit.errors.IncorrectObjectTypeException
  324. * typeHint was not OBJ_ANY, and the object's actual type does
  325. * not match typeHint.
  326. * @throws java.io.IOException
  327. * the object store cannot be accessed.
  328. */
  329. public long getObjectSize(AnyObjectId objectId, int typeHint)
  330. throws MissingObjectException, IncorrectObjectTypeException,
  331. IOException {
  332. return open(objectId, typeHint).getSize();
  333. }
  334. /**
  335. * Asynchronous object size lookup.
  336. *
  337. * @param objectIds
  338. * objects to get the size of from the object store. The supplied
  339. * collection must not be modified until the queue has finished.
  340. * @param reportMissing
  341. * if true missing objects are reported by calling failure with a
  342. * MissingObjectException. This may be more expensive for the
  343. * implementation to guarantee. If false the implementation may
  344. * choose to report MissingObjectException, or silently skip over
  345. * the object with no warning.
  346. * @return queue to read object sizes from.
  347. */
  348. public <T extends ObjectId> AsyncObjectSizeQueue<T> getObjectSize(
  349. Iterable<T> objectIds, final boolean reportMissing) {
  350. final Iterator<T> idItr = objectIds.iterator();
  351. return new AsyncObjectSizeQueue<T>() {
  352. private T cur;
  353. private long sz;
  354. @Override
  355. public boolean next() throws MissingObjectException, IOException {
  356. if (idItr.hasNext()) {
  357. cur = idItr.next();
  358. sz = getObjectSize(cur, OBJ_ANY);
  359. return true;
  360. } else {
  361. return false;
  362. }
  363. }
  364. @Override
  365. public T getCurrent() {
  366. return cur;
  367. }
  368. @Override
  369. public ObjectId getObjectId() {
  370. return cur;
  371. }
  372. @Override
  373. public long getSize() {
  374. return sz;
  375. }
  376. @Override
  377. public boolean cancel(boolean mayInterruptIfRunning) {
  378. return true;
  379. }
  380. @Override
  381. public void release() {
  382. // Since we are sequential by default, we don't
  383. // have any state to clean up if we terminate early.
  384. }
  385. };
  386. }
  387. /**
  388. * Advise the reader to avoid unreachable objects.
  389. * <p>
  390. * While enabled the reader will skip over anything previously proven to be
  391. * unreachable. This may be dangerous in the face of concurrent writes.
  392. *
  393. * @param avoid
  394. * true to avoid unreachable objects.
  395. * @since 3.0
  396. */
  397. public void setAvoidUnreachableObjects(boolean avoid) {
  398. // Do nothing by default.
  399. }
  400. /**
  401. * An index that can be used to speed up ObjectWalks.
  402. *
  403. * @return the index or null if one does not exist.
  404. * @throws java.io.IOException
  405. * when the index fails to load
  406. * @since 3.0
  407. */
  408. public BitmapIndex getBitmapIndex() throws IOException {
  409. return null;
  410. }
  411. /**
  412. * Get the {@link org.eclipse.jgit.lib.ObjectInserter} from which this
  413. * reader was created using {@code inserter.newReader()}
  414. *
  415. * @return the {@link org.eclipse.jgit.lib.ObjectInserter} from which this
  416. * reader was created using {@code inserter.newReader()}, or null if
  417. * this reader was not created from an inserter.
  418. * @since 4.4
  419. */
  420. @Nullable
  421. public ObjectInserter getCreatedFromInserter() {
  422. return null;
  423. }
  424. /**
  425. * {@inheritDoc}
  426. * <p>
  427. * Release any resources used by this reader.
  428. * <p>
  429. * A reader that has been released can be used again, but may need to be
  430. * released after the subsequent usage.
  431. *
  432. * @since 4.0
  433. */
  434. @Override
  435. public abstract void close();
  436. /**
  437. * Sets the threshold at which a file will be streamed rather than loaded
  438. * entirely into memory
  439. *
  440. * @param threshold
  441. * the new threshold
  442. * @since 4.6
  443. */
  444. public void setStreamFileThreshold(int threshold) {
  445. streamFileThreshold = threshold;
  446. }
  447. /**
  448. * Returns the threshold at which a file will be streamed rather than loaded
  449. * entirely into memory
  450. *
  451. * @return the threshold in bytes
  452. * @since 4.6
  453. */
  454. public int getStreamFileThreshold() {
  455. return streamFileThreshold;
  456. }
  457. /**
  458. * Wraps a delegate ObjectReader.
  459. *
  460. * @since 4.4
  461. */
  462. public static abstract class Filter extends ObjectReader {
  463. /**
  464. * @return delegate ObjectReader to handle all processing.
  465. * @since 4.4
  466. */
  467. protected abstract ObjectReader delegate();
  468. @Override
  469. public ObjectReader newReader() {
  470. return delegate().newReader();
  471. }
  472. @Override
  473. public AbbreviatedObjectId abbreviate(AnyObjectId objectId)
  474. throws IOException {
  475. return delegate().abbreviate(objectId);
  476. }
  477. @Override
  478. public AbbreviatedObjectId abbreviate(AnyObjectId objectId, int len)
  479. throws IOException {
  480. return delegate().abbreviate(objectId, len);
  481. }
  482. @Override
  483. public Collection<ObjectId> resolve(AbbreviatedObjectId id)
  484. throws IOException {
  485. return delegate().resolve(id);
  486. }
  487. @Override
  488. public boolean has(AnyObjectId objectId) throws IOException {
  489. return delegate().has(objectId);
  490. }
  491. @Override
  492. public boolean has(AnyObjectId objectId, int typeHint) throws IOException {
  493. return delegate().has(objectId, typeHint);
  494. }
  495. @Override
  496. public ObjectLoader open(AnyObjectId objectId)
  497. throws MissingObjectException, IOException {
  498. return delegate().open(objectId);
  499. }
  500. @Override
  501. public ObjectLoader open(AnyObjectId objectId, int typeHint)
  502. throws MissingObjectException, IncorrectObjectTypeException,
  503. IOException {
  504. return delegate().open(objectId, typeHint);
  505. }
  506. @Override
  507. public Set<ObjectId> getShallowCommits() throws IOException {
  508. return delegate().getShallowCommits();
  509. }
  510. @Override
  511. public <T extends ObjectId> AsyncObjectLoaderQueue<T> open(
  512. Iterable<T> objectIds, boolean reportMissing) {
  513. return delegate().open(objectIds, reportMissing);
  514. }
  515. @Override
  516. public long getObjectSize(AnyObjectId objectId, int typeHint)
  517. throws MissingObjectException, IncorrectObjectTypeException,
  518. IOException {
  519. return delegate().getObjectSize(objectId, typeHint);
  520. }
  521. @Override
  522. public <T extends ObjectId> AsyncObjectSizeQueue<T> getObjectSize(
  523. Iterable<T> objectIds, boolean reportMissing) {
  524. return delegate().getObjectSize(objectIds, reportMissing);
  525. }
  526. @Override
  527. public void setAvoidUnreachableObjects(boolean avoid) {
  528. delegate().setAvoidUnreachableObjects(avoid);
  529. }
  530. @Override
  531. public BitmapIndex getBitmapIndex() throws IOException {
  532. return delegate().getBitmapIndex();
  533. }
  534. @Override
  535. @Nullable
  536. public ObjectInserter getCreatedFromInserter() {
  537. return delegate().getCreatedFromInserter();
  538. }
  539. @Override
  540. public void close() {
  541. delegate().close();
  542. }
  543. }
  544. }