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 implements AutoCloseable {
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  	@Override
78  	public abstract void close();
79  
80  	/**
81  	 * Does the requested object exist in this database?
82  	 * <p>
83  	 * This is a one-shot call interface which may be faster than allocating a
84  	 * {@link #newReader()} to perform the lookup.
85  	 *
86  	 * @param objectId
87  	 *            identity of the object to test for existence of.
88  	 * @return true if the specified object is stored in this database.
89  	 * @throws java.io.IOException
90  	 *             the object store cannot be accessed.
91  	 */
92  	public boolean has(AnyObjectId objectId) throws IOException {
93  		try (ObjectReader or = newReader()) {
94  			return or.has(objectId);
95  		}
96  	}
97  
98  	/**
99  	 * Open an object from this database.
100 	 * <p>
101 	 * This is a one-shot call interface which may be faster than allocating a
102 	 * {@link #newReader()} to perform the lookup.
103 	 *
104 	 * @param objectId
105 	 *            identity of the object to open.
106 	 * @return a {@link org.eclipse.jgit.lib.ObjectLoader} for accessing the object.
107 	 * @throws MissingObjectException
108 	 *             the object does not exist.
109 	 * @throws java.io.IOException
110 	 *             the object store cannot be accessed.
111 	 */
112 	public ObjectLoader open(AnyObjectId objectId)
113 			throws IOException {
114 		return open(objectId, ObjectReader.OBJ_ANY);
115 	}
116 
117 	/**
118 	 * Open an object from this database.
119 	 * <p>
120 	 * This is a one-shot call interface which may be faster than allocating a
121 	 * {@link #newReader()} to perform the lookup.
122 	 *
123 	 * @param objectId
124 	 *            identity of the object to open.
125 	 * @param typeHint
126 	 *            hint about the type of object being requested, e.g.
127 	 *            {@link org.eclipse.jgit.lib.Constants#OBJ_BLOB};
128 	 *            {@link org.eclipse.jgit.lib.ObjectReader#OBJ_ANY} if the
129 	 *            object type is not known, or does not matter to the caller.
130 	 * @return a {@link org.eclipse.jgit.lib.ObjectLoader} for accessing the
131 	 *         object.
132 	 * @throws org.eclipse.jgit.errors.MissingObjectException
133 	 *             the object does not exist.
134 	 * @throws org.eclipse.jgit.errors.IncorrectObjectTypeException
135 	 *             typeHint was not OBJ_ANY, and the object's actual type does
136 	 *             not match typeHint.
137 	 * @throws java.io.IOException
138 	 *             the object store cannot be accessed.
139 	 */
140 	public ObjectLoader open(AnyObjectId objectId, int typeHint)
141 			throws MissingObjectException, IncorrectObjectTypeException,
142 			IOException {
143 		try (ObjectReader or = newReader()) {
144 			return or.open(objectId, typeHint);
145 		}
146 	}
147 
148 	/**
149 	 * Create a new cached database instance over this database. This instance might
150 	 * optimize queries by caching some information about database. So some modifications
151 	 * done after instance creation might fail to be noticed.
152 	 *
153 	 * @return new cached database instance
154 	 */
155 	public ObjectDatabase newCachedDatabase() {
156 		return this;
157 	}
158 
159 	/**
160 	 * Get a quick, rough count of objects in this repository. Ignores loose
161 	 * objects. Returns {@code -1} if an exception occurs.
162 	 *
163 	 * @return quick, rough count of objects in this repository, {@code -1} if
164 	 *         an exception occurs
165 	 * @since 6.1
166 	 */
167 	public abstract long getApproximateObjectCount();
168 }