1 /*
2 * Copyright (C) 2009, 2022 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 import java.util.Collections;
15 import java.util.Set;
16
17 import org.eclipse.jgit.errors.IncorrectObjectTypeException;
18 import org.eclipse.jgit.errors.MissingObjectException;
19
20 /**
21 * Abstraction of arbitrary object storage.
22 * <p>
23 * An object database stores one or more Git objects, indexed by their unique
24 * {@link org.eclipse.jgit.lib.ObjectId}.
25 */
26 public abstract class ObjectDatabase implements AutoCloseable {
27
28 private static final Set<ObjectId> shallowCommits = Collections.emptySet();
29
30 /**
31 * Initialize a new database instance for access.
32 */
33 protected ObjectDatabase() {
34 // Protected to force extension.
35 }
36
37 /**
38 * Does this database exist yet?
39 *
40 * @return true if this database is already created; false if the caller
41 * should invoke {@link #create()} to create this database location.
42 */
43 public boolean exists() {
44 return true;
45 }
46
47 /**
48 * Initialize a new object database at this location.
49 *
50 * @throws java.io.IOException
51 * the database could not be created.
52 */
53 public void create() throws IOException {
54 // Assume no action is required.
55 }
56
57 /**
58 * Create a new {@code ObjectInserter} to insert new objects.
59 * <p>
60 * The returned inserter is not itself thread-safe, but multiple concurrent
61 * inserter instances created from the same {@code ObjectDatabase} must be
62 * thread-safe.
63 *
64 * @return writer the caller can use to create objects in this database.
65 */
66 public abstract ObjectInserter newInserter();
67
68 /**
69 * Create a new {@code ObjectReader} to read existing objects.
70 * <p>
71 * The returned reader is not itself thread-safe, but multiple concurrent
72 * reader instances created from the same {@code ObjectDatabase} must be
73 * thread-safe.
74 *
75 * @return reader the caller can use to load objects from this database.
76 */
77 public abstract ObjectReader newReader();
78
79 /**
80 * @return the shallow commits of the current repository
81 *
82 * @throws IOException the database could not be read
83 *
84 * @since 6.3
85 */
86 public Set<ObjectId> getShallowCommits() throws IOException {
87 return shallowCommits;
88 }
89
90
91 /**
92 * Update the shallow commits of the current repository
93 *
94 * @param shallowCommits the new shallow commits
95 *
96 * @throws IOException the database could not be updated
97 *
98 * @since 6.3
99 */
100 public void setShallowCommits(Set<ObjectId> shallowCommits)
101 throws IOException {
102 if (!shallowCommits.isEmpty()) {
103 throw new UnsupportedOperationException(
104 "Shallow commits expected to be empty."); //$NON-NLS-1$
105 }
106 }
107
108 /**
109 * Close any resources held by this database.
110 */
111 @Override
112 public abstract void close();
113
114 /**
115 * Does the requested object exist in this database?
116 * <p>
117 * This is a one-shot call interface which may be faster than allocating a
118 * {@link #newReader()} to perform the lookup.
119 *
120 * @param objectId
121 * identity of the object to test for existence of.
122 * @return true if the specified object is stored in this database.
123 * @throws java.io.IOException
124 * the object store cannot be accessed.
125 */
126 public boolean has(AnyObjectId objectId) throws IOException {
127 try (ObjectReader or = newReader()) {
128 return or.has(objectId);
129 }
130 }
131
132 /**
133 * Open an object from this database.
134 * <p>
135 * This is a one-shot call interface which may be faster than allocating a
136 * {@link #newReader()} to perform the lookup.
137 *
138 * @param objectId
139 * identity of the object to open.
140 * @return a {@link org.eclipse.jgit.lib.ObjectLoader} for accessing the object.
141 * @throws MissingObjectException
142 * the object does not exist.
143 * @throws java.io.IOException
144 * the object store cannot be accessed.
145 */
146 public ObjectLoader open(AnyObjectId objectId)
147 throws IOException {
148 return open(objectId, ObjectReader.OBJ_ANY);
149 }
150
151 /**
152 * Open an object from this database.
153 * <p>
154 * This is a one-shot call interface which may be faster than allocating a
155 * {@link #newReader()} to perform the lookup.
156 *
157 * @param objectId
158 * identity of the object to open.
159 * @param typeHint
160 * hint about the type of object being requested, e.g.
161 * {@link org.eclipse.jgit.lib.Constants#OBJ_BLOB};
162 * {@link org.eclipse.jgit.lib.ObjectReader#OBJ_ANY} if the
163 * object type is not known, or does not matter to the caller.
164 * @return a {@link org.eclipse.jgit.lib.ObjectLoader} for accessing the
165 * object.
166 * @throws org.eclipse.jgit.errors.MissingObjectException
167 * the object does not exist.
168 * @throws org.eclipse.jgit.errors.IncorrectObjectTypeException
169 * typeHint was not OBJ_ANY, and the object's actual type does
170 * not match typeHint.
171 * @throws java.io.IOException
172 * the object store cannot be accessed.
173 */
174 public ObjectLoader open(AnyObjectId objectId, int typeHint)
175 throws MissingObjectException, IncorrectObjectTypeException,
176 IOException {
177 try (ObjectReader or = newReader()) {
178 return or.open(objectId, typeHint);
179 }
180 }
181
182 /**
183 * Create a new cached database instance over this database. This instance might
184 * optimize queries by caching some information about database. So some modifications
185 * done after instance creation might fail to be noticed.
186 *
187 * @return new cached database instance
188 */
189 public ObjectDatabase newCachedDatabase() {
190 return this;
191 }
192
193 /**
194 * Get a quick, rough count of objects in this repository. Ignores loose
195 * objects. Returns {@code -1} if an exception occurs.
196 *
197 * @return quick, rough count of objects in this repository, {@code -1} if
198 * an exception occurs
199 * @since 6.1
200 */
201 public abstract long getApproximateObjectCount();
202 }