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