Class PackIndex

  • All Implemented Interfaces:
    java.lang.Iterable<PackIndex.MutableEntry>, ObjectIdSet
    Direct Known Subclasses:
    PackIndexV1, PackIndexV2

    public abstract class PackIndex
    extends java.lang.Object
    implements java.lang.Iterable<PackIndex.MutableEntry>, ObjectIdSet
    Access path to locate objects by ObjectId in a Pack.

    Indexes are strictly redundant information in that we can rebuild all of the data held in the index file from the on disk representation of the pack file itself, but it is faster to access for random requests because data is stored by ObjectId.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      (package private) class  PackIndex.EntriesIterator  
      static class  PackIndex.MutableEntry
      Represent mutable entry of pack index consisting of object id and offset in pack (both mutable).
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected byte[] packChecksum
      Footer checksum applied on the bottom of the pack file.
    • Constructor Summary

      Constructors 
      Constructor Description
      PackIndex()  
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      boolean contains​(AnyObjectId id)
      Returns true if the objectId is contained within the collection.
      abstract long findCRC32​(AnyObjectId objId)
      Retrieve stored CRC32 checksum of the requested object raw-data (including header).
      abstract long findOffset​(AnyObjectId objId)
      Locate the file offset position for the requested object.
      byte[] getChecksum()  
      abstract long getObjectCount()
      Obtain the total number of objects described by this index.
      ObjectId getObjectId​(int nthPosition)
      Get ObjectId for the n-th object entry returned by iterator().
      abstract ObjectId getObjectId​(long nthPosition)
      Get ObjectId for the n-th object entry returned by iterator().
      (package private) abstract long getOffset​(long nthPosition)
      Get offset in a pack for the n-th object entry returned by iterator().
      abstract long getOffset64Count()
      Obtain the total number of objects needing 64 bit offsets.
      abstract boolean hasCRC32Support()
      Check whether this index supports (has) CRC32 checksums for objects.
      boolean hasObject​(AnyObjectId id)
      Determine if an object is contained within the pack file.
      private static boolean isTOC​(byte[] h)  
      abstract java.util.Iterator<PackIndex.MutableEntry> iterator()
      static PackIndex open​(java.io.File idxFile)
      Open an existing pack .idx file for reading.
      static PackIndex read​(java.io.InputStream fd)
      Read an existing pack index file from a buffered stream.
      abstract void resolve​(java.util.Set<ObjectId> matches, AbbreviatedObjectId id, int matchLimit)
      Find objects matching the prefix abbreviation.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface java.lang.Iterable

        forEach, spliterator
    • Field Detail

      • packChecksum

        protected byte[] packChecksum
        Footer checksum applied on the bottom of the pack file.
    • Constructor Detail

      • PackIndex

        public PackIndex()
    • Method Detail

      • open

        public static PackIndex open​(java.io.File idxFile)
                              throws java.io.IOException
        Open an existing pack .idx file for reading.

        The format of the file will be automatically detected and a proper access implementation for that format will be constructed and returned to the caller. The file may or may not be held open by the returned instance.

        Parameters:
        idxFile - existing pack .idx to read.
        Returns:
        access implementation for the requested file.
        Throws:
        java.io.FileNotFoundException - the file does not exist.
        java.io.IOException - the file exists but could not be read due to security errors, unrecognized data version, or unexpected data corruption.
      • read

        public static PackIndex read​(java.io.InputStream fd)
                              throws java.io.IOException,
                                     CorruptObjectException
        Read an existing pack index file from a buffered stream.

        The format of the file will be automatically detected and a proper access implementation for that format will be constructed and returned to the caller. The file may or may not be held open by the returned instance.

        Parameters:
        fd - stream to read the index file from. The stream must be buffered as some small IOs are performed against the stream. The caller is responsible for closing the stream.
        Returns:
        a copy of the index in-memory.
        Throws:
        java.io.IOException - the stream cannot be read.
        CorruptObjectException - the stream does not contain a valid pack index.
      • isTOC

        private static boolean isTOC​(byte[] h)
      • hasObject

        public boolean hasObject​(AnyObjectId id)
        Determine if an object is contained within the pack file.
        Parameters:
        id - the object to look for. Must not be null.
        Returns:
        true if the object is listed in this index; false otherwise.
      • contains

        public boolean contains​(AnyObjectId id)
        Returns true if the objectId is contained within the collection.
        Specified by:
        contains in interface ObjectIdSet
        Parameters:
        id - the objectId to find
        Returns:
        whether the collection contains the objectId.
      • iterator

        public abstract java.util.Iterator<PackIndex.MutableEntry> iterator()

        Provide iterator that gives access to index entries. Note, that iterator returns reference to mutable object, the same reference in each call - for performance reason. If client needs immutable objects, it must copy returned object on its own.

        Iterator returns objects in SHA-1 lexicographical order.

        Specified by:
        iterator in interface java.lang.Iterable<PackIndex.MutableEntry>
      • getObjectCount

        public abstract long getObjectCount()
        Obtain the total number of objects described by this index.
        Returns:
        number of objects in this index, and likewise in the associated pack that this index was generated from.
      • getOffset64Count

        public abstract long getOffset64Count()
        Obtain the total number of objects needing 64 bit offsets.
        Returns:
        number of objects in this index using a 64 bit offset; that is an object positioned after the 2 GB position within the file.
      • getObjectId

        public abstract ObjectId getObjectId​(long nthPosition)
        Get ObjectId for the n-th object entry returned by iterator().

        This method is a constant-time replacement for the following loop:

         Iterator<MutableEntry> eItr = index.iterator();
         int curPosition = 0;
         while (eItr.hasNext() && curPosition++ < nthPosition)
                eItr.next();
         ObjectId result = eItr.next().toObjectId();
         
        Parameters:
        nthPosition - position within the traversal of iterator() that the caller needs the object for. The first returned PackIndex.MutableEntry is 0, the second is 1, etc.
        Returns:
        the ObjectId for the corresponding entry.
      • getObjectId

        public final ObjectId getObjectId​(int nthPosition)
        Get ObjectId for the n-th object entry returned by iterator().

        This method is a constant-time replacement for the following loop:

         Iterator<MutableEntry> eItr = index.iterator();
         int curPosition = 0;
         while (eItr.hasNext() && curPosition++ < nthPosition)
                eItr.next();
         ObjectId result = eItr.next().toObjectId();
         
        Parameters:
        nthPosition - unsigned 32 bit position within the traversal of iterator() that the caller needs the object for. The first returned PackIndex.MutableEntry is 0, the second is 1, etc. Positions past 2**31-1 are negative, but still valid.
        Returns:
        the ObjectId for the corresponding entry.
      • getOffset

        abstract long getOffset​(long nthPosition)
        Get offset in a pack for the n-th object entry returned by iterator().
        Parameters:
        nthPosition - unsigned 32 bit position within the traversal of iterator() for which the caller needs the offset. The first returned PackIndex.MutableEntry is 0, the second is 1, etc. Positions past 2**31-1 are negative, but still valid.
        Returns:
        the offset in a pack for the corresponding entry.
      • findOffset

        public abstract long findOffset​(AnyObjectId objId)
        Locate the file offset position for the requested object.
        Parameters:
        objId - name of the object to locate within the pack.
        Returns:
        offset of the object's header and compressed content; -1 if the object does not exist in this index and is thus not stored in the associated pack.
      • findCRC32

        public abstract long findCRC32​(AnyObjectId objId)
                                throws MissingObjectException,
                                       java.lang.UnsupportedOperationException
        Retrieve stored CRC32 checksum of the requested object raw-data (including header).
        Parameters:
        objId - id of object to look for
        Returns:
        CRC32 checksum of specified object (at 32 less significant bits)
        Throws:
        MissingObjectException - when requested ObjectId was not found in this index
        java.lang.UnsupportedOperationException - when this index doesn't support CRC32 checksum
      • hasCRC32Support

        public abstract boolean hasCRC32Support()
        Check whether this index supports (has) CRC32 checksums for objects.
        Returns:
        true if CRC32 is stored, false otherwise
      • resolve

        public abstract void resolve​(java.util.Set<ObjectId> matches,
                                     AbbreviatedObjectId id,
                                     int matchLimit)
                              throws java.io.IOException
        Find objects matching the prefix abbreviation.
        Parameters:
        matches - set to add any located ObjectIds to. This is an output parameter.
        id - prefix to search for.
        matchLimit - maximum number of results to return. At most this many ObjectIds should be added to matches before returning.
        Throws:
        java.io.IOException - the index cannot be read.
      • getChecksum

        public byte[] getChecksum()
        Returns:
        the checksum of the pack; caller must not modify it
        Since:
        5.5