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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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.storage.dfs;
  44. import java.io.EOFException;
  45. import java.io.IOException;
  46. import java.io.InputStream;
  47. import java.io.OutputStream;
  48. import java.security.MessageDigest;
  49. import java.util.Collections;
  50. import java.util.List;
  51. import java.util.zip.CRC32;
  52. import java.util.zip.Deflater;
  53. import java.util.zip.DeflaterOutputStream;
  54. import org.eclipse.jgit.lib.Constants;
  55. import org.eclipse.jgit.lib.ObjectId;
  56. import org.eclipse.jgit.lib.ObjectIdOwnerMap;
  57. import org.eclipse.jgit.lib.ObjectInserter;
  58. import org.eclipse.jgit.storage.file.PackIndex;
  59. import org.eclipse.jgit.storage.file.PackIndexWriter;
  60. import org.eclipse.jgit.transport.PackedObjectInfo;
  61. import org.eclipse.jgit.util.BlockList;
  62. import org.eclipse.jgit.util.IO;
  63. import org.eclipse.jgit.util.NB;
  64. import org.eclipse.jgit.util.TemporaryBuffer;
  65. import org.eclipse.jgit.util.io.CountingOutputStream;
  66. /** Inserts objects into the DFS. */
  67. public class DfsInserter extends ObjectInserter {
  68. /** Always produce version 2 indexes, to get CRC data. */
  69. private static final int INDEX_VERSION = 2;
  70. private final DfsObjDatabase db;
  71. private List<PackedObjectInfo> objectList;
  72. private ObjectIdOwnerMap<PackedObjectInfo> objectMap;
  73. private DfsBlockCache cache;
  74. private DfsPackKey packKey;
  75. private DfsPackDescription packDsc;
  76. private PackStream packOut;
  77. private boolean rollback;
  78. /**
  79. * Initialize a new inserter.
  80. *
  81. * @param db
  82. * database the inserter writes to.
  83. */
  84. protected DfsInserter(DfsObjDatabase db) {
  85. this.db = db;
  86. }
  87. @Override
  88. public DfsPackParser newPackParser(InputStream in) throws IOException {
  89. return new DfsPackParser(db, this, in);
  90. }
  91. @Override
  92. public ObjectId insert(int type, byte[] data, int off, int len)
  93. throws IOException {
  94. ObjectId id = idFor(type, data, off, len);
  95. if (objectMap != null && objectMap.contains(id))
  96. return id;
  97. if (db.has(id))
  98. return id;
  99. long offset = beginObject(type, len);
  100. packOut.compress.write(data, off, len);
  101. packOut.compress.finish();
  102. return endObject(id, offset);
  103. }
  104. @Override
  105. public ObjectId insert(int type, long len, InputStream in)
  106. throws IOException {
  107. byte[] buf = buffer();
  108. if (len <= buf.length) {
  109. IO.readFully(in, buf, 0, (int) len);
  110. return insert(type, buf, 0, (int) len);
  111. }
  112. long offset = beginObject(type, len);
  113. MessageDigest md = digest();
  114. md.update(Constants.encodedTypeString(type));
  115. md.update((byte) ' ');
  116. md.update(Constants.encodeASCII(len));
  117. md.update((byte) 0);
  118. while (0 < len) {
  119. int n = in.read(buf, 0, (int) Math.min(buf.length, len));
  120. if (n <= 0)
  121. throw new EOFException();
  122. md.update(buf, 0, n);
  123. packOut.compress.write(buf, 0, n);
  124. len -= n;
  125. }
  126. packOut.compress.finish();
  127. return endObject(ObjectId.fromRaw(md.digest()), offset);
  128. }
  129. @Override
  130. public void flush() throws IOException {
  131. if (packDsc == null)
  132. return;
  133. if (packOut == null)
  134. throw new IOException();
  135. byte[] packHash = packOut.writePackFooter();
  136. packDsc.setPackSize(packOut.getCount());
  137. packOut.close();
  138. packOut = null;
  139. sortObjectsById();
  140. PackIndex index = writePackIndex(packDsc, packHash, objectList);
  141. db.commitPack(Collections.singletonList(packDsc), null);
  142. rollback = false;
  143. DfsPackFile p = cache.getOrCreate(packDsc, packKey);
  144. if (index != null)
  145. p.setPackIndex(index);
  146. db.addPack(p);
  147. clear();
  148. }
  149. @Override
  150. public void release() {
  151. if (packOut != null) {
  152. try {
  153. packOut.close();
  154. } catch (IOException err) {
  155. // Ignore a close failure, the pack should be removed.
  156. } finally {
  157. packOut = null;
  158. }
  159. }
  160. if (rollback && packDsc != null) {
  161. try {
  162. db.rollbackPack(Collections.singletonList(packDsc));
  163. } finally {
  164. packDsc = null;
  165. rollback = false;
  166. }
  167. }
  168. clear();
  169. }
  170. private void clear() {
  171. objectList = null;
  172. objectMap = null;
  173. packKey = null;
  174. packDsc = null;
  175. }
  176. private long beginObject(int type, long len) throws IOException {
  177. if (packOut == null)
  178. beginPack();
  179. long offset = packOut.getCount();
  180. packOut.beginObject(type, len);
  181. return offset;
  182. }
  183. private ObjectId endObject(ObjectId id, long offset) {
  184. PackedObjectInfo obj = new PackedObjectInfo(id);
  185. obj.setOffset(offset);
  186. obj.setCRC((int) packOut.crc32.getValue());
  187. objectList.add(obj);
  188. objectMap.addIfAbsent(obj);
  189. return id;
  190. }
  191. private void beginPack() throws IOException {
  192. objectList = new BlockList<PackedObjectInfo>();
  193. objectMap = new ObjectIdOwnerMap<PackedObjectInfo>();
  194. cache = DfsBlockCache.getInstance();
  195. rollback = true;
  196. packDsc = db.newPack(DfsObjDatabase.PackSource.INSERT);
  197. packOut = new PackStream(db.writePackFile(packDsc));
  198. packKey = new DfsPackKey();
  199. // Write the header as though it were a single object pack.
  200. byte[] buf = packOut.hdrBuf;
  201. System.arraycopy(Constants.PACK_SIGNATURE, 0, buf, 0, 4);
  202. NB.encodeInt32(buf, 4, 2); // Always use pack version 2.
  203. NB.encodeInt32(buf, 8, 1); // Always assume 1 object.
  204. packOut.write(buf, 0, 12);
  205. }
  206. private void sortObjectsById() {
  207. Collections.sort(objectList);
  208. }
  209. PackIndex writePackIndex(DfsPackDescription pack, byte[] packHash,
  210. List<PackedObjectInfo> list) throws IOException {
  211. pack.setObjectCount(list.size());
  212. // If there are less than 58,000 objects, the entire index fits in under
  213. // 2 MiB. Callers will probably need the index immediately, so buffer
  214. // the index in process and load from the buffer.
  215. TemporaryBuffer.Heap buf = null;
  216. PackIndex packIndex = null;
  217. if (list.size() <= 58000) {
  218. buf = new TemporaryBuffer.Heap(2 << 20);
  219. index(buf, packHash, list);
  220. packIndex = PackIndex.read(buf.openInputStream());
  221. }
  222. DfsOutputStream os = db.writePackIndex(pack);
  223. try {
  224. CountingOutputStream cnt = new CountingOutputStream(os);
  225. if (buf != null)
  226. buf.writeTo(cnt, null);
  227. else
  228. index(cnt, packHash, list);
  229. pack.setIndexSize(cnt.getCount());
  230. } finally {
  231. os.close();
  232. }
  233. return packIndex;
  234. }
  235. private static void index(OutputStream out, byte[] packHash,
  236. List<PackedObjectInfo> list) throws IOException {
  237. PackIndexWriter.createVersion(out, INDEX_VERSION).write(list, packHash);
  238. }
  239. private class PackStream extends OutputStream {
  240. private final DfsOutputStream out;
  241. private final MessageDigest md;
  242. private final byte[] hdrBuf;
  243. private final Deflater deflater;
  244. private final int blockSize;
  245. private long currPos; // Position of currBuf[0] in the output stream.
  246. private int currPtr; // Number of bytes in currBuf.
  247. private byte[] currBuf;
  248. final CRC32 crc32;
  249. final DeflaterOutputStream compress;
  250. PackStream(DfsOutputStream out) {
  251. this.out = out;
  252. hdrBuf = new byte[32];
  253. md = Constants.newMessageDigest();
  254. crc32 = new CRC32();
  255. deflater = new Deflater(Deflater.BEST_COMPRESSION);
  256. compress = new DeflaterOutputStream(this, deflater, 8192);
  257. int size = out.blockSize();
  258. if (size <= 0)
  259. size = cache.getBlockSize();
  260. else if (size < cache.getBlockSize())
  261. size = (cache.getBlockSize() / size) * size;
  262. blockSize = size;
  263. currBuf = new byte[blockSize];
  264. }
  265. long getCount() {
  266. return currPos + currPtr;
  267. }
  268. void beginObject(int objectType, long length) throws IOException {
  269. crc32.reset();
  270. deflater.reset();
  271. write(hdrBuf, 0, encodeTypeSize(objectType, length));
  272. }
  273. private int encodeTypeSize(int type, long rawLength) {
  274. long nextLength = rawLength >>> 4;
  275. hdrBuf[0] = (byte) ((nextLength > 0 ? 0x80 : 0x00) | (type << 4) | (rawLength & 0x0F));
  276. rawLength = nextLength;
  277. int n = 1;
  278. while (rawLength > 0) {
  279. nextLength >>>= 7;
  280. hdrBuf[n++] = (byte) ((nextLength > 0 ? 0x80 : 0x00) | (rawLength & 0x7F));
  281. rawLength = nextLength;
  282. }
  283. return n;
  284. }
  285. @Override
  286. public void write(final int b) throws IOException {
  287. hdrBuf[0] = (byte) b;
  288. write(hdrBuf, 0, 1);
  289. }
  290. @Override
  291. public void write(byte[] data, int off, int len) throws IOException {
  292. crc32.update(data, off, len);
  293. md.update(data, off, len);
  294. writeNoHash(data, off, len);
  295. }
  296. private void writeNoHash(byte[] data, int off, int len)
  297. throws IOException {
  298. while (0 < len) {
  299. int n = Math.min(len, currBuf.length - currPtr);
  300. if (n == 0) {
  301. flushBlock();
  302. currBuf = new byte[blockSize];
  303. continue;
  304. }
  305. System.arraycopy(data, off, currBuf, currPtr, n);
  306. off += n;
  307. len -= n;
  308. currPtr += n;
  309. }
  310. }
  311. private void flushBlock() throws IOException {
  312. out.write(currBuf, 0, currPtr);
  313. byte[] buf;
  314. if (currPtr == currBuf.length)
  315. buf = currBuf;
  316. else
  317. buf = copyOf(currBuf, 0, currPtr);
  318. cache.put(new DfsBlock(packKey, currPos, buf));
  319. currPos += currPtr;
  320. currPtr = 0;
  321. currBuf = null;
  322. }
  323. private byte[] copyOf(byte[] src, int ptr, int cnt) {
  324. byte[] dst = new byte[cnt];
  325. System.arraycopy(src, ptr, dst, 0, cnt);
  326. return dst;
  327. }
  328. byte[] writePackFooter() throws IOException {
  329. byte[] packHash = md.digest();
  330. writeNoHash(packHash, 0, packHash.length);
  331. if (currPtr != 0)
  332. flushBlock();
  333. return packHash;
  334. }
  335. @Override
  336. public void close() throws IOException {
  337. deflater.end();
  338. out.close();
  339. }
  340. }
  341. }