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.

RefMap.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /*
  2. * Copyright (C) 2010, 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.util;
  44. import java.util.AbstractMap;
  45. import java.util.AbstractSet;
  46. import java.util.Iterator;
  47. import java.util.Map;
  48. import java.util.NoSuchElementException;
  49. import java.util.Set;
  50. import org.eclipse.jgit.lib.AnyObjectId;
  51. import org.eclipse.jgit.lib.ObjectId;
  52. import org.eclipse.jgit.lib.Ref;
  53. import org.eclipse.jgit.lib.RefComparator;
  54. /**
  55. * Specialized Map to present a {@code RefDatabase} namespace.
  56. * <p>
  57. * Although not declared as a {@link java.util.SortedMap}, iterators from this
  58. * map's projections always return references in {@link RefComparator} ordering.
  59. * The map's internal representation is a sorted array of {@link Ref} objects,
  60. * which means lookup and replacement is O(log N), while insertion and removal
  61. * can be as expensive as O(N + log N) while the list expands or contracts.
  62. * Since this is not a general map implementation, all entries must be keyed by
  63. * the reference name.
  64. * <p>
  65. * This class is really intended as a helper for {@code RefDatabase}, which
  66. * needs to perform a merge-join of three sorted {@link RefList}s in order to
  67. * present the unified namespace of the packed-refs file, the loose refs/
  68. * directory tree, and the resolved form of any symbolic references.
  69. */
  70. public class RefMap extends AbstractMap<String, Ref> {
  71. /**
  72. * Prefix denoting the reference subspace this map contains.
  73. * <p>
  74. * All reference names in this map must start with this prefix. If the
  75. * prefix is not the empty string, it must end with a '/'.
  76. */
  77. private final String prefix;
  78. /** Immutable collection of the packed references at construction time. */
  79. private RefList<Ref> packed;
  80. /**
  81. * Immutable collection of the loose references at construction time.
  82. * <p>
  83. * If an entry appears here and in {@link #packed}, this entry must take
  84. * precedence, as its more current. Symbolic references in this collection
  85. * are typically unresolved, so they only tell us who their target is, but
  86. * not the current value of the target.
  87. */
  88. private RefList<Ref> loose;
  89. /**
  90. * Immutable collection of resolved symbolic references.
  91. * <p>
  92. * This collection contains only the symbolic references we were able to
  93. * resolve at map construction time. Other loose references must be read
  94. * from {@link #loose}. Every entry in this list must be matched by an entry
  95. * in {@code loose}, otherwise it might be omitted by the map.
  96. */
  97. private RefList<Ref> resolved;
  98. private int size;
  99. private boolean sizeIsValid;
  100. private Set<Entry<String, Ref>> entrySet;
  101. /** Construct an empty map with a small initial capacity. */
  102. public RefMap() {
  103. prefix = "";
  104. packed = RefList.emptyList();
  105. loose = RefList.emptyList();
  106. resolved = RefList.emptyList();
  107. }
  108. /**
  109. * Construct a map to merge 3 collections together.
  110. *
  111. * @param prefix
  112. * prefix used to slice the lists down. Only references whose
  113. * names start with this prefix will appear to reside in the map.
  114. * Must not be null, use {@code ""} (the empty string) to select
  115. * all list items.
  116. * @param packed
  117. * items from the packed reference list, this is the last list
  118. * searched.
  119. * @param loose
  120. * items from the loose reference list, this list overrides
  121. * {@code packed} if a name appears in both.
  122. * @param resolved
  123. * resolved symbolic references. This list overrides the prior
  124. * list {@code loose}, if an item appears in both. Items in this
  125. * list <b>must</b> also appear in {@code loose}.
  126. */
  127. public RefMap(String prefix, RefList<Ref> packed, RefList<Ref> loose,
  128. RefList<Ref> resolved) {
  129. this.prefix = prefix;
  130. this.packed = packed;
  131. this.loose = loose;
  132. this.resolved = resolved;
  133. }
  134. @Override
  135. public boolean containsKey(Object name) {
  136. return get(name) != null;
  137. }
  138. @Override
  139. public Ref get(Object key) {
  140. String name = toRefName((String) key);
  141. Ref ref = resolved.get(name);
  142. if (ref == null)
  143. ref = loose.get(name);
  144. if (ref == null)
  145. ref = packed.get(name);
  146. return ref;
  147. }
  148. @Override
  149. public Ref put(final String keyName, Ref value) {
  150. String name = toRefName(keyName);
  151. if (!name.equals(value.getName()))
  152. throw new IllegalArgumentException();
  153. if (!resolved.isEmpty()) {
  154. // Collapse the resolved list into the loose list so we
  155. // can discard it and stop joining the two together.
  156. for (Ref ref : resolved)
  157. loose = loose.put(ref);
  158. resolved = RefList.emptyList();
  159. }
  160. int idx = loose.find(name);
  161. if (0 <= idx) {
  162. Ref prior = loose.get(name);
  163. loose = loose.set(idx, value);
  164. return prior;
  165. } else {
  166. Ref prior = get(keyName);
  167. loose = loose.add(idx, value);
  168. sizeIsValid = false;
  169. return prior;
  170. }
  171. }
  172. @Override
  173. public Ref remove(Object key) {
  174. String name = toRefName((String) key);
  175. Ref res = null;
  176. int idx;
  177. if (0 <= (idx = packed.find(name))) {
  178. res = packed.get(name);
  179. packed = packed.remove(idx);
  180. sizeIsValid = false;
  181. }
  182. if (0 <= (idx = loose.find(name))) {
  183. res = loose.get(name);
  184. loose = loose.remove(idx);
  185. sizeIsValid = false;
  186. }
  187. if (0 <= (idx = resolved.find(name))) {
  188. res = resolved.get(name);
  189. resolved = resolved.remove(idx);
  190. sizeIsValid = false;
  191. }
  192. return res;
  193. }
  194. @Override
  195. public boolean isEmpty() {
  196. return entrySet().isEmpty();
  197. }
  198. @Override
  199. public Set<Entry<String, Ref>> entrySet() {
  200. if (entrySet == null) {
  201. entrySet = new AbstractSet<Entry<String, Ref>>() {
  202. @Override
  203. public Iterator<Entry<String, Ref>> iterator() {
  204. return new SetIterator();
  205. }
  206. @Override
  207. public int size() {
  208. if (!sizeIsValid) {
  209. size = 0;
  210. Iterator<?> i = entrySet().iterator();
  211. for (; i.hasNext(); i.next())
  212. size++;
  213. sizeIsValid = true;
  214. }
  215. return size;
  216. }
  217. @Override
  218. public boolean isEmpty() {
  219. if (sizeIsValid)
  220. return 0 == size;
  221. return !iterator().hasNext();
  222. }
  223. @Override
  224. public void clear() {
  225. packed = RefList.emptyList();
  226. loose = RefList.emptyList();
  227. resolved = RefList.emptyList();
  228. size = 0;
  229. sizeIsValid = true;
  230. }
  231. };
  232. }
  233. return entrySet;
  234. }
  235. @Override
  236. public String toString() {
  237. StringBuilder r = new StringBuilder();
  238. boolean first = true;
  239. r.append('[');
  240. for (Ref ref : values()) {
  241. if (first)
  242. first = false;
  243. else
  244. r.append(", ");
  245. r.append(ref);
  246. }
  247. r.append(']');
  248. return r.toString();
  249. }
  250. private String toRefName(String name) {
  251. if (0 < prefix.length())
  252. name = prefix + name;
  253. return name;
  254. }
  255. private String toMapKey(Ref ref) {
  256. String name = ref.getName();
  257. if (0 < prefix.length())
  258. name = name.substring(prefix.length());
  259. return name;
  260. }
  261. private class SetIterator implements Iterator<Entry<String, Ref>> {
  262. private int packedIdx;
  263. private int looseIdx;
  264. private int resolvedIdx;
  265. private Entry<String, Ref> next;
  266. SetIterator() {
  267. if (0 < prefix.length()) {
  268. packedIdx = -(packed.find(prefix) + 1);
  269. looseIdx = -(loose.find(prefix) + 1);
  270. resolvedIdx = -(resolved.find(prefix) + 1);
  271. }
  272. }
  273. public boolean hasNext() {
  274. if (next == null)
  275. next = peek();
  276. return next != null;
  277. }
  278. public Entry<String, Ref> next() {
  279. if (hasNext()) {
  280. Entry<String, Ref> r = next;
  281. next = peek();
  282. return r;
  283. }
  284. throw new NoSuchElementException();
  285. }
  286. public Entry<String, Ref> peek() {
  287. if (packedIdx < packed.size() && looseIdx < loose.size()) {
  288. Ref p = packed.get(packedIdx);
  289. Ref l = loose.get(looseIdx);
  290. int cmp = RefComparator.compareTo(p, l);
  291. if (cmp < 0) {
  292. packedIdx++;
  293. return toEntry(p);
  294. }
  295. if (cmp == 0)
  296. packedIdx++;
  297. looseIdx++;
  298. return toEntry(resolveLoose(l));
  299. }
  300. if (looseIdx < loose.size())
  301. return toEntry(resolveLoose(loose.get(looseIdx++)));
  302. if (packedIdx < packed.size())
  303. return toEntry(packed.get(packedIdx++));
  304. return null;
  305. }
  306. private Ref resolveLoose(final Ref l) {
  307. if (resolvedIdx < resolved.size()) {
  308. Ref r = resolved.get(resolvedIdx);
  309. int cmp = RefComparator.compareTo(l, r);
  310. if (cmp == 0) {
  311. resolvedIdx++;
  312. return r;
  313. } else if (cmp > 0) {
  314. // WTF, we have a symbolic entry but no match
  315. // in the loose collection. That's an error.
  316. throw new IllegalStateException();
  317. }
  318. }
  319. return l;
  320. }
  321. private Ent toEntry(Ref p) {
  322. if (p.getName().startsWith(prefix))
  323. return new Ent(p);
  324. packedIdx = packed.size();
  325. looseIdx = loose.size();
  326. resolvedIdx = resolved.size();
  327. return null;
  328. }
  329. public void remove() {
  330. throw new UnsupportedOperationException();
  331. }
  332. }
  333. private class Ent implements Entry<String, Ref> {
  334. private Ref ref;
  335. Ent(Ref ref) {
  336. this.ref = ref;
  337. }
  338. public String getKey() {
  339. return toMapKey(ref);
  340. }
  341. public Ref getValue() {
  342. return ref;
  343. }
  344. public Ref setValue(Ref value) {
  345. Ref prior = put(getKey(), value);
  346. ref = value;
  347. return prior;
  348. }
  349. @Override
  350. public int hashCode() {
  351. return getKey().hashCode();
  352. }
  353. @Override
  354. public boolean equals(Object obj) {
  355. if (obj instanceof Map.Entry) {
  356. final Object key = ((Map.Entry) obj).getKey();
  357. final Object val = ((Map.Entry) obj).getValue();
  358. if (key instanceof String && val instanceof Ref) {
  359. final Ref r = (Ref) val;
  360. if (r.getName().equals(ref.getName())) {
  361. final ObjectId a = r.getObjectId();
  362. final ObjectId b = ref.getObjectId();
  363. if (a != null && b != null && AnyObjectId.equals(a, b))
  364. return true;
  365. }
  366. }
  367. }
  368. return false;
  369. }
  370. @Override
  371. public String toString() {
  372. return ref.toString();
  373. }
  374. }
  375. }