ObjectDatabase.java

  1. /*
  2.  * Copyright (C) 2009, 2022 Google Inc. and others
  3.  *
  4.  * This program and the accompanying materials are made available under the
  5.  * terms of the Eclipse Distribution License v. 1.0 which is available at
  6.  * https://www.eclipse.org/org/documents/edl-v10.php.
  7.  *
  8.  * SPDX-License-Identifier: BSD-3-Clause
  9.  */

  10. package org.eclipse.jgit.lib;

  11. import java.io.IOException;
  12. import java.util.Collections;
  13. import java.util.Set;

  14. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  15. import org.eclipse.jgit.errors.MissingObjectException;

  16. /**
  17.  * Abstraction of arbitrary object storage.
  18.  * <p>
  19.  * An object database stores one or more Git objects, indexed by their unique
  20.  * {@link org.eclipse.jgit.lib.ObjectId}.
  21.  */
  22. public abstract class ObjectDatabase implements AutoCloseable {

  23.     private static final Set<ObjectId> shallowCommits = Collections.emptySet();

  24.     /**
  25.      * Initialize a new database instance for access.
  26.      */
  27.     protected ObjectDatabase() {
  28.         // Protected to force extension.
  29.     }

  30.     /**
  31.      * Does this database exist yet?
  32.      *
  33.      * @return true if this database is already created; false if the caller
  34.      *         should invoke {@link #create()} to create this database location.
  35.      */
  36.     public boolean exists() {
  37.         return true;
  38.     }

  39.     /**
  40.      * Initialize a new object database at this location.
  41.      *
  42.      * @throws java.io.IOException
  43.      *             the database could not be created.
  44.      */
  45.     public void create() throws IOException {
  46.         // Assume no action is required.
  47.     }

  48.     /**
  49.      * Create a new {@code ObjectInserter} to insert new objects.
  50.      * <p>
  51.      * The returned inserter is not itself thread-safe, but multiple concurrent
  52.      * inserter instances created from the same {@code ObjectDatabase} must be
  53.      * thread-safe.
  54.      *
  55.      * @return writer the caller can use to create objects in this database.
  56.      */
  57.     public abstract ObjectInserter newInserter();

  58.     /**
  59.      * Create a new {@code ObjectReader} to read existing objects.
  60.      * <p>
  61.      * The returned reader is not itself thread-safe, but multiple concurrent
  62.      * reader instances created from the same {@code ObjectDatabase} must be
  63.      * thread-safe.
  64.      *
  65.      * @return reader the caller can use to load objects from this database.
  66.      */
  67.     public abstract ObjectReader newReader();

  68.     /**
  69.      * @return the shallow commits of the current repository
  70.      *
  71.      * @throws IOException the database could not be read
  72.      *
  73.      * @since 6.3
  74.      */
  75.     public Set<ObjectId> getShallowCommits() throws IOException {
  76.         return shallowCommits;
  77.     }


  78.     /**
  79.      * Update the shallow commits of the current repository
  80.      *
  81.      * @param shallowCommits the new shallow commits
  82.      *
  83.      * @throws IOException the database could not be updated
  84.      *
  85.      * @since 6.3
  86.      */
  87.     public void setShallowCommits(Set<ObjectId> shallowCommits)
  88.             throws IOException {
  89.         if (!shallowCommits.isEmpty()) {
  90.             throw new UnsupportedOperationException(
  91.                     "Shallow commits expected to be empty."); //$NON-NLS-1$
  92.         }
  93.     }

  94.     /**
  95.      * Close any resources held by this database.
  96.      */
  97.     @Override
  98.     public abstract void close();

  99.     /**
  100.      * Does the requested object exist in this database?
  101.      * <p>
  102.      * This is a one-shot call interface which may be faster than allocating a
  103.      * {@link #newReader()} to perform the lookup.
  104.      *
  105.      * @param objectId
  106.      *            identity of the object to test for existence of.
  107.      * @return true if the specified object is stored in this database.
  108.      * @throws java.io.IOException
  109.      *             the object store cannot be accessed.
  110.      */
  111.     public boolean has(AnyObjectId objectId) throws IOException {
  112.         try (ObjectReader or = newReader()) {
  113.             return or.has(objectId);
  114.         }
  115.     }

  116.     /**
  117.      * Open an object from this database.
  118.      * <p>
  119.      * This is a one-shot call interface which may be faster than allocating a
  120.      * {@link #newReader()} to perform the lookup.
  121.      *
  122.      * @param objectId
  123.      *            identity of the object to open.
  124.      * @return a {@link org.eclipse.jgit.lib.ObjectLoader} for accessing the object.
  125.      * @throws MissingObjectException
  126.      *             the object does not exist.
  127.      * @throws java.io.IOException
  128.      *             the object store cannot be accessed.
  129.      */
  130.     public ObjectLoader open(AnyObjectId objectId)
  131.             throws IOException {
  132.         return open(objectId, ObjectReader.OBJ_ANY);
  133.     }

  134.     /**
  135.      * Open an object from this database.
  136.      * <p>
  137.      * This is a one-shot call interface which may be faster than allocating a
  138.      * {@link #newReader()} to perform the lookup.
  139.      *
  140.      * @param objectId
  141.      *            identity of the object to open.
  142.      * @param typeHint
  143.      *            hint about the type of object being requested, e.g.
  144.      *            {@link org.eclipse.jgit.lib.Constants#OBJ_BLOB};
  145.      *            {@link org.eclipse.jgit.lib.ObjectReader#OBJ_ANY} if the
  146.      *            object type is not known, or does not matter to the caller.
  147.      * @return a {@link org.eclipse.jgit.lib.ObjectLoader} for accessing the
  148.      *         object.
  149.      * @throws org.eclipse.jgit.errors.MissingObjectException
  150.      *             the object does not exist.
  151.      * @throws org.eclipse.jgit.errors.IncorrectObjectTypeException
  152.      *             typeHint was not OBJ_ANY, and the object's actual type does
  153.      *             not match typeHint.
  154.      * @throws java.io.IOException
  155.      *             the object store cannot be accessed.
  156.      */
  157.     public ObjectLoader open(AnyObjectId objectId, int typeHint)
  158.             throws MissingObjectException, IncorrectObjectTypeException,
  159.             IOException {
  160.         try (ObjectReader or = newReader()) {
  161.             return or.open(objectId, typeHint);
  162.         }
  163.     }

  164.     /**
  165.      * Create a new cached database instance over this database. This instance might
  166.      * optimize queries by caching some information about database. So some modifications
  167.      * done after instance creation might fail to be noticed.
  168.      *
  169.      * @return new cached database instance
  170.      */
  171.     public ObjectDatabase newCachedDatabase() {
  172.         return this;
  173.     }

  174.     /**
  175.      * Get a quick, rough count of objects in this repository. Ignores loose
  176.      * objects. Returns {@code -1} if an exception occurs.
  177.      *
  178.      * @return quick, rough count of objects in this repository, {@code -1} if
  179.      *         an exception occurs
  180.      * @since 6.1
  181.      */
  182.     public abstract long getApproximateObjectCount();
  183. }