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(File idxFile, PackIndex packIndex,
  55.             PackReverseIndex reverseIndex)
  56.             throws IOException {
  57.         try (SilentFileInputStream fd = new SilentFileInputStream(idxFile)) {
  58.             try {
  59.                 return read(fd, packIndex, reverseIndex);
  60.             } catch (IOException ioe) {
  61.                 throw new IOException(
  62.                         MessageFormat.format(JGitText.get().unreadablePackIndex,
  63.                                 idxFile.getAbsolutePath()),
  64.                         ioe);
  65.             }
  66.         }
  67.     }

  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 fd
  76.      *            stream to read the bitmap index file from. The stream must be
  77.      *            buffered as some small IOs are performed against the stream.
  78.      *            The caller is responsible for closing the stream.
  79.      * @param packIndex
  80.      *            the pack index for the corresponding pack file.
  81.      * @param reverseIndex
  82.      *            the pack reverse index for the corresponding pack file.
  83.      * @return a copy of the index in-memory.
  84.      * @throws java.io.IOException
  85.      *             the stream cannot be read.
  86.      * @throws CorruptObjectException
  87.      *             the stream does not contain a valid pack bitmap index.
  88.      */
  89.     public static PackBitmapIndex read(InputStream fd, PackIndex packIndex,
  90.             PackReverseIndex reverseIndex) throws IOException {
  91.         return new PackBitmapIndexV1(fd, packIndex, reverseIndex);
  92.     }

  93.     /**
  94.      * Read an existing pack bitmap index file from a buffered stream.
  95.      * <p>
  96.      * The format of the file will be automatically detected and a proper access
  97.      * implementation for that format will be constructed and returned to the
  98.      * caller. The file may or may not be held open by the returned instance.
  99.      *
  100.      * @param fd
  101.      *            stream to read the bitmap index file from. The stream must be
  102.      *            buffered as some small IOs are performed against the stream.
  103.      *            The caller is responsible for closing the stream.
  104.      * @param packIndexSupplier
  105.      *            the supplier for pack index for the corresponding pack file.
  106.      * @param reverseIndexSupplier
  107.      *            the supplier for pack reverse index for the corresponding pack
  108.      *            file.
  109.      * @param loadParallelRevIndex
  110.      *            whether reverse index should be loaded in parallel
  111.      * @return a copy of the index in-memory.
  112.      * @throws java.io.IOException
  113.      *             the stream cannot be read.
  114.      * @throws CorruptObjectException
  115.      *             the stream does not contain a valid pack bitmap index.
  116.      */
  117.     public static PackBitmapIndex read(InputStream fd,
  118.             SupplierWithIOException<PackIndex> packIndexSupplier,
  119.             SupplierWithIOException<PackReverseIndex> reverseIndexSupplier,
  120.             boolean loadParallelRevIndex)
  121.             throws IOException {
  122.         return new PackBitmapIndexV1(fd, packIndexSupplier,
  123.                 reverseIndexSupplier, loadParallelRevIndex);
  124.     }

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

  127.     /**
  128.      * Finds the position in the bitmap of the object.
  129.      *
  130.      * @param objectId
  131.      *            the id for which the bitmap position will be found.
  132.      * @return the bitmap id or -1 if the object was not found.
  133.      */
  134.     public abstract int findPosition(AnyObjectId objectId);

  135.     /**
  136.      * Get the object at the bitmap position.
  137.      *
  138.      * @param position
  139.      *            the id for which the object will be found.
  140.      * @return the ObjectId.
  141.      * @throws java.lang.IllegalArgumentException
  142.      *             when the item is not found.
  143.      */
  144.     public abstract ObjectId getObject(int position)
  145.             throws IllegalArgumentException;

  146.     /**
  147.      * Returns a bitmap containing positions for objects that have the given Git
  148.      * type.
  149.      *
  150.      * @param bitmap
  151.      *            the object bitmap.
  152.      * @param type
  153.      *            the Git type.
  154.      * @return the object bitmap with only objects of the Git type.
  155.      */
  156.     public abstract EWAHCompressedBitmap ofObjectType(
  157.             EWAHCompressedBitmap bitmap, int type);

  158.     /**
  159.      * Returns the previously constructed bitmap for the object.
  160.      *
  161.      * @param objectId
  162.      *            the id for which the bitmap will be found.
  163.      * @return the bitmap or null if the object was not found.
  164.      */
  165.     public abstract EWAHCompressedBitmap getBitmap(AnyObjectId objectId);

  166.     /**
  167.      * Obtain the total number of objects described by this index.
  168.      * {@code getObjectCount() - 1} is the largest bit that will be set in a
  169.      * bitmap.
  170.      *
  171.      * @return number of objects in this index, and likewise in the associated
  172.      *         pack that this index was generated from.
  173.      */
  174.     public abstract int getObjectCount();

  175.     /**
  176.      * Returns the number of bitmaps in this bitmap index.
  177.      *
  178.      * @return the number of bitmaps in this bitmap index.
  179.      */
  180.     public abstract int getBitmapCount();

  181.     /**
  182.      * Supplier that propagates IOException.
  183.      *
  184.      * @param <T>
  185.      *            the return type which is expected from {@link #get()}
  186.      */
  187.     @FunctionalInterface
  188.     public interface SupplierWithIOException<T> {
  189.         /**
  190.          * @return result
  191.          * @throws IOException
  192.          */
  193.         T get() throws IOException;
  194.     }
  195. }