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.

ReftableDatabase.java 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /*
  2. * Copyright (C) 2017, Google LLC
  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.internal.storage.reftable;
  44. import java.io.IOException;
  45. import java.util.ArrayList;
  46. import java.util.Collections;
  47. import java.util.HashSet;
  48. import java.util.List;
  49. import java.util.Set;
  50. import java.util.TreeSet;
  51. import java.util.concurrent.locks.ReentrantLock;
  52. import org.eclipse.jgit.annotations.Nullable;
  53. import org.eclipse.jgit.lib.ObjectId;
  54. import org.eclipse.jgit.lib.Ref;
  55. import org.eclipse.jgit.lib.RefDatabase;
  56. import org.eclipse.jgit.lib.ReflogReader;
  57. import org.eclipse.jgit.transport.ReceiveCommand;
  58. /**
  59. * Operations on {@link MergedReftable} that is common to various reftable-using
  60. * subclasses of {@link RefDatabase}. See
  61. * {@link org.eclipse.jgit.internal.storage.dfs.DfsReftableDatabase} for an
  62. * example.
  63. */
  64. public abstract class ReftableDatabase {
  65. // Protects mergedTables.
  66. private final ReentrantLock lock = new ReentrantLock(true);
  67. private Reftable mergedTables;
  68. /**
  69. * ReftableDatabase lazily initializes its merged reftable on the first read after
  70. * construction or clearCache() call. This function should always instantiate a new
  71. * MergedReftable based on the list of reftables specified by the underlying storage.
  72. *
  73. * @return the ReftableStack for this instance
  74. * @throws IOException
  75. * on I/O problems.
  76. */
  77. abstract protected MergedReftable openMergedReftable() throws IOException;
  78. /**
  79. * @return the next available logical timestamp for an additional reftable
  80. * in the stack.
  81. * @throws java.io.IOException
  82. * on I/O problems.
  83. */
  84. public long nextUpdateIndex() throws IOException {
  85. lock.lock();
  86. try {
  87. return reader().maxUpdateIndex() + 1;
  88. } finally {
  89. lock.unlock();
  90. }
  91. }
  92. /**
  93. * @return a ReflogReader for the given ref
  94. * @param refname
  95. * the name of the ref.
  96. * @throws IOException
  97. * on I/O problems
  98. */
  99. public ReflogReader getReflogReader(String refname) throws IOException {
  100. lock.lock();
  101. try {
  102. return new ReftableReflogReader(lock, reader(), refname);
  103. } finally {
  104. lock.unlock();
  105. }
  106. }
  107. /**
  108. * @return a ReceiveCommand for the change from oldRef to newRef
  109. * @param oldRef
  110. * a ref
  111. * @param newRef
  112. * a ref
  113. */
  114. public static ReceiveCommand toCommand(Ref oldRef, Ref newRef) {
  115. ObjectId oldId = toId(oldRef);
  116. ObjectId newId = toId(newRef);
  117. String name = oldRef != null ? oldRef.getName() : newRef.getName();
  118. if (oldRef != null && oldRef.isSymbolic()) {
  119. if (newRef != null) {
  120. if (newRef.isSymbolic()) {
  121. return ReceiveCommand.link(oldRef.getTarget().getName(),
  122. newRef.getTarget().getName(), name);
  123. }
  124. // This should pass in oldId for compat with
  125. // RefDirectoryUpdate
  126. return ReceiveCommand.unlink(oldRef.getTarget().getName(),
  127. newId, name);
  128. }
  129. return ReceiveCommand.unlink(oldRef.getTarget().getName(),
  130. ObjectId.zeroId(), name);
  131. }
  132. if (newRef != null && newRef.isSymbolic()) {
  133. if (oldRef != null) {
  134. if (oldRef.isSymbolic()) {
  135. return ReceiveCommand.link(oldRef.getTarget().getName(),
  136. newRef.getTarget().getName(), name);
  137. }
  138. return ReceiveCommand.link(oldId,
  139. newRef.getTarget().getName(), name);
  140. }
  141. return ReceiveCommand.link(ObjectId.zeroId(),
  142. newRef.getTarget().getName(), name);
  143. }
  144. return new ReceiveCommand(oldId, newId, name);
  145. }
  146. private static ObjectId toId(Ref ref) {
  147. if (ref != null) {
  148. ObjectId id = ref.getObjectId();
  149. if (id != null) {
  150. return id;
  151. }
  152. }
  153. return ObjectId.zeroId();
  154. }
  155. /**
  156. * @return the lock protecting underlying ReftableReaders against concurrent
  157. * reads.
  158. */
  159. public ReentrantLock getLock() {
  160. return lock;
  161. }
  162. /**
  163. * @return the merged reftable that is implemented by the stack of
  164. * reftables. Return value must be accessed under lock.
  165. * @throws IOException
  166. * on I/O problems
  167. */
  168. private Reftable reader() throws IOException {
  169. if (!lock.isLocked()) {
  170. throw new IllegalStateException(
  171. "must hold lock to access merged table"); //$NON-NLS-1$
  172. }
  173. if (mergedTables == null) {
  174. mergedTables = openMergedReftable();
  175. }
  176. return mergedTables;
  177. }
  178. /**
  179. * @return whether the given refName would be illegal in a repository that
  180. * uses loose refs.
  181. * @param refName
  182. * the name to check
  183. * @param added
  184. * a sorted set of refs we pretend have been added to the
  185. * database.
  186. * @param deleted
  187. * a set of refs we pretend have been removed from the database.
  188. * @throws IOException
  189. * on I/O problems
  190. */
  191. public boolean isNameConflicting(String refName, TreeSet<String> added,
  192. Set<String> deleted) throws IOException {
  193. lock.lock();
  194. try {
  195. Reftable table = reader();
  196. // Cannot be nested within an existing reference.
  197. int lastSlash = refName.lastIndexOf('/');
  198. while (0 < lastSlash) {
  199. String prefix = refName.substring(0, lastSlash);
  200. if (!deleted.contains(prefix)
  201. && (table.hasRef(prefix) || added.contains(prefix))) {
  202. return true;
  203. }
  204. lastSlash = refName.lastIndexOf('/', lastSlash - 1);
  205. }
  206. // Cannot be the container of an existing reference.
  207. String prefix = refName + '/';
  208. RefCursor c = table.seekRefsWithPrefix(prefix);
  209. while (c.next()) {
  210. if (!deleted.contains(c.getRef().getName())) {
  211. return true;
  212. }
  213. }
  214. String it = added.ceiling(refName + '/');
  215. if (it != null && it.startsWith(prefix)) {
  216. return true;
  217. }
  218. return false;
  219. } finally {
  220. lock.unlock();
  221. }
  222. }
  223. /**
  224. * Read a single reference.
  225. * <p>
  226. * This method expects an unshortened reference name and does not search
  227. * using the standard search path.
  228. *
  229. * @param name
  230. * the unabbreviated name of the reference.
  231. * @return the reference (if it exists); else {@code null}.
  232. * @throws java.io.IOException
  233. * the reference space cannot be accessed.
  234. */
  235. @Nullable
  236. public Ref exactRef(String name) throws IOException {
  237. lock.lock();
  238. try {
  239. Reftable table = reader();
  240. Ref ref = table.exactRef(name);
  241. if (ref != null && ref.isSymbolic()) {
  242. return table.resolve(ref);
  243. }
  244. return ref;
  245. } finally {
  246. lock.unlock();
  247. }
  248. }
  249. /**
  250. * Returns refs whose names start with a given prefix.
  251. *
  252. * @param prefix
  253. * string that names of refs should start with; may be empty (to
  254. * return all refs).
  255. * @return immutable list of refs whose names start with {@code prefix}.
  256. * @throws java.io.IOException
  257. * the reference space cannot be accessed.
  258. */
  259. public List<Ref> getRefsByPrefix(String prefix) throws IOException {
  260. List<Ref> all = new ArrayList<>();
  261. lock.lock();
  262. try {
  263. Reftable table = reader();
  264. try (RefCursor rc = RefDatabase.ALL.equals(prefix) ? table.allRefs()
  265. : table.seekRefsWithPrefix(prefix)) {
  266. while (rc.next()) {
  267. Ref ref = table.resolve(rc.getRef());
  268. if (ref != null && ref.getObjectId() != null) {
  269. all.add(ref);
  270. }
  271. }
  272. }
  273. } finally {
  274. lock.unlock();
  275. }
  276. return Collections.unmodifiableList(all);
  277. }
  278. /**
  279. * @return whether there is a fast SHA1 to ref map.
  280. * @throws IOException in case of I/O problems.
  281. */
  282. public boolean hasFastTipsWithSha1() throws IOException {
  283. lock.lock();
  284. try {
  285. return reader().hasObjectMap();
  286. } finally {
  287. lock.unlock();
  288. }
  289. }
  290. /**
  291. * Returns all refs that resolve directly to the given {@link ObjectId}.
  292. * Includes peeled {@linkObjectId}s.
  293. *
  294. * @param id
  295. * {@link ObjectId} to resolve
  296. * @return a {@link Set} of {@link Ref}s whose tips point to the provided
  297. * id.
  298. * @throws java.io.IOException
  299. * on I/O errors.
  300. */
  301. public Set<Ref> getTipsWithSha1(ObjectId id) throws IOException {
  302. lock.lock();
  303. try {
  304. RefCursor cursor = reader().byObjectId(id);
  305. Set<Ref> refs = new HashSet<>();
  306. while (cursor.next()) {
  307. refs.add(cursor.getRef());
  308. }
  309. return refs;
  310. } finally {
  311. lock.unlock();
  312. }
  313. }
  314. /**
  315. * Drops all data that might be cached in memory.
  316. */
  317. public void clearCache() {
  318. lock.lock();
  319. try {
  320. mergedTables = null;
  321. } finally {
  322. lock.unlock();
  323. }
  324. }
  325. }