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