您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

DfsRefDatabase.java 12KB

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