org.eclipse.jgit.internal.storage.dfs

## Class DfsObjDatabase

- java.lang.Object
- - org.eclipse.jgit.lib.ObjectDatabase
  - - org.eclipse.jgit.internal.storage.dfs.DfsObjDatabase

    public abstract class DfsObjDatabase
    extends ObjectDatabase

Manages objects stored in `DfsPackFile` on a storage system.

### Nested Class Summary

Nested Classes
`static class` `DfsObjDatabase.PackSource` Sources for a pack file.

### Constructor Summary

Constructors
`protected` `DfsObjDatabase(DfsRepository repository, DfsReaderOptions options)` Initialize an object database for our repository.

### Method Summary

All Methods Instance Methods Abstract Methods Concrete Methods
`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.
`DfsPackFile[]` `getCurrentPacks()` List currently known pack files in the repository, without scanning.
`DfsPackFile[]` `getPacks()` Scan and list all available pack files in the repository.
`DfsReaderOptions` `getReaderOptions()`
`protected DfsRepository` `getRepository()`
`protected abstract List<DfsPackDescription>` `listPacks()` List the available pack files.
`ObjectInserter` `newInserter()` Create a new `ObjectInserter` to insert new objects.
`protected abstract DfsPackDescription` `newPack(DfsObjDatabase.PackSource source)` Generate a new unique name for a pack file.
`ObjectReader` `newReader()` Create a new `ObjectReader` to read existing objects.
`protected abstract ReadableChannel` `openFile(DfsPackDescription desc, PackExt ext)` Open a pack, pack index, or other related file for reading.
`protected abstract void` `rollbackPack(Collection<DfsPackDescription> desc)` Try to rollback a pack creation.
`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, has, newCachedDatabase, open, open`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### 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()

Returns:
  configured reader options, such as read-ahead.

#### newReader

    public ObjectReader newReader()

Description copied from class: `ObjectDatabase`
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()

Description copied from class: `ObjectDatabase`
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.

#### getRepository

    protected DfsRepository getRepository()

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.

#### 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.

#### 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 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.

#### close

    public void close()

Description copied from class: `ObjectDatabase`
Close any resources held by this database.

Specified by:
  `close` in class `ObjectDatabase`