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.

PackBitmapIndex.java 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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.File;
  45. import java.io.FileInputStream;
  46. import java.io.IOException;
  47. import java.io.InputStream;
  48. import java.text.MessageFormat;
  49. import org.eclipse.jgit.errors.CorruptObjectException;
  50. import org.eclipse.jgit.internal.JGitText;
  51. import org.eclipse.jgit.lib.AnyObjectId;
  52. import org.eclipse.jgit.lib.ObjectId;
  53. import com.googlecode.javaewah.EWAHCompressedBitmap;
  54. /**
  55. * Logical representation of the bitmap data stored in the pack index.
  56. * {@link org.eclipse.jgit.lib.ObjectId}s are encoded as a single integer in the
  57. * range [0, {@link #getObjectCount()}). Compressed bitmaps are available at
  58. * certain {@code ObjectId}s, which represent all of the objects reachable from
  59. * that {@code ObjectId} (include the {@code ObjectId} itself). The meaning of
  60. * the positions in the bitmaps can be decoded using {@link #getObject(int)} and
  61. * {@link #ofObjectType(EWAHCompressedBitmap, int)}. Furthermore,
  62. * {@link #findPosition(AnyObjectId)} can be used to build other bitmaps that a
  63. * compatible with the encoded bitmaps available from the index.
  64. */
  65. public abstract class PackBitmapIndex {
  66. /** Flag bit denoting the bitmap should be reused during index creation. */
  67. public static final int FLAG_REUSE = 1;
  68. /**
  69. * Read an existing pack bitmap index file from a buffered stream.
  70. * <p>
  71. * The format of the file will be automatically detected and a proper access
  72. * implementation for that format will be constructed and returned to the
  73. * caller. The file may or may not be held open by the returned instance.
  74. *
  75. * @param idxFile
  76. * existing pack .bitmap to read.
  77. * @param packIndex
  78. * the pack index for the corresponding pack file.
  79. * @param reverseIndex
  80. * the pack reverse index for the corresponding pack file.
  81. * @return a copy of the index in-memory.
  82. * @throws java.io.IOException
  83. * the stream cannot be read.
  84. * @throws CorruptObjectException
  85. * the stream does not contain a valid pack bitmap index.
  86. */
  87. public static PackBitmapIndex open(
  88. File idxFile, PackIndex packIndex, PackReverseIndex reverseIndex)
  89. throws IOException {
  90. final FileInputStream fd = new FileInputStream(idxFile);
  91. try {
  92. return read(fd, packIndex, reverseIndex);
  93. } catch (IOException ioe) {
  94. throw new IOException(MessageFormat
  95. .format(JGitText.get().unreadablePackIndex,
  96. idxFile.getAbsolutePath()),
  97. ioe);
  98. } finally {
  99. try {
  100. fd.close();
  101. } catch (IOException err2) {
  102. // ignore
  103. }
  104. }
  105. }
  106. /**
  107. * Read an existing pack bitmap index file from a buffered stream.
  108. * <p>
  109. * The format of the file will be automatically detected and a proper access
  110. * implementation for that format will be constructed and returned to the
  111. * caller. The file may or may not be held open by the returned instance.
  112. *
  113. * @param fd
  114. * stream to read the bitmap index file from. The stream must be
  115. * buffered as some small IOs are performed against the stream.
  116. * The caller is responsible for closing the stream.
  117. * @param packIndex
  118. * the pack index for the corresponding pack file.
  119. * @param reverseIndex
  120. * the pack reverse index for the corresponding pack file.
  121. * @return a copy of the index in-memory.
  122. * @throws java.io.IOException
  123. * the stream cannot be read.
  124. * @throws CorruptObjectException
  125. * the stream does not contain a valid pack bitmap index.
  126. */
  127. public static PackBitmapIndex read(
  128. InputStream fd, PackIndex packIndex, PackReverseIndex reverseIndex)
  129. throws IOException {
  130. return new PackBitmapIndexV1(fd, packIndex, reverseIndex);
  131. }
  132. /** Footer checksum applied on the bottom of the pack file. */
  133. byte[] packChecksum;
  134. /**
  135. * Finds the position in the bitmap of the object.
  136. *
  137. * @param objectId
  138. * the id for which the bitmap position will be found.
  139. * @return the bitmap id or -1 if the object was not found.
  140. */
  141. public abstract int findPosition(AnyObjectId objectId);
  142. /**
  143. * Get the object at the bitmap position.
  144. *
  145. * @param position
  146. * the id for which the object will be found.
  147. * @return the ObjectId.
  148. * @throws java.lang.IllegalArgumentException
  149. * when the item is not found.
  150. */
  151. public abstract ObjectId getObject(int position) throws IllegalArgumentException;
  152. /**
  153. * Returns a bitmap containing positions for objects that have the given Git
  154. * type.
  155. *
  156. * @param bitmap
  157. * the object bitmap.
  158. * @param type
  159. * the Git type.
  160. * @return the object bitmap with only objects of the Git type.
  161. */
  162. public abstract EWAHCompressedBitmap ofObjectType(
  163. EWAHCompressedBitmap bitmap, int type);
  164. /**
  165. * Returns the previously constructed bitmap for the object.
  166. *
  167. * @param objectId
  168. * the id for which the bitmap will be found.
  169. * @return the bitmap or null if the object was not found.
  170. */
  171. public abstract EWAHCompressedBitmap getBitmap(AnyObjectId objectId);
  172. /**
  173. * Obtain the total number of objects described by this index.
  174. * {@code getObjectCount() - 1} is the largest bit that will be set in a
  175. * bitmap.
  176. *
  177. * @return number of objects in this index, and likewise in the associated
  178. * pack that this index was generated from.
  179. */
  180. public abstract int getObjectCount();
  181. /**
  182. * Returns the number of bitmaps in this bitmap index.
  183. *
  184. * @return the number of bitmaps in this bitmap index.
  185. */
  186. public abstract int getBitmapCount();
  187. }