PackBitmapIndex.java

  1. /*
  2.  * Copyright (C) 2012, Google Inc. and others
  3.  *
  4.  * This program and the accompanying materials are made available under the
  5.  * terms of the Eclipse Distribution License v. 1.0 which is available at
  6.  * https://www.eclipse.org/org/documents/edl-v10.php.
  7.  *
  8.  * SPDX-License-Identifier: BSD-3-Clause
  9.  */

  10. package org.eclipse.jgit.internal.storage.file;

  11. import java.io.File;
  12. import java.io.IOException;
  13. import java.io.InputStream;
  14. import java.text.MessageFormat;

  15. import org.eclipse.jgit.errors.CorruptObjectException;
  16. import org.eclipse.jgit.internal.JGitText;
  17. import org.eclipse.jgit.lib.AnyObjectId;
  18. import org.eclipse.jgit.lib.ObjectId;
  19. import org.eclipse.jgit.util.io.SilentFileInputStream;

  20. import com.googlecode.javaewah.EWAHCompressedBitmap;

  21. /**
  22.  * Logical representation of the bitmap data stored in the pack index.
  23.  * {@link org.eclipse.jgit.lib.ObjectId}s are encoded as a single integer in the
  24.  * range [0, {@link #getObjectCount()}). Compressed bitmaps are available at
  25.  * certain {@code ObjectId}s, which represent all of the objects reachable from
  26.  * that {@code ObjectId} (include the {@code ObjectId} itself). The meaning of
  27.  * the positions in the bitmaps can be decoded using {@link #getObject(int)} and
  28.  * {@link #ofObjectType(EWAHCompressedBitmap, int)}. Furthermore,
  29.  * {@link #findPosition(AnyObjectId)} can be used to build other bitmaps that a
  30.  * compatible with the encoded bitmaps available from the index.
  31.  */
  32. public abstract class PackBitmapIndex {
  33.     /** Flag bit denoting the bitmap should be reused during index creation. */
  34.     public static final int FLAG_REUSE = 1;

  35.     /**
  36.      * Read an existing pack bitmap index file from a buffered stream.
  37.      * <p>
  38.      * The format of the file will be automatically detected and a proper access
  39.      * implementation for that format will be constructed and returned to the
  40.      * caller. The file may or may not be held open by the returned instance.
  41.      *
  42.      * @param idxFile
  43.      *            existing pack .bitmap to read.
  44.      * @param packIndex
  45.      *            the pack index for the corresponding pack file.
  46.      * @param reverseIndex
  47.      *            the pack reverse index for the corresponding pack file.
  48.      * @return a copy of the index in-memory.
  49.      * @throws java.io.IOException
  50.      *             the stream cannot be read.
  51.      * @throws CorruptObjectException
  52.      *             the stream does not contain a valid pack bitmap index.
  53.      */
  54.     public static PackBitmapIndex open(
  55.             File idxFile, PackIndex packIndex, PackReverseIndex reverseIndex)
  56.             throws IOException {
  57.         try (SilentFileInputStream fd = new SilentFileInputStream(
  58.                 idxFile)) {
  59.             try {
  60.                 return read(fd, packIndex, reverseIndex);
  61.             } catch (IOException ioe) {
  62.                 throw new IOException(
  63.                         MessageFormat.format(JGitText.get().unreadablePackIndex,
  64.                                 idxFile.getAbsolutePath()),
  65.                         ioe);
  66.             }
  67.         }
  68.     }

  69.     /**
  70.      * Read an existing pack bitmap index file from a buffered stream.
  71.      * <p>
  72.      * The format of the file will be automatically detected and a proper access
  73.      * implementation for that format will be constructed and returned to the
  74.      * caller. The file may or may not be held open by the returned instance.
  75.      *
  76.      * @param fd
  77.      *            stream to read the bitmap index file from. The stream must be
  78.      *            buffered as some small IOs are performed against the stream.
  79.      *            The caller is responsible for closing the stream.
  80.      * @param packIndex
  81.      *            the pack index for the corresponding pack file.
  82.      * @param reverseIndex
  83.      *            the pack reverse index for the corresponding pack file.
  84.      * @return a copy of the index in-memory.
  85.      * @throws java.io.IOException
  86.      *             the stream cannot be read.
  87.      * @throws CorruptObjectException
  88.      *             the stream does not contain a valid pack bitmap index.
  89.      */
  90.     public static PackBitmapIndex read(
  91.             InputStream fd, PackIndex packIndex, PackReverseIndex reverseIndex)
  92.             throws IOException {
  93.         return new PackBitmapIndexV1(fd, packIndex, reverseIndex);
  94.     }

  95.     /** Footer checksum applied on the bottom of the pack file. */
  96.     byte[] packChecksum;

  97.     /**
  98.      * Finds the position in the bitmap of the object.
  99.      *
  100.      * @param objectId
  101.      *            the id for which the bitmap position will be found.
  102.      * @return the bitmap id or -1 if the object was not found.
  103.      */
  104.     public abstract int findPosition(AnyObjectId objectId);

  105.     /**
  106.      * Get the object at the bitmap position.
  107.      *
  108.      * @param position
  109.      *            the id for which the object will be found.
  110.      * @return the ObjectId.
  111.      * @throws java.lang.IllegalArgumentException
  112.      *             when the item is not found.
  113.      */
  114.     public abstract ObjectId getObject(int position) throws IllegalArgumentException;

  115.     /**
  116.      * Returns a bitmap containing positions for objects that have the given Git
  117.      * type.
  118.      *
  119.      * @param bitmap
  120.      *            the object bitmap.
  121.      * @param type
  122.      *            the Git type.
  123.      * @return the object bitmap with only objects of the Git type.
  124.      */
  125.     public abstract EWAHCompressedBitmap ofObjectType(
  126.             EWAHCompressedBitmap bitmap, int type);

  127.     /**
  128.      * Returns the previously constructed bitmap for the object.
  129.      *
  130.      * @param objectId
  131.      *            the id for which the bitmap will be found.
  132.      * @return the bitmap or null if the object was not found.
  133.      */
  134.     public abstract EWAHCompressedBitmap getBitmap(AnyObjectId objectId);

  135.     /**
  136.      * Obtain the total number of objects described by this index.
  137.      * {@code getObjectCount() - 1} is the largest bit that will be set in a
  138.      * bitmap.
  139.      *
  140.      * @return number of objects in this index, and likewise in the associated
  141.      *         pack that this index was generated from.
  142.      */
  143.     public abstract int getObjectCount();

  144.     /**
  145.      * Returns the number of bitmaps in this bitmap index.
  146.      *
  147.      * @return the number of bitmaps in this bitmap index.
  148.      */
  149.     public abstract int getBitmapCount();
  150. }