Class DfsReftableDatabase

Direct Known Subclasses:
InMemoryRepository.MemRefDatabase

public class DfsReftableDatabase extends DfsRefDatabase
A DfsRefDatabase that uses reftable for storage.

A DfsRefDatabase instance is thread-safe.

Implementors may wish to use DfsPackDescription.getMaxUpdateIndex() as the primary key identifier for a PackExt.REFTABLE only pack description, ensuring that when there are competing transactions one wins, and one will fail.

  • Field Details

  • Constructor Details

    • DfsReftableDatabase

      protected DfsReftableDatabase(DfsRepository repo)
      Initialize the reference database for a repository.
      Parameters:
      repo - the repository this database instance manages references for.
  • Method Details

    • hasVersioning

      public boolean hasVersioning()
      With versioning, each reference has a version number that increases on update. See Ref.getUpdateIndex().
      Overrides:
      hasVersioning in class RefDatabase
      Returns:
      true if the implementation assigns update indices to references.
    • performsAtomicTransactions

      public boolean performsAtomicTransactions()
      Whether the database is capable of performing batch updates as atomic transactions.

      If true, by default BatchRefUpdate instances will perform updates atomically, meaning either all updates will succeed, or all updates will fail. It is still possible to turn off this behavior on a per-batch basis by calling update.setAtomic(false).

      If false, BatchRefUpdate instances will never perform updates atomically, and calling update.setAtomic(true) will cause the entire batch to fail with REJECTED_OTHER_REASON.

      This definition of atomicity is stronger than what is provided by ReceivePack. ReceivePack will attempt to reject all commands if it knows in advance some commands may fail, even if the storage layer does not support atomic transactions. Here, atomicity applies even in the case of unforeseeable errors.

      Overrides:
      performsAtomicTransactions in class RefDatabase
      Returns:
      whether transactions are atomic by default.
    • newBatchUpdate

      public BatchRefUpdate newBatchUpdate()
      Create a new batch update to attempt on this database.

      The default implementation performs a sequential update of each command.

      Overrides:
      newBatchUpdate in class RefDatabase
      Returns:
      a new batch update object.
    • getReftableConfig

      public ReftableConfig getReftableConfig()
      Get configuration to write new reftables with.
      Returns:
      configuration to write new reftables with.
    • getLock

      protected ReentrantLock getLock()
      Get the lock protecting this instance's state.
      Returns:
      the lock protecting this instance's state.
    • compactDuringCommit

      protected boolean compactDuringCommit()
      Whether to compact reftable instead of extending the stack depth.
      Returns:
      true if commit of a new small reftable should try to replace a prior small reftable by performing a compaction, instead of extending the stack depth.
    • stack

      protected DfsReftableStack stack() throws IOException
      Obtain a handle to the stack of reftables. Must hold lock.
      Returns:
      (possibly cached) handle to the stack.
      Throws:
      IOException - if tables cannot be opened.
    • isNameConflicting

      public boolean isNameConflicting(String refName) throws IOException
      Description copied from class: DfsRefDatabase
      Determine if a proposed reference name overlaps with an existing one.

      Reference names use '/' as a component separator, and may be stored in a hierarchical storage such as a directory on the local filesystem.

      If the reference "refs/heads/foo" exists then "refs/heads/foo/bar" must not exist, as a reference cannot have a value and also be a container for other references at the same time.

      If the reference "refs/heads/foo/bar" exists than the reference "refs/heads/foo" cannot exist, for the same reason.

      Overrides:
      isNameConflicting in class DfsRefDatabase
      Parameters:
      refName - proposed name.
      Returns:
      true if the name overlaps with an existing reference; false if using this name right now would be safe.
      Throws:
      IOException - the database could not be read to check for conflicts.
      See Also:
    • exactRef

      public Ref exactRef(String name) throws IOException
      Read a single reference.

      Unlike RefDatabase.findRef(java.lang.String), this method expects an unshortened reference name and does not search using the standard RefDatabase.SEARCH_PATH.

      Overrides:
      exactRef in class DfsRefDatabase
      Parameters:
      name - the unabbreviated name of the reference.
      Returns:
      the reference (if it exists); else null.
      Throws:
      IOException - the reference space cannot be accessed.
    • getRefs

      public Map<String,Ref> getRefs(String prefix) throws IOException
      Get a section of the reference namespace.
      Overrides:
      getRefs in class DfsRefDatabase
      Parameters:
      prefix - prefix to search the namespace with; must end with /. If the empty string (RefDatabase.ALL), obtain a complete snapshot of all references.
      Returns:
      modifiable map that is a complete snapshot of the current reference namespace, with prefix removed from the start of each key. The map can be an unsorted map.
      Throws:
      IOException - the reference space cannot be accessed.
    • getRefsByPrefix

      public List<Ref> getRefsByPrefix(String prefix) throws IOException
      Returns refs whose names start with a given prefix.

      The default implementation uses RefDatabase.getRefs(String). Implementors of RefDatabase should override this method directly if a better implementation is possible.

      Overrides:
      getRefsByPrefix in class RefDatabase
      Parameters:
      prefix - string that names of refs should start with; may be empty (to return all refs).
      Returns:
      immutable list of refs whose names start with prefix.
      Throws:
      IOException - the reference space cannot be accessed.
    • getRefsByPrefixWithExclusions

      public List<Ref> getRefsByPrefixWithExclusions(String include, Set<String> excludes) throws IOException
      Returns refs whose names start with a given prefix excluding all refs that start with one of the given prefixes.

      The default implementation is not efficient. Implementors of RefDatabase should override this method directly if a better implementation is possible.

      Overrides:
      getRefsByPrefixWithExclusions in class RefDatabase
      Parameters:
      include - string that names of refs should start with; may be empty.
      excludes - strings that names of refs can't start with; may be empty.
      Returns:
      immutable list of refs whose names start with prefix and none of the strings in exclude.
      Throws:
      IOException - the reference space cannot be accessed.
    • getTipsWithSha1

      public Set<Ref> getTipsWithSha1(ObjectId id) throws IOException
      Returns all refs that resolve directly to the given ObjectId. Includes peeled ObjectIds. This is the inverse lookup of RefDatabase.exactRef(String...).

      The default implementation uses a linear scan. Implementors of RefDatabase should override this method directly if a better implementation is possible.

      Overrides:
      getTipsWithSha1 in class RefDatabase
      Parameters:
      id - ObjectId to resolve
      Returns:
      a Set of Refs whose tips point to the provided id.
      Throws:
      IOException - the reference space cannot be accessed.
    • hasFastTipsWithSha1

      public boolean hasFastTipsWithSha1() throws IOException
      If the ref database does not support fast inverse queries, it may be advantageous to build a complete SHA1 to ref map in advance for multiple uses. To let applications decide on this decision, this function indicates whether the inverse map is available.
      Overrides:
      hasFastTipsWithSha1 in class RefDatabase
      Returns:
      whether this RefDatabase supports fast inverse ref queries.
      Throws:
      IOException - on I/O problems.
    • peel

      public Ref peel(Ref ref) throws IOException
      Peel a possibly unpeeled reference by traversing the annotated tags.

      If the reference cannot be peeled (as it does not refer to an annotated tag) the peeled id stays null, but Ref.isPeeled() will be true.

      Implementors should check Ref.isPeeled() before performing any additional work effort.

      Overrides:
      peel in class DfsRefDatabase
      Parameters:
      ref - The reference to peel
      Returns:
      ref if ref.isPeeled() is true; otherwise a new Ref object representing the same data as Ref, but isPeeled() will be true and getPeeledObjectId() will contain the peeled object (or null).
      Throws:
      IOException - the reference space or object space cannot be accessed.
    • exists

      boolean exists() throws IOException
      Overrides:
      exists in class DfsRefDatabase
      Throws:
      IOException
    • clearCache

      void clearCache()
      Overrides:
      clearCache in class DfsRefDatabase
    • compareAndPut

      protected boolean compareAndPut(Ref oldRef, @Nullable Ref newRef) throws IOException
      Compare a reference, and put if it matches.

      Two reference match if and only if they satisfy the following:

      • If one reference is a symbolic ref, the other one should be a symbolic ref.
      • If both are symbolic refs, the target names should be same.
      • If both are object ID refs, the object IDs should be same.
      Specified by:
      compareAndPut in class DfsRefDatabase
      Parameters:
      oldRef - old value to compare to. If the reference is expected to not exist the old value has a storage of Ref.Storage.NEW and an ObjectId value of null.
      newRef - new reference to store.
      Returns:
      true if the put was successful; false otherwise.
      Throws:
      IOException - the reference cannot be put due to a system error.
    • compareAndRemove

      protected boolean compareAndRemove(Ref oldRef) throws IOException
      Compare a reference, and delete if it matches.
      Specified by:
      compareAndRemove in class DfsRefDatabase
      Parameters:
      oldRef - the old reference information that was previously read.
      Returns:
      true if the remove was successful; false otherwise.
      Throws:
      IOException - the reference could not be removed due to a system error.
    • scanAllRefs

      protected DfsRefDatabase.RefCache scanAllRefs() throws IOException
      Read all known references in the repository.
      Specified by:
      scanAllRefs in class DfsRefDatabase
      Returns:
      all current references of the repository.
      Throws:
      IOException - references cannot be accessed.
    • stored

      void stored(Ref ref)
      Overrides:
      stored in class DfsRefDatabase
    • removed

      void removed(String refName)
      Overrides:
      removed in class DfsRefDatabase
    • cachePeeledState

      protected void cachePeeledState(Ref oldLeaf, Ref newLeaf)
      Update the cached peeled state of a reference

      The ref database invokes this method after it peels a reference that had not been peeled before. This allows the storage to cache the peel state of the reference, and if it is actually peelable, the target that it peels to, so that on-the-fly peeling doesn't have to happen on the next reference read.

      Overrides:
      cachePeeledState in class DfsRefDatabase
      Parameters:
      oldLeaf - the old reference.
      newLeaf - the new reference, with peel information.