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.

DfsReader.java 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. /*
  2. * Copyright (C) 2008-2011, Google Inc.
  3. * Copyright (C) 2006-2008, Shawn O. Pearce <spearce@spearce.org>
  4. * and other copyright owners as documented in the project's IP log.
  5. *
  6. * This program and the accompanying materials are made available
  7. * under the terms of the Eclipse Distribution License v1.0 which
  8. * accompanies this distribution, is reproduced below, and is
  9. * available at http://www.eclipse.org/org/documents/edl-v10.php
  10. *
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials provided
  23. * with the distribution.
  24. *
  25. * - Neither the name of the Eclipse Foundation, Inc. nor the
  26. * names of its contributors may be used to endorse or promote
  27. * products derived from this software without specific prior
  28. * written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  31. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  32. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  33. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  35. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  37. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  39. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  42. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  43. */
  44. package org.eclipse.jgit.internal.storage.dfs;
  45. import static org.eclipse.jgit.lib.Constants.OBJECT_ID_LENGTH;
  46. import java.io.IOException;
  47. import java.util.ArrayList;
  48. import java.util.Collection;
  49. import java.util.Collections;
  50. import java.util.Comparator;
  51. import java.util.HashSet;
  52. import java.util.Iterator;
  53. import java.util.List;
  54. import java.util.Set;
  55. import java.util.zip.DataFormatException;
  56. import java.util.zip.Inflater;
  57. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  58. import org.eclipse.jgit.errors.MissingObjectException;
  59. import org.eclipse.jgit.errors.StoredObjectRepresentationNotAvailableException;
  60. import org.eclipse.jgit.internal.JGitText;
  61. import org.eclipse.jgit.internal.storage.file.BitmapIndexImpl;
  62. import org.eclipse.jgit.internal.storage.file.PackBitmapIndex;
  63. import org.eclipse.jgit.internal.storage.file.PackIndex;
  64. import org.eclipse.jgit.internal.storage.file.PackReverseIndex;
  65. import org.eclipse.jgit.internal.storage.pack.CachedPack;
  66. import org.eclipse.jgit.internal.storage.pack.ObjectReuseAsIs;
  67. import org.eclipse.jgit.internal.storage.pack.ObjectToPack;
  68. import org.eclipse.jgit.internal.storage.pack.PackOutputStream;
  69. import org.eclipse.jgit.internal.storage.pack.PackWriter;
  70. import org.eclipse.jgit.lib.AbbreviatedObjectId;
  71. import org.eclipse.jgit.lib.AnyObjectId;
  72. import org.eclipse.jgit.lib.AsyncObjectLoaderQueue;
  73. import org.eclipse.jgit.lib.AsyncObjectSizeQueue;
  74. import org.eclipse.jgit.lib.BitmapIndex;
  75. import org.eclipse.jgit.lib.BitmapIndex.BitmapBuilder;
  76. import org.eclipse.jgit.lib.InflaterCache;
  77. import org.eclipse.jgit.lib.ObjectId;
  78. import org.eclipse.jgit.lib.ObjectLoader;
  79. import org.eclipse.jgit.lib.ObjectReader;
  80. import org.eclipse.jgit.lib.ProgressMonitor;
  81. import org.eclipse.jgit.util.BlockList;
  82. /**
  83. * Reader to access repository content through.
  84. * <p>
  85. * See the base {@link ObjectReader} documentation for details. Notably, a
  86. * reader is not thread safe.
  87. */
  88. public final class DfsReader extends ObjectReader implements ObjectReuseAsIs {
  89. /** Temporary buffer large enough for at least one raw object id. */
  90. final byte[] tempId = new byte[OBJECT_ID_LENGTH];
  91. /** Database this reader loads objects from. */
  92. final DfsObjDatabase db;
  93. private Inflater inf;
  94. private DfsBlock block;
  95. private DeltaBaseCache baseCache;
  96. private DfsPackFile last;
  97. private boolean avoidUnreachable;
  98. DfsReader(DfsObjDatabase db) {
  99. this.db = db;
  100. }
  101. DfsReaderOptions getOptions() {
  102. return db.getReaderOptions();
  103. }
  104. DeltaBaseCache getDeltaBaseCache() {
  105. if (baseCache == null)
  106. baseCache = new DeltaBaseCache(this);
  107. return baseCache;
  108. }
  109. int getStreamFileThreshold() {
  110. return getOptions().getStreamFileThreshold();
  111. }
  112. @Override
  113. public ObjectReader newReader() {
  114. return new DfsReader(db);
  115. }
  116. @Override
  117. public void setAvoidUnreachableObjects(boolean avoid) {
  118. avoidUnreachable = avoid;
  119. }
  120. @Override
  121. public BitmapIndex getBitmapIndex() throws IOException {
  122. for (DfsPackFile pack : db.getPacks()) {
  123. PackBitmapIndex bitmapIndex = pack.getBitmapIndex(this);
  124. if (bitmapIndex != null)
  125. return new BitmapIndexImpl(bitmapIndex);
  126. }
  127. return null;
  128. }
  129. public Collection<CachedPack> getCachedPacksAndUpdate(
  130. BitmapBuilder needBitmap) throws IOException {
  131. for (DfsPackFile pack : db.getPacks()) {
  132. PackBitmapIndex bitmapIndex = pack.getBitmapIndex(this);
  133. if (needBitmap.removeAllOrNone(bitmapIndex))
  134. return Collections.<CachedPack> singletonList(
  135. new DfsCachedPack(pack));
  136. }
  137. return Collections.emptyList();
  138. }
  139. @Override
  140. public Collection<ObjectId> resolve(AbbreviatedObjectId id)
  141. throws IOException {
  142. if (id.isComplete())
  143. return Collections.singleton(id.toObjectId());
  144. boolean noGarbage = avoidUnreachable;
  145. HashSet<ObjectId> matches = new HashSet<ObjectId>(4);
  146. for (DfsPackFile pack : db.getPacks()) {
  147. if (noGarbage && pack.isGarbage())
  148. continue;
  149. pack.resolve(this, matches, id, 256);
  150. if (256 <= matches.size())
  151. break;
  152. }
  153. return matches;
  154. }
  155. @Override
  156. public boolean has(AnyObjectId objectId) throws IOException {
  157. if (last != null && last.hasObject(this, objectId))
  158. return true;
  159. boolean noGarbage = avoidUnreachable;
  160. for (DfsPackFile pack : db.getPacks()) {
  161. if (pack == last || (noGarbage && pack.isGarbage()))
  162. continue;
  163. if (pack.hasObject(this, objectId)) {
  164. last = pack;
  165. return true;
  166. }
  167. }
  168. return false;
  169. }
  170. @Override
  171. public ObjectLoader open(AnyObjectId objectId, int typeHint)
  172. throws MissingObjectException, IncorrectObjectTypeException,
  173. IOException {
  174. if (last != null) {
  175. ObjectLoader ldr = last.get(this, objectId);
  176. if (ldr != null)
  177. return ldr;
  178. }
  179. boolean noGarbage = avoidUnreachable;
  180. for (DfsPackFile pack : db.getPacks()) {
  181. if (pack == last || (noGarbage && pack.isGarbage()))
  182. continue;
  183. ObjectLoader ldr = pack.get(this, objectId);
  184. if (ldr != null) {
  185. last = pack;
  186. return ldr;
  187. }
  188. }
  189. if (typeHint == OBJ_ANY)
  190. throw new MissingObjectException(objectId.copy(),
  191. JGitText.get().unknownObjectType2);
  192. throw new MissingObjectException(objectId.copy(), typeHint);
  193. }
  194. @Override
  195. public Set<ObjectId> getShallowCommits() {
  196. return Collections.emptySet();
  197. }
  198. private static final Comparator<FoundObject<?>> FOUND_OBJECT_SORT = new Comparator<FoundObject<?>>() {
  199. public int compare(FoundObject<?> a, FoundObject<?> b) {
  200. int cmp = a.packIndex - b.packIndex;
  201. if (cmp == 0)
  202. cmp = Long.signum(a.offset - b.offset);
  203. return cmp;
  204. }
  205. };
  206. private static class FoundObject<T extends ObjectId> {
  207. final T id;
  208. final DfsPackFile pack;
  209. final long offset;
  210. final int packIndex;
  211. FoundObject(T objectId, int packIdx, DfsPackFile pack, long offset) {
  212. this.id = objectId;
  213. this.pack = pack;
  214. this.offset = offset;
  215. this.packIndex = packIdx;
  216. }
  217. FoundObject(T objectId) {
  218. this.id = objectId;
  219. this.pack = null;
  220. this.offset = 0;
  221. this.packIndex = 0;
  222. }
  223. }
  224. private <T extends ObjectId> Iterable<FoundObject<T>> findAll(
  225. Iterable<T> objectIds) throws IOException {
  226. ArrayList<FoundObject<T>> r = new ArrayList<FoundObject<T>>();
  227. DfsPackFile[] packList = db.getPacks();
  228. if (packList.length == 0) {
  229. for (T t : objectIds)
  230. r.add(new FoundObject<T>(t));
  231. return r;
  232. }
  233. int lastIdx = 0;
  234. DfsPackFile lastPack = packList[lastIdx];
  235. boolean noGarbage = avoidUnreachable;
  236. OBJECT_SCAN: for (T t : objectIds) {
  237. try {
  238. long p = lastPack.findOffset(this, t);
  239. if (0 < p) {
  240. r.add(new FoundObject<T>(t, lastIdx, lastPack, p));
  241. continue;
  242. }
  243. } catch (IOException e) {
  244. // Fall though and try to examine other packs.
  245. }
  246. for (int i = 0; i < packList.length; i++) {
  247. if (i == lastIdx)
  248. continue;
  249. DfsPackFile pack = packList[i];
  250. if (noGarbage && pack.isGarbage())
  251. continue;
  252. try {
  253. long p = pack.findOffset(this, t);
  254. if (0 < p) {
  255. r.add(new FoundObject<T>(t, i, pack, p));
  256. lastIdx = i;
  257. lastPack = pack;
  258. continue OBJECT_SCAN;
  259. }
  260. } catch (IOException e) {
  261. // Examine other packs.
  262. }
  263. }
  264. r.add(new FoundObject<T>(t));
  265. }
  266. Collections.sort(r, FOUND_OBJECT_SORT);
  267. last = lastPack;
  268. return r;
  269. }
  270. @Override
  271. public <T extends ObjectId> AsyncObjectLoaderQueue<T> open(
  272. Iterable<T> objectIds, final boolean reportMissing) {
  273. Iterable<FoundObject<T>> order;
  274. IOException error = null;
  275. try {
  276. order = findAll(objectIds);
  277. } catch (IOException e) {
  278. order = Collections.emptyList();
  279. error = e;
  280. }
  281. final Iterator<FoundObject<T>> idItr = order.iterator();
  282. final IOException findAllError = error;
  283. return new AsyncObjectLoaderQueue<T>() {
  284. private FoundObject<T> cur;
  285. public boolean next() throws MissingObjectException, IOException {
  286. if (idItr.hasNext()) {
  287. cur = idItr.next();
  288. return true;
  289. } else if (findAllError != null) {
  290. throw findAllError;
  291. } else {
  292. return false;
  293. }
  294. }
  295. public T getCurrent() {
  296. return cur.id;
  297. }
  298. public ObjectId getObjectId() {
  299. return cur.id;
  300. }
  301. public ObjectLoader open() throws IOException {
  302. if (cur.pack == null)
  303. throw new MissingObjectException(cur.id,
  304. JGitText.get().unknownObjectType2);
  305. return cur.pack.load(DfsReader.this, cur.offset);
  306. }
  307. public boolean cancel(boolean mayInterruptIfRunning) {
  308. return true;
  309. }
  310. public void release() {
  311. // Nothing to clean up.
  312. }
  313. };
  314. }
  315. @Override
  316. public <T extends ObjectId> AsyncObjectSizeQueue<T> getObjectSize(
  317. Iterable<T> objectIds, final boolean reportMissing) {
  318. Iterable<FoundObject<T>> order;
  319. IOException error = null;
  320. try {
  321. order = findAll(objectIds);
  322. } catch (IOException e) {
  323. order = Collections.emptyList();
  324. error = e;
  325. }
  326. final Iterator<FoundObject<T>> idItr = order.iterator();
  327. final IOException findAllError = error;
  328. return new AsyncObjectSizeQueue<T>() {
  329. private FoundObject<T> cur;
  330. private long sz;
  331. public boolean next() throws MissingObjectException, IOException {
  332. if (idItr.hasNext()) {
  333. cur = idItr.next();
  334. if (cur.pack == null)
  335. throw new MissingObjectException(cur.id,
  336. JGitText.get().unknownObjectType2);
  337. sz = cur.pack.getObjectSize(DfsReader.this, cur.offset);
  338. return true;
  339. } else if (findAllError != null) {
  340. throw findAllError;
  341. } else {
  342. return false;
  343. }
  344. }
  345. public T getCurrent() {
  346. return cur.id;
  347. }
  348. public ObjectId getObjectId() {
  349. return cur.id;
  350. }
  351. public long getSize() {
  352. return sz;
  353. }
  354. public boolean cancel(boolean mayInterruptIfRunning) {
  355. return true;
  356. }
  357. public void release() {
  358. // Nothing to clean up.
  359. }
  360. };
  361. }
  362. @Override
  363. public long getObjectSize(AnyObjectId objectId, int typeHint)
  364. throws MissingObjectException, IncorrectObjectTypeException,
  365. IOException {
  366. if (last != null) {
  367. long sz = last.getObjectSize(this, objectId);
  368. if (0 <= sz)
  369. return sz;
  370. }
  371. for (DfsPackFile pack : db.getPacks()) {
  372. if (pack == last)
  373. continue;
  374. long sz = pack.getObjectSize(this, objectId);
  375. if (0 <= sz) {
  376. last = pack;
  377. return sz;
  378. }
  379. }
  380. if (typeHint == OBJ_ANY)
  381. throw new MissingObjectException(objectId.copy(),
  382. JGitText.get().unknownObjectType2);
  383. throw new MissingObjectException(objectId.copy(), typeHint);
  384. }
  385. public DfsObjectToPack newObjectToPack(AnyObjectId objectId, int type) {
  386. return new DfsObjectToPack(objectId, type);
  387. }
  388. private static final Comparator<DfsObjectToPack> OFFSET_SORT = new Comparator<DfsObjectToPack>() {
  389. public int compare(DfsObjectToPack a, DfsObjectToPack b) {
  390. return Long.signum(a.getOffset() - b.getOffset());
  391. }
  392. };
  393. public void selectObjectRepresentation(PackWriter packer,
  394. ProgressMonitor monitor, Iterable<ObjectToPack> objects)
  395. throws IOException, MissingObjectException {
  396. for (DfsPackFile pack : db.getPacks()) {
  397. List<DfsObjectToPack> tmp = findAllFromPack(pack, objects);
  398. if (tmp.isEmpty())
  399. continue;
  400. Collections.sort(tmp, OFFSET_SORT);
  401. PackReverseIndex rev = pack.getReverseIdx(this);
  402. DfsObjectRepresentation rep = new DfsObjectRepresentation(pack);
  403. for (DfsObjectToPack otp : tmp) {
  404. pack.representation(rep, otp.getOffset(), this, rev);
  405. otp.setOffset(0);
  406. packer.select(otp, rep);
  407. if (!otp.isFound()) {
  408. otp.setFound();
  409. monitor.update(1);
  410. }
  411. }
  412. }
  413. }
  414. private List<DfsObjectToPack> findAllFromPack(DfsPackFile pack,
  415. Iterable<ObjectToPack> objects) throws IOException {
  416. List<DfsObjectToPack> tmp = new BlockList<DfsObjectToPack>();
  417. PackIndex idx = pack.getPackIndex(this);
  418. for (ObjectToPack otp : objects) {
  419. long p = idx.findOffset(otp);
  420. if (0 < p) {
  421. otp.setOffset(p);
  422. tmp.add((DfsObjectToPack) otp);
  423. }
  424. }
  425. return tmp;
  426. }
  427. public void copyObjectAsIs(PackOutputStream out, ObjectToPack otp,
  428. boolean validate) throws IOException,
  429. StoredObjectRepresentationNotAvailableException {
  430. DfsObjectToPack src = (DfsObjectToPack) otp;
  431. src.pack.copyAsIs(out, src, validate, this);
  432. }
  433. public void writeObjects(PackOutputStream out, List<ObjectToPack> list)
  434. throws IOException {
  435. for (ObjectToPack otp : list)
  436. out.writeObject(otp);
  437. }
  438. public void copyPackAsIs(PackOutputStream out, CachedPack pack)
  439. throws IOException {
  440. ((DfsCachedPack) pack).copyAsIs(out, this);
  441. }
  442. /**
  443. * Copy bytes from the window to a caller supplied buffer.
  444. *
  445. * @param pack
  446. * the file the desired window is stored within.
  447. * @param position
  448. * position within the file to read from.
  449. * @param dstbuf
  450. * destination buffer to copy into.
  451. * @param dstoff
  452. * offset within <code>dstbuf</code> to start copying into.
  453. * @param cnt
  454. * number of bytes to copy. This value may exceed the number of
  455. * bytes remaining in the window starting at offset
  456. * <code>pos</code>.
  457. * @return number of bytes actually copied; this may be less than
  458. * <code>cnt</code> if <code>cnt</code> exceeded the number of bytes
  459. * available.
  460. * @throws IOException
  461. * this cursor does not match the provider or id and the proper
  462. * window could not be acquired through the provider's cache.
  463. */
  464. int copy(DfsPackFile pack, long position, byte[] dstbuf, int dstoff, int cnt)
  465. throws IOException {
  466. if (cnt == 0)
  467. return 0;
  468. long length = pack.length;
  469. if (0 <= length && length <= position)
  470. return 0;
  471. int need = cnt;
  472. do {
  473. pin(pack, position);
  474. int r = block.copy(position, dstbuf, dstoff, need);
  475. position += r;
  476. dstoff += r;
  477. need -= r;
  478. if (length < 0)
  479. length = pack.length;
  480. } while (0 < need && position < length);
  481. return cnt - need;
  482. }
  483. /**
  484. * Inflate a region of the pack starting at {@code position}.
  485. *
  486. * @param pack
  487. * the file the desired window is stored within.
  488. * @param position
  489. * position within the file to read from.
  490. * @param dstbuf
  491. * destination buffer the inflater should output decompressed
  492. * data to. Must be large enough to store the entire stream,
  493. * unless headerOnly is true.
  494. * @param headerOnly
  495. * if true the caller wants only {@code dstbuf.length} bytes.
  496. * @return number of bytes inflated into <code>dstbuf</code>.
  497. * @throws IOException
  498. * this cursor does not match the provider or id and the proper
  499. * window could not be acquired through the provider's cache.
  500. * @throws DataFormatException
  501. * the inflater encountered an invalid chunk of data. Data
  502. * stream corruption is likely.
  503. */
  504. int inflate(DfsPackFile pack, long position, byte[] dstbuf,
  505. boolean headerOnly) throws IOException, DataFormatException {
  506. prepareInflater();
  507. pin(pack, position);
  508. position += block.setInput(position, inf);
  509. for (int dstoff = 0;;) {
  510. int n = inf.inflate(dstbuf, dstoff, dstbuf.length - dstoff);
  511. dstoff += n;
  512. if (inf.finished() || (headerOnly && dstoff == dstbuf.length))
  513. return dstoff;
  514. if (inf.needsInput()) {
  515. pin(pack, position);
  516. position += block.setInput(position, inf);
  517. } else if (n == 0)
  518. throw new DataFormatException();
  519. }
  520. }
  521. DfsBlock quickCopy(DfsPackFile p, long pos, long cnt)
  522. throws IOException {
  523. pin(p, pos);
  524. if (block.contains(p.key, pos + (cnt - 1)))
  525. return block;
  526. return null;
  527. }
  528. Inflater inflater() {
  529. prepareInflater();
  530. return inf;
  531. }
  532. private void prepareInflater() {
  533. if (inf == null)
  534. inf = InflaterCache.get();
  535. else
  536. inf.reset();
  537. }
  538. void pin(DfsPackFile pack, long position) throws IOException {
  539. DfsBlock b = block;
  540. if (b == null || !b.contains(pack.key, position)) {
  541. // If memory is low, we may need what is in our window field to
  542. // be cleaned up by the GC during the get for the next window.
  543. // So we always clear it, even though we are just going to set
  544. // it again.
  545. block = null;
  546. block = pack.getOrLoadBlock(position, this);
  547. }
  548. }
  549. void unpin() {
  550. block = null;
  551. }
  552. /** Release the current window cursor. */
  553. @Override
  554. public void close() {
  555. last = null;
  556. block = null;
  557. baseCache = null;
  558. try {
  559. InflaterCache.release(inf);
  560. } finally {
  561. inf = null;
  562. }
  563. }
  564. }