Class DfsPackParser

java.lang.Object
org.eclipse.jgit.transport.PackParser
org.eclipse.jgit.internal.storage.dfs.DfsPackParser

public class DfsPackParser extends PackParser
Parses a pack stream into the DFS, by creating a new pack and index.
  • Field Details

    • objdb

      private final DfsObjDatabase objdb
    • objins

      private final DfsInserter objins
    • crc

      private final CRC32 crc
      CRC-32 computation for objects that are appended onto the pack.
    • packDigest

      private final MessageDigest packDigest
      Running SHA-1 of the entire pack stream.
    • blockSize

      private int blockSize
      Block size to use when caching data for read back.
    • packEnd

      private long packEnd
      Current end of the pack file.
    • packHash

      private byte[] packHash
      Checksum of the entire pack file.
    • def

      private Deflater def
      Compresses delta bases when completing a thin pack.
    • isEmptyPack

      private boolean isEmptyPack
      True if the pack is an empty pack.
    • packDsc

      private DfsPackDescription packDsc
      Name of the pack file, computed in onPackHeader(long).
    • packKey

      private DfsStreamKey packKey
      Key used during delta resolution reading delta chains.
    • packIndex

      private PackIndex packIndex
      If the index was small enough, the entire index after writing.
    • out

      private DfsOutputStream out
      Stream to the DFS storage, opened during onPackHeader(long).
    • currBuf

      private byte[] currBuf
      Data being written that has not yet been cached.
    • currPos

      private long currPos
    • currEnd

      private int currEnd
    • blockCache

      private DfsBlockCache blockCache
      Cache the chunks were stored into or get read back from.
    • readPos

      private long readPos
      Cached block that is being read.
    • readBlock

      private DfsBlock readBlock
  • Constructor Details

    • DfsPackParser

      protected DfsPackParser(DfsObjDatabase db, DfsInserter ins, InputStream in)
      Initialize a new pack parser.
      Parameters:
      db - database the objects will be imported into.
      ins - inserter the parser will use to help it inject the objects.
      in - the stream to parse.
  • Method Details

    • parse

      public PackLock parse(ProgressMonitor receiving, ProgressMonitor resolving) throws IOException
      Parse the pack stream.
      Overrides:
      parse in class PackParser
      Parameters:
      receiving - receives progress feedback during the initial receiving objects phase. If null, NullProgressMonitor will be used.
      resolving - receives progress feedback during the resolving objects phase.
      Returns:
      the pack lock, if one was requested by setting PackParser.setLockMessage(String).
      Throws:
      IOException - the stream is malformed, or contains corrupt objects.
    • getPackDescription

      public DfsPackDescription getPackDescription()
      Get description of the imported pack, if one was made.
      Returns:
      description of the imported pack, if one was made.
    • onPackHeader

      protected void onPackHeader(long objectCount) throws IOException
      Provide the implementation with the original stream's pack header.
      Specified by:
      onPackHeader in class PackParser
      Parameters:
      objectCount - number of objects expected in the stream.
      Throws:
      IOException - the implementation refuses to work with this many objects.
    • onBeginWholeObject

      protected void onBeginWholeObject(long streamPosition, int type, long inflatedSize) throws IOException
      Event notifying the start of an object stored whole (not as a delta).
      Specified by:
      onBeginWholeObject in class PackParser
      Parameters:
      streamPosition - position of this object in the incoming stream.
      type - type of the object; one of Constants.OBJ_COMMIT, Constants.OBJ_TREE, Constants.OBJ_BLOB, or Constants.OBJ_TAG.
      inflatedSize - size of the object when fully inflated. The size stored within the pack may be larger or smaller, and is not yet known.
      Throws:
      IOException - the object cannot be recorded.
    • onEndWholeObject

      protected void onEndWholeObject(PackedObjectInfo info) throws IOException
      Event notifying the current object.
      Specified by:
      onEndWholeObject in class PackParser
      Parameters:
      info - object information.
      Throws:
      IOException - the object cannot be recorded.
    • onBeginOfsDelta

      protected void onBeginOfsDelta(long streamPosition, long baseStreamPosition, long inflatedSize) throws IOException
      Event notifying start of a delta referencing its base by offset.
      Specified by:
      onBeginOfsDelta in class PackParser
      Parameters:
      streamPosition - position of this object in the incoming stream.
      baseStreamPosition - position of the base object in the incoming stream. The base must be before the delta, therefore baseStreamPosition < deltaStreamPosition. This is not the position returned by a prior end object event.
      inflatedSize - size of the delta when fully inflated. The size stored within the pack may be larger or smaller, and is not yet known.
      Throws:
      IOException - the object cannot be recorded.
    • onBeginRefDelta

      protected void onBeginRefDelta(long streamPosition, AnyObjectId baseId, long inflatedSize) throws IOException
      Event notifying start of a delta referencing its base by ObjectId.
      Specified by:
      onBeginRefDelta in class PackParser
      Parameters:
      streamPosition - position of this object in the incoming stream.
      baseId - name of the base object. This object may be later in the stream, or might not appear at all in the stream (in the case of a thin-pack).
      inflatedSize - size of the delta when fully inflated. The size stored within the pack may be larger or smaller, and is not yet known.
      Throws:
      IOException - the object cannot be recorded.
    • onEndDelta

      protected PackParser.UnresolvedDelta onEndDelta() throws IOException
      Event notifying the current object.
      Overrides:
      onEndDelta in class PackParser
      Returns:
      object information that must be populated with at least the offset.
      Throws:
      IOException - the object cannot be recorded.
    • onInflatedObjectData

      protected void onInflatedObjectData(PackedObjectInfo obj, int typeCode, byte[] data) throws IOException
      Invoked for commits, trees, tags, and small blobs.
      Specified by:
      onInflatedObjectData in class PackParser
      Parameters:
      obj - the object info, populated.
      typeCode - the type of the object.
      data - inflated data for the object.
      Throws:
      IOException - the object cannot be archived.
    • onObjectHeader

      protected void onObjectHeader(PackParser.Source src, byte[] raw, int pos, int len) throws IOException
      Store (and/or checksum) an object header.

      Invoked after any of the onBegin() events. The entire header is supplied in a single invocation, before any object data is supplied.

      Specified by:
      onObjectHeader in class PackParser
      Parameters:
      src - where the data came from
      raw - buffer to read data from.
      pos - first offset within buffer that is valid.
      len - number of bytes in buffer that are valid.
      Throws:
      IOException - the stream cannot be archived.
    • onObjectData

      protected void onObjectData(PackParser.Source src, byte[] raw, int pos, int len) throws IOException
      Store (and/or checksum) a portion of an object's data.

      This method may be invoked multiple times per object, depending on the size of the object, the size of the parser's internal read buffer, and the alignment of the object relative to the read buffer.

      Invoked after PackParser.onObjectHeader(Source, byte[], int, int).

      Specified by:
      onObjectData in class PackParser
      Parameters:
      src - where the data came from
      raw - buffer to read data from.
      pos - first offset within buffer that is valid.
      len - number of bytes in buffer that are valid.
      Throws:
      IOException - the stream cannot be archived.
    • onStoreStream

      protected void onStoreStream(byte[] raw, int pos, int len) throws IOException
      Store bytes received from the raw stream.

      This method is invoked during PackParser.parse(ProgressMonitor) as data is consumed from the incoming stream. Implementors may use this event to archive the raw incoming stream to the destination repository in large chunks, without paying attention to object boundaries.

      The only component of the pack not supplied to this method is the last 20 bytes of the pack that comprise the trailing SHA-1 checksum. Those are passed to PackParser.onPackFooter(byte[]).

      Specified by:
      onStoreStream in class PackParser
      Parameters:
      raw - buffer to copy data out of.
      pos - first offset within the buffer that is valid.
      len - number of bytes in the buffer that are valid.
      Throws:
      IOException - the stream cannot be archived.
    • buffer

      private void buffer(byte[] raw, int pos, int len) throws IOException
      Throws:
      IOException
    • flushBlock

      private DfsBlock flushBlock() throws IOException
      Throws:
      IOException
    • onPackFooter

      protected void onPackFooter(byte[] hash) throws IOException
      Provide the implementation with the original stream's pack footer.
      Specified by:
      onPackFooter in class PackParser
      Parameters:
      hash - the trailing 20 bytes of the pack, this is a SHA-1 checksum of all of the pack data.
      Throws:
      IOException - the stream cannot be archived.
    • seekDatabase

      Reposition the database to re-read a previously stored object.

      If the database is computing CRC-32 checksums for object data, it should reset its internal CRC instance during this method call.

      Specified by:
      seekDatabase in class PackParser
      Parameters:
      obj - the object position to begin reading from. This is from PackParser.newInfo(AnyObjectId, UnresolvedDelta, ObjectId).
      info - object to populate with type and size.
      Returns:
      the info object.
      Throws:
      IOException - the database cannot reposition to this location.
    • seekDatabase

      Reposition the database to re-read a previously stored object.

      If the database is computing CRC-32 checksums for object data, it should reset its internal CRC instance during this method call.

      Specified by:
      seekDatabase in class PackParser
      Parameters:
      delta - the object position to begin reading from. This is an instance previously returned by PackParser.onEndDelta().
      info - object to populate with type and size.
      Returns:
      the info object.
      Throws:
      IOException - the database cannot reposition to this location.
    • readDatabase

      protected int readDatabase(byte[] dst, int pos, int cnt) throws IOException
      Read from the database's current position into the buffer.
      Specified by:
      readDatabase in class PackParser
      Parameters:
      dst - the buffer to copy read data into.
      pos - position within dst to start copying data into.
      cnt - ideal target number of bytes to read. Actual read length may be shorter.
      Returns:
      number of bytes stored.
      Throws:
      IOException - the database cannot be accessed.
    • read

      private int read(long pos, byte[] dst, int off, int len) throws IOException
      Throws:
      IOException
    • toBlockStart

      private long toBlockStart(long pos)
    • checkCRC

      protected boolean checkCRC(int oldCRC)
      Check the current CRC matches the expected value.

      This method is invoked when an object is read back in from the database and its data is used during delta resolution. The CRC is validated after the object has been fully read, allowing the parser to verify there was no silent data corruption.

      Implementations are free to ignore this check by always returning true if they are performing other data integrity validations at a lower level.

      Specified by:
      checkCRC in class PackParser
      Parameters:
      oldCRC - the prior CRC that was recorded during the first scan of the object from the pack stream.
      Returns:
      true if the CRC matches; false if it does not.
    • onAppendBase

      protected boolean onAppendBase(int typeCode, byte[] data, PackedObjectInfo info) throws IOException
      Provide the implementation with a base that was outside of the pack.

      This event only occurs on a thin pack for base objects that were outside of the pack and came from the local repository. Usually an implementation uses this event to compress the base and append it onto the end of the pack, so the pack stays self-contained.

      Specified by:
      onAppendBase in class PackParser
      Parameters:
      typeCode - type of the base object.
      data - complete content of the base object.
      info - packed object information for this base. Implementors must populate the CRC and offset members if returning true.
      Returns:
      true if the info should be included in the object list returned by PackParser.getSortedObjectList(Comparator), false if it should not be included.
      Throws:
      IOException - the base could not be included into the pack.
    • onEndThinPack

      protected void onEndThinPack() throws IOException
      Event indicating a thin pack has been completely processed.

      This event is invoked only if a thin pack has delta references to objects external from the pack. The event is called after all of those deltas have been resolved.

      Specified by:
      onEndThinPack in class PackParser
      Throws:
      IOException - the pack cannot be archived.
    • writePackIndex

      private void writePackIndex() throws IOException
      Throws:
      IOException