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

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