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.

DfsInserter.java 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  1. /*
  2. * Copyright (C) 2011, 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.internal.storage.dfs;
  44. import static org.eclipse.jgit.internal.storage.pack.PackExt.INDEX;
  45. import static org.eclipse.jgit.internal.storage.pack.PackExt.PACK;
  46. import static org.eclipse.jgit.lib.Constants.OBJ_OFS_DELTA;
  47. import static org.eclipse.jgit.lib.Constants.OBJ_REF_DELTA;
  48. import java.io.BufferedInputStream;
  49. import java.io.EOFException;
  50. import java.io.IOException;
  51. import java.io.InputStream;
  52. import java.io.OutputStream;
  53. import java.nio.ByteBuffer;
  54. import java.security.MessageDigest;
  55. import java.text.MessageFormat;
  56. import java.util.Collection;
  57. import java.util.Collections;
  58. import java.util.HashSet;
  59. import java.util.List;
  60. import java.util.Set;
  61. import java.util.zip.CRC32;
  62. import java.util.zip.DataFormatException;
  63. import java.util.zip.Deflater;
  64. import java.util.zip.DeflaterOutputStream;
  65. import java.util.zip.Inflater;
  66. import java.util.zip.InflaterInputStream;
  67. import org.eclipse.jgit.errors.CorruptObjectException;
  68. import org.eclipse.jgit.errors.LargeObjectException;
  69. import org.eclipse.jgit.internal.JGitText;
  70. import org.eclipse.jgit.internal.storage.file.PackIndex;
  71. import org.eclipse.jgit.internal.storage.file.PackIndexWriter;
  72. import org.eclipse.jgit.internal.storage.pack.PackExt;
  73. import org.eclipse.jgit.lib.AbbreviatedObjectId;
  74. import org.eclipse.jgit.lib.AnyObjectId;
  75. import org.eclipse.jgit.lib.Constants;
  76. import org.eclipse.jgit.lib.ObjectId;
  77. import org.eclipse.jgit.lib.ObjectIdOwnerMap;
  78. import org.eclipse.jgit.lib.ObjectInserter;
  79. import org.eclipse.jgit.lib.ObjectLoader;
  80. import org.eclipse.jgit.lib.ObjectReader;
  81. import org.eclipse.jgit.lib.ObjectStream;
  82. import org.eclipse.jgit.transport.PackedObjectInfo;
  83. import org.eclipse.jgit.util.BlockList;
  84. import org.eclipse.jgit.util.IO;
  85. import org.eclipse.jgit.util.NB;
  86. import org.eclipse.jgit.util.TemporaryBuffer;
  87. import org.eclipse.jgit.util.io.CountingOutputStream;
  88. /** Inserts objects into the DFS. */
  89. public class DfsInserter extends ObjectInserter {
  90. /** Always produce version 2 indexes, to get CRC data. */
  91. private static final int INDEX_VERSION = 2;
  92. final DfsObjDatabase db;
  93. int compression = Deflater.BEST_COMPRESSION;
  94. List<PackedObjectInfo> objectList;
  95. ObjectIdOwnerMap<PackedObjectInfo> objectMap;
  96. DfsBlockCache cache;
  97. DfsPackKey packKey;
  98. DfsPackDescription packDsc;
  99. PackStream packOut;
  100. private boolean rollback;
  101. private boolean checkExisting = true;
  102. /**
  103. * Initialize a new inserter.
  104. *
  105. * @param db
  106. * database the inserter writes to.
  107. */
  108. protected DfsInserter(DfsObjDatabase db) {
  109. this.db = db;
  110. }
  111. /**
  112. * @param check
  113. * if false, will write out possibly-duplicate objects without
  114. * first checking whether they exist in the repo; default is true.
  115. */
  116. public void checkExisting(boolean check) {
  117. checkExisting = check;
  118. }
  119. void setCompressionLevel(int compression) {
  120. this.compression = compression;
  121. }
  122. @Override
  123. public DfsPackParser newPackParser(InputStream in) throws IOException {
  124. return new DfsPackParser(db, this, in);
  125. }
  126. @Override
  127. public ObjectReader newReader() {
  128. return new Reader();
  129. }
  130. @Override
  131. public ObjectId insert(int type, byte[] data, int off, int len)
  132. throws IOException {
  133. ObjectId id = idFor(type, data, off, len);
  134. if (objectMap != null && objectMap.contains(id))
  135. return id;
  136. // Ignore unreachable (garbage) objects here.
  137. if (checkExisting && db.has(id, true))
  138. return id;
  139. long offset = beginObject(type, len);
  140. packOut.compress.write(data, off, len);
  141. packOut.compress.finish();
  142. return endObject(id, offset);
  143. }
  144. @Override
  145. public ObjectId insert(int type, long len, InputStream in)
  146. throws IOException {
  147. byte[] buf = insertBuffer(len);
  148. if (len <= buf.length) {
  149. IO.readFully(in, buf, 0, (int) len);
  150. return insert(type, buf, 0, (int) len);
  151. }
  152. long offset = beginObject(type, len);
  153. MessageDigest md = digest();
  154. md.update(Constants.encodedTypeString(type));
  155. md.update((byte) ' ');
  156. md.update(Constants.encodeASCII(len));
  157. md.update((byte) 0);
  158. while (0 < len) {
  159. int n = in.read(buf, 0, (int) Math.min(buf.length, len));
  160. if (n <= 0)
  161. throw new EOFException();
  162. md.update(buf, 0, n);
  163. packOut.compress.write(buf, 0, n);
  164. len -= n;
  165. }
  166. packOut.compress.finish();
  167. return endObject(ObjectId.fromRaw(md.digest()), offset);
  168. }
  169. private byte[] insertBuffer(long len) {
  170. byte[] buf = buffer();
  171. if (len <= buf.length)
  172. return buf;
  173. if (len < db.getReaderOptions().getStreamFileThreshold()) {
  174. try {
  175. return new byte[(int) len];
  176. } catch (OutOfMemoryError noMem) {
  177. return buf;
  178. }
  179. }
  180. return buf;
  181. }
  182. @Override
  183. public void flush() throws IOException {
  184. if (packDsc == null)
  185. return;
  186. if (packOut == null)
  187. throw new IOException();
  188. byte[] packHash = packOut.writePackFooter();
  189. packDsc.addFileExt(PACK);
  190. packDsc.setFileSize(PACK, packOut.getCount());
  191. packOut.close();
  192. packOut = null;
  193. sortObjectsById();
  194. PackIndex index = writePackIndex(packDsc, packHash, objectList);
  195. db.commitPack(Collections.singletonList(packDsc), null);
  196. rollback = false;
  197. DfsPackFile p = cache.getOrCreate(packDsc, packKey);
  198. if (index != null)
  199. p.setPackIndex(index);
  200. db.addPack(p);
  201. clear();
  202. }
  203. @Override
  204. public void close() {
  205. if (packOut != null) {
  206. try {
  207. packOut.close();
  208. } catch (IOException err) {
  209. // Ignore a close failure, the pack should be removed.
  210. } finally {
  211. packOut = null;
  212. }
  213. }
  214. if (rollback && packDsc != null) {
  215. try {
  216. db.rollbackPack(Collections.singletonList(packDsc));
  217. } finally {
  218. packDsc = null;
  219. rollback = false;
  220. }
  221. }
  222. clear();
  223. }
  224. private void clear() {
  225. objectList = null;
  226. objectMap = null;
  227. packKey = null;
  228. packDsc = null;
  229. }
  230. private long beginObject(int type, long len) throws IOException {
  231. if (packOut == null)
  232. beginPack();
  233. long offset = packOut.getCount();
  234. packOut.beginObject(type, len);
  235. return offset;
  236. }
  237. private ObjectId endObject(ObjectId id, long offset) {
  238. PackedObjectInfo obj = new PackedObjectInfo(id);
  239. obj.setOffset(offset);
  240. obj.setCRC((int) packOut.crc32.getValue());
  241. objectList.add(obj);
  242. objectMap.addIfAbsent(obj);
  243. return id;
  244. }
  245. private void beginPack() throws IOException {
  246. objectList = new BlockList<PackedObjectInfo>();
  247. objectMap = new ObjectIdOwnerMap<PackedObjectInfo>();
  248. cache = DfsBlockCache.getInstance();
  249. rollback = true;
  250. packDsc = db.newPack(DfsObjDatabase.PackSource.INSERT);
  251. packOut = new PackStream(db.writeFile(packDsc, PACK));
  252. packKey = new DfsPackKey();
  253. // Write the header as though it were a single object pack.
  254. byte[] buf = packOut.hdrBuf;
  255. System.arraycopy(Constants.PACK_SIGNATURE, 0, buf, 0, 4);
  256. NB.encodeInt32(buf, 4, 2); // Always use pack version 2.
  257. NB.encodeInt32(buf, 8, 1); // Always assume 1 object.
  258. packOut.write(buf, 0, 12);
  259. }
  260. private void sortObjectsById() {
  261. Collections.sort(objectList);
  262. }
  263. PackIndex writePackIndex(DfsPackDescription pack, byte[] packHash,
  264. List<PackedObjectInfo> list) throws IOException {
  265. pack.setIndexVersion(INDEX_VERSION);
  266. pack.setObjectCount(list.size());
  267. // If there are less than 58,000 objects, the entire index fits in under
  268. // 2 MiB. Callers will probably need the index immediately, so buffer
  269. // the index in process and load from the buffer.
  270. TemporaryBuffer.Heap buf = null;
  271. PackIndex packIndex = null;
  272. if (list.size() <= 58000) {
  273. buf = new TemporaryBuffer.Heap(2 << 20);
  274. index(buf, packHash, list);
  275. packIndex = PackIndex.read(buf.openInputStream());
  276. }
  277. DfsOutputStream os = db.writeFile(pack, INDEX);
  278. try {
  279. CountingOutputStream cnt = new CountingOutputStream(os);
  280. if (buf != null)
  281. buf.writeTo(cnt, null);
  282. else
  283. index(cnt, packHash, list);
  284. pack.addFileExt(INDEX);
  285. pack.setFileSize(INDEX, cnt.getCount());
  286. } finally {
  287. os.close();
  288. }
  289. return packIndex;
  290. }
  291. private static void index(OutputStream out, byte[] packHash,
  292. List<PackedObjectInfo> list) throws IOException {
  293. PackIndexWriter.createVersion(out, INDEX_VERSION).write(list, packHash);
  294. }
  295. private class PackStream extends OutputStream {
  296. private final DfsOutputStream out;
  297. private final MessageDigest md;
  298. final byte[] hdrBuf;
  299. private final Deflater deflater;
  300. private final int blockSize;
  301. private long currPos; // Position of currBuf[0] in the output stream.
  302. private int currPtr; // Number of bytes in currBuf.
  303. private byte[] currBuf;
  304. final CRC32 crc32;
  305. final DeflaterOutputStream compress;
  306. PackStream(DfsOutputStream out) {
  307. this.out = out;
  308. hdrBuf = new byte[32];
  309. md = Constants.newMessageDigest();
  310. crc32 = new CRC32();
  311. deflater = new Deflater(compression);
  312. compress = new DeflaterOutputStream(this, deflater, 8192);
  313. int size = out.blockSize();
  314. if (size <= 0)
  315. size = cache.getBlockSize();
  316. else if (size < cache.getBlockSize())
  317. size = (cache.getBlockSize() / size) * size;
  318. blockSize = size;
  319. currBuf = new byte[blockSize];
  320. }
  321. long getCount() {
  322. return currPos + currPtr;
  323. }
  324. void beginObject(int objectType, long length) throws IOException {
  325. crc32.reset();
  326. deflater.reset();
  327. write(hdrBuf, 0, encodeTypeSize(objectType, length));
  328. }
  329. private int encodeTypeSize(int type, long rawLength) {
  330. long nextLength = rawLength >>> 4;
  331. hdrBuf[0] = (byte) ((nextLength > 0 ? 0x80 : 0x00) | (type << 4) | (rawLength & 0x0F));
  332. rawLength = nextLength;
  333. int n = 1;
  334. while (rawLength > 0) {
  335. nextLength >>>= 7;
  336. hdrBuf[n++] = (byte) ((nextLength > 0 ? 0x80 : 0x00) | (rawLength & 0x7F));
  337. rawLength = nextLength;
  338. }
  339. return n;
  340. }
  341. @Override
  342. public void write(final int b) throws IOException {
  343. hdrBuf[0] = (byte) b;
  344. write(hdrBuf, 0, 1);
  345. }
  346. @Override
  347. public void write(byte[] data, int off, int len) throws IOException {
  348. crc32.update(data, off, len);
  349. md.update(data, off, len);
  350. writeNoHash(data, off, len);
  351. }
  352. private void writeNoHash(byte[] data, int off, int len)
  353. throws IOException {
  354. while (0 < len) {
  355. int n = Math.min(len, currBuf.length - currPtr);
  356. if (n == 0) {
  357. flushBlock();
  358. currBuf = new byte[blockSize];
  359. continue;
  360. }
  361. System.arraycopy(data, off, currBuf, currPtr, n);
  362. off += n;
  363. len -= n;
  364. currPtr += n;
  365. }
  366. }
  367. private void flushBlock() throws IOException {
  368. out.write(currBuf, 0, currPtr);
  369. byte[] buf;
  370. if (currPtr == currBuf.length)
  371. buf = currBuf;
  372. else
  373. buf = copyOf(currBuf, 0, currPtr);
  374. cache.put(new DfsBlock(packKey, currPos, buf));
  375. currPos += currPtr;
  376. currPtr = 0;
  377. currBuf = null;
  378. }
  379. private byte[] copyOf(byte[] src, int ptr, int cnt) {
  380. byte[] dst = new byte[cnt];
  381. System.arraycopy(src, ptr, dst, 0, cnt);
  382. return dst;
  383. }
  384. byte[] writePackFooter() throws IOException {
  385. byte[] packHash = md.digest();
  386. writeNoHash(packHash, 0, packHash.length);
  387. if (currPtr != 0)
  388. flushBlock();
  389. return packHash;
  390. }
  391. int read(long pos, byte[] dst, int ptr, int cnt) throws IOException {
  392. int r = 0;
  393. while (pos < currPos && r < cnt) {
  394. DfsBlock b = getOrLoadBlock(pos);
  395. int n = b.copy(pos, dst, ptr + r, cnt - r);
  396. pos += n;
  397. r += n;
  398. }
  399. if (currPos <= pos && r < cnt) {
  400. int s = (int) (pos - currPos);
  401. int n = Math.min(currPtr - s, cnt - r);
  402. System.arraycopy(currBuf, s, dst, ptr + r, n);
  403. r += n;
  404. }
  405. return r;
  406. }
  407. byte[] inflate(DfsReader ctx, long pos, int len) throws IOException,
  408. DataFormatException {
  409. byte[] dstbuf;
  410. try {
  411. dstbuf = new byte[len];
  412. } catch (OutOfMemoryError noMemory) {
  413. return null; // Caller will switch to large object streaming.
  414. }
  415. Inflater inf = ctx.inflater();
  416. pos += setInput(pos, inf);
  417. for (int dstoff = 0;;) {
  418. int n = inf.inflate(dstbuf, dstoff, dstbuf.length - dstoff);
  419. dstoff += n;
  420. if (inf.finished())
  421. return dstbuf;
  422. if (inf.needsInput())
  423. pos += setInput(pos, inf);
  424. else if (n == 0)
  425. throw new DataFormatException();
  426. }
  427. }
  428. private int setInput(long pos, Inflater inf)
  429. throws IOException, DataFormatException {
  430. if (pos < currPos)
  431. return getOrLoadBlock(pos).setInput(pos, inf);
  432. if (pos < currPos + currPtr) {
  433. int s = (int) (pos - currPos);
  434. int n = currPtr - s;
  435. inf.setInput(currBuf, s, n);
  436. return n;
  437. }
  438. throw new EOFException(DfsText.get().unexpectedEofInPack);
  439. }
  440. private DfsBlock getOrLoadBlock(long pos) throws IOException {
  441. long s = toBlockStart(pos);
  442. DfsBlock b = cache.get(packKey, s);
  443. if (b != null)
  444. return b;
  445. byte[] d = new byte[blockSize];
  446. for (int p = 0; p < blockSize;) {
  447. int n = out.read(s + p, ByteBuffer.wrap(d, p, blockSize - p));
  448. if (n <= 0)
  449. throw new EOFException(DfsText.get().unexpectedEofInPack);
  450. p += n;
  451. }
  452. b = new DfsBlock(packKey, s, d);
  453. cache.put(b);
  454. return b;
  455. }
  456. private long toBlockStart(long pos) {
  457. return (pos / blockSize) * blockSize;
  458. }
  459. @Override
  460. public void close() throws IOException {
  461. deflater.end();
  462. out.close();
  463. }
  464. }
  465. private class Reader extends ObjectReader {
  466. private final DfsReader ctx = new DfsReader(db);
  467. @Override
  468. public ObjectReader newReader() {
  469. return db.newReader();
  470. }
  471. @Override
  472. public Collection<ObjectId> resolve(AbbreviatedObjectId id)
  473. throws IOException {
  474. Collection<ObjectId> stored = ctx.resolve(id);
  475. if (objectList == null)
  476. return stored;
  477. Set<ObjectId> r = new HashSet<ObjectId>(stored.size() + 2);
  478. r.addAll(stored);
  479. for (PackedObjectInfo obj : objectList) {
  480. if (id.prefixCompare(obj) == 0)
  481. r.add(obj.copy());
  482. }
  483. return r;
  484. }
  485. @Override
  486. public ObjectLoader open(AnyObjectId objectId, int typeHint)
  487. throws IOException {
  488. if (objectMap == null)
  489. return ctx.open(objectId, typeHint);
  490. PackedObjectInfo obj = objectMap.get(objectId);
  491. if (obj == null)
  492. return ctx.open(objectId, typeHint);
  493. byte[] buf = buffer();
  494. int cnt = packOut.read(obj.getOffset(), buf, 0, 20);
  495. if (cnt <= 0)
  496. throw new EOFException(DfsText.get().unexpectedEofInPack);
  497. int c = buf[0] & 0xff;
  498. int type = (c >> 4) & 7;
  499. if (type == OBJ_OFS_DELTA || type == OBJ_REF_DELTA)
  500. throw new IOException(MessageFormat.format(
  501. DfsText.get().cannotReadBackDelta, Integer.toString(type)));
  502. long sz = c & 0x0f;
  503. int ptr = 1;
  504. int shift = 4;
  505. while ((c & 0x80) != 0) {
  506. if (ptr >= cnt)
  507. throw new EOFException(DfsText.get().unexpectedEofInPack);
  508. c = buf[ptr++] & 0xff;
  509. sz += ((long) (c & 0x7f)) << shift;
  510. shift += 7;
  511. }
  512. long zpos = obj.getOffset() + ptr;
  513. if (sz < ctx.getStreamFileThreshold()) {
  514. byte[] data = inflate(obj, zpos, (int) sz);
  515. if (data != null)
  516. return new ObjectLoader.SmallObject(type, data);
  517. }
  518. return new StreamLoader(obj.copy(), type, sz, packKey, zpos);
  519. }
  520. private byte[] inflate(PackedObjectInfo obj, long zpos, int sz)
  521. throws IOException, CorruptObjectException {
  522. try {
  523. return packOut.inflate(ctx, zpos, sz);
  524. } catch (DataFormatException dfe) {
  525. CorruptObjectException coe = new CorruptObjectException(
  526. MessageFormat.format(
  527. JGitText.get().objectAtHasBadZlibStream,
  528. Long.valueOf(obj.getOffset()),
  529. packDsc.getFileName(PackExt.PACK)));
  530. coe.initCause(dfe);
  531. throw coe;
  532. }
  533. }
  534. @Override
  535. public Set<ObjectId> getShallowCommits() throws IOException {
  536. return ctx.getShallowCommits();
  537. }
  538. @Override
  539. public ObjectInserter getCreatedFromInserter() {
  540. return DfsInserter.this;
  541. }
  542. @Override
  543. public void close() {
  544. ctx.close();
  545. }
  546. }
  547. private class StreamLoader extends ObjectLoader {
  548. private final ObjectId id;
  549. private final int type;
  550. private final long size;
  551. private final DfsPackKey srcPack;
  552. private final long pos;
  553. StreamLoader(ObjectId id, int type, long sz,
  554. DfsPackKey key, long pos) {
  555. this.id = id;
  556. this.type = type;
  557. this.size = sz;
  558. this.srcPack = key;
  559. this.pos = pos;
  560. }
  561. @Override
  562. public ObjectStream openStream() throws IOException {
  563. final DfsReader ctx = new DfsReader(db);
  564. if (srcPack != packKey) {
  565. try {
  566. // Post DfsInserter.flush() use the normal code path.
  567. // The newly created pack is registered in the cache.
  568. return ctx.open(id, type).openStream();
  569. } finally {
  570. ctx.close();
  571. }
  572. }
  573. int bufsz = 8192;
  574. final Inflater inf = ctx.inflater();
  575. return new ObjectStream.Filter(type,
  576. size, new BufferedInputStream(new InflaterInputStream(
  577. new ReadBackStream(pos), inf, bufsz), bufsz)) {
  578. @Override
  579. public void close() throws IOException {
  580. ctx.close();
  581. super.close();
  582. }
  583. };
  584. }
  585. @Override
  586. public int getType() {
  587. return type;
  588. }
  589. @Override
  590. public long getSize() {
  591. return size;
  592. }
  593. @Override
  594. public boolean isLarge() {
  595. return true;
  596. }
  597. @Override
  598. public byte[] getCachedBytes() throws LargeObjectException {
  599. throw new LargeObjectException.ExceedsLimit(
  600. db.getReaderOptions().getStreamFileThreshold(), size);
  601. }
  602. }
  603. private final class ReadBackStream extends InputStream {
  604. private long pos;
  605. ReadBackStream(long offset) {
  606. pos = offset;
  607. }
  608. @Override
  609. public int read() throws IOException {
  610. byte[] b = new byte[1];
  611. int n = read(b);
  612. return n == 1 ? b[0] & 0xff : -1;
  613. }
  614. @Override
  615. public int read(byte[] buf, int ptr, int len) throws IOException {
  616. int n = packOut.read(pos, buf, ptr, len);
  617. if (n > 0) {
  618. pos += n;
  619. }
  620. return n;
  621. }
  622. }
  623. }