Class DfsObjDatabase

    • Constructor Detail

      • DfsObjDatabase

        protected DfsObjDatabase​(DfsRepository repository,
                                 DfsReaderOptions options)
        Initialize an object database for our repository.
        Parameters:
        repository - repository owning this object database.
        options - how readers should access the object database.
    • Method Detail

      • getReaderOptions

        public DfsReaderOptions getReaderOptions()
        Get configured reader options, such as read-ahead.
        Returns:
        configured reader options, such as read-ahead.
      • setPackComparator

        public void setPackComparator​(Comparator<DfsPackDescription> packComparator)
        Set the comparator used when searching for objects across packs.

        An optimal comparator will find more objects without having to load large idx files from storage only to find that they don't contain the object. See DfsPackDescription.objectLookupComparator() for the default heuristics.

        Parameters:
        packComparator - comparator.
      • newReader

        public DfsReader newReader()
        Create a new ObjectReader to read existing objects.

        The returned reader is not itself thread-safe, but multiple concurrent reader instances created from the same ObjectDatabase must be thread-safe.

        Specified by:
        newReader in class ObjectDatabase
        Returns:
        reader the caller can use to load objects from this database.
      • newInserter

        public ObjectInserter newInserter()
        Create a new ObjectInserter to insert new objects.

        The returned inserter is not itself thread-safe, but multiple concurrent inserter instances created from the same ObjectDatabase must be thread-safe.

        Specified by:
        newInserter in class ObjectDatabase
        Returns:
        writer the caller can use to create objects in this database.
      • getPacks

        public DfsPackFile[] getPacks()
                               throws IOException
        Scan and list all available pack files in the repository.
        Returns:
        list of available packs. The returned array is shared with the implementation and must not be modified by the caller.
        Throws:
        IOException - the pack list cannot be initialized.
      • getReftables

        public DfsReftable[] getReftables()
                                   throws IOException
        Scan and list all available reftable files in the repository.
        Returns:
        list of available reftables. The returned array is shared with the implementation and must not be modified by the caller.
        Throws:
        IOException - the pack list cannot be initialized.
      • getPackList

        public DfsObjDatabase.PackList getPackList()
                                            throws IOException
        Scan and list all available pack files in the repository.
        Returns:
        list of available packs, with some additional metadata. The returned array is shared with the implementation and must not be modified by the caller.
        Throws:
        IOException - the pack list cannot be initialized.
      • getRepository

        protected DfsRepository getRepository()
        Get repository owning this object database.
        Returns:
        repository owning this object database.
      • getCurrentPacks

        public DfsPackFile[] getCurrentPacks()
        List currently known pack files in the repository, without scanning.
        Returns:
        list of available packs. The returned array is shared with the implementation and must not be modified by the caller.
      • getCurrentReftables

        public DfsReftable[] getCurrentReftables()
        List currently known reftable files in the repository, without scanning.
        Returns:
        list of available reftables. The returned array is shared with the implementation and must not be modified by the caller.
      • getCurrentPackList

        public DfsObjDatabase.PackList getCurrentPackList()
        List currently known pack files in the repository, without scanning.
        Returns:
        list of available packs, with some additional metadata. The returned array is shared with the implementation and must not be modified by the caller.
      • has

        public boolean has​(AnyObjectId objectId,
                           boolean avoidUnreachableObjects)
                    throws IOException
        Does the requested object exist in this database?

        This differs from ObjectDatabase's implementation in that we can selectively ignore unreachable (garbage) objects.

        Parameters:
        objectId - identity of the object to test for existence of.
        avoidUnreachableObjects - if true, ignore objects that are unreachable.
        Returns:
        true if the specified object is stored in this database.
        Throws:
        IOException - the object store cannot be accessed.
      • newPack

        protected abstract DfsPackDescription newPack​(DfsObjDatabase.PackSource source)
                                               throws IOException
        Generate a new unique name for a pack file.
        Parameters:
        source - where the pack stream is created.
        Returns:
        a unique name for the pack file. Must not collide with any other pack file name in the same DFS.
        Throws:
        IOException - a new unique pack description cannot be generated.
      • newPack

        protected DfsPackDescription newPack​(DfsObjDatabase.PackSource source,
                                             long estimatedPackSize)
                                      throws IOException
        Generate a new unique name for a pack file.

        Default implementation of this method would be equivalent to newPack(source).setEstimatedPackSize(estimatedPackSize). But the clients can override this method to use the given estomatedPackSize value more efficiently in the process of creating a new DfsPackDescription object.

        Parameters:
        source - where the pack stream is created.
        estimatedPackSize - the estimated size of the pack.
        Returns:
        a unique name for the pack file. Must not collide with any other pack file name in the same DFS.
        Throws:
        IOException - a new unique pack description cannot be generated.
      • commitPack

        protected void commitPack​(Collection<DfsPackDescription> desc,
                                  Collection<DfsPackDescription> replaces)
                           throws IOException
        Commit a pack and index pair that was written to the DFS.

        Committing the pack/index pair makes them visible to readers. The JGit DFS code always writes the pack, then the index. This allows a simple commit process to do nothing if readers always look for both files to exist and the DFS performs atomic creation of the file (e.g. stream to a temporary file and rename to target on close).

        During pack compaction or GC the new pack file may be replacing other older files. Implementations should remove those older files (if any) as part of the commit of the new file.

        This method is a trivial wrapper around commitPackImpl(Collection, Collection) that calls the implementation and fires events.

        Parameters:
        desc - description of the new packs.
        replaces - if not null, list of packs to remove.
        Throws:
        IOException - the packs cannot be committed. On failure a rollback must also be attempted by the caller.
      • rollbackPack

        protected abstract void rollbackPack​(Collection<DfsPackDescription> desc)
        Try to rollback a pack creation.

        JGit DFS always writes the pack first, then the index. If the pack does not yet exist, then neither does the index. A safe DFS implementation would try to remove both files to ensure they are really gone.

        A rollback does not support failures, as it only occurs when there is already a failure in progress. A DFS implementor may wish to log warnings/error messages when a rollback fails, but should not send new exceptions up the Java callstack.

        Parameters:
        desc - pack to delete.
      • listPacks

        protected abstract List<DfsPackDescription> listPacks()
                                                       throws IOException
        List the available pack files.

        The returned list must support random access and must be mutable by the caller. It is sorted in place using the natural sorting of the returned DfsPackDescription objects.

        Returns:
        available packs. May be empty if there are no packs.
        Throws:
        IOException - the packs cannot be listed and the object database is not functional to the caller.
      • openFile

        protected abstract ReadableChannel openFile​(DfsPackDescription desc,
                                                    PackExt ext)
                                             throws FileNotFoundException,
                                                    IOException
        Open a pack, pack index, or other related file for reading.
        Parameters:
        desc - description of pack related to the data that will be read. This is an instance previously obtained from listPacks(), but not necessarily from the same DfsObjDatabase instance.
        ext - file extension that will be read i.e "pack" or "idx".
        Returns:
        channel to read the file.
        Throws:
        FileNotFoundException - the file does not exist.
        IOException - the file cannot be opened.
      • writeFile

        protected abstract DfsOutputStream writeFile​(DfsPackDescription desc,
                                                     PackExt ext)
                                              throws IOException
        Open a pack, pack index, or other related file for writing.
        Parameters:
        desc - description of pack related to the data that will be written. This is an instance previously obtained from newPack(PackSource).
        ext - file extension that will be written i.e "pack" or "idx".
        Returns:
        channel to write the file.
        Throws:
        IOException - the file cannot be opened.
      • clearCache

        protected void clearCache()
        Clears the cached list of packs, forcing them to be scanned again.