Class Pack

  • All Implemented Interfaces:
    java.lang.Iterable<PackIndex.MutableEntry>

    public class Pack
    extends java.lang.Object
    implements java.lang.Iterable<PackIndex.MutableEntry>
    A Git version 2 pack file representation. A pack file contains Git objects in delta packed format yielding high compression of lots of object where some objects are similar.
    • Field Detail

      • LOG

        private static final org.slf4j.Logger LOG
      • SORT

        public static final java.util.Comparator<Pack> SORT
        Sorts PackFiles to be most recently created to least recently created.
      • packFile

        private final PackFile packFile
      • hash

        final int hash
      • fd

        private java.io.RandomAccessFile fd
      • readLock

        private final java.lang.Object readLock
        Serializes reads performed against fd.
      • length

        long length
      • activeWindows

        private int activeWindows
      • activeCopyRawData

        private int activeCopyRawData
      • packLastModified

        java.time.Instant packLastModified
      • invalid

        private volatile boolean invalid
      • invalidatingCause

        private volatile java.lang.Exception invalidatingCause
      • transientErrorCount

        private java.util.concurrent.atomic.AtomicInteger transientErrorCount
      • packChecksum

        private byte[] packChecksum
      • loadedIdx

        private volatile PackIndex loadedIdx
      • corruptObjects

        private volatile LongList corruptObjects
        Objects we have tried to read, and discovered to be corrupt.

        The list is allocated after the first corruption is found, and filled in as more entries are discovered. Typically this list is never used, as pack files do not usually contain corrupt objects.

    • Constructor Detail

      • Pack

        public Pack​(java.io.File packFile,
                    @Nullable
                    PackFile bitmapIdxFile)
        Construct a reader for an existing, pre-indexed packfile.
        Parameters:
        packFile - path of the .pack file holding the data.
        bitmapIdxFile - existing bitmap index file with the same base as the pack
    • Method Detail

      • idx

        private PackIndex idx()
                       throws java.io.IOException
        Throws:
        java.io.IOException
      • getPackFile

        public PackFile getPackFile()
        Get the File object which locates this pack on disk.
        Returns:
        the File object which locates this pack on disk.
      • getIndex

        public PackIndex getIndex()
                           throws java.io.IOException
        Get the index for this pack file.
        Returns:
        the index for this pack file.
        Throws:
        java.io.IOException
      • getPackName

        public java.lang.String getPackName()
        Get name extracted from pack-*.pack pattern.
        Returns:
        name extracted from pack-*.pack pattern.
      • hasObject

        public boolean hasObject​(AnyObjectId id)
                          throws java.io.IOException
        Determine if an object is contained within the pack file.

        For performance reasons only the index file is searched; the main pack content is ignored entirely.

        Parameters:
        id - the object to look for. Must not be null.
        Returns:
        true if the object is in this pack; false otherwise.
        Throws:
        java.io.IOException - the index file cannot be loaded into memory.
      • shouldBeKept

        public boolean shouldBeKept()
        Determines whether a .keep file exists for this pack file.
        Returns:
        true if a .keep file exist.
      • get

        ObjectLoader get​(WindowCursor curs,
                         AnyObjectId id)
                  throws java.io.IOException
        Get an object from this pack.
        Parameters:
        curs - temporary working space associated with the calling thread.
        id - the object to obtain from the pack. Must not be null.
        Returns:
        the object loader for the requested object if it is contained in this pack; null if the object was not found.
        Throws:
        java.io.IOException - the pack file or the index could not be read.
      • resolve

        void resolve​(java.util.Set<ObjectId> matches,
                     AbbreviatedObjectId id,
                     int matchLimit)
              throws java.io.IOException
        Throws:
        java.io.IOException
      • close

        public void close()
        Close the resources utilized by this repository
      • iterator

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

        Provide iterator over entries in associated pack index, that should also exist in this pack file. Objects returned by such iterator are mutable during iteration.

        Iterator returns objects in SHA-1 lexicographical order.

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

        long getObjectCount()
                     throws java.io.IOException
        Obtain the total number of objects available in this pack. This method relies on pack index, giving number of effectively available objects.
        Returns:
        number of objects in index of this pack, likewise in this pack
        Throws:
        java.io.IOException - the index file cannot be loaded into memory.
      • findObjectForOffset

        ObjectId findObjectForOffset​(long offset)
                              throws java.io.IOException
        Search for object id with the specified start offset in associated pack (reverse) index.
        Parameters:
        offset - start offset of object to find
        Returns:
        object id for this offset, or null if no object was found
        Throws:
        java.io.IOException - the index file cannot be loaded into memory.
      • getFileSnapshot

        PackFileSnapshot getFileSnapshot()
        Return the @FileSnapshot associated to the underlying packfile that has been used when the object was created.
        Returns:
        the packfile @FileSnapshot that the object is loaded from.
      • decompress

        private final byte[] decompress​(long position,
                                        int sz,
                                        WindowCursor curs)
                                 throws java.io.IOException,
                                        java.util.zip.DataFormatException
        Throws:
        java.io.IOException
        java.util.zip.DataFormatException
      • invalid

        boolean invalid()
      • setInvalid

        void setInvalid()
      • incrementTransientErrorCount

        int incrementTransientErrorCount()
      • resetTransientErrorCount

        void resetTransientErrorCount()
      • readFully

        private void readFully​(long position,
                               byte[] dstbuf,
                               int dstoff,
                               int cnt,
                               WindowCursor curs)
                        throws java.io.IOException
        Throws:
        java.io.IOException
      • endCopyAsIs

        private void endCopyAsIs()
      • beginWindowCache

        boolean beginWindowCache()
                          throws java.io.IOException
        Throws:
        java.io.IOException
      • endWindowCache

        boolean endWindowCache()
      • doOpen

        private void doOpen()
                     throws java.io.IOException
        Throws:
        java.io.IOException
      • openFail

        private void openFail​(boolean invalidate,
                              java.lang.Exception cause)
      • doClose

        private void doClose()
      • read

        ByteArrayWindow read​(long pos,
                             int size)
                      throws java.io.IOException
        Throws:
        java.io.IOException
      • mmap

        ByteWindow mmap​(long pos,
                        int size)
                 throws java.io.IOException
        Throws:
        java.io.IOException
      • onOpenPack

        private void onOpenPack()
                         throws java.io.IOException
        Throws:
        java.io.IOException
      • getDeltaHeader

        byte[] getDeltaHeader​(WindowCursor wc,
                              long pos)
                       throws java.io.IOException,
                              java.util.zip.DataFormatException
        Throws:
        java.io.IOException
        java.util.zip.DataFormatException
      • getObjectType

        int getObjectType​(WindowCursor curs,
                          long pos)
                   throws java.io.IOException
        Throws:
        java.io.IOException
      • getObjectSize

        long getObjectSize​(WindowCursor curs,
                           AnyObjectId id)
                    throws java.io.IOException
        Throws:
        java.io.IOException
      • getObjectSize

        long getObjectSize​(WindowCursor curs,
                           long pos)
                    throws java.io.IOException
        Throws:
        java.io.IOException
      • getBitmapIndex

        PackBitmapIndex getBitmapIndex()
                                throws java.io.IOException
        Throws:
        java.io.IOException
      • getReverseIdx

        private PackReverseIndex getReverseIdx()
                                        throws java.io.IOException
        Throws:
        java.io.IOException
      • isCorrupt

        private boolean isCorrupt​(long offset)
      • setCorrupt

        private void setCorrupt​(long offset)
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object