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.

PackBitmapIndexV1.java 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * Copyright (C) 2012, 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.file;
  44. import java.io.DataInput;
  45. import java.io.IOException;
  46. import java.io.InputStream;
  47. import java.text.MessageFormat;
  48. import java.util.Arrays;
  49. import org.eclipse.jgit.internal.JGitText;
  50. import org.eclipse.jgit.lib.AnyObjectId;
  51. import org.eclipse.jgit.lib.Constants;
  52. import org.eclipse.jgit.lib.ObjectId;
  53. import org.eclipse.jgit.lib.ObjectIdOwnerMap;
  54. import org.eclipse.jgit.util.IO;
  55. import org.eclipse.jgit.util.NB;
  56. import com.googlecode.javaewah.EWAHCompressedBitmap;
  57. /**
  58. * Support for the pack bitmap index v1 format.
  59. *
  60. * @see PackBitmapIndex
  61. */
  62. class PackBitmapIndexV1 extends BasePackBitmapIndex {
  63. static final byte[] MAGIC = { 'B', 'I', 'T', 'M' };
  64. static final int OPT_FULL = 1;
  65. private static final int MAX_XOR_OFFSET = 126;
  66. private final PackIndex packIndex;
  67. private final PackReverseIndex reverseIndex;
  68. private final EWAHCompressedBitmap commits;
  69. private final EWAHCompressedBitmap trees;
  70. private final EWAHCompressedBitmap blobs;
  71. private final EWAHCompressedBitmap tags;
  72. private final ObjectIdOwnerMap<StoredBitmap> bitmaps;
  73. PackBitmapIndexV1(final InputStream fd, PackIndex packIndex,
  74. PackReverseIndex reverseIndex) throws IOException {
  75. super(new ObjectIdOwnerMap<StoredBitmap>());
  76. this.packIndex = packIndex;
  77. this.reverseIndex = reverseIndex;
  78. this.bitmaps = getBitmaps();
  79. final byte[] scratch = new byte[32];
  80. IO.readFully(fd, scratch, 0, scratch.length);
  81. // Check the magic bytes
  82. for (int i = 0; i < MAGIC.length; i++) {
  83. if (scratch[i] != MAGIC[i]) {
  84. byte[] actual = new byte[MAGIC.length];
  85. System.arraycopy(scratch, 0, actual, 0, MAGIC.length);
  86. throw new IOException(MessageFormat.format(
  87. JGitText.get().expectedGot, Arrays.toString(MAGIC),
  88. Arrays.toString(actual)));
  89. }
  90. }
  91. // Read the version (2 bytes)
  92. final int version = NB.decodeUInt16(scratch, 4);
  93. if (version != 1)
  94. throw new IOException(MessageFormat.format(
  95. JGitText.get().unsupportedPackIndexVersion,
  96. Integer.valueOf(version)));
  97. // Read the options (2 bytes)
  98. final int opts = NB.decodeUInt16(scratch, 6);
  99. if ((opts & OPT_FULL) == 0)
  100. throw new IOException(MessageFormat.format(
  101. JGitText.get().expectedGot, Integer.valueOf(OPT_FULL),
  102. Integer.valueOf(opts)));
  103. // Read the number of entries (1 int32)
  104. long numEntries = NB.decodeUInt32(scratch, 8);
  105. if (numEntries > Integer.MAX_VALUE)
  106. throw new IOException(JGitText.get().indexFileIsTooLargeForJgit);
  107. // Checksum applied on the bottom of the corresponding pack file.
  108. this.packChecksum = new byte[20];
  109. System.arraycopy(scratch, 12, packChecksum, 0, packChecksum.length);
  110. // Read the bitmaps for the Git types
  111. SimpleDataInput dataInput = new SimpleDataInput(fd);
  112. this.commits = readBitmap(dataInput);
  113. this.trees = readBitmap(dataInput);
  114. this.blobs = readBitmap(dataInput);
  115. this.tags = readBitmap(dataInput);
  116. // An entry is object id, xor offset, flag byte, and a length encoded
  117. // bitmap. The object id is an int32 of the nth position sorted by name.
  118. // The xor offset is a single byte offset back in the list of entries.
  119. StoredBitmap[] recentBitmaps = new StoredBitmap[MAX_XOR_OFFSET];
  120. for (int i = 0; i < (int) numEntries; i++) {
  121. IO.readFully(fd, scratch, 0, 6);
  122. int nthObjectId = NB.decodeInt32(scratch, 0);
  123. int xorOffset = scratch[4];
  124. int flags = scratch[5];
  125. EWAHCompressedBitmap bitmap = readBitmap(dataInput);
  126. if (nthObjectId < 0)
  127. throw new IOException(MessageFormat.format(
  128. JGitText.get().invalidId, String.valueOf(nthObjectId)));
  129. if (xorOffset < 0)
  130. throw new IOException(MessageFormat.format(
  131. JGitText.get().invalidId, String.valueOf(xorOffset)));
  132. if (xorOffset > MAX_XOR_OFFSET)
  133. throw new IOException(MessageFormat.format(
  134. JGitText.get().expectedLessThanGot,
  135. String.valueOf(MAX_XOR_OFFSET),
  136. String.valueOf(xorOffset)));
  137. if (xorOffset > i)
  138. throw new IOException(MessageFormat.format(
  139. JGitText.get().expectedLessThanGot, String.valueOf(i),
  140. String.valueOf(xorOffset)));
  141. ObjectId objectId = packIndex.getObjectId(nthObjectId);
  142. StoredBitmap xorBitmap = null;
  143. if (xorOffset > 0) {
  144. int index = (i - xorOffset);
  145. xorBitmap = recentBitmaps[index % recentBitmaps.length];
  146. if (xorBitmap == null)
  147. throw new IOException(MessageFormat.format(
  148. JGitText.get().invalidId,
  149. String.valueOf(xorOffset)));
  150. }
  151. StoredBitmap sb = new StoredBitmap(
  152. objectId, bitmap, xorBitmap, flags);
  153. bitmaps.add(sb);
  154. recentBitmaps[i % recentBitmaps.length] = sb;
  155. }
  156. }
  157. @Override
  158. public int findPosition(AnyObjectId objectId) {
  159. long offset = packIndex.findOffset(objectId);
  160. if (offset == -1)
  161. return -1;
  162. return reverseIndex.findPostion(offset);
  163. }
  164. @Override
  165. public ObjectId getObject(int position) throws IllegalArgumentException {
  166. ObjectId objectId = reverseIndex.findObjectByPosition(position);
  167. if (objectId == null)
  168. throw new IllegalArgumentException();
  169. return objectId;
  170. }
  171. @Override
  172. public int getObjectCount() {
  173. return (int) packIndex.getObjectCount();
  174. }
  175. @Override
  176. public EWAHCompressedBitmap ofObjectType(
  177. EWAHCompressedBitmap bitmap, int type) {
  178. switch (type) {
  179. case Constants.OBJ_BLOB:
  180. return blobs.and(bitmap);
  181. case Constants.OBJ_TREE:
  182. return trees.and(bitmap);
  183. case Constants.OBJ_COMMIT:
  184. return commits.and(bitmap);
  185. case Constants.OBJ_TAG:
  186. return tags.and(bitmap);
  187. }
  188. throw new IllegalArgumentException();
  189. }
  190. @Override
  191. public int getBitmapCount() {
  192. return bitmaps.size();
  193. }
  194. @Override
  195. public boolean equals(Object o) {
  196. // TODO(cranger): compare the pack checksum?
  197. if (o instanceof PackBitmapIndexV1)
  198. return getPackIndex() == ((PackBitmapIndexV1) o).getPackIndex();
  199. return false;
  200. }
  201. @Override
  202. public int hashCode() {
  203. return getPackIndex().hashCode();
  204. }
  205. PackIndex getPackIndex() {
  206. return packIndex;
  207. }
  208. private static EWAHCompressedBitmap readBitmap(DataInput dataInput)
  209. throws IOException {
  210. EWAHCompressedBitmap bitmap = new EWAHCompressedBitmap();
  211. bitmap.deserialize(dataInput);
  212. return bitmap;
  213. }
  214. }