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.

ConfigSnapshot.java 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. * Copyright (C) 2010, Mathias Kinzler <mathias.kinzler@sap.com>
  3. * Copyright (C) 2009, Constantine Plotnikov <constantine.plotnikov@gmail.com>
  4. * Copyright (C) 2007, Dave Watson <dwatson@mimvista.com>
  5. * Copyright (C) 2008-2012, Google Inc.
  6. * Copyright (C) 2009, JetBrains s.r.o.
  7. * Copyright (C) 2007-2008, Robin Rosenberg <robin.rosenberg@dewire.com>
  8. * Copyright (C) 2006-2008, Shawn O. Pearce <spearce@spearce.org>
  9. * Copyright (C) 2008, Thad Hughes <thadh@thad.corp.google.com>
  10. * and other copyright owners as documented in the project's IP log.
  11. *
  12. * This program and the accompanying materials are made available
  13. * under the terms of the Eclipse Distribution License v1.0 which
  14. * accompanies this distribution, is reproduced below, and is
  15. * available at http://www.eclipse.org/org/documents/edl-v10.php
  16. *
  17. * All rights reserved.
  18. *
  19. * Redistribution and use in source and binary forms, with or
  20. * without modification, are permitted provided that the following
  21. * conditions are met:
  22. *
  23. * - Redistributions of source code must retain the above copyright
  24. * notice, this list of conditions and the following disclaimer.
  25. *
  26. * - Redistributions in binary form must reproduce the above
  27. * copyright notice, this list of conditions and the following
  28. * disclaimer in the documentation and/or other materials provided
  29. * with the distribution.
  30. *
  31. * - Neither the name of the Eclipse Foundation, Inc. nor the
  32. * names of its contributors may be used to endorse or promote
  33. * products derived from this software without specific prior
  34. * written permission.
  35. *
  36. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  37. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  38. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  39. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  40. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  41. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  42. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  43. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  44. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  45. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  46. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  47. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  48. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  49. */
  50. package org.eclipse.jgit.lib;
  51. import static org.eclipse.jgit.util.StringUtils.compareIgnoreCase;
  52. import static org.eclipse.jgit.util.StringUtils.compareWithCase;
  53. import static org.eclipse.jgit.util.StringUtils.toLowerCase;
  54. import java.util.AbstractSet;
  55. import java.util.ArrayList;
  56. import java.util.Collections;
  57. import java.util.Comparator;
  58. import java.util.HashMap;
  59. import java.util.Iterator;
  60. import java.util.LinkedHashMap;
  61. import java.util.LinkedHashSet;
  62. import java.util.List;
  63. import java.util.Map;
  64. import java.util.Set;
  65. import java.util.concurrent.ConcurrentHashMap;
  66. import org.eclipse.jgit.util.StringUtils;
  67. class ConfigSnapshot {
  68. final List<ConfigLine> entryList;
  69. final Map<Object, Object> cache;
  70. final ConfigSnapshot baseState;
  71. volatile List<ConfigLine> sorted;
  72. volatile SectionNames names;
  73. ConfigSnapshot(List<ConfigLine> entries, ConfigSnapshot base) {
  74. entryList = entries;
  75. cache = new ConcurrentHashMap<Object, Object>(16, 0.75f, 1);
  76. baseState = base;
  77. }
  78. Set<String> getSections() {
  79. return names().sections;
  80. }
  81. Set<String> getSubsections(String section) {
  82. Map<String, Set<String>> m = names().subsections;
  83. Set<String> r = m.get(section);
  84. if (r == null)
  85. r = m.get(toLowerCase(section));
  86. if (r == null)
  87. return Collections.emptySet();
  88. return Collections.unmodifiableSet(r);
  89. }
  90. Set<String> getNames(String section, String subsection) {
  91. return getNames(section, subsection, false);
  92. }
  93. Set<String> getNames(String section, String subsection, boolean recursive) {
  94. Map<String, String> m = getNamesInternal(section, subsection, recursive);
  95. return new CaseFoldingSet(m);
  96. }
  97. private Map<String, String> getNamesInternal(String section,
  98. String subsection, boolean recursive) {
  99. List<ConfigLine> s = sorted();
  100. int idx = find(s, section, subsection, ""); //$NON-NLS-1$
  101. if (idx < 0)
  102. idx = -(idx + 1);
  103. Map<String, String> m = new LinkedHashMap<String, String>();
  104. while (idx < s.size()) {
  105. ConfigLine e = s.get(idx++);
  106. if (!e.match(section, subsection))
  107. break;
  108. if (e.name == null)
  109. continue;
  110. String l = toLowerCase(e.name);
  111. if (!m.containsKey(l))
  112. m.put(l, e.name);
  113. }
  114. if (recursive && baseState != null)
  115. m.putAll(baseState.getNamesInternal(section, subsection, recursive));
  116. return m;
  117. }
  118. String[] get(String section, String subsection, String name) {
  119. List<ConfigLine> s = sorted();
  120. int idx = find(s, section, subsection, name);
  121. if (idx < 0)
  122. return null;
  123. int end = end(s, idx, section, subsection, name);
  124. String[] r = new String[end - idx];
  125. for (int i = 0; idx < end;)
  126. r[i++] = s.get(idx++).value;
  127. return r;
  128. }
  129. private int find(List<ConfigLine> s, String s1, String s2, String name) {
  130. int low = 0;
  131. int high = s.size();
  132. while (low < high) {
  133. int mid = (low + high) >>> 1;
  134. ConfigLine e = s.get(mid);
  135. int cmp = compare2(
  136. s1, s2, name,
  137. e.section, e.subsection, e.name);
  138. if (cmp < 0)
  139. high = mid;
  140. else if (cmp == 0)
  141. return first(s, mid, s1, s2, name);
  142. else
  143. low = mid + 1;
  144. }
  145. return -(low + 1);
  146. }
  147. private int first(List<ConfigLine> s, int i, String s1, String s2, String n) {
  148. while (0 < i) {
  149. if (s.get(i - 1).match(s1, s2, n))
  150. i--;
  151. else
  152. return i;
  153. }
  154. return i;
  155. }
  156. private int end(List<ConfigLine> s, int i, String s1, String s2, String n) {
  157. while (i < s.size()) {
  158. if (s.get(i).match(s1, s2, n))
  159. i++;
  160. else
  161. return i;
  162. }
  163. return i;
  164. }
  165. private List<ConfigLine> sorted() {
  166. List<ConfigLine> r = sorted;
  167. if (r == null)
  168. sorted = r = sort(entryList);
  169. return r;
  170. }
  171. private static List<ConfigLine> sort(List<ConfigLine> in) {
  172. List<ConfigLine> sorted = new ArrayList<ConfigLine>(in.size());
  173. for (ConfigLine line : in) {
  174. if (line.section != null && line.name != null)
  175. sorted.add(line);
  176. }
  177. Collections.sort(sorted, new LineComparator());
  178. return sorted;
  179. }
  180. private static int compare2(
  181. String aSection, String aSubsection, String aName,
  182. String bSection, String bSubsection, String bName) {
  183. int c = compareIgnoreCase(aSection, bSection);
  184. if (c != 0)
  185. return c;
  186. if (aSubsection == null && bSubsection != null)
  187. return -1;
  188. if (aSubsection != null && bSubsection == null)
  189. return 1;
  190. if (aSubsection != null) {
  191. c = compareWithCase(aSubsection, bSubsection);
  192. if (c != 0)
  193. return c;
  194. }
  195. return compareIgnoreCase(aName, bName);
  196. }
  197. private static class LineComparator implements Comparator<ConfigLine> {
  198. public int compare(ConfigLine a, ConfigLine b) {
  199. return compare2(
  200. a.section, a.subsection, a.name,
  201. b.section, b.subsection, b.name);
  202. }
  203. }
  204. private SectionNames names() {
  205. SectionNames n = names;
  206. if (n == null)
  207. names = n = new SectionNames(this);
  208. return n;
  209. }
  210. private static class SectionNames {
  211. final CaseFoldingSet sections;
  212. final Map<String, Set<String>> subsections;
  213. SectionNames(ConfigSnapshot cfg) {
  214. Map<String, String> sec = new LinkedHashMap<String, String>();
  215. Map<String, Set<String>> sub = new HashMap<String, Set<String>>();
  216. while (cfg != null) {
  217. for (ConfigLine e : cfg.entryList) {
  218. if (e.section == null)
  219. continue;
  220. String l1 = toLowerCase(e.section);
  221. if (!sec.containsKey(l1))
  222. sec.put(l1, e.section);
  223. if (e.subsection == null)
  224. continue;
  225. Set<String> m = sub.get(l1);
  226. if (m == null) {
  227. m = new LinkedHashSet<String>();
  228. sub.put(l1, m);
  229. }
  230. m.add(e.subsection);
  231. }
  232. cfg = cfg.baseState;
  233. }
  234. sections = new CaseFoldingSet(sec);
  235. subsections = sub;
  236. }
  237. }
  238. private static class CaseFoldingSet extends AbstractSet<String> {
  239. private final Map<String, String> names;
  240. CaseFoldingSet(Map<String, String> names) {
  241. this.names = names;
  242. }
  243. @Override
  244. public boolean contains(Object needle) {
  245. if (needle instanceof String) {
  246. String n = (String) needle;
  247. return names.containsKey(n)
  248. || names.containsKey(StringUtils.toLowerCase(n));
  249. }
  250. return false;
  251. }
  252. @Override
  253. public Iterator<String> iterator() {
  254. final Iterator<String> i = names.values().iterator();
  255. return new Iterator<String>() {
  256. public boolean hasNext() {
  257. return i.hasNext();
  258. }
  259. public String next() {
  260. return i.next();
  261. }
  262. public void remove() {
  263. throw new UnsupportedOperationException();
  264. }
  265. };
  266. }
  267. @Override
  268. public int size() {
  269. return names.size();
  270. }
  271. }
  272. }