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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 ObjectId}s are encoded as a single integer in the range [0,
  57. * {@link #getObjectCount()}). Compressed bitmaps are available at certain
  58. * {@code ObjectId}s, which represent all of the objects reachable from that
  59. * {@code ObjectId} (include the {@code ObjectId} itself). The meaning of the
  60. * 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 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. final String path = idxFile.getAbsolutePath();
  95. final IOException err;
  96. err = new IOException(MessageFormat.format(
  97. JGitText.get().unreadablePackIndex, path));
  98. err.initCause(ioe);
  99. throw err;
  100. } finally {
  101. try {
  102. fd.close();
  103. } catch (IOException err2) {
  104. // ignore
  105. }
  106. }
  107. }
  108. /**
  109. * Read an existing pack bitmap index file from a buffered stream.
  110. * <p>
  111. * The format of the file will be automatically detected and a proper access
  112. * implementation for that format will be constructed and returned to the
  113. * caller. The file may or may not be held open by the returned instance.
  114. *
  115. * @param fd
  116. * stream to read the bitmap index file from. The stream must be
  117. * buffered as some small IOs are performed against the stream.
  118. * The caller is responsible for closing the stream.
  119. * @param packIndex
  120. * the pack index for the corresponding pack file.
  121. * @param reverseIndex
  122. * the pack reverse index for the corresponding pack file.
  123. * @return a copy of the index in-memory.
  124. * @throws IOException
  125. * the stream cannot be read.
  126. * @throws CorruptObjectException
  127. * the stream does not contain a valid pack bitmap index.
  128. */
  129. public static PackBitmapIndex read(
  130. InputStream fd, PackIndex packIndex, PackReverseIndex reverseIndex)
  131. throws IOException {
  132. return new PackBitmapIndexV1(fd, packIndex, reverseIndex);
  133. }
  134. /** Footer checksum applied on the bottom of the pack file. */
  135. byte[] packChecksum;
  136. /**
  137. * Finds the position in the bitmap of the object.
  138. *
  139. * @param objectId
  140. * the id for which the bitmap position will be found.
  141. * @return the bitmap id or -1 if the object was not found.
  142. */
  143. public abstract int findPosition(AnyObjectId objectId);
  144. /**
  145. * Get the object at the bitmap position.
  146. *
  147. * @param position
  148. * the id for which the object will be found.
  149. * @return the ObjectId.
  150. * @throws IllegalArgumentException
  151. * when the item is not found.
  152. */
  153. public abstract ObjectId getObject(int position) throws IllegalArgumentException;
  154. /**
  155. * Returns a bitmap containing positions for objects that have the given Git
  156. * type.
  157. *
  158. * @param bitmap
  159. * the object bitmap.
  160. * @param type
  161. * the Git type.
  162. * @return the object bitmap with only objects of the Git type.
  163. */
  164. public abstract EWAHCompressedBitmap ofObjectType(
  165. EWAHCompressedBitmap bitmap, int type);
  166. /**
  167. * Returns the previously constructed bitmap for the object.
  168. *
  169. * @param objectId
  170. * the id for which the bitmap will be found.
  171. * @return the bitmap or null if the object was not found.
  172. */
  173. public abstract EWAHCompressedBitmap getBitmap(AnyObjectId objectId);
  174. /**
  175. * Obtain the total number of objects described by this index.
  176. * {@code getObjectCount() - 1} is the largest bit that will be set in a
  177. * bitmap.
  178. *
  179. * @return number of objects in this index, and likewise in the associated
  180. * pack that this index was generated from.
  181. */
  182. public abstract int getObjectCount();
  183. /**
  184. * Returns the number of bitmaps in this bitmap index.
  185. *
  186. * @return the number of bitmaps in this bitmap index.
  187. */
  188. public abstract int getBitmapCount();
  189. }