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

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