ObjectDatabase.java

  1. /*
  2.  * Copyright (C) 2009, Google Inc.
  3.  * and other copyright owners as documented in the project's IP log.
  4.  *
  5.  * This program and the accompanying materials are made available
  6.  * under the terms of the Eclipse Distribution License v1.0 which
  7.  * accompanies this distribution, is reproduced below, and is
  8.  * available at http://www.eclipse.org/org/documents/edl-v10.php
  9.  *
  10.  * All rights reserved.
  11.  *
  12.  * Redistribution and use in source and binary forms, with or
  13.  * without modification, are permitted provided that the following
  14.  * conditions are met:
  15.  *
  16.  * - Redistributions of source code must retain the above copyright
  17.  *   notice, this list of conditions and the following disclaimer.
  18.  *
  19.  * - Redistributions in binary form must reproduce the above
  20.  *   copyright notice, this list of conditions and the following
  21.  *   disclaimer in the documentation and/or other materials provided
  22.  *   with the distribution.
  23.  *
  24.  * - Neither the name of the Eclipse Foundation, Inc. nor the
  25.  *   names of its contributors may be used to endorse or promote
  26.  *   products derived from this software without specific prior
  27.  *   written permission.
  28.  *
  29.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30.  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31.  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33.  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34.  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38.  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39.  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41.  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42.  */

  43. package org.eclipse.jgit.lib;

  44. import java.io.IOException;

  45. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  46. import org.eclipse.jgit.errors.MissingObjectException;

  47. /**
  48.  * Abstraction of arbitrary object storage.
  49.  * <p>
  50.  * An object database stores one or more Git objects, indexed by their unique
  51.  * {@link org.eclipse.jgit.lib.ObjectId}.
  52.  */
  53. public abstract class ObjectDatabase {
  54.     /**
  55.      * Initialize a new database instance for access.
  56.      */
  57.     protected ObjectDatabase() {
  58.         // Protected to force extension.
  59.     }

  60.     /**
  61.      * Does this database exist yet?
  62.      *
  63.      * @return true if this database is already created; false if the caller
  64.      *         should invoke {@link #create()} to create this database location.
  65.      */
  66.     public boolean exists() {
  67.         return true;
  68.     }

  69.     /**
  70.      * Initialize a new object database at this location.
  71.      *
  72.      * @throws java.io.IOException
  73.      *             the database could not be created.
  74.      */
  75.     public void create() throws IOException {
  76.         // Assume no action is required.
  77.     }

  78.     /**
  79.      * Create a new {@code ObjectInserter} to insert new objects.
  80.      * <p>
  81.      * The returned inserter is not itself thread-safe, but multiple concurrent
  82.      * inserter instances created from the same {@code ObjectDatabase} must be
  83.      * thread-safe.
  84.      *
  85.      * @return writer the caller can use to create objects in this database.
  86.      */
  87.     public abstract ObjectInserter newInserter();

  88.     /**
  89.      * Create a new {@code ObjectReader} to read existing objects.
  90.      * <p>
  91.      * The returned reader is not itself thread-safe, but multiple concurrent
  92.      * reader instances created from the same {@code ObjectDatabase} must be
  93.      * thread-safe.
  94.      *
  95.      * @return reader the caller can use to load objects from this database.
  96.      */
  97.     public abstract ObjectReader newReader();

  98.     /**
  99.      * Close any resources held by this database.
  100.      */
  101.     public abstract void close();

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

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

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

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