You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ObjectDatabase.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. package org.eclipse.jgit.lib;
  44. import java.io.IOException;
  45. import java.util.Collection;
  46. import java.util.concurrent.atomic.AtomicReference;
  47. /**
  48. * Abstraction of arbitrary object storage.
  49. * <p>
  50. * An object database stores one or more Git objects, indexed by their unique
  51. * {@link ObjectId}. Optionally an object database can reference one or more
  52. * alternates; other ObjectDatabase instances that are searched in addition to
  53. * the current database.
  54. * <p>
  55. * Databases are usually divided into two halves: a half that is considered to
  56. * be fast to search, and a half that is considered to be slow to search. When
  57. * alternates are present the fast half is fully searched (recursively through
  58. * all alternates) before the slow half is considered.
  59. */
  60. public abstract class ObjectDatabase {
  61. /** Constant indicating no alternate databases exist. */
  62. protected static final ObjectDatabase[] NO_ALTERNATES = {};
  63. private final AtomicReference<ObjectDatabase[]> alternates;
  64. /** Initialize a new database instance for access. */
  65. protected ObjectDatabase() {
  66. alternates = new AtomicReference<ObjectDatabase[]>();
  67. }
  68. /**
  69. * Does this database exist yet?
  70. *
  71. * @return true if this database is already created; false if the caller
  72. * should invoke {@link #create()} to create this database location.
  73. */
  74. public boolean exists() {
  75. return true;
  76. }
  77. /**
  78. * Initialize a new object database at this location.
  79. *
  80. * @throws IOException
  81. * the database could not be created.
  82. */
  83. public void create() throws IOException {
  84. // Assume no action is required.
  85. }
  86. /**
  87. * Close any resources held by this database and its active alternates.
  88. */
  89. public final void close() {
  90. closeSelf();
  91. closeAlternates();
  92. }
  93. /**
  94. * Close any resources held by this database only; ignoring alternates.
  95. * <p>
  96. * To fully close this database and its referenced alternates, the caller
  97. * should instead invoke {@link #close()}.
  98. */
  99. public void closeSelf() {
  100. // Assume no action is required.
  101. }
  102. /** Fully close all loaded alternates and clear the alternate list. */
  103. public final void closeAlternates() {
  104. ObjectDatabase[] alt = alternates.get();
  105. if (alt != null) {
  106. alternates.set(null);
  107. closeAlternates(alt);
  108. }
  109. }
  110. /**
  111. * Does the requested object exist in this database?
  112. * <p>
  113. * Alternates (if present) are searched automatically.
  114. *
  115. * @param objectId
  116. * identity of the object to test for existence of.
  117. * @return true if the specified object is stored in this database, or any
  118. * of the alternate databases.
  119. */
  120. public final boolean hasObject(final AnyObjectId objectId) {
  121. return hasObjectImpl1(objectId) || hasObjectImpl2(objectId.name());
  122. }
  123. private final boolean hasObjectImpl1(final AnyObjectId objectId) {
  124. if (hasObject1(objectId)) {
  125. return true;
  126. }
  127. for (final ObjectDatabase alt : getAlternates()) {
  128. if (alt.hasObjectImpl1(objectId)) {
  129. return true;
  130. }
  131. }
  132. return tryAgain1() && hasObject1(objectId);
  133. }
  134. private final boolean hasObjectImpl2(final String objectId) {
  135. if (hasObject2(objectId)) {
  136. return true;
  137. }
  138. for (final ObjectDatabase alt : getAlternates()) {
  139. if (alt.hasObjectImpl2(objectId)) {
  140. return true;
  141. }
  142. }
  143. return false;
  144. }
  145. /**
  146. * Fast half of {@link #hasObject(AnyObjectId)}.
  147. *
  148. * @param objectId
  149. * identity of the object to test for existence of.
  150. * @return true if the specified object is stored in this database.
  151. */
  152. protected abstract boolean hasObject1(AnyObjectId objectId);
  153. /**
  154. * Slow half of {@link #hasObject(AnyObjectId)}.
  155. *
  156. * @param objectName
  157. * identity of the object to test for existence of.
  158. * @return true if the specified object is stored in this database.
  159. */
  160. protected boolean hasObject2(String objectName) {
  161. // Assume the search took place during hasObject1.
  162. return false;
  163. }
  164. /**
  165. * Open an object from this database.
  166. * <p>
  167. * Alternates (if present) are searched automatically.
  168. *
  169. * @param curs
  170. * temporary working space associated with the calling thread.
  171. * @param objectId
  172. * identity of the object to open.
  173. * @return a {@link ObjectLoader} for accessing the data of the named
  174. * object, or null if the object does not exist.
  175. * @throws IOException
  176. */
  177. public final ObjectLoader openObject(final WindowCursor curs,
  178. final AnyObjectId objectId) throws IOException {
  179. ObjectLoader ldr;
  180. ldr = openObjectImpl1(curs, objectId);
  181. if (ldr != null) {
  182. return ldr;
  183. }
  184. ldr = openObjectImpl2(curs, objectId.name(), objectId);
  185. if (ldr != null) {
  186. return ldr;
  187. }
  188. return null;
  189. }
  190. private ObjectLoader openObjectImpl1(final WindowCursor curs,
  191. final AnyObjectId objectId) throws IOException {
  192. ObjectLoader ldr;
  193. ldr = openObject1(curs, objectId);
  194. if (ldr != null) {
  195. return ldr;
  196. }
  197. for (final ObjectDatabase alt : getAlternates()) {
  198. ldr = alt.openObjectImpl1(curs, objectId);
  199. if (ldr != null) {
  200. return ldr;
  201. }
  202. }
  203. if (tryAgain1()) {
  204. ldr = openObject1(curs, objectId);
  205. if (ldr != null) {
  206. return ldr;
  207. }
  208. }
  209. return null;
  210. }
  211. private ObjectLoader openObjectImpl2(final WindowCursor curs,
  212. final String objectName, final AnyObjectId objectId)
  213. throws IOException {
  214. ObjectLoader ldr;
  215. ldr = openObject2(curs, objectName, objectId);
  216. if (ldr != null) {
  217. return ldr;
  218. }
  219. for (final ObjectDatabase alt : getAlternates()) {
  220. ldr = alt.openObjectImpl2(curs, objectName, objectId);
  221. if (ldr != null) {
  222. return ldr;
  223. }
  224. }
  225. return null;
  226. }
  227. /**
  228. * Fast half of {@link #openObject(WindowCursor, AnyObjectId)}.
  229. *
  230. * @param curs
  231. * temporary working space associated with the calling thread.
  232. * @param objectId
  233. * identity of the object to open.
  234. * @return a {@link ObjectLoader} for accessing the data of the named
  235. * object, or null if the object does not exist.
  236. * @throws IOException
  237. */
  238. protected abstract ObjectLoader openObject1(WindowCursor curs,
  239. AnyObjectId objectId) throws IOException;
  240. /**
  241. * Slow half of {@link #openObject(WindowCursor, AnyObjectId)}.
  242. *
  243. * @param curs
  244. * temporary working space associated with the calling thread.
  245. * @param objectName
  246. * name of the object to open.
  247. * @param objectId
  248. * identity of the object to open.
  249. * @return a {@link ObjectLoader} for accessing the data of the named
  250. * object, or null if the object does not exist.
  251. * @throws IOException
  252. */
  253. protected ObjectLoader openObject2(WindowCursor curs, String objectName,
  254. AnyObjectId objectId) throws IOException {
  255. // Assume the search took place during openObject1.
  256. return null;
  257. }
  258. /**
  259. * Open the object from all packs containing it.
  260. * <p>
  261. * If any alternates are present, their packs are also considered.
  262. *
  263. * @param out
  264. * result collection of loaders for this object, filled with
  265. * loaders from all packs containing specified object
  266. * @param curs
  267. * temporary working space associated with the calling thread.
  268. * @param objectId
  269. * id of object to search for
  270. * @throws IOException
  271. */
  272. final void openObjectInAllPacks(final Collection<PackedObjectLoader> out,
  273. final WindowCursor curs, final AnyObjectId objectId)
  274. throws IOException {
  275. openObjectInAllPacks1(out, curs, objectId);
  276. for (final ObjectDatabase alt : getAlternates()) {
  277. alt.openObjectInAllPacks1(out, curs, objectId);
  278. }
  279. }
  280. /**
  281. * Open the object from all packs containing it.
  282. *
  283. * @param out
  284. * result collection of loaders for this object, filled with
  285. * loaders from all packs containing specified object
  286. * @param curs
  287. * temporary working space associated with the calling thread.
  288. * @param objectId
  289. * id of object to search for
  290. * @throws IOException
  291. */
  292. void openObjectInAllPacks1(Collection<PackedObjectLoader> out,
  293. WindowCursor curs, AnyObjectId objectId) throws IOException {
  294. // Assume no pack support
  295. }
  296. /**
  297. * @return true if the fast-half search should be tried again.
  298. */
  299. protected boolean tryAgain1() {
  300. return false;
  301. }
  302. /**
  303. * Get the alternate databases known to this database.
  304. *
  305. * @return the alternate list. Never null, but may be an empty array.
  306. */
  307. public final ObjectDatabase[] getAlternates() {
  308. ObjectDatabase[] r = alternates.get();
  309. if (r == null) {
  310. synchronized (alternates) {
  311. r = alternates.get();
  312. if (r == null) {
  313. try {
  314. r = loadAlternates();
  315. } catch (IOException e) {
  316. r = NO_ALTERNATES;
  317. }
  318. alternates.set(r);
  319. }
  320. }
  321. }
  322. return r;
  323. }
  324. /**
  325. * Load the list of alternate databases into memory.
  326. * <p>
  327. * This method is invoked by {@link #getAlternates()} if the alternate list
  328. * has not yet been populated, or if {@link #closeAlternates()} has been
  329. * called on this instance and the alternate list is needed again.
  330. * <p>
  331. * If the alternate array is empty, implementors should consider using the
  332. * constant {@link #NO_ALTERNATES}.
  333. *
  334. * @return the alternate list for this database.
  335. * @throws IOException
  336. * the alternate list could not be accessed. The empty alternate
  337. * array {@link #NO_ALTERNATES} will be assumed by the caller.
  338. */
  339. protected ObjectDatabase[] loadAlternates() throws IOException {
  340. return NO_ALTERNATES;
  341. }
  342. /**
  343. * Close the list of alternates returned by {@link #loadAlternates()}.
  344. *
  345. * @param alt
  346. * the alternate list, from {@link #loadAlternates()}.
  347. */
  348. protected void closeAlternates(ObjectDatabase[] alt) {
  349. for (final ObjectDatabase d : alt) {
  350. d.close();
  351. }
  352. }
  353. /**
  354. * Create a new cached database instance over this database. This instance might
  355. * optimize queries by caching some information about database. So some modifications
  356. * done after instance creation might fail to be noticed.
  357. *
  358. * @return new cached database instance
  359. * @see CachedObjectDatabase
  360. */
  361. public ObjectDatabase newCachedDatabase() {
  362. return new CachedObjectDatabase(this);
  363. }
  364. }