ObjectDatabase.java

  1. /*
  2.  * Copyright (C) 2009, 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 org.eclipse.jgit.errors.IncorrectObjectTypeException;
  13. import org.eclipse.jgit.errors.MissingObjectException;

  14. /**
  15.  * Abstraction of arbitrary object storage.
  16.  * <p>
  17.  * An object database stores one or more Git objects, indexed by their unique
  18.  * {@link org.eclipse.jgit.lib.ObjectId}.
  19.  */
  20. public abstract class ObjectDatabase {
  21.     /**
  22.      * Initialize a new database instance for access.
  23.      */
  24.     protected ObjectDatabase() {
  25.         // Protected to force extension.
  26.     }

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

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

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

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

  65.     /**
  66.      * Close any resources held by this database.
  67.      */
  68.     public abstract void close();

  69.     /**
  70.      * Does the requested object exist in this database?
  71.      * <p>
  72.      * This is a one-shot call interface which may be faster than allocating a
  73.      * {@link #newReader()} to perform the lookup.
  74.      *
  75.      * @param objectId
  76.      *            identity of the object to test for existence of.
  77.      * @return true if the specified object is stored in this database.
  78.      * @throws java.io.IOException
  79.      *             the object store cannot be accessed.
  80.      */
  81.     public boolean has(AnyObjectId objectId) throws IOException {
  82.         try (ObjectReader or = newReader()) {
  83.             return or.has(objectId);
  84.         }
  85.     }

  86.     /**
  87.      * Open an object from this database.
  88.      * <p>
  89.      * This is a one-shot call interface which may be faster than allocating a
  90.      * {@link #newReader()} to perform the lookup.
  91.      *
  92.      * @param objectId
  93.      *            identity of the object to open.
  94.      * @return a {@link org.eclipse.jgit.lib.ObjectLoader} for accessing the object.
  95.      * @throws MissingObjectException
  96.      *             the object does not exist.
  97.      * @throws java.io.IOException
  98.      *             the object store cannot be accessed.
  99.      */
  100.     public ObjectLoader open(AnyObjectId objectId)
  101.             throws IOException {
  102.         return open(objectId, ObjectReader.OBJ_ANY);
  103.     }

  104.     /**
  105.      * Open an object from this database.
  106.      * <p>
  107.      * This is a one-shot call interface which may be faster than allocating a
  108.      * {@link #newReader()} to perform the lookup.
  109.      *
  110.      * @param objectId
  111.      *            identity of the object to open.
  112.      * @param typeHint
  113.      *            hint about the type of object being requested, e.g.
  114.      *            {@link org.eclipse.jgit.lib.Constants#OBJ_BLOB};
  115.      *            {@link org.eclipse.jgit.lib.ObjectReader#OBJ_ANY} if the
  116.      *            object type is not known, or does not matter to the caller.
  117.      * @return a {@link org.eclipse.jgit.lib.ObjectLoader} for accessing the
  118.      *         object.
  119.      * @throws org.eclipse.jgit.errors.MissingObjectException
  120.      *             the object does not exist.
  121.      * @throws org.eclipse.jgit.errors.IncorrectObjectTypeException
  122.      *             typeHint was not OBJ_ANY, and the object's actual type does
  123.      *             not match typeHint.
  124.      * @throws java.io.IOException
  125.      *             the object store cannot be accessed.
  126.      */
  127.     public ObjectLoader open(AnyObjectId objectId, int typeHint)
  128.             throws MissingObjectException, IncorrectObjectTypeException,
  129.             IOException {
  130.         try (ObjectReader or = newReader()) {
  131.             return or.open(objectId, typeHint);
  132.         }
  133.     }

  134.     /**
  135.      * Create a new cached database instance over this database. This instance might
  136.      * optimize queries by caching some information about database. So some modifications
  137.      * done after instance creation might fail to be noticed.
  138.      *
  139.      * @return new cached database instance
  140.      */
  141.     public ObjectDatabase newCachedDatabase() {
  142.         return this;
  143.     }
  144. }