View Javadoc
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  
44  package org.eclipse.jgit.lib;
45  
46  import java.io.IOException;
47  
48  import org.eclipse.jgit.errors.IncorrectObjectTypeException;
49  import org.eclipse.jgit.errors.MissingObjectException;
50  
51  /**
52   * Abstraction of arbitrary object storage.
53   * <p>
54   * An object database stores one or more Git objects, indexed by their unique
55   * {@link ObjectId}.
56   */
57  public abstract class ObjectDatabase {
58  	/** Initialize a new database instance for access. */
59  	protected ObjectDatabase() {
60  		// Protected to force extension.
61  	}
62  
63  	/**
64  	 * Does this database exist yet?
65  	 *
66  	 * @return true if this database is already created; false if the caller
67  	 *         should invoke {@link #create()} to create this database location.
68  	 */
69  	public boolean exists() {
70  		return true;
71  	}
72  
73  	/**
74  	 * Initialize a new object database at this location.
75  	 *
76  	 * @throws IOException
77  	 *             the database could not be created.
78  	 */
79  	public void create() throws IOException {
80  		// Assume no action is required.
81  	}
82  
83  	/**
84  	 * Create a new {@code ObjectInserter} to insert new objects.
85  	 * <p>
86  	 * The returned inserter is not itself thread-safe, but multiple concurrent
87  	 * inserter instances created from the same {@code ObjectDatabase} must be
88  	 * thread-safe.
89  	 *
90  	 * @return writer the caller can use to create objects in this database.
91  	 */
92  	public abstract ObjectInserter newInserter();
93  
94  	/**
95  	 * Create a new {@code ObjectReader} to read existing objects.
96  	 * <p>
97  	 * The returned reader is not itself thread-safe, but multiple concurrent
98  	 * reader instances created from the same {@code ObjectDatabase} must be
99  	 * thread-safe.
100 	 *
101 	 * @return reader the caller can use to load objects from this database.
102 	 */
103 	public abstract ObjectReader newReader();
104 
105 	/**
106 	 * Close any resources held by this database.
107 	 */
108 	public abstract void close();
109 
110 	/**
111 	 * Does the requested object exist in this database?
112 	 * <p>
113 	 * This is a one-shot call interface which may be faster than allocating a
114 	 * {@link #newReader()} to perform the lookup.
115 	 *
116 	 * @param objectId
117 	 *            identity of the object to test for existence of.
118 	 * @return true if the specified object is stored in this database.
119 	 * @throws IOException
120 	 *             the object store cannot be accessed.
121 	 */
122 	public boolean has(final AnyObjectId objectId) throws IOException {
123 		try (final ObjectReader or = newReader()) {
124 			return or.has(objectId);
125 		}
126 	}
127 
128 	/**
129 	 * Open an object from this database.
130 	 * <p>
131 	 * This is a one-shot call interface which may be faster than allocating a
132 	 * {@link #newReader()} to perform the lookup.
133 	 *
134 	 * @param objectId
135 	 *            identity of the object to open.
136 	 * @return a {@link ObjectLoader} for accessing the object.
137 	 * @throws MissingObjectException
138 	 *             the object does not exist.
139 	 * @throws IOException
140 	 *             the object store cannot be accessed.
141 	 */
142 	public ObjectLoader open(final AnyObjectId objectId)
143 			throws IOException {
144 		return open(objectId, ObjectReader.OBJ_ANY);
145 	}
146 
147 	/**
148 	 * Open an object from this database.
149 	 * <p>
150 	 * This is a one-shot call interface which may be faster than allocating a
151 	 * {@link #newReader()} to perform the lookup.
152 	 *
153 	 * @param objectId
154 	 *            identity of the object to open.
155 	 * @param typeHint
156 	 *            hint about the type of object being requested, e.g.
157 	 *            {@link Constants#OBJ_BLOB}; {@link ObjectReader#OBJ_ANY} if
158 	 *            the object type is not known, or does not matter to the
159 	 *            caller.
160 	 * @return a {@link ObjectLoader} for accessing the object.
161 	 * @throws MissingObjectException
162 	 *             the object does not exist.
163 	 * @throws IncorrectObjectTypeException
164 	 *             typeHint was not OBJ_ANY, and the object's actual type does
165 	 *             not match typeHint.
166 	 * @throws IOException
167 	 *             the object store cannot be accessed.
168 	 */
169 	public ObjectLoader open(AnyObjectId objectId, int typeHint)
170 			throws MissingObjectException, IncorrectObjectTypeException,
171 			IOException {
172 		try (final ObjectReader or = newReader()) {
173 			return or.open(objectId, typeHint);
174 		}
175 	}
176 
177 	/**
178 	 * Create a new cached database instance over this database. This instance might
179 	 * optimize queries by caching some information about database. So some modifications
180 	 * done after instance creation might fail to be noticed.
181 	 *
182 	 * @return new cached database instance
183 	 */
184 	public ObjectDatabase newCachedDatabase() {
185 		return this;
186 	}
187 }