Class DfsObjDatabase
- java.lang.Object
-
- org.eclipse.jgit.lib.ObjectDatabase
-
- org.eclipse.jgit.internal.storage.dfs.DfsObjDatabase
-
- All Implemented Interfaces:
AutoCloseable
- Direct Known Subclasses:
InMemoryRepository.MemObjDatabase
public abstract class DfsObjDatabase extends ObjectDatabase
Manages objects stored inDfsPackFile
on a storage system.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
DfsObjDatabase.PackList
Snapshot of packs scanned in a single pass.static class
DfsObjDatabase.PackSource
Sources for a pack file.
-
Constructor Summary
Constructors Modifier Constructor Description protected
DfsObjDatabase(DfsRepository repository, DfsReaderOptions options)
Initialize an object database for our repository.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected void
clearCache()
Clears the cached list of packs, forcing them to be scanned again.void
close()
Close any resources held by this database.protected void
commitPack(Collection<DfsPackDescription> desc, Collection<DfsPackDescription> replaces)
Commit a pack and index pair that was written to the DFS.protected abstract void
commitPackImpl(Collection<DfsPackDescription> desc, Collection<DfsPackDescription> replaces)
Implementation of pack commit.DfsObjDatabase.PackList
getCurrentPackList()
List currently known pack files in the repository, without scanning.DfsPackFile[]
getCurrentPacks()
List currently known pack files in the repository, without scanning.DfsReftable[]
getCurrentReftables()
List currently known reftable files in the repository, without scanning.DfsObjDatabase.PackList
getPackList()
Scan and list all available pack files in the repository.DfsPackFile[]
getPacks()
Scan and list all available pack files in the repository.DfsReaderOptions
getReaderOptions()
Get configured reader options, such as read-ahead.DfsReftable[]
getReftables()
Scan and list all available reftable files in the repository.protected DfsRepository
getRepository()
Get repository owning this object database.boolean
has(AnyObjectId objectId, boolean avoidUnreachableObjects)
Does the requested object exist in this database?protected abstract List<DfsPackDescription>
listPacks()
List the available pack files.ObjectInserter
newInserter()
Create a newObjectInserter
to insert new objects.protected abstract DfsPackDescription
newPack(DfsObjDatabase.PackSource source)
Generate a new unique name for a pack file.protected DfsPackDescription
newPack(DfsObjDatabase.PackSource source, long estimatedPackSize)
Generate a new unique name for a pack file.DfsReader
newReader()
Create a newObjectReader
to read existing objects.protected abstract ReadableChannel
openFile(DfsPackDescription desc, PackExt ext)
Open a pack, pack index, or other related file for reading.protected Comparator<DfsReftable>
reftableComparator()
Get comparator to sortDfsReftable
by priority.protected abstract void
rollbackPack(Collection<DfsPackDescription> desc)
Try to rollback a pack creation.void
setPackComparator(Comparator<DfsPackDescription> packComparator)
Set the comparator used when searching for objects across packs.protected abstract DfsOutputStream
writeFile(DfsPackDescription desc, PackExt ext)
Open a pack, pack index, or other related file for writing.-
Methods inherited from class org.eclipse.jgit.lib.ObjectDatabase
create, exists, getApproximateObjectCount, getShallowCommits, has, newCachedDatabase, open, open, setShallowCommits
-
-
-
-
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 newObjectReader
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 classObjectDatabase
- Returns:
- reader the caller can use to load objects from this database.
-
newInserter
public ObjectInserter newInserter()
Create a newObjectInserter
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 classObjectDatabase
- 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 givenestomatedPackSize
value more efficiently in the process of creating a newDfsPackDescription
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.
-
commitPackImpl
protected abstract void commitPackImpl(Collection<DfsPackDescription> desc, Collection<DfsPackDescription> replaces) throws IOException
Implementation of pack commit.- Parameters:
desc
- description of the new packs.replaces
- if not null, list of packs to remove.- Throws:
IOException
- the packs cannot be committed.- See Also:
commitPack(Collection, Collection)
-
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 fromlistPacks()
, 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 fromnewPack(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.
-
reftableComparator
protected Comparator<DfsReftable> reftableComparator()
Get comparator to sortDfsReftable
by priority.- Returns:
- comparator to sort
DfsReftable
by priority.
-
clearCache
protected void clearCache()
Clears the cached list of packs, forcing them to be scanned again.
-
close
public void close()
Close any resources held by this database.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in classObjectDatabase
-
-