Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

DfsRefDatabase.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /*
  2. * Copyright (C) 2011, 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.storage.dfs;
  44. import static org.eclipse.jgit.lib.Ref.Storage.NEW;
  45. import java.io.IOException;
  46. import java.util.Collections;
  47. import java.util.List;
  48. import java.util.Map;
  49. import java.util.concurrent.atomic.AtomicReference;
  50. import org.eclipse.jgit.errors.MissingObjectException;
  51. import org.eclipse.jgit.lib.ObjectIdRef;
  52. import org.eclipse.jgit.lib.Ref;
  53. import org.eclipse.jgit.lib.RefDatabase;
  54. import org.eclipse.jgit.lib.RefRename;
  55. import org.eclipse.jgit.lib.SymbolicRef;
  56. import org.eclipse.jgit.revwalk.RevObject;
  57. import org.eclipse.jgit.revwalk.RevTag;
  58. import org.eclipse.jgit.revwalk.RevWalk;
  59. import org.eclipse.jgit.util.RefList;
  60. import org.eclipse.jgit.util.RefMap;
  61. /** */
  62. public abstract class DfsRefDatabase extends RefDatabase {
  63. private final DfsRepository repository;
  64. private final AtomicReference<RefCache> cache;
  65. /**
  66. * Initialize the reference database for a repository.
  67. *
  68. * @param repository
  69. * the repository this database instance manages references for.
  70. */
  71. protected DfsRefDatabase(DfsRepository repository) {
  72. this.repository = repository;
  73. this.cache = new AtomicReference<RefCache>();
  74. }
  75. /** @return the repository the database holds the references of. */
  76. protected DfsRepository getRepository() {
  77. return repository;
  78. }
  79. boolean exists() throws IOException {
  80. return 0 < read().size();
  81. }
  82. @Override
  83. public Ref getRef(String needle) throws IOException {
  84. RefCache curr = read();
  85. for (String prefix : SEARCH_PATH) {
  86. Ref ref = curr.ids.get(prefix + needle);
  87. if (ref != null) {
  88. ref = resolve(ref, 0, curr.ids);
  89. return ref;
  90. }
  91. }
  92. return null;
  93. }
  94. private Ref getOneRef(String refName) throws IOException {
  95. RefCache curr = read();
  96. Ref ref = curr.ids.get(refName);
  97. if (ref != null)
  98. return resolve(ref, 0, curr.ids);
  99. return ref;
  100. }
  101. @Override
  102. public List<Ref> getAdditionalRefs() {
  103. return Collections.emptyList();
  104. }
  105. @Override
  106. public Map<String, Ref> getRefs(String prefix) throws IOException {
  107. RefCache curr = read();
  108. RefList<Ref> packed = RefList.emptyList();
  109. RefList<Ref> loose = curr.ids;
  110. RefList.Builder<Ref> sym = new RefList.Builder<Ref>(curr.sym.size());
  111. for (int idx = 0; idx < curr.sym.size(); idx++) {
  112. Ref ref = curr.sym.get(idx);
  113. String name = ref.getName();
  114. ref = resolve(ref, 0, loose);
  115. if (ref != null && ref.getObjectId() != null) {
  116. sym.add(ref);
  117. } else {
  118. // A broken symbolic reference, we have to drop it from the
  119. // collections the client is about to receive. Should be a
  120. // rare occurrence so pay a copy penalty.
  121. int toRemove = loose.find(name);
  122. if (0 <= toRemove)
  123. loose = loose.remove(toRemove);
  124. }
  125. }
  126. return new RefMap(prefix, packed, loose, sym.toRefList());
  127. }
  128. private Ref resolve(Ref ref, int depth, RefList<Ref> loose)
  129. throws IOException {
  130. if (!ref.isSymbolic())
  131. return ref;
  132. Ref dst = ref.getTarget();
  133. if (MAX_SYMBOLIC_REF_DEPTH <= depth)
  134. return null; // claim it doesn't exist
  135. dst = loose.get(dst.getName());
  136. if (dst == null)
  137. return ref;
  138. dst = resolve(dst, depth + 1, loose);
  139. if (dst == null)
  140. return null;
  141. return new SymbolicRef(ref.getName(), dst);
  142. }
  143. @Override
  144. public Ref peel(Ref ref) throws IOException {
  145. final Ref oldLeaf = ref.getLeaf();
  146. if (oldLeaf.isPeeled() || oldLeaf.getObjectId() == null)
  147. return ref;
  148. Ref newLeaf = doPeel(oldLeaf);
  149. RefCache cur = read();
  150. int idx = cur.ids.find(oldLeaf.getName());
  151. if (0 <= idx && cur.ids.get(idx) == oldLeaf) {
  152. RefList<Ref> newList = cur.ids.set(idx, newLeaf);
  153. cache.compareAndSet(cur, new RefCache(newList, cur));
  154. cachePeeledState(oldLeaf, newLeaf);
  155. }
  156. return recreate(ref, newLeaf);
  157. }
  158. private Ref doPeel(final Ref leaf) throws MissingObjectException,
  159. IOException {
  160. RevWalk rw = new RevWalk(repository);
  161. try {
  162. RevObject obj = rw.parseAny(leaf.getObjectId());
  163. if (obj instanceof RevTag) {
  164. return new ObjectIdRef.PeeledTag(
  165. leaf.getStorage(),
  166. leaf.getName(),
  167. leaf.getObjectId(),
  168. rw.peel(obj).copy());
  169. } else {
  170. return new ObjectIdRef.PeeledNonTag(
  171. leaf.getStorage(),
  172. leaf.getName(),
  173. leaf.getObjectId());
  174. }
  175. } finally {
  176. rw.release();
  177. }
  178. }
  179. private static Ref recreate(Ref old, Ref leaf) {
  180. if (old.isSymbolic()) {
  181. Ref dst = recreate(old.getTarget(), leaf);
  182. return new SymbolicRef(old.getName(), dst);
  183. }
  184. return leaf;
  185. }
  186. @Override
  187. public DfsRefUpdate newUpdate(String refName, boolean detach)
  188. throws IOException {
  189. boolean detachingSymbolicRef = false;
  190. Ref ref = getOneRef(refName);
  191. if (ref == null)
  192. ref = new ObjectIdRef.Unpeeled(NEW, refName, null);
  193. else
  194. detachingSymbolicRef = detach && ref.isSymbolic();
  195. if (detachingSymbolicRef) {
  196. ref = new ObjectIdRef.Unpeeled(NEW, refName, ref.getObjectId());
  197. }
  198. DfsRefUpdate update = new DfsRefUpdate(this, ref);
  199. if (detachingSymbolicRef)
  200. update.setDetachingSymbolicRef();
  201. return update;
  202. }
  203. @Override
  204. public RefRename newRename(String fromName, String toName)
  205. throws IOException {
  206. DfsRefUpdate src = newUpdate(fromName, true);
  207. DfsRefUpdate dst = newUpdate(toName, true);
  208. return new DfsRefRename(src, dst);
  209. }
  210. @Override
  211. public boolean isNameConflicting(String refName) throws IOException {
  212. RefList<Ref> all = read().ids;
  213. // Cannot be nested within an existing reference.
  214. int lastSlash = refName.lastIndexOf('/');
  215. while (0 < lastSlash) {
  216. String needle = refName.substring(0, lastSlash);
  217. if (all.contains(needle))
  218. return true;
  219. lastSlash = refName.lastIndexOf('/', lastSlash - 1);
  220. }
  221. // Cannot be the container of an existing reference.
  222. String prefix = refName + '/';
  223. int idx = -(all.find(prefix) + 1);
  224. if (idx < all.size() && all.get(idx).getName().startsWith(prefix))
  225. return true;
  226. return false;
  227. }
  228. @Override
  229. public void create() {
  230. // Nothing to do.
  231. }
  232. @Override
  233. public void close() {
  234. clearCache();
  235. }
  236. void clearCache() {
  237. cache.set(null);
  238. }
  239. void stored(Ref ref) {
  240. RefCache oldCache, newCache;
  241. do {
  242. oldCache = cache.get();
  243. if (oldCache == null)
  244. return;
  245. newCache = oldCache.put(ref);
  246. } while (!cache.compareAndSet(oldCache, newCache));
  247. }
  248. void removed(String refName) {
  249. RefCache oldCache, newCache;
  250. do {
  251. oldCache = cache.get();
  252. if (oldCache == null)
  253. return;
  254. newCache = oldCache.remove(refName);
  255. } while (!cache.compareAndSet(oldCache, newCache));
  256. }
  257. private RefCache read() throws IOException {
  258. RefCache c = cache.get();
  259. if (c == null) {
  260. c = scanAllRefs();
  261. cache.set(c);
  262. }
  263. return c;
  264. }
  265. /**
  266. * Read all known references in the repository.
  267. *
  268. * @return all current references of the repository.
  269. * @throws IOException
  270. * references cannot be accessed.
  271. */
  272. protected abstract RefCache scanAllRefs() throws IOException;
  273. /**
  274. * Compare a reference, and put if it matches.
  275. *
  276. * @param oldRef
  277. * old value to compare to. If the reference is expected to not
  278. * exist the old value has a storage of
  279. * {@link org.eclipse.jgit.lib.Ref.Storage#NEW} and an ObjectId
  280. * value of {@code null}.
  281. * @param newRef
  282. * new reference to store.
  283. * @return true if the put was successful; false otherwise.
  284. * @throws IOException
  285. * the reference cannot be put due to a system error.
  286. */
  287. protected abstract boolean compareAndPut(Ref oldRef, Ref newRef)
  288. throws IOException;
  289. /**
  290. * Compare a reference, and delete if it matches.
  291. *
  292. * @param oldRef
  293. * the old reference information that was previously read.
  294. * @return true if the remove was successful; false otherwise.
  295. * @throws IOException
  296. * the reference could not be removed due to a system error.
  297. */
  298. protected abstract boolean compareAndRemove(Ref oldRef) throws IOException;
  299. /**
  300. * Update the cached peeled state of a reference
  301. * <p>
  302. * The ref database invokes this method after it peels a reference that had
  303. * not been peeled before. This allows the storage to cache the peel state
  304. * of the reference, and if it is actually peelable, the target that it
  305. * peels to, so that on-the-fly peeling doesn't have to happen on the next
  306. * reference read.
  307. *
  308. * @param oldLeaf
  309. * the old reference.
  310. * @param newLeaf
  311. * the new reference, with peel information.
  312. */
  313. protected void cachePeeledState(Ref oldLeaf, Ref newLeaf) {
  314. try {
  315. compareAndPut(oldLeaf, newLeaf);
  316. } catch (IOException e) {
  317. // Ignore an exception during caching.
  318. }
  319. }
  320. /** Collection of references managed by this database. */
  321. public static class RefCache {
  322. final RefList<Ref> ids;
  323. final RefList<Ref> sym;
  324. /**
  325. * Initialize a new reference cache.
  326. * <p>
  327. * The two reference lists supplied must be sorted in correct order
  328. * (string compare order) by name.
  329. *
  330. * @param ids
  331. * references that carry an ObjectId, and all of {@code sym}.
  332. * @param sym
  333. * references that are symbolic references to others.
  334. */
  335. public RefCache(RefList<Ref> ids, RefList<Ref> sym) {
  336. this.ids = ids;
  337. this.sym = sym;
  338. }
  339. RefCache(RefList<Ref> ids, RefCache old) {
  340. this(ids, old.sym);
  341. }
  342. /** @return number of references in this cache. */
  343. public int size() {
  344. return ids.size();
  345. }
  346. /**
  347. * Find a reference by name.
  348. *
  349. * @param name
  350. * full name of the reference.
  351. * @return the reference, if it exists, otherwise null.
  352. */
  353. public Ref get(String name) {
  354. return ids.get(name);
  355. }
  356. /**
  357. * Obtain a modified copy of the cache with a ref stored.
  358. * <p>
  359. * This cache instance is not modified by this method.
  360. *
  361. * @param ref
  362. * reference to add or replace.
  363. * @return a copy of this cache, with the reference added or replaced.
  364. */
  365. public RefCache put(Ref ref) {
  366. RefList<Ref> newIds = this.ids.put(ref);
  367. RefList<Ref> newSym = this.sym;
  368. if (ref.isSymbolic()) {
  369. newSym = newSym.put(ref);
  370. } else {
  371. int p = newSym.find(ref.getName());
  372. if (0 <= p)
  373. newSym = newSym.remove(p);
  374. }
  375. return new RefCache(newIds, newSym);
  376. }
  377. /**
  378. * Obtain a modified copy of the cache with the ref removed.
  379. * <p>
  380. * This cache instance is not modified by this method.
  381. *
  382. * @param refName
  383. * reference to remove, if it exists.
  384. * @return a copy of this cache, with the reference removed.
  385. */
  386. public RefCache remove(String refName) {
  387. RefList<Ref> newIds = this.ids;
  388. int p = newIds.find(refName);
  389. if (0 <= p)
  390. newIds = newIds.remove(p);
  391. RefList<Ref> newSym = this.sym;
  392. p = newSym.find(refName);
  393. if (0 <= p)
  394. newSym = newSym.remove(p);
  395. return new RefCache(newIds, newSym);
  396. }
  397. }
  398. }